Java Code Examples for org.apache.calcite.sql.fun.SqlStdOperatorTable#instance()

The following examples show how to use org.apache.calcite.sql.fun.SqlStdOperatorTable#instance() . 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: 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 2
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 3
Source File: Frameworks.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a ConfigBuilder, initializing to defaults. */
private ConfigBuilder() {
  convertletTable = StandardConvertletTable.INSTANCE;
  operatorTable = SqlStdOperatorTable.instance();
  programs = ImmutableList.of();
  context = Contexts.empty();
  parserConfig = SqlParser.Config.DEFAULT;
  sqlValidatorConfig = SqlValidator.Config.DEFAULT;
  sqlToRelConverterConfig = SqlToRelConverter.Config.DEFAULT;
  typeSystem = RelDataTypeSystem.DEFAULT;
  evolveLattice = false;
  statisticProvider = QuerySqlStatisticProvider.SILENT_CACHING_INSTANCE;
}
 
Example 4
Source File: PlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testValidateUserDefinedAggregate() throws Exception {
  final SqlStdOperatorTable stdOpTab = SqlStdOperatorTable.instance();
  SqlOperatorTable opTab =
      ChainedSqlOperatorTable.of(stdOpTab,
          new ListSqlOperatorTable(
              ImmutableList.of(new MyCountAggFunction())));
  final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .defaultSchema(
          CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR))
      .operatorTable(opTab)
      .build();
  final Planner planner = Frameworks.getPlanner(config);
  SqlNode parse =
      planner.parse("select \"deptno\", my_count(\"empid\") from \"emps\"\n"
          + "group by \"deptno\"");
  assertThat(Util.toLinux(parse.toString()),
      equalTo("SELECT `deptno`, `MY_COUNT`(`empid`)\n"
          + "FROM `emps`\n"
          + "GROUP BY `deptno`"));

  // MY_COUNT is recognized as an aggregate function, and therefore it is OK
  // that its argument empid is not in the GROUP BY clause.
  SqlNode validate = planner.validate(parse);
  assertThat(validate, notNullValue());

  // The presence of an aggregate function in the SELECT clause causes it
  // to become an aggregate query. Non-aggregate expressions become illegal.
  planner.close();
  planner.reset();
  parse = planner.parse("select \"deptno\", count(1) from \"emps\"");
  try {
    validate = planner.validate(parse);
    fail("expected exception, got " + validate);
  } catch (ValidationException e) {
    assertThat(e.getCause().getCause().getMessage(),
        containsString("Expression 'deptno' is not being grouped"));
  }
}
 
Example 5
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an operator table.
 *
 * @return New operator table
 */
protected SqlOperatorTable createOperatorTable() {
  final MockSqlOperatorTable opTab =
      new MockSqlOperatorTable(SqlStdOperatorTable.instance());
  MockSqlOperatorTable.addRamp(opTab);
  return opTab;
}
 
Example 6
Source File: ContextSqlValidator.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@code ContextSqlValidator}.
 * @param context Prepare context.
 * @param mutable Whether to get the mutable schema.
 */
public ContextSqlValidator(CalcitePrepare.Context context, boolean mutable) {
  super(SqlStdOperatorTable.instance(), getCatalogReader(context, mutable),
      context.getTypeFactory(), Config.DEFAULT);
}