diff --git a/src/servers/src/postgres/types.rs b/src/servers/src/postgres/types.rs index b8251a5d95..88f5add4bc 100644 --- a/src/servers/src/postgres/types.rs +++ b/src/servers/src/postgres/types.rs @@ -898,7 +898,13 @@ pub(super) fn parameters_to_scalar_values( .and_then(|t| t.as_ref()); let client_type = if let Some(client_given_type) = client_param_types.get(idx) { - client_given_type.clone() + match (client_given_type, server_type) { + (&Type::UNKNOWN, Some(server_type)) => { + // If client type is unknown, use the server type. + type_gt_to_pg(server_type).map_err(convert_err)? + } + _ => client_given_type.clone(), + } } else if let Some(server_provided_type) = &server_type { type_gt_to_pg(server_provided_type).map_err(convert_err)? } else { diff --git a/src/sql/src/statements/transform/type_alias.rs b/src/sql/src/statements/transform/type_alias.rs index fb61abf27e..2d78855f2a 100644 --- a/src/sql/src/statements/transform/type_alias.rs +++ b/src/sql/src/statements/transform/type_alias.rs @@ -33,12 +33,21 @@ use crate::statements::{TimezoneInfo, sql_data_type_to_concrete_data_type}; /// - `TimestampMillisecond`, `Timestamp_ms` for `Timestamp(3)`. /// - `TimestampMicrosecond`, `Timestamp_us` for `Timestamp(6)`. /// - `TimestampNanosecond`, `Timestamp_ns` for `Timestamp(9)`. -/// - `INT8` for `tinyint` +/// - TinyText, MediumText, LongText for `Text`. +/// +/// SQL dialect integer type aliases (MySQL & PostgreSQL): +/// - `INT2` for `smallint` +/// - `INT4` for `int` +/// - `INT8` for `bigint` +/// - `FLOAT4` for `float` +/// - `FLOAT8` for `double` +/// +/// Extended type aliases for Arrow types: /// - `INT16` for `smallint` /// - `INT32` for `int` /// - `INT64` for `bigint` /// - And `UINT8`, `UINT16` etc. for `TinyIntUnsigned` etc. -/// - TinyText, MediumText, LongText for `Text`. +/// pub(crate) struct TypeAliasTransformRule; impl TransformRule for TypeAliasTransformRule { @@ -153,13 +162,12 @@ fn replace_type_alias(data_type: &mut DataType) { // Remember to update `get_data_type_by_alias_name()` if you modify this method. pub(crate) fn get_type_by_alias(data_type: &DataType) -> Option { match data_type { - // The sqlparser latest version contains the Int8 alias for Postgres Bigint. - // Which means 8 bytes in postgres (not 8 bits). - // See https://docs.rs/sqlparser/latest/sqlparser/ast/enum.DataType.html#variant.Int8 DataType::Custom(name, tokens) if name.0.len() == 1 && tokens.is_empty() => { get_data_type_by_alias_name(name.0[0].to_string_unquoted().as_str()) } - DataType::Int8(None) => Some(DataType::TinyInt(None)), + DataType::Int2(None) => Some(DataType::SmallInt(None)), + DataType::Int4(None) => Some(DataType::Int(None)), + DataType::Int8(None) => Some(DataType::BigInt(None)), DataType::Int16 => Some(DataType::SmallInt(None)), DataType::Int32 => Some(DataType::Int(None)), DataType::Int64 => Some(DataType::BigInt(None)), @@ -167,6 +175,8 @@ pub(crate) fn get_type_by_alias(data_type: &DataType) -> Option { DataType::UInt16 => Some(DataType::SmallIntUnsigned(None)), DataType::UInt32 => Some(DataType::IntUnsigned(None)), DataType::UInt64 => Some(DataType::BigIntUnsigned(None)), + DataType::Float4 => Some(DataType::Float(None)), + DataType::Float8 => Some(DataType::Double(ExactNumberInfo::None)), DataType::Float32 => Some(DataType::Float(None)), DataType::Float64 => Some(DataType::Double(ExactNumberInfo::None)), DataType::Bool => Some(DataType::Boolean), @@ -199,8 +209,9 @@ pub(crate) fn get_data_type_by_alias_name(name: &str) -> Option { Some(DataType::Timestamp(Some(9), TimezoneInfo::None)) } // Number type alias - // We keep them for backward compatibility. - "INT8" => Some(DataType::TinyInt(None)), + "INT2" => Some(DataType::SmallInt(None)), + "INT4" => Some(DataType::Int(None)), + "INT8" => Some(DataType::BigInt(None)), "INT16" => Some(DataType::SmallInt(None)), "INT32" => Some(DataType::Int(None)), "INT64" => Some(DataType::BigInt(None)), @@ -208,6 +219,8 @@ pub(crate) fn get_data_type_by_alias_name(name: &str) -> Option { "UINT16" => Some(DataType::SmallIntUnsigned(None)), "UINT32" => Some(DataType::IntUnsigned(None)), "UINT64" => Some(DataType::BigIntUnsigned(None)), + "FLOAT4" => Some(DataType::Float(None)), + "FLOAT8" => Some(DataType::Double(ExactNumberInfo::None)), "FLOAT32" => Some(DataType::Float(None)), "FLOAT64" => Some(DataType::Double(ExactNumberInfo::None)), // String type alias @@ -238,14 +251,29 @@ mod tests { get_data_type_by_alias_name("FLOAT64"), Some(DataType::Double(ExactNumberInfo::None)) ); - assert_eq!( get_data_type_by_alias_name("float32"), Some(DataType::Float(None)) ); + assert_eq!( + get_data_type_by_alias_name("float8"), + Some(DataType::Double(ExactNumberInfo::None)) + ); + assert_eq!( + get_data_type_by_alias_name("float4"), + Some(DataType::Float(None)) + ); assert_eq!( get_data_type_by_alias_name("int8"), - Some(DataType::TinyInt(None)) + Some(DataType::BigInt(None)) + ); + assert_eq!( + get_data_type_by_alias_name("int4"), + Some(DataType::Int(None)) + ); + assert_eq!( + get_data_type_by_alias_name("int2"), + Some(DataType::SmallInt(None)) ); assert_eq!( get_data_type_by_alias_name("INT16"), @@ -394,11 +422,15 @@ CREATE TABLE data_types ( tt tinytext, mt mediumtext, lt longtext, - tint int8, + i2 int2, + i4 int4, + i8 int8, sint int16, i int32, bint int64, v varchar, + f4 float4, + f8 float8, f float32, d float64, b boolean, @@ -423,11 +455,15 @@ CREATE TABLE data_types ( tt TINYTEXT, mt MEDIUMTEXT, lt LONGTEXT, - tint TINYINT, + i2 SMALLINT, + i4 INT, + i8 BIGINT, sint SMALLINT, i INT, bint BIGINT, v VARCHAR, + f4 FLOAT, + f8 DOUBLE, f FLOAT, d DOUBLE, b BOOLEAN, diff --git a/tests/cases/standalone/common/create/create_type_alias.result b/tests/cases/standalone/common/create/create_type_alias.result index bcd4cde30a..ded9e13472 100644 --- a/tests/cases/standalone/common/create/create_type_alias.result +++ b/tests/cases/standalone/common/create/create_type_alias.result @@ -1,6 +1,14 @@ CREATE TABLE data_types ( s string, - tint int8, + i2 int2, + i4 int4, + i8 int8, + f4 float4, + f8 float8, + u64 uint64, + u32 uint32, + u16 uint16, + u8 uint8, sint int16, i INT32, bint INT64, @@ -26,7 +34,15 @@ SHOW CREATE TABLE data_types; +------------+------------------------------------------------------------+ | data_types | CREATE TABLE IF NOT EXISTS "data_types" ( | | | "s" STRING NULL, | -| | "tint" TINYINT NULL, | +| | "i2" SMALLINT NULL, | +| | "i4" INT NULL, | +| | "i8" BIGINT NULL, | +| | "f4" FLOAT NULL, | +| | "f8" DOUBLE NULL, | +| | "u64" BIGINT UNSIGNED NULL, | +| | "u32" INT UNSIGNED NULL, | +| | "u16" SMALLINT UNSIGNED NULL, | +| | "u8" TINYINT UNSIGNED NULL, | | | "sint" SMALLINT NULL, | | | "i" INT NULL, | | | "bint" BIGINT NULL, | @@ -55,7 +71,15 @@ DESC TABLE data_types; | Column | Type | Key | Null | Default | Semantic Type | +--------+----------------------+-----+------+---------------------+---------------+ | s | String | PRI | YES | | TAG | -| tint | Int8 | | YES | | FIELD | +| i2 | Int16 | | YES | | FIELD | +| i4 | Int32 | | YES | | FIELD | +| i8 | Int64 | | YES | | FIELD | +| f4 | Float32 | | YES | | FIELD | +| f8 | Float64 | | YES | | FIELD | +| u64 | UInt64 | | YES | | FIELD | +| u32 | UInt32 | | YES | | FIELD | +| u16 | UInt16 | | YES | | FIELD | +| u8 | UInt8 | | YES | | FIELD | | sint | Int16 | | YES | | FIELD | | i | Int32 | | YES | | FIELD | | bint | Int64 | | YES | | FIELD | diff --git a/tests/cases/standalone/common/create/create_type_alias.sql b/tests/cases/standalone/common/create/create_type_alias.sql index 937fb0b506..710035606b 100644 --- a/tests/cases/standalone/common/create/create_type_alias.sql +++ b/tests/cases/standalone/common/create/create_type_alias.sql @@ -1,6 +1,14 @@ CREATE TABLE data_types ( s string, - tint int8, + i2 int2, + i4 int4, + i8 int8, + f4 float4, + f8 float8, + u64 uint64, + u32 uint32, + u16 uint16, + u8 uint8, sint int16, i INT32, bint INT64, diff --git a/tests/cases/standalone/common/function/geo.result b/tests/cases/standalone/common/function/geo.result index c9a064d405..738ad8d94a 100644 --- a/tests/cases/standalone/common/function/geo.result +++ b/tests/cases/standalone/common/function/geo.result @@ -32,11 +32,11 @@ Error: 3001(EngineExecuteQuery), Cast error: Can't cast value -1 to type UInt8 SELECT h3_latlng_to_cell(37.76938, -122.3889, 8::Int8), h3_latlng_to_cell_string(37.76938, -122.3889, 8::Int8); -+-------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ -| h3_latlng_to_cell(Float64(37.76938),Float64(-122.3889),arrow_cast(Int64(8),Utf8("Int8"))) | h3_latlng_to_cell_string(Float64(37.76938),Float64(-122.3889),arrow_cast(Int64(8),Utf8("Int8"))) | -+-------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ -| 613196570438926335 | 88283082e7fffff | -+-------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------+ ++--------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------+ +| h3_latlng_to_cell(Float64(37.76938),Float64(-122.3889),arrow_cast(Int64(8),Utf8("Int64"))) | h3_latlng_to_cell_string(Float64(37.76938),Float64(-122.3889),arrow_cast(Int64(8),Utf8("Int64"))) | ++--------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------+ +| 613196570438926335 | 88283082e7fffff | ++--------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------+ SELECT h3_latlng_to_cell(37.76938, -122.3889, 8::Int16), h3_latlng_to_cell_string(37.76938, -122.3889, 8::Int16); @@ -248,11 +248,11 @@ Error: 3001(EngineExecuteQuery), Cast error: Can't cast value -1 to type UInt8 SELECT geohash(37.76938, -122.3889, 11::Int8); -+----------------------------------------------------------------------------------+ -| geohash(Float64(37.76938),Float64(-122.3889),arrow_cast(Int64(11),Utf8("Int8"))) | -+----------------------------------------------------------------------------------+ -| 9q8yygxneft | -+----------------------------------------------------------------------------------+ ++-----------------------------------------------------------------------------------+ +| geohash(Float64(37.76938),Float64(-122.3889),arrow_cast(Int64(11),Utf8("Int64"))) | ++-----------------------------------------------------------------------------------+ +| 9q8yygxneft | ++-----------------------------------------------------------------------------------+ SELECT geohash(37.76938, -122.3889, 11::Int16);