org.apache.calcite.sql.validate.SqlValidatorUtil Java Examples

The following examples show how to use org.apache.calcite.sql.validate.SqlValidatorUtil. 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 File: SqlTypeUtil.java    From Bats with Apache License 2.0 7 votes vote down vote up
/**
 * Adds collation and charset to a character type, returns other types
 * unchanged.
 *
 * @param type        Type
 * @param typeFactory Type factory
 * @return Type with added charset and collation, or unchanged type if it is
 * not a char type.
 */
public static RelDataType addCharsetAndCollation(
    RelDataType type,
    RelDataTypeFactory typeFactory) {
  if (!inCharFamily(type)) {
    return type;
  }
  Charset charset = type.getCharset();
  if (charset == null) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = type.getCollation();
  if (collation == null) {
    collation = SqlCollation.IMPLICIT;
  }

  // todo: should get the implicit collation from repository
  //   instead of null
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(type);
  return type;
}
 
Example #2
Source File: Lattice.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static void populateAliases(SqlNode from, List<String> aliases,
    String current) {
  if (from instanceof SqlJoin) {
    SqlJoin join = (SqlJoin) from;
    populateAliases(join.getLeft(), aliases, null);
    populateAliases(join.getRight(), aliases, null);
  } else if (from.getKind() == SqlKind.AS) {
    populateAliases(SqlUtil.stripAs(from), aliases,
        SqlValidatorUtil.getAlias(from, -1));
  } else {
    if (current == null) {
      current = SqlValidatorUtil.getAlias(from, -1);
    }
    aliases.add(current);
  }
}
 
Example #3
Source File: Lattice.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static void populateAliases(SqlNode from, List<String> aliases,
    @Nullable String current) {
  if (from instanceof SqlJoin) {
    SqlJoin join = (SqlJoin) from;
    populateAliases(join.getLeft(), aliases, null);
    populateAliases(join.getRight(), aliases, null);
  } else if (from.getKind() == SqlKind.AS) {
    populateAliases(SqlUtil.stripAs(from), aliases,
        SqlValidatorUtil.getAlias(from, -1));
  } else {
    if (current == null) {
      current = SqlValidatorUtil.getAlias(from, -1);
    }
    aliases.add(current);
  }
}
 
Example #4
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a record type with specified field names.
 *
 * <p>The array of field names may be null, or any of the names within it
 * can be null. We recommend using explicit names where possible, because it
 * makes it much easier to figure out the intent of fields when looking at
 * planner output.
 *
 * @param typeFactory Type factory
 * @param exprs       Expressions
 * @param names       Field names, may be null, or elements may be null
 * @param suggester   Generates alternative names if {@code names} is not
 *                    null and its elements are not unique
 * @return Record type
 */
public static RelDataType createStructType(RelDataTypeFactory typeFactory, final List<? extends RexNode> exprs,
        List<String> names, SqlValidatorUtil.Suggester suggester) {
    if (names != null && suggester != null) {
        names = SqlValidatorUtil.uniquify(names, suggester, typeFactory.getTypeSystem().isSchemaCaseSensitive());
    }
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    for (int i = 0; i < exprs.size(); i++) {
        String name;
        if (names == null || (name = names.get(i)) == null) {
            name = "$f" + i;
        }
        builder.add(name, exprs.get(i).getType());
    }
    return builder.build();
}
 
Example #5
Source File: DrillLateralJoinRelBase.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
protected RelDataType deriveRowType() {
  switch (joinType) {
    case LEFT:
    case INNER:
      return constructRowType(SqlValidatorUtil.deriveJoinRowType(left.getRowType(),
        removeImplicitField(right.getRowType()), joinType.toJoinType(),
        getCluster().getTypeFactory(), null,
        ImmutableList.of()));
    case ANTI:
    case SEMI:
      return constructRowType(left.getRowType());
    default:
      throw new IllegalStateException("Unknown join type " + joinType);
  }
}
 
