Skip to content

Commit 1c334bf

Browse files
committed
poc
1 parent 7e944ed commit 1c334bf

File tree

2 files changed

+134
-11
lines changed

2 files changed

+134
-11
lines changed

datafusion/core/src/execution/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,12 @@ impl ContextProvider for SessionState {
17411741
.as_ref()
17421742
.and_then(|provider| provider.get(&provider_type)?.get_type(variable_names))
17431743
}
1744+
1745+
fn get_config_option(&self, variable: &str) -> Option<ScalarValue> {
1746+
self.config.config_options.read().get(variable)
1747+
}
1748+
1749+
17441750
}
17451751

17461752
impl FunctionRegistry for SessionState {

datafusion/sql/src/planner.rs

Lines changed: 128 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub trait ContextProvider {
8888
fn get_aggregate_meta(&self, name: &str) -> Option<Arc<AggregateUDF>>;
8989
/// Getter for system/user-defined variable type
9090
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>;
9193
}
9294

9395
/// SQL query planner
@@ -558,7 +560,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
558560
let mut fields = Vec::with_capacity(columns.len());
559561

560562
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)?;
562564
let allow_null = column
563565
.options
564566
.iter()
@@ -1721,7 +1723,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
17211723
SQLExpr::TypedString { data_type, value } => {
17221724
Ok(Expr::Cast(Cast::new(
17231725
Box::new(lit(value)),
1724-
convert_data_type(&data_type)?,
1726+
self.convert_data_type(&data_type)?,
17251727
)))
17261728
}
17271729
SQLExpr::Cast { expr, data_type } => Ok(Expr::Cast(Cast::new(
@@ -1730,7 +1732,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
17301732
&schema,
17311733
&mut HashMap::new(),
17321734
)?),
1733-
convert_data_type(&data_type)?,
1735+
self.convert_data_type(&data_type)?,
17341736
))),
17351737
other => Err(DataFusionError::NotImplemented(format!(
17361738
"Unsupported value {:?} in a values list expression",
@@ -1909,23 +1911,23 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
19091911
data_type,
19101912
} => Ok(Expr::Cast(Cast::new(
19111913
Box::new(self.sql_expr_to_logical_expr(*expr, schema, ctes)?),
1912-
convert_data_type(&data_type)?,
1914+
self.convert_data_type(&data_type)?,
19131915
))),
19141916

19151917
SQLExpr::TryCast {
19161918
expr,
19171919
data_type,
19181920
} => Ok(Expr::TryCast {
19191921
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)?,
19211923
}),
19221924

19231925
SQLExpr::TypedString {
19241926
data_type,
19251927
value,
19261928
} => Ok(Expr::Cast(Cast::new(
19271929
Box::new(lit(value)),
1928-
convert_data_type(&data_type)?,
1930+
self.convert_data_type(&data_type)?,
19291931
))),
19301932

19311933
SQLExpr::IsNull(expr) => Ok(Expr::IsNull(Box::new(
@@ -2482,11 +2484,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
24822484
}
24832485

24842486
// 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+
//}
24902492

24912493
// parse value string from Expr
24922494
let value_string = match &value[0] {
@@ -2671,6 +2673,113 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
26712673
Ok(lit(ScalarValue::new_list(Some(values), data_type)))
26722674
}
26732675
}
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+
26742783
}
26752784

26762785
/// Normalize a SQL object name
@@ -2809,6 +2918,7 @@ fn extract_possible_join_keys(
28092918
}
28102919

28112920
/// Convert SQL simple data type to relational representation of data type
2921+
/*
28122922
pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
28132923
match sql_type {
28142924
SQLDataType::Boolean => Ok(DataType::Boolean),
@@ -2892,8 +3002,10 @@ pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
28923002
))),
28933003
}
28943004
}
3005+
*/
28953006

28963007
/// Convert SQL data type to relational representation of data type
3008+
/*
28973009
pub fn convert_data_type(sql_type: &SQLDataType) -> Result<DataType> {
28983010
match sql_type {
28993011
SQLDataType::Array(inner_sql_type) => {
@@ -2906,6 +3018,7 @@ pub fn convert_data_type(sql_type: &SQLDataType) -> Result<DataType> {
29063018
other => convert_simple_data_type(other),
29073019
}
29083020
}
3021+
*/
29093022

29103023
// Parse number in sql string, convert to Expr::Literal
29113024
fn parse_sql_number(n: &str) -> Result<Expr> {
@@ -5003,6 +5116,10 @@ mod tests {
50035116
fn get_variable_type(&self, _: &[String]) -> Option<DataType> {
50045117
unimplemented!()
50055118
}
5119+
5120+
fn get_config_option(&self, variable: &str) -> Option<ScalarValue>{
5121+
unimplemented!()
5122+
}
50065123
}
50075124

50085125
#[test]

0 commit comments

Comments
 (0)