diff --git a/src/datatypes/src/data_type.rs b/src/datatypes/src/data_type.rs index 8ad388c2ff7..07109629294 100644 --- a/src/datatypes/src/data_type.rs +++ b/src/datatypes/src/data_type.rs @@ -25,6 +25,7 @@ use common_time::timestamp::TimeUnit; use enum_dispatch::enum_dispatch; use paste::paste; use serde::{Deserialize, Serialize}; +use snafu::OptionExt; use crate::error::{self, Error, Result}; use crate::type_id::LogicalTypeId; @@ -477,11 +478,11 @@ impl TryFrom<&ArrowDataType> for ConcreteDataType { ArrowDataType::Utf8View => Self::String(StringType::utf8_view()), ArrowDataType::LargeUtf8 => Self::String(StringType::large_utf8()), ArrowDataType::List(field) => Self::List(ListType::new(Arc::new( - ConcreteDataType::from_arrow_type(field.data_type()), + ConcreteDataType::try_from(field.data_type())?, ))), ArrowDataType::Dictionary(key_type, value_type) => { - let key_type = ConcreteDataType::from_arrow_type(key_type); - let value_type = ConcreteDataType::from_arrow_type(value_type); + let key_type = ConcreteDataType::try_from(key_type.as_ref())?; + let value_type = ConcreteDataType::try_from(value_type.as_ref())?; Self::Dictionary(DictionaryType::new(key_type, value_type)) } ArrowDataType::Time32(u) => ConcreteDataType::Time(TimeType::from_unit(u.into())), @@ -490,7 +491,11 @@ impl TryFrom<&ArrowDataType> for ConcreteDataType { ConcreteDataType::Duration(DurationType::from_unit(u.into())) } ArrowDataType::Decimal128(precision, scale) => { - ConcreteDataType::decimal128_datatype(*precision, *scale) + Decimal128Type::try_new(*precision, *scale) + .map(ConcreteDataType::Decimal128) + .with_context(|| error::UnsupportedArrowTypeSnafu { + arrow_type: dt.clone(), + })? } ArrowDataType::Struct(fields) => { ConcreteDataType::Struct(StructType::try_from_arrow_fields(fields)?) @@ -924,6 +929,28 @@ mod tests { ); } + #[test] + fn test_from_arrow_decimal() { + assert_eq!( + ConcreteDataType::decimal128_datatype(10, 2), + ConcreteDataType::try_from(&ArrowDataType::Decimal128(10, 2)).unwrap() + ); + for (precision, scale) in [(0, 0), (39, 0), (10, -2), (5, 6)] { + let arrow_type = ArrowDataType::Decimal128(precision, scale); + assert!( + ConcreteDataType::try_from(&arrow_type).is_err(), + "{arrow_type} should be rejected" + ); + } + let invalid_decimal = ArrowDataType::Decimal128(10, -2); + let list_type = + ArrowDataType::List(Arc::new(Field::new("item", invalid_decimal.clone(), true))); + assert!(ConcreteDataType::try_from(&list_type).is_err()); + let dictionary_type = + ArrowDataType::Dictionary(Box::new(ArrowDataType::Int32), Box::new(invalid_decimal)); + assert!(ConcreteDataType::try_from(&dictionary_type).is_err()); + } + #[test] fn test_is_null() { assert!(ConcreteDataType::null_datatype().is_null()); diff --git a/src/datatypes/src/types/decimal_type.rs b/src/datatypes/src/types/decimal_type.rs index 9a4c2698412..ba7cc8fd087 100644 --- a/src/datatypes/src/types/decimal_type.rs +++ b/src/datatypes/src/types/decimal_type.rs @@ -46,6 +46,19 @@ impl Decimal128Type { Decimal128Type { precision, scale } } + /// Returns `None` if the precision or scale is out of the range [`Decimal128Type::new`] accepts, + /// including values that do not fit into `u8` or `i8`. + pub fn try_new(precision: impl TryInto, scale: impl TryInto) -> Option { + let (Ok(precision), Ok(scale)) = (precision.try_into(), scale.try_into()) else { + return None; + }; + let is_valid = precision > 0 + && precision <= DECIMAL128_MAX_PRECISION + && scale >= 0 + && scale <= precision as i8; + is_valid.then_some(Decimal128Type { precision, scale }) + } + pub fn precision(&self) -> u8 { self.precision } diff --git a/src/sql/src/statements.rs b/src/sql/src/statements.rs index 53b8e94b475..4c727af6526 100644 --- a/src/sql/src/statements.rs +++ b/src/sql/src/statements.rs @@ -43,9 +43,9 @@ use datatypes::json::JsonSettings; use datatypes::prelude::ConcreteDataType; use datatypes::schema::{COMMENT_KEY, ColumnSchema}; use datatypes::types::json_type::JsonNativeType; -use datatypes::types::{JsonFormat, JsonType, TimestampType}; +use datatypes::types::{Decimal128Type, JsonFormat, JsonType, TimestampType}; use datatypes::value::Value; -use snafu::ResultExt; +use snafu::{OptionExt, ResultExt}; use sqlparser::ast::{ExactNumberInfo, Ident}; use crate::ast::{ @@ -258,15 +258,20 @@ pub fn sql_data_type_to_concrete_data_type(data_type: &SqlDataType) -> Result Ok(ConcreteDataType::interval_month_day_nano_datatype()), - SqlDataType::Decimal(exact_info) => match exact_info { - ExactNumberInfo::None => Ok(ConcreteDataType::decimal128_default_datatype()), - // refer to https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html - // In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0). - ExactNumberInfo::Precision(p) => Ok(ConcreteDataType::decimal128_datatype(*p as u8, 0)), - ExactNumberInfo::PrecisionAndScale(p, s) => { - Ok(ConcreteDataType::decimal128_datatype(*p as u8, *s as i8)) - } - }, + SqlDataType::Decimal(exact_info) => { + let (precision, scale) = match exact_info { + ExactNumberInfo::None => return Ok(ConcreteDataType::decimal128_default_datatype()), + // refer to https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html + // In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0). + ExactNumberInfo::Precision(p) => (*p, 0), + ExactNumberInfo::PrecisionAndScale(p, s) => (*p, *s), + }; + Decimal128Type::try_new(precision, scale) + .map(ConcreteDataType::Decimal128) + .with_context(|| error::SqlTypeNotSupportedSnafu { + t: data_type.clone(), + }) + } SqlDataType::JSON => Ok(ConcreteDataType::Json(JsonType::new(JsonFormat::Jsonb))), // Vector type and JSON2 type SqlDataType::Custom(name, args) if name.0.len() == 1 => { @@ -448,6 +453,33 @@ mod tests { ), ConcreteDataType::vector_datatype(3), ); + check_type( + SqlDataType::Decimal(ExactNumberInfo::Precision(10)), + ConcreteDataType::decimal128_datatype(10, 0), + ); + check_type( + SqlDataType::Decimal(ExactNumberInfo::PrecisionAndScale(38, 38)), + ConcreteDataType::decimal128_datatype(38, 38), + ); + } + + #[test] + pub fn test_invalid_decimal_to_concrete_data_type() { + for info in [ + ExactNumberInfo::Precision(0), + ExactNumberInfo::Precision(39), + ExactNumberInfo::Precision(266), + ExactNumberInfo::PrecisionAndScale(65, 30), + ExactNumberInfo::PrecisionAndScale(266, 2), + ExactNumberInfo::PrecisionAndScale(5, -1), + ExactNumberInfo::PrecisionAndScale(5, 6), + ] { + let sql_type = SqlDataType::Decimal(info); + assert!( + sql_data_type_to_concrete_data_type(&sql_type).is_err(), + "{sql_type} should be rejected" + ); + } } #[test] diff --git a/tests/cases/standalone/common/types/decimal/decimal_table.result b/tests/cases/standalone/common/types/decimal/decimal_table.result index 592fbc6cb99..d62f80143fb 100644 --- a/tests/cases/standalone/common/types/decimal/decimal_table.result +++ b/tests/cases/standalone/common/types/decimal/decimal_table.result @@ -440,3 +440,27 @@ DROP TABLE all_types; Affected Rows: 0 +CREATE TABLE invalid_decimals(d DECIMAL(65,30), ts timestamp time index); + +Error: 2000(InvalidSyntax), SQL data type not supported yet: Decimal(PrecisionAndScale(65, 30)) + +CREATE TABLE invalid_decimals(d DECIMAL(266,2), ts timestamp time index); + +Error: 2000(InvalidSyntax), SQL data type not supported yet: Decimal(PrecisionAndScale(266, 2)) + +CREATE TABLE invalid_decimals(d DECIMAL(5,-1), ts timestamp time index); + +Error: 2000(InvalidSyntax), SQL data type not supported yet: Decimal(PrecisionAndScale(5, -1)) + +SELECT CAST(1 AS DECIMAL(5,-2)); + +Error: 1001(Unsupported), Unsupported arrow data type, type: Decimal128(5, -2) + +SELECT arrow_cast(1, 'Decimal128(10,-2)'); + +Error: 1001(Unsupported), Unsupported arrow data type, type: Decimal128(10, -2) + +SELECT [arrow_cast(1, 'Decimal128(10,-2)')]; + +Error: 1001(Unsupported), Unsupported arrow data type, type: Decimal128(10, -2) + diff --git a/tests/cases/standalone/common/types/decimal/decimal_table.sql b/tests/cases/standalone/common/types/decimal/decimal_table.sql index 60400e3bcd6..df072a53973 100644 --- a/tests/cases/standalone/common/types/decimal/decimal_table.sql +++ b/tests/cases/standalone/common/types/decimal/decimal_table.sql @@ -373,3 +373,15 @@ ADMIN flush_table('all_types'); SELECT * FROM public."all_types" ORDER BY m1 DESC LIMIT 100; DROP TABLE all_types; + +CREATE TABLE invalid_decimals(d DECIMAL(65,30), ts timestamp time index); + +CREATE TABLE invalid_decimals(d DECIMAL(266,2), ts timestamp time index); + +CREATE TABLE invalid_decimals(d DECIMAL(5,-1), ts timestamp time index); + +SELECT CAST(1 AS DECIMAL(5,-2)); + +SELECT arrow_cast(1, 'Decimal128(10,-2)'); + +SELECT [arrow_cast(1, 'Decimal128(10,-2)')];