@@ -88,6 +88,8 @@ pub trait ContextProvider {
88
88
fn get_aggregate_meta ( & self , name : & str ) -> Option < Arc < AggregateUDF > > ;
89
89
/// Getter for system/user-defined variable type
90
90
fn get_variable_type ( & self , variable_names : & [ String ] ) -> Option < DataType > ;
91
+ /// Getter for config_options
92
+ fn get_config_option ( & self , variable : & str ) -> Option < ScalarValue > ;
91
93
}
92
94
93
95
/// SQL query planner
@@ -558,7 +560,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
558
560
let mut fields = Vec :: with_capacity ( columns. len ( ) ) ;
559
561
560
562
for column in columns {
561
- let data_type = convert_simple_data_type ( & column. data_type ) ?;
563
+ let data_type = self . convert_simple_data_type ( & column. data_type ) ?;
562
564
let allow_null = column
563
565
. options
564
566
. iter ( )
@@ -1721,7 +1723,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
1721
1723
SQLExpr :: TypedString { data_type, value } => {
1722
1724
Ok ( Expr :: Cast ( Cast :: new (
1723
1725
Box :: new ( lit ( value) ) ,
1724
- convert_data_type ( & data_type) ?,
1726
+ self . convert_data_type ( & data_type) ?,
1725
1727
) ) )
1726
1728
}
1727
1729
SQLExpr :: Cast { expr, data_type } => Ok ( Expr :: Cast ( Cast :: new (
@@ -1730,7 +1732,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
1730
1732
& schema,
1731
1733
& mut HashMap :: new ( ) ,
1732
1734
) ?) ,
1733
- convert_data_type ( & data_type) ?,
1735
+ self . convert_data_type ( & data_type) ?,
1734
1736
) ) ) ,
1735
1737
other => Err ( DataFusionError :: NotImplemented ( format ! (
1736
1738
"Unsupported value {:?} in a values list expression" ,
@@ -1909,23 +1911,23 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
1909
1911
data_type,
1910
1912
} => Ok ( Expr :: Cast ( Cast :: new (
1911
1913
Box :: new ( self . sql_expr_to_logical_expr ( * expr, schema, ctes) ?) ,
1912
- convert_data_type ( & data_type) ?,
1914
+ self . convert_data_type ( & data_type) ?,
1913
1915
) ) ) ,
1914
1916
1915
1917
SQLExpr :: TryCast {
1916
1918
expr,
1917
1919
data_type,
1918
1920
} => Ok ( Expr :: TryCast {
1919
1921
expr : Box :: new ( self . sql_expr_to_logical_expr ( * expr, schema, ctes) ?) ,
1920
- data_type : convert_data_type ( & data_type) ?,
1922
+ data_type : self . convert_data_type ( & data_type) ?,
1921
1923
} ) ,
1922
1924
1923
1925
SQLExpr :: TypedString {
1924
1926
data_type,
1925
1927
value,
1926
1928
} => Ok ( Expr :: Cast ( Cast :: new (
1927
1929
Box :: new ( lit ( value) ) ,
1928
- convert_data_type ( & data_type) ?,
1930
+ self . convert_data_type ( & data_type) ?,
1929
1931
) ) ) ,
1930
1932
1931
1933
SQLExpr :: IsNull ( expr) => Ok ( Expr :: IsNull ( Box :: new (
@@ -2482,11 +2484,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
2482
2484
}
2483
2485
2484
2486
// we don't support change time zone until we complete time zone related implementation
2485
- if variable_lower == "datafusion.execution.time_zone" {
2486
- return Err ( DataFusionError :: Plan (
2487
- "Changing Time Zone isn't supported yet" . to_string ( ) ,
2488
- ) ) ;
2489
- }
2487
+ // if variable_lower == "datafusion.execution.time_zone" {
2488
+ // return Err(DataFusionError::Plan(
2489
+ // "Changing Time Zone isn't supported yet".to_string(),
2490
+ // ));
2491
+ // }
2490
2492
2491
2493
// parse value string from Expr
2492
2494
let value_string = match & value[ 0 ] {
@@ -2671,6 +2673,113 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
2671
2673
Ok ( lit ( ScalarValue :: new_list ( Some ( values) , data_type) ) )
2672
2674
}
2673
2675
}
2676
+
2677
+ fn convert_data_type ( & self , sql_type : & SQLDataType ) -> Result < DataType > {
2678
+ match sql_type {
2679
+ SQLDataType :: Array ( inner_sql_type) => {
2680
+ let data_type = self . convert_simple_data_type ( inner_sql_type) ?;
2681
+
2682
+ Ok ( DataType :: List ( Box :: new ( Field :: new (
2683
+ "field" , data_type, true ,
2684
+ ) ) ) )
2685
+ }
2686
+ other => self . convert_simple_data_type ( other) ,
2687
+ }
2688
+ }
2689
+ fn convert_simple_data_type ( & self , sql_type : & SQLDataType ) -> Result < DataType > {
2690
+ match sql_type {
2691
+ SQLDataType :: Boolean => Ok ( DataType :: Boolean ) ,
2692
+ SQLDataType :: TinyInt ( _) => Ok ( DataType :: Int8 ) ,
2693
+ SQLDataType :: SmallInt ( _) => Ok ( DataType :: Int16 ) ,
2694
+ SQLDataType :: Int ( _) | SQLDataType :: Integer ( _) => Ok ( DataType :: Int32 ) ,
2695
+ SQLDataType :: BigInt ( _) => Ok ( DataType :: Int64 ) ,
2696
+ SQLDataType :: UnsignedTinyInt ( _) => Ok ( DataType :: UInt8 ) ,
2697
+ SQLDataType :: UnsignedSmallInt ( _) => Ok ( DataType :: UInt16 ) ,
2698
+ SQLDataType :: UnsignedInt ( _) | SQLDataType :: UnsignedInteger ( _) => {
2699
+ Ok ( DataType :: UInt32 )
2700
+ }
2701
+ SQLDataType :: UnsignedBigInt ( _) => Ok ( DataType :: UInt64 ) ,
2702
+ SQLDataType :: Float ( _) => Ok ( DataType :: Float32 ) ,
2703
+ SQLDataType :: Real => Ok ( DataType :: Float32 ) ,
2704
+ SQLDataType :: Double | SQLDataType :: DoublePrecision => Ok ( DataType :: Float64 ) ,
2705
+ SQLDataType :: Char ( _)
2706
+ | SQLDataType :: Varchar ( _)
2707
+ | SQLDataType :: Text
2708
+ | SQLDataType :: String => Ok ( DataType :: Utf8 ) ,
2709
+ SQLDataType :: Timestamp ( tz_info) => {
2710
+ let tz = if matches ! ( tz_info, TimezoneInfo :: Tz )
2711
+ || matches ! ( tz_info, TimezoneInfo :: WithTimeZone )
2712
+ {
2713
+ match self . schema_provider . get_config_option ( "datafusion.execution.time_zone" ) {
2714
+ Some ( ScalarValue :: Utf8 ( s) ) => {
2715
+ s
2716
+ }
2717
+ Some ( _) => {
2718
+ None
2719
+ }
2720
+ None => None
2721
+ }
2722
+ //Some("+00:00".to_string())
2723
+
2724
+ } else {
2725
+ None
2726
+ } ;
2727
+ Ok ( DataType :: Timestamp ( TimeUnit :: Nanosecond , tz) )
2728
+ }
2729
+ SQLDataType :: Date => Ok ( DataType :: Date32 ) ,
2730
+ SQLDataType :: Time ( tz_info) => {
2731
+ if matches ! ( tz_info, TimezoneInfo :: None )
2732
+ || matches ! ( tz_info, TimezoneInfo :: WithoutTimeZone )
2733
+ {
2734
+ Ok ( DataType :: Time64 ( TimeUnit :: Nanosecond ) )
2735
+ } else {
2736
+ // We dont support TIMETZ and TIME WITH TIME ZONE for now
2737
+ Err ( DataFusionError :: NotImplemented ( format ! (
2738
+ "Unsupported SQL type {:?}" ,
2739
+ sql_type
2740
+ ) ) )
2741
+ }
2742
+ }
2743
+ SQLDataType :: Decimal ( exact_number_info) => {
2744
+ let ( precision, scale) = match * exact_number_info {
2745
+ ExactNumberInfo :: None => ( None , None ) ,
2746
+ ExactNumberInfo :: Precision ( precision) => ( Some ( precision) , None ) ,
2747
+ ExactNumberInfo :: PrecisionAndScale ( precision, scale) => {
2748
+ ( Some ( precision) , Some ( scale) )
2749
+ }
2750
+ } ;
2751
+ make_decimal_type ( precision, scale)
2752
+ }
2753
+ SQLDataType :: Bytea => Ok ( DataType :: Binary ) ,
2754
+ // Explicitly list all other types so that if sqlparser
2755
+ // adds/changes the `SQLDataType` the compiler will tell us on upgrade
2756
+ // and avoid bugs like https://github.com/apache/arrow-datafusion/issues/3059
2757
+ SQLDataType :: Nvarchar ( _)
2758
+ | SQLDataType :: Uuid
2759
+ | SQLDataType :: Binary ( _)
2760
+ | SQLDataType :: Varbinary ( _)
2761
+ | SQLDataType :: Blob ( _)
2762
+ | SQLDataType :: Datetime
2763
+ | SQLDataType :: Interval
2764
+ | SQLDataType :: Regclass
2765
+ | SQLDataType :: Custom ( _)
2766
+ | SQLDataType :: Array ( _)
2767
+ | SQLDataType :: Enum ( _)
2768
+ | SQLDataType :: Set ( _)
2769
+ | SQLDataType :: MediumInt ( _)
2770
+ | SQLDataType :: UnsignedMediumInt ( _)
2771
+ | SQLDataType :: Character ( _)
2772
+ | SQLDataType :: CharacterVarying ( _)
2773
+ | SQLDataType :: CharVarying ( _)
2774
+ | SQLDataType :: CharacterLargeObject ( _)
2775
+ | SQLDataType :: CharLargeObject ( _)
2776
+ | SQLDataType :: Clob ( _) => Err ( DataFusionError :: NotImplemented ( format ! (
2777
+ "Unsupported SQL type {:?}" ,
2778
+ sql_type
2779
+ ) ) ) ,
2780
+ }
2781
+ }
2782
+
2674
2783
}
2675
2784
2676
2785
/// Normalize a SQL object name
@@ -2809,6 +2918,7 @@ fn extract_possible_join_keys(
2809
2918
}
2810
2919
2811
2920
/// Convert SQL simple data type to relational representation of data type
2921
+ /*
2812
2922
pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
2813
2923
match sql_type {
2814
2924
SQLDataType::Boolean => Ok(DataType::Boolean),
@@ -2892,8 +3002,10 @@ pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
2892
3002
))),
2893
3003
}
2894
3004
}
3005
+ */
2895
3006
2896
3007
/// Convert SQL data type to relational representation of data type
3008
+ /*
2897
3009
pub fn convert_data_type(sql_type: &SQLDataType) -> Result<DataType> {
2898
3010
match sql_type {
2899
3011
SQLDataType::Array(inner_sql_type) => {
@@ -2906,6 +3018,7 @@ pub fn convert_data_type(sql_type: &SQLDataType) -> Result<DataType> {
2906
3018
other => convert_simple_data_type(other),
2907
3019
}
2908
3020
}
3021
+ */
2909
3022
2910
3023
// Parse number in sql string, convert to Expr::Literal
2911
3024
fn parse_sql_number ( n : & str ) -> Result < Expr > {
@@ -5003,6 +5116,10 @@ mod tests {
5003
5116
fn get_variable_type ( & self , _: & [ String ] ) -> Option < DataType > {
5004
5117
unimplemented ! ( )
5005
5118
}
5119
+
5120
+ fn get_config_option ( & self , variable : & str ) -> Option < ScalarValue > {
5121
+ unimplemented ! ( )
5122
+ }
5006
5123
}
5007
5124
5008
5125
#[ test]
0 commit comments