Java Code Examples for org.apache.calcite.util.Util

The following examples show how to use org.apache.calcite.util.Util. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: calcite   Source File: MongoTable.java    License: Apache License 2.0 6 votes vote down vote up
/** Executes an "aggregate" operation on the underlying collection.
 *
 * <p>For example:
 * <code>zipsTable.aggregate(
 * "{$filter: {state: 'OR'}",
 * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}")
 * </code></p>
 *
 * @param mongoDb MongoDB connection
 * @param fields List of fields to project; or null to return map
 * @param operations One or more JSON strings
 * @return Enumerator of results
 */
private Enumerable<Object> aggregate(final MongoDatabase mongoDb,
    final List<Map.Entry<String, Class>> fields,
    final List<String> operations) {
  final List<Bson> list = new ArrayList<>();
  for (String operation : operations) {
    list.add(BsonDocument.parse(operation));
  }
  final Function1<Document, Object> getter =
      MongoEnumerator.getter(fields);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      final Iterator<Document> resultIterator;
      try {
        resultIterator = mongoDb.getCollection(collectionName)
            .aggregate(list).iterator();
      } catch (Exception e) {
        throw new RuntimeException("While running MongoDB query "
            + Util.toString(operations, "[", ",\n", "]"), e);
      }
      return new MongoEnumerator(resultIterator, getter);
    }
  };
}
 
Example 2
Source Project: calcite   Source File: RexUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any
 * {@link org.apache.calcite.rex.RexFieldAccess} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsFieldAccess(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitFieldAccess(RexFieldAccess fieldAccess) {
            throw new Util.FoundOne(fieldAccess);
          }
        };
    node.accept(visitor);
    return false;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return true;
  }
}
 
Example 3
Source Project: calcite   Source File: DruidRules.java    License: Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final DruidQuery query = call.rel(1);
  if (!DruidQuery.isValidSignature(query.signature() + 'l')) {
    return;
  }
  // Either it is:
  // - a pure limit above a query of type scan
  // - a sort and limit on a dimension/metric part of the druid group by query
  if (sort.offset != null && RexLiteral.intValue(sort.offset) != 0) {
    // offset not supported by Druid
    return;
  }
  if (query.getQueryType() == QueryType.SCAN && !RelOptUtil.isPureLimit(sort)) {
    return;
  }

  final RelNode newSort = sort
      .copy(sort.getTraitSet(), ImmutableList.of(Util.last(query.rels)));
  call.transformTo(DruidQuery.extendQuery(query, newSort));
}
 
Example 4
Source Project: calcite   Source File: MaterializedViewRule.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all the input references by the position in the
 * input column set. If a reference index cannot be found in
 * the input set, then we return null.
 */
protected RexNode shuttleReferences(final RexBuilder rexBuilder,
    final RexNode node, final Mapping mapping) {
  try {
    RexShuttle visitor =
        new RexShuttle() {
          @Override public RexNode visitInputRef(RexInputRef inputRef) {
            int pos = mapping.getTargetOpt(inputRef.getIndex());
            if (pos != -1) {
              // Found it
              return rexBuilder.makeInputRef(inputRef.getType(), pos);
            }
            throw Util.FoundOne.NULL;
          }
        };
    return visitor.apply(node);
  } catch (Util.FoundOne ex) {
    Util.swallow(ex, null);
    return null;
  }
}
 
Example 5
Source Project: Bats   Source File: RelOptTableImpl.java    License: Apache License 2.0 6 votes vote down vote up
/** Helper for {@link #getColumnStrategies()}. */
public static List<ColumnStrategy> columnStrategies(final RelOptTable table) {
    final int fieldCount = table.getRowType().getFieldCount();
    final InitializerExpressionFactory ief = Util.first(table.unwrap(InitializerExpressionFactory.class),
            NullInitializerExpressionFactory.INSTANCE);
    return new AbstractList<ColumnStrategy>() {
        @Override
        public int size() {
            return fieldCount;
        }

        @Override
        public ColumnStrategy get(int index) {
            return ief.generationStrategy(table, index);
        }
    };
}
 
