Skip to content

Commit 2db11df

Browse files
authored
fix(query): add compatibility check for decimal64 (#18280)
1 parent 11251a8 commit 2db11df

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/query/expression/src/converts/meta/index_scalar.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,26 @@ use serde::Deserialize;
2323
use serde::Serialize;
2424

2525
use crate::types::decimal::DecimalScalar;
26+
use crate::types::i256;
2627
use crate::types::number::NumberScalar;
28+
use crate::types::DecimalSize;
2729
use crate::Scalar;
2830

31+
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, EnumAsInner)]
32+
pub enum IndexDecimalScalar {
33+
// For compatibility reason
34+
// The old version only support Decimal128 and Decimal256
35+
// We don't store decimal64 in index scalar, this is only used for compatibility reading
36+
Decimal64(i64, DecimalSize),
37+
Decimal128(i128, DecimalSize),
38+
Decimal256(i256, DecimalSize),
39+
}
40+
2941
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, EnumAsInner)]
3042
pub enum IndexScalar {
3143
Null,
3244
Number(NumberScalar),
33-
Decimal(DecimalScalar),
45+
Decimal(IndexDecimalScalar),
3446
Timestamp(i64),
3547
Date(i32),
3648
Interval(months_days_micros),
@@ -50,7 +62,21 @@ impl TryFrom<IndexScalar> for Scalar {
5062
Ok(match value {
5163
IndexScalar::Null => Scalar::Null,
5264
IndexScalar::Number(num_scalar) => Scalar::Number(num_scalar),
53-
IndexScalar::Decimal(dec_scalar) => Scalar::Decimal(dec_scalar),
65+
IndexScalar::Decimal(dec_scalar) => match dec_scalar {
66+
IndexDecimalScalar::Decimal128(v, size) => {
67+
if size.can_carried_by_64() {
68+
Scalar::Decimal(DecimalScalar::Decimal64(v as i64, size))
69+
} else {
70+
Scalar::Decimal(DecimalScalar::Decimal128(v, size))
71+
}
72+
}
73+
IndexDecimalScalar::Decimal256(v, size) => {
74+
Scalar::Decimal(DecimalScalar::Decimal256(v, size))
75+
}
76+
IndexDecimalScalar::Decimal64(v, size) => {
77+
Scalar::Decimal(DecimalScalar::Decimal64(v, size))
78+
}
79+
},
5480
IndexScalar::Timestamp(ts) => Scalar::Timestamp(ts),
5581
IndexScalar::Date(date) => Scalar::Date(date),
5682
IndexScalar::Interval(interval) => Scalar::Interval(interval),
@@ -77,7 +103,16 @@ impl TryFrom<Scalar> for IndexScalar {
77103
Ok(match value {
78104
Scalar::Null => IndexScalar::Null,
79105
Scalar::Number(num_scalar) => IndexScalar::Number(num_scalar),
80-
Scalar::Decimal(dec_scalar) => IndexScalar::Decimal(dec_scalar),
106+
Scalar::Decimal(dec_scalar) => {
107+
match dec_scalar {
108+
// still save as old format for compatibility
109+
DecimalScalar::Decimal64(v, size) => {
110+
IndexScalar::Decimal(IndexDecimalScalar::Decimal128(v as i128, size))
111+
}
112+
DecimalScalar::Decimal128(v, size) => IndexScalar::Decimal(IndexDecimalScalar::Decimal128(v, size)),
113+
DecimalScalar::Decimal256(v, size) => IndexScalar::Decimal(IndexDecimalScalar::Decimal256(v, size)),
114+
}
115+
}
81116
Scalar::Timestamp(ts) => IndexScalar::Timestamp(ts),
82117
Scalar::Date(date) => IndexScalar::Date(date),
83118
Scalar::Interval(interval) => IndexScalar::Interval(interval),

src/query/expression/src/values.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ pub enum Value<T: AccessType> {
104104
Column(T::Column),
105105
}
106106

107+
/// Note:
108+
/// We must modify IndexScalar if we modify Scalar
107109
#[derive(
108110
Debug, Clone, EnumAsInner, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize,
109111
)]

tests/compat_fuse/compat-logictest/base/fuse_compat_write

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ CREATE TABLE fuse_compat_table (
2222
c_array VARIANT,
2323
c_object VARIANT,
2424
c_variant VARIANT,
25-
c_array_str ARRAY(String)
25+
c_array_str ARRAY(String),
26+
c_decimal_64 DECIMAL(8, 4),
27+
c_decimal_128 DECIMAL(28, 4),
28+
c_decimal_256 DECIMAL(72, 4),
2629
) Engine = Fuse;
2730

2831
statement ok
@@ -43,7 +46,10 @@ INSERT INTO fuse_compat_table VALUES(
4346
parse_json('[1,2,3,["a","b","c"]]'),
4447
parse_json('{"a":1,"b":{"c":2}}'),
4548
parse_json('[1,{"a":1,"b":{"c":2}}]'),
46-
['item1', 'item2_looooooooooooooooooong_val', 'item3', 'item4']
49+
['item1', 'item2_looooooooooooooooooong_val', 'item3', 'item4'],
50+
123.345,
51+
123.3456,
52+
123.456789,
4753
);
4854

4955
statement ok

0 commit comments

Comments
 (0)