Search in sources :

Example 46 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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)

Example 47 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class PlannerTest method getPlanner.

private Planner getPlanner(List<RelTraitDef> traitDefs, SqlParser.Config parserConfig, Program... programs) {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR)).traitDefs(traitDefs).programs(programs).build();
    return Frameworks.getPlanner(config);
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 48 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class FrameworksTest method testUpdate.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2039">[CALCITE-2039]
 * AssertionError when pushing project to ProjectableFilterableTable</a>
 * using UPDATE via {@link Frameworks}.
 */
@Test
public void testUpdate() throws Exception {
    Table table = new TableImpl();
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    SchemaPlus schema = rootSchema.add("x", new AbstractSchema());
    schema.add("MYTABLE", table);
    List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelDistributionTraitDef.INSTANCE);
    SqlParser.Config parserConfig = SqlParser.configBuilder(SqlParser.Config.DEFAULT).setCaseSensitive(false).build();
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(schema).traitDefs(traitDefs).ruleSets(RuleSets.ofList(AbstractConverter.ExpandConversionRule.INSTANCE)).programs(Programs.ofRules(Programs.RULE_SET)).build();
    executeQuery(config, " UPDATE MYTABLE set id=7 where id=1", CalcitePrepareImpl.DEBUG);
}
Also used : RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) Table(org.apache.calcite.schema.Table) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) Test(org.junit.Test)

Example 49 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class FrameworksTest method testFrameworksValidatorWithIdentifierExpansion.

/**
 * Tests that the validator expands identifiers by default.
 *
 * <p>Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-593">[CALCITE-593]
 * Validator in Frameworks should expand identifiers</a>.
 */
@Test
public void testFrameworksValidatorWithIdentifierExpansion() throws Exception {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR)).build();
    final Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse("select * from \"emps\" ");
    SqlNode val = planner.validate(parse);
    String valStr = val.toSqlString(AnsiSqlDialect.DEFAULT, false).getSql();
    String expandedStr = "SELECT `emps`.`empid`, `emps`.`deptno`, `emps`.`name`, `emps`.`salary`, `emps`.`commission`\n" + "FROM `hr`.`emps` AS `emps`";
    assertThat(Util.toLinux(valStr), equalTo(expandedStr));
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 50 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class RelWriterTest method testReader.

/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonReader}.
 */
@Test
public void testReader() {
    String s = Frameworks.withPlanner(new Frameworks.PlannerAction<String>() {

        public String apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            SchemaPlus schema = rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
            final RelJsonReader reader = new RelJsonReader(cluster, relOptSchema, schema);
            RelNode node;
            try {
                node = reader.read(XX);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
        }
    });
    assertThat(s, isLinux("LogicalAggregate(group=[{0}], agg#0=[COUNT(DISTINCT $1)], agg#1=[COUNT()])\n" + "  LogicalFilter(condition=[=($1, 10)])\n" + "    LogicalTableScan(table=[[hr, emps]])\n"));
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) IOException(java.io.IOException) RelNode(org.apache.calcite.rel.RelNode) JdbcTest(org.apache.calcite.test.JdbcTest) Frameworks(org.apache.calcite.tools.Frameworks) RelJsonReader(org.apache.calcite.rel.externalize.RelJsonReader) JdbcTest(org.apache.calcite.test.JdbcTest) Test(org.junit.Test)

Aggregations

SchemaPlus (org.apache.calcite.schema.SchemaPlus)180 Test (org.junit.Test)56 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)42 Connection (java.sql.Connection)39 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 ResultSet (java.sql.ResultSet)26 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)24 Table (org.apache.calcite.schema.Table)20 RelNode (org.apache.calcite.rel.RelNode)19 IOException (java.io.IOException)17 Statement (java.sql.Statement)17 SqlNode (org.apache.calcite.sql.SqlNode)17 PreparedStatement (java.sql.PreparedStatement)16 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)15 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)15 ArrayList (java.util.ArrayList)14 SQLException (java.sql.SQLException)13 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)13 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)13 Properties (java.util.Properties)12