Search in sources :

Example 26 with RelRoot

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

the class PlannerTest method assertValidPlan.

/**
 *  Asserts a Planner generates the correct plan using the provided
 *  type system.
 */
private void assertValidPlan(String sql, RelDataTypeSystem typeSystem, Matcher<String> planMatcher) throws SqlParseException, ValidationException, RelConversionException {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.POST)).typeSystem(typeSystem).build();
    final Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    final SqlNode validate = planner.validate(parse);
    final RelRoot root = planner.rel(validate);
    assertThat(toString(root.rel), planMatcher);
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelRoot(org.apache.calcite.rel.RelRoot) SqlNode(org.apache.calcite.sql.SqlNode)

Example 27 with RelRoot

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

the class PlannerTest method checkView.

private void checkView(String sql, Matcher<String> matcher) throws SqlParseException, ValidationException, RelConversionException {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.POST)).build();
    final Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    final SqlNode validate = planner.validate(parse);
    final RelRoot root = planner.rel(validate);
    assertThat(toString(root.rel), matcher);
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelRoot(org.apache.calcite.rel.RelRoot) SqlNode(org.apache.calcite.sql.SqlNode)

Example 28 with RelRoot

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

the class MycatViewTable method toRel.

@Override
public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable relOptTable) {
    try {
        final RelRoot root = context.expandView(relOptTable.getRowType(), sql, ImmutableList.of(getSchemaName()), ImmutableList.of(getSchemaName(), getTableName()));
        final RelNode rel = RelOptUtil.createCastRel(root.rel, relOptTable.getRowType(), true);
        // Expand any views
        final RelNode rel2 = rel.accept(new RelShuttleImpl() {

            @Override
            public RelNode visit(TableScan scan) {
                final RelOptTable table = scan.getTable();
                final TranslatableTable translatableTable = table.unwrap(TranslatableTable.class);
                if (translatableTable != null) {
                    return translatableTable.toRel(context, table);
                }
                return super.visit(scan);
            }
        });
        return root.withRel(rel2).rel;
    } catch (Exception e) {
        throw new RuntimeException("Error while parsing view definition: " + sql, e);
    }
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) RelNode(org.apache.calcite.rel.RelNode) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) RelRoot(org.apache.calcite.rel.RelRoot) TranslatableTable(org.apache.calcite.schema.TranslatableTable) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 29 with RelRoot

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

the class DruidPlanner method planWithDruidConvention.

/**
 * Construct a {@link PlannerResult} for a {@link RelNode} that is directly translatable to a native Druid query.
 */
private PlannerResult planWithDruidConvention(final RelRoot root, @Nullable final SqlExplain explain, @Nullable final SqlInsert insert) throws ValidationException, RelConversionException {
    final RelRoot possiblyLimitedRoot = possiblyWrapRootWithOuterLimitFromContext(root);
    final QueryMaker queryMaker = buildQueryMaker(root, insert);
    plannerContext.setQueryMaker(queryMaker);
    RelNode parameterized = rewriteRelDynamicParameters(possiblyLimitedRoot.rel);
    final DruidRel<?> druidRel = (DruidRel<?>) planner.transform(Rules.DRUID_CONVENTION_RULES, planner.getEmptyTraitSet().replace(DruidConvention.instance()).plus(root.collation), parameterized);
    if (explain != null) {
        return planExplanation(druidRel, explain, true);
    } else {
        final Supplier<Sequence<Object[]>> resultsSupplier = () -> {
            // sanity check
            final Set<ResourceAction> readResourceActions = plannerContext.getResourceActions().stream().filter(action -> action.getAction() == Action.READ).collect(Collectors.toSet());
            Preconditions.checkState(readResourceActions.isEmpty() == druidRel.getDataSourceNames().isEmpty() || // them with InlineDataSource of empty rows.
            readResourceActions.size() >= druidRel.getDataSourceNames().size(), "Authorization sanity check failed");
            return druidRel.runQuery();
        };
        return new PlannerResult(resultsSupplier, queryMaker.getResultType());
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RelNode(org.apache.calcite.rel.RelNode) DruidRel(org.apache.druid.sql.calcite.rel.DruidRel) QueryMaker(org.apache.druid.sql.calcite.run.QueryMaker) RelRoot(org.apache.calcite.rel.RelRoot) Sequence(org.apache.druid.java.util.common.guava.Sequence) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence)

Example 30 with RelRoot

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

the class TestLogicalPlan method testSort.

@Test
public void testSort() throws SqlParseException {
    String sql = "select * from test order by name, amount desc";
    RelRoot relRoot = parse(sql);
    AssertRelNode assertSort = Assert.relNode(relRoot.rel);
    assertSort.isA(LogicalSort.class).convention(Convention.NONE).singleInput().isA(LogicalProject.class).convention(Convention.NONE).singleInput().isA(DingoTableScan.class).convention(DingoConventions.DINGO);
    LogicalSort logicalSort = (LogicalSort) assertSort.getInstance();
    List<RelFieldCollation> collations = logicalSort.getCollation().getFieldCollations();
    assertThat(collations.get(0)).hasFieldOrPropertyWithValue("fieldIndex", 1).hasFieldOrPropertyWithValue("direction", ASCENDING).hasFieldOrPropertyWithValue("nullDirection", LAST);
    assertThat(collations.get(1)).hasFieldOrPropertyWithValue("fieldIndex", 2).hasFieldOrPropertyWithValue("direction", DESCENDING).hasFieldOrPropertyWithValue("nullDirection", FIRST);
    assertThat(logicalSort.fetch).isNull();
    assertThat(logicalSort.offset).isNull();
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DingoTableScan(io.dingodb.calcite.rel.DingoTableScan) RelRoot(org.apache.calcite.rel.RelRoot) NlsString(org.apache.calcite.util.NlsString) AssertRelNode(io.dingodb.calcite.assertion.AssertRelNode) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Test(org.junit.jupiter.api.Test)

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