Search in sources :

Example 66 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class AbstractIndexPlanGenerator method go.

public void go() throws InvalidRelException {
    RelNode top = indexContext.getCall().rel(0);
    final RelNode input;
    if (top instanceof DrillProjectRel) {
        DrillProjectRel topProject = (DrillProjectRel) top;
        input = topProject.getInput();
    } else if (top instanceof DrillFilterRel) {
        DrillFilterRel topFilter = (DrillFilterRel) top;
        input = topFilter.getInput();
    } else if (top instanceof DrillSortRel) {
        DrillSortRel topSort = (DrillSortRel) top;
        input = topSort.getInput();
    } else if (top instanceof DrillSortRel) {
        DrillSortRel topSort = (DrillSortRel) top;
        input = topSort.getInput();
    } else {
        return;
    }
    RelTraitSet traits = input.getTraitSet().plus(Prel.DRILL_PHYSICAL);
    RelNode convertedInput = Prule.convert(input, traits);
    this.go(top, convertedInput);
}
Also used : DrillSortRel(org.apache.drill.exec.planner.logical.DrillSortRel) RelNode(org.apache.calcite.rel.RelNode) DrillProjectRel(org.apache.drill.exec.planner.logical.DrillProjectRel) DrillFilterRel(org.apache.drill.exec.planner.logical.DrillFilterRel) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 67 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class IndexPlanUtils method buildCoveringIndexScan.

public static ScanPrel buildCoveringIndexScan(DrillScanRelBase origScan, IndexGroupScan indexGroupScan, IndexCallContext indexContext, IndexDescriptor indexDesc) {
    FunctionalIndexInfo functionInfo = indexDesc.getFunctionalInfo();
    // to record the new (renamed)paths added
    List<SchemaPath> rewrittenPaths = Lists.newArrayList();
    DbGroupScan dbGroupScan = (DbGroupScan) getGroupScan(origScan);
    indexGroupScan.setColumns(rewriteFunctionColumn(dbGroupScan.getColumns(), functionInfo, rewrittenPaths));
    DrillDistributionTrait partition = scanIsPartition(getGroupScan(origScan)) ? DrillDistributionTrait.RANDOM_DISTRIBUTED : DrillDistributionTrait.SINGLETON;
    RelDataType newRowType = FunctionalIndexHelper.rewriteFunctionalRowType(origScan, indexContext, functionInfo, rewrittenPaths);
    // add a default collation trait otherwise Calcite runs into a ClassCastException, which at first glance
    // seems like a Calcite bug
    RelTraitSet indexScanTraitSet = origScan.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(RelCollationTraitDef.INSTANCE.getDefault()).plus(partition);
    // condition that the index actually has collation property (e.g hash indexes don't)
    if (indexDesc.getCollation() != null) {
        RelCollation collationTrait = buildCollationCoveringIndexScan(indexDesc, indexContext);
        indexScanTraitSet = indexScanTraitSet.plus(collationTrait);
    }
    ScanPrel indexScanPrel = new ScanPrel(origScan.getCluster(), indexScanTraitSet, indexGroupScan, newRowType, origScan.getTable());
    return indexScanPrel;
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) SchemaPath(org.apache.drill.common.expression.SchemaPath) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillDistributionTrait(org.apache.drill.exec.planner.physical.DrillDistributionTrait)

Example 68 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class IndexIntersectPlanGenerator method buildRestrictedDBScan.

