-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: add schema id and schemas to table metadata #2096
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
Changes from 4 commits
f3bb110
18ef10e
c0bb4f3
de3ca4d
113b2e3
c06f1ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,6 +157,21 @@ public static Schema assignFreshIds(Schema schema, NextID nextId) { | |
| .fields()); | ||
| } | ||
|
|
||
| /** | ||
| * Assigns fresh ids from the {@link NextID nextId function} for all fields in a schema. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: function should be outside the bracket
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm actually I think either way may make sense? We either link |
||
| * | ||
| * @param schemaId an ID assigned to this schema | ||
| * @param schema a schema | ||
| * @param nextId an id assignment function | ||
| * @return a structurally identical schema with new ids assigned by the nextId function | ||
| */ | ||
| public static Schema assignFreshIds(int schemaId, Schema schema, NextID nextId) { | ||
| return new Schema(schemaId, TypeUtil | ||
| .visit(schema.asStruct(), new AssignFreshIds(nextId)) | ||
| .asNestedType() | ||
| .fields()); | ||
| } | ||
|
|
||
| /** | ||
| * Assigns ids to match a given schema, and fresh ids from the {@link NextID nextId function} for all other fields. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ public class SchemaParser { | |
| private SchemaParser() { | ||
| } | ||
|
|
||
| private static final String SCHEMA_ID = "schema-id"; | ||
| private static final String TYPE = "type"; | ||
| private static final String STRUCT = "struct"; | ||
| private static final String LIST = "list"; | ||
|
|
@@ -58,9 +59,17 @@ private SchemaParser() { | |
| private static final String VALUE_REQUIRED = "value-required"; | ||
|
|
||
| static void toJson(Types.StructType struct, JsonGenerator generator) throws IOException { | ||
| toJson(struct, null, generator); | ||
| } | ||
|
|
||
| static void toJson(Types.StructType struct, Integer schemaId, JsonGenerator generator) throws IOException { | ||
rdblue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| generator.writeStartObject(); | ||
|
|
||
| generator.writeStringField(TYPE, STRUCT); | ||
| if (schemaId != null) { | ||
| generator.writeNumberField(SCHEMA_ID, schemaId); | ||
| } | ||
|
|
||
| generator.writeArrayFieldStart(FIELDS); | ||
| for (Types.NestedField field : struct.fields()) { | ||
| generator.writeStartObject(); | ||
|
|
@@ -134,6 +143,10 @@ static void toJson(Type type, JsonGenerator generator) throws IOException { | |
| } | ||
| } | ||
|
|
||
| public static void toJsonWithVersion(Schema schema, JsonGenerator generator) throws IOException { | ||
|
||
| toJson(schema.asStruct(), schema.schemaId(), generator); | ||
| } | ||
|
|
||
| public static void toJson(Schema schema, JsonGenerator generator) throws IOException { | ||
| toJson(schema.asStruct(), generator); | ||
| } | ||
|
|
@@ -253,4 +266,15 @@ public static Schema fromJson(String json) { | |
| } | ||
| }); | ||
| } | ||
|
|
||
| public static Schema fromJsonWithId(int schemaId, JsonNode json) { | ||
|
||
| Type type = typeFromJson(json); | ||
| Preconditions.checkArgument(type.isNestedType() && type.asNestedType().isStructType(), | ||
| "Cannot create schema, not a struct type: %s", type); | ||
| return new Schema(schemaId, type.asNestedType().asStructType().fields()); | ||
| } | ||
|
|
||
| public static Schema fromJsonWithId(JsonNode json) { | ||
| return fromJsonWithId(JsonUtil.getInt(SCHEMA_ID, json), json); | ||
| } | ||
| } | ||
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 we should add a constant for the default schema ID. That way we can reference it in other places when we fill in the default.