Search in sources :

Example 1 with DrillScanRelBase

use of org.apache.drill.exec.planner.common.DrillScanRelBase in project drill by apache.

the class DrillRelMdSelectivity method getScanSelectivity.

private Double getScanSelectivity(RelNode rel, RelMetadataQuery mq, RexNode predicate) {
    double ROWCOUNT_UNKNOWN = -1.0;
    GroupScan scan = null;
    PlannerSettings settings = PrelUtil.getPlannerSettings(rel.getCluster().getPlanner());
    final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    if (rel instanceof DrillScanRel) {
        scan = ((DrillScanRel) rel).getGroupScan();
    } else if (rel instanceof ScanPrel) {
        scan = ((ScanPrel) rel).getGroupScan();
    }
    if (scan != null) {
        if (settings.isStatisticsEnabled() && scan instanceof DbGroupScan) {
            double filterRows = ((DbGroupScan) scan).getRowCount(predicate, rel);
            double totalRows = ((DbGroupScan) scan).getRowCount(null, rel);
            if (filterRows != ROWCOUNT_UNKNOWN && totalRows != ROWCOUNT_UNKNOWN && totalRows > 0) {
                return Math.min(1.0, filterRows / totalRows);
            }
        }
    }
    // Do not mess with statistics used for DBGroupScans.
    if (rel instanceof TableScan) {
        if (DrillRelOptUtil.guessRows(rel)) {
            return super.getSelectivity(rel, mq, predicate);
        }
        DrillTable table = Utilities.getDrillTable(rel.getTable());
        try {
            TableMetadata tableMetadata;
            if (table != null && (tableMetadata = table.getGroupScan().getTableMetadata()) != null && TableStatisticsKind.HAS_DESCRIPTIVE_STATISTICS.getValue(tableMetadata)) {
                List<SchemaPath> fieldNames;
                if (rel instanceof DrillScanRelBase) {
                    fieldNames = ((DrillScanRelBase) rel).getGroupScan().getColumns();
                } else {
                    fieldNames = rel.getRowType().getFieldNames().stream().map(SchemaPath::getSimplePath).collect(Collectors.toList());
                }
                return getScanSelectivityInternal(tableMetadata, predicate, fieldNames, rexBuilder);
            }
        } catch (IOException e) {
            super.getSelectivity(rel, mq, predicate);
        }
    }
    return super.getSelectivity(rel, mq, predicate);
}
Also used : TableMetadata(org.apache.drill.metastore.metadata.TableMetadata) TableScan(org.apache.calcite.rel.core.TableScan) DrillScanRel(org.apache.drill.exec.planner.logical.DrillScanRel) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) IOException(java.io.IOException) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) GroupScan(org.apache.drill.exec.physical.base.GroupScan) SchemaPath(org.apache.drill.common.expression.SchemaPath) DbGroupScan(org.apache.drill.exec.physical.base.DbGroupScan) DrillScanRelBase(org.apache.drill.exec.planner.common.DrillScanRelBase) RexBuilder(org.apache.calcite.rex.RexBuilder)

Example 2 with DrillScanRelBase

use of org.apache.drill.exec.planner.common.DrillScanRelBase in project drill by apache.

the class JoinPruleBase method createRangePartitionRightPlan.

