-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
support decimal data type in create table #1431
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,7 +372,27 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
SQLDataType::Char(_) | SQLDataType::Varchar(_) | SQLDataType::Text => { | ||
Ok(DataType::Utf8) | ||
} | ||
SQLDataType::Decimal(_, _) => Ok(DataType::Float64), | ||
SQLDataType::Decimal(precision, scale) => { | ||
match (precision, scale) { | ||
(None, _) | (_, None) => { | ||
return Err(DataFusionError::Internal(format!( | ||
"Invalid Decimal type ({:?}), precision or scale can't be empty.", | ||
sql_type | ||
))); | ||
} | ||
(Some(p), Some(s)) => { | ||
// TODO add bound checker in some utils file or function | ||
if *p > 38 || *s > *p { | ||
return Err(DataFusionError::Internal(format!( | ||
"Error Decimal Type ({:?}), precision must be less than or equal to 38 and scale can't be greater than precision", | ||
sql_type | ||
))); | ||
} else { | ||
Ok(DataType::Decimal(*p as usize, *s as usize)) | ||
} | ||
} | ||
} | ||
} | ||
SQLDataType::Float(_) => Ok(DataType::Float32), | ||
SQLDataType::Real => Ok(DataType::Float32), | ||
SQLDataType::Double => Ok(DataType::Float64), | ||
|
@@ -1994,8 +2014,8 @@ fn extract_possible_join_keys( | |
} | ||
|
||
/// Convert SQL data type to relational representation of data type | ||
pub fn convert_data_type(sql: &SQLDataType) -> Result<DataType> { | ||
match sql { | ||
pub fn convert_data_type(sql_type: &SQLDataType) -> Result<DataType> { | ||
match sql_type { | ||
SQLDataType::Boolean => Ok(DataType::Boolean), | ||
SQLDataType::SmallInt(_) => Ok(DataType::Int16), | ||
SQLDataType::Int(_) => Ok(DataType::Int32), | ||
|
@@ -2006,6 +2026,27 @@ pub fn convert_data_type(sql: &SQLDataType) -> Result<DataType> { | |
SQLDataType::Char(_) | SQLDataType::Varchar(_) => Ok(DataType::Utf8), | ||
SQLDataType::Timestamp => Ok(DataType::Timestamp(TimeUnit::Nanosecond, None)), | ||
SQLDataType::Date => Ok(DataType::Date32), | ||
SQLDataType::Decimal(precision, scale) => { | ||
match (precision, scale) { | ||
(None, _) | (_, None) => { | ||
return Err(DataFusionError::Internal(format!( | ||
"Invalid Decimal type ({:?}), precision or scale can't be empty.", | ||
sql_type | ||
))); | ||
} | ||
(Some(p), Some(s)) => { | ||
// TODO add bound checker in some utils file or function | ||
if *p > 38 || *s > *p { | ||
return Err(DataFusionError::Internal(format!( | ||
"Error Decimal Type ({:?})", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
sql_type | ||
))); | ||
} else { | ||
Ok(DataType::Decimal(*p as usize, *s as usize)) | ||
} | ||
} | ||
} | ||
} | ||
other => Err(DataFusionError::NotImplemented(format!( | ||
"Unsupported SQL type {:?}", | ||
other | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3761,6 +3761,28 @@ async fn register_aggregate_csv(ctx: &mut ExecutionContext) -> Result<()> { | |
Ok(()) | ||
} | ||
|
||
async fn register_simple_aggregate_csv_with_decimal_by_sql(ctx: &mut ExecutionContext) { | ||
let df = ctx | ||
.sql( | ||
"CREATE EXTERNAL TABLE aggregate_simple ( | ||
c1 DECIMAL(10,6) NOT NULL, | ||
c2 DOUBLE NOT NULL, | ||
c3 BOOLEAN NOT NULL | ||
) | ||
STORED AS CSV | ||
WITH HEADER ROW | ||
LOCATION 'tests/aggregate_simple.csv'", | ||
) | ||
.await | ||
.expect("Creating dataframe for CREATE EXTERNAL TABLE with decimal data type"); | ||
|
||
let results = df.collect().await.expect("Executing CREATE EXTERNAL TABLE"); | ||
assert!( | ||
results.is_empty(), | ||
"Expected no rows from executing CREATE EXTERNAL TABLE" | ||
); | ||
} | ||
|
||
async fn register_aggregate_simple_csv(ctx: &mut ExecutionContext) -> Result<()> { | ||
// It's not possible to use aggregate_test_100, not enought similar values to test grouping on floats | ||
let schema = Arc::new(Schema::new(vec![ | ||
|
@@ -6459,3 +6481,34 @@ async fn test_select_wildcard_without_table() -> Result<()> { | |
} | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn csv_query_with_decimal_by_sql() -> Result<()> { | ||
let mut ctx = ExecutionContext::new(); | ||
register_simple_aggregate_csv_with_decimal_by_sql(&mut ctx).await; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
let sql = "SELECT c1 from aggregate_simple"; | ||
let actual = execute_to_batches(&mut ctx, sql).await; | ||
let expected = vec![ | ||
"+----------+", | ||
"| c1 |", | ||
"+----------+", | ||
"| 0.000010 |", | ||
"| 0.000020 |", | ||
"| 0.000020 |", | ||
"| 0.000030 |", | ||
"| 0.000030 |", | ||
"| 0.000030 |", | ||
"| 0.000040 |", | ||
"| 0.000040 |", | ||
"| 0.000040 |", | ||
"| 0.000040 |", | ||
"| 0.000050 |", | ||
"| 0.000050 |", | ||
"| 0.000050 |", | ||
"| 0.000050 |", | ||
"| 0.000050 |", | ||
"+----------+", | ||
]; | ||
assert_batches_eq!(expected, &actual); | ||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