Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature parametric types #3 #4264

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions presto-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
<artifactId>jackson-annotations</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>http-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.facebook.presto.client;

import com.facebook.presto.spi.type.NamedTypeSignature;
import com.facebook.presto.spi.type.StandardTypes;
import com.facebook.presto.spi.type.TypeSignature;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -25,43 +27,37 @@
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;

@Immutable
public class ClientTypeSignature
{
private static final Pattern PATTERN = Pattern.compile(".*[<>,].*");
private final String rawType;
private final List<ClientTypeSignature> typeArguments;
private final List<Object> literalArguments;
private final List<ClientTypeSignatureParameter> arguments;

public ClientTypeSignature(TypeSignature typeSignature)
{
this(
typeSignature.getBase(),
Lists.transform(typeSignature.getParameters(), ClientTypeSignature::new),
typeSignature.getLiteralParameters());
Lists.transform(typeSignature.getParameters(), ClientTypeSignatureParameter::new));
}

@JsonCreator
public ClientTypeSignature(
@JsonProperty("rawType") String rawType,
@JsonProperty("typeArguments") List<ClientTypeSignature> typeArguments,
@JsonProperty("literalArguments") List<Object> literalArguments)
@JsonProperty("arguments") List<ClientTypeSignatureParameter> arguments)
{
checkArgument(rawType != null, "rawType is null");
this.rawType = rawType;
checkArgument(!rawType.isEmpty(), "rawType is empty");
checkArgument(!PATTERN.matcher(rawType).matches(), "Bad characters in rawType type: %s", rawType);
checkArgument(typeArguments != null, "typeArguments is null");
checkArgument(literalArguments != null, "literalArguments is null");
for (Object literal : literalArguments) {
checkArgument(literal instanceof String || literal instanceof Long, "Unsupported literal type: %s", literal.getClass());
}
this.typeArguments = unmodifiableList(new ArrayList<>(typeArguments));
this.literalArguments = unmodifiableList(new ArrayList<>(literalArguments));
checkArgument(arguments != null, "arguments is null");
this.arguments = unmodifiableList(new ArrayList<>(arguments));
}

@JsonProperty
Expand All @@ -70,53 +66,95 @@ public String getRawType()
return rawType;
}

@JsonProperty
public List<ClientTypeSignatureParameter> getArguments()
{
return arguments;
}

/**
* This field is deprecated and clients should switch to {@link #getArguments()}
*/
@Deprecated
@JsonProperty
public List<ClientTypeSignature> getTypeArguments()
{
return typeArguments;
List<ClientTypeSignature> result = new ArrayList<>();
for (ClientTypeSignatureParameter argument : arguments) {
switch (argument.getKind()) {
case TYPE_SIGNATURE:
result.add(argument.getTypeSignature());
break;
case NAMED_TYPE_SIGNATURE:
result.add(new ClientTypeSignature(argument.getNamedTypeSignature().getTypeSignature()));
break;
default:
return new ArrayList<>();
}
}
return result;
}

/**
* This field is deprecated and clients should switch to {@link #getArguments()}
*/
@Deprecated
@JsonProperty
public List<Object> getLiteralArguments()
{
return literalArguments;
List<Object> result = new ArrayList<>();
for (ClientTypeSignatureParameter argument : arguments) {
switch (argument.getKind()) {
case NAMED_TYPE_SIGNATURE:
result.add(argument.getNamedTypeSignature().getName());
break;
default:
return new ArrayList<>();
}
}
return result;
}

