2727import org .apache .iceberg .relocated .com .google .common .base .Preconditions ;
2828import org .apache .iceberg .util .JsonUtil ;
2929
30- public class ContentFileParser {
30+ class ContentFileParser {
3131 private static final String SPEC_ID = "spec-id" ;
3232 private static final String CONTENT = "content" ;
3333 private static final String FILE_PATH = "file-path" ;
@@ -50,8 +50,6 @@ public class ContentFileParser {
5050 private static final String CONTENT_OFFSET = "content-offset" ;
5151 private static final String CONTENT_SIZE = "content-size-in-bytes" ;
5252
53- private static ThreadLocal <Map <Integer , PartitionSpec >> extraSpecs = new ThreadLocal <>();
54-
5553 private ContentFileParser () {}
5654
5755 private static boolean hasPartitionData (StructLike partitionData ) {
@@ -63,7 +61,7 @@ static String toJson(ContentFile<?> contentFile, PartitionSpec spec) {
6361 generator -> ContentFileParser .toJson (contentFile , spec , generator ), false );
6462 }
6563
66- public static void toJson (ContentFile <?> contentFile , PartitionSpec spec , JsonGenerator generator )
64+ static void toJson (ContentFile <?> contentFile , PartitionSpec spec , JsonGenerator generator )
6765 throws IOException {
6866 Preconditions .checkArgument (contentFile != null , "Invalid content file: null" );
6967 Preconditions .checkArgument (spec != null , "Invalid partition spec: null" );
@@ -136,23 +134,33 @@ public static void toJson(ContentFile<?> contentFile, PartitionSpec spec, JsonGe
136134 generator .writeEndObject ();
137135 }
138136
139- public static ContentFile <?> fromJson (JsonNode jsonNode , PartitionSpec spec ) {
137+ static ContentFile <?> fromJson (JsonNode jsonNode , Map < Integer , PartitionSpec > specsById ) {
140138 Preconditions .checkArgument (jsonNode != null , "Invalid JSON node for content file: null" );
141139 Preconditions .checkArgument (
142140 jsonNode .isObject (), "Invalid JSON node for content file: non-object (%s)" , jsonNode );
143- // Preconditions.checkArgument(spec != null, "Invalid partition spec: null");
144-
141+ Preconditions .checkArgument (specsById != null , "Invalid partition spec: null" );
145142 int specId = JsonUtil .getInt (SPEC_ID , jsonNode );
146143 FileContent fileContent = FileContent .valueOf (JsonUtil .getString (CONTENT , jsonNode ));
147144 String filePath = JsonUtil .getString (FILE_PATH , jsonNode );
148145 FileFormat fileFormat = FileFormat .fromString (JsonUtil .getString (FILE_FORMAT , jsonNode ));
149146
150147 PartitionData partitionData = null ;
151148 if (jsonNode .has (PARTITION )) {
152- // now its callers responsibility to set specs in the parser
153- partitionData =
154- partitionDataFromRawValue (
155- jsonNode .get (PARTITION ), spec == null ? extraSpecs .get ().get (specId ) : spec );
149+ partitionData = new PartitionData (specsById .get (specId ).partitionType ());
150+ StructLike structLike =
151+ (StructLike )
152+ SingleValueParser .fromJson (
153+ specsById .get (specId ).partitionType (), jsonNode .get (PARTITION ));
154+ Preconditions .checkState (
155+ partitionData .size () == structLike .size (),
156+ "Invalid partition data size: expected = %s, actual = %s" ,
157+ partitionData .size (),
158+ structLike .size ());
159+ for (int pos = 0 ; pos < partitionData .size (); ++pos ) {
160+ Class <?> javaClass =
161+ specsById .get (specId ).partitionType ().fields ().get (pos ).type ().typeId ().javaClass ();
162+ partitionData .set (pos , structLike .get (pos , javaClass ));
163+ }
156164 }
157165
158166 long fileSizeInBytes = JsonUtil .getLong (FILE_SIZE , jsonNode );
@@ -197,32 +205,6 @@ public static ContentFile<?> fromJson(JsonNode jsonNode, PartitionSpec spec) {
197205 }
198206 }
199207
200- public static void setSpec (Map <Integer , PartitionSpec > partitionSpec ) {
201- // sets a thread local
202- extraSpecs .set (partitionSpec );
203- }
204-
205- static PartitionData partitionDataFromRawValue (JsonNode rawPartitionValue , PartitionSpec spec ) {
206- if (rawPartitionValue == null ) {
207- return null ;
208- }
209-
210- PartitionData partitionData = new PartitionData (spec .partitionType ());
211- StructLike structLike =
212- (StructLike ) SingleValueParser .fromJson (spec .partitionType (), rawPartitionValue );
213- Preconditions .checkState (
214- partitionData .size () == structLike .size (),
215- "Invalid partition data size: expected = %s, actual = %s" ,
216- partitionData .size (),
217- structLike .size ());
218- for (int pos = 0 ; pos < partitionData .size (); ++pos ) {
219- Class <?> javaClass = spec .partitionType ().fields ().get (pos ).type ().typeId ().javaClass ();
220- partitionData .set (pos , structLike .get (pos , javaClass ));
221- }
222-
223- return partitionData ;
224- }
225-
226208 private static void metricsToJson (ContentFile <?> contentFile , JsonGenerator generator )
227209 throws IOException {
228210 generator .writeNumberField (RECORD_COUNT , contentFile .recordCount ());
0 commit comments