Search in sources :

Example 31 with ConstantExpression

use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.

the class LogicalRowExpressions method flipOperatorFunctionWithOneVarOneConstant.

public RowExpression flipOperatorFunctionWithOneVarOneConstant(RowExpression expressions) {
    if (expressions instanceof CallExpression) {
        CallExpression call = (CallExpression) expressions;
        FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(call.getFunctionHandle());
        if (functionMetadata.getOperatorType().isPresent() && functionMetadata.getOperatorType().get().isComparisonOperator()) {
            if (call.getArguments().get(1) instanceof VariableReferenceExpression && call.getArguments().get(0) instanceof ConstantExpression) {
                OperatorType operator;
                switch(functionMetadata.getOperatorType().get()) {
                    case LESS_THAN:
                        operator = GREATER_THAN;
                        break;
                    case LESS_THAN_OR_EQUAL:
                        operator = GREATER_THAN_OR_EQUAL;
                        break;
                    case GREATER_THAN:
                        operator = LESS_THAN;
                        break;
                    case GREATER_THAN_OR_EQUAL:
                        operator = LESS_THAN_OR_EQUAL;
                        break;
                    case IS_DISTINCT_FROM:
                        operator = IS_DISTINCT_FROM;
                        break;
                    case EQUAL:
                    case NOT_EQUAL:
                        operator = functionMetadata.getOperatorType().get();
                        break;
                    default:
                        throw new UnsupportedOperationException(format("Unsupported or is not comparison operator: %s", functionMetadata.getOperatorType().get()));
                }
                List<RowExpression> arguments = new ArrayList<>();
                arguments.add(call.getArguments().get(1));
                arguments.add(call.getArguments().get(0));
                return new CallExpression(operator.name(), functionResolution.comparisonFunction(operator, call.getArguments().get(1).getType(), call.getArguments().get(0).getType()), call.getType(), arguments, Optional.empty());
            }
        }
    }
    return expressions;
}
Also used : FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) CallExpression(io.prestosql.spi.relation.CallExpression) OperatorType(io.prestosql.spi.function.OperatorType)

Example 32 with ConstantExpression

use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.

the class TypeUtils method extractValueFromRowExpression.

