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

feat: support postgres types transparently in queries #7156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ arrow-buffer = { version = "43.0.0", default-features = false }
arrow-flight = { version = "43.0.0", features = ["flight-sql-experimental"] }
arrow-schema = { version = "43.0.0", default-features = false }
parquet = { version = "43.0.0", features = ["arrow", "async", "object_store"] }
sqlparser = { version = "0.36.1", features = ["visitor"] }
# todo: use released version of sqlparser-rs once it is released
sqlparser = { git = "https://github.com/sqlparser-rs/sqlparser-rs", branch = "main", features = ["visitor"] }


[profile.release]
codegen-units = 1
Expand Down
18 changes: 9 additions & 9 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,20 +305,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {

fn convert_simple_data_type(&self, sql_type: &SQLDataType) -> Result<DataType> {
match sql_type {
SQLDataType::Boolean => Ok(DataType::Boolean),
SQLDataType::Boolean | SQLDataType::Bool=> 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::SmallInt(_) | SQLDataType::Int2(_) => Ok(DataType::Int16),
SQLDataType::Int(_) | SQLDataType::Integer(_) | SQLDataType::Int4(_) => Ok(DataType::Int32),
SQLDataType::BigInt(_) | SQLDataType::Int8(_) => Ok(DataType::Int64),
SQLDataType::UnsignedTinyInt(_) => Ok(DataType::UInt8),
SQLDataType::UnsignedSmallInt(_) => Ok(DataType::UInt16),
SQLDataType::UnsignedInt(_) | SQLDataType::UnsignedInteger(_) => {
SQLDataType::UnsignedSmallInt(_) | SQLDataType::UnsignedInt2(_) => Ok(DataType::UInt16),
SQLDataType::UnsignedInt(_) | SQLDataType::UnsignedInteger(_)| SQLDataType::UnsignedInt4(_) => {
Ok(DataType::UInt32)
}
SQLDataType::UnsignedBigInt(_) => Ok(DataType::UInt64),
SQLDataType::UnsignedBigInt(_) | SQLDataType::UnsignedInt8(_) => Ok(DataType::UInt64),
SQLDataType::Float(_) => Ok(DataType::Float32),
SQLDataType::Real => Ok(DataType::Float32),
SQLDataType::Double | SQLDataType::DoublePrecision => Ok(DataType::Float64),
SQLDataType::Real | SQLDataType::FLOAT4=> Ok(DataType::Float32),
SQLDataType::Double | SQLDataType::DoublePrecision | SQLDataType::FLOAT8=> Ok(DataType::Float64),
SQLDataType::Char(_)
| SQLDataType::Varchar(_)
| SQLDataType::Text
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
self.delete_to_plan(table_name, selection)
}

Statement::StartTransaction { modes } => {
Statement::StartTransaction { modes, .. } => {
let isolation_level: ast::TransactionIsolationLevel = modes
.iter()
.filter_map(|m: &ast::TransactionMode| match m {
Expand Down
19 changes: 19 additions & 0 deletions datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4121,6 +4121,25 @@ fn test_multi_grouping_sets() {
quick_test(sql, expected);
}

#[test]
fn test_create_table_with_postgres_type_alias() {
let sql = "create table foo_table(int8_col int8, int4_col int4, int2_col int2,\
float4_col float4, float8_col float8, bool_col bool);";
let expected = "CreateMemoryTable: Bare { table: \"foo_table\" }\n EmptyRelation";
quick_test(sql, expected);
}

#[test]
fn test_select_table_with_postgres_type_alias() {
let sql = "select age::int8 as int8_col, age::int4 as int4_col, age::int2 as int2_col,\
TRY_CAST(age AS FLOAT)::float4 as float4_col, TRY_CAST(age AS FLOAT)::float8 as float8_col, (age = 18) as bool \
from person;";
let expected = "Projection: CAST(person.age AS Int64) AS int8_col, CAST(person.age AS Int32) AS int4_col, \
CAST(person.age AS Int16) AS int2_col, CAST(TRY_CAST(person.age AS Float32) AS Float32) AS float4_col, \
CAST(TRY_CAST(person.age AS Float32) AS Float64) AS float8_col, person.age = Int64(18) AS bool\n TableScan: person";
quick_test(sql, expected);
}

fn assert_field_not_found(err: DataFusionError, name: &str) {
match err {
DataFusionError::SchemaError { .. } => {
Expand Down
Loading