mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-05 08:50:58 +00:00
fix: reject invalid decimals in gRPC column types and nested arrow types
Signed-off-by: dayan02dev <pawarudayan92@gmail.com>
This commit is contained in:
1 parent
5f16c38538
commit
6b12a85075
7 files changed
+40
-11
No files matched your search
+10
-1
@@ -89,6 +89,14 @@ pub enum Error {
|
||||
#[snafu(implicit)]
|
||||
location: Location,
|
||||
},
|
||||
|
||||
#[snafu(display("Invalid decimal precision {} or scale {}", precision, scale))]
|
||||
InvalidDecimalType {
|
||||
precision: i32,
|
||||
scale: i32,
|
||||
#[snafu(implicit)]
|
||||
location: Location,
|
||||
},
|
||||
}
|
||||
|
||||
impl ErrorExt for Error {
|
||||
@@ -96,7 +104,8 @@ impl ErrorExt for Error {
|
||||
match self {
|
||||
Error::UnknownColumnDataType { .. }
|
||||
| Error::InvalidTimeUnit { .. }
|
||||
| Error::InconsistentTimeUnit { .. } => StatusCode::InvalidArguments,
|
||||
| Error::InconsistentTimeUnit { .. }
|
||||
| Error::InvalidDecimalType { .. } => StatusCode::InvalidArguments,
|
||||
Error::IntoColumnDataType { .. } | Error::SerializeJson { .. } => {
|
||||
StatusCode::Unexpected
|
||||
}
|
||||
|
||||
+14
-1
@@ -24,7 +24,8 @@ use datatypes::json::value::{JsonNumber, JsonValue, JsonValueRef, JsonVariant, J
|
||||
use datatypes::prelude::{ConcreteDataType, ValueRef};
|
||||
use datatypes::types::json_type::JsonNativeType;
|
||||
use datatypes::types::{
|
||||
IntervalType, JsonFormat, JsonType, StructField, StructType, TimeType, TimestampType,
|
||||
Decimal128Type, IntervalType, JsonFormat, JsonType, StructField, StructType, TimeType,
|
||||
TimestampType,
|
||||
};
|
||||
use datatypes::value::{ListValueRef, OrderedF32, OrderedF64, StructValueRef, Value};
|
||||
use datatypes::vectors::VectorRef;
|
||||
@@ -58,6 +59,18 @@ impl ColumnDataTypeWrapper {
|
||||
pub fn try_new(datatype: i32, datatype_ext: Option<ColumnDataTypeExtension>) -> Result<Self> {
|
||||
let datatype = ColumnDataType::try_from(datatype)
|
||||
.context(error::UnknownColumnDataTypeSnafu { datatype })?;
|
||||
if datatype == ColumnDataType::Decimal128
|
||||
&& let Some(TypeExt::DecimalType(d)) =
|
||||
datatype_ext.as_ref().and_then(|ext| ext.type_ext.as_ref())
|
||||
{
|
||||
ensure!(
|
||||
Decimal128Type::try_new(d.precision, d.scale).is_some(),
|
||||
error::InvalidDecimalTypeSnafu {
|
||||
precision: d.precision,
|
||||
scale: d.scale,
|
||||
}
|
||||
);
|
||||
}
|
||||
Ok(Self {
|
||||
datatype,
|
||||
datatype_ext,
|
||||
|
||||
@@ -478,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())),
|
||||
|
||||
@@ -46,8 +46,12 @@ impl Decimal128Type {
|
||||
Decimal128Type { precision, scale }
|
||||
}
|
||||
|
||||
/// Returns `None` if the precision or scale is out of the range [`Decimal128Type::new`] accepts.
|
||||
pub fn try_new(precision: u8, scale: i8) -> Option<Self> {
|
||||
/// 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
|
||||
|
||||
@@ -266,10 +266,7 @@ pub fn sql_data_type_to_concrete_data_type(data_type: &SqlDataType) -> Result<Co
|
||||
ExactNumberInfo::Precision(p) => (*p, 0),
|
||||
ExactNumberInfo::PrecisionAndScale(p, s) => (*p, *s),
|
||||
};
|
||||
u8::try_from(precision)
|
||||
.ok()
|
||||
.zip(i8::try_from(scale).ok())
|
||||
.and_then(|(precision, scale)| Decimal128Type::try_new(precision, scale))
|
||||
Decimal128Type::try_new(precision, scale)
|
||||
.map(ConcreteDataType::Decimal128)
|
||||
.with_context(|| error::SqlTypeNotSupportedSnafu {
|
||||
t: data_type.clone(),
|
||||
|
||||
@@ -460,3 +460,7 @@ 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)
|
||||
|
||||
@@ -383,3 +383,5 @@ 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)')];
|
||||
Reference in new issue
Block a user