public static Object extractValueFromRowExpression(RowExpression rowExpression) {
    if (rowExpression instanceof CallExpression) {
        CallExpression callExpression = (CallExpression) rowExpression;
        String name = ((BuiltInFunctionHandle) callExpression.getFunctionHandle()).getSignature().getNameSuffix();
        if (name.equals(CAST_OPERATOR)) {
            Object valueExtracted = extractValueFromRowExpression(callExpression.getArguments().get(0));
            String valueTypeName = valueExtracted.getClass().getTypeName().toLowerCase(Locale.ROOT);
            String expectedTypeName = rowExpression.getType().getDisplayName().toLowerCase(Locale.ROOT);
            if (!valueTypeName.contains(expectedTypeName) && expectedTypeName.equals("real")) {
                // A cast is necessary for Real type, so use Float casting
                return Float.parseFloat(valueExtracted.toString());
            } else if (!valueTypeName.contains(expectedTypeName) && expectedTypeName.equals("date")) {
                // A cast is necessary for Date type, so use Long casting
                LocalDate dateGiven = LocalDate.parse(valueExtracted.toString());
                return dateGiven.toEpochDay();
            }
            return valueExtracted;
        }
    } else if (rowExpression instanceof ConstantExpression) {
        ConstantExpression constant = (ConstantExpression) rowExpression;
        return getActualValue(constant.getType(), constant.getValue());
    }
    throw new UnsupportedOperationException("Not Implemented Exception: " + rowExpression.toString());
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CallExpression(io.prestosql.spi.relation.CallExpression) LocalDate(java.time.LocalDate)

Example 33 with ConstantExpression

use of io.prestosql.spi.relation.ConstantExpression in project boostkit-bigdata by kunpengcompute.

the class OmniRowExpressionUtil method expressionStringify.

/**
 * Stringify a RowExpression
 *
 * @param rowExpression RowExpression from openLooKeng
 * @return String representation of the RowExpression
 */
public static String expressionStringify(RowExpression rowExpression) {
    if (rowExpression instanceof CallExpression) {
        CallExpression callExpression = (CallExpression) rowExpression;
        List<String> args = callExpression.getArguments().stream().map(OmniRowExpressionUtil::expressionStringify).collect(Collectors.toList());
        return callExpression.getDisplayName() + ":" + typeToDataTypeId(callExpression.getType()) + "(" + Joiner.on(", ").join(args) + ")";
    }
    if (rowExpression instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) rowExpression;
        List<String> args = specialForm.getArguments().stream().map(OmniRowExpressionUtil::expressionStringify).collect(Collectors.toList());
        return specialForm.getForm().name() + ":" + toDataType(specialForm.getType()).getId().ordinal() + "(" + Joiner.on(", ").join(args) + ")";
    }
    if (rowExpression instanceof LambdaDefinitionExpression) {
        LambdaDefinitionExpression lambdaDefinitionExpression = (LambdaDefinitionExpression) rowExpression;
        return "(" + Joiner.on(", ").join(lambdaDefinitionExpression.getArguments()) + ") -> " + lambdaDefinitionExpression.getBody();
    }
    if (rowExpression instanceof InputReferenceExpression || rowExpression instanceof VariableReferenceExpression) {
        return rowExpression.toString();
    }
    if (rowExpression instanceof ConstantExpression) {
        ConstantExpression constantExpression = (ConstantExpression) rowExpression;
        Type type = rowExpression.getType();
        if (type instanceof UnknownType && constantExpression.getValue() == null) {
            return "NULL:0";
        }
        if ((type instanceof VarcharType || type instanceof CharType) && constantExpression.getValue() instanceof Slice) {
            String varcharValue = ((Slice) constantExpression.getValue()).toStringAscii();
            return "'" + varcharValue + "':" + typeToDataTypeId(type);
        }
        if (type instanceof DecimalType && !((DecimalType) type).isShort() && constantExpression.getValue() instanceof Slice) {
            return Decimals.decodeUnscaledValue((Slice) constantExpression.getValue()) + ":" + typeToDataTypeId(type);
        }
        return constantExpression.getValue() + ":" + typeToDataTypeId(type);
    }
    return rowExpression.toString();
}
Also used : InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) VarcharType(io.prestosql.spi.type.VarcharType) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) UnknownType(io.prestosql.spi.type.UnknownType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) OperatorUtils.toDataType(nova.hetu.olk.tool.OperatorUtils.toDataType) Type(io.prestosql.spi.type.Type) VarcharType(io.prestosql.spi.type.VarcharType) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 34 with ConstantExpression

use of io.prestosql.spi.relation.ConstantExpression in project boostkit-bigdata by kunpengcompute.

the class OmniRowExpressionUtil method generateLikeExpr.

/**
 * Converts SQL like query syntax into regex syntax and recreate return new
 * appropriate filter
 *
 * @param rawString String from OmniLocalExecutionPlanner
 * @param translatedExpr Optional<RowExpression> from
 * OmniLocalExecutionPlanner
 * @return new Optional<RowExpression> with new regex syntax argument
 */
public static Optional<RowExpression> generateLikeExpr(String rawString, Optional<RowExpression> translatedExpr) {
    List<RowExpression> newArgs = new LinkedList<RowExpression>();
    StringBuilder regexString = new StringBuilder(rawString.length() * 2 + 2);
    regexString.append('^');
    for (char curChar : rawString.toCharArray()) {
        switch(curChar) {
            case '%':
                regexString.append(".*");
                break;
            case '_':
                regexString.append(".");
                break;
            case '\\':
            case '^':
            case '$':
            case '.':
            case '*':
                regexString.append("\\");
                regexString.append(curChar);
                break;
            default:
                regexString.append(curChar);
                break;
        }
    }
    regexString.append('$');
    ConstantExpression regexStringSlice = new ConstantExpression(Slices.utf8Slice(regexString.toString()), VARCHAR);
    newArgs.add(((CallExpression) translatedExpr.get()).getArguments().get(0));
    newArgs.add(regexStringSlice);
    Optional<RowExpression> likeTranslatedFilter = Optional.of(new CallExpression(((CallExpression) translatedExpr.get()).getDisplayName().toUpperCase(Locale.ROOT), ((CallExpression) translatedExpr.get()).getFunctionHandle(), ((CallExpression) translatedExpr.get()).getType(), newArgs));
    return likeTranslatedFilter;
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) CallExpression(io.prestosql.spi.relation.CallExpression) LinkedList(java.util.LinkedList)