@Override
public String toString()
{
StringBuilder typeName = new StringBuilder(rawType);
if (!typeArguments.isEmpty()) {
typeName.append("<");
boolean first = true;
for (ClientTypeSignature argument : typeArguments) {
if (!first) {
typeName.append(",");
}
first = false;
typeName.append(argument.toString());
}
typeName.append(">");
if (rawType.equals(StandardTypes.ROW)) {
return rowToString();
}
if (!literalArguments.isEmpty()) {
typeName.append("(");
boolean first = true;
for (Object parameter : literalArguments) {
if (!first) {
typeName.append(",");
}
first = false;
if (parameter instanceof String) {
typeName.append("'").append(parameter).append("'");
}
else {
typeName.append(parameter.toString());
else {
StringBuilder typeName = new StringBuilder(rawType);
if (!arguments.isEmpty()) {
typeName.append("(");
boolean first = true;
for (ClientTypeSignatureParameter argument : arguments) {
if (!first) {
typeName.append(",");
}
first = false;
typeName.append(argument.toString());
}
typeName.append(")");
}
typeName.append(")");
return typeName.toString();
}
}

return typeName.toString();
@Deprecated
private String rowToString()
{
String types = arguments.stream()
.map(ClientTypeSignatureParameter::getNamedTypeSignature)
.map(NamedTypeSignature::getTypeSignature)
.map(TypeSignature::toString)
.collect(Collectors.joining(","));

String fieldNames = arguments.stream()
.map(ClientTypeSignatureParameter::getNamedTypeSignature)
.map(NamedTypeSignature::getName)
.map(name -> format("'%s'", name))
.collect(Collectors.joining(","));

return format("row<%s>(%s)", types, fieldNames);
}

@Override
Expand All @@ -132,13 +170,12 @@ public boolean equals(Object o)
ClientTypeSignature other = (ClientTypeSignature) o;

return Objects.equals(this.rawType.toLowerCase(Locale.ENGLISH), other.rawType.toLowerCase(Locale.ENGLISH)) &&
Objects.equals(this.typeArguments, other.typeArguments) &&
Objects.equals(this.literalArguments, other.literalArguments);
Objects.equals(this.arguments, other.arguments);
}

@Override
public int hashCode()
{
return Objects.hash(rawType.toLowerCase(Locale.ENGLISH), typeArguments, literalArguments);
return Objects.hash(rawType.toLowerCase(Locale.ENGLISH), arguments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* 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.client;

import com.facebook.presto.spi.type.NamedTypeSignature;
import com.facebook.presto.spi.type.ParameterKind;
import com.facebook.presto.spi.type.TypeSignatureParameter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import javax.annotation.concurrent.Immutable;

import java.io.IOException;
import java.util.Objects;

import static java.lang.String.format;

@Immutable
@JsonDeserialize(using = ClientTypeSignatureParameter.ClientTypeSignatureParameterDeserializer.class)
public class ClientTypeSignatureParameter
{
private final ParameterKind kind;
private final Object value;

public ClientTypeSignatureParameter(TypeSignatureParameter typeParameterSignature)
{
this.kind = typeParameterSignature.getKind();
switch (kind) {
case TYPE_SIGNATURE:
value = new ClientTypeSignature(typeParameterSignature.getTypeSignature());
break;
case LONG_LITERAL:
value = typeParameterSignature.getLongLiteral();
break;
case NAMED_TYPE_SIGNATURE:
value = typeParameterSignature.getNamedTypeSignature();
break;
default:
throw new UnsupportedOperationException(format("Unknown kind [%s]", kind));
}
}

@JsonCreator
public ClientTypeSignatureParameter(
@JsonProperty("kind") ParameterKind kind,
@JsonProperty("value") Object value)
{
this.kind = kind;
this.value = value;
}

@JsonProperty
public ParameterKind getKind()
{
return kind;
}

@JsonProperty
public Object getValue()
{
return value;
}

private <A> A getValue(ParameterKind expectedParameterKind, Class<A> target)
{
if (kind != expectedParameterKind) {
throw new IllegalArgumentException(format("ParameterKind is [%s] but expected [%s]", kind, expectedParameterKind));
}
return target.cast(value);
}

public ClientTypeSignature getTypeSignature()
{
return getValue(ParameterKind.TYPE_SIGNATURE, ClientTypeSignature.class);
}

public Long getLongLiteral()
{
return getValue(ParameterKind.LONG_LITERAL, Long.class);
}

public NamedTypeSignature getNamedTypeSignature()
{
return getValue(ParameterKind.NAMED_TYPE_SIGNATURE, NamedTypeSignature.class);
}

@Override
public String toString()
{
return value.toString();
}

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

ClientTypeSignatureParameter other = (ClientTypeSignatureParameter) o;

return Objects.equals(this.kind, other.kind) &&
Objects.equals(this.value, other.value);
}

@Override
public int hashCode()
{
return Objects.hash(kind, value);
}

public static class ClientTypeSignatureParameterDeserializer extends JsonDeserializer<ClientTypeSignatureParameter>
{
private static final ObjectMapper MAPPER = new ObjectMapper();

@Override
public ClientTypeSignatureParameter deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException
{
JsonNode node = jp.getCodec().readTree(jp);
ParameterKind kind = MAPPER.readValue(MAPPER.treeAsTokens(node.get("kind")), ParameterKind.class);
JsonParser jsonValue = MAPPER.treeAsTokens(node.get("value"));
Object value;
switch (kind) {
case TYPE_SIGNATURE:
value = MAPPER.readValue(jsonValue, ClientTypeSignature.class);
break;
case NAMED_TYPE_SIGNATURE:
value = MAPPER.readValue(jsonValue, NamedTypeSignature.class);
break;
case LONG_LITERAL:
value = MAPPER.readValue(jsonValue, Long.class);
break;
default:
throw new UnsupportedOperationException(format("Unsupported kind [%s]", kind));
}
return new ClientTypeSignatureParameter(kind, value);
}
}
}
Loading