Search in sources :

Example 21 with OperatorType

use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.

the class TestFunctionRegistry method testExactMatchBeforeCoercion.

@Test
public void testExactMatchBeforeCoercion() {
    Metadata metadata = createTestMetadataManager();
    boolean foundOperator = false;
    for (SqlFunction function : listOperators(metadata)) {
        OperatorType operatorType = unmangleOperator(function.getSignature().getName().getObjectName());
        if (operatorType == OperatorType.CAST || operatorType == OperatorType.SATURATED_FLOOR_CAST) {
            continue;
        }
        if (!function.getSignature().getTypeVariableConstraints().isEmpty()) {
            continue;
        }
        if (function.getSignature().getArgumentTypes().stream().anyMatch(TypeSignature::isCalculated)) {
            continue;
        }
        List<Type> argumentTypes = function.getSignature().getArgumentTypes().stream().map(metadata::getType).collect(toImmutableList());
        Signature exactOperator = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of(mangleOperatorName(operatorType)), fromTypes(argumentTypes));
        assertEquals(exactOperator, function.getSignature());
        foundOperator = true;
    }
    assertTrue(foundOperator);
}
Also used : TypeSignature(io.prestosql.spi.type.TypeSignature) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) SqlType(io.prestosql.spi.function.SqlType) Signature(io.prestosql.spi.function.Signature) TypeSignature(io.prestosql.spi.type.TypeSignature) OperatorType(io.prestosql.spi.function.OperatorType) SqlFunction(io.prestosql.spi.function.SqlFunction) Test(org.testng.annotations.Test)

Example 22 with OperatorType

use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.

the class DecimalOperators method decimalSubtractOperator.

private static SqlScalarFunction decimalSubtractOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = Signature.builder().kind(SCALAR).operatorType(SUBTRACT).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class, SUBTRACT).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Also used : ADD(io.prestosql.spi.function.OperatorType.ADD) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) LiteralParameters(io.prestosql.spi.function.LiteralParameters) DecimalType(io.prestosql.spi.type.DecimalType) Math.abs(java.lang.Math.abs) UnscaledDecimal128Arithmetic.unscaledDecimal(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) DIVISION_BY_ZERO(io.prestosql.spi.StandardErrorCode.DIVISION_BY_ZERO) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) IsNull(io.prestosql.spi.function.IsNull) ScalarOperator(io.prestosql.spi.function.ScalarOperator) INDETERMINATE(io.prestosql.spi.function.OperatorType.INDETERMINATE) OperatorType(io.prestosql.spi.function.OperatorType) BigInteger(java.math.BigInteger) PolymorphicScalarFunctionBuilder(io.prestosql.metadata.PolymorphicScalarFunctionBuilder) Long.signum(java.lang.Long.signum) MULTIPLY(io.prestosql.spi.function.OperatorType.MULTIPLY) UnscaledDecimal128Arithmetic(io.prestosql.spi.type.UnscaledDecimal128Arithmetic) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) Decimals.longTenToNth(io.prestosql.spi.type.Decimals.longTenToNth) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) List(java.util.List) UnscaledDecimal128Arithmetic.divideRoundUp(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) SqlType(io.prestosql.spi.function.SqlType) TypeSignature(io.prestosql.spi.type.TypeSignature) MODULUS(io.prestosql.spi.function.OperatorType.MODULUS) HASH_CODE(io.prestosql.spi.function.OperatorType.HASH_CODE) DIVIDE(io.prestosql.spi.function.OperatorType.DIVIDE) Slice(io.airlift.slice.Slice) StandardTypes(io.prestosql.spi.type.StandardTypes) UnscaledDecimal128Arithmetic.throwIfOverflows(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) SpecializeContext(io.prestosql.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) NUMERIC_VALUE_OUT_OF_RANGE(io.prestosql.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) Decimals(io.prestosql.spi.type.Decimals) UnscaledDecimal128Arithmetic.isZero(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) SignatureBuilder(io.prestosql.spi.function.SignatureBuilder) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Signature.longVariableExpression(io.prestosql.spi.function.Signature.longVariableExpression) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) XX_HASH_64(io.prestosql.spi.function.OperatorType.XX_HASH_64) XxHash64(io.airlift.slice.XxHash64) Decimals.encodeUnscaledValue(io.prestosql.spi.type.Decimals.encodeUnscaledValue) SUBTRACT(io.prestosql.spi.function.OperatorType.SUBTRACT) Integer.max(java.lang.Integer.max) UnscaledDecimal128Arithmetic.remainder(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(io.prestosql.spi.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.rescale) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature)

Example 23 with OperatorType

use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.

the class BTreeIndex method lookUp.