protected void createRangePartitionRightPlan(RelOptRuleCall call, RowKeyJoinRel join, PhysicalJoinType physicalJoinType, boolean implementAsRowKeyJoin, RelNode left, RelNode right, RelCollation collationLeft, RelCollation collationRight) throws InvalidRelException {
    assert join.getRightKeys().size() == 1 : "Cannot create range partition plan with multi-column join condition";
    int joinKeyRight = join.getRightKeys().get(0);
    List<DrillDistributionTrait.DistributionField> rangeDistFields = Lists.newArrayList(new DrillDistributionTrait.DistributionField(joinKeyRight));
    List<FieldReference> rangeDistRefList = Lists.newArrayList();
    FieldReference rangeDistRef = FieldReference.getWithQuotedRef(right.getRowType().getFieldList().get(joinKeyRight).getName());
    rangeDistRefList.add(rangeDistRef);
    RelNode leftScan = DrillPushRowKeyJoinToScanRule.getValidJoinInput(left);
    DrillDistributionTrait rangePartRight = new DrillDistributionTrait(DrillDistributionTrait.DistributionType.RANGE_DISTRIBUTED, ImmutableList.copyOf(rangeDistFields), ((DbGroupScan) ((DrillScanRelBase) leftScan).getGroupScan()).getRangePartitionFunction(rangeDistRefList));
    RelTraitSet traitsLeft = null;
    RelTraitSet traitsRight = null;
    if (physicalJoinType == PhysicalJoinType.HASH_JOIN) {
        traitsLeft = left.getTraitSet().plus(Prel.DRILL_PHYSICAL);
        traitsRight = right.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(rangePartRight);
    }
    final RelNode convertedLeft = convert(left, traitsLeft);
    final RelNode convertedRight = convert(right, traitsRight);
    DrillJoinRelBase newJoin = null;
    if (physicalJoinType == PhysicalJoinType.HASH_JOIN) {
        if (implementAsRowKeyJoin) {
            newJoin = new RowKeyJoinPrel(join.getCluster(), traitsLeft, convertedLeft, convertedRight, join.getCondition(), join.getJoinType(), join.isSemiJoin());
        } else {
            newJoin = new HashJoinPrel(join.getCluster(), traitsLeft, convertedLeft, convertedRight, join.getCondition(), join.getJoinType(), false, /* no swap */
            null, /* no runtime filter */
            true, /* useful for join-restricted scans */
            JoinControl.DEFAULT, join.isSemiJoin());
        }
    }
    if (newJoin != null) {
        call.transformTo(newJoin);
    }
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillJoinRelBase(org.apache.drill.exec.planner.common.DrillJoinRelBase) RelNode(org.apache.calcite.rel.RelNode) DistributionField(org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField) DrillScanRelBase(org.apache.drill.exec.planner.common.DrillScanRelBase) DistributionField(org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)

Example 3 with DrillScanRelBase

use of org.apache.drill.exec.planner.common.DrillScanRelBase in project drill by apache.

the class DrillRelMdRowCount method getRowCount.

@Override
public Double getRowCount(TableScan rel, RelMetadataQuery mq) {
    DrillTable table = Utilities.getDrillTable(rel.getTable());
    PlannerSettings settings = PrelUtil.getSettings(rel.getCluster());
    // If guessing, return selectivity from RelMDRowCount
    if (DrillRelOptUtil.guessRows(rel)) {
        return super.getRowCount(rel, mq);
    }
    // Return rowcount from statistics, if available. Otherwise, delegate to parent.
    try {
        if (table != null && table.getGroupScan().getTableMetadata() != null && TableStatisticsKind.HAS_DESCRIPTIVE_STATISTICS.getValue(table.getGroupScan().getTableMetadata())) {
            /* For GroupScan rely on accurate count from the scan, if available, instead of
           * statistics since partition pruning/filter pushdown might have occurred.
           * e.g. ParquetGroupScan returns accurate rowcount. The other way would be to
           * iterate over the rowgroups present in the GroupScan to compute the rowcount.
           */
            if (!table.getGroupScan().getScanStats(settings).getGroupScanProperty().hasExactRowCount()) {
                return TableStatisticsKind.EST_ROW_COUNT.getValue(table.getGroupScan().getTableMetadata());
            } else {
                if (!(rel instanceof DrillScanRelBase)) {
                    return table.getGroupScan().getScanStats(settings).getRecordCount();
                }
            }
        }
    } catch (IOException ex) {
        return super.getRowCount(rel, mq);
    }
    return super.getRowCount(rel, mq);
}
Also used : DrillTable(org.apache.drill.exec.planner.logical.DrillTable) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) DrillScanRelBase(org.apache.drill.exec.planner.common.DrillScanRelBase) IOException(java.io.IOException)

Aggregations

DrillScanRelBase (org.apache.drill.exec.planner.common.DrillScanRelBase)3 IOException (java.io.IOException)2 DrillTable (org.apache.drill.exec.planner.logical.DrillTable)2 PlannerSettings (org.apache.drill.exec.planner.physical.PlannerSettings)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 RelNode (org.apache.calcite.rel.RelNode)1 TableScan (org.apache.calcite.rel.core.TableScan)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 FieldReference (org.apache.drill.common.expression.FieldReference)1 SchemaPath (org.apache.drill.common.expression.SchemaPath)1 DbGroupScan (org.apache.drill.exec.physical.base.DbGroupScan)1 GroupScan (org.apache.drill.exec.physical.base.GroupScan)1 DrillJoinRelBase (org.apache.drill.exec.planner.common.DrillJoinRelBase)1 DrillScanRel (org.apache.drill.exec.planner.logical.DrillScanRel)1 DistributionField (org.apache.drill.exec.planner.physical.DrillDistributionTrait.DistributionField)1 ScanPrel (org.apache.drill.exec.planner.physical.ScanPrel)1 TableMetadata (org.apache.drill.metastore.metadata.TableMetadata)1