Skip to content

Commit 20f6da2

Browse files
authored
remove sql_type_code remove usage (#3651)
This PR is a follow-up PR in the series for deprecating using sql_type_codes in JDBC, and instead using "richer" Relational types. The support for relational types were introduced some time bakc in #3510. This PR shifts to relational type usage, away from sql_type usage, to make way for retiring the latter field completely from the protobuf. After this, we could remove the field in 2 steps: 1. stop setting jdbcSqlType fields in JDBC proto and mark them deprecated. 2. remove from protobuf
1 parent f0bc0f2 commit 20f6da2

File tree

9 files changed

+353
-188
lines changed

9 files changed

+353
-188
lines changed

fdb-relational-grpc/src/main/java/com/apple/foundationdb/relational/jdbc/RelationalArrayFacade.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import com.google.common.base.Suppliers;
4545

4646
import javax.annotation.Nonnull;
47-
import javax.annotation.Nullable;
4847
import java.sql.SQLException;
4948
import java.sql.SQLFeatureNotSupportedException;
5049
import java.sql.Types;
@@ -78,10 +77,8 @@ class RelationalArrayFacade implements RelationalArray {
7877
this.delegate = array;
7978
}
8079

81-
@Nullable
8280
private DataType.ArrayType computeType() {
83-
return delegateMetadata.getType() == Type.UNKNOWN ? null :
84-
DataType.ArrayType.from(RelationalStructFacade.RelationalStructFacadeMetaData.getDataType(delegateMetadata.getType(), delegateMetadata, delegateMetadata.getNullable()));
81+
return DataType.ArrayType.from(TypeConversion.getDataType(delegateMetadata.getType(), delegateMetadata, delegateMetadata.getNullable()));
8582
}
8683

8784
/**
@@ -153,12 +150,12 @@ public Object getArray(long oneBasedIndex, int askedForCount) throws SQLExceptio
153150
int count = getCount(askedForCount, this.delegate.getElementCount(), index);
154151
final var array = new Object[count];
155152
int j = 0;
156-
final var componentType = this.delegateMetadata.getJavaSqlTypesCode();
153+
final var componentType = this.delegateMetadata.getType();
157154
for (int i = index; i < count; i++) {
158-
if (componentType == Types.STRUCT) {
155+
if (componentType == Type.STRUCT) {
159156
array[j++] = new RelationalStructFacade(delegateMetadata.getStructMetadata(), delegate.getElement(i).getStruct());
160157
} else {
161-
Assert.failUnchecked(ErrorCode.UNKNOWN_TYPE, "Type not supported: " + SqlTypeNamesSupport.getSqlTypeName(componentType));
158+
Assert.failUnchecked(ErrorCode.UNKNOWN_TYPE, "Type not supported: " + componentType.name());
162159
}
163160
}
164161
return array;
@@ -185,9 +182,9 @@ public RelationalResultSet getResultSet(long oneBasedIndex, int askedForCount) t
185182
final var componentType = this.delegateMetadata.getType();
186183
final var componentSqlType = this.delegateMetadata.getJavaSqlTypesCode();
187184
final var componentColumnBuilder = ColumnMetadata.newBuilder().setName("VALUE").setType(componentType).setJavaSqlTypesCode(componentSqlType);
188-
if (componentSqlType == Types.ARRAY) {
185+
if (componentType == Type.ARRAY) {
189186
componentColumnBuilder.setArrayMetadata(this.delegateMetadata.getArrayMetadata());
190-
} else if (componentSqlType == Types.STRUCT) {
187+
} else if (componentType == Type.STRUCT) {
191188
componentColumnBuilder.setStructMetadata(this.delegateMetadata.getStructMetadata());
192189
}
193190
resultSetBuilder.setMetadata(ResultSetMetadata.newBuilder().setColumnMetadata(ListColumnMetadata.newBuilder()
@@ -197,9 +194,9 @@ public RelationalResultSet getResultSet(long oneBasedIndex, int askedForCount) t
197194
final var listColumnBuilder = ListColumn.newBuilder();
198195
listColumnBuilder.addColumn(Column.newBuilder().setInteger(i + 1).build());
199196
final var valueColumnBuilder = Column.newBuilder();
200-
if (componentSqlType == Types.STRUCT) {
197+
if (componentType == Type.STRUCT) {
201198
valueColumnBuilder.setStruct(delegate.getElement(i).getStruct());
202-
} else if (componentSqlType == Types.INTEGER) {
199+
} else if (componentType == Type.INTEGER) {
203200
valueColumnBuilder.setInteger(delegate.getElement(i).getInteger());
204201
} else if (componentSqlType == Types.VARCHAR) {
205202
valueColumnBuilder.setString(delegate.getElement(i).getString());
@@ -208,7 +205,7 @@ public RelationalResultSet getResultSet(long oneBasedIndex, int askedForCount) t
208205
} else if (componentSqlType == Types.BOOLEAN) {
209206
valueColumnBuilder.setBoolean(delegate.getElement(i).getBoolean());
210207
} else {
211-
Assert.failUnchecked(ErrorCode.UNKNOWN_TYPE, "Type not supported: " + SqlTypeNamesSupport.getSqlTypeName(componentSqlType));
208+
Assert.failUnchecked(ErrorCode.UNKNOWN_TYPE, "Type not supported: " + componentType.name());
212209
}
213210
resultSetBuilder.addRow(Struct.newBuilder()
214211
.setColumns(listColumnBuilder
@@ -287,7 +284,7 @@ private void initOrCheckMetadata(ListColumnMetadata innerMetadata) {
287284
builder.setStructMetadata(innerMetadata);
288285
metadata = builder.build();
289286
} else {
290-
Assert.thatUnchecked(metadata.getJavaSqlTypesCode() == Types.STRUCT, ErrorCode.DATATYPE_MISMATCH, "dataType mismatch!");
287+
Assert.thatUnchecked(metadata.getType() == Type.ARRAY, ErrorCode.DATATYPE_MISMATCH, "dataType mismatch!");
291288
}
292289
}
293290
}

fdb-relational-grpc/src/main/java/com/apple/foundationdb/relational/jdbc/RelationalResultSetMetaDataFacade.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.apple.foundationdb.relational.jdbc;
2222

2323
import com.apple.foundationdb.relational.api.ArrayMetaData;
24+
import com.apple.foundationdb.relational.api.RelationalStructMetaData;
2425
import com.apple.foundationdb.relational.api.StructMetaData;
2526
import com.apple.foundationdb.relational.api.RelationalResultSetMetaData;
2627
import com.apple.foundationdb.relational.api.exceptions.ErrorCode;
@@ -56,9 +57,10 @@ public DataType.StructType getRelationalDataType() throws SQLException {
5657
}
5758

5859
@Override
59-
public StructMetaData getStructMetaData(int oneBasedColumn) throws SQLException {
60-
return new RelationalStructFacade.RelationalStructFacadeMetaData(
61-
this.delegate.getColumnMetadata().getColumnMetadata(PositionalIndex.toProtobuf(oneBasedColumn)).getStructMetadata());
60+
public StructMetaData getStructMetaData(int oneBasedColumn) {
61+
return RelationalStructMetaData.of(TypeConversion.getStructDataType(
62+
this.delegate.getColumnMetadata().getColumnMetadata(
63+
PositionalIndex.toProtobuf(oneBasedColumn)).getStructMetadata().getColumnMetadataList(), false));
6264
}
6365

6466
@Override
@@ -85,8 +87,8 @@ public String getColumnLabel(int column) throws SQLException {
8587

8688
@Override
8789
public int getColumnType(int oneBasedColumn) throws SQLException {
88-
return this.delegate.getColumnMetadata()
89-
.getColumnMetadata(PositionalIndex.toProtobuf(oneBasedColumn)).getJavaSqlTypesCode();
90+
return TypeConversion.toSqlType(this.delegate.getColumnMetadata()
91+
.getColumnMetadata(PositionalIndex.toProtobuf(oneBasedColumn)).getType());
9092
}
9193

9294
@Override

fdb-relational-grpc/src/main/java/com/apple/foundationdb/relational/jdbc/RelationalStructFacade.java

Lines changed: 10 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@
2020

2121
package com.apple.foundationdb.relational.jdbc;
2222

23-
import com.apple.foundationdb.relational.api.ArrayMetaData;
2423
import com.apple.foundationdb.relational.api.RelationalArray;
2524
import com.apple.foundationdb.relational.api.RelationalStruct;
2625
import com.apple.foundationdb.relational.api.RelationalStructBuilder;
2726
import com.apple.foundationdb.relational.api.RelationalStructMetaData;
2827
import com.apple.foundationdb.relational.api.StructMetaData;
2928
import com.apple.foundationdb.relational.api.exceptions.ErrorCode;
30-
import com.apple.foundationdb.relational.api.exceptions.RelationalException;
3129
import com.apple.foundationdb.relational.api.metadata.DataType;
3230
import com.apple.foundationdb.relational.jdbc.grpc.v1.column.Column;
3331
import com.apple.foundationdb.relational.jdbc.grpc.v1.column.ColumnMetadata;
34-
import com.apple.foundationdb.relational.jdbc.grpc.v1.column.EnumMetadata;
3532
import com.apple.foundationdb.relational.jdbc.grpc.v1.column.ListColumn;
3633
import com.apple.foundationdb.relational.jdbc.grpc.v1.column.ListColumnMetadata;
3734
import com.apple.foundationdb.relational.jdbc.grpc.v1.column.Struct;
@@ -54,6 +51,8 @@
5451
import java.util.UUID;
5552
import java.util.function.Supplier;
5653

54+
import static com.apple.foundationdb.relational.jdbc.TypeConversion.getStructDataType;
55+
5756
/**
5857
* Facade over grpc protobuf objects that offers a {@link RelationalStruct} view.
5958
* Used by jdbc client but also serializable (protobuf) so can be passed over
@@ -69,7 +68,6 @@ class RelationalStructFacade implements RelationalStruct {
6968
/**
7069
* A StructMetaData facade over {@link #delegateMetadata}.
7170
*/
72-
private final StructMetaData structMetaData;
7371
private final Supplier<DataType.StructType> type;
7472

7573
/**
@@ -87,10 +85,9 @@ class RelationalStructFacade implements RelationalStruct {
8785
private boolean wasNull;
8886

8987
RelationalStructFacade(ListColumnMetadata delegateMetadata, Struct delegate) {
90-
type = Suppliers.memoize(() -> TypeConversion.getStructDataType(delegateMetadata.getColumnMetadataList(), false));
88+
type = Suppliers.memoize(() -> getStructDataType(delegateMetadata.getColumnMetadataList(), false));
9189
this.delegate = delegate;
9290
this.delegateMetadata = delegateMetadata;
93-
this.structMetaData = new RelationalStructFacadeMetaData(delegateMetadata);
9491
this.wasNull = false;
9592
}
9693

@@ -115,7 +112,7 @@ ListColumnMetadata getDelegateMetadata() {
115112
*/
116113
@Override
117114
public StructMetaData getMetaData() {
118-
return type.get() != null ? RelationalStructMetaData.of(type.get()) : structMetaData;
115+
return RelationalStructMetaData.of(type.get());
119116
}
120117

121118
@Override
@@ -430,7 +427,7 @@ int getZeroBasedOffsetOrThrow(String fieldName) throws SQLException {
430427
}
431428

432429
@Override
433-
public RelationalStructBuilder addBoolean(String fieldName, boolean b) throws SQLException {
430+
public RelationalStructBuilder addBoolean(String fieldName, boolean b) {
434431
// Add the metadata and get offset at where to insert data.
435432
int offset = addMetadata(ColumnMetadata.newBuilder()
436433
.setName(fieldName).setJavaSqlTypesCode(Types.BOOLEAN).setType(Type.BOOLEAN).build());
@@ -440,7 +437,7 @@ public RelationalStructBuilder addBoolean(String fieldName, boolean b) throws SQ
440437
}
441438

442439
@Override
443-
public RelationalStructBuilder addLong(String fieldName, long l) throws SQLException {
440+
public RelationalStructBuilder addLong(String fieldName, long l) {
444441
int offset = addMetadata(ColumnMetadata.newBuilder()
445442
.setName(fieldName).setJavaSqlTypesCode(Types.BIGINT).setType(Type.LONG).build());
446443
this.listColumnBuilder.addColumn(offset, Column.newBuilder().setLong(l).build());
@@ -453,15 +450,15 @@ public RelationalStructBuilder addFloat(String fieldName, float f) throws SQLExc
453450
}
454451

455452
@Override
456-
public RelationalStructBuilder addDouble(String fieldName, double d) throws SQLException {
453+
public RelationalStructBuilder addDouble(String fieldName, double d) {
457454
int offset = addMetadata(ColumnMetadata.newBuilder()
458455
.setName(fieldName).setJavaSqlTypesCode(Types.DOUBLE).setType(Type.DOUBLE).build());
459456
this.listColumnBuilder.addColumn(offset, Column.newBuilder().setDouble(d).build());
460457
return this;
461458
}
462459

463460
@Override
464-
public RelationalStructBuilder addBytes(String fieldName, byte[] bytes) throws SQLException {
461+
public RelationalStructBuilder addBytes(String fieldName, byte[] bytes) {
465462
int offset = addMetadata(ColumnMetadata.newBuilder()
466463
.setName(fieldName).setJavaSqlTypesCode(Types.BINARY).setType(Type.BYTES).build());
467464
this.listColumnBuilder.addColumn(offset, Column.newBuilder().setBinary(ByteString.copyFrom(bytes)).build());
@@ -470,7 +467,7 @@ public RelationalStructBuilder addBytes(String fieldName, byte[] bytes) throws S
470467

471468
@Override
472469
@SpotBugsSuppressWarnings("NP")
473-
public RelationalStructBuilder addString(String fieldName, @Nullable String s) throws SQLException {
470+
public RelationalStructBuilder addString(String fieldName, @Nullable String s) {
474471
int offset = addMetadata(ColumnMetadata.newBuilder()
475472
.setName(fieldName).setJavaSqlTypesCode(Types.VARCHAR).setType(Type.STRING).build());
476473
// TODO: setString requires a non-null string, but this method takes a nullable string
@@ -503,7 +500,7 @@ public RelationalStructBuilder addStruct(String fieldName, @Nonnull RelationalSt
503500
// Insert the data portion of RelationalStruct here.
504501
RelationalStructFacade relationalStructFacade = struct.unwrap(RelationalStructFacade.class);
505502
int offset = addMetadata(ColumnMetadata.newBuilder().setName(fieldName)
506-
.setJavaSqlTypesCode(Types.STRUCT).setType(Type.STRUCT).setStructMetadata(relationalStructFacade.delegateMetadata).build());
503+
.setJavaSqlTypesCode(Types.STRUCT).setType(Type.STRUCT).setStructMetadata(relationalStructFacade.getDelegateMetadata()).build());
507504
this.listColumnBuilder
508505
.addColumn(offset, Column.newBuilder().setStruct(relationalStructFacade.delegate).build());
509506
return this;
@@ -540,152 +537,4 @@ public RelationalStruct build() {
540537
return new RelationalStructFacade(columnListMetadataBuilder.build(), struct);
541538
}
542539
}
543-
544-
/**
545-
* Facade over protobuf column metadata to present a StructMetaData view.
546-
*/
547-
static final class RelationalStructFacadeMetaData implements StructMetaData {
548-
private final ListColumnMetadata metadata;
549-
private final Supplier<DataType.StructType> type;
550-
551-
RelationalStructFacadeMetaData(ListColumnMetadata metadata) {
552-
this.metadata = metadata;
553-
this.type = Suppliers.memoize(this::computeType);
554-
}
555-
556-
private DataType.StructType computeType() {
557-
return getStructDataType(metadata.getColumnMetadataList(), false);
558-
}
559-
560-
private static DataType.EnumType getEnumDataType(@Nonnull EnumMetadata enumMetadata, boolean nullable) {
561-
final var enumValues = new ArrayList<DataType.EnumType.EnumValue>();
562-
int i = 1;
563-
for (var value: enumMetadata.getValuesList()) {
564-
enumValues.add(DataType.EnumType.EnumValue.of(value, i++));
565-
}
566-
return DataType.EnumType.from(enumMetadata.getName(), enumValues, nullable);
567-
}
568-
569-
private static DataType.StructType getStructDataType(@Nonnull List<ColumnMetadata> columnMetadataList, boolean nullable) {
570-
final var fields = new ArrayList<DataType.StructType.Field>();
571-
for (int i = 0; i < columnMetadataList.size(); i++) {
572-
final var columnMetadata = columnMetadataList.get(i);
573-
fields.add(DataType.StructType.Field.from(columnMetadata.getName(), getDataType(columnMetadata.getType(), columnMetadata, columnMetadata.getNullable()), i));
574-
}
575-
return DataType.StructType.from("ANONYMOUS_STRUCT", fields, nullable);
576-
}
577-
578-
static DataType getDataType(@Nonnull Type type, @Nonnull ColumnMetadata columnMetadata, boolean nullable) {
579-
switch (type) {
580-
case LONG:
581-
return nullable ? DataType.Primitives.NULLABLE_LONG.type() : DataType.Primitives.LONG.type();
582-
case INTEGER:
583-
return nullable ? DataType.Primitives.NULLABLE_INTEGER.type() : DataType.Primitives.INTEGER.type();
584-
case DOUBLE:
585-
return nullable ? DataType.Primitives.NULLABLE_DOUBLE.type() : DataType.Primitives.DOUBLE.type();
586-
case FLOAT:
587-
return nullable ? DataType.Primitives.NULLABLE_FLOAT.type() : DataType.Primitives.FLOAT.type();
588-
case BOOLEAN:
589-
return nullable ? DataType.Primitives.NULLABLE_BOOLEAN.type() : DataType.Primitives.BOOLEAN.type();
590-
case BYTES:
591-
return nullable ? DataType.Primitives.NULLABLE_BYTES.type() : DataType.Primitives.BYTES.type();
592-
case UUID:
593-
return nullable ? DataType.Primitives.NULLABLE_UUID.type() : DataType.Primitives.UUID.type();
594-
case STRING:
595-
return nullable ? DataType.Primitives.NULLABLE_STRING.type() : DataType.Primitives.STRING.type();
596-
case VERSION:
597-
return nullable ? DataType.Primitives.NULLABLE_VERSION.type() : DataType.Primitives.VERSION.type();
598-
case STRUCT:
599-
return getStructDataType(columnMetadata.getStructMetadata().getColumnMetadataList(), nullable);
600-
case ENUM:
601-
return getEnumDataType(columnMetadata.getEnumMetadata(), nullable);
602-
case ARRAY:
603-
final var arrayMetadata = columnMetadata.getArrayMetadata();
604-
return DataType.ArrayType.from(getDataType(arrayMetadata.getType(), arrayMetadata, arrayMetadata.getNullable()), nullable);
605-
default:
606-
throw new RelationalException("Not implemeneted: " + type.name(), ErrorCode.INTERNAL_ERROR).toUncheckedWrappedException();
607-
}
608-
}
609-
610-
@Override
611-
public int getColumnCount() throws SQLException {
612-
return metadata.getColumnMetadataCount();
613-
}
614-
615-
@Override
616-
public int isNullable(int oneBasedColumn) throws SQLException {
617-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
618-
}
619-
620-
@Override
621-
public String getTypeName() throws SQLException {
622-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
623-
}
624-
625-
@Override
626-
public String getColumnLabel(int oneBasedColumn) throws SQLException {
627-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
628-
}
629-
630-
@Override
631-
public String getColumnName(int oneBasedColumn) throws SQLException {
632-
return metadata.getColumnMetadata(PositionalIndex.toProtobuf(oneBasedColumn)).getName();
633-
}
634-
635-
@Override
636-
public String getSchemaName(int oneBasedColumn) throws SQLException {
637-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
638-
}
639-
640-
@Override
641-
public String getTableName(int oneBasedColumn) throws SQLException {
642-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
643-
}
644-
645-
@Override
646-
public String getCatalogName(int oneBasedColumn) throws SQLException {
647-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
648-
}
649-
650-
@Override
651-
public int getColumnType(int oneBasedColumn) throws SQLException {
652-
return metadata.getColumnMetadata(PositionalIndex.toProtobuf(oneBasedColumn)).getJavaSqlTypesCode();
653-
}
654-
655-
@Override
656-
public String getColumnTypeName(int oneBasedColumn) throws SQLException {
657-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
658-
}
659-
660-
@Override
661-
public StructMetaData getStructMetaData(int oneBasedColumn) throws SQLException {
662-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
663-
}
664-
665-
@Override
666-
public ArrayMetaData getArrayMetaData(int oneBasedColumn) throws SQLException {
667-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
668-
}
669-
670-
@Override
671-
public int getLeadingPhantomColumnCount() {
672-
return -1000;
673-
}
674-
675-
@Nonnull
676-
@Override
677-
public DataType.StructType getRelationalDataType() {
678-
return type.get();
679-
}
680-
681-
@Override
682-
public <T> T unwrap(Class<T> iface) throws SQLException {
683-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
684-
}
685-
686-
@Override
687-
public boolean isWrapperFor(Class<?> iface) throws SQLException {
688-
throw new SQLException("Not implemented", ErrorCode.UNSUPPORTED_OPERATION.getErrorCode());
689-
}
690-
}
691540
}

0 commit comments

Comments
 (0)