1
1
use std:: cmp:: Ordering ;
2
+ use std:: collections:: HashMap ;
2
3
use std:: fmt:: { Display , Formatter } ;
3
4
4
5
use chrono:: { DateTime , NaiveDate , NaiveDateTime , TimeZone , Utc } ;
5
6
6
7
use crate :: actions:: schemas:: ToDataType ;
7
- use crate :: schema:: { ArrayType , DataType , PrimitiveType , StructField } ;
8
+ use crate :: schema:: { ArrayType , DataType , MapType , PrimitiveType , StructField } ;
8
9
use crate :: utils:: require;
9
10
use crate :: { DeltaResult , Error } ;
10
11
@@ -22,11 +23,11 @@ pub struct ArrayData {
22
23
}
23
24
24
25
impl ArrayData {
25
- #[ cfg_attr( feature = "developer-visibility" , visibility:: make( pub ) ) ]
26
26
pub fn new ( tpe : ArrayType , elements : impl IntoIterator < Item = impl Into < Scalar > > ) -> Self {
27
27
let elements = elements. into_iter ( ) . map ( Into :: into) . collect ( ) ;
28
28
Self { tpe, elements }
29
29
}
30
+
30
31
pub fn array_type ( & self ) -> & ArrayType {
31
32
& self . tpe
32
33
}
@@ -89,6 +90,12 @@ impl StructData {
89
90
}
90
91
}
91
92
93
+ #[ derive( Debug , Clone ) ]
94
+ pub struct MapData {
95
+ data_type : MapType ,
96
+ elements : Vec < ( Scalar , Scalar ) > ,
97
+ }
98
+
92
99
/// A single value, which can be null. Used for representing literal values
93
100
/// in [Expressions][crate::expressions::Expression].
94
101
#[ derive( Debug , Clone ) ]
@@ -125,6 +132,8 @@ pub enum Scalar {
125
132
Struct ( StructData ) ,
126
133
/// Array Value
127
134
Array ( ArrayData ) ,
135
+ /// Map Value
136
+ Map ( MapData ) ,
128
137
}
129
138
130
139
impl Scalar {
@@ -146,6 +155,7 @@ impl Scalar {
146
155
Self :: Null ( data_type) => data_type. clone ( ) ,
147
156
Self :: Struct ( data) => DataType :: struct_type ( data. fields . clone ( ) ) ,
148
157
Self :: Array ( data) => data. tpe . clone ( ) . into ( ) ,
158
+ Self :: Map ( map_data) => map_data. data_type . clone ( ) . into ( ) ,
149
159
}
150
160
}
151
161
@@ -222,6 +232,7 @@ impl Display for Scalar {
222
232
}
223
233
write ! ( f, ")" )
224
234
}
235
+ Self :: Map ( _map_data) => todo ! ( ) ,
225
236
}
226
237
}
227
238
}
@@ -269,6 +280,7 @@ impl PartialOrd for Scalar {
269
280
( Null ( _) , _) => None , // NOTE: NULL values are incomparable by definition
270
281
( Struct ( _) , _) => None , // TODO: Support Struct?
271
282
( Array ( _) , _) => None , // TODO: Support Array?
283
+ ( Map ( _) , _) => None , // TODO: Support Map?
272
284
}
273
285
}
274
286
}
@@ -348,6 +360,53 @@ impl<T: Into<Scalar> + ToDataType> From<Option<T>> for Scalar {
348
360
}
349
361
}
350
362
363
+ impl < T : Into < Scalar > + ToDataType > From < Vec < T > > for Scalar {
364
+ fn from ( v : Vec < T > ) -> Self {
365
+ Scalar :: Array ( ArrayData :: new ( ArrayType :: new ( T :: to_data_type ( ) , true ) , v) )
366
+ }
367
+ }
368
+
369
+ impl < K , V > From < HashMap < K , V > > for Scalar
370
+ where
371
+ K : Into < Scalar > + ToDataType ,
372
+ V : Into < Scalar > + ToDataType ,
373
+ {
374
+ fn from ( map : HashMap < K , V > ) -> Self {
375
+ Scalar :: Map ( MapData {
376
+ data_type : MapType :: new ( K :: to_data_type ( ) , V :: to_data_type ( ) , true ) ,
377
+ elements : map. into_iter ( ) . map ( |( k, v) | ( k. into ( ) , v. into ( ) ) ) . collect ( ) ,
378
+ } )
379
+ }
380
+ }
381
+
382
+ use crate :: actions:: Format ;
383
+ impl From < Format > for Scalar {
384
+ fn from ( format : Format ) -> Self {
385
+ let map_type = MapType :: new ( DataType :: STRING , DataType :: STRING , true ) ;
386
+ let fields = vec ! [
387
+ StructField :: new( "provider" , DataType :: STRING , false ) ,
388
+ StructField :: new( "options" , DataType :: Map ( Box :: new( map_type) ) , true ) ,
389
+ ] ;
390
+ let values = vec ! [
391
+ Scalar :: String ( format. provider. to_string( ) ) ,
392
+ format. options. into( ) ,
393
+ ] ;
394
+ Scalar :: Struct ( StructData :: try_new ( fields, values) . unwrap ( ) )
395
+ }
396
+ }
397
+
398
+ use crate :: table_features:: { ReaderFeature , WriterFeature } ;
399
+ impl From < ReaderFeature > for Scalar {
400
+ fn from ( feature : ReaderFeature ) -> Self {
401
+ Scalar :: String ( feature. to_string ( ) )
402
+ }
403
+ }
404
+ impl From < WriterFeature > for Scalar {
405
+ fn from ( feature : WriterFeature ) -> Self {
406
+ Scalar :: String ( feature. to_string ( ) )
407
+ }
408
+ }
409
+
351
410
// TODO: add more From impls
352
411
353
412
impl PrimitiveType {
0 commit comments