Java Code Examples for org.apache.calcite.sql.validate.SqlValidator#validate()

The following examples show how to use org.apache.calcite.sql.validate.SqlValidator#validate() . 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: CalcitePrepareImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Shared implementation for {@link #parse}, {@link #convert} and
 * {@link #analyzeView}. */
private ParseResult parse_(Context context, String sql, boolean convert,
    boolean analyze, boolean fail) {
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  CalciteCatalogReader catalogReader =
      new CalciteCatalogReader(
          context.getRootSchema(),
          context.getDefaultSchemaPath(),
          typeFactory,
          context.config());
  SqlParser parser = createParser(sql);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseStmt();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }
  final SqlValidator validator = createSqlValidator(context, catalogReader);
  SqlNode sqlNode1 = validator.validate(sqlNode);
  if (convert) {
    return convert_(
        context, sql, analyze, fail, catalogReader, validator, sqlNode1);
  }
  return new ParseResult(this, validator, sql, sqlNode1,
      validator.getValidatedNodeType(sqlNode1));
}
 
Example 2
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void assertExceptionIsThrown(String sql, String expectedMsgPattern) {
  final SqlValidator validator;
  final SqlNode sqlNode;
  final SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
  try {
    sqlNode = parseQuery(sap.sql);
    validator = getValidator();
  } catch (Throwable e) {
    checkParseEx(e, expectedMsgPattern, sap.sql);
    return;
  }

  Throwable thrown = null;
  try {
    validator.validate(sqlNode);
  } catch (Throwable ex) {
    thrown = ex;
  }

  SqlTests.checkEx(thrown, expectedMsgPattern, sap, SqlTests.Stage.VALIDATE);
}
 
Example 3
Source File: AbstractMaterializedViewTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelNode toRel(RelOptCluster cluster, SchemaPlus rootSchema,
    SchemaPlus defaultSchema, String sql) throws SqlParseException {
  final SqlParser parser = SqlParser.create(sql, SqlParser.Config.DEFAULT);
  final SqlNode parsed = parser.parseStmt();

  final CalciteCatalogReader catalogReader = new CalciteCatalogReader(
      CalciteSchema.from(rootSchema),
      CalciteSchema.from(defaultSchema).path(null),
      new JavaTypeFactoryImpl(), new CalciteConnectionConfigImpl(new Properties()));

  final SqlValidator validator = new ValidatorForTest(SqlStdOperatorTable.instance(),
      catalogReader, new JavaTypeFactoryImpl(), SqlConformanceEnum.DEFAULT);
  final SqlNode validated = validator.validate(parsed);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
      .withTrimUnusedFields(true)
      .withExpand(true)
      .withDecorrelationEnabled(true)
      .build();
  final SqlToRelConverter converter = new SqlToRelConverter(
      (rowType, queryString, schemaPath, viewPath) -> {
        throw new UnsupportedOperationException("cannot expand view");
      }, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, config);
  return converter.convertQuery(validated, false, true).rel;
}
 
Example 4
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 5
Source File: SqlSetOption.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void validate(SqlValidator validator,
    SqlValidatorScope scope) {
  validator.validate(value);
}
 
Example 6
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlNode parseAndValidate(SqlValidator validator, String sql) {
  SqlNode sqlNode;
  try {
    sqlNode = parseQuery(sql);
  } catch (Throwable e) {
    throw new RuntimeException("Error while parsing query: " + sql, e);
  }
  return validator.validate(sqlNode);
}
 
Example 7
Source File: SqlSetOption.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public void validate(SqlValidator validator,
    SqlValidatorScope scope) {
  validator.validate(value);
}