Search in sources :

Example 66 with RelDataTypeField

use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.

the class StarColumnConverter method visitScan.

@Override
public Prel visitScan(ScanPrel scanPrel, Void value) throws RuntimeException {
    if (StarColumnHelper.containsStarColumn(scanPrel.getRowType()) && prefixedForStar) {
        List<RexNode> exprs = Lists.newArrayList();
        for (RelDataTypeField field : scanPrel.getRowType().getFieldList()) {
            RexNode expr = scanPrel.getCluster().getRexBuilder().makeInputRef(field.getType(), field.getIndex());
            exprs.add(expr);
        }
        List<String> fieldNames = Lists.newArrayList();
        long tableId = tableNumber.getAndIncrement();
        for (String name : scanPrel.getRowType().getFieldNames()) {
            if (StarColumnHelper.isNonPrefixedStarColumn(name)) {
                // Add prefix to * column.
                fieldNames.add("T" + tableId + StarColumnHelper.PREFIX_DELIMITER + name);
            } else {
                // Keep regular column as it is.
                fieldNames.add(name);
            }
        }
        RelDataType rowType = RexUtil.createStructType(scanPrel.getCluster().getTypeFactory(), exprs, fieldNames);
        // insert a PAS.
        ProjectPrel proj = new ProjectPrel(scanPrel.getCluster(), scanPrel.getTraitSet(), scanPrel, exprs, rowType);
        return proj;
    } else {
        return visitPrel(scanPrel, value);
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 67 with RelDataTypeField

use of org.apache.calcite.rel.type.RelDataTypeField in project drill by apache.

the class RewriteProjectToFlatten method visitProject.

@Override
public Prel visitProject(ProjectPrel node, Object unused) throws RelConversionException {
    ProjectPrel project = node;
    List<RexNode> exprList = new ArrayList<>();
    boolean rewrite = false;
    List<RelDataTypeField> relDataTypes = new ArrayList<>();
    int i = 0;
    RexNode flatttenExpr = null;
    for (RexNode rex : project.getChildExps()) {
        RexNode newExpr = rex;
        if (rex instanceof RexCall) {
            RexCall function = (RexCall) rex;
            String functionName = function.getOperator().getName();
            if (functionName.equalsIgnoreCase("flatten")) {
                rewrite = true;
                if (function.getOperands().size() != 1) {
                    throw new RelConversionException("Flatten expression expects a single input.");
                }
                newExpr = function.getOperands().get(0);
                RexBuilder builder = new RexBuilder(factory);
                flatttenExpr = builder.makeInputRef(new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory), i);
            }
        }
        relDataTypes.add(project.getRowType().getFieldList().get(i));
        i++;
        exprList.add(newExpr);
    }
    if (rewrite == true) {
        // TODO - figure out what is the right setting for the traits
        Prel newChild = ((Prel) project.getInput(0)).accept(this, null);
        ProjectPrel newProject = new ProjectPrel(node.getCluster(), project.getTraitSet(), newChild, exprList, new RelRecordType(relDataTypes));
        FlattenPrel flatten = new FlattenPrel(project.getCluster(), project.getTraitSet(), newProject, flatttenExpr);
        return flatten;
    }
    Prel child = ((Prel) project.getInput()).accept(this, null);
    return (Prel) project.copy(project.getTraitSet(), child, exprList, new RelRecordType(relDataTypes));
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) ArrayList(java.util.ArrayList) RelDataTypeDrillImpl(org.apache.drill.exec.planner.types.RelDataTypeDrillImpl) RelRecordType(org.apache.calcite.rel.type.RelRecordType) RelConversionException(org.apache.calcite.tools.RelConversionException) Prel(org.apache.drill.exec.planner.physical.Prel) ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexCall(org.apache.calcite.rex.RexCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) FlattenPrel(org.apache.drill.exec.planner.physical.FlattenPrel) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RexNode(org.apache.calcite.rex.RexNode)

Example 68 with RelDataTypeField

use of org.apache.calcite.rel.type.RelDataTypeField in project lucene-solr by apache.

the class SolrSort method implement.

public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    List<RelFieldCollation> sortCollations = collation.getFieldCollations();
    if (!sortCollations.isEmpty()) {
        // Construct a series of order clauses from the desired collation
        final List<RelDataTypeField> fields = getRowType().getFieldList();
        for (RelFieldCollation fieldCollation : sortCollations) {
            final String name = fields.get(fieldCollation.getFieldIndex()).getName();
            String direction = "asc";
            if (fieldCollation.getDirection().equals(RelFieldCollation.Direction.DESCENDING)) {
                direction = "desc";
            }
            implementor.addOrder(name, direction);
        }
    }
    if (fetch != null) {
        implementor.setLimit(((RexLiteral) fetch).getValue().toString());
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Aggregations

RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)68 RexNode (org.apache.calcite.rex.RexNode)41 ArrayList (java.util.ArrayList)33 RelNode (org.apache.calcite.rel.RelNode)30 RelDataType (org.apache.calcite.rel.type.RelDataType)25 RexInputRef (org.apache.calcite.rex.RexInputRef)21 RexBuilder (org.apache.calcite.rex.RexBuilder)20 Pair (org.apache.calcite.util.Pair)17 HashMap (java.util.HashMap)13 AggregateCall (org.apache.calcite.rel.core.AggregateCall)12 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)12 Map (java.util.Map)8 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)6 NavigableMap (java.util.NavigableMap)6 SortedMap (java.util.SortedMap)6 TreeMap (java.util.TreeMap)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 JoinRelType (org.apache.calcite.rel.core.JoinRelType)5