Example 6
Source Project: Bats   Source File: DateRangeRules.java    License: Apache License 2.0 6 votes vote down vote up
private RexLiteral dateTimeLiteral(RexBuilder rexBuilder, Calendar calendar, RexNode operand) {
    final TimestampString ts;
    final int p;
    switch (operand.getType().getSqlTypeName()) {
    case TIMESTAMP:
        ts = TimestampString.fromCalendarFields(calendar);
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampLiteral(ts, p);
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        ts = TimestampString.fromCalendarFields(calendar);
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        final TimestampString localTs = new TimestampWithTimeZoneString(ts, tz)
                .withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(localTs, p);
    case DATE:
        final DateString d = DateString.fromCalendarFields(calendar);
        return rexBuilder.makeDateLiteral(d);
    default:
        throw Util.unexpected(operand.getType().getSqlTypeName());
    }
}
 
Example 7
Source Project: Bats   Source File: SubQueryRemoveRule.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites an EXISTS RexSubQuery into a {@link Join}.
 *
 * @param e            EXISTS sub-query to rewrite
 * @param variablesSet A set of variables used by a relational
 *                     expression of the specified RexSubQuery
 * @param logic        Logic for evaluating
 * @param builder      Builder
 *
 * @return Expression that may be used to replace the RexSubQuery
 */
private RexNode rewriteExists(RexSubQuery e, Set<CorrelationId> variablesSet, RelOptUtil.Logic logic,
        RelBuilder builder) {
    builder.push(e.getRel());

    builder.project(builder.alias(builder.literal(true), "i"));
    switch (logic) {
    case TRUE:
        // Handles queries with single EXISTS in filter condition:
        // select e.deptno from emp as e
        // where exists (select deptno from emp)
        builder.aggregate(builder.groupKey(0));
        builder.as("dt");
        builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
        return builder.literal(true);
    default:
        builder.distinct();
    }

    builder.as("dt");

    builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);

    return builder.isNotNull(Util.last(builder.fields()));
}
 
Example 8
Source Project: Bats   Source File: SqlValidatorUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Finds a {@link org.apache.calcite.schema.CalciteSchema.TableEntry} in a
 * given catalog reader whose table has the given name, possibly qualified.
 *
 * <p>Uses the case-sensitivity policy of the specified catalog reader.
 *
 * <p>If not found, returns null.
 *
 * @param catalogReader accessor to the table metadata
 * @param names Name of table, may be qualified or fully-qualified
 *
 * @return TableEntry with a table with the given name, or null
 */
public static CalciteSchema.TableEntry getTableEntry(
    SqlValidatorCatalogReader catalogReader, List<String> names) {
  // First look in the default schema, if any.
  // If not found, look in the root schema.
  for (List<String> schemaPath : catalogReader.getSchemaPaths()) {
    CalciteSchema schema =
        getSchema(catalogReader.getRootSchema(),
            Iterables.concat(schemaPath, Util.skipLast(names)),
            catalogReader.nameMatcher());
    if (schema == null) {
      continue;
    }
    CalciteSchema.TableEntry entry =
        getTableEntryFrom(schema, Util.last(names),
            catalogReader.nameMatcher().isCaseSensitive());
    if (entry != null) {
      return entry;
    }
  }
  return null;
}
 
Example 9
Source Project: calcite   Source File: RexUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexTableInputRef} nodes.
 *
 * @param node a RexNode tree
 * @return first such node found or null if it there is no such node
 */
public static RexTableInputRef containsTableInputRef(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitTableInputRef(RexTableInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (RexTableInputRef) e.getNode();
  }
}
 
Example 10
Source Project: calcite   Source File: CsvTest.java    License: Apache License 2.0 6 votes vote down vote up
private static void collect(List<String> result, ResultSet resultSet)
    throws SQLException {
  final StringBuilder buf = new StringBuilder();
  while (resultSet.next()) {
    buf.setLength(0);
    int n = resultSet.getMetaData().getColumnCount();
    String sep = "";
    for (int i = 1; i <= n; i++) {
      buf.append(sep)
          .append(resultSet.getMetaData().getColumnLabel(i))
          .append("=")
          .append(resultSet.getString(i));
      sep = "; ";
    }
    result.add(Util.toLinux(buf.toString()));
  }
}
 
