Search in sources :

Example 11 with LogicalTableScan

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

the class EnumerableTableScanRule method convert.

@Override
public RelNode convert(RelNode rel) {
    LogicalTableScan scan = (LogicalTableScan) rel;
    final RelOptTable relOptTable = scan.getTable();
    final Table table = relOptTable.unwrap(Table.class);
    if (!EnumerableTableScan.canHandle(table)) {
        return null;
    }
    final Expression expression = relOptTable.getExpression(Object.class);
    if (expression == null) {
        return null;
    }
    return EnumerableTableScan.create(scan.getCluster(), relOptTable);
}
Also used : Table(org.apache.calcite.schema.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) Expression(org.apache.calcite.linq4j.tree.Expression) RelOptTable(org.apache.calcite.plan.RelOptTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 12 with LogicalTableScan

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

the class RelMetadataTest method checkAverageRowSize.

private void checkAverageRowSize(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
    Double rowSize = mq.getAverageRowSize(empScan);
    List<Double> columnSizes = mq.getAverageColumnSizes(empScan);
    assertThat(columnSizes.size(), equalTo(empScan.getRowType().getFieldCount()));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0)));
    assertThat(rowSize, equalTo(89.0));
    // Empty values
    final LogicalValues emptyValues = LogicalValues.createEmpty(cluster, empTable.getRowType());
    rowSize = mq.getAverageRowSize(emptyValues);
    columnSizes = mq.getAverageColumnSizes(emptyValues);
    assertThat(columnSizes.size(), equalTo(emptyValues.getRowType().getFieldCount()));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0)));
    assertThat(rowSize, equalTo(89.0));
    // Values
    final RelDataType rowType = cluster.getTypeFactory().builder().add("a", SqlTypeName.INTEGER).add("b", SqlTypeName.VARCHAR).add("c", SqlTypeName.VARCHAR).build();
    final ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = ImmutableList.builder();
    addRow(tuples, rexBuilder, 1, "1234567890", "ABC");
    addRow(tuples, rexBuilder, 2, "1", "A");
    addRow(tuples, rexBuilder, 3, "2", null);
    final LogicalValues values = LogicalValues.create(cluster, rowType, tuples.build());
    rowSize = mq.getAverageRowSize(values);
    columnSizes = mq.getAverageColumnSizes(values);
    assertThat(columnSizes.size(), equalTo(values.getRowType().getFieldCount()));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 8.0, 3.0)));
    assertThat(rowSize, equalTo(15.0));
    // Union
    final LogicalUnion union = LogicalUnion.create(ImmutableList.<RelNode>of(empScan, emptyValues), true);
    rowSize = mq.getAverageRowSize(union);
    columnSizes = mq.getAverageColumnSizes(union);
    assertThat(columnSizes.size(), equalTo(9));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0)));
    assertThat(rowSize, equalTo(89.0));
    // Filter
    final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
    final LogicalFilter filter = LogicalFilter.create(deptScan, rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN, rexBuilder.makeInputRef(deptScan, 0), rexBuilder.makeExactLiteral(BigDecimal.TEN)));
    rowSize = mq.getAverageRowSize(filter);
    columnSizes = mq.getAverageColumnSizes(filter);
    assertThat(columnSizes.size(), equalTo(2));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 20.0)));
    assertThat(rowSize, equalTo(24.0));
    // Project
    final LogicalProject deptProject = LogicalProject.create(filter, ImmutableList.of(rexBuilder.makeInputRef(filter, 0), rexBuilder.makeInputRef(filter, 1), rexBuilder.makeCall(SqlStdOperatorTable.PLUS, rexBuilder.makeInputRef(filter, 0), rexBuilder.makeExactLiteral(BigDecimal.ONE)), rexBuilder.makeCall(SqlStdOperatorTable.CHAR_LENGTH, rexBuilder.makeInputRef(filter, 1))), (List<String>) null);
    rowSize = mq.getAverageRowSize(deptProject);
    columnSizes = mq.getAverageColumnSizes(deptProject);
    assertThat(columnSizes.size(), equalTo(4));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 20.0, 4.0, 4.0)));
    assertThat(rowSize, equalTo(32.0));
    // Join
    final LogicalJoin join = LogicalJoin.create(empScan, deptProject, rexBuilder.makeLiteral(true), ImmutableSet.<CorrelationId>of(), JoinRelType.INNER);
    rowSize = mq.getAverageRowSize(join);
    columnSizes = mq.getAverageColumnSizes(join);
    assertThat(columnSizes.size(), equalTo(13));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0, 4.0, 20.0, 4.0, 4.0)));
    assertThat(rowSize, equalTo(121.0));
    // Aggregate
    final LogicalAggregate aggregate = LogicalAggregate.create(join, ImmutableBitSet.of(2, 0), ImmutableList.<ImmutableBitSet>of(), ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, ImmutableIntList.of(), -1, 2, join, null, null)));
    rowSize = mq.getAverageRowSize(aggregate);
    columnSizes = mq.getAverageColumnSizes(aggregate);
    assertThat(columnSizes.size(), equalTo(3));
    assertThat(columnSizes, equalTo(Arrays.asList(4.0, 20.0, 8.0)));
    assertThat(rowSize, equalTo(32.0));
    // Smoke test Parallelism and Memory metadata providers
    assertThat(mq.memory(aggregate), nullValue());
    assertThat(mq.cumulativeMemoryWithinPhase(aggregate), nullValue());
    assertThat(mq.cumulativeMemoryWithinPhaseSplit(aggregate), nullValue());
    assertThat(mq.isPhaseTransition(aggregate), is(false));
    assertThat(mq.splitCount(aggregate), is(1));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) LogicalUnion(org.apache.calcite.rel.logical.LogicalUnion) ImmutableList(com.google.common.collect.ImmutableList) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelDataType(org.apache.calcite.rel.type.RelDataType) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) LogicalValues(org.apache.calcite.rel.logical.LogicalValues) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Example 13 with LogicalTableScan

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

