fix: return an error instead of panicking on an invalid DECIMAL precision or scale (#10736)

* test: cover out-of-range decimal precision and scale

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* fix: return an error for out-of-range decimal precision and scale

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* test: cover invalid decimals in gRPC column types and nested arrow types

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* fix: reject invalid decimals in gRPC column types and nested arrow types

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* test: cover invalid decimals nested in gRPC column types

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* fix: reject invalid decimals nested in gRPC column types

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* test: cover invalid decimals in gRPC modify column type

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* fix: reject invalid decimals in gRPC modify column type

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* revert: move the gRPC column type checks to a separate PR

Restore src/api and src/common/grpc-expr to main, undoing the gRPC parts of
5f16c3853, 6b12a8507, d3dc705ac, dd0b6b456, 63d1ca7ce and e925e84c5.
The gRPC conversion becomes a fallible TryFrom in its own PR, as asked in review.

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

---------

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>
This commit is contained in:
dayan02dev authored and GitHub committed 2026-12-04 07:06:54 +00:00
1 parent 251e931e92
commit 2e537f347c
5 files changed
+123 -15

No files matched your search

+31 -4
View File
@@ -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());
+13
View File
@@ -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<u8>, scale: impl TryInto<i8>) -> Option<Self> {
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
}
+43 -11
View File
@@ -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<Co
.map(|t| ConcreteDataType::timestamp_datatype(t.unit()))
.unwrap_or(ConcreteDataType::timestamp_millisecond_datatype())),
SqlDataType::Interval { .. } => 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]
@@ -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)
@@ -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)')];