Search in sources :

Example 61 with LogicalExpression

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

the class FunctionGenerationHelper method getFunctionExpression.

private static LogicalExpression getFunctionExpression(String name, MajorType returnType, FunctionImplementationRegistry registry, HoldingContainer... args) {
    List<MajorType> argTypes = new ArrayList<MajorType>(args.length);
    List<LogicalExpression> argExpressions = new ArrayList<LogicalExpression>(args.length);
    for (HoldingContainer c : args) {
        argTypes.add(c.getMajorType());
        argExpressions.add(new HoldingContainerExpression(c));
    }
    return new FunctionCall(name, argExpressions, ExpressionPosition.UNKNOWN);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) ArrayList(java.util.ArrayList) FunctionCall(org.apache.drill.common.expression.FunctionCall) HoldingContainerExpression(org.apache.drill.exec.expr.HoldingContainerExpression)

Example 62 with LogicalExpression

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

the class FunctionGenerationHelper method getTypeComparisonFunction.

/**
   * Wraps the comparison function in an If-statement which compares the types first, evaluating the comaprison function only
   * if the types are equivialent
   *
   * @param comparisonFunction
   * @param args
   * @return
   */
private static LogicalExpression getTypeComparisonFunction(LogicalExpression comparisonFunction, HoldingContainer... args) {
    List<LogicalExpression> argExpressions = Lists.newArrayList();
    List<MajorType> argTypes = Lists.newArrayList();
    for (HoldingContainer c : args) {
        argTypes.add(c.getMajorType());
        argExpressions.add(new HoldingContainerExpression(c));
    }
    FunctionCall call = new FunctionCall("compareType", argExpressions, ExpressionPosition.UNKNOWN);
    List<LogicalExpression> newArgs = Lists.newArrayList();
    newArgs.add(call);
    newArgs.add(new IntExpression(0, ExpressionPosition.UNKNOWN));
    FunctionCall notEqual = new FunctionCall("not_equal", newArgs, ExpressionPosition.UNKNOWN);
    IfExpression.IfCondition ifCondition = new IfCondition(notEqual, call);
    IfExpression ifExpression = IfExpression.newBuilder().setIfCondition(ifCondition).setElse(comparisonFunction).build();
    return ifExpression;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) IfExpression(org.apache.drill.common.expression.IfExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) IntExpression(org.apache.drill.common.expression.ValueExpressions.IntExpression) FunctionCall(org.apache.drill.common.expression.FunctionCall) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition) HoldingContainerExpression(org.apache.drill.exec.expr.HoldingContainerExpression) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Example 63 with LogicalExpression

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

the class CloneVisitor method visitIfExpression.

@Override
public LogicalExpression visitIfExpression(IfExpression ifExpr, Void value) throws RuntimeException {
    LogicalExpression ifCondition = ifExpr.ifCondition.condition.accept(this, null);
    LogicalExpression ifExpression = ifExpr.ifCondition.expression.accept(this, null);
    LogicalExpression elseExpression = ifExpr.elseExpression.accept(this, null);
    IfExpression.IfCondition condition = new IfCondition(ifCondition, ifExpression);
    return IfExpression.newBuilder().setIfCondition(condition).setElse(elseExpression).build();
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) IfExpression(org.apache.drill.common.expression.IfExpression) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Example 64 with LogicalExpression

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

the class ExpressionTreeMaterializer method convertToNullableType.

public static LogicalExpression convertToNullableType(LogicalExpression fromExpr, MinorType toType, FunctionLookupContext functionLookupContext, ErrorCollector errorCollector) {
    String funcName = "convertToNullable" + toType.toString();
    List<LogicalExpression> args = Lists.newArrayList();
    args.add(fromExpr);
    FunctionCall funcCall = new FunctionCall(funcName, args, ExpressionPosition.UNKNOWN);
    FunctionResolver resolver = FunctionResolverFactory.getResolver(funcCall);
    DrillFuncHolder matchedConvertToNullableFuncHolder = functionLookupContext.findDrillFunction(resolver, funcCall);
    if (matchedConvertToNullableFuncHolder == null) {
        logFunctionResolutionError(errorCollector, funcCall);
        return NullExpression.INSTANCE;
    }
    return matchedConvertToNullableFuncHolder.getExpr(funcName, args, ExpressionPosition.UNKNOWN);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) QuotedString(org.apache.drill.common.expression.ValueExpressions.QuotedString) FunctionCall(org.apache.drill.common.expression.FunctionCall) FunctionResolver(org.apache.drill.exec.resolver.FunctionResolver)

