18
18
use crate :: { data_type_from_json, data_type_to_json} ;
19
19
use arrow:: datatypes:: { DataType , Field } ;
20
20
use arrow:: error:: { ArrowError , Result } ;
21
+ use arrow_ipc:: writer:: DictionaryTracker ;
21
22
use std:: collections:: HashMap ;
22
23
use std:: sync:: Arc ;
23
24
@@ -218,7 +219,6 @@ pub fn field_from_json(json: &serde_json::Value) -> Result<Field> {
218
219
_ => data_type,
219
220
} ;
220
221
221
- let mut dict_id = 0 ;
222
222
let mut dict_is_ordered = false ;
223
223
224
224
let data_type = match map. get ( "dictionary" ) {
@@ -231,14 +231,6 @@ pub fn field_from_json(json: &serde_json::Value) -> Result<Field> {
231
231
) ) ;
232
232
}
233
233
} ;
234
- dict_id = match dictionary. get ( "id" ) {
235
- Some ( Value :: Number ( n) ) => n. as_i64 ( ) . unwrap ( ) ,
236
- _ => {
237
- return Err ( ArrowError :: ParseError (
238
- "Field missing 'id' attribute" . to_string ( ) ,
239
- ) ) ;
240
- }
241
- } ;
242
234
dict_is_ordered = match dictionary. get ( "isOrdered" ) {
243
235
Some ( & Value :: Bool ( n) ) => n,
244
236
_ => {
@@ -252,8 +244,7 @@ pub fn field_from_json(json: &serde_json::Value) -> Result<Field> {
252
244
_ => data_type,
253
245
} ;
254
246
255
- #[ allow( deprecated) ]
256
- let mut field = Field :: new_dict ( name, data_type, nullable, dict_id, dict_is_ordered) ;
247
+ let mut field = Field :: new_dict ( name, data_type, nullable, dict_is_ordered) ;
257
248
field. set_metadata ( metadata) ;
258
249
Ok ( field)
259
250
}
@@ -264,27 +255,28 @@ pub fn field_from_json(json: &serde_json::Value) -> Result<Field> {
264
255
}
265
256
266
257
/// Generate a JSON representation of the `Field`.
267
- pub fn field_to_json ( field : & Field ) -> serde_json:: Value {
258
+ pub fn field_to_json ( dict_tracker : & mut DictionaryTracker , field : & Field ) -> serde_json:: Value {
268
259
let children: Vec < serde_json:: Value > = match field. data_type ( ) {
269
- DataType :: Struct ( fields) => fields. iter ( ) . map ( |x| field_to_json ( x. as_ref ( ) ) ) . collect ( ) ,
260
+ DataType :: Struct ( fields) => fields
261
+ . iter ( )
262
+ . map ( |x| field_to_json ( dict_tracker, x. as_ref ( ) ) )
263
+ . collect ( ) ,
270
264
DataType :: List ( field)
271
265
| DataType :: LargeList ( field)
272
266
| DataType :: FixedSizeList ( field, _)
273
- | DataType :: Map ( field, _) => vec ! [ field_to_json( field) ] ,
267
+ | DataType :: Map ( field, _) => vec ! [ field_to_json( dict_tracker , field) ] ,
274
268
_ => vec ! [ ] ,
275
269
} ;
276
270
277
271
match field. data_type ( ) {
278
272
DataType :: Dictionary ( ref index_type, ref value_type) => {
279
- #[ allow( deprecated) ]
280
- let dict_id = field. dict_id ( ) . unwrap ( ) ;
281
273
serde_json:: json!( {
282
274
"name" : field. name( ) ,
283
275
"nullable" : field. is_nullable( ) ,
284
276
"type" : data_type_to_json( value_type) ,
285
277
"children" : children,
286
278
"dictionary" : {
287
- "id" : dict_id ,
279
+ "id" : dict_tracker . next_dict_id ( ) ,
288
280
"indexType" : data_type_to_json( index_type) ,
289
281
"isOrdered" : field. dict_is_ordered( ) . unwrap( ) ,
290
282
}
@@ -345,7 +337,8 @@ mod tests {
345
337
}"# ,
346
338
)
347
339
. unwrap ( ) ;
348
- assert_eq ! ( value, field_to_json( & f) ) ;
340
+ let mut dictionary_tracker = DictionaryTracker :: new ( false ) ;
341
+ assert_eq ! ( value, field_to_json( & mut dictionary_tracker, & f) ) ;
349
342
}
350
343
351
344
#[ test]
@@ -398,7 +391,8 @@ mod tests {
398
391
}"# ,
399
392
)
400
393
. unwrap ( ) ;
401
- assert_eq ! ( value, field_to_json( & f) ) ;
394
+ let mut dictionary_tracker = DictionaryTracker :: new ( false ) ;
395
+ assert_eq ! ( value, field_to_json( & mut dictionary_tracker, & f) ) ;
402
396
}
403
397
404
398
#[ test]
@@ -415,7 +409,8 @@ mod tests {
415
409
}"# ,
416
410
)
417
411
. unwrap ( ) ;
418
- assert_eq ! ( value, field_to_json( & f) ) ;
412
+ let mut dictionary_tracker = DictionaryTracker :: new ( false ) ;
413
+ assert_eq ! ( value, field_to_json( & mut dictionary_tracker, & f) ) ;
419
414
}
420
415
#[ test]
421
416
fn parse_struct_from_json ( ) {
0 commit comments