Search in sources :

Example 46 with TableScan

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

the class DruidQuery method explainTerms.

@Override
public RelWriter explainTerms(RelWriter pw) {
    for (RelNode rel : rels) {
        if (rel instanceof TableScan) {
            TableScan tableScan = (TableScan) rel;
            pw.item("table", tableScan.getTable().getQualifiedName());
            pw.item("intervals", intervals);
        } else if (rel instanceof Filter) {
            pw.item("filter", ((Filter) rel).getCondition());
        } else if (rel instanceof Project) {
            if (((Project) rel).getInput() instanceof Aggregate) {
                pw.item("post_projects", ((Project) rel).getProjects());
            } else {
                pw.item("projects", ((Project) rel).getProjects());
            }
        } else if (rel instanceof Aggregate) {
            final Aggregate aggregate = (Aggregate) rel;
            pw.item("groups", aggregate.getGroupSet()).item("aggs", aggregate.getAggCallList());
        } else if (rel instanceof Sort) {
            final Sort sort = (Sort) rel;
            for (Ord<RelFieldCollation> ord : Ord.zip(sort.collation.getFieldCollations())) {
                pw.item("sort" + ord.i, ord.e.getFieldIndex());
            }
            for (Ord<RelFieldCollation> ord : Ord.zip(sort.collation.getFieldCollations())) {
                pw.item("dir" + ord.i, ord.e.shortString());
            }
            pw.itemIf("fetch", sort.fetch, sort.fetch != null);
        } else {
            throw new AssertionError("rel type not supported in Druid query " + rel);
        }
    }
    return pw;
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) Project(org.apache.calcite.rel.core.Project) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Sort(org.apache.calcite.rel.core.Sort) Aggregate(org.apache.calcite.rel.core.Aggregate)

Example 47 with TableScan

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

the class Lattice method populate.