Example 65 with LogicalExpression

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

the class CompareFunctionsProcessor method visitConvertExpression.

@Override
public Boolean visitConvertExpression(ConvertExpression e, LogicalExpression valueArg) throws RuntimeException {
    if (e.getConvertFunction() == ConvertExpression.CONVERT_FROM) {
        String encodingType = e.getEncodingType();
        int prefixLength = 0;
        // CONVERT_FROM(BYTE_SUBSTR(row_key, 1, 8), 'DATE_EPOCH_BE') < DATE '2015-06-17'
        if (e.getInput() instanceof FunctionCall) {
            // We can prune scan range only for big-endian encoded data
            if (encodingType.endsWith("_BE") == false) {
                return false;
            }
            FunctionCall call = (FunctionCall) e.getInput();
            String functionName = call.getName();
            if (!functionName.equalsIgnoreCase("byte_substr")) {
                return false;
            }
            LogicalExpression nameArg = call.args.get(0);
            LogicalExpression valueArg1 = call.args.size() >= 2 ? call.args.get(1) : null;
            LogicalExpression valueArg2 = call.args.size() >= 3 ? call.args.get(2) : null;
            if (((nameArg instanceof SchemaPath) == false) || (valueArg1 == null) || ((valueArg1 instanceof IntExpression) == false) || (valueArg2 == null) || ((valueArg2 instanceof IntExpression) == false)) {
                return false;
            }
            boolean isRowKey = ((SchemaPath) nameArg).getAsUnescapedPath().equals(DrillHBaseConstants.ROW_KEY);
            int offset = ((IntExpression) valueArg1).getInt();
            if (!isRowKey || (offset != 1)) {
                return false;
            }
            this.path = (SchemaPath) nameArg;
            prefixLength = ((IntExpression) valueArg2).getInt();
            this.isRowKeyPrefixComparison = true;
            return visitRowKeyPrefixConvertExpression(e, prefixLength, valueArg);
        }
        if (e.getInput() instanceof SchemaPath) {
            ByteBuf bb = null;
            switch(encodingType) {
                case "INT_BE":
                case "INT":
                case "UINT_BE":
                case "UINT":
                case "UINT4_BE":
                case "UINT4":
                    if (valueArg instanceof IntExpression && (isEqualityFn || encodingType.startsWith("U"))) {
                        bb = newByteBuf(4, encodingType.endsWith("_BE"));
                        bb.writeInt(((IntExpression) valueArg).getInt());
                    }
                    break;
                case "BIGINT_BE":
                case "BIGINT":
                case "UINT8_BE":
                case "UINT8":
                    if (valueArg instanceof LongExpression && (isEqualityFn || encodingType.startsWith("U"))) {
                        bb = newByteBuf(8, encodingType.endsWith("_BE"));
                        bb.writeLong(((LongExpression) valueArg).getLong());
                    }
                    break;
                case "FLOAT":
                    if (valueArg instanceof FloatExpression && isEqualityFn) {
                        bb = newByteBuf(4, true);
                        bb.writeFloat(((FloatExpression) valueArg).getFloat());
                    }
                    break;
                case "DOUBLE":
                    if (valueArg instanceof DoubleExpression && isEqualityFn) {
                        bb = newByteBuf(8, true);
                        bb.writeDouble(((DoubleExpression) valueArg).getDouble());
                    }
                    break;
                case "TIME_EPOCH":
                case "TIME_EPOCH_BE":
                    if (valueArg instanceof TimeExpression) {
                        bb = newByteBuf(8, encodingType.endsWith("_BE"));
                        bb.writeLong(((TimeExpression) valueArg).getTime());
                    }
                    break;
                case "DATE_EPOCH":
                case "DATE_EPOCH_BE":
                    if (valueArg instanceof DateExpression) {
                        bb = newByteBuf(8, encodingType.endsWith("_BE"));
                        bb.writeLong(((DateExpression) valueArg).getDate());
                    }
                    break;
                case "BOOLEAN_BYTE":
                    if (valueArg instanceof BooleanExpression) {
                        bb = newByteBuf(1, false);
                        bb.writeByte(((BooleanExpression) valueArg).getBoolean() ? 1 : 0);
                    }
                    break;
                case "DOUBLE_OB":
                case "DOUBLE_OBD":
                    if (valueArg instanceof DoubleExpression) {
                        bb = newByteBuf(9, true);
                        PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 9);
                        if (encodingType.endsWith("_OBD")) {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat64(br, ((DoubleExpression) valueArg).getDouble(), Order.DESCENDING);
                            this.sortOrderAscending = false;
                        } else {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat64(br, ((DoubleExpression) valueArg).getDouble(), Order.ASCENDING);
                        }
                    }
                    break;
                case "FLOAT_OB":
                case "FLOAT_OBD":
                    if (valueArg instanceof FloatExpression) {
                        bb = newByteBuf(5, true);
                        PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 5);
                        if (encodingType.endsWith("_OBD")) {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat32(br, ((FloatExpression) valueArg).getFloat(), Order.DESCENDING);
                            this.sortOrderAscending = false;
                        } else {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat32(br, ((FloatExpression) valueArg).getFloat(), Order.ASCENDING);
                        }
                    }
                    break;
                case "BIGINT_OB":
                case "BIGINT_OBD":
                    if (valueArg instanceof LongExpression) {
                        bb = newByteBuf(9, true);
                        PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 9);
                        if (encodingType.endsWith("_OBD")) {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br, ((LongExpression) valueArg).getLong(), Order.DESCENDING);
                            this.sortOrderAscending = false;
                        } else {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br, ((LongExpression) valueArg).getLong(), Order.ASCENDING);
                        }
                    }
                    break;
                case "INT_OB":
                case "INT_OBD":
                    if (valueArg instanceof IntExpression) {
                        bb = newByteBuf(5, true);
                        PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 5);
                        if (encodingType.endsWith("_OBD")) {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeInt32(br, ((IntExpression) valueArg).getInt(), Order.DESCENDING);
                            this.sortOrderAscending = false;
                        } else {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeInt32(br, ((IntExpression) valueArg).getInt(), Order.ASCENDING);
                        }
                    }
                    break;
                case "UTF8_OB":
                case "UTF8_OBD":
                    if (valueArg instanceof QuotedString) {
                        int stringLen = ((QuotedString) valueArg).value.getBytes(Charsets.UTF_8).length;
                        bb = newByteBuf(stringLen + 2, true);
                        PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, stringLen + 2);
                        if (encodingType.endsWith("_OBD")) {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeString(br, ((QuotedString) valueArg).value, Order.DESCENDING);
                            this.sortOrderAscending = false;
                        } else {
                            org.apache.hadoop.hbase.util.OrderedBytes.encodeString(br, ((QuotedString) valueArg).value, Order.ASCENDING);
                        }
                    }
                    break;
                case "UTF8":
                    // let visitSchemaPath() handle this.
                    return e.getInput().accept(this, valueArg);
            }
            if (bb != null) {
                this.value = bb.array();
                this.path = (SchemaPath) e.getInput();
                return true;
            }
        }
    }
    return false;
}
Also used : IntExpression(org.apache.drill.common.expression.ValueExpressions.IntExpression) DoubleExpression(org.apache.drill.common.expression.ValueExpressions.DoubleExpression) FloatExpression(org.apache.drill.common.expression.ValueExpressions.FloatExpression) TimeExpression(org.apache.drill.common.expression.ValueExpressions.TimeExpression) QuotedString(org.apache.drill.common.expression.ValueExpressions.QuotedString) PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange) ByteBuf(io.netty.buffer.ByteBuf) QuotedString(org.apache.drill.common.expression.ValueExpressions.QuotedString) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) BooleanExpression(org.apache.drill.common.expression.ValueExpressions.BooleanExpression) DateExpression(org.apache.drill.common.expression.ValueExpressions.DateExpression) SchemaPath(org.apache.drill.common.expression.SchemaPath) SimplePositionedMutableByteRange(org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange) LongExpression(org.apache.drill.common.expression.ValueExpressions.LongExpression) 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