Example #6
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 6 votes vote down vote up
public PreparingTable getTable(final List<String> names) {
  // First look in the default schema, if any.
  // If not found, look in the root schema.
  CalciteSchema.TableEntry entry = SqlValidatorUtil.getTableEntry(this, names);
  if (entry != null) {
    final Table table = entry.getTable();
    if (table instanceof Wrapper) {
      final PreparingTable relOptTable =
          ((Wrapper) table).unwrap(PreparingTable.class);
      if (relOptTable != null) {
        return relOptTable;
      }
    }
    return RelOptTableImpl.create(this,
        table.getRowType(typeFactory), entry, null);
  }
  return null;
}
 
Example #7
Source File: RelBuilder.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates a {@link Project} of the given
 * expressions and field names, and optionally optimizing.
 *
 * <p>If {@code fieldNames} is null, or if a particular entry in
 * {@code fieldNames} is null, derives field names from the input
 * expressions.
 *
 * <p>If {@code force} is false,
 * and the input is a {@code Project},
 * and the expressions  make the trivial projection ($0, $1, ...),
 * modifies the input.
 *
 * @param nodes       Expressions
 * @param fieldNames  Suggested field names, or null to generate
 * @param force       Whether to create a renaming Project if the
 *                    projections are trivial
 */
public RelBuilder projectNamed(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    final List<? extends RexNode> nodeList = nodes instanceof List ? (List) nodes : ImmutableList.copyOf(nodes);
    final List<String> fieldNameList = fieldNames == null ? null
            : fieldNames instanceof List ? (List<String>) fieldNames : ImmutableNullableList.copyOf(fieldNames);
    final RelNode input = peek();
    final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), nodeList, fieldNameList,
            SqlValidatorUtil.F_SUGGESTER);
    if (!force && RexUtil.isIdentity(nodeList, input.getRowType())) {
        if (input instanceof Project && fieldNames != null) {
            // Rename columns of child projection if desired field names are given.
            final Frame frame = stack.pop();
            final Project childProject = (Project) frame.rel;
            final Project newInput = childProject.copy(childProject.getTraitSet(), childProject.getInput(),
                    childProject.getProjects(), rowType);
            stack.push(new Frame(newInput, frame.fields));
        }
    } else {
        project(nodeList, rowType.getFieldNames(), force);
    }
    return this;
}
 
Example #8
Source File: SqlCallBinding.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public int getGroupCount() {
  final SelectScope selectScope =
      SqlValidatorUtil.getEnclosingSelectScope(scope);
  if (selectScope == null) {
    // Probably "VALUES expr". Treat same as "SELECT expr GROUP BY ()"
    return 0;
  }
  final SqlSelect select = selectScope.getNode();
  final SqlNodeList group = select.getGroup();
  if (group != null) {
    int n = 0;
    for (SqlNode groupItem : group) {
      if (!(groupItem instanceof SqlNodeList)
          || ((SqlNodeList) groupItem).size() != 0) {
        ++n;
      }
    }
    return n;
  }
  return validator.isAggregate(select) ? 0 : -1;
}
 
Example #9
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Prepare.PreparingTable getTable(final List<String> names) {
  // First look in the default schema, if any.
  // If not found, look in the root schema.
  CalciteSchema.TableEntry entry = SqlValidatorUtil.getTableEntry(this, names);
  if (entry != null) {
    final Table table = entry.getTable();
    if (table instanceof Wrapper) {
      final Prepare.PreparingTable relOptTable =
          ((Wrapper) table).unwrap(Prepare.PreparingTable.class);
      if (relOptTable != null) {
        return relOptTable;
      }
    }
    return RelOptTableImpl.create(this,
        table.getRowType(typeFactory), entry, null);
  }
  return null;
}
 
Example #10
Source File: SqlCallBinding.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public int getGroupCount() {
  final SelectScope selectScope =
      SqlValidatorUtil.getEnclosingSelectScope(scope);
  if (selectScope == null) {
    // Probably "VALUES expr". Treat same as "SELECT expr GROUP BY ()"
    return 0;
  }
  final SqlSelect select = selectScope.getNode();
  final SqlNodeList group = select.getGroup();
  if (group != null) {
    int n = 0;
    for (SqlNode groupItem : group) {
      if (!(groupItem instanceof SqlNodeList)
          || ((SqlNodeList) groupItem).size() != 0) {
        ++n;
      }
    }
    return n;
  }
  return validator.isAggregate(select) ? 0 : -1;
}
 
