viirya commented on code in PR #4102:
URL: https://github.com/apache/arrow-datafusion/pull/4102#discussion_r1022184509
##########
datafusion/sql/src/planner.rs:
##########
@@ -2919,19 +2987,103 @@ fn extract_possible_join_keys(
}
}
-// Parse number in sql string, convert to Expr::Literal
-fn parse_sql_number(n: &str) -> Result<Expr> {
- // parse first as i64
- n.parse::<i64>()
- .map(lit)
- // if parsing as i64 fails try f64
- .or_else(|_| n.parse::<f64>().map(lit))
- .map_err(|_| {
- DataFusionError::from(ParserError(format!(
- "Cannot parse {} as i64 or f64",
- n
- )))
- })
+/// Convert SQL simple data type to relational representation of data type
+pub fn convert_simple_data_type(sql_type: &SQLDataType) -> Result<DataType> {
+ match sql_type {
+ SQLDataType::Boolean => Ok(DataType::Boolean),
+ SQLDataType::TinyInt(_) => Ok(DataType::Int8),
+ SQLDataType::SmallInt(_) => Ok(DataType::Int16),
+ SQLDataType::Int(_) | SQLDataType::Integer(_) => Ok(DataType::Int32),
+ SQLDataType::BigInt(_) => Ok(DataType::Int64),
+ SQLDataType::UnsignedTinyInt(_) => Ok(DataType::UInt8),
+ SQLDataType::UnsignedSmallInt(_) => Ok(DataType::UInt16),
+ SQLDataType::UnsignedInt(_) | SQLDataType::UnsignedInteger(_) => {
+ Ok(DataType::UInt32)
+ }
+ SQLDataType::UnsignedBigInt(_) => Ok(DataType::UInt64),
+ SQLDataType::Float(_) => Ok(DataType::Float32),
+ SQLDataType::Real => Ok(DataType::Float32),
+ SQLDataType::Double | SQLDataType::DoublePrecision =>
Ok(DataType::Float64),
+ SQLDataType::Char(_)
+ | SQLDataType::Varchar(_)
+ | SQLDataType::Text
+ | SQLDataType::String => Ok(DataType::Utf8),
+ SQLDataType::Timestamp(tz_info) => {
+ let tz = if matches!(tz_info, TimezoneInfo::Tz)
+ || matches!(tz_info, TimezoneInfo::WithTimeZone)
+ {
+ Some("UTC".to_string())
+ } else {
+ None
+ };
+ Ok(DataType::Timestamp(TimeUnit::Nanosecond, tz))
+ }
+ SQLDataType::Date => Ok(DataType::Date32),
+ SQLDataType::Time(tz_info) => {
+ if matches!(tz_info, TimezoneInfo::None)
+ || matches!(tz_info, TimezoneInfo::WithoutTimeZone)
+ {
+ Ok(DataType::Time64(TimeUnit::Nanosecond))
+ } else {
+ // We dont support TIMETZ and TIME WITH TIME ZONE for now
+ Err(DataFusionError::NotImplemented(format!(
+ "Unsupported SQL type {:?}",
+ sql_type
+ )))
+ }
+ }
+ SQLDataType::Decimal(exact_number_info) => {
+ let (precision, scale) = match *exact_number_info {
+ ExactNumberInfo::None => (None, None),
+ ExactNumberInfo::Precision(precision) => (Some(precision),
None),
+ ExactNumberInfo::PrecisionAndScale(precision, scale) => {
+ (Some(precision), Some(scale))
+ }
+ };
+ make_decimal_type(precision, scale)
+ }
+ SQLDataType::Bytea => Ok(DataType::Binary),
+ // Explicitly list all other types so that if sqlparser
+ // adds/changes the `SQLDataType` the compiler will tell us on upgrade
+ // and avoid bugs like
https://github.com/apache/arrow-datafusion/issues/3059
+ SQLDataType::Nvarchar(_)
+ | SQLDataType::Uuid
+ | SQLDataType::Binary(_)
+ | SQLDataType::Varbinary(_)
+ | SQLDataType::Blob(_)
+ | SQLDataType::Datetime
+ | SQLDataType::Interval
+ | SQLDataType::Regclass
+ | SQLDataType::Custom(_)
+ | SQLDataType::Array(_)
+ | SQLDataType::Enum(_)
+ | SQLDataType::Set(_)
+ | SQLDataType::MediumInt(_)
+ | SQLDataType::UnsignedMediumInt(_)
+ | SQLDataType::Character(_)
+ | SQLDataType::CharacterVarying(_)
+ | SQLDataType::CharVarying(_)
+ | SQLDataType::CharacterLargeObject(_)
+ | SQLDataType::CharLargeObject(_)
+ | SQLDataType::Clob(_) => Err(DataFusionError::NotImplemented(format!(
+ "Unsupported SQL type {:?}",
+ sql_type
+ ))),
+ }
+}
+
+/// Convert SQL data type to relational representation of data type
+pub fn convert_data_type(sql_type: &SQLDataType) -> Result<DataType> {
Review Comment:
Not sure why this change adds this and `convert_simple_data_type`. Otherwise
other change looks good to me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]