-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Core: add variant type support #11831
base: main
Are you sure you want to change the base?
Conversation
5a18496
to
68d690e
Compare
cc @rdblue, @RussellSpitzer, @flyrain and @JonasJ-ap. This is to add the changes in core to support variant type. |
@@ -39,7 +42,7 @@ class Identity<T> implements Transform<T, T> { | |||
@Deprecated | |||
public static <I> Identity<I> get(Type type) { | |||
Preconditions.checkArgument( | |||
type.typeId() != Type.TypeID.VARIANT, "Unsupported type for identity: %s", type); | |||
!UNSUPPORTED_TYPES.contains(type), "Unsupported type for identity: %s", type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this? It works just fine right? We can alter this when we add another type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It works fine. I thought you mentioned that you wanted to make such change in a previous PR. :)
@@ -709,6 +709,10 @@ public T map(Types.MapType map, Supplier<T> keyResult, Supplier<T> valueResult) | |||
return null; | |||
} | |||
|
|||
public T variant() { | |||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be safer to throw UnsupportedOperationException
here instead? I think that's what we have done for expression visitors when we add new cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see others like map()/list() all return null here.
@@ -132,6 +133,8 @@ static void toJson(Type.PrimitiveType primitive, JsonGenerator generator) throws | |||
static void toJson(Type type, JsonGenerator generator) throws IOException { | |||
if (type.isPrimitiveType()) { | |||
toJson(type.asPrimitiveType(), generator); | |||
} else if (type.isVariantType()) { | |||
generator.writeString(type.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update TestSchemaParser
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we don't have TestSchemaParser
. I'm trying to use TestMetadataUpdateParser.java indirectly test the change here. Let me know if we should create TestSchemaParser
.
@@ -42,6 +42,7 @@ private SchemaParser() {} | |||
private static final String STRUCT = "struct"; | |||
private static final String LIST = "list"; | |||
private static final String MAP = "map"; | |||
private static final String VARIANT = "variant"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types don't typically go here. Maybe add this to fromPrimitiveString
. It fits better there, even if it isn't a primitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of that. But PrimitiveHolder calls fromPrimitiveString() which is used in PrimitiveType class. Not sure if it would cause an issue in serialization.
/** Replacement for primitive types in Java Serialization. */
class PrimitiveHolder implements Serializable {
private String typeAsString = null;
...
Object readResolve() throws ObjectStreamException {
return Types.fromPrimitiveString(typeAsString);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And also fromPrimitiveString() is returning PrimitiveType. I think it's better not to mix variant with primitive. I create typeFromTypeString(String typeString) to handle both cases. Let me know how that looks.
@@ -166,6 +169,10 @@ public static String toJson(Schema schema, boolean pretty) { | |||
|
|||
private static Type typeFromJson(JsonNode json) { | |||
if (json.isTextual()) { | |||
if (VARIANT.equalsIgnoreCase(json.asText())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think fromPrimitiveString
should handle this.
@@ -52,6 +56,15 @@ public class TestMetadataUpdateParser { | |||
Types.NestedField.required(1, "id", Types.IntegerType.get()), | |||
Types.NestedField.optional(2, "data", Types.StringType.get())); | |||
|
|||
private static final Schema ID_VARIANTDATA_SCHEMA = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the right place for a Variant test. What are you trying to test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no TestSchemaParser. I'm trying to test SchemaParser indirectly. Let me know if we should add TestSchemaParser or we don't have that because it's tested indirectly.
@@ -150,4 +152,16 @@ public void projectWithMapSchemaChanged() { | |||
.as("Result of buildAvroProjection is missing some IDs") | |||
.isFalse(); | |||
} | |||
|
|||
@Test | |||
public void testVariantConversion() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the projection test suite the right place for conversion tests? Isn't there a conversion test suite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. Let me move there.
68d690e
to
04958ca
Compare
04958ca
to
b276d3f
Compare
b276d3f
to
fe6038a
Compare
This is to add some required changes in API and core module for Variant support, including:
Part of: #10392