Example #11
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a record type with specified field names.
 *
 * <p>The array of field names may be null, or any of the names within it
 * can be null. We recommend using explicit names where possible, because it
 * makes it much easier to figure out the intent of fields when looking at
 * planner output.
 *
 * @param typeFactory Type factory
 * @param exprs       Expressions
 * @param names       Field names, may be null, or elements may be null
 * @param suggester   Generates alternative names if {@code names} is not
 *                    null and its elements are not unique
 * @return Record type
 */
public static RelDataType createStructType(
    RelDataTypeFactory typeFactory,
    final List<? extends RexNode> exprs,
    List<String> names,
    SqlValidatorUtil.Suggester suggester) {
  if (names != null && suggester != null) {
    names = SqlValidatorUtil.uniquify(names, suggester,
        typeFactory.getTypeSystem().isSchemaCaseSensitive());
  }
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  for (int i = 0; i < exprs.size(); i++) {
    String name;
    if (names == null || (name = names.get(i)) == null) {
      name = "$f" + i;
    }
    builder.add(name, exprs.get(i).getType());
  }
  return builder.build();
}
 
Example #12
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Adds collation and charset to a character type, returns other types
 * unchanged.
 *
 * @param type        Type
 * @param typeFactory Type factory
 * @return Type with added charset and collation, or unchanged type if it is
 * not a char type.
 */
public static RelDataType addCharsetAndCollation(
    RelDataType type,
    RelDataTypeFactory typeFactory) {
  if (!inCharFamily(type)) {
    return type;
  }
  Charset charset = type.getCharset();
  if (charset == null) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = type.getCollation();
  if (collation == null) {
    collation = SqlCollation.IMPLICIT;
  }

  // todo: should get the implicit collation from repository
  //   instead of null
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(type);
  return type;
}
 
