org.apache.calcite.sql.util.ListSqlOperatorTable Java Examples

The following examples show how to use org.apache.calcite.sql.util.ListSqlOperatorTable. 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: CalciteCatalogReader.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates an operator table that contains functions in the given class.
   *
   * @see ModelHandler#addFunctions */
  public static SqlOperatorTable operatorTable(String className) {
    // Dummy schema to collect the functions
    final CalciteSchema schema =
        CalciteSchema.createRootSchema(false, false);
//    ModelHandler.addFunctions(schema.plus(), null, ImmutableList.of(),
//        className, "*", true);

    // The following is technical debt; see [CALCITE-2082] Remove
    // RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor
    final SqlTypeFactoryImpl typeFactory =
        new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);

    final ListSqlOperatorTable table = new ListSqlOperatorTable();
    for (String name : schema.getFunctionNames()) {
      for (Function function : schema.getFunctions(name, true)) {
        final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO);
        table.add(
            toOp(typeFactory, id, function));
      }
    }
    return table;
  }
 
Example #2
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates an operator table that contains functions in the given class.
 *
 * @see ModelHandler#addFunctions */
public static SqlOperatorTable operatorTable(String className) {
  // Dummy schema to collect the functions
  final CalciteSchema schema =
      CalciteSchema.createRootSchema(false, false);
  ModelHandler.addFunctions(schema.plus(), null, ImmutableList.of(),
      className, "*", true);

  // The following is technical debt; see [CALCITE-2082] Remove
  // RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor
  final SqlTypeFactoryImpl typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);

  final ListSqlOperatorTable table = new ListSqlOperatorTable();
  for (String name : schema.getFunctionNames()) {
    for (Function function : schema.getFunctions(name, true)) {
      final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO);
      table.add(
          toOp(typeFactory, id, function));
    }
  }
  return table;
}
 
Example #3
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 #4
Source File: SamzaSqlUdfOperatorTable.java    From samza with Apache License 2.0 4 votes vote down vote up
public SamzaSqlUdfOperatorTable(List<SamzaSqlScalarFunctionImpl> scalarFunctions) {
  this.operatorTable = new ListSqlOperatorTable(getSqlOperators(scalarFunctions));
}
 
Example #5
Source File: MockSqlOperatorTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public MockSqlOperatorTable(SqlOperatorTable parentTable) {
  super(ImmutableList.of(parentTable, new ListSqlOperatorTable()));
  listOpTab = (ListSqlOperatorTable) tableList.get(1);
}