private static boolean populate(List<RelNode> nodes, List<int[][]> tempLinks, RelNode rel) {
    if (nodes.isEmpty() && rel instanceof LogicalProject) {
        return populate(nodes, tempLinks, ((LogicalProject) rel).getInput());
    }
    if (rel instanceof TableScan) {
        nodes.add(rel);
        return true;
    }
    if (rel instanceof LogicalJoin) {
        LogicalJoin join = (LogicalJoin) rel;
        if (join.getJoinType() != JoinRelType.INNER) {
            throw new RuntimeException("only inner join allowed, but got " + join.getJoinType());
        }
        populate(nodes, tempLinks, join.getLeft());
        populate(nodes, tempLinks, join.getRight());
        for (RexNode rex : RelOptUtil.conjunctions(join.getCondition())) {
            tempLinks.add(grab(nodes, rex));
        }
        return true;
    }
    throw new RuntimeException("Invalid node type " + rel.getClass().getSimpleName() + " in lattice query");
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with TableScan

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

the class RelOptMaterialization method tryUseStar.

/**
 * Converts a relational expression to one that uses a
 * {@link org.apache.calcite.schema.impl.StarTable}.
 *
 * <p>The relational expression is already in leaf-join-form, per
 * {@link #toLeafJoinForm(org.apache.calcite.rel.RelNode)}.
 *
 * @return Rewritten expression, or null if expression cannot be rewritten
 * to use the star
 */
public static RelNode tryUseStar(RelNode rel, final RelOptTable starRelOptTable) {
    final StarTable starTable = starRelOptTable.unwrap(StarTable.class);
    assert starTable != null;
    RelNode rel2 = rel.accept(new RelShuttleImpl() {

        @Override
        public RelNode visit(TableScan scan) {
            RelOptTable relOptTable = scan.getTable();
            final Table table = relOptTable.unwrap(Table.class);
            if (table.equals(starTable.tables.get(0))) {
                Mappings.TargetMapping mapping = Mappings.createShiftMapping(starRelOptTable.getRowType().getFieldCount(), 0, 0, relOptTable.getRowType().getFieldCount());
                final RelOptCluster cluster = scan.getCluster();
                final RelNode scan2 = starRelOptTable.toRel(RelOptUtil.getContext(cluster));
                return RelOptUtil.createProject(scan2, Mappings.asList(mapping.inverse()));
            }
            return scan;
        }

        @Override
        public RelNode visit(LogicalJoin join) {
            for (; ; ) {
                RelNode rel = super.visit(join);
                if (rel == join || !(rel instanceof LogicalJoin)) {
                    return rel;
                }
                join = (LogicalJoin) rel;
                final ProjectFilterTable left = ProjectFilterTable.of(join.getLeft());
                if (left != null) {
                    final ProjectFilterTable right = ProjectFilterTable.of(join.getRight());
                    if (right != null) {
                        try {
                            match(left, right, join.getCluster());
                        } catch (Util.FoundOne e) {
                            return (RelNode) e.getNode();
                        }
                    }
                }
            }
        }

        /**
         * Throws a {@link org.apache.calcite.util.Util.FoundOne} containing
         * a {@link org.apache.calcite.rel.logical.LogicalTableScan} on
         * success.  (Yes, an exception for normal operation.)
         */
        private void match(ProjectFilterTable left, ProjectFilterTable right, RelOptCluster cluster) {
            final Mappings.TargetMapping leftMapping = left.mapping();
            final Mappings.TargetMapping rightMapping = right.mapping();
            final RelOptTable leftRelOptTable = left.getTable();
            final Table leftTable = leftRelOptTable.unwrap(Table.class);
            final int leftCount = leftRelOptTable.getRowType().getFieldCount();
            final RelOptTable rightRelOptTable = right.getTable();
            final Table rightTable = rightRelOptTable.unwrap(Table.class);
            if (leftTable instanceof StarTable && ((StarTable) leftTable).tables.contains(rightTable)) {
                final int offset = ((StarTable) leftTable).columnOffset(rightTable);
                Mappings.TargetMapping mapping = Mappings.merge(leftMapping, Mappings.offsetTarget(Mappings.offsetSource(rightMapping, offset), leftMapping.getTargetCount()));
                final RelNode project = RelOptUtil.createProject(LogicalTableScan.create(cluster, leftRelOptTable), Mappings.asList(mapping.inverse()));
                final List<RexNode> conditions = Lists.newArrayList();
                if (left.condition != null) {
                    conditions.add(left.condition);
                }
                if (right.condition != null) {
                    conditions.add(RexUtil.apply(mapping, RexUtil.shift(right.condition, offset)));
                }
                final RelNode filter = RelOptUtil.createFilter(project, conditions);
                throw new Util.FoundOne(filter);
            }
            if (rightTable instanceof StarTable && ((StarTable) rightTable).tables.contains(leftTable)) {
                final int offset = ((StarTable) rightTable).columnOffset(leftTable);
                Mappings.TargetMapping mapping = Mappings.merge(Mappings.offsetSource(leftMapping, offset), Mappings.offsetTarget(rightMapping, leftCount));
                final RelNode project = RelOptUtil.createProject(LogicalTableScan.create(cluster, rightRelOptTable), Mappings.asList(mapping.inverse()));
                final List<RexNode> conditions = Lists.newArrayList();
                if (left.condition != null) {
                    conditions.add(RexUtil.apply(mapping, RexUtil.shift(left.condition, offset)));
                }
                if (right.condition != null) {
                    conditions.add(RexUtil.apply(mapping, right.condition));
                }
                final RelNode filter = RelOptUtil.createFilter(project, conditions);
                throw new Util.FoundOne(filter);
            }
        }
    });
    if (rel2 == rel) {
        // No rewrite happened.
        return null;
    }
    final Program program = Programs.hep(ImmutableList.of(ProjectFilterTransposeRule.INSTANCE, AggregateProjectMergeRule.INSTANCE, AggregateFilterTransposeRule.INSTANCE), false, DefaultRelMetadataProvider.INSTANCE);
    return program.run(null, rel2, null, ImmutableList.<RelOptMaterialization>of(), ImmutableList.<RelOptLattice>of());
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) Table(org.apache.calcite.schema.Table) StarTable(org.apache.calcite.schema.impl.StarTable) Program(org.apache.calcite.tools.Program) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) RexUtil(org.apache.calcite.rex.RexUtil) Util(org.apache.calcite.util.Util) RelNode(org.apache.calcite.rel.RelNode) StarTable(org.apache.calcite.schema.impl.StarTable) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 49 with TableScan

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.TableScan in project drill by apache.

the class ConvertCountToDirectScanRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final Aggregate agg = call.rel(0);
    final TableScan scan = call.rel(call.rels.length - 1);
    final Project project = call.rels.length == 3 ? (Project) call.rel(1) : null;
    // 3) Additional checks are done further below ..
    if (agg.getGroupCount() > 0 || agg.containsDistinctCall()) {
        return;
    }
    DrillTable drillTable = DrillRelOptUtil.getDrillTable(scan);
    if (drillTable == null) {
        logger.debug("Rule does not apply since an eligible drill table instance was not found.");
        return;
    }
    Object selection = drillTable.getSelection();
    if (!(selection instanceof FormatSelection)) {
        logger.debug("Rule does not apply since only Parquet file format is eligible.");
        return;
    }
    PlannerSettings settings = call.getPlanner().getContext().unwrap(PlannerSettings.class);
    // Rule is applicable only if the statistics for row count and null count are available from the metadata,
    FormatSelection formatSelection = (FormatSelection) selection;
    // Rule cannot be applied if the selection had wildcard since the totalrowcount cannot be read from the parent directory
    if (formatSelection.getSelection().hadWildcard()) {
        logger.debug("Rule does not apply when there is a wild card since the COUNT could not be determined from metadata.");
        return;
    }
    Pair<Boolean, Metadata_V4.MetadataSummary> status = checkMetadataForScanStats(settings, drillTable, formatSelection);
    if (!status.getLeft()) {
        logger.debug("Rule does not apply since MetadataSummary metadata was not found.");
        return;
    }
    Metadata_V4.MetadataSummary metadataSummary = status.getRight();
    Map<String, Long> result = collectCounts(settings, metadataSummary, agg, scan, project);
    logger.trace("Calculated the following aggregate counts: {}", result);
    // if counts could not be determined, rule won't be applied
    if (result.isEmpty()) {
        logger.debug("Rule does not apply since one or more COUNTs could not be determined from metadata.");
        return;
    }
    Path summaryFileName = Metadata.getSummaryFileName(formatSelection.getSelection().getSelectionRoot());
    final RelDataType scanRowType = CountToDirectScanUtils.constructDataType(agg, result.keySet());
    final DynamicPojoRecordReader<Long> reader = new DynamicPojoRecordReader<>(CountToDirectScanUtils.buildSchema(scanRowType.getFieldNames()), Collections.singletonList(new ArrayList<>(result.values())));
    final ScanStats scanStats = new ScanStats(ScanStats.GroupScanProperty.EXACT_ROW_COUNT, 1, 1, scanRowType.getFieldCount());
    final MetadataDirectGroupScan directScan = new MetadataDirectGroupScan(reader, summaryFileName, 1, scanStats, true, false);
    final DrillDirectScanRel newScan = new DrillDirectScanRel(scan.getCluster(), scan.getTraitSet().plus(DrillRel.DRILL_LOGICAL), directScan, scanRowType);
    final DrillProjectRel newProject = new DrillProjectRel(agg.getCluster(), agg.getTraitSet().plus(DrillRel.DRILL_LOGICAL), newScan, CountToDirectScanUtils.prepareFieldExpressions(scanRowType), agg.getRowType());
    call.transformTo(newProject);
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaPath(org.apache.drill.common.expression.SchemaPath) TableScan(org.apache.calcite.rel.core.TableScan) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) DynamicPojoRecordReader(org.apache.drill.exec.store.pojo.DynamicPojoRecordReader) ArrayList(java.util.ArrayList) FormatSelection(org.apache.drill.exec.store.dfs.FormatSelection) RelDataType(org.apache.calcite.rel.type.RelDataType) Project(org.apache.calcite.rel.core.Project) MetadataDirectGroupScan(org.apache.drill.exec.store.direct.MetadataDirectGroupScan) Metadata_V4(org.apache.drill.exec.store.parquet.metadata.Metadata_V4) Aggregate(org.apache.calcite.rel.core.Aggregate) ScanStats(org.apache.drill.exec.physical.base.ScanStats)

Example 50 with TableScan

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.TableScan in project drill by apache.

the class FindLimit0Visitor method addLimitOnTopOfLeafNodes.

