Search in sources :

Example 1 with SqlStdOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.fun.SqlStdOperatorTable in project calcite by apache.

the class SqlOperatorBaseTest method testSqlOperatorOverloading.

@Test
public void testSqlOperatorOverloading() {
    final SqlStdOperatorTable operatorTable = SqlStdOperatorTable.instance();
    for (SqlOperator sqlOperator : operatorTable.getOperatorList()) {
        String operatorName = sqlOperator.getName();
        List<SqlOperator> routines = new ArrayList<>();
        operatorTable.lookupOperatorOverloads(new SqlIdentifier(operatorName, SqlParserPos.ZERO), null, sqlOperator.getSyntax(), routines);
        Iterator<SqlOperator> iter = routines.iterator();
        while (iter.hasNext()) {
            SqlOperator operator = iter.next();
            if (!sqlOperator.getClass().isInstance(operator)) {
                iter.remove();
            }
        }
        assertThat(routines.size(), equalTo(1));
        assertThat(sqlOperator, equalTo(routines.get(0)));
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlLimitsTest(org.apache.calcite.test.SqlLimitsTest) Test(org.junit.Test)

Example 2 with SqlStdOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.fun.SqlStdOperatorTable in project calcite by apache.

the class PlannerTest method testValidateUserDefinedAggregate.

@Test
public void testValidateUserDefinedAggregate() throws Exception {
    final SqlStdOperatorTable stdOpTab = SqlStdOperatorTable.instance();
    SqlOperatorTable opTab = ChainedSqlOperatorTable.of(stdOpTab, new ListSqlOperatorTable(ImmutableList.<SqlOperator>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"));
    }
}
Also used : ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) SqlOperator(org.apache.calcite.sql.SqlOperator) ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Aggregations

SqlOperator (org.apache.calcite.sql.SqlOperator)2 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)1 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)1 ListSqlOperatorTable (org.apache.calcite.sql.util.ListSqlOperatorTable)1 SqlString (org.apache.calcite.sql.util.SqlString)1 SqlLimitsTest (org.apache.calcite.test.SqlLimitsTest)1 TimestampString (org.apache.calcite.util.TimestampString)1