Example 11
Source Project: calcite   Source File: DruidQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override public RelOptCost computeSelfCost(RelOptPlanner planner,
    RelMetadataQuery mq) {
  return Util.last(rels)
      .computeSelfCost(planner, mq)
      // Cost increases with the number of fields queried.
      // A plan returning 100 or more columns will have 2x the cost of a
      // plan returning 2 columns.
      // A plan where all extra columns are pruned will be preferred.
      .multiplyBy(
          RelMdUtil.linear(querySpec.fieldNames.size(), 2, 100, 1d, 2d))
      .multiplyBy(getQueryTypeCostMultiplier())
      // A Scan leaf filter is better than having filter spec if possible.
      .multiplyBy(rels.size() > 1 && rels.get(1) instanceof Filter ? 0.5 : 1.0)
      // a plan with sort pushed to druid is better than doing sort outside of druid
      .multiplyBy(Util.last(rels) instanceof Sort ? 0.1 : 1.0)
      .multiplyBy(getIntervalCostMultiplier());
}
 
Example 12
Source Project: Bats   Source File: RexUtil.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns whether a {@link Join} contains a sub-query. */
public static boolean containsSubQuery(Join join) {
    try {
        join.getCondition().accept(INSTANCE);
        return false;
    } catch (Util.FoundOne e) {
        return true;
    }
}
 
Example 13
Source Project: dremio-oss   Source File: SqlImplementor.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlNode createLeftCall(SqlOperator op, List<SqlNode> nodeList) {
  if (nodeList.size() == 2) {
    return op.createCall(new SqlNodeList(nodeList, POS));
  }
  final List<SqlNode> butLast = Util.skipLast(nodeList);
  final SqlNode last = nodeList.get(nodeList.size() - 1);
  final SqlNode call = createLeftCall(op, butLast);
  return op.createCall(new SqlNodeList(ImmutableList.of(call, last), POS));
}
 
Example 14
Source Project: calcite   Source File: JaninoRelMetadataProvider.java    License: Apache License 2.0 5 votes vote down vote up
synchronized <M extends Metadata, H extends MetadataHandler<M>> H create(
    MetadataDef<M> def) {
  try {
    final Key key = new Key((MetadataDef) def, provider,
        ImmutableList.copyOf(ALL_RELS));
    //noinspection unchecked
    return (H) HANDLERS.get(key);
  } catch (UncheckedExecutionException | ExecutionException e) {
    Util.throwIfUnchecked(e.getCause());
    throw new RuntimeException(e.getCause());
  }
}
 
Example 15
Source Project: calcite   Source File: PlannerTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Tests that Hive dialect does not generate "AS". */
@Test void testHiveDialect() throws SqlParseException {
  Planner planner = getPlanner(null);
  SqlNode parse = planner.parse(
      "select * from (select * from \"emps\") as t\n"
          + "where \"name\" like '%e%'");
  final SqlDialect hiveDialect =
      SqlDialect.DatabaseProduct.HIVE.getDialect();
  assertThat(Util.toLinux(parse.toSqlString(hiveDialect).getSql()),
      equalTo("SELECT *\n"
          + "FROM (SELECT *\n"
          + "FROM emps) T\n"
          + "WHERE name LIKE '%e%'"));
}
 
Example 16
Source Project: calcite   Source File: DruidRules.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new List of RelNodes in the order of the given order of the oldNodes,
 * the given {@link Filter}, and any extra nodes.
 */
private static List<RelNode> constructNewNodes(List<RelNode> oldNodes,
    boolean addFilter, int startIndex, RelNode filter, RelNode... trailingNodes) {
  List<RelNode> newNodes = new ArrayList<>();

  // The first item should always be the Table scan, so any filter would go after that
  newNodes.add(oldNodes.get(0));

  if (addFilter) {
    newNodes.add(filter);
    // This is required so that each RelNode is linked to the one before it
    if (startIndex < oldNodes.size()) {
      RelNode next = oldNodes.get(startIndex);
      newNodes.add(next.copy(next.getTraitSet(), Collections.singletonList(filter)));
      startIndex++;
    }
  }

  // Add the rest of the nodes from oldNodes
  for (int i = startIndex; i < oldNodes.size(); i++) {
    newNodes.add(oldNodes.get(i));
  }

  // Add the trailing nodes (need to link them)
  for (RelNode node : trailingNodes) {
    newNodes.add(node.copy(node.getTraitSet(), Collections.singletonList(Util.last(newNodes))));
  }

  return newNodes;
}
 
Example 17
Source Project: calcite   Source File: SqlIdentifier.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an identifier that consists of this identifier plus a wildcard star.
 * Does not modify this identifier.
 */
public SqlIdentifier plusStar() {
  final SqlIdentifier id = this.plus("*", SqlParserPos.ZERO);
  return new SqlIdentifier(
      id.names.stream().map(s -> s.equals("*") ? "" : s)
          .collect(Util.toImmutableList()),
      null, id.pos, id.componentPositions);
}
 
