Skip to content

Commit

Permalink
[#2117] improvement(api): Type adds UNKNOWN column data type to handl…
Browse files Browse the repository at this point in the history
…e an unresolvable type from the catalog
  • Loading branch information
SteNicholas committed Feb 8, 2024
1 parent 5a9069e commit 42245d3
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ enum Name {
UNION,

/** The null type. A null type represents a value that is null. */
NULL
NULL,

/** The unknown type. An unknown type represents an unresolvable type. */
UNKNOWN
}

/** The base type of all primitive types. */
Expand Down
22 changes: 22 additions & 0 deletions api/src/main/java/com/datastrato/gravitino/rel/types/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ public String simpleString() {
}
}

/** The data type representing `UNKNOWN` values. */
public static class UnknownType implements Type {
private static final UnknownType INSTANCE = new UnknownType();

/** @return The singleton instance of {@link UnknownType}. */
public static UnknownType get() {
return INSTANCE;
}

private UnknownType() {}

@Override
public Name name() {
return Name.UNKNOWN;
}

@Override
public String simpleString() {
return "unknown";
}
}

/** The boolean type in Gravitino. */
public static class BooleanType extends Type.PrimitiveType {
private static final BooleanType INSTANCE = new BooleanType();
Expand Down
5 changes: 5 additions & 0 deletions api/src/test/java/com/datastrato/gravitino/rel/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public void testPrimitiveTypes() {
Assertions.assertSame(nullType, Types.NullType.get());
Assertions.assertEquals("null", nullType.simpleString());

Types.UnknownType unknownType = Types.UnknownType.get();
Assertions.assertEquals(Type.Name.UNKNOWN, unknownType.name());
Assertions.assertSame(unknownType, Types.UnknownType.get());
Assertions.assertEquals("unknown", unknownType.simpleString());

Types.ByteType byteType = Types.ByteType.get();
Assertions.assertEquals(Type.Name.BYTE, byteType.name());
Assertions.assertSame(byteType, Types.ByteType.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static Type convert(String hiveType) throws IllegalArgumentException {
* @return The equivalent Gravitino Type.
* @throws IllegalArgumentException if the Hive data type category is unknown or unsupported.
*/
private static Type toGravitinoType(TypeInfo hiveTypeInfo) throws IllegalArgumentException {
public static Type toGravitinoType(TypeInfo hiveTypeInfo) throws IllegalArgumentException {
switch (hiveTypeInfo.getCategory()) {
case PRIMITIVE:
switch (hiveTypeInfo.getTypeName()) {
Expand Down Expand Up @@ -98,8 +98,7 @@ private static Type toGravitinoType(TypeInfo hiveTypeInfo) throws IllegalArgumen
return Types.DecimalType.of(decimalTypeInfo.precision(), decimalTypeInfo.scale());
}

throw new IllegalArgumentException(
"Unknown Hive type: " + hiveTypeInfo.getQualifiedName());
return Types.UnknownType.get();
}
case LIST:
return Types.ListType.nullable(
Expand Down Expand Up @@ -128,8 +127,7 @@ private static Type toGravitinoType(TypeInfo hiveTypeInfo) throws IllegalArgumen
.map(FromHiveType::toGravitinoType)
.toArray(Type[]::new));
default:
throw new IllegalArgumentException(
"Unknown category of Hive type: " + hiveTypeInfo.getCategory());
return Types.UnknownType.get();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

import com.datastrato.gravitino.catalog.hive.converter.FromHiveType;
import com.datastrato.gravitino.catalog.hive.converter.ToHiveType;
import com.datastrato.gravitino.rel.types.Types;
import java.util.Arrays;
import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -69,11 +71,20 @@ public void testTypeConverter() {
Arrays.asList(
getPrimitiveTypeInfo(STRING_TYPE_NAME), getPrimitiveTypeInfo(INT_TYPE_NAME)))
.getTypeName());
Assertions.assertEquals(
Types.UnknownType.get(), FromHiveType.toGravitinoType(new UnknownTypeInfo()));
}

private void testConverter(String typeName) {
TypeInfo hiveType = getTypeInfoFromTypeString(typeName);
TypeInfo convertedType = ToHiveType.convert(FromHiveType.convert(hiveType.getTypeName()));
Assertions.assertEquals(hiveType, convertedType);
}

static class UnknownTypeInfo extends PrimitiveTypeInfo {
@Override
public String getTypeName() {
return Types.UnknownType.get().simpleString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Type toGravitinoType(JdbcTypeBean typeBean) {
case BINARY:
return Types.BinaryType.get();
default:
throw new IllegalArgumentException("Not a supported type: " + typeBean.getTypeName());
return Types.UnknownType.get();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public void testToGravitinoType() {
checkJdbcTypeToGravitinoType(Types.FixedCharType.of(20), CHAR, "20", null);
checkJdbcTypeToGravitinoType(Types.StringType.get(), TEXT, null, null);
checkJdbcTypeToGravitinoType(Types.BinaryType.get(), BINARY, null, null);
checkJdbcTypeToGravitinoType(
Types.UnknownType.get(), Types.UnknownType.get().simpleString(), null, null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Type toGravitinoType(JdbcTypeBean typeBean) {
case BYTEA:
return Types.BinaryType.get();
default:
throw new IllegalArgumentException("Not a supported type: " + typeBean);
return Types.UnknownType.get();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void testToGravitinoType() {
checkJdbcTypeToGravitinoType(Types.FixedCharType.of(20), BPCHAR, "20", null);
checkJdbcTypeToGravitinoType(Types.StringType.get(), TEXT, null, null);
checkJdbcTypeToGravitinoType(Types.BinaryType.get(), BYTEA, null, null);
checkJdbcTypeToGravitinoType(
Types.UnknownType.get(), Types.UnknownType.get().simpleString(), null, null);
}

@Test
Expand Down

0 comments on commit 42245d3

Please sign in to comment.