Search in sources :

Example 16 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project calcite by apache.

the class TableScanRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final LogicalTableScan oldRel = call.rel(0);
    RelNode newRel = oldRel.getTable().toRel(RelOptUtil.getContext(oldRel.getCluster()));
    call.transformTo(newRel);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 17 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project calcite by apache.

the class RelWriterTest method testWriter.

/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonWriter} on
 * a simple tree of relational expressions, consisting of a table, a filter
 * and an aggregate node.
 */
@Test
public void testWriter() {
    String s = Frameworks.withPlanner(new Frameworks.PlannerAction<String>() {

        public String apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
            LogicalTableScan scan = LogicalTableScan.create(cluster, relOptSchema.getTableForMember(Arrays.asList("hr", "emps")));
            final RexBuilder rexBuilder = cluster.getRexBuilder();
            LogicalFilter filter = LogicalFilter.create(scan, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(scan), "deptno", true), rexBuilder.makeExactLiteral(BigDecimal.TEN)));
            final RelJsonWriter writer = new RelJsonWriter();
            final RelDataType bigIntType = cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
            LogicalAggregate aggregate = LogicalAggregate.create(filter, ImmutableBitSet.of(0), null, ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT, true, false, ImmutableList.of(1), -1, bigIntType, "c"), AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, ImmutableList.<Integer>of(), -1, bigIntType, "d")));
            aggregate.explain(writer);
            return writer.asString();
        }
    });
    assertThat(s, is(XX));
}
Also used : LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) JdbcTest(org.apache.calcite.test.JdbcTest) Frameworks(org.apache.calcite.tools.Frameworks) RexBuilder(org.apache.calcite.rex.RexBuilder) JdbcTest(org.apache.calcite.test.JdbcTest) Test(org.junit.Test)

Example 18 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project druid by druid-io.

the class DruidTableScanRule method onMatch.

