Skip to content

Commit

Permalink
Convert JDBC driver to ClientTypeSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Jan 27, 2019
1 parent 904d511 commit cbbf251
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -132,6 +134,15 @@ public List<ClientTypeSignatureParameter> getArguments()
return arguments;
}

public List<ClientTypeSignature> getArgumentsAsTypeSignatures()
{
return arguments.stream()
.peek(parameter -> checkState(parameter.getKind() == ParameterKind.TYPE,
"Expected all parameters to be TypeSignatures but [%s] was found", parameter))
.map(ClientTypeSignatureParameter::getTypeSignature)
.collect(toImmutableList());
}

/**
* This field is deprecated and clients should switch to {@link #getArguments()}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import com.google.common.collect.ImmutableList;
import io.prestosql.spi.type.NamedTypeSignature;
import io.prestosql.spi.type.ParameterKind;
import io.prestosql.spi.type.TypeSignature;
import io.prestosql.spi.type.TypeSignatureParameter;

import java.util.ArrayList;
import java.util.Base64;
Expand Down Expand Up @@ -52,7 +50,6 @@
import static io.prestosql.spi.type.StandardTypes.TIME_WITH_TIME_ZONE;
import static io.prestosql.spi.type.StandardTypes.TINYINT;
import static io.prestosql.spi.type.StandardTypes.VARCHAR;
import static io.prestosql.spi.type.TypeSignature.parseTypeSignature;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
Expand All @@ -67,8 +64,8 @@ public static Iterable<List<Object>> fixData(List<Column> columns, Iterable<List
return null;
}
requireNonNull(columns, "columns is null");
List<TypeSignature> signatures = columns.stream()
.map(column -> parseTypeSignature(column.getType()))
List<ClientTypeSignature> signatures = columns.stream()
.map(Column::getTypeSignature)
.collect(toList());
ImmutableList.Builder<List<Object>> rows = ImmutableList.builder();
for (List<Object> row : data) {
Expand All @@ -85,45 +82,45 @@ public static Iterable<List<Object>> fixData(List<Column> columns, Iterable<List
/**
* Force values coming from Jackson to have the expected object type.
*/
private static Object fixValue(TypeSignature signature, Object value)
private static Object fixValue(ClientTypeSignature signature, Object value)
{
if (value == null) {
return null;
}

if (signature.getBase().equals(ARRAY)) {
if (signature.getRawType().equals(ARRAY)) {
List<Object> fixedValue = new ArrayList<>();
for (Object object : List.class.cast(value)) {
fixedValue.add(fixValue(signature.getTypeParametersAsTypeSignatures().get(0), object));
fixedValue.add(fixValue(signature.getArgumentsAsTypeSignatures().get(0), object));
}
return fixedValue;
}
if (signature.getBase().equals(MAP)) {
TypeSignature keySignature = signature.getTypeParametersAsTypeSignatures().get(0);
TypeSignature valueSignature = signature.getTypeParametersAsTypeSignatures().get(1);
if (signature.getRawType().equals(MAP)) {
ClientTypeSignature keySignature = signature.getArgumentsAsTypeSignatures().get(0);
ClientTypeSignature valueSignature = signature.getArgumentsAsTypeSignatures().get(1);
Map<Object, Object> fixedValue = new HashMap<>();
for (Map.Entry<?, ?> entry : (Set<Map.Entry<?, ?>>) Map.class.cast(value).entrySet()) {
fixedValue.put(fixValue(keySignature, entry.getKey()), fixValue(valueSignature, entry.getValue()));
}
return fixedValue;
}
if (signature.getBase().equals(ROW)) {
if (signature.getRawType().equals(ROW)) {
Map<String, Object> fixedValue = new LinkedHashMap<>();
List<Object> listValue = List.class.cast(value);
checkArgument(listValue.size() == signature.getParameters().size(), "Mismatched data values and row type");
checkArgument(listValue.size() == signature.getArguments().size(), "Mismatched data values and row type");
for (int i = 0; i < listValue.size(); i++) {
TypeSignatureParameter parameter = signature.getParameters().get(i);
ClientTypeSignatureParameter parameter = signature.getArguments().get(i);
checkArgument(
parameter.getKind() == ParameterKind.NAMED_TYPE,
"Unexpected parameter [%s] for row type",
parameter);
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
String key = namedTypeSignature.getName().orElse("field" + i);
fixedValue.put(key, fixValue(namedTypeSignature.getTypeSignature(), listValue.get(i)));
fixedValue.put(key, fixValue(new ClientTypeSignature(namedTypeSignature.getTypeSignature()), listValue.get(i)));
}
return fixedValue;
}
switch (signature.getBase()) {
switch (signature.getRawType()) {
case BIGINT:
if (value instanceof String) {
return Long.parseLong((String) value);
Expand Down
30 changes: 15 additions & 15 deletions presto-jdbc/src/main/java/io/prestosql/jdbc/ColumnInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package io.prestosql.jdbc;

import com.google.common.collect.ImmutableList;
import io.prestosql.spi.type.TypeSignature;
import io.prestosql.spi.type.TypeSignatureParameter;
import io.prestosql.client.ClientTypeSignature;
import io.prestosql.client.ClientTypeSignatureParameter;

import java.sql.Types;
import java.util.List;
Expand All @@ -35,7 +35,7 @@ class ColumnInfo

private final int columnType;
private final List<Integer> columnParameterTypes;
private final TypeSignature columnTypeSignature;
private final ClientTypeSignature columnTypeSignature;
private final Nullable nullable;
private final boolean currency;
private final boolean signed;
Expand All @@ -56,7 +56,7 @@ public enum Nullable
public ColumnInfo(
int columnType,
List<Integer> columnParameterTypes,
TypeSignature columnTypeSignature,
ClientTypeSignature columnTypeSignature,
Nullable nullable,
boolean currency,
boolean signed,
Expand Down Expand Up @@ -85,11 +85,11 @@ public ColumnInfo(
this.catalogName = requireNonNull(catalogName, "catalogName is null");
}

public static void setTypeInfo(Builder builder, TypeSignature type)
public static void setTypeInfo(Builder builder, ClientTypeSignature type)
{
builder.setColumnType(getType(type));
ImmutableList.Builder<Integer> parameterTypes = ImmutableList.builder();
for (TypeSignatureParameter parameter : type.getParameters()) {
for (ClientTypeSignatureParameter parameter : type.getArguments()) {
parameterTypes.add(getType(parameter));
}
builder.setColumnParameterTypes(parameterTypes.build());
Expand Down Expand Up @@ -182,14 +182,14 @@ public static void setTypeInfo(Builder builder, TypeSignature type)
break;
case "decimal":
builder.setSigned(true);
builder.setColumnDisplaySize(type.getParameters().get(0).getLongLiteral().intValue() + 2); // dot and sign
builder.setPrecision(type.getParameters().get(0).getLongLiteral().intValue());
builder.setScale(type.getParameters().get(1).getLongLiteral().intValue());
builder.setColumnDisplaySize(type.getArguments().get(0).getLongLiteral().intValue() + 2); // dot and sign
builder.setPrecision(type.getArguments().get(0).getLongLiteral().intValue());
builder.setScale(type.getArguments().get(1).getLongLiteral().intValue());
break;
}
}

private static int getType(TypeSignatureParameter typeParameter)
private static int getType(ClientTypeSignatureParameter typeParameter)
{
switch (typeParameter.getKind()) {
case TYPE:
Expand All @@ -199,9 +199,9 @@ private static int getType(TypeSignatureParameter typeParameter)
}
}

private static int getType(TypeSignature type)
private static int getType(ClientTypeSignature type)
{
switch (type.getBase()) {
switch (type.getRawType()) {
case "array":
return Types.ARRAY;
case "boolean":
Expand Down Expand Up @@ -258,7 +258,7 @@ public String getColumnTypeName()
return columnTypeSignature.toString();
}

public TypeSignature getColumnTypeSignature()
public ClientTypeSignature getColumnTypeSignature()
{
return columnTypeSignature;
}
Expand Down Expand Up @@ -322,7 +322,7 @@ static class Builder
{
private int columnType;
private List<Integer> columnParameterTypes;
private TypeSignature columnTypeSignature;
private ClientTypeSignature columnTypeSignature;
private Nullable nullable;
private boolean currency;
private boolean signed;
Expand All @@ -346,7 +346,7 @@ public void setColumnParameterTypes(List<Integer> columnParameterTypes)
this.columnParameterTypes = ImmutableList.copyOf(requireNonNull(columnParameterTypes, "columnParameterTypes is null"));
}

public Builder setColumnTypeSignature(TypeSignature columnTypeSignature)
public Builder setColumnTypeSignature(ClientTypeSignature columnTypeSignature)
{
this.columnTypeSignature = columnTypeSignature;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import static com.google.common.collect.Iterators.concat;
import static com.google.common.collect.Iterators.transform;
import static io.prestosql.jdbc.ColumnInfo.setTypeInfo;
import static io.prestosql.spi.type.TypeSignature.parseTypeSignature;
import static java.lang.String.format;
import static java.math.BigDecimal.ROUND_HALF_UP;
import static java.util.Locale.ENGLISH;
Expand Down Expand Up @@ -1125,7 +1124,7 @@ public Array getArray(int columnIndex)
}

ColumnInfo columnInfo = columnInfo(columnIndex);
String elementTypeName = getOnlyElement(columnInfo.getColumnTypeSignature().getParameters()).toString();
String elementTypeName = getOnlyElement(columnInfo.getColumnTypeSignature().getArguments()).toString();
int elementType = getOnlyElement(columnInfo.getColumnParameterTypes());
return new PrestoArray(elementTypeName, elementType, (List<?>) value);
}
Expand Down Expand Up @@ -1862,10 +1861,10 @@ private static List<ColumnInfo> getColumnInfo(List<Column> columns)
.setTableName("") // TODO
.setColumnLabel(column.getName())
.setColumnName(column.getName()) // TODO
.setColumnTypeSignature(parseTypeSignature(column.getType().toUpperCase(ENGLISH)))
.setColumnTypeSignature(column.getTypeSignature())
.setNullable(Nullable.UNKNOWN)
.setCurrency(false);
setTypeInfo(builder, parseTypeSignature(column.getType()));
setTypeInfo(builder, column.getTypeSignature());
list.add(builder.build());
}
return list.build();
Expand Down

0 comments on commit cbbf251

Please sign in to comment.