Search in sources :

Example 46 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project hive by apache.

the class HiveSubQRemoveRelBuilder method inferAlias.

/** Infers the alias of an expression.
   *
   * <p>If the expression was created by {@link #alias}, replaces the expression
   * in the project list.
   */
private String inferAlias(List<RexNode> exprList, RexNode expr) {
    switch(expr.getKind()) {
        case INPUT_REF:
            final RexInputRef ref = (RexInputRef) expr;
            return peek(0).getRowType().getFieldNames().get(ref.getIndex());
        case CAST:
            return inferAlias(exprList, ((RexCall) expr).getOperands().get(0));
        case AS:
            final RexCall call = (RexCall) expr;
            for (; ; ) {
                final int i = exprList.indexOf(expr);
                if (i < 0) {
                    break;
                }
                exprList.set(i, call.getOperands().get(0));
            }
            return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue()).getValue();
        default:
            return null;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) NlsString(org.apache.calcite.util.NlsString)

Example 47 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project hive by apache.

the class HiveCalciteUtil method createUDTFForSetOp.

public static HiveTableFunctionScan createUDTFForSetOp(RelOptCluster cluster, RelNode input) throws SemanticException {
    RelTraitSet traitSet = TraitsUtil.getDefaultTraitSet(cluster);
    List<RexNode> originalInputRefs = Lists.transform(input.getRowType().getFieldList(), new Function<RelDataTypeField, RexNode>() {

        @Override
        public RexNode apply(RelDataTypeField input) {
            return new RexInputRef(input.getIndex(), input.getType());
        }
    });
    ImmutableList.Builder<RelDataType> argTypeBldr = ImmutableList.<RelDataType>builder();
    for (int i = 0; i < originalInputRefs.size(); i++) {
        argTypeBldr.add(originalInputRefs.get(i).getType());
    }
    RelDataType retType = input.getRowType();
    String funcName = "replicate_rows";
    FunctionInfo fi = FunctionRegistry.getFunctionInfo(funcName);
    SqlOperator calciteOp = SqlFunctionConverter.getCalciteOperator(funcName, fi.getGenericUDTF(), argTypeBldr.build(), retType);
    // Hive UDTF only has a single input
    List<RelNode> list = new ArrayList<>();
    list.add(input);
    RexNode rexNode = cluster.getRexBuilder().makeCall(calciteOp, originalInputRefs);
    return HiveTableFunctionScan.create(cluster, traitSet, list, rexNode, null, retType, null);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) FunctionInfo(org.apache.hadoop.hive.ql.exec.FunctionInfo) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project druid by druid-io.

the class GroupByRules method toLimitSpec.

public static DefaultLimitSpec toLimitSpec(final List<String> rowOrder, final Sort sort) {
    final Integer limit = sort.fetch != null ? RexLiteral.intValue(sort.fetch) : null;
    final List<OrderByColumnSpec> orderBys = Lists.newArrayListWithCapacity(sort.getChildExps().size());
    if (sort.offset != null) {
        // LimitSpecs don't accept offsets.
        return null;
    }
    // Extract orderBy column specs.
    for (int sortKey = 0; sortKey < sort.getChildExps().size(); sortKey++) {
        final RexNode sortExpression = sort.getChildExps().get(sortKey);
        final RelFieldCollation collation = sort.getCollation().getFieldCollations().get(sortKey);
        final OrderByColumnSpec.Direction direction;
        final StringComparator comparator;
        if (collation.getDirection() == RelFieldCollation.Direction.ASCENDING) {
            direction = OrderByColumnSpec.Direction.ASCENDING;
        } else if (collation.getDirection() == RelFieldCollation.Direction.DESCENDING) {
            direction = OrderByColumnSpec.Direction.DESCENDING;
        } else {
            throw new ISE("WTF?! Don't know what to do with direction[%s]", collation.getDirection());
        }
        final SqlTypeName sortExpressionType = sortExpression.getType().getSqlTypeName();
        if (SqlTypeName.NUMERIC_TYPES.contains(sortExpressionType) || SqlTypeName.TIMESTAMP == sortExpressionType || SqlTypeName.DATE == sortExpressionType) {
            comparator = StringComparators.NUMERIC;
        } else {
            comparator = StringComparators.LEXICOGRAPHIC;
        }
        if (sortExpression.isA(SqlKind.INPUT_REF)) {
            final RexInputRef ref = (RexInputRef) sortExpression;
            final String fieldName = rowOrder.get(ref.getIndex());
            orderBys.add(new OrderByColumnSpec(fieldName, direction, comparator));
        } else {
            // We don't support sorting by anything other than refs which actually appear in the query result.
            return null;
        }
    }
    return new DefaultLimitSpec(orderBys, limit);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DefaultLimitSpec(io.druid.query.groupby.orderby.DefaultLimitSpec) StringComparator(io.druid.query.ordering.StringComparator) OrderByColumnSpec(io.druid.query.groupby.orderby.OrderByColumnSpec) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) ISE(io.druid.java.util.common.ISE) RexNode(org.apache.calcite.rex.RexNode)

Example 49 with RexInputRef

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

the class DrillRelOptUtil method findItemOrFlatten.

/**
   * Travesal RexNode to find the item/flattern operator. Continue search if RexNode has a
   * RexInputRef which refers to a RexNode in project expressions.
   *
   * @param node : RexNode to search
   * @param projExprs : the list of project expressions. Empty list means there is No project operator underneath.
   * @return : Return null if there is NONE; return the first appearance of item/flatten RexCall.
   */