@Override
public Iterator<String> lookUp(Object expression) throws IndexLookUpException {
    Collection<String> lookUpResults = Collections.emptyList();
    if (expression instanceof CallExpression) {
        CallExpression callExp = (CallExpression) expression;
        Object key = extractValueFromRowExpression(callExp.getArguments().get(1));
        BuiltInFunctionHandle builtInFunctionHandle;
        if (callExp.getFunctionHandle() instanceof BuiltInFunctionHandle) {
            builtInFunctionHandle = (BuiltInFunctionHandle) callExp.getFunctionHandle();
        } else {
            throw new UnsupportedOperationException("Unsupported function: " + callExp.getDisplayName());
        }
        Optional<OperatorType> operatorOptional = Signature.getOperatorType(builtInFunctionHandle.getSignature().getNameSuffix());
        if (operatorOptional.isPresent()) {
            OperatorType operator = operatorOptional.get();
            switch(operator) {
                case EQUAL:
                    if (dataMap.containsKey(key)) {
                        lookUpResults = Collections.singleton(dataMap.get(key));
                    }
                    break;
                case LESS_THAN:
                    lookUpResults = rangeLookUp(dataMap.firstKey(), true, key, false);
                    break;
                case LESS_THAN_OR_EQUAL:
                    lookUpResults = rangeLookUp(dataMap.firstKey(), true, key, true);
                    break;
                case GREATER_THAN:
                    lookUpResults = rangeLookUp(key, false, dataMap.lastKey(), true);
                    break;
                case GREATER_THAN_OR_EQUAL:
                    lookUpResults = rangeLookUp(key, true, dataMap.lastKey(), true);
                    break;
                default:
                    throw new UnsupportedOperationException("Expression not supported");
            }
        }
    } else if (expression instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) expression;
        switch(specialForm.getForm()) {
            case BETWEEN:
                Object left = extractValueFromRowExpression(specialForm.getArguments().get(1));
                Object right = extractValueFromRowExpression(specialForm.getArguments().get(2));
                lookUpResults = rangeLookUp(left, true, right, true);
                break;
            case IN:
                lookUpResults = new ArrayList<>();
                for (RowExpression exp : specialForm.getArguments().subList(1, specialForm.getArguments().size())) {
                    Object key = extractValueFromRowExpression(exp);
                    if (dataMap.containsKey(key)) {
                        lookUpResults.add(dataMap.get(key));
                    }
                }
                break;
            default:
                throw new UnsupportedOperationException("Expression not supported");
        }
    } else {
        throw new UnsupportedOperationException("Expression not supported");
    }
    Set<String> symbolSet = new HashSet<>();
    // break lookUp results to symbols and keep in a set. e.g. ["1,2,2,4","2,3"] -> set{"1", "2", "3", "4"}
    for (String res : lookUpResults) {
        Collections.addAll(symbolSet, res.split(","));
    }
    // translate the symbols to actual data values
    List<String> translated = new ArrayList<>(symbolSet.size());
    for (String sym : symbolSet) {
        translated.add(symbolTable != null ? symbolTable.get(sym) : sym);
    }
    translated.sort(String::compareTo);
    return translated.iterator();
}
Also used : ArrayList(java.util.ArrayList) TypeUtils.extractValueFromRowExpression(io.prestosql.spi.heuristicindex.TypeUtils.extractValueFromRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) OperatorType(io.prestosql.spi.function.OperatorType) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) HashSet(java.util.HashSet)

Example 24 with OperatorType

use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.

the class HanaRowExpressionConverter method handleOperatorFunction.

