Search in sources :

Example 6 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project storm by apache.

the class RelNodeCompiler method visitFilter.

@Override
public Void visitFilter(Filter filter, List<Void> inputStreams) throws Exception {
    beginStage(filter);
    List<RexNode> childExps = filter.getChildExps();
    RelDataType inputRowType = filter.getInput(0).getRowType();
    pw.print("Context context = new StormContext(Processor.dataContext);\n");
    pw.print("context.values = _data.toArray();\n");
    pw.print("Object[] outputValues = new Object[1];\n");
    pw.write(rexCompiler.compileToBlock(childExps, inputRowType).toString());
    String r = "((Boolean) outputValues[0])";
    if (filter.getCondition().getType().isNullable()) {
        pw.print(String.format("    if (%s != null && %s) { ctx.emit(_data); }\n", r, r));
    } else {
        pw.print(String.format("    if (%s) { ctx.emit(_data); }\n", r, r));
    }
    endStage();
    return null;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 7 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project storm by apache.

the class RelNodeCompiler method visitProject.

@Override
public Void visitProject(Project project, List<Void> inputStreams) throws Exception {
    beginStage(project);
    List<RexNode> childExps = project.getChildExps();
    RelDataType inputRowType = project.getInput(0).getRowType();
    int outputCount = project.getRowType().getFieldCount();
    pw.print("Context context = new StormContext(Processor.dataContext);\n");
    pw.print("context.values = _data.toArray();\n");
    pw.print(String.format("Object[] outputValues = new Object[%d];\n", outputCount));
    pw.write(rexCompiler.compileToBlock(childExps, inputRowType).toString());
    pw.print("    ctx.emit(new Values(outputValues));\n");
    endStage();
    return null;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveCalciteUtil method createSingleArgAggCall.

public static AggregateCall createSingleArgAggCall(String funcName, RelOptCluster cluster, PrimitiveTypeInfo typeInfo, Integer pos, RelDataType aggFnRetType) {
    ImmutableList.Builder<RelDataType> aggArgRelDTBldr = new ImmutableList.Builder<RelDataType>();
    aggArgRelDTBldr.add(TypeConverter.convert(typeInfo, cluster.getTypeFactory()));
    SqlAggFunction aggFunction = SqlFunctionConverter.getCalciteAggFn(funcName, false, aggArgRelDTBldr.build(), aggFnRetType);
    List<Integer> argList = new ArrayList<Integer>();
    argList.add(pos);
    return new AggregateCall(aggFunction, false, argList, aggFnRetType, null);
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) ImmutableList(com.google.common.collect.ImmutableList) Builder(com.google.common.collect.ImmutableMap.Builder) RexBuilder(org.apache.calcite.rex.RexBuilder) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 9 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveSubQRemoveRelBuilder method values.

/** Creates a {@link Values}.
   *
   * <p>The {@code values} array must have the same number of entries as
   * {@code fieldNames}, or an integer multiple if you wish to create multiple
   * rows.
   *
   * <p>If there are zero rows, or if all values of a any column are
   * null, this method cannot deduce the type of columns. For these cases,
   * call {@link #values(Iterable, RelDataType)}.
   *
   * @param fieldNames Field names
   * @param values Values
   */
public HiveSubQRemoveRelBuilder values(String[] fieldNames, Object... values) {
    if (fieldNames == null || fieldNames.length == 0 || values.length % fieldNames.length != 0 || values.length < fieldNames.length) {
        throw new IllegalArgumentException("Value count must be a positive multiple of field count");
    }
    final int rowCount = values.length / fieldNames.length;
    for (Ord<String> fieldName : Ord.zip(fieldNames)) {
        if (allNull(values, fieldName.i, fieldNames.length)) {
            throw new IllegalArgumentException("All values of field '" + fieldName.e + "' are null; cannot deduce type");
        }
    }
    final ImmutableList<ImmutableList<RexLiteral>> tupleList = tupleList(fieldNames.length, values);
    final RelDataTypeFactory.FieldInfoBuilder rowTypeBuilder = cluster.getTypeFactory().builder();
    for (final Ord<String> fieldName : Ord.zip(fieldNames)) {
        final String name = fieldName.e != null ? fieldName.e : "expr$" + fieldName.i;
        final RelDataType type = cluster.getTypeFactory().leastRestrictive(new AbstractList<RelDataType>() {

            public RelDataType get(int index) {
                return tupleList.get(index).get(fieldName.i).getType();
            }

            public int size() {
                return rowCount;
            }
        });
        rowTypeBuilder.add(name, type);
    }
    final RelDataType rowType = rowTypeBuilder.build();
    return values(tupleList, rowType);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString)

Example 10 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveSubQRemoveRelBuilder method field.

/** As {@link #field(int, int, int)}, but if {@code alias} is true, the method
   * may apply an alias to make sure that the field has the same name as in the
   * input frame. If no alias is applied the expression is definitely a
   * {@link RexInputRef}. */
private RexNode field(int inputCount, int inputOrdinal, int fieldOrdinal, boolean alias) {
    final Frame frame = peek_(inputCount, inputOrdinal);
    final RelNode input = frame.rel;
    final RelDataType rowType = input.getRowType();
    if (fieldOrdinal < 0 || fieldOrdinal > rowType.getFieldCount()) {
        throw new IllegalArgumentException("field ordinal [" + fieldOrdinal + "] out of range; input fields are: " + rowType.getFieldNames());
    }
    final RelDataTypeField field = rowType.getFieldList().get(fieldOrdinal);
    final int offset = inputOffset(inputCount, inputOrdinal);
    final RexInputRef ref = cluster.getRexBuilder().makeInputRef(field.getType(), offset + fieldOrdinal);
    final RelDataTypeField aliasField = frame.fields().get(fieldOrdinal);
    if (!alias || field.getName().equals(aliasField.getName())) {
        return ref;
    } else {
        return alias(ref, aliasField.getName());
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)88 RexNode (org.apache.calcite.rex.RexNode)48 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelNode (org.apache.calcite.rel.RelNode)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)25 ArrayList (java.util.ArrayList)21 RexInputRef (org.apache.calcite.rex.RexInputRef)16 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)14 AggregateCall (org.apache.calcite.rel.core.AggregateCall)13 ImmutableList (com.google.common.collect.ImmutableList)9 BigDecimal (java.math.BigDecimal)8 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)7 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 Prel (org.apache.drill.exec.planner.physical.Prel)6 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)6 Builder (com.google.common.collect.ImmutableList.Builder)5 LinkedList (java.util.LinkedList)5