1616// under the License.
1717
1818use crate :: error:: { _plan_datafusion_err, _plan_err} ;
19+ use crate :: metadata:: FieldMetadata ;
1920use crate :: { Result , ScalarValue } ;
20- use arrow:: datatypes:: DataType ;
21+ use arrow:: datatypes:: FieldRef ;
2122use std:: collections:: HashMap ;
2223
2324/// The parameter value corresponding to the placeholder
2425#[ derive( Debug , Clone ) ]
2526pub enum ParamValues {
2627 /// For positional query parameters, like `SELECT * FROM test WHERE a > $1 AND b = $2`
27- List ( Vec < ScalarValue > ) ,
28+ List ( Vec < ( ScalarValue , Option < FieldMetadata > ) > ) ,
2829 /// For named query parameters, like `SELECT * FROM test WHERE a > $foo AND b = $goo`
29- Map ( HashMap < String , ScalarValue > ) ,
30+ Map ( HashMap < String , ( ScalarValue , Option < FieldMetadata > ) > ) ,
3031}
3132
3233impl ParamValues {
3334 /// Verify parameter list length and type
34- pub fn verify ( & self , expect : & [ DataType ] ) -> Result < ( ) > {
35+ pub fn verify ( & self , expect : & [ FieldRef ] ) -> Result < ( ) > {
3536 match self {
3637 ParamValues :: List ( list) => {
3738 // Verify if the number of params matches the number of values
@@ -45,15 +46,28 @@ impl ParamValues {
4546
4647 // Verify if the types of the params matches the types of the values
4748 let iter = expect. iter ( ) . zip ( list. iter ( ) ) ;
48- for ( i, ( param_type, value) ) in iter. enumerate ( ) {
49- if * param_type != value. data_type ( ) {
49+ for ( i, ( param_type, ( value, maybe_metadata ) ) ) in iter. enumerate ( ) {
50+ if * param_type. data_type ( ) != value. data_type ( ) {
5051 return _plan_err ! (
5152 "Expected parameter of type {}, got {:?} at index {}" ,
5253 param_type,
5354 value. data_type( ) ,
5455 i
5556 ) ;
5657 }
58+
59+ if let Some ( expected_metadata) = maybe_metadata {
60+ // Probably too strict of a comparison (this is an example of where
61+ // the concept of type equality would be useful)
62+ if & expected_metadata. to_hashmap ( ) != param_type. metadata ( ) {
63+ return _plan_err ! (
64+ "Expected parameter with metadata {:?}, got {:?} at index {}" ,
65+ expected_metadata,
66+ param_type. metadata( ) ,
67+ i
68+ ) ;
69+ }
70+ }
5771 }
5872 Ok ( ( ) )
5973 }
@@ -65,7 +79,10 @@ impl ParamValues {
6579 }
6680 }
6781
68- pub fn get_placeholders_with_values ( & self , id : & str ) -> Result < ScalarValue > {
82+ pub fn get_placeholders_with_values (
83+ & self ,
84+ id : & str ,
85+ ) -> Result < ( ScalarValue , Option < FieldMetadata > ) > {
6986 match self {
7087 ParamValues :: List ( list) => {
7188 if id. is_empty ( ) {
@@ -99,7 +116,7 @@ impl ParamValues {
99116
100117impl From < Vec < ScalarValue > > for ParamValues {
101118 fn from ( value : Vec < ScalarValue > ) -> Self {
102- Self :: List ( value)
119+ Self :: List ( value. into_iter ( ) . map ( |v| ( v , None ) ) . collect ( ) )
103120 }
104121}
105122
@@ -108,8 +125,10 @@ where
108125 K : Into < String > ,
109126{
110127 fn from ( value : Vec < ( K , ScalarValue ) > ) -> Self {
111- let value: HashMap < String , ScalarValue > =
112- value. into_iter ( ) . map ( |( k, v) | ( k. into ( ) , v) ) . collect ( ) ;
128+ let value: HashMap < String , ( ScalarValue , Option < FieldMetadata > ) > = value
129+ . into_iter ( )
130+ . map ( |( k, v) | ( k. into ( ) , ( v, None ) ) )
131+ . collect ( ) ;
113132 Self :: Map ( value)
114133 }
115134}
@@ -119,8 +138,10 @@ where
119138 K : Into < String > ,
120139{
121140 fn from ( value : HashMap < K , ScalarValue > ) -> Self {
122- let value: HashMap < String , ScalarValue > =
123- value. into_iter ( ) . map ( |( k, v) | ( k. into ( ) , v) ) . collect ( ) ;
141+ let value: HashMap < String , ( ScalarValue , Option < FieldMetadata > ) > = value
142+ . into_iter ( )
143+ . map ( |( k, v) | ( k. into ( ) , ( v, None ) ) )
144+ . collect ( ) ;
124145 Self :: Map ( value)
125146 }
126147}
0 commit comments