From 0ec41a902dc7bb2da774af78e646da99932c9e87 Mon Sep 17 00:00:00 2001 From: Dain Sundstrom Date: Sat, 6 Apr 2019 13:55:24 -0700 Subject: [PATCH] Remove FunctionRegistry.getCoercion(Type, Type) --- presto-main/src/main/java/io/prestosql/cost/StatsUtil.java | 2 +- .../main/java/io/prestosql/metadata/FunctionRegistry.java | 5 ----- .../java/io/prestosql/operator/scalar/FormatFunction.java | 2 +- .../java/io/prestosql/operator/scalar/MapToMapCast.java | 4 +++- .../java/io/prestosql/operator/scalar/TryCastFunction.java | 2 +- .../java/io/prestosql/sql/analyzer/ExpressionAnalyzer.java | 4 ++-- .../main/java/io/prestosql/sql/gen/CastCodeGenerator.java | 2 +- .../java/io/prestosql/sql/planner/DomainTranslator.java | 2 +- .../io/prestosql/sql/planner/ExpressionInterpreter.java | 6 +++--- .../java/io/prestosql/sql/planner/LiteralInterpreter.java | 2 +- .../sql/planner/iterative/rule/UnwrapCastInComparison.java | 4 ++-- .../sql/relational/optimizer/ExpressionOptimizer.java | 2 +- .../java/io/prestosql/metadata/TestFunctionRegistry.java | 2 +- 13 files changed, 18 insertions(+), 21 deletions(-) diff --git a/presto-main/src/main/java/io/prestosql/cost/StatsUtil.java b/presto-main/src/main/java/io/prestosql/cost/StatsUtil.java index ff2d0679331d5..4eefadb3d79e5 100644 --- a/presto-main/src/main/java/io/prestosql/cost/StatsUtil.java +++ b/presto-main/src/main/java/io/prestosql/cost/StatsUtil.java @@ -40,7 +40,7 @@ static OptionalDouble toStatsRepresentation(Metadata metadata, Session session, { if (convertibleToDoubleWithCast(type)) { InterpretedFunctionInvoker functionInvoker = new InterpretedFunctionInvoker(metadata.getFunctionRegistry()); - Signature castSignature = metadata.getFunctionRegistry().getCoercion(type, DoubleType.DOUBLE); + Signature castSignature = metadata.getFunctionRegistry().getCoercion(type.getTypeSignature(), DoubleType.DOUBLE.getTypeSignature()); return OptionalDouble.of((double) functionInvoker.invoke(castSignature, session.toConnectorSession(), singletonList(value))); } diff --git a/presto-main/src/main/java/io/prestosql/metadata/FunctionRegistry.java b/presto-main/src/main/java/io/prestosql/metadata/FunctionRegistry.java index 5c1a43460171a..c3915f08958c9 100644 --- a/presto-main/src/main/java/io/prestosql/metadata/FunctionRegistry.java +++ b/presto-main/src/main/java/io/prestosql/metadata/FunctionRegistry.java @@ -1098,11 +1098,6 @@ public Signature resolveOperator(OperatorType operatorType, List } } - public Signature getCoercion(Type fromType, Type toType) - { - return getCoercion(fromType.getTypeSignature(), toType.getTypeSignature()); - } - public Signature getCoercion(TypeSignature fromType, TypeSignature toType) { Signature signature = internalOperator(OperatorType.CAST.name(), toType, ImmutableList.of(fromType)); diff --git a/presto-main/src/main/java/io/prestosql/operator/scalar/FormatFunction.java b/presto-main/src/main/java/io/prestosql/operator/scalar/FormatFunction.java index 98e828957cf8f..105e7110379e5 100644 --- a/presto-main/src/main/java/io/prestosql/operator/scalar/FormatFunction.java +++ b/presto-main/src/main/java/io/prestosql/operator/scalar/FormatFunction.java @@ -244,7 +244,7 @@ else if (type.getJavaType() == Slice.class) { private static MethodHandle castToVarchar(FunctionRegistry functionRegistry, Type type) { try { - Signature cast = functionRegistry.getCoercion(type, VARCHAR); + Signature cast = functionRegistry.getCoercion(type.getTypeSignature(), VARCHAR.getTypeSignature()); return functionRegistry.getScalarFunctionImplementation(cast).getMethodHandle(); } catch (OperatorNotFoundException e) { diff --git a/presto-main/src/main/java/io/prestosql/operator/scalar/MapToMapCast.java b/presto-main/src/main/java/io/prestosql/operator/scalar/MapToMapCast.java index 37bb340dba691..6d67511b3634b 100644 --- a/presto-main/src/main/java/io/prestosql/operator/scalar/MapToMapCast.java +++ b/presto-main/src/main/java/io/prestosql/operator/scalar/MapToMapCast.java @@ -19,6 +19,7 @@ import io.prestosql.metadata.BoundVariables; import io.prestosql.metadata.FunctionRegistry; import io.prestosql.metadata.Metadata; +import io.prestosql.metadata.Signature; import io.prestosql.metadata.SqlOperator; import io.prestosql.operator.aggregation.TypedSet; import io.prestosql.spi.PrestoException; @@ -107,7 +108,8 @@ private MethodHandle buildProcessor(FunctionRegistry functionRegistry, Type from MethodHandle getter = nativeValueGetter(fromType); // Adapt cast that takes ([ConnectorSession,] ?) to one that takes (?, ConnectorSession), where ? is the return type of getter. - ScalarFunctionImplementation castImplementation = functionRegistry.getScalarFunctionImplementation(functionRegistry.getCoercion(fromType, toType)); + Signature signature = functionRegistry.getCoercion(fromType.getTypeSignature(), toType.getTypeSignature()); + ScalarFunctionImplementation castImplementation = functionRegistry.getScalarFunctionImplementation(signature); MethodHandle cast = castImplementation.getMethodHandle(); if (cast.type().parameterArray()[0] != ConnectorSession.class) { cast = MethodHandles.dropArguments(cast, 0, ConnectorSession.class); diff --git a/presto-main/src/main/java/io/prestosql/operator/scalar/TryCastFunction.java b/presto-main/src/main/java/io/prestosql/operator/scalar/TryCastFunction.java index abc86846e436a..3bb6f89667bd5 100644 --- a/presto-main/src/main/java/io/prestosql/operator/scalar/TryCastFunction.java +++ b/presto-main/src/main/java/io/prestosql/operator/scalar/TryCastFunction.java @@ -81,7 +81,7 @@ public ScalarFunctionImplementation specialize(BoundVariables boundVariables, in // the resulting method needs to return a boxed type FunctionRegistry functionRegistry = metadata.getFunctionRegistry(); - Signature signature = functionRegistry.getCoercion(fromType, toType); + Signature signature = functionRegistry.getCoercion(fromType.getTypeSignature(), toType.getTypeSignature()); ScalarFunctionImplementation implementation = functionRegistry.getScalarFunctionImplementation(signature); argumentProperties = ImmutableList.of(implementation.getArgumentProperty(0)); MethodHandle coercion = implementation.getMethodHandle(); diff --git a/presto-main/src/main/java/io/prestosql/sql/analyzer/ExpressionAnalyzer.java b/presto-main/src/main/java/io/prestosql/sql/analyzer/ExpressionAnalyzer.java index 4933e33cb980c..38486e46c7447 100644 --- a/presto-main/src/main/java/io/prestosql/sql/analyzer/ExpressionAnalyzer.java +++ b/presto-main/src/main/java/io/prestosql/sql/analyzer/ExpressionAnalyzer.java @@ -738,7 +738,7 @@ protected Type visitGenericLiteral(GenericLiteral node, StackableAstVisitorConte if (!JSON.equals(type)) { try { - metadata.getFunctionRegistry().getCoercion(VARCHAR, type); + metadata.getFunctionRegistry().getCoercion(VARCHAR.getTypeSignature(), type.getTypeSignature()); } catch (IllegalArgumentException e) { throw new SemanticException(TYPE_MISMATCH, node, "No literal form for type %s", type); @@ -1054,7 +1054,7 @@ public Type visitCast(Cast node, StackableAstVisitorContext context) Type value = process(node.getExpression(), context); if (!value.equals(UNKNOWN) && !node.isTypeOnly()) { try { - metadata.getFunctionRegistry().getCoercion(value, type); + metadata.getFunctionRegistry().getCoercion(value.getTypeSignature(), type.getTypeSignature()); } catch (OperatorNotFoundException e) { throw new SemanticException(TYPE_MISMATCH, node, "Cannot cast %s to %s", value, type); diff --git a/presto-main/src/main/java/io/prestosql/sql/gen/CastCodeGenerator.java b/presto-main/src/main/java/io/prestosql/sql/gen/CastCodeGenerator.java index 116fdd830f787..a777e31f696ed 100644 --- a/presto-main/src/main/java/io/prestosql/sql/gen/CastCodeGenerator.java +++ b/presto-main/src/main/java/io/prestosql/sql/gen/CastCodeGenerator.java @@ -31,7 +31,7 @@ public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorCon Signature function = generatorContext .getRegistry() - .getCoercion(argument.getType(), returnType); + .getCoercion(argument.getType().getTypeSignature(), returnType.getTypeSignature()); return generatorContext.generateCall(function.getName(), generatorContext.getRegistry().getScalarFunctionImplementation(function), ImmutableList.of(generatorContext.generate(argument))); } diff --git a/presto-main/src/main/java/io/prestosql/sql/planner/DomainTranslator.java b/presto-main/src/main/java/io/prestosql/sql/planner/DomainTranslator.java index 6891a1d6e5cca..3811b612d33f2 100644 --- a/presto-main/src/main/java/io/prestosql/sql/planner/DomainTranslator.java +++ b/presto-main/src/main/java/io/prestosql/sql/planner/DomainTranslator.java @@ -671,7 +671,7 @@ private Optional getSaturatedFloorCastOperator(Type fromType, Type to private int compareOriginalValueToCoerced(Type originalValueType, Object originalValue, Type coercedValueType, Object coercedValue) { - Signature castToOriginalTypeOperator = metadata.getFunctionRegistry().getCoercion(coercedValueType, originalValueType); + Signature castToOriginalTypeOperator = metadata.getFunctionRegistry().getCoercion(coercedValueType.getTypeSignature(), originalValueType.getTypeSignature()); Object coercedValueInOriginalType = functionInvoker.invoke(castToOriginalTypeOperator, session.toConnectorSession(), coercedValue); Block originalValueBlock = Utils.nativeValueToBlock(originalValueType, originalValue); Block coercedValueBlock = Utils.nativeValueToBlock(originalValueType, coercedValueInOriginalType); diff --git a/presto-main/src/main/java/io/prestosql/sql/planner/ExpressionInterpreter.java b/presto-main/src/main/java/io/prestosql/sql/planner/ExpressionInterpreter.java index 677656aa8b03a..d16c1f99afe8c 100644 --- a/presto-main/src/main/java/io/prestosql/sql/planner/ExpressionInterpreter.java +++ b/presto-main/src/main/java/io/prestosql/sql/planner/ExpressionInterpreter.java @@ -786,8 +786,8 @@ protected Object visitNullIfExpression(NullIfExpression node, Object context) Type commonType = typeCoercion.getCommonSuperType(firstType, secondType).get(); - Signature firstCast = metadata.getFunctionRegistry().getCoercion(firstType, commonType); - Signature secondCast = metadata.getFunctionRegistry().getCoercion(secondType, commonType); + Signature firstCast = metadata.getFunctionRegistry().getCoercion(firstType.getTypeSignature(), commonType.getTypeSignature()); + Signature secondCast = metadata.getFunctionRegistry().getCoercion(secondType.getTypeSignature(), commonType.getTypeSignature()); // cast(first as ) == cast(second as ) boolean equal = Boolean.TRUE.equals(invokeOperator( @@ -1089,7 +1089,7 @@ public Object visitCast(Cast node, Object context) return null; } - Signature operator = metadata.getFunctionRegistry().getCoercion(sourceType, targetType); + Signature operator = metadata.getFunctionRegistry().getCoercion(sourceType.getTypeSignature(), targetType.getTypeSignature()); try { return functionInvoker.invoke(operator, session, ImmutableList.of(value)); diff --git a/presto-main/src/main/java/io/prestosql/sql/planner/LiteralInterpreter.java b/presto-main/src/main/java/io/prestosql/sql/planner/LiteralInterpreter.java index b78342b05cb7f..88cec97f93617 100644 --- a/presto-main/src/main/java/io/prestosql/sql/planner/LiteralInterpreter.java +++ b/presto-main/src/main/java/io/prestosql/sql/planner/LiteralInterpreter.java @@ -140,7 +140,7 @@ protected Object visitGenericLiteral(GenericLiteral node, ConnectorSession sessi } try { - Signature signature = metadata.getFunctionRegistry().getCoercion(VARCHAR, type); + Signature signature = metadata.getFunctionRegistry().getCoercion(VARCHAR.getTypeSignature(), type.getTypeSignature()); return functionInvoker.invoke(signature, session, ImmutableList.of(utf8Slice(node.getValue()))); } catch (IllegalArgumentException e) { diff --git a/presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/UnwrapCastInComparison.java b/presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/UnwrapCastInComparison.java index b7bf4d9c001df..bf0a943d6c92e 100644 --- a/presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/UnwrapCastInComparison.java +++ b/presto-main/src/main/java/io/prestosql/sql/planner/iterative/rule/UnwrapCastInComparison.java @@ -178,7 +178,7 @@ private Expression unwrapCast(ComparisonExpression expression) return expression; } - Signature sourceToTarget = metadata.getFunctionRegistry().getCoercion(sourceType, targetType); + Signature sourceToTarget = metadata.getFunctionRegistry().getCoercion(sourceType.getTypeSignature(), targetType.getTypeSignature()); Optional sourceRange = sourceType.getRange(); if (sourceRange.isPresent()) { @@ -269,7 +269,7 @@ private Expression unwrapCast(ComparisonExpression expression) Signature targetToSource; try { - targetToSource = metadata.getFunctionRegistry().getCoercion(targetType, sourceType); + targetToSource = metadata.getFunctionRegistry().getCoercion(targetType.getTypeSignature(), sourceType.getTypeSignature()); } catch (OperatorNotFoundException e) { // Without a cast between target -> source, there's nothing more we can do diff --git a/presto-main/src/main/java/io/prestosql/sql/relational/optimizer/ExpressionOptimizer.java b/presto-main/src/main/java/io/prestosql/sql/relational/optimizer/ExpressionOptimizer.java index 49cb7f0f30b4b..e68a102fc6922 100644 --- a/presto-main/src/main/java/io/prestosql/sql/relational/optimizer/ExpressionOptimizer.java +++ b/presto-main/src/main/java/io/prestosql/sql/relational/optimizer/ExpressionOptimizer.java @@ -250,7 +250,7 @@ private CallExpression rewriteCast(CallExpression call) } return call( - metadata.getFunctionRegistry().getCoercion(call.getArguments().get(0).getType(), call.getType()), + metadata.getFunctionRegistry().getCoercion(call.getArguments().get(0).getType().getTypeSignature(), call.getType().getTypeSignature()), call.getType(), call.getArguments()); } diff --git a/presto-main/src/test/java/io/prestosql/metadata/TestFunctionRegistry.java b/presto-main/src/test/java/io/prestosql/metadata/TestFunctionRegistry.java index 69fbbb27c1dc0..d6c79bcc4fc50 100644 --- a/presto-main/src/test/java/io/prestosql/metadata/TestFunctionRegistry.java +++ b/presto-main/src/test/java/io/prestosql/metadata/TestFunctionRegistry.java @@ -60,7 +60,7 @@ public class TestFunctionRegistry public void testIdentityCast() { FunctionRegistry registry = createTestMetadataManager().getFunctionRegistry(); - Signature exactOperator = registry.getCoercion(HYPER_LOG_LOG, HYPER_LOG_LOG); + Signature exactOperator = registry.getCoercion(HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature()); assertEquals(exactOperator.getName(), mangleOperatorName(OperatorType.CAST.name())); assertEquals(transform(exactOperator.getArgumentTypes(), Functions.toStringFunction()), ImmutableList.of(StandardTypes.HYPER_LOG_LOG)); assertEquals(exactOperator.getReturnType().getBase(), StandardTypes.HYPER_LOG_LOG);