private Pair<RelNode, DbGroupScan> buildRestrictedDBScan(RexNode remnant, boolean isAnyIndexAsync) {
    DbGroupScan origDbGroupScan = (DbGroupScan) IndexPlanUtils.getGroupScan(origScan);
    List<SchemaPath> cols = new ArrayList<SchemaPath>(origDbGroupScan.getColumns());
    if (!checkRowKey(cols)) {
        cols.add(origDbGroupScan.getRowKeyPath());
    }
    // Create a restricted groupscan from the primary table's groupscan
    DbGroupScan restrictedGroupScan = origDbGroupScan.getRestrictedScan(cols);
    if (restrictedGroupScan == null) {
        logger.error("Null restricted groupscan in IndexIntersectPlanGenerator.convertChild");
        return null;
    }
    DrillDistributionTrait partition = IndexPlanUtils.scanIsPartition(IndexPlanUtils.getGroupScan(origScan)) ? DrillDistributionTrait.RANDOM_DISTRIBUTED : DrillDistributionTrait.SINGLETON;
    RelNode lastRelNode;
    RelDataType dbscanRowType = convertRowType(origScan.getRowType(), origScan.getCluster().getTypeFactory());
    ScanPrel dbScan = new ScanPrel(origScan.getCluster(), origScan.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(partition), restrictedGroupScan, dbscanRowType, origScan.getTable());
    lastRelNode = dbScan;
    // build the row type for the left Project
    List<RexNode> leftProjectExprs = Lists.newArrayList();
    int leftRowKeyIndex = getRowKeyIndex(dbScan.getRowType(), origScan);
    final RelDataTypeField leftRowKeyField = dbScan.getRowType().getFieldList().get(leftRowKeyIndex);
    final RelDataTypeFactory.FieldInfoBuilder leftFieldTypeBuilder = dbScan.getCluster().getTypeFactory().builder();
    FilterPrel leftIndexFilterPrel = null;
    // full original condition.
    if (isAnyIndexAsync) {
        new FilterPrel(dbScan.getCluster(), dbScan.getTraitSet(), dbScan, indexContext.getOrigCondition());
        lastRelNode = leftIndexFilterPrel;
    }
    // new Project's rowtype is original Project's rowtype [plus rowkey if rowkey is not in original rowtype]
    ProjectPrel leftIndexProjectPrel = null;
    if (origProject != null) {
        RelDataType origRowType = origProject.getRowType();
        List<RelDataTypeField> origProjFields = origRowType.getFieldList();
        leftFieldTypeBuilder.addAll(origProjFields);
        // get the exprs from the original Project IFF there is a project
        leftProjectExprs.addAll(IndexPlanUtils.getProjects(origProject));
        // add the rowkey IFF rowkey is not in orig scan
        if (getRowKeyIndex(origRowType, origScan) < 0) {
            leftFieldTypeBuilder.add(leftRowKeyField);
            leftProjectExprs.add(RexInputRef.of(leftRowKeyIndex, dbScan.getRowType()));
        }
        final RelDataType leftProjectRowType = leftFieldTypeBuilder.build();
        leftIndexProjectPrel = new ProjectPrel(dbScan.getCluster(), dbScan.getTraitSet(), leftIndexFilterPrel == null ? dbScan : leftIndexFilterPrel, leftProjectExprs, leftProjectRowType);
        lastRelNode = leftIndexProjectPrel;
    }
    final RelTraitSet leftTraits = dbScan.getTraitSet().plus(Prel.DRILL_PHYSICAL);
    // final RelNode convertedLeft = convert(leftIndexProjectPrel, leftTraits);
    final RelNode convertedLeft = Prule.convert(lastRelNode, leftTraits);
    return new Pair<>(convertedLeft, restrictedGroupScan);
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillDistributionTrait(org.apache.drill.exec.planner.physical.DrillDistributionTrait) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) SchemaPath(org.apache.drill.common.expression.SchemaPath) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexNode(org.apache.calcite.rex.RexNode) FilterPrel(org.apache.drill.exec.planner.physical.FilterPrel) Pair(org.apache.calcite.util.Pair)