the class RelMetadataTest method checkCollation.

private void checkCollation(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
    List<RelCollation> collations = RelMdCollation.table(empScan.getTable());
    assertThat(collations.size(), equalTo(0));
    // ORDER BY field#0 ASC, field#1 ASC
    final RelCollation collation = RelCollations.of(new RelFieldCollation(0), new RelFieldCollation(1));
    collations = RelMdCollation.sort(collation);
    assertThat(collations.size(), equalTo(1));
    assertThat(collations.get(0).getFieldCollations().size(), equalTo(2));
    final Sort empSort = LogicalSort.create(empScan, collation, null, null);
    final List<RexNode> projects = ImmutableList.of(rexBuilder.makeInputRef(empSort, 1), rexBuilder.makeLiteral("foo"), rexBuilder.makeInputRef(empSort, 0), rexBuilder.makeCall(SqlStdOperatorTable.MINUS, rexBuilder.makeInputRef(empSort, 0), rexBuilder.makeInputRef(empSort, 3)));
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    collations = RelMdCollation.project(mq, empSort, projects);
    assertThat(collations.size(), equalTo(1));
    assertThat(collations.get(0).getFieldCollations().size(), equalTo(2));
    assertThat(collations.get(0).getFieldCollations().get(0).getFieldIndex(), equalTo(2));
    assertThat(collations.get(0).getFieldCollations().get(1).getFieldIndex(), equalTo(0));
    final LogicalProject project = LogicalProject.create(empSort, projects, ImmutableList.of("a", "b", "c", "d"));
    final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
    final RelCollation deptCollation = RelCollations.of(new RelFieldCollation(0), new RelFieldCollation(1));
    final Sort deptSort = LogicalSort.create(deptScan, deptCollation, null, null);
    final ImmutableIntList leftKeys = ImmutableIntList.of(2);
    final ImmutableIntList rightKeys = ImmutableIntList.of(0);
    final EnumerableMergeJoin join;
    try {
        join = EnumerableMergeJoin.create(project, deptSort, rexBuilder.makeLiteral(true), leftKeys, rightKeys, JoinRelType.INNER);
    } catch (InvalidRelException e) {
        throw new RuntimeException(e);
    }
    collations = RelMdCollation.mergeJoin(mq, project, deptSort, leftKeys, rightKeys);
    assertThat(collations, equalTo(join.getTraitSet().getTraits(RelCollationTraitDef.INSTANCE)));
    // Values (empty)
    collations = RelMdCollation.values(mq, empTable.getRowType(), ImmutableList.<ImmutableList<RexLiteral>>of());
    assertThat(collations.toString(), equalTo("[[0, 1, 2, 3, 4, 5, 6, 7, 8], " + "[1, 2, 3, 4, 5, 6, 7, 8], " + "[2, 3, 4, 5, 6, 7, 8], " + "[3, 4, 5, 6, 7, 8], " + "[4, 5, 6, 7, 8], " + "[5, 6, 7, 8], " + "[6, 7, 8], " + "[7, 8], " + "[8]]"));
    final LogicalValues emptyValues = LogicalValues.createEmpty(cluster, empTable.getRowType());
    assertThat(mq.collations(emptyValues), equalTo(collations));
    // Values (non-empty)
    final RelDataType rowType = cluster.getTypeFactory().builder().add("a", SqlTypeName.INTEGER).add("b", SqlTypeName.INTEGER).add("c", SqlTypeName.INTEGER).add("d", SqlTypeName.INTEGER).build();
    final ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = ImmutableList.builder();
    // sort keys are [a], [a, b], [a, b, c], [a, b, c, d], [a, c], [b], [b, a],
    // [b, d]
    // algorithm deduces [a, b, c, d], [b, d] which is a useful sub-set
    addRow(tuples, rexBuilder, 1, 1, 1, 1);
    addRow(tuples, rexBuilder, 1, 2, 0, 3);
    addRow(tuples, rexBuilder, 2, 3, 2, 2);
    addRow(tuples, rexBuilder, 3, 3, 1, 4);
    collations = RelMdCollation.values(mq, rowType, tuples.build());
    assertThat(collations.toString(), equalTo("[[0, 1, 2, 3], [1, 3]]"));
    final LogicalValues values = LogicalValues.create(cluster, rowType, tuples.build());
    assertThat(mq.collations(values), equalTo(collations));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) InvalidRelException(org.apache.calcite.rel.InvalidRelException) ImmutableList(com.google.common.collect.ImmutableList) EnumerableMergeJoin(org.apache.calcite.adapter.enumerable.EnumerableMergeJoin) RelDataType(org.apache.calcite.rel.type.RelDataType) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) LogicalValues(org.apache.calcite.rel.logical.LogicalValues) RelCollation(org.apache.calcite.rel.RelCollation) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexBuilder(org.apache.calcite.rex.RexBuilder) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode)

