Search in sources :

Example 51 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project samza by apache.

the class QueryPlanner method plan.

public RelRoot plan(String query) {
    try (Planner planner = getPlanner()) {
        SqlNode sql = planner.parse(query);
        SqlNode validatedSql = planner.validate(sql);
        RelRoot relRoot = planner.rel(validatedSql);
        LOG.info("query plan without optimization:\n" + RelOptUtil.toString(relRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
        if (!isQueryPlanOptimizerEnabled) {
            LOG.info("Skipping query optimization as it is disabled.");
            return relRoot;
        }
        return optimize(planner, relRoot);
    } catch (Exception e) {
        String errorMsg = SamzaSqlValidator.formatErrorString(query, e);
        LOG.error(errorMsg, e);
        throw new SamzaException(errorMsg, e);
    }
}
Also used : Planner(org.apache.calcite.tools.Planner) RelRoot(org.apache.calcite.rel.RelRoot) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 52 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project samza by apache.

the class SamzaSqlDslConverter method convertDsl.

@Override
public Collection<RelRoot> convertDsl(String dsl) {
    // TODO: Introduce an API to parse a dsl string and return one or more sql statements
    List<String> sqlStmts = fetchSqlFromConfig(config);
    List<RelRoot> relRoots = new LinkedList<>();
    for (String sql : sqlStmts) {
        QueryPlanner planner = getQueryPlanner(getSqlConfig(Collections.singletonList(sql), config));
        // we always pass only select query to the planner for samza sql. The reason is that samza sql supports
        // schema evolution where source and destination could up to an extent have independent schema evolution while
        // calcite expects strict comformance of the destination schema with that of the fields in the select query.
        SamzaSqlQueryParser.QueryInfo qinfo = SamzaSqlQueryParser.parseQuery(sql);
        RelRoot relRoot = planner.plan(qinfo.getSelectQuery());
        relRoots.add(relRoot);
    }
    return relRoots;
}
Also used : RelRoot(org.apache.calcite.rel.RelRoot) SamzaSqlQueryParser(org.apache.samza.sql.util.SamzaSqlQueryParser) LinkedList(java.util.LinkedList) QueryPlanner(org.apache.samza.sql.planner.QueryPlanner)

Example 53 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class RelMetadataTest method testBrokenCustomProvider.

@Test
public void testBrokenCustomProvider() {
    final List<String> buf = Lists.newArrayList();
    ColTypeImpl.THREAD_LIST.set(buf);
    final String sql = "select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0";
    final RelRoot root = tester.withClusterFactory(new Function<RelOptCluster, RelOptCluster>() {

        public RelOptCluster apply(RelOptCluster cluster) {
            cluster.setMetadataProvider(ChainedRelMetadataProvider.of(ImmutableList.of(BrokenColTypeImpl.SOURCE, cluster.getMetadataProvider())));
            return cluster;
        }
    }).convertSqlToRel(sql);
    final RelNode rel = root.rel;
    assertThat(rel, instanceOf(LogicalFilter.class));
    final MyRelMetadataQuery mq = new MyRelMetadataQuery();
    try {
        assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
        fail("expected error");
    } catch (IllegalArgumentException e) {
        final String value = "No handler for method [public abstract java.lang.String " + "org.apache.calcite.test.RelMetadataTest$ColType.getColType(int)] " + "applied to argument of type [interface org.apache.calcite.rel.RelNode]; " + "we recommend you create a catch-all (RelNode) handler";
        assertThat(e.getMessage(), is(value));
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Function(com.google.common.base.Function) RelNode(org.apache.calcite.rel.RelNode) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelRoot(org.apache.calcite.rel.RelRoot) Test(org.junit.Test)

Example 54 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class RelMetadataTest method testCustomProvider.

@Test
public void testCustomProvider() {
    final List<String> buf = Lists.newArrayList();
    ColTypeImpl.THREAD_LIST.set(buf);
    final String sql = "select deptno, count(*) from emp where deptno > 10 " + "group by deptno having count(*) = 0";
    final RelRoot root = tester.withClusterFactory(new Function<RelOptCluster, RelOptCluster>() {

        public RelOptCluster apply(RelOptCluster cluster) {
            // Create a custom provider that includes ColType.
            // Include the same provider twice just to be devious.
            final ImmutableList<RelMetadataProvider> list = ImmutableList.of(ColTypeImpl.SOURCE, ColTypeImpl.SOURCE, cluster.getMetadataProvider());
            cluster.setMetadataProvider(ChainedRelMetadataProvider.of(list));
            return cluster;
        }
    }).convertSqlToRel(sql);
    final RelNode rel = root.rel;
    // Top node is a filter. Its metadata uses getColType(RelNode, int).
    assertThat(rel, instanceOf(LogicalFilter.class));
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    assertThat(colType(mq, rel, 0), equalTo("DEPTNO-rel"));
    assertThat(colType(mq, rel, 1), equalTo("EXPR$1-rel"));
    // Next node is an aggregate. Its metadata uses
    // getColType(LogicalAggregate, int).
    final RelNode input = rel.getInput(0);
    assertThat(input, instanceOf(LogicalAggregate.class));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    // There is no caching. Another request causes another call to the provider.
    assertThat(buf.toString(), equalTo("[DEPTNO-rel, EXPR$1-rel, DEPTNO-agg]"));
    assertThat(buf.size(), equalTo(3));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(4));
    // Now add a cache. Only the first request for each piece of metadata
    // generates a new call to the provider.
    final RelOptPlanner planner = rel.getCluster().getPlanner();
    rel.getCluster().setMetadataProvider(new CachingRelMetadataProvider(rel.getCluster().getMetadataProvider(), planner));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(5));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(5));
    assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
    assertThat(buf.size(), equalTo(6));
    assertThat(colType(mq, input, 1), equalTo("EXPR$1-agg"));
    assertThat(buf.size(), equalTo(6));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(6));
    // With a different timestamp, a metadata item is re-computed on first call.
    long timestamp = planner.getRelMetadataTimestamp(rel);
    assertThat(timestamp, equalTo(0L));
    ((MockRelOptPlanner) planner).setRelMetadataTimestamp(timestamp + 1);
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(7));
    assertThat(colType(mq, input, 0), equalTo("DEPTNO-agg"));
    assertThat(buf.size(), equalTo(7));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelRoot(org.apache.calcite.rel.RelRoot) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Function(com.google.common.base.Function) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) RelNode(org.apache.calcite.rel.RelNode) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) ReflectiveRelMetadataProvider(org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider) CachingRelMetadataProvider(org.apache.calcite.rel.metadata.CachingRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) Test(org.junit.Test)