Example #13
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private List<String> createNewFieldNames(
	RelDataType rowType,
	RexBuilder rexBuilder,
	int primitiveFieldCount,
	ArrayBuffer<RexNode> extractedRexNodes,
	List<RexNode> calcProjects) {
	for (int i = 0; i < primitiveFieldCount; i++) {
		calcProjects.add(RexInputRef.of(i, rowType));
	}
	// add the fields of the extracted rex calls.
	Iterator<RexNode> iterator = extractedRexNodes.iterator();
	while (iterator.hasNext()) {
		calcProjects.add(iterator.next());
	}

	List<String> nameList = new LinkedList<>();
	for (int i = 0; i < primitiveFieldCount; i++) {
		nameList.add(rowType.getFieldNames().get(i));
	}
	Iterator<Object> indicesIterator = extractedRexNodes.indices().iterator();
	while (indicesIterator.hasNext()) {
		nameList.add("f" + indicesIterator.next());
	}
	return SqlValidatorUtil.uniquify(
		nameList,
		rexBuilder.getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
}
 
Example #14
Source File: EnumerableRelFactories.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode createProject(RelNode input, List<RelHint> hints,
                      List<? extends RexNode> childExprs, List<String> fieldNames) {
  final RelDataType rowType =
      RexUtil.createStructType(input.getCluster().getTypeFactory(), childExprs,
          fieldNames, SqlValidatorUtil.F_SUGGESTER);
  return EnumerableProject.create(input, childExprs, rowType);
}
 
Example #15
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
public List<SqlMoniker> getAllSchemaObjectNames(List<String> names) {
  final CalciteSchema schema =
      SqlValidatorUtil.getSchema(rootSchema, names, nameMatcher);
  if (schema == null) {
    return ImmutableList.of();
  }
  final List<SqlMoniker> result = new ArrayList<>();

  // Add root schema if not anonymous
  if (!schema.name.equals("")) {
    result.add(moniker(schema, null, SqlMonikerType.SCHEMA));
  }

  final Map<String, CalciteSchema> schemaMap = schema.getSubSchemaMap();

  for (String subSchema : schemaMap.keySet()) {
    result.add(moniker(schema, subSchema, SqlMonikerType.SCHEMA));
  }

  for (String table : schema.getTableNames()) {
    result.add(moniker(schema, table, SqlMonikerType.TABLE));
  }

  final NavigableSet<String> functions = schema.getFunctionNames();
  for (String function : functions) { // views are here as well
    result.add(moniker(schema, function, SqlMonikerType.FUNCTION));
  }
  return result;
}
 
Example #16
Source File: RelDataTypeFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that field names are unique.
 */
public Builder uniquify() {
  final List<String> uniqueNames = SqlValidatorUtil.uniquify(names,
      typeFactory.getTypeSystem().isSchemaCaseSensitive());
  if (uniqueNames != names) {
    names.clear();
    names.addAll(uniqueNames);
  }
  return this;
}
 
Example #17
Source File: Correlate.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected RelDataType deriveRowType() {
  switch (joinType) {
  case LEFT:
  case INNER:
    return SqlValidatorUtil.deriveJoinRowType(left.getRowType(),
        right.getRowType(), joinType,
        getCluster().getTypeFactory(), null,
        ImmutableList.of());
  case ANTI:
  case SEMI:
    return left.getRowType();
  default:
    throw new IllegalStateException("Unknown join type " + joinType);
  }
}
 
Example #18
Source File: Join.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelDataType createJoinType(
    RelDataTypeFactory typeFactory,
    RelDataType leftType,
    RelDataType rightType,
    List<String> fieldNameList,
    List<RelDataTypeField> systemFieldList) {
  return SqlValidatorUtil.createJoinType(typeFactory, leftType, rightType,
      fieldNameList, systemFieldList);
}
 
Example #19
Source File: Join.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelDataType deriveJoinRowType(
    RelDataType leftType,
    RelDataType rightType,
    JoinRelType joinType,
    RelDataTypeFactory typeFactory,
    List<String> fieldNameList,
    List<RelDataTypeField> systemFieldList) {
  return SqlValidatorUtil.deriveJoinRowType(leftType, rightType, joinType,
      typeFactory, fieldNameList, systemFieldList);
}
 
Example #20
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType getNamedType(SqlIdentifier typeName) {
  CalciteSchema.TypeEntry typeEntry = SqlValidatorUtil.getTypeEntry(getRootSchema(), typeName);
  if (typeEntry != null) {
    return typeEntry.getType().apply(typeFactory);
  } else {
    return null;
  }
}
 
Example #21
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private List<String> createNewFieldNames(
	RelDataType rowType,
	RexBuilder rexBuilder,
	int primitiveFieldCount,
	ArrayBuffer<RexNode> extractedRexNodes,
	List<RexNode> calcProjects) {
	for (int i = 0; i < primitiveFieldCount; i++) {
		calcProjects.add(RexInputRef.of(i, rowType));
	}
	// add the fields of the extracted rex calls.
	Iterator<RexNode> iterator = extractedRexNodes.iterator();
	while (iterator.hasNext()) {
		calcProjects.add(iterator.next());
	}

	List<String> nameList = new LinkedList<>();
	for (int i = 0; i < primitiveFieldCount; i++) {
		nameList.add(rowType.getFieldNames().get(i));
	}
	Iterator<Object> indicesIterator = extractedRexNodes.indices().iterator();
	while (indicesIterator.hasNext()) {
		nameList.add("f" + indicesIterator.next());
	}
	return SqlValidatorUtil.uniquify(
		nameList,
		rexBuilder.getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
}
 
Example #22
Source File: SQLAnalyzerFactory.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method to create the SQLAnalyzer using the appropriate implementation of SqlValidatorWithHints.
 *
 * If createForSqlSuggestions is true, construct a SqlAdvisorValidator instance,
 * otherwise construct a SqlValidatorImpl instance. Inject this into the constructor
 * for a SQLAnalyzer object.
 *
 * @param username
 * @param sabotContext
 * @param context
 * @param createForSqlSuggestions
 * @return SQLAnalyzer instance
 */
public static SQLAnalyzer createSQLAnalyzer(final String username,
                                            final SabotContext sabotContext,
                                            final List<String> context,
                                            final boolean createForSqlSuggestions,
                                            ProjectOptionManager projectOptionManager) {
  final ViewExpansionContext viewExpansionContext = new ViewExpansionContext(username);
  final OptionManager optionManager = OptionManagerWrapper.Builder.newBuilder()
    .withOptionManager(new DefaultOptionManager(sabotContext.getOptionValidatorListing()))
    .withOptionManager(new EagerCachingOptionManager(projectOptionManager))
    .withOptionManager(new QueryOptionManager(sabotContext.getOptionValidatorListing()))
    .build();
  final NamespaceKey defaultSchemaPath = context == null ? null : new NamespaceKey(context);

  final SchemaConfig newSchemaConfig = SchemaConfig.newBuilder(username)
    .defaultSchema(defaultSchemaPath)
    .optionManager(optionManager)
    .setViewExpansionContext(viewExpansionContext)
    .build();

  Catalog catalog = sabotContext.getCatalogService()
      .getCatalog(MetadataRequestOptions.of(newSchemaConfig));
  JavaTypeFactory typeFactory = JavaTypeFactoryImpl.INSTANCE;
  DremioCatalogReader catalogReader = new DremioCatalogReader(catalog, typeFactory);

  FunctionImplementationRegistry functionImplementationRegistry = optionManager.getOption
    (PlannerSettings.ENABLE_DECIMAL_V2_KEY).getBoolVal() ? sabotContext.getDecimalFunctionImplementationRegistry()
      : sabotContext.getFunctionImplementationRegistry();
  OperatorTable opTable = new OperatorTable(functionImplementationRegistry);
  SqlOperatorTable chainedOpTable =  new ChainedSqlOperatorTable(ImmutableList.<SqlOperatorTable>of(opTable, catalogReader));

  // Create the appropriate implementation depending on intended use of the validator.
  SqlValidatorWithHints validator =
    createForSqlSuggestions ?
      new SqlAdvisorValidator(chainedOpTable, catalogReader, typeFactory, DremioSqlConformance.INSTANCE) :
      SqlValidatorUtil.newValidator(chainedOpTable, catalogReader, typeFactory, DremioSqlConformance.INSTANCE);

  return new SQLAnalyzer(validator);
}
 
Example #23
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
private void addSelect(
    List<SqlNode> selectList, SqlNode node, RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  if (name.toLowerCase().startsWith("expr$")) {
    //Put it in ordinalMap
    ordinalMap.put(name.toLowerCase(), node);
  } else if (alias == null || !alias.equals(name)) {
    node = SqlStdOperatorTable.AS.createCall(
        POS, node, new SqlIdentifier(name, POS));
  }
  selectList.add(node);
}
 
Example #24
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void addSelect(List<SqlNode> selectList, SqlNode node,
                      RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  if (alias == null || !alias.equals(name)) {
    node = SqlStdOperatorTable.AS.createCall(
      POS, node, new SqlIdentifier(name, POS));
  }
  selectList.add(node);
}
 
Example #25
Source File: PrelTransformer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Rel addRenamedProject(SqlHandlerConfig config, Rel rel, RelDataType validatedRowType) {
  RelDataType t = rel.getRowType();

  RexBuilder b = rel.getCluster().getRexBuilder();
  List<RexNode> projections = Lists.newArrayList();
  int projectCount = t.getFieldList().size();

  for (int i =0; i < projectCount; i++) {
    projections.add(b.makeInputRef(rel, i));
  }

  final List<String> fieldNames2 = SqlValidatorUtil.uniquify(
          validatedRowType.getFieldNames(),
          SqlValidatorUtil.F_SUGGESTER,
          rel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());

  RelDataType newRowType = RexUtil.createStructType(rel.getCluster().getTypeFactory(), projections, fieldNames2);

  ProjectRel topProj = ProjectRel.create(rel.getCluster(), rel.getTraitSet(), rel, projections, newRowType);

  final boolean hasAnyType = Iterables.find(
      validatedRowType.getFieldList(),
      new Predicate<RelDataTypeField>() {
        @Override
        public boolean apply(@Nullable RelDataTypeField input) {
          return input.getType().getSqlTypeName() == SqlTypeName.ANY;
        }
      },
      null
  ) != null;

  // Add a final non-trivial Project to get the validatedRowType, if child is not project or the input row type
  // contains at least one field of type ANY
  if (rel instanceof Project && MoreRelOptUtil.isTrivialProject(topProj, true) && !hasAnyType) {
    return rel;
  }

  return topProj;
}
 
Example #26
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 5 votes vote down vote up
static Pair<SqlNode, SqlValidator> testSqlValidator() throws Exception {
    String sql = "select * from my_schema.test where f1=1 or f2=2 order by f3 limit 2";
    sql = "select * from test";
    sql = "select * from my_schema2.test2";
    sql = "select sum(f1),max(f2) from test";

    sql = "select t1.f1,sum(Distinct f1) as sumf1 from test as t1 "
            + "where f2>20 group by f1 having f1>10 order by f1 limit 2";
    // sql = "insert into test(f1,f2,f3) values(1,2,3)";
    // sql = "update test set f1=100 where f2>10";
    // sql = "delete from test where f2>10";
    SqlNode sqlNode = parse(sql);

    SqlOperatorTable opTab = SqlStdOperatorTable.instance();
    RelDataTypeFactory typeFactory = createJavaTypeFactory();
    SqlValidatorCatalogReader catalogReader = createCalciteCatalogReader(typeFactory);
    SqlConformance conformance = SqlConformanceEnum.DEFAULT;

    List<String> names = new ArrayList<>();
    names.add("my_schema");
    names.add("test");
    catalogReader.getTable(names);

    SqlValidator sqlValidator = SqlValidatorUtil.newValidator(opTab, catalogReader, typeFactory, conformance);
    sqlNode = sqlValidator.validate(sqlNode);
    // System.out.println(sqlNode);

    sql = "insert into test(f1,f2,f3) values(1,2,3)";
    // sqlNode = parse(sql);
    // sqlNode = sqlValidator.validate(sqlNode);

    return new Pair<>(sqlNode, sqlValidator);
}
 
Example #27
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a result based on a single relational expression.
 */
public Result result(SqlNode node, Collection<Clause> clauses, RelNode rel) {
  final String alias2 = SqlValidatorUtil.getAlias(node, -1);
  final String alias3 = alias2 != null ? alias2 : "t";
  final String alias4 =
      SqlValidatorUtil.uniquify(
          alias3, aliasSet, SqlValidatorUtil.EXPR_SUGGESTER);
  final String alias5 = alias2 == null || !alias2.equals(alias4) ? alias4
      : null;
  return new Result(node, clauses, alias5,
      Collections.singletonList(Pair.of(alias4, rel.getRowType())));
}
 
Example #28
Source File: SolrRules.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static List<String> solrFieldNames(final RelDataType rowType) {
  return SqlValidatorUtil.uniquify(
      new AbstractList<String>() {
        @Override
        public String get(int index) {
          return rowType.getFieldList().get(index).getName();
        }

        @Override
        public int size() {
          return rowType.getFieldCount();
        }
      }, true);
}
 
Example #29
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void addSelect(List<SqlNode> selectList, SqlNode node, RelDataType rowType) {
  final String name = rowType.getFieldNames().get(selectList.size());
  final String alias = SqlValidatorUtil.getAlias(node, -1);
  if (alias == null || !alias.equals(name)) {
    node = SqlStdOperatorTable.AS.createCall(POS, node, new SqlIdentifier(name, POS));
  }
  selectList.add(node);
}
 
Example #30
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void addSelect(List<SqlNode> selectList, SqlNode node,
                      RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  final String lowerName = name.toLowerCase(Locale.ROOT);
  if (lowerName.startsWith("expr$")) {
    // Put it in ordinalMap
    ordinalMap.put(lowerName, node);
  } else if (alias == null || !alias.equals(name)) {
    node = as(node, name);
  }
  selectList.add(node);
}