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
_ => {
@@ -253,7 +245,7 @@ pub fn field_from_json(json: &serde_json::Value) -> Result<Field> {
253
245
} ;
254
246
255
247
#[ allow( deprecated) ]
256
- let mut field = Field :: new_dict ( name, data_type, nullable, dict_id , dict_is_ordered) ;
248
+ let mut field = Field :: new_dict ( name, data_type, nullable, 0 , dict_is_ordered) ;
257
249
field. set_metadata ( metadata) ;
258
250
Ok ( field)
259
251
}
@@ -264,27 +256,28 @@ pub fn field_from_json(json: &serde_json::Value) -> Result<Field> {
264
256
}
265
257
266
258
/// Generate a JSON representation of the `Field`.
267
- pub fn field_to_json ( field : & Field ) -> serde_json:: Value {
259
+ pub fn field_to_json ( dict_tracker : & mut DictionaryTracker , field : & Field ) -> serde_json:: Value {
268
260
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 ( ) ,
261
+ DataType :: Struct ( fields) => fields
262
+ . iter ( )
263
+ . map ( |x| field_to_json ( dict_tracker, x. as_ref ( ) ) )
264
+ . collect ( ) ,
270
265
DataType :: List ( field)
271
266
| DataType :: LargeList ( field)
272
267
| DataType :: FixedSizeList ( field, _)
273
- | DataType :: Map ( field, _) => vec ! [ field_to_json( field) ] ,
268
+ | DataType :: Map ( field, _) => vec ! [ field_to_json( dict_tracker , field) ] ,
274
269
_ => vec ! [ ] ,
275
270
} ;
276
271
277
272
match field. data_type ( ) {
278
273
DataType :: Dictionary ( ref index_type, ref value_type) => {
279
- #[ allow( deprecated) ]
280
- let dict_id = field. dict_id ( ) . unwrap ( ) ;
281
274
serde_json:: json!( {
282
275
"name" : field. name( ) ,
283
276
"nullable" : field. is_nullable( ) ,
284
277
"type" : data_type_to_json( value_type) ,
285
278
"children" : children,
286
279
"dictionary" : {
287
- "id" : dict_id ,
280
+ "id" : dict_tracker . next_dict_id ( ) ,
288
281
"indexType" : data_type_to_json( index_type) ,
289
282
"isOrdered" : field. dict_is_ordered( ) . unwrap( ) ,
290
283
}
@@ -345,7 +338,8 @@ mod tests {
345
338
}"# ,
346
339
)
347
340
. unwrap ( ) ;
348
- assert_eq ! ( value, field_to_json( & f) ) ;
341
+ let mut dictionary_tracker = DictionaryTracker :: new ( false ) ;
342
+ assert_eq ! ( value, field_to_json( & mut dictionary_tracker, & f) ) ;
349
343
}
350
344
351
345
#[ test]
@@ -398,7 +392,8 @@ mod tests {
398
392
}"# ,
399
393
)
400
394
. unwrap ( ) ;
401
- assert_eq ! ( value, field_to_json( & f) ) ;
395
+ let mut dictionary_tracker = DictionaryTracker :: new ( false ) ;
396
+ assert_eq ! ( value, field_to_json( & mut dictionary_tracker, & f) ) ;
402
397
}
403
398
404
399
#[ test]
@@ -415,7 +410,8 @@ mod tests {
415
410
}"# ,
416
411
)
417
412
. unwrap ( ) ;
418
- assert_eq ! ( value, field_to_json( & f) ) ;
413
+ let mut dictionary_tracker = DictionaryTracker :: new ( false ) ;
414
+ assert_eq ! ( value, field_to_json( & mut dictionary_tracker, & f) ) ;
419
415
}
420
416
#[ test]
421
417
fn parse_struct_from_json ( ) {
0 commit comments