Example 14 with LogicalTableScan

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

the class RelMetadataTest method checkAllPredicates.

private void checkAllPredicates(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
    final RelBuilder relBuilder = RelBuilder.proto().create(cluster, null);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
    relBuilder.push(empScan);
    RelOptPredicateList predicates = mq.getAllPredicates(empScan);
    assertThat(predicates.pulledUpPredicates.isEmpty(), is(true));
    relBuilder.filter(relBuilder.equals(relBuilder.field("EMPNO"), relBuilder.literal(BigDecimal.ONE)));
    final RelNode filter = relBuilder.peek();
    predicates = mq.getAllPredicates(filter);
    assertThat(predicates.pulledUpPredicates.size(), is(1));
    RexCall call = (RexCall) predicates.pulledUpPredicates.get(0);
    assertThat(call.getOperands().size(), is(2));
    RexTableInputRef inputRef1 = (RexTableInputRef) call.getOperands().get(0);
    assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
    assertThat(inputRef1.getIndex(), is(0));
    final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
    relBuilder.push(deptScan);
    relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.field(2, 0, "DEPTNO"), relBuilder.field(2, 1, "DEPTNO")));
    relBuilder.project(relBuilder.field("DEPTNO"));
    final RelNode project = relBuilder.peek();
    predicates = mq.getAllPredicates(project);
    assertThat(predicates.pulledUpPredicates.size(), is(2));
    // From Filter
    call = (RexCall) predicates.pulledUpPredicates.get(0);
    assertThat(call.getOperands().size(), is(2));
    inputRef1 = (RexTableInputRef) call.getOperands().get(0);
    assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
    assertThat(inputRef1.getIndex(), is(0));
    // From Join
    call = (RexCall) predicates.pulledUpPredicates.get(1);
    assertThat(call.getOperands().size(), is(2));
    inputRef1 = (RexTableInputRef) call.getOperands().get(0);
    assertTrue(inputRef1.getQualifiedName().equals(EMP_QNAME));
    assertThat(inputRef1.getIndex(), is(7));
    RexTableInputRef inputRef2 = (RexTableInputRef) call.getOperands().get(1);
    assertTrue(inputRef2.getQualifiedName().equals(ImmutableList.of("CATALOG", "SALES", "DEPT")));
    assertThat(inputRef2.getIndex(), is(0));
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RexTableInputRef(org.apache.calcite.rex.RexTableInputRef) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 15 with LogicalTableScan

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

the class RelMetadataTest method checkPredicates.