Example 69 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class MetadataAggPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    MetadataAggRel aggregate = call.rel(0);
    RelNode input = aggregate.getInput();
    int groupByExprsSize = aggregate.getContext().groupByExpressions().size();
    List<RelFieldCollation> collations = new ArrayList<>();
    List<String> names = new ArrayList<>();
    for (int i = 0; i < groupByExprsSize; i++) {
        collations.add(new RelFieldCollation(i + 1));
        SchemaPath fieldPath = getArgumentReference(aggregate.getContext().groupByExpressions().get(i));
        names.add(fieldPath.getRootSegmentPath());
    }
    RelCollation collation = new NamedRelCollation(collations, names);
    RelTraitSet traits;
    if (aggregate.getContext().groupByExpressions().isEmpty()) {
        DrillDistributionTrait singleDist = DrillDistributionTrait.SINGLETON;
        RelTraitSet singleDistTrait = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(singleDist);
        createTransformRequest(call, aggregate, input, singleDistTrait);
    } else {
        // hash distribute on all grouping keys
        DrillDistributionTrait distOnAllKeys = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.HASH_DISTRIBUTED, ImmutableList.copyOf(getDistributionFields(aggregate.getContext().groupByExpressions())));
        PlannerSettings settings = PrelUtil.getPlannerSettings(call.getPlanner());
        boolean smallInput = input.estimateRowCount(input.getCluster().getMetadataQuery()) < settings.getSliceTarget();
        // to produce sort locally before aggregation is produced for large inputs
        if (aggregate.getContext().createNewAggregations() && !smallInput) {
            traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL);
            RelNode convertedInput = convert(input, traits);
            new TwoPhaseMetadataAggSubsetTransformer(call, collation, distOnAllKeys).go(aggregate, convertedInput);
        } else {
            // TODO: DRILL-7433 - replace DrillDistributionTrait.SINGLETON with distOnAllKeys when parallelization for MetadataHandler is implemented
            traits = call.getPlanner().emptyTraitSet().plus(Prel.DRILL_PHYSICAL).plus(collation).plus(DrillDistributionTrait.SINGLETON);
            createTransformRequest(call, aggregate, input, traits);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) MetadataAggRel(org.apache.drill.exec.planner.logical.MetadataAggRel) RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) SchemaPath(org.apache.drill.common.expression.SchemaPath) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 70 with RelTraitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet in project drill by apache.

the class ScanPrule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillScanRel scan = (DrillScanRel) call.rel(0);
    GroupScan groupScan = scan.getGroupScan();
    DrillDistributionTrait partition = (groupScan.getMaxParallelizationWidth() > 1 || groupScan.getDistributionAffinity() == DistributionAffinity.HARD) ? DrillDistributionTrait.RANDOM_DISTRIBUTED : DrillDistributionTrait.SINGLETON;
    final RelTraitSet traits = scan.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(partition);
    final ScanPrel newScan = new ScanPrel(scan.getCluster(), traits, groupScan, scan.getRowType(), scan.getTable());
    call.transformTo(newScan);
}
Also used : GroupScan(org.apache.drill.exec.physical.base.GroupScan) DrillScanRel(org.apache.drill.exec.planner.logical.DrillScanRel) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Aggregations

RelTraitSet (org.apache.calcite.plan.RelTraitSet)190 RelNode (org.apache.calcite.rel.RelNode)111 RelOptCluster (org.apache.calcite.plan.RelOptCluster)38 RelCollation (org.apache.calcite.rel.RelCollation)36 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)26 RexNode (org.apache.calcite.rex.RexNode)24 ArrayList (java.util.ArrayList)20 InvalidRelException (org.apache.calcite.rel.InvalidRelException)19 RelDataType (org.apache.calcite.rel.type.RelDataType)14 SqlNode (org.apache.calcite.sql.SqlNode)14 List (java.util.List)13 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)13 RelOptTable (org.apache.calcite.plan.RelOptTable)11 Sort (org.apache.calcite.rel.core.Sort)11 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)11 ImmutableList (com.google.common.collect.ImmutableList)10 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)10 Test (org.junit.Test)9 Table (org.apache.calcite.schema.Table)8 RexInputRef (org.apache.calcite.rex.RexInputRef)7