Skip to content

Commit

Permalink
Add QualifiedObjectName to TypeSignature
Browse files Browse the repository at this point in the history
The TypeSignature base can either be a unqualified name, which means it's a standard type,
or it can be a QualifiedObjectName, which means it's a schema type.
  • Loading branch information
rongrong committed Nov 10, 2020
1 parent b411d77 commit a6d77a6
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package com.facebook.presto.common.type;

import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.common.type.LongEnumType.LongEnumMap;
import com.facebook.presto.common.type.VarcharEnumType.VarcharEnumMap;
import com.facebook.presto.common.type.encoding.Base32;
Expand Down Expand Up @@ -41,7 +42,7 @@

public class TypeSignature
{
private final String base;
private final TypeSignatureBase base;
private final List<TypeSignatureParameter> parameters;
private final boolean calculated;

Expand All @@ -65,17 +66,29 @@ public class TypeSignature
SIMPLE_TYPE_WITH_SPACES.add("double precision");
}

public TypeSignature(QualifiedObjectName base, TypeSignatureParameter... parameters)
{
this(base, asList(parameters));
}

public TypeSignature(QualifiedObjectName base, List<TypeSignatureParameter> parameters)
{
this(new TypeSignatureBase(base), parameters);
}

public TypeSignature(String base, TypeSignatureParameter... parameters)
{
this(base, asList(parameters));
}

public TypeSignature(String base, List<TypeSignatureParameter> parameters)
{
checkArgument(base != null, "base is null");
this(new TypeSignatureBase(base), parameters);
}

private TypeSignature(TypeSignatureBase base, List<TypeSignatureParameter> parameters)
{
this.base = base;
checkArgument(!base.isEmpty(), "base is empty");
checkArgument(validateName(base), "Bad characters in base type: %s", base);
checkArgument(parameters != null, "parameters is null");
this.parameters = unmodifiableList(new ArrayList<>(parameters));

Expand All @@ -84,7 +97,7 @@ public TypeSignature(String base, List<TypeSignatureParameter> parameters)

public String getBase()
{
return base;
return base.toString();
}

public List<TypeSignatureParameter> getParameters()
Expand Down Expand Up @@ -516,18 +529,19 @@ private static boolean isValidStartOfIdentifier(char c)
@JsonValue
public String toString()
{
String baseString = base.toString();
if (parameters.isEmpty()) {
return base;
return baseString;
}

if (base.equalsIgnoreCase(StandardTypes.VARCHAR) &&
if (baseString.equalsIgnoreCase(StandardTypes.VARCHAR) &&
(parameters.size() == 1) &&
parameters.get(0).isLongLiteral() &&
parameters.get(0).getLongLiteral() == VarcharType.UNBOUNDED_LENGTH) {
return base;
return baseString;
}

StringBuilder typeName = new StringBuilder(base);
StringBuilder typeName = new StringBuilder(baseString);
typeName.append("(").append(parameters.get(0));
for (int i = 1; i < parameters.size(); i++) {
typeName.append(",").append(parameters.get(i));
Expand All @@ -550,11 +564,6 @@ private static void verify(boolean argument, String message)
}
}

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);
Expand All @@ -576,13 +585,13 @@ public boolean equals(Object o)

TypeSignature other = (TypeSignature) o;

return Objects.equals(this.base.toLowerCase(ENGLISH), other.base.toLowerCase(ENGLISH)) &&
return Objects.equals(this.base, other.base) &&
Objects.equals(this.parameters, other.parameters);
}

@Override
public int hashCode()
{
return Objects.hash(base.toLowerCase(ENGLISH), parameters);
return Objects.hash(base, parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 com.facebook.presto.common.type;

import com.facebook.presto.common.QualifiedObjectName;

import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Locale.ENGLISH;

public class TypeSignatureBase
{
private final Optional<String> standardTypeBase;
private final Optional<QualifiedObjectName> typeBase;

public static TypeSignatureBase of(String name)
{
if (name.chars().noneMatch(c -> c == '.')) {
return new TypeSignatureBase(name);
}
return new TypeSignatureBase(QualifiedObjectName.valueOf(name.toLowerCase(ENGLISH)));
}

public static TypeSignatureBase of(QualifiedObjectName name)
{
return new TypeSignatureBase(name);
}

private TypeSignatureBase(String standardTypeBase)
{
checkArgument(standardTypeBase != null, "standardTypeBase is null");
checkArgument(!standardTypeBase.isEmpty(), "standardTypeBase is empty");
checkArgument(standardTypeBase.chars().noneMatch(c -> c == '.'), "Standard type %s should not have '.' in it", standardTypeBase);
checkArgument(validateName(standardTypeBase), "Bad characters in base type: %s", standardTypeBase);
this.standardTypeBase = Optional.of(standardTypeBase);
this.typeBase = Optional.empty();
}

private TypeSignatureBase(QualifiedObjectName typeBase)
{
checkArgument(typeBase != null, "typeBase is null");
checkArgument(validateName(typeBase.getObjectName()), "Bad characters in base type: %s", typeBase);
this.standardTypeBase = Optional.empty();
this.typeBase = Optional.of(typeBase);
}

public boolean isStandardType()
{
return standardTypeBase.isPresent();
}

public QualifiedObjectName getQualifiedObjectName()
{
checkArgument(typeBase.isPresent(), "TypeSignatureBase %s is not a QualifiedObjectName", toString());
return typeBase.get();
}

private static boolean validateName(String name)
{
return name.chars().noneMatch(c -> c == '<' || c == '>' || c == ',');
}

private static void checkArgument(boolean argument, String format, Object... args)
{
if (!argument) {
throw new IllegalArgumentException(format(format, args));
}
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

TypeSignatureBase other = (TypeSignatureBase) obj;

return Objects.equals(standardTypeBase.map(s -> s.toLowerCase(ENGLISH)), other.standardTypeBase.map(s -> s.toLowerCase(ENGLISH))) &&
Objects.equals(typeBase, other.typeBase);
}

@Override
public int hashCode()
{
return Objects.hash(standardTypeBase.map(s -> s.toLowerCase(ENGLISH)), typeBase);
}

@Override
public String toString()
{
return standardTypeBase.orElseGet(() -> typeBase.get().toString());
}
}

0 comments on commit a6d77a6

Please sign in to comment.