@Override
public void onMatch(final RelOptRuleCall call) {
    final LogicalTableScan scan = call.rel(0);
    final RelOptTable table = scan.getTable();
    final DruidTable druidTable = table.unwrap(DruidTable.class);
    if (druidTable != null) {
        call.transformTo(DruidQueryRel.scanTable(scan, table, druidTable, plannerContext));
    }
}
Also used : DruidTable(org.apache.druid.sql.calcite.table.DruidTable) RelOptTable(org.apache.calcite.plan.RelOptTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 19 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project flink by apache.

the class PushProjectIntoTableSourceScanRule method getPrimaryKeyProjections.

private List<RexNode> getPrimaryKeyProjections(LogicalTableScan scan) {
    final TableSourceTable source = scan.getTable().unwrap(TableSourceTable.class);
    final ResolvedSchema schema = source.contextResolvedTable().getResolvedSchema();
    if (!schema.getPrimaryKey().isPresent()) {
        return Collections.emptyList();
    }
    final FlinkTypeFactory typeFactory = unwrapTypeFactory(scan);
    final UniqueConstraint primaryKey = schema.getPrimaryKey().get();
    return primaryKey.getColumns().stream().map(columnName -> {
        final int idx = scan.getRowType().getFieldNames().indexOf(columnName);
        final Column column = schema.getColumn(idx).orElseThrow(() -> new TableException(String.format("Column at index %d not found.", idx)));
        return new RexInputRef(idx, typeFactory.createFieldTypeFromLogicalType(column.getDataType().getLogicalType()));
    }).collect(Collectors.toList());
}
Also used : IntStream(java.util.stream.IntStream) NestedProjectionUtil(org.apache.flink.table.planner.plan.utils.NestedProjectionUtil) Arrays(java.util.Arrays) ShortcutUtils.unwrapTypeFactory(org.apache.flink.table.planner.utils.ShortcutUtils.unwrapTypeFactory) SourceAbilityContext(org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext) Column(org.apache.flink.table.catalog.Column) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) RexNodeExtractor(org.apache.flink.table.planner.plan.utils.RexNodeExtractor) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RowType(org.apache.flink.table.types.logical.RowType) SupportsProjectionPushDown(org.apache.flink.table.connector.source.abilities.SupportsProjectionPushDown) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) NestedSchema(org.apache.flink.table.planner.plan.utils.NestedSchema) Projection(org.apache.flink.table.connector.Projection) ProjectRemoveRule(org.apache.calcite.rel.rules.ProjectRemoveRule) DynamicSourceUtils.createProducedType(org.apache.flink.table.planner.connectors.DynamicSourceUtils.createProducedType) RelDataType(org.apache.calcite.rel.type.RelDataType) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) TableConfig(org.apache.flink.table.api.TableConfig) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) ProjectPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.ProjectPushDownSpec) TableException(org.apache.flink.table.api.TableException) ShortcutUtils.unwrapContext(org.apache.flink.table.planner.utils.ShortcutUtils.unwrapContext) RelRule(org.apache.calcite.plan.RelRule) NestedColumn(org.apache.flink.table.planner.plan.utils.NestedColumn) Collectors(java.util.stream.Collectors) DynamicSourceUtils.createRequiredMetadataKeys(org.apache.flink.table.planner.connectors.DynamicSourceUtils.createRequiredMetadataKeys) SourceAbilitySpec(org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RexInputRef(org.apache.calcite.rex.RexInputRef) Objects(java.util.Objects) DynamicSourceUtils(org.apache.flink.table.planner.connectors.DynamicSourceUtils) RelOptRule(org.apache.calcite.plan.RelOptRule) List(java.util.List) Stream(java.util.stream.Stream) UniqueConstraint(org.apache.flink.table.catalog.UniqueConstraint) SupportsReadingMetadata(org.apache.flink.table.connector.source.abilities.SupportsReadingMetadata) ReadingMetadataSpec(org.apache.flink.table.planner.plan.abilities.source.ReadingMetadataSpec) Internal(org.apache.flink.annotation.Internal) Collections(java.util.Collections) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) TableException(org.apache.flink.table.api.TableException) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) Column(org.apache.flink.table.catalog.Column) NestedColumn(org.apache.flink.table.planner.plan.utils.NestedColumn) UniqueConstraint(org.apache.flink.table.catalog.UniqueConstraint) RexInputRef(org.apache.calcite.rex.RexInputRef) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema)

Example 20 with LogicalTableScan

use of org.apache.calcite.rel.logical.LogicalTableScan in project flink by apache.

the class DynamicSourceUtils method pushTableScan.

private static void pushTableScan(boolean isBatchMode, FlinkRelBuilder relBuilder, ContextResolvedTable contextResolvedTable, FlinkStatistic statistic, List<RelHint> hints, DynamicTableSource tableSource) {
    final RowType producedType = createProducedType(contextResolvedTable.getResolvedSchema(), tableSource);
    final RelDataType producedRelDataType = relBuilder.getTypeFactory().buildRelNodeRowType(producedType);
    final TableSourceTable tableSourceTable = new TableSourceTable(relBuilder.getRelOptSchema(), producedRelDataType, statistic, tableSource, !isBatchMode, contextResolvedTable, ShortcutUtils.unwrapContext(relBuilder), new SourceAbilitySpec[0]);
    final LogicalTableScan scan = LogicalTableScan.create(relBuilder.getCluster(), tableSourceTable, hints);
    relBuilder.push(scan);
}
Also used : RowType(org.apache.flink.table.types.logical.RowType) RelDataType(org.apache.calcite.rel.type.RelDataType) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Aggregations

LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)23 RelDataType (org.apache.calcite.rel.type.RelDataType)10 TableSourceTable (org.apache.flink.table.planner.plan.schema.TableSourceTable)8 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 RelNode (org.apache.calcite.rel.RelNode)6 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)5 Filter (org.apache.calcite.rel.core.Filter)4 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)4 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)4 RexNode (org.apache.calcite.rex.RexNode)4 RelBuilder (org.apache.calcite.tools.RelBuilder)4 ArrayList (java.util.ArrayList)3 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 RelOptTable (org.apache.calcite.plan.RelOptTable)3 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)3 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)3 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)3 Before (org.junit.Before)3