Skip to content

Commit

Permalink
Move type alias resolution to Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
martint committed Sep 11, 2019
1 parent d0a17c3 commit abe290f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
10 changes: 10 additions & 0 deletions presto-main/src/main/java/io/prestosql/metadata/TypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public TypeRegistry(Set<Type> types)
addType(BOOLEAN);
addType(BIGINT);
addType(INTEGER);
addType("int", INTEGER);
addType(SMALLINT);
addType(TINYINT);
addType(DOUBLE);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
19 changes: 2 additions & 17 deletions presto-spi/src/main/java/io/prestosql/spi/type/TypeSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, String> BASE_NAME_ALIAS_TO_CANONICAL =
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static final Set<String> 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);
Expand Down Expand Up @@ -113,7 +107,7 @@ public static TypeSignature parseTypeSignature(String signature, Set<String> 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);
Expand All @@ -133,7 +127,7 @@ public static TypeSignature parseTypeSignature(String signature, Set<String> 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;
}
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit abe290f

Please sign in to comment.