Example 55 with RelRoot

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.

the class RelOptTestBase method checkPlanning.

/**
 * Checks the plan for a SQL statement before/after executing a given rule,
 * with a pre-program to prepare the tree.
 *
 * @param tester     Tester
 * @param preProgram Program to execute before comparing before state
 * @param planner    Planner
 * @param sql        SQL query
 * @param unchanged  Whether the rule is to have no effect
 */
protected void checkPlanning(Tester tester, HepProgram preProgram, RelOptPlanner planner, String sql, boolean unchanged) {
    final DiffRepository diffRepos = getDiffRepos();
    String sql2 = diffRepos.expand("sql", sql);
    final RelRoot root = tester.convertSqlToRel(sql2);
    final RelNode relInitial = root.rel;
    assertTrue(relInitial != null);
    List<RelMetadataProvider> list = Lists.newArrayList();
    list.add(DefaultRelMetadataProvider.INSTANCE);
    planner.registerMetadataProviders(list);
    RelMetadataProvider plannerChain = ChainedRelMetadataProvider.of(list);
    final RelOptCluster cluster = relInitial.getCluster();
    cluster.setMetadataProvider(plannerChain);
    RelNode relBefore;
    if (preProgram == null) {
        relBefore = relInitial;
    } else {
        HepPlanner prePlanner = new HepPlanner(preProgram);
        prePlanner.setRoot(relInitial);
        relBefore = prePlanner.findBestExp();
    }
    assertThat(relBefore, notNullValue());
    final String planBefore = NL + RelOptUtil.toString(relBefore);
    diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
    SqlToRelTestBase.assertValid(relBefore);
    planner.setRoot(relBefore);
    RelNode r = planner.findBestExp();
    if (tester.isLateDecorrelate()) {
        final String planMid = NL + RelOptUtil.toString(r);
        diffRepos.assertEquals("planMid", "${planMid}", planMid);
        SqlToRelTestBase.assertValid(r);
        final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
        r = RelDecorrelator.decorrelateQuery(r, relBuilder);
    }
    final String planAfter = NL + RelOptUtil.toString(r);
    if (unchanged) {
        assertThat(planAfter, is(planBefore));
    } else {
        diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
        if (planBefore.equals(planAfter)) {
            throw new AssertionError("Expected plan before and after is the same.\n" + "You must use unchanged=true or call checkPlanUnchanged");
        }
    }
    SqlToRelTestBase.assertValid(r);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) ChainedRelMetadataProvider(org.apache.calcite.rel.metadata.ChainedRelMetadataProvider) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) RelRoot(org.apache.calcite.rel.RelRoot) HepPlanner(org.apache.calcite.plan.hep.HepPlanner)

Aggregations

RelRoot (org.apache.calcite.rel.RelRoot)77 SqlNode (org.apache.calcite.sql.SqlNode)30 RelNode (org.apache.calcite.rel.RelNode)22 Test (org.junit.jupiter.api.Test)18 NlsString (org.apache.calcite.util.NlsString)17 DingoTableScan (io.dingodb.calcite.rel.DingoTableScan)13 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)11 SqlToRelConverter (org.apache.calcite.sql2rel.SqlToRelConverter)11 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)9 SamzaSqlDslConverterFactory (org.apache.samza.sql.dsl.SamzaSqlDslConverterFactory)8 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)7 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)7 DslConverter (org.apache.samza.sql.interfaces.DslConverter)7 Test (org.junit.Test)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 SqlParser (org.apache.calcite.sql.parser.SqlParser)6 Planner (org.apache.calcite.tools.Planner)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5