-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Introduce implementation for FileType #3608
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
base: master
Are you sure you want to change the base?
Changes from all commits
c5c92b3
54823a8
89b656d
2dd4091
08761a8
be266d3
c880902
f5685dd
3fcd28f
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 |
|---|---|---|
|
|
@@ -847,12 +847,115 @@ public THIS addFields(Type... types) { | |
| @Override | ||
| protected GroupType build(String name) { | ||
| if (newLogicalTypeSet) { | ||
| if (logicalTypeAnnotation instanceof LogicalTypeAnnotation.FileLogicalTypeAnnotation) { | ||
| validateFileTypeFields(name, fields); | ||
| } | ||
| return new GroupType(repetition, name, logicalTypeAnnotation, fields, id); | ||
| } else { | ||
| return new GroupType(repetition, name, getOriginalType(), fields, id); | ||
| } | ||
| } | ||
|
|
||
| private static void validateFileTypeFields(String name, List<Type> fields) { | ||
| boolean hasUri = false; | ||
| boolean hasOffset = false; | ||
| boolean hasSize = false; | ||
| boolean hasInline = false; | ||
| for (Type field : fields) { | ||
| String fieldName = field.getName(); | ||
| if (!LogicalTypeAnnotation.FileLogicalTypeAnnotation.FIELD_NAMES.contains(fieldName)) { | ||
| throw new IllegalArgumentException("FILE type group '" + name + "' contains unrecognized field '" | ||
| + fieldName + "'. Valid fields are: " | ||
| + String.join(", ", LogicalTypeAnnotation.FileLogicalTypeAnnotation.FIELD_NAMES)); | ||
| } | ||
| Preconditions.checkArgument( | ||
| field.isPrimitive() && field.getRepetition() == Type.Repetition.OPTIONAL, | ||
| "FILE type field '%s' must be an optional primitive in group '%s'", | ||
| fieldName, | ||
| name); | ||
| validateFileTypeFieldPhysicalType(name, field.asPrimitiveType()); | ||
| if (LogicalTypeAnnotation.FileLogicalTypeAnnotation.URI_FIELD.equals(fieldName)) { | ||
| hasUri = true; | ||
| } else if (LogicalTypeAnnotation.FileLogicalTypeAnnotation.OFFSET_FIELD.equals(fieldName)) { | ||
| hasOffset = true; | ||
| } else if (LogicalTypeAnnotation.FileLogicalTypeAnnotation.SIZE_FIELD.equals(fieldName)) { | ||
| hasSize = true; | ||
| } else if (LogicalTypeAnnotation.FileLogicalTypeAnnotation.INLINE_FIELD.equals(fieldName)) { | ||
| hasInline = true; | ||
| } | ||
| } | ||
| // A value resolves to bytes only via inline storage or an external reference, so a group must | ||
| // declare at least one of `inline` or `uri`. A group declaring neither — even if it declares | ||
| // `offset` or `size` — can never produce a resolvable value, so reject it at schema-build | ||
| // time. | ||
| Preconditions.checkArgument( | ||
| hasInline || hasUri, | ||
| "FILE type group '%s' must declare at least one of 'inline' or 'uri'; a value resolves to " | ||
| + "bytes only via inline storage or an external reference, so a group declaring " | ||
| + "neither can never produce a valid value", | ||
| name); | ||
| // `offset` locates a byte range within the external file identified by `uri`, so it is | ||
| // meaningless without `uri`. A group that declares `offset` but not `uri` can never produce a | ||
| // valid value, so reject it at schema-build time. | ||
| Preconditions.checkArgument( | ||
| !hasOffset || hasUri, | ||
| "FILE type group '%s' declares field 'offset' but not 'uri'; 'offset' locates a range " | ||
| + "within an external file and may only be set together with 'uri'", | ||
| name); | ||
|
Comment on lines
+900
to
+904
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. I don't think we can make this assertion at the schema layer. If a FILE group contains
|
||
| // The spec requires `size` to be set whenever `offset` is set. A group that declares | ||
| // `offset` but not `size` can never produce a valid value, so reject it at schema-build | ||
| // time. | ||
| Preconditions.checkArgument( | ||
| !hasOffset || hasSize, | ||
| "FILE type group '%s' declares field 'offset' but not 'size'; 'size' is required whenever 'offset' is set", | ||
|
Comment on lines
+908
to
+910
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. Same here, if all |
||
| name); | ||
| // The remaining spec rules are per-value constraints the schema builder cannot verify because | ||
| // it sees only which fields are declared, not their values in each row: `size` being set | ||
| // whenever `offset` is set, and `offset`/`size` being non-negative. Those are the | ||
| // responsibility of writers and consumers of FILE values. | ||
| } | ||
|
|
||
| /** | ||
| * Validates that a declared FILE field uses the physical type required by the spec: | ||
| * {@code uri}, {@code content_type}, and {@code checksum} are STRING (BINARY), {@code offset} | ||
| * and {@code size} are INT64, and {@code inline} is BYTE_ARRAY (BINARY). | ||
| */ | ||
| private static void validateFileTypeFieldPhysicalType(String name, PrimitiveType field) { | ||
| String fieldName = field.getName(); | ||
| PrimitiveType.PrimitiveTypeName physicalType = field.getPrimitiveTypeName(); | ||
| switch (fieldName) { | ||
| case LogicalTypeAnnotation.FileLogicalTypeAnnotation.URI_FIELD: | ||
| case LogicalTypeAnnotation.FileLogicalTypeAnnotation.CONTENT_TYPE_FIELD: | ||
| case LogicalTypeAnnotation.FileLogicalTypeAnnotation.CHECKSUM_FIELD: | ||
| Preconditions.checkArgument( | ||
| physicalType == PrimitiveType.PrimitiveTypeName.BINARY | ||
| && field.getLogicalTypeAnnotation() | ||
| instanceof LogicalTypeAnnotation.StringLogicalTypeAnnotation, | ||
| "FILE type field '%s' must be a STRING (BINARY annotated as STRING) in group '%s'", | ||
| fieldName, | ||
| name); | ||
| break; | ||
| case LogicalTypeAnnotation.FileLogicalTypeAnnotation.OFFSET_FIELD: | ||
| case LogicalTypeAnnotation.FileLogicalTypeAnnotation.SIZE_FIELD: | ||
| Preconditions.checkArgument( | ||
| physicalType == PrimitiveType.PrimitiveTypeName.INT64, | ||
| "FILE type field '%s' must be an INT64 in group '%s'", | ||
| fieldName, | ||
| name); | ||
| break; | ||
| case LogicalTypeAnnotation.FileLogicalTypeAnnotation.INLINE_FIELD: | ||
| Preconditions.checkArgument( | ||
| physicalType == PrimitiveType.PrimitiveTypeName.BINARY, | ||
| "FILE type field '%s' must be a BYTE_ARRAY (BINARY) in group '%s'", | ||
| fieldName, | ||
| name); | ||
| break; | ||
| default: | ||
| // Unreachable: field names are validated against FIELD_NAMES before this call. | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public MapBuilder<THIS> map(Type.Repetition repetition) { | ||
| return new MapBuilder<>(self()).repetition(repetition); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.