private void checkPredicates(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
    final RelBuilder relBuilder = RelBuilder.proto().create(cluster, null);
    final RelMetadataQuery mq = RelMetadataQuery.instance();
    final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
    relBuilder.push(empScan);
    RelOptPredicateList predicates = mq.getPulledUpPredicates(empScan);
    assertThat(predicates.pulledUpPredicates.isEmpty(), is(true));
    relBuilder.filter(relBuilder.equals(relBuilder.field("EMPNO"), relBuilder.literal(BigDecimal.ONE)));
    final RelNode filter = relBuilder.peek();
    predicates = mq.getPulledUpPredicates(filter);
    assertThat(predicates.pulledUpPredicates.toString(), is("[=($0, 1)]"));
    final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
    relBuilder.push(deptScan);
    relBuilder.semiJoin(relBuilder.equals(relBuilder.field(2, 0, "DEPTNO"), relBuilder.field(2, 1, "DEPTNO")));
    final SemiJoin semiJoin = (SemiJoin) relBuilder.build();
    predicates = mq.getPulledUpPredicates(semiJoin);
    assertThat(predicates.pulledUpPredicates, sortsAs("[=($0, 1)]"));
    assertThat(predicates.leftInferredPredicates, sortsAs("[]"));
    assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
    // Create a Join similar to the previous SemiJoin
    relBuilder.push(filter);
    relBuilder.push(deptScan);
    relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.field(2, 0, "DEPTNO"), relBuilder.field(2, 1, "DEPTNO")));
    relBuilder.project(relBuilder.field("DEPTNO"));
    final RelNode project = relBuilder.peek();
    predicates = mq.getPulledUpPredicates(project);
    // No inferred predicates, because we already know DEPTNO is NOT NULL
    assertThat(predicates.pulledUpPredicates, sortsAs("[]"));
    assertThat(project.getRowType().getFullTypeString(), is("RecordType(INTEGER NOT NULL DEPTNO) NOT NULL"));
    assertThat(predicates.leftInferredPredicates.isEmpty(), is(true));
    assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
    // Create a Join similar to the previous Join, but joining on MGR, which
    // is nullable. From the join condition "e.MGR = d.DEPTNO" we can deduce
    // the projected predicate "IS NOT NULL($0)".
    relBuilder.push(filter);
    relBuilder.push(deptScan);
    relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.field(2, 0, "MGR"), relBuilder.field(2, 1, "DEPTNO")));
    relBuilder.project(relBuilder.field("MGR"));
    final RelNode project2 = relBuilder.peek();
    predicates = mq.getPulledUpPredicates(project2);
    assertThat(predicates.pulledUpPredicates, sortsAs("[IS NOT NULL($0)]"));
    assertThat(predicates.leftInferredPredicates.isEmpty(), is(true));
    assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
    // Create another similar Join. From the join condition
    // e.MGR - e.EMPNO = d.DEPTNO + e.MGR_COMM
    // we can deduce the projected predicate
    // MGR IS NOT NULL OR MGR_COMM IS NOT NULL
    // 
    // EMPNO is omitted because it is NOT NULL.
    // MGR_COMM is a made-up nullable field.
    relBuilder.push(filter);
    relBuilder.project(Iterables.concat(relBuilder.fields(), ImmutableList.of(relBuilder.alias(relBuilder.call(SqlStdOperatorTable.PLUS, relBuilder.field("MGR"), relBuilder.field("COMM")), "MGR_COMM"))));
    relBuilder.push(deptScan);
    relBuilder.join(JoinRelType.INNER, relBuilder.equals(relBuilder.call(SqlStdOperatorTable.MINUS, relBuilder.field(2, 0, "MGR"), relBuilder.field(2, 0, "EMPNO")), relBuilder.call(SqlStdOperatorTable.PLUS, relBuilder.field(2, 1, "DEPTNO"), relBuilder.field(2, 0, "MGR_COMM"))));
    relBuilder.project(relBuilder.field("MGR"), relBuilder.field("NAME"), relBuilder.field("MGR_COMM"), relBuilder.field("COMM"));
    final RelNode project3 = relBuilder.peek();
    predicates = mq.getPulledUpPredicates(project3);
    assertThat(predicates.pulledUpPredicates, sortsAs("[OR(IS NOT NULL($0), IS NOT NULL($2))]"));
    assertThat(predicates.leftInferredPredicates.isEmpty(), is(true));
    assertThat(predicates.rightInferredPredicates.isEmpty(), is(true));
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) SemiJoin(org.apache.calcite.rel.core.SemiJoin) 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