Search in sources :

Example 51 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.

the class StringCastReturnTypeInference method getType.

/**
   * Defines function return type and sets cast length as type precision
   * if cast length is simple long expression.
   *
   * @param logicalExpressions logical expressions
   * @param attributes function attributes
   * @return return type
   */
@Override
public TypeProtos.MajorType getType(List<LogicalExpression> logicalExpressions, FunctionAttributes attributes) {
    TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder().setMinorType(attributes.getReturnValue().getType().getMinorType()).setMode(FunctionUtils.getReturnTypeDataMode(logicalExpressions, attributes));
    LogicalExpression logicalExpression = logicalExpressions.get(1);
    if (logicalExpressions.get(1) instanceof ValueExpressions.LongExpression) {
        long precision = ((ValueExpressions.LongExpression) logicalExpression).getLong();
        builder.setPrecision(Ints.checkedCast(precision));
    }
    return builder.build();
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression)

Example 52 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.

the class FunctionGenerationHelper method getOrderingComparator.

/**
   * Finds ordering comparator ("compare_to...") FunctionHolderExpression with
   * a specified ordering for NULL (and considering NULLS <i>equal</i>).
   * @param  null_high  whether NULL should compare as the lowest value (if
   *                    {@code false}) or the highest value (if {@code true})
   * @param  left  ...
   * @param  right  ...
   * @param  registry  ...
   * @return
   *     FunctionHolderExpression containing the found function implementation
   */
public static LogicalExpression getOrderingComparator(boolean null_high, HoldingContainer left, HoldingContainer right, FunctionImplementationRegistry registry) {
    final String comparator_name = null_high ? COMPARE_TO_NULLS_HIGH : COMPARE_TO_NULLS_LOW;
    if (!isComparableType(left.getMajorType()) || !isComparableType(right.getMajorType())) {
        throw new UnsupportedOperationException(formatCanNotCompareMsg(left.getMajorType(), right.getMajorType()));
    }
    LogicalExpression comparisonFunctionExpression = getFunctionExpression(comparator_name, Types.required(MinorType.INT), registry, left, right);
    ErrorCollector collector = new ErrorCollectorImpl();
    if (!isUnionType(left.getMajorType()) && !isUnionType(right.getMajorType())) {
        return ExpressionTreeMaterializer.materialize(comparisonFunctionExpression, null, collector, registry);
    } else {
        LogicalExpression typeComparisonFunctionExpression = getTypeComparisonFunction(comparisonFunctionExpression, left, right);
        return ExpressionTreeMaterializer.materialize(typeComparisonFunctionExpression, null, collector, registry);
    }
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ErrorCollector(org.apache.drill.common.expression.ErrorCollector)

Example 53 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.

the class DrillWindowRel method toDrill.

protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
    List<LogicalExpression> args = Lists.newArrayList();
    for (Integer i : call.getArgList()) {
        args.add(new FieldReference(fn.get(i)));
    }
    // for count(1).
    if (args.isEmpty()) {
        args.add(new ValueExpressions.LongExpression(1l));
    }
    LogicalExpression expr = new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
    return expr;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) FunctionCall(org.apache.drill.common.expression.FunctionCall)

Example 54 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.

the class AggPrelBase method createKeysAndExprs.

protected void createKeysAndExprs() {
    final List<String> childFields = getInput().getRowType().getFieldNames();
    final List<String> fields = getRowType().getFieldNames();
    for (int group : BitSets.toIter(groupSet)) {
        FieldReference fr = FieldReference.getWithQuotedRef(childFields.get(group));
        keys.add(new NamedExpression(fr, fr));
    }
    for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
        int aggExprOrdinal = groupSet.cardinality() + aggCall.i;
        FieldReference ref = FieldReference.getWithQuotedRef(fields.get(aggExprOrdinal));
        LogicalExpression expr = toDrill(aggCall.e, childFields);
        NamedExpression ne = new NamedExpression(expr, ref);
        aggExprs.add(ne);
        if (getOperatorPhase() == OperatorPhase.PHASE_1of2) {
            if (aggCall.e.getAggregation().getName().equals("COUNT")) {
                // If we are doing a COUNT aggregate in Phase1of2, then in Phase2of2 we should SUM the COUNTs,
                SqlAggFunction sumAggFun = new SqlSumCountAggFunction(aggCall.e.getType());
                AggregateCall newAggCall = new AggregateCall(sumAggFun, aggCall.e.isDistinct(), Collections.singletonList(aggExprOrdinal), aggCall.e.getType(), aggCall.e.getName());
                phase2AggCallList.add(newAggCall);
            } else {
                AggregateCall newAggCall = new AggregateCall(aggCall.e.getAggregation(), aggCall.e.isDistinct(), Collections.singletonList(aggExprOrdinal), aggCall.e.getType(), aggCall.e.getName());
                phase2AggCallList.add(newAggCall);
            }
        }
    }
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 55 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.

the class AggPrelBase method toDrill.

protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
    List<LogicalExpression> args = Lists.newArrayList();
    for (Integer i : call.getArgList()) {
        args.add(FieldReference.getWithQuotedRef(fn.get(i)));
    }
    // for count(1).
    if (args.isEmpty()) {
        args.add(new ValueExpressions.LongExpression(1l));
    }
    LogicalExpression expr = new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
    return expr;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) FunctionCall(org.apache.drill.common.expression.FunctionCall)

Aggregations

LogicalExpression (org.apache.drill.common.expression.LogicalExpression)90 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)32 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)31 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)18 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)17 FieldReference (org.apache.drill.common.expression.FieldReference)14 FunctionCall (org.apache.drill.common.expression.FunctionCall)14 SchemaPath (org.apache.drill.common.expression.SchemaPath)12 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)11 MaterializedField (org.apache.drill.exec.record.MaterializedField)11 JConditional (com.sun.codemodel.JConditional)10 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)10 ValueVector (org.apache.drill.exec.vector.ValueVector)10 IOException (java.io.IOException)9 Ordering (org.apache.drill.common.logical.data.Order.Ordering)9 DrillParseContext (org.apache.drill.exec.planner.logical.DrillParseContext)9 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)8 QuotedString (org.apache.drill.common.expression.ValueExpressions.QuotedString)8 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)8 ArrayList (java.util.ArrayList)7