private String handleOperatorFunction(CallExpression call, FunctionMetadata functionMetadata, JdbcConverterContext context) {
    Optional<OperatorType> operatorTypeOptional = functionMetadata.getOperatorType();
    OperatorType type = operatorTypeOptional.get();
    if (type.equals(OperatorType.CAST)) {
        return handleCastOperator(call.getArguments().get(0), call.getType(), context);
    }
    List<String> argumentList = call.getArguments().stream().map(expr -> expr.accept(this, context)).collect(Collectors.toList());
    if (type.isArithmeticOperator()) {
        if (type.equals(OperatorType.MODULUS)) {
            return format("MOD(%s, %s)", argumentList.get(0), argumentList.get(1));
        } else {
            return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
        }
    }
    if (type.isComparisonOperator()) {
        final String[] hanaCompareOperators = new String[] { "=", ">", "<", ">=", "<=", "!=", "<>" };
        if (Arrays.asList(hanaCompareOperators).contains(type.getOperator())) {
            return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
        } else {
            String exceptionInfo = "Hana Connector does not support comparison operator " + type.getOperator();
            throw new PrestoException(NOT_SUPPORTED, exceptionInfo);
        }
    }
    if (type.equals(OperatorType.SUBSCRIPT)) {
        if (call.getArguments().size() == 2) {
            return format("MEMBER_AT(%s, %s)", argumentList.get(0), argumentList.get(1));
        }
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal argument num of function " + type.getOperator());
    }
    if (call.getArguments().size() == 1 && type.equals(OperatorType.NEGATION)) {
        String value = argumentList.get(0);
        String separator = value.startsWith("-") ? " " : "";
        return format("-%s%s", separator, value);
    }
    throw new PrestoException(NOT_SUPPORTED, String.format("Unknown operator %s in push down", type.getOperator()));
}
Also used : Arrays(java.util.Arrays) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) DeterminismEvaluator(io.prestosql.spi.relation.DeterminismEvaluator) CallExpression(io.prestosql.spi.relation.CallExpression) DateTimeFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.DateTimeFunctionCallRewriter) ConfigSupplier(io.prestosql.configmanager.ConfigSupplier) OperatorType(io.prestosql.spi.function.OperatorType) Map(java.util.Map) FunctionMetadataManager(io.prestosql.spi.function.FunctionMetadataManager) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) Type(io.prestosql.spi.type.Type) FromBase64CallRewriter(io.prestosql.sql.builder.functioncall.functions.base.FromBase64CallRewriter) ENGLISH(java.util.Locale.ENGLISH) PrestoException(io.prestosql.spi.PrestoException) TIME(io.prestosql.spi.type.TimeType.TIME) Set(java.util.Set) FunctionCallRewriter(io.prestosql.sql.builder.functioncall.functions.FunctionCallRewriter) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) BuildInDirectMapFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.BuildInDirectMapFunctionCallRewriter) FunctionHandle(io.prestosql.spi.function.FunctionHandle) RowExpressionService(io.prestosql.spi.relation.RowExpressionService) List(java.util.List) BaseJdbcRowExpressionConverter(io.prestosql.plugin.jdbc.optimization.BaseJdbcRowExpressionConverter) Stream(java.util.stream.Stream) ArrayConstructorCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.ArrayConstructorCallRewriter) VarbinaryLiteralFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.VarbinaryLiteralFunctionCallRewriter) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) QualifiedName(io.prestosql.spi.sql.expression.QualifiedName) Optional(java.util.Optional) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) DefaultUdfRewriteConfigSupplier(io.prestosql.configmanager.DefaultUdfRewriteConfigSupplier) Joiner(com.google.common.base.Joiner) FunctionWriterManager(io.prestosql.sql.builder.functioncall.FunctionWriterManager) HanaConfig(io.hetu.core.plugin.hana.HanaConfig) Slice(io.airlift.slice.Slice) JdbcConverterContext(io.prestosql.plugin.jdbc.optimization.JdbcConverterContext) HashMap(java.util.HashMap) StandardFunctionResolution(io.prestosql.spi.function.StandardFunctionResolution) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) HanaConstants(io.hetu.core.plugin.hana.HanaConstants) DateTimeUtils(io.prestosql.spi.util.DateTimeUtils) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) SpecialForm(io.prestosql.spi.relation.SpecialForm) HanaUnsupportedFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.HanaUnsupportedFunctionCallRewriter) DefaultConnectorConfigFunctionRewriter(io.prestosql.sql.builder.functioncall.functions.config.DefaultConnectorConfigFunctionRewriter) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) UdfFunctionRewriteConstants(io.hetu.core.plugin.hana.rewrite.UdfFunctionRewriteConstants) FunctionWriterManagerGroup(io.prestosql.sql.builder.functioncall.FunctionWriterManagerGroup) DateAddFunctionCallRewrite(io.hetu.core.plugin.hana.rewrite.functioncall.DateAddFunctionCallRewrite) RowExpression(io.prestosql.spi.relation.RowExpression) Collections(java.util.Collections) VarcharType(io.prestosql.spi.type.VarcharType) PrestoException(io.prestosql.spi.PrestoException) OperatorType(io.prestosql.spi.function.OperatorType)

Example 25 with OperatorType

use of io.prestosql.spi.function.OperatorType 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)

Aggregations

OperatorType (io.prestosql.spi.function.OperatorType)28 CallExpression (io.prestosql.spi.relation.CallExpression)11 PrestoException (io.prestosql.spi.PrestoException)10 Signature (io.prestosql.spi.function.Signature)9 ImmutableList (com.google.common.collect.ImmutableList)8 FunctionMetadata (io.prestosql.spi.function.FunctionMetadata)8 TypeSignature (io.prestosql.spi.type.TypeSignature)8 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)8 List (java.util.List)8 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)7 RowExpression (io.prestosql.spi.relation.RowExpression)7 FunctionHandle (io.prestosql.spi.function.FunctionHandle)6 Slice (io.airlift.slice.Slice)5 SqlType (io.prestosql.spi.function.SqlType)5 SpecialForm (io.prestosql.spi.relation.SpecialForm)5 Type (io.prestosql.spi.type.Type)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 SqlScalarFunction (io.prestosql.metadata.SqlScalarFunction)4 UsedByGeneratedCode (io.prestosql.spi.annotation.UsedByGeneratedCode)4