Search in sources :

Example 76 with RexNode

use of org.apache.calcite.rex.RexNode in project drill by apache.

the class DrillRelOptUtil method containIdentity.

/**
   * Returns whether the leading edge of a given array of expressions is
   * wholly {@link RexInputRef} objects with types and names corresponding
   * to the underlying row type. */
private static boolean containIdentity(List<? extends RexNode> exps, RelDataType rowType, RelDataType childRowType) {
    List<RelDataTypeField> fields = rowType.getFieldList();
    List<RelDataTypeField> childFields = childRowType.getFieldList();
    int fieldCount = childFields.size();
    if (exps.size() != fieldCount) {
        return false;
    }
    for (int i = 0; i < exps.size(); i++) {
        RexNode exp = exps.get(i);
        if (!(exp instanceof RexInputRef)) {
            return false;
        }
        RexInputRef var = (RexInputRef) exp;
        if (var.getIndex() != i) {
            return false;
        }
        if (!fields.get(i).getName().equals(childFields.get(i).getName())) {
            return false;
        }
        if (!fields.get(i).getType().equals(childFields.get(i).getType())) {
            return false;
        }
    }
    return true;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 77 with RexNode

use of org.apache.calcite.rex.RexNode in project drill by apache.

the class DrillFilterRelBase method estimateCpuCost.

/* Given the condition (C1 and C2 and C3 and ... C_n), here is how to estimate cpu cost of FILTER :
  *  Let's say child's rowcount is n. We assume short circuit evaluation will be applied to the boolean expression evaluation.
  *  #_of_comparison = n + n * Selectivity(C1) + n * Selectivity(C1 and C2) + ... + n * Selecitivity(C1 and C2 ... and C_n)
  *  cpu_cost = #_of_comparison * DrillCostBase_COMPARE_CPU_COST;
  */
private double estimateCpuCost(RelMetadataQuery mq) {
    RelNode child = this.getInput();
    double compNum = mq.getRowCount(child);
    for (int i = 0; i < numConjuncts; i++) {
        RexNode conjFilter = RexUtil.composeConjunction(this.getCluster().getRexBuilder(), conjunctions.subList(0, i + 1), false);
        compNum += Filter.estimateFilteredRows(child, conjFilter);
    }
    return compNum * DrillCostBase.COMPARE_CPU_COST;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Example 78 with RexNode

use of org.apache.calcite.rex.RexNode in project drill by apache.

the class ConvertHiveParquetScanToDrillParquetScan method createProjectRel.

/**
   * Create a project that converts the native scan output to expected output of Hive scan.
   */
private DrillProjectRel createProjectRel(final DrillScanRel hiveScanRel, final Map<String, String> partitionColMapping, final DrillScanRel nativeScanRel) {
    final List<RexNode> rexNodes = Lists.newArrayList();
    final RexBuilder rb = hiveScanRel.getCluster().getRexBuilder();
    final RelDataType hiveScanRowType = hiveScanRel.getRowType();
    for (String colName : hiveScanRowType.getFieldNames()) {
        final String dirColName = partitionColMapping.get(colName);
        if (dirColName != null) {
            rexNodes.add(createPartitionColumnCast(hiveScanRel, nativeScanRel, colName, dirColName, rb));
        } else {
            rexNodes.add(createColumnFormatConversion(hiveScanRel, nativeScanRel, colName, rb));
        }
    }
    return DrillProjectRel.create(hiveScanRel.getCluster(), hiveScanRel.getTraitSet(), nativeScanRel, rexNodes, hiveScanRowType);
}
Also used : RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 79 with RexNode

use of org.apache.calcite.rex.RexNode in project drill by apache.

the class ProjectAllowDupPrel method getProjectExpressions.

@Override
protected List<NamedExpression> getProjectExpressions(DrillParseContext context) {
    List<NamedExpression> expressions = Lists.newArrayList();
    for (Pair<RexNode, String> pair : Pair.zip(exps, getRowType().getFieldNames())) {
        LogicalExpression expr = DrillOptiq.toDrill(context, getInput(), pair.left);
        expressions.add(new NamedExpression(expr, FieldReference.getWithQuotedRef(pair.right)));
    }
    return expressions;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 80 with RexNode

use of org.apache.calcite.rex.RexNode in project drill by apache.

the class LimitUnionExchangeTransposeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final LimitPrel limit = (LimitPrel) call.rel(0);
    final UnionExchangePrel unionExchangePrel = (UnionExchangePrel) call.rel(1);
    RelNode child = unionExchangePrel.getInput();
    final int offset = limit.getOffset() != null ? Math.max(0, RexLiteral.intValue(limit.getOffset())) : 0;
    final int fetch = Math.max(0, RexLiteral.intValue(limit.getFetch()));
    // child Limit uses conservative approach:  use offset 0 and fetch = parent limit offset + parent limit fetch.
    final RexNode childFetch = limit.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(offset + fetch));
    final RelNode limitUnderExchange = new LimitPrel(child.getCluster(), child.getTraitSet(), child, null, childFetch);
    final RelNode newUnionExch = new UnionExchangePrel(unionExchangePrel.getCluster(), unionExchangePrel.getTraitSet(), limitUnderExchange);
    final RelNode limitAboveExchange = new LimitPrel(limit.getCluster(), limit.getTraitSet(), newUnionExch, limit.getOffset(), limit.getFetch(), true);
    call.transformTo(limitAboveExchange);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexNode (org.apache.calcite.rex.RexNode)204 RelNode (org.apache.calcite.rel.RelNode)75 ArrayList (java.util.ArrayList)68 RexBuilder (org.apache.calcite.rex.RexBuilder)52 RelDataType (org.apache.calcite.rel.type.RelDataType)48 RexInputRef (org.apache.calcite.rex.RexInputRef)43 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)41 RexCall (org.apache.calcite.rex.RexCall)32 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)28 Pair (org.apache.calcite.util.Pair)22 HashMap (java.util.HashMap)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)21 RexLiteral (org.apache.calcite.rex.RexLiteral)19 ImmutableList (com.google.common.collect.ImmutableList)18 Project (org.apache.calcite.rel.core.Project)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)14 Filter (org.apache.calcite.rel.core.Filter)12 HashSet (java.util.HashSet)11 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)11 BigDecimal (java.math.BigDecimal)10