Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser option for parsing SQL numeric literals as decimal #4102

Merged
merged 8 commits into from
Nov 15, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
upmerge
  • Loading branch information
andygrove committed Nov 14, 2022
commit 571834bce52f9c3d1479cfd69b69c3d914aa918e
115 changes: 115 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,121 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
})
}
}

fn convert_data_type(&self, sql_type: &SQLDataType) -> Result<DataType> {
match sql_type {
SQLDataType::Array(inner_sql_type) => {
let data_type = self.convert_simple_data_type(inner_sql_type)?;

Ok(DataType::List(Box::new(Field::new(
"field", data_type, true,
))))
}
other => self.convert_simple_data_type(other),
}
}
fn convert_simple_data_type(&self, 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)
{
// Timestamp With Time Zone
// INPUT : [SQLDataType] TimestampTz + [RuntimeConfig] Time Zone
// OUTPUT: [ArrowDataType] Timestamp<TimeUnit, Some(Time Zone)>
match self
.schema_provider
.get_config_option("datafusion.execution.time_zone")
{
Some(ScalarValue::Utf8(s)) => s,
Some(v) => {
return Err(DataFusionError::Internal(format!(
"Incorrect data type for time_zone: {}",
v.get_datatype(),
)))
}
None => return Err(DataFusionError::Internal(
"Config Option datafusion.execution.time_zone doesn't exist"
.to_string(),
)),
}
} else {
// Timestamp Without Time zone
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
))),
}
}
}

/// Normalize a SQL object name
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.