public static DrillRel addLimitOnTopOfLeafNodes(final DrillRel rel) {
    final Pointer<Boolean> isUnsupported = new Pointer<>(false);
    // to visit unsupported functions
    final RexShuttle unsupportedFunctionsVisitor = new RexShuttle() {

        @Override
        public RexNode visitCall(RexCall call) {
            final SqlOperator operator = call.getOperator();
            if (isUnsupportedScalarFunction(operator)) {
                isUnsupported.value = true;
                return call;
            }
            return super.visitCall(call);
        }
    };
    // to visit unsupported operators
    final RelShuttle unsupportedOperationsVisitor = new RelShuttleImpl() {

        @Override
        public RelNode visit(RelNode other) {
            if (other instanceof DrillUnionRelBase) {
                isUnsupported.value = true;
                return other;
            } else if (other instanceof DrillProjectRelBase) {
                if (!isUnsupported.value) {
                    other.accept(unsupportedFunctionsVisitor);
                }
                if (isUnsupported.value) {
                    return other;
                }
            }
            return super.visit(other);
        }
    };
    rel.accept(unsupportedOperationsVisitor);
    if (isUnsupported.value) {
        return rel;
    }
    // to add LIMIT (0) on top of leaf nodes
    final RelShuttle addLimitOnScanVisitor = new RelShuttleImpl() {

        private RelNode addLimitAsParent(RelNode node) {
            final RexBuilder builder = node.getCluster().getRexBuilder();
            final RexLiteral offset = builder.makeExactLiteral(BigDecimal.ZERO);
            final RexLiteral fetch = builder.makeExactLiteral(BigDecimal.ZERO);
            return new DrillLimitRel(node.getCluster(), node.getTraitSet(), node, offset, fetch);
        }

        @Override
        public RelNode visit(LogicalValues values) {
            return addLimitAsParent(values);
        }

        @Override
        public RelNode visit(TableScan scan) {
            return addLimitAsParent(scan);
        }

        @Override
        public RelNode visit(RelNode other) {
            if (other.getInputs().isEmpty()) {
                // leaf operator
                return addLimitAsParent(other);
            }
            return super.visit(other);
        }
    };
    return (DrillRel) rel.accept(addLimitOnScanVisitor);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TableScan(org.apache.calcite.rel.core.TableScan) RexShuttle(org.apache.calcite.rex.RexShuttle) DrillSqlOperator(org.apache.drill.exec.planner.sql.DrillSqlOperator) SqlOperator(org.apache.calcite.sql.SqlOperator) RelShuttle(org.apache.calcite.rel.RelShuttle) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) Pointer(org.apache.drill.exec.util.Pointer) DrillProjectRelBase(org.apache.drill.exec.planner.common.DrillProjectRelBase) DrillLimitRel(org.apache.drill.exec.planner.logical.DrillLimitRel) LogicalValues(org.apache.calcite.rel.logical.LogicalValues) RexCall(org.apache.calcite.rex.RexCall) RelNode(org.apache.calcite.rel.RelNode) DrillUnionRelBase(org.apache.drill.exec.planner.common.DrillUnionRelBase) RexBuilder(org.apache.calcite.rex.RexBuilder) DrillRel(org.apache.drill.exec.planner.logical.DrillRel)

Aggregations

TableScan (org.apache.calcite.rel.core.TableScan)51 RelNode (org.apache.calcite.rel.RelNode)19 ArrayList (java.util.ArrayList)14 PlannerSettings (org.apache.drill.exec.planner.physical.PlannerSettings)13 Project (org.apache.calcite.rel.core.Project)12 DrillScanRel (org.apache.drill.exec.planner.logical.DrillScanRel)11 Filter (org.apache.calcite.rel.core.Filter)10 IOException (java.io.IOException)9 RexNode (org.apache.calcite.rex.RexNode)9 GroupScan (org.apache.drill.exec.physical.base.GroupScan)9 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)8 DrillFilterRel (org.apache.drill.exec.planner.logical.DrillFilterRel)8 Aggregate (org.apache.calcite.rel.core.Aggregate)7 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)7 RexBuilder (org.apache.calcite.rex.RexBuilder)6 RelOptCluster (org.apache.calcite.plan.RelOptCluster)5 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)5 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SchemaPath (org.apache.drill.common.expression.SchemaPath)5