From abe290f9fb2054ea3e6730b526a4dde981dde3b9 Mon Sep 17 00:00:00 2001 From: Martin Traverso Date: Tue, 10 Sep 2019 15:03:24 -0700 Subject: [PATCH] Move type alias resolution to Metadata --- .../plugin/geospatial/GeoFunctions.java | 4 ++-- .../io/prestosql/metadata/TypeRegistry.java | 10 ++++++++++ .../scalar/ArraySortComparatorFunction.java | 10 +++++----- .../prestosql/sql/query/TestExpressions.java | 18 ++++++++++++++++++ .../type/TestConventionDependencies.java | 2 +- .../io/prestosql/spi/type/TypeSignature.java | 19 ++----------------- .../prestosql/spi/type/TestTypeSignature.java | 7 ++----- 7 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 presto-main/src/test/java/io/prestosql/sql/query/TestExpressions.java diff --git a/presto-geospatial/src/main/java/io/prestosql/plugin/geospatial/GeoFunctions.java b/presto-geospatial/src/main/java/io/prestosql/plugin/geospatial/GeoFunctions.java index fc511132062cd..003e64b4ed819 100644 --- a/presto-geospatial/src/main/java/io/prestosql/plugin/geospatial/GeoFunctions.java +++ b/presto-geospatial/src/main/java/io/prestosql/plugin/geospatial/GeoFunctions.java @@ -1199,7 +1199,7 @@ public static Slice stGeometryType(@SqlType(GEOMETRY_TYPE_NAME) Slice input) @ScalarFunction @SqlNullable @Description("Returns an array of spatial partition IDs for a given geometry") - @SqlType("array(int)") + @SqlType("array(integer)") public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(GEOMETRY_TYPE_NAME) Slice geometry) { Envelope envelope = deserializeEnvelope(geometry); @@ -1214,7 +1214,7 @@ public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @ScalarFunction @SqlNullable @Description("Returns an array of spatial partition IDs for a geometry representing a set of points within specified distance from the input geometry") - @SqlType("array(int)") + @SqlType("array(integer)") public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(GEOMETRY_TYPE_NAME) Slice geometry, @SqlType(DOUBLE) double distance) { if (isNaN(distance)) { diff --git a/presto-main/src/main/java/io/prestosql/metadata/TypeRegistry.java b/presto-main/src/main/java/io/prestosql/metadata/TypeRegistry.java index b8a07d41efc65..418b57473877b 100644 --- a/presto-main/src/main/java/io/prestosql/metadata/TypeRegistry.java +++ b/presto-main/src/main/java/io/prestosql/metadata/TypeRegistry.java @@ -98,6 +98,7 @@ public TypeRegistry(Set types) addType(BOOLEAN); addType(BIGINT); addType(INTEGER); + addType("int", INTEGER); addType(SMALLINT); addType(TINYINT); addType(DOUBLE); @@ -198,6 +199,15 @@ public void addType(Type type) checkState(existingType == null || existingType.equals(type), "Type %s is already registered", type); } + public void addType(String alias, Type type) + { + requireNonNull(alias, "alias is null"); + requireNonNull(type, "type is null"); + + Type existingType = types.putIfAbsent(TypeSignature.parseTypeSignature(alias), type); + checkState(existingType == null || existingType.equals(type), "Alias %s is already mapped to %s", alias, type); + } + public void addParametricType(ParametricType parametricType) { String name = parametricType.getName().toLowerCase(Locale.ENGLISH); diff --git a/presto-main/src/main/java/io/prestosql/operator/scalar/ArraySortComparatorFunction.java b/presto-main/src/main/java/io/prestosql/operator/scalar/ArraySortComparatorFunction.java index 73ac65b200170..c3f3b1c1d0b13 100644 --- a/presto-main/src/main/java/io/prestosql/operator/scalar/ArraySortComparatorFunction.java +++ b/presto-main/src/main/java/io/prestosql/operator/scalar/ArraySortComparatorFunction.java @@ -54,7 +54,7 @@ public ArraySortComparatorFunction(@TypeParameter("T") Type elementType) public Block sortLong( @TypeParameter("T") Type type, @SqlType("array(T)") Block block, - @SqlType("function(T, T, int)") ComparatorLongLambda function) + @SqlType("function(T, T, integer)") ComparatorLongLambda function) { int arrayLength = block.getPositionCount(); initPositionsList(arrayLength); @@ -74,7 +74,7 @@ public Block sortLong( public Block sortDouble( @TypeParameter("T") Type type, @SqlType("array(T)") Block block, - @SqlType("function(T, T, int)") ComparatorDoubleLambda function) + @SqlType("function(T, T, integer)") ComparatorDoubleLambda function) { int arrayLength = block.getPositionCount(); initPositionsList(arrayLength); @@ -94,7 +94,7 @@ public Block sortDouble( public Block sortBoolean( @TypeParameter("T") Type type, @SqlType("array(T)") Block block, - @SqlType("function(T, T, int)") ComparatorBooleanLambda function) + @SqlType("function(T, T, integer)") ComparatorBooleanLambda function) { int arrayLength = block.getPositionCount(); initPositionsList(arrayLength); @@ -114,7 +114,7 @@ public Block sortBoolean( public Block sortSlice( @TypeParameter("T") Type type, @SqlType("array(T)") Block block, - @SqlType("function(T, T, int)") ComparatorSliceLambda function) + @SqlType("function(T, T, integer)") ComparatorSliceLambda function) { int arrayLength = block.getPositionCount(); initPositionsList(arrayLength); @@ -134,7 +134,7 @@ public Block sortSlice( public Block sortObject( @TypeParameter("T") Type type, @SqlType("array(T)") Block block, - @SqlType("function(T, T, int)") ComparatorBlockLambda function) + @SqlType("function(T, T, integer)") ComparatorBlockLambda function) { int arrayLength = block.getPositionCount(); initPositionsList(arrayLength); diff --git a/presto-main/src/test/java/io/prestosql/sql/query/TestExpressions.java b/presto-main/src/test/java/io/prestosql/sql/query/TestExpressions.java new file mode 100644 index 0000000000000..6054f3d8bb2e2 --- /dev/null +++ b/presto-main/src/test/java/io/prestosql/sql/query/TestExpressions.java @@ -0,0 +1,18 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.prestosql.sql.query; + +public class TestExpressions +{ +} diff --git a/presto-main/src/test/java/io/prestosql/type/TestConventionDependencies.java b/presto-main/src/test/java/io/prestosql/type/TestConventionDependencies.java index 1838c4674e2f0..7eb2689de956f 100644 --- a/presto-main/src/test/java/io/prestosql/type/TestConventionDependencies.java +++ b/presto-main/src/test/java/io/prestosql/type/TestConventionDependencies.java @@ -90,7 +90,7 @@ public static long testBlockPositionConvention( returnType = StandardTypes.INTEGER, argumentTypes = {StandardTypes.INTEGER, StandardTypes.INTEGER}, convention = @Convention(arguments = {NEVER_NULL, BLOCK_POSITION}, result = FAIL_ON_NULL)) MethodHandle function, - @SqlType("array(int)") Block array) + @SqlType("array(integer)") Block array) { long sum = 0; for (int i = 0; i < array.getPositionCount(); i++) { diff --git a/presto-spi/src/main/java/io/prestosql/spi/type/TypeSignature.java b/presto-spi/src/main/java/io/prestosql/spi/type/TypeSignature.java index 8cc8beb99df06..98e005866ea0d 100644 --- a/presto-spi/src/main/java/io/prestosql/spi/type/TypeSignature.java +++ b/presto-spi/src/main/java/io/prestosql/spi/type/TypeSignature.java @@ -20,11 +20,9 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.TreeMap; import java.util.TreeSet; import java.util.regex.Pattern; @@ -40,14 +38,10 @@ public class TypeSignature private final boolean calculated; private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_]([a-zA-Z0-9_:@])*"); - private static final Map BASE_NAME_ALIAS_TO_CANONICAL = - new TreeMap<>(String.CASE_INSENSITIVE_ORDER); private static final Set SIMPLE_TYPE_WITH_SPACES = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); static { - BASE_NAME_ALIAS_TO_CANONICAL.put("int", StandardTypes.INTEGER); - SIMPLE_TYPE_WITH_SPACES.add(StandardTypes.TIME_WITH_TIME_ZONE); SIMPLE_TYPE_WITH_SPACES.add(StandardTypes.TIMESTAMP_WITH_TIME_ZONE); SIMPLE_TYPE_WITH_SPACES.add(StandardTypes.INTERVAL_DAY_TO_SECOND); @@ -113,7 +107,7 @@ public static TypeSignature parseTypeSignature(String signature, Set lit return VarcharType.createUnboundedVarcharType().getTypeSignature(); } checkArgument(!literalCalculationParameters.contains(signature), "Bad type signature: '%s'", signature); - return new TypeSignature(canonicalizeBaseName(signature), new ArrayList<>()); + return new TypeSignature(signature, new ArrayList<>()); } if (signature.toLowerCase(Locale.ENGLISH).startsWith(StandardTypes.ROW + "(")) { return parseRowTypeSignature(signature, literalCalculationParameters); @@ -133,7 +127,7 @@ public static TypeSignature parseTypeSignature(String signature, Set lit if (bracketCount == 0) { verify(baseName == null, "Expected baseName to be null"); verify(parameterStart == -1, "Expected parameter start to be -1"); - baseName = canonicalizeBaseName(signature.substring(0, i)); + baseName = signature.substring(0, i); checkArgument(!literalCalculationParameters.contains(baseName), "Bad type signature: '%s'", signature); parameterStart = i + 1; } @@ -373,15 +367,6 @@ private static boolean validateName(String name) return name.chars().noneMatch(c -> c == '<' || c == '>' || c == ','); } - private static String canonicalizeBaseName(String baseName) - { - String canonicalBaseName = BASE_NAME_ALIAS_TO_CANONICAL.get(baseName); - if (canonicalBaseName == null) { - return baseName; - } - return canonicalBaseName; - } - @Override public boolean equals(Object o) { diff --git a/presto-spi/src/test/java/io/prestosql/spi/type/TestTypeSignature.java b/presto-spi/src/test/java/io/prestosql/spi/type/TestTypeSignature.java index c43d8f79a4032..df3f6637f1159 100644 --- a/presto-spi/src/test/java/io/prestosql/spi/type/TestTypeSignature.java +++ b/presto-spi/src/test/java/io/prestosql/spi/type/TestTypeSignature.java @@ -164,8 +164,8 @@ public void parseRowSignature() rowSignature(namedParameter("a", false, signature("bigint")), namedParameter("b", false, varchar()))); // field type canonicalization - assertEquals(parseTypeSignature("row(col iNt)"), parseTypeSignature("row(col integer)")); - assertEquals(parseTypeSignature("row(a Int(p1))"), parseTypeSignature("row(a integer(p1))")); + assertEquals(parseTypeSignature("row(col Integer)"), parseTypeSignature("row(col integer)")); + assertEquals(parseTypeSignature("row(a Integer(p1))"), parseTypeSignature("row(a integer(p1))")); // signature with invalid type assertRowSignature( @@ -224,13 +224,10 @@ public void parseSignature() { assertSignature("boolean", "boolean", ImmutableList.of()); assertSignature("varchar", "varchar", ImmutableList.of(Integer.toString(VarcharType.UNBOUNDED_LENGTH))); - assertEquals(parseTypeSignature("int"), parseTypeSignature("integer")); assertSignature("array(bigint)", "array", ImmutableList.of("bigint")); - assertEquals(parseTypeSignature("array(int)"), parseTypeSignature("array(integer)")); assertSignature("array(array(bigint))", "array", ImmutableList.of("array(bigint)")); - assertEquals(parseTypeSignature("array(array(int))"), parseTypeSignature("array(array(integer))")); assertSignature( "array(timestamp with time zone)", "array",