public static RexCall findItemOrFlatten(final RexNode node, final List<RexNode> projExprs) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {

            public Void visitCall(RexCall call) {
                if ("item".equals(call.getOperator().getName().toLowerCase()) || "flatten".equals(call.getOperator().getName().toLowerCase())) {
                    throw new Util.FoundOne(call);
                /* throw exception to interrupt tree walk (this is similar to
                                              other utility methods in RexUtil.java */
                }
                return super.visitCall(call);
            }

            public Void visitInputRef(RexInputRef inputRef) {
                if (projExprs.size() == 0) {
                    return super.visitInputRef(inputRef);
                } else {
                    final int index = inputRef.getIndex();
                    RexNode n = projExprs.get(index);
                    if (n instanceof RexCall) {
                        RexCall r = (RexCall) n;
                        if ("item".equals(r.getOperator().getName().toLowerCase()) || "flatten".equals(r.getOperator().getName().toLowerCase())) {
                            throw new Util.FoundOne(r);
                        }
                    }
                    return super.visitInputRef(inputRef);
                }
            }
        };
        node.accept(visitor);
        return null;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return (RexCall) e.getNode();
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptUtil(org.apache.calcite.plan.RelOptUtil) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) Util(org.apache.calcite.util.Util) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl) RexNode(org.apache.calcite.rex.RexNode)

Example 50 with RexInputRef

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

the class ConvertCountToDirectScan method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final DrillAggregateRel agg = (DrillAggregateRel) call.rel(0);
    final DrillScanRel scan = (DrillScanRel) call.rel(call.rels.length - 1);
    final DrillProjectRel proj = call.rels.length == 3 ? (DrillProjectRel) call.rel(1) : null;
    final GroupScan oldGrpScan = scan.getGroupScan();
    final PlannerSettings settings = PrelUtil.getPlannerSettings(call.getPlanner());
    //    4) No distinct agg call.
    if (!(oldGrpScan.getScanStats(settings).getGroupScanProperty().hasExactRowCount() && agg.getGroupCount() == 0 && agg.getAggCallList().size() == 1 && !agg.containsDistinctCall())) {
        return;
    }
    AggregateCall aggCall = agg.getAggCallList().get(0);
    if (aggCall.getAggregation().getName().equals("COUNT")) {
        long cnt = 0;
        //  count(Not-null-input) ==> rowCount
        if (aggCall.getArgList().isEmpty() || (aggCall.getArgList().size() == 1 && !agg.getInput().getRowType().getFieldList().get(aggCall.getArgList().get(0).intValue()).getType().isNullable())) {
            cnt = (long) oldGrpScan.getScanStats(settings).getRecordCount();
        } else if (aggCall.getArgList().size() == 1) {
            // count(columnName) ==> Agg ( Scan )) ==> columnValueCount
            int index = aggCall.getArgList().get(0);
            if (proj != null) {
                if (proj.getProjects().get(index) instanceof RexInputRef) {
                    index = ((RexInputRef) proj.getProjects().get(index)).getIndex();
                } else {
                    // do not apply for all other cases.
                    return;
                }
            }
            String columnName = scan.getRowType().getFieldNames().get(index).toLowerCase();
            cnt = oldGrpScan.getColumnValueCount(SchemaPath.getSimplePath(columnName));
            if (cnt == GroupScan.NO_COLUMN_STATS) {
                // if column stats are not available don't apply this rule
                return;
            }
        } else {
            // do nothing.
            return;
        }
        RelDataType scanRowType = getCountDirectScanRowType(agg.getCluster().getTypeFactory());
        final ScanPrel newScan = ScanPrel.create(scan, scan.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON), getCountDirectScan(cnt), scanRowType);
        List<RexNode> exprs = Lists.newArrayList();
        exprs.add(RexInputRef.of(0, scanRowType));
        final ProjectPrel newProj = new ProjectPrel(agg.getCluster(), agg.getTraitSet().plus(Prel.DRILL_PHYSICAL).plus(DrillDistributionTrait.SINGLETON), newScan, exprs, agg.getRowType());
        call.transformTo(newProj);
    }
}
Also used : DrillScanRel(org.apache.drill.exec.planner.logical.DrillScanRel) DrillProjectRel(org.apache.drill.exec.planner.logical.DrillProjectRel) DrillAggregateRel(org.apache.drill.exec.planner.logical.DrillAggregateRel) RelDataType(org.apache.calcite.rel.type.RelDataType) DirectGroupScan(org.apache.drill.exec.store.direct.DirectGroupScan) GroupScan(org.apache.drill.exec.physical.base.GroupScan) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexInputRef (org.apache.calcite.rex.RexInputRef)51 RexNode (org.apache.calcite.rex.RexNode)43 ArrayList (java.util.ArrayList)22 RelNode (org.apache.calcite.rel.RelNode)21 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)21 RelDataType (org.apache.calcite.rel.type.RelDataType)16 RexCall (org.apache.calcite.rex.RexCall)14 AggregateCall (org.apache.calcite.rel.core.AggregateCall)11 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)11 ImmutableList (com.google.common.collect.ImmutableList)10 HashMap (java.util.HashMap)10 RexBuilder (org.apache.calcite.rex.RexBuilder)10 Pair (org.apache.calcite.util.Pair)8 HashSet (java.util.HashSet)7 BigDecimal (java.math.BigDecimal)6 RexLiteral (org.apache.calcite.rex.RexLiteral)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 List (java.util.List)5 Builder (com.google.common.collect.ImmutableList.Builder)4 ISE (io.druid.java.util.common.ISE)4