mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-05 08:50:58 +00:00
fix: reject invalid decimals nested in gRPC column types
Signed-off-by: dayan02dev <pawarudayan92@gmail.com>
This commit is contained in:
1 parent
d3dc705ace
commit
dd0b6b4568
1 file changed
+38
-12
+38
-12
@@ -59,18 +59,7 @@ 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,
|
||||
}
|
||||
);
|
||||
}
|
||||
ensure_valid_decimal(datatype, datatype_ext.as_ref())?;
|
||||
Ok(Self {
|
||||
datatype,
|
||||
datatype_ext,
|
||||
@@ -100,6 +89,43 @@ impl ColumnDataTypeWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
/// Rejects an out-of-range decimal precision or scale anywhere in the column type, because
|
||||
/// converting the wrapper into a [`ConcreteDataType`] asserts the range.
|
||||
fn ensure_valid_decimal(
|
||||
datatype: ColumnDataType,
|
||||
datatype_ext: Option<&ColumnDataTypeExtension>,
|
||||
) -> Result<()> {
|
||||
let Some(type_ext) = datatype_ext.and_then(|ext| ext.type_ext.as_ref()) else {
|
||||
return Ok(());
|
||||
};
|
||||
match (datatype, type_ext) {
|
||||
(ColumnDataType::Decimal128, TypeExt::DecimalType(d)) => ensure!(
|
||||
Decimal128Type::try_new(d.precision, d.scale).is_some(),
|
||||
error::InvalidDecimalTypeSnafu {
|
||||
precision: d.precision,
|
||||
scale: d.scale,
|
||||
}
|
||||
),
|
||||
(ColumnDataType::List, TypeExt::ListType(d)) => {
|
||||
ensure_valid_decimal(d.datatype(), d.datatype_extension.as_deref())?
|
||||
}
|
||||
(ColumnDataType::Struct, TypeExt::StructType(d)) => {
|
||||
for field in &d.fields {
|
||||
ensure_valid_decimal(field.datatype(), field.datatype_extension.as_ref())?;
|
||||
}
|
||||
}
|
||||
(ColumnDataType::Dictionary, TypeExt::DictionaryType(d)) => {
|
||||
ensure_valid_decimal(d.key_datatype(), d.key_datatype_extension.as_deref())?;
|
||||
ensure_valid_decimal(d.value_datatype(), d.value_datatype_extension.as_deref())?;
|
||||
}
|
||||
(ColumnDataType::Json, TypeExt::JsonNativeType(d)) => {
|
||||
ensure_valid_decimal(d.datatype(), d.datatype_extension.as_deref())?
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the time unit if `datatype` is a timestamp type.
|
||||
pub fn timestamp_unit(datatype: ColumnDataType) -> Option<TimeUnit> {
|
||||
match datatype {
|
||||
|
||||
Reference in new issue
Block a user