Example 35 with ConstantExpression

use of io.prestosql.spi.relation.ConstantExpression in project boostkit-bigdata by kunpengcompute.

the class DataIoAdapter method createAggProjection.

private RowExpression createAggProjection(Expression expression) {
    Type prestoType = NdpUtils.transOlkDataType(expression.dataType(), false);
    AggExpressionType aggExpressionType = AggExpressionType.valueOf(expression.getClass().getSimpleName());
    switch(aggExpressionType) {
        case Add:
            return createAggBinCall((Add) expression, "Add", prestoType);
        case Subtract:
            return createAggBinCall((Subtract) expression, "Subtract", prestoType);
        case Multiply:
            return createAggBinCall((Multiply) expression, "Multiply", prestoType);
        case Divide:
            return createAggBinCall((Divide) expression, "Divide", prestoType);
        case Remainder:
            return createAggBinCall((Remainder) expression, "Remainder", prestoType);
        case Literal:
            Object value = NdpUtils.transData(expression.dataType().toString(), expression.toString());
            return new ConstantExpression(value, prestoType);
        case AttributeReference:
            String aggColumnName = expression.toString().split("#")[0];
            int field;
            if (null == columnNameMap.get(aggColumnName)) {
                field = columnNameMap.size();
                columnNameMap.put(aggColumnName, field);
                int columnId = NdpUtils.getColumnId(expression.toString()) - columnOffset;
                boolean isPartitionKey = partitionColumnName.contains(aggColumnName);
                String partitionValue = NdpUtils.getPartitionValue(filePath, aggColumnName);
                omnidataColumns.add(new Column(columnId, aggColumnName, prestoType, isPartitionKey, partitionValue));
            } else {
                field = columnNameMap.get(aggColumnName);
            }
            return new InputReferenceExpression(field, prestoType);
        default:
            throw new UnsupportedOperationException("unsupported agg operation type");
    }
}
Also used : RowDecodeType(com.huawei.boostkit.omnidata.decode.type.RowDecodeType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecodeType(com.huawei.boostkit.omnidata.decode.type.DecodeType) BigintType(io.prestosql.spi.type.BigintType) LongDecodeType(com.huawei.boostkit.omnidata.decode.type.LongDecodeType) DoubleType(io.prestosql.spi.type.DoubleType) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) Column(com.huawei.boostkit.omnidata.model.Column) ConstantExpression(io.prestosql.spi.relation.ConstantExpression)

Aggregations

ConstantExpression (io.prestosql.spi.relation.ConstantExpression)35 RowExpression (io.prestosql.spi.relation.RowExpression)23 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)20 CallExpression (io.prestosql.spi.relation.CallExpression)13 Test (org.testng.annotations.Test)12 SpecialForm (io.prestosql.spi.relation.SpecialForm)11 FunctionHandle (io.prestosql.spi.function.FunctionHandle)9 Type (io.prestosql.spi.type.Type)6 ArrayList (java.util.ArrayList)5 Slice (io.airlift.slice.Slice)4 Symbol (io.prestosql.spi.plan.Symbol)4 InputReferenceExpression (io.prestosql.spi.relation.InputReferenceExpression)4 FunctionResolution (io.prestosql.sql.relational.FunctionResolution)4 ImmutableList (com.google.common.collect.ImmutableList)3 Scope (io.airlift.bytecode.Scope)3 LogicalRowExpressions (io.prestosql.expressions.LogicalRowExpressions)3 PlanNode (io.prestosql.spi.plan.PlanNode)3 DateType (io.prestosql.spi.type.DateType)3 Map (java.util.Map)3 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2