Example 18
Source Project: Bats   Source File: SqlAdvisorValidator.java    License: Apache License 2.0 5 votes vote down vote up
protected void validateOver(SqlCall call, SqlValidatorScope scope) {
  try {
    final OverScope overScope = (OverScope) getOverScope(call);
    final SqlNode relation = call.operand(0);
    validateFrom(relation, unknownType, scope);
    final SqlNode window = call.operand(1);
    SqlValidatorScope opScope = scopes.get(relation);
    if (opScope == null) {
      opScope = overScope;
    }
    validateWindow(window, opScope, null);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example 19
Source Project: calcite   Source File: SqlValidatorUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void getSchemaObjectMonikers(
    SqlValidatorCatalogReader catalogReader,
    List<String> names,
    List<SqlMoniker> hints) {
  // Assume that the last name is 'dummy' or similar.
  List<String> subNames = Util.skipLast(names);

  // Try successively with catalog.schema, catalog and no prefix
  for (List<String> x : catalogReader.getSchemaPaths()) {
    final List<String> names2 =
        ImmutableList.<String>builder().addAll(x).addAll(subNames).build();
    hints.addAll(catalogReader.getAllSchemaObjectNames(names2));
  }
}
 
Example 20
Source Project: calcite   Source File: EnumerableRepeatUnion.java    License: Apache License 2.0 5 votes vote down vote up
@Override public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    // return repeatUnion(<seedExp>, <iterativeExp>, iterationLimit, all, <comparer>);

    BlockBuilder builder = new BlockBuilder();
    RelNode seed = getSeedRel();
    RelNode iteration = getIterativeRel();

    Result seedResult = implementor.visitChild(this, 0, (EnumerableRel) seed, pref);
    Result iterationResult = implementor.visitChild(this, 1, (EnumerableRel) iteration, pref);

    Expression seedExp = builder.append("seed", seedResult.block);
    Expression iterativeExp = builder.append("iteration", iterationResult.block);

    PhysType physType = PhysTypeImpl.of(
        implementor.getTypeFactory(),
        getRowType(),
        pref.prefer(seedResult.format));

    Expression unionExp = Expressions.call(
        BuiltInMethod.REPEAT_UNION.method,
        seedExp,
        iterativeExp,
        Expressions.constant(iterationLimit, int.class),
        Expressions.constant(all, boolean.class),
        Util.first(physType.comparer(), Expressions.call(BuiltInMethod.IDENTITY_COMPARER.method)));
    builder.add(unionExp);

    return implementor.result(physType, builder.toBlock());
  }
 
Example 21
Source Project: Bats   Source File: RexOver.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether an expression contains an OVER clause.
 */
public static boolean containsOver(RexNode expr) {
    try {
        expr.accept(FINDER);
        return false;
    } catch (OverFound e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 22
Source Project: calcite   Source File: SqlNodeToRexConverterImpl.java    License: Apache License 2.0 5 votes vote down vote up
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  final SqlRexConvertlet convertlet = convertletTable.get(call);
  if (convertlet != null) {
    return convertlet.convertCall(cx, call);
  }

  // No convertlet was suitable. (Unlikely, because the standard
  // convertlet table has a fall-back for all possible calls.)
  throw Util.needToImplement(call);
}
 
Example 23
Source Project: calcite   Source File: DiffRepository.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Flushes the reference document to the file system.
 */
private void flushDoc() {
  try {
    boolean b = logFile.getParentFile().mkdirs();
    Util.discard(b);
    try (Writer w = Util.printWriter(logFile)) {
      write(doc, w, indent);
    }
  } catch (IOException e) {
    throw new RuntimeException("error while writing test reference log '"
        + logFile + "'", e);
  }
}
 
Example 24
Source Project: calcite   Source File: RexExecutorImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example 25
Source Project: calcite   Source File: AliasNamespace.java    License: Apache License 2.0 5 votes vote down vote up
protected RelDataType validateImpl(RelDataType targetRowType) {
  final List<String> nameList = new ArrayList<>();
  final List<SqlNode> operands = call.getOperandList();
  final SqlValidatorNamespace childNs =
      validator.getNamespace(operands.get(0));
  final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
  final List<SqlNode> columnNames = Util.skip(operands, 2);
  for (final SqlNode operand : columnNames) {
    String name = ((SqlIdentifier) operand).getSimple();
    if (nameList.contains(name)) {
      throw validator.newValidationError(operand,
          RESOURCE.aliasListDuplicate(name));
    }
    nameList.add(name);
  }
  if (nameList.size() != rowType.getFieldCount()) {
    // Position error over all column names
    final SqlNode node = operands.size() == 3
        ? operands.get(2)
        : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
    throw validator.newValidationError(node,
        RESOURCE.aliasListDegree(rowType.getFieldCount(), getString(rowType),
            nameList.size()));
  }
  final List<RelDataType> typeList = new ArrayList<>();
  for (RelDataTypeField field : rowType.getFieldList()) {
    typeList.add(field.getType());
  }
  return validator.getTypeFactory().createStructType(
      typeList,
      nameList);
}
 
Example 26
Source Project: calcite   Source File: SqlLiteral.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a numeric literal's value as a {@link BigDecimal}.
 */
public BigDecimal bigDecimalValue() {
  switch (typeName) {
  case DECIMAL:
  case DOUBLE:
    return (BigDecimal) value;
  default:
    throw Util.unexpected(typeName);
  }
}
 
Example 27
Source Project: calcite   Source File: SqlValidatorUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Checks that there are no duplicates in a list of {@link SqlIdentifier}.
 */
static void checkIdentifierListForDuplicates(List<SqlNode> columnList,
    SqlValidatorImpl.ValidationErrorFunction validationErrorFunction) {
  final List<List<String>> names = Lists.transform(columnList,
      o -> ((SqlIdentifier) o).names);
  final int i = Util.firstDuplicate(names);
  if (i >= 0) {
    throw validationErrorFunction.apply(columnList.get(i),
        RESOURCE.duplicateNameInColumnList(Util.last(names.get(i))));
  }
}
 
Example 28
Source Project: Bats   Source File: HepPlanner.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new HepPlanner with the option to keep the graph a
 * tree (noDag = true) or allow DAG (noDag = false).
 *
 * @param noDag      If false, create shared nodes if expressions are
 *                   identical
 * @param program    Program controlling rule application
 * @param onCopyHook Function to call when a node is copied
 */
public HepPlanner(
    HepProgram program,
    Context context,
    boolean noDag,
    Function2<RelNode, RelNode, Void> onCopyHook,
    RelOptCostFactory costFactory) {
  super(costFactory, context);
  this.mainProgram = program;
  this.onCopyHook = Util.first(onCopyHook, Functions.ignore2());
  this.noDag = noDag;
}
 
Example 29
Source Project: calcite   Source File: TableNamespace.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures that extended columns that have the same name as a base column also
 * have the same data-type.
 */
private void checkExtendedColumnTypes(SqlNodeList extendList) {
  final List<RelDataTypeField> extendedFields =
      SqlValidatorUtil.getExtendedColumns(validator, table, extendList);
  final List<RelDataTypeField> baseFields =
      getBaseRowType().getFieldList();
  final Map<String, Integer> nameToIndex =
      SqlValidatorUtil.mapNameToIndex(baseFields);

  for (final RelDataTypeField extendedField : extendedFields) {
    final String extFieldName = extendedField.getName();
    if (nameToIndex.containsKey(extFieldName)) {
      final Integer baseIndex = nameToIndex.get(extFieldName);
      final RelDataType baseType = baseFields.get(baseIndex).getType();
      final RelDataType extType = extendedField.getType();

      if (!extType.equals(baseType)) {
        // Get the extended column node that failed validation.
        final SqlNode extColNode =
            Iterables.find(extendList.getList(),
                sqlNode -> sqlNode instanceof SqlIdentifier
                    && Util.last(((SqlIdentifier) sqlNode).names).equals(
                        extendedField.getName()));

        throw validator.getValidationErrorFunction().apply(extColNode,
            RESOURCE.typeNotAssignable(
                baseFields.get(baseIndex).getName(), baseType.getFullTypeString(),
                extendedField.getName(), extType.getFullTypeString()));
      }
    }
  }
}
 
Example 30
Source Project: calcite   Source File: RexImpTable.java    License: Apache License 2.0 5 votes vote down vote up
private Expression implementRecurse(RexToLixTranslator translator,
    final List<Expression> argValueList) {
  if (argValueList.size() == 1) {
    return argValueList.get(0);
  } else {
    return Expressions.condition(
        translator.checkNotNull(argValueList.get(0)),
        argValueList.get(0),
        implementRecurse(translator, Util.skip(argValueList)));
  }
}