Search in sources :

Example 11 with FunctionMetadata

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

the class ClickHouseRowExpressionConverter 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()) {
        return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
    }
    if (type.isComparisonOperator()) {
        final String[] clickHouseCompareOperators = new String[] { "=", ">", "<", ">=", "<=", "!=", "<>" };
        if (Arrays.asList(clickHouseCompareOperators).contains(type.getOperator())) {
            return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
        } else {
            String exceptionInfo = "ClickHouse Connector does not support comparison operator " + type.getOperator();
            throw new PrestoException(NOT_SUPPORTED, exceptionInfo);
        }
    }
    if (type.equals(OperatorType.SUBSCRIPT)) {
        throw new PrestoException(NOT_SUPPORTED, "ClickHouse Connector does not support subscript now");
    }
    /*
         * "Negative" needs to be tested
         */
    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 : FunctionWriterManager(io.prestosql.sql.builder.functioncall.FunctionWriterManager) Arrays(java.util.Arrays) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) JdbcConverterContext(io.prestosql.plugin.jdbc.optimization.JdbcConverterContext) HashMap(java.util.HashMap) DateParseFunctionCallRewriter(io.hetu.core.plugin.clickhouse.rewrite.functioncall.DateParseFunctionCallRewriter) StandardFunctionResolution(io.prestosql.spi.function.StandardFunctionResolution) DeterminismEvaluator(io.prestosql.spi.relation.DeterminismEvaluator) BaseFunctionUtil.isDefaultFunction(io.prestosql.sql.builder.functioncall.BaseFunctionUtil.isDefaultFunction) CallExpression(io.prestosql.spi.relation.CallExpression) ClickHouseConstants(io.hetu.core.plugin.clickhouse.ClickHouseConstants) ConfigSupplier(io.prestosql.configmanager.ConfigSupplier) OperatorType(io.prestosql.spi.function.OperatorType) Map(java.util.Map) FunctionMetadataManager(io.prestosql.spi.function.FunctionMetadataManager) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) UdfFunctionRewriteConstants(io.hetu.core.plugin.clickhouse.rewrite.UdfFunctionRewriteConstants) Type(io.prestosql.spi.type.Type) FromBase64CallRewriter(io.prestosql.sql.builder.functioncall.functions.base.FromBase64CallRewriter) SpecialForm(io.prestosql.spi.relation.SpecialForm) ENGLISH(java.util.Locale.ENGLISH) DefaultConnectorConfigFunctionRewriter(io.prestosql.sql.builder.functioncall.functions.config.DefaultConnectorConfigFunctionRewriter) PrestoException(io.prestosql.spi.PrestoException) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Set(java.util.Set) FunctionCallRewriter(io.prestosql.sql.builder.functioncall.functions.FunctionCallRewriter) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) FunctionHandle(io.prestosql.spi.function.FunctionHandle) ClickHouseUnsupportedFunctionCallRewriter(io.hetu.core.plugin.clickhouse.rewrite.ClickHouseUnsupportedFunctionCallRewriter) RowExpressionService(io.prestosql.spi.relation.RowExpressionService) List(java.util.List) BaseJdbcRowExpressionConverter(io.prestosql.plugin.jdbc.optimization.BaseJdbcRowExpressionConverter) Stream(java.util.stream.Stream) FunctionWriterManagerGroup(io.prestosql.sql.builder.functioncall.FunctionWriterManagerGroup) QualifiedName(io.prestosql.spi.sql.expression.QualifiedName) RowExpression(io.prestosql.spi.relation.RowExpression) Optional(java.util.Optional) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) DefaultUdfRewriteConfigSupplier(io.prestosql.configmanager.DefaultUdfRewriteConfigSupplier) BuildInDirectMapFunctionCallRewriter(io.hetu.core.plugin.clickhouse.rewrite.BuildInDirectMapFunctionCallRewriter) ClickHouseConfig(io.hetu.core.plugin.clickhouse.ClickHouseConfig) Collections(java.util.Collections) VarcharType(io.prestosql.spi.type.VarcharType) Joiner(com.google.common.base.Joiner) PrestoException(io.prestosql.spi.PrestoException) OperatorType(io.prestosql.spi.function.OperatorType)

Example 12 with FunctionMetadata

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

the class WindowFunctionValidator method visitFunctionCall.

@Override
protected Void visitFunctionCall(FunctionCall functionCall, Analysis analysis) {
    requireNonNull(analysis, "analysis is null");
    FunctionMetadata functionMetadata = functionAndTypeManager.getFunctionMetadata(analysis.getFunctionHandle(functionCall));
    if (functionMetadata != null && functionMetadata.getFunctionKind() == WINDOW && !functionCall.getWindow().isPresent()) {
        throw new SemanticException(WINDOW_REQUIRES_OVER, functionCall, "Window function %s requires an OVER clause", functionMetadata.getName());
    }
    return super.visitFunctionCall(functionCall, analysis);
}
Also used : FunctionMetadata(io.prestosql.spi.function.FunctionMetadata)

Example 13 with FunctionMetadata

use of io.prestosql.spi.function.FunctionMetadata 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 14 with FunctionMetadata

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

the class HanaRowExpressionConverter method visitCall.

@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
    FunctionHandle functionHandle = call.getFunctionHandle();
    FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(functionHandle);
    String functionName = functionMetadata.getName().getObjectName();
    if (hanaNotSupportFunctions.contains(functionName)) {
        throw new PrestoException(NOT_SUPPORTED, "Hana connector does not support " + functionName);
    }
    if (standardFunctionResolution.isOperator(functionHandle)) {
        return handleOperatorFunction(call, functionMetadata, context);
    }
    List<String> argumentList = call.getArguments().stream().map(expr -> expr.accept(this, context)).collect(Collectors.toList());
    if (standardFunctionResolution.isNotFunction(functionHandle)) {
        return format("(NOT %s)", argumentList.get(0));
    }
    if (standardFunctionResolution.isLikeFunction(functionHandle)) {
        return format("(%s LIKE %s)", argumentList.get(0), argumentList.get(1));
    }
    if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
        return format("ARRAY(%s)", Joiner.on(", ").join(argumentList));
    }
    return functionCall(new QualifiedName(Collections.singletonList(functionName)), false, argumentList, Optional.empty(), Optional.empty(), Optional.empty());
}
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) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) QualifiedName(io.prestosql.spi.sql.expression.QualifiedName) PrestoException(io.prestosql.spi.PrestoException) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 15 with FunctionMetadata

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

FunctionMetadata (io.prestosql.spi.function.FunctionMetadata)16 OperatorType (io.prestosql.spi.function.OperatorType)10 PrestoException (io.prestosql.spi.PrestoException)9 CallExpression (io.prestosql.spi.relation.CallExpression)8 RowExpression (io.prestosql.spi.relation.RowExpression)8 FunctionHandle (io.prestosql.spi.function.FunctionHandle)7 Type (io.prestosql.spi.type.Type)6 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)5 String.format (java.lang.String.format)5 List (java.util.List)5 Map (java.util.Map)5 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 Joiner (com.google.common.base.Joiner)4 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)4 ConfigSupplier (io.prestosql.configmanager.ConfigSupplier)4 DefaultUdfRewriteConfigSupplier (io.prestosql.configmanager.DefaultUdfRewriteConfigSupplier)4 BaseJdbcRowExpressionConverter (io.prestosql.plugin.jdbc.optimization.BaseJdbcRowExpressionConverter)4 JdbcConverterContext (io.prestosql.plugin.jdbc.optimization.JdbcConverterContext)4 NOT_SUPPORTED (io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED)4