diff --git a/Cargo.lock b/Cargo.lock index ea1687736f423..799d5d0c59659 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,7 +165,7 @@ dependencies = [ [[package]] name = "arrow2" version = "0.16.0" -source = "git+https://github.com/jorgecarleitao/arrow2?rev=615300b#615300ba2fd6490682d892bc37538df737d3e3db" +source = "git+https://github.com/TCeason/arrow2?branch=ISSUE-1411#bf5c86b81f5621d169bf1fc16dd40ef15735ca51" dependencies = [ "ahash 0.8.2", "arrow-format", @@ -7726,7 +7726,7 @@ checksum = "9091b6114800a5f2141aee1d1b9d6ca3592ac062dc5decb3764ec5895a47b4eb" [[package]] name = "strawboat" version = "0.1.0" -source = "git+https://github.com/sundy-li/strawboat?rev=dfbe357#dfbe357ec1f8968e4bb02756ab17ad54b5bffdcb" +source = "git+https://github.com/TCeason/strawboat?rev=c4d94e8#c4d94e89c67fbf362dfed26fe151fc114c6acfde" dependencies = [ "arrow2", "bytemuck", diff --git a/Cargo.toml b/Cargo.toml index d81db8556a75b..387df89c46778 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -173,7 +173,7 @@ rpath = false # For example: # arrow-format = { git = "https://github.com/datafuse-extras/arrow-format", rev = "78dacc1" } -arrow2 = { git = "https://github.com/jorgecarleitao/arrow2", rev = "615300b" } +arrow2 = { git = "https://github.com/TCeason/arrow2", branch = "ISSUE-1411" } parquet2 = { git = "https://github.com/jorgecarleitao/parquet2", rev = "ed0e1ff" } limits-rs = { git = "https://github.com/datafuse-extras/limits-rs", rev = "abfcf7b" } metrics = { git = "https://github.com/datafuse-extras/metrics.git", rev = "bc49d03" } diff --git a/src/common/arrow/Cargo.toml b/src/common/arrow/Cargo.toml index eeb8f86c07792..7a79568d99cf0 100644 --- a/src/common/arrow/Cargo.toml +++ b/src/common/arrow/Cargo.toml @@ -35,7 +35,7 @@ simd = ["arrow/simd"] # Workspace dependencies # Crates.io dependencies -arrow = { package = "arrow2", git = "https://github.com/jorgecarleitao/arrow2", rev = "615300b", default-features = false, features = [ +arrow = { package = "arrow2", git = "https://github.com/TCeason/arrow2", branch = "ISSUE-1411", default-features = false, features = [ "io_parquet", "io_parquet_compression", "serde_types", @@ -43,7 +43,7 @@ arrow = { package = "arrow2", git = "https://github.com/jorgecarleitao/arrow2", arrow-format = { version = "0.8.0", features = ["flight-data", "flight-service", "ipc"] } futures = "0.3.24" -native = { package = "strawboat", git = "https://github.com/sundy-li/strawboat", rev = "dfbe357" } +native = { package = "strawboat", git = "https://github.com/TCeason/strawboat", rev = "c4d94e8" } parquet2 = { version = "0.17.0", default_features = false, features = ["serde_types"] } [dev-dependencies] diff --git a/src/query/expression/src/utils/display.rs b/src/query/expression/src/utils/display.rs index c434157f988e7..e15796abeda04 100755 --- a/src/query/expression/src/utils/display.rs +++ b/src/query/expression/src/utils/display.rs @@ -814,7 +814,7 @@ fn display_decimal_256(num: i256, scale: u8) -> String { buf, "{}.{:0>width$}", num / pow_scale, - num % pow_scale.abs(), + (num % pow_scale).abs(), width = scale as usize ) .unwrap(); @@ -823,7 +823,7 @@ fn display_decimal_256(num: i256, scale: u8) -> String { buf, "-{}.{:0>width$}", -num / pow_scale, - num % pow_scale, + (num % pow_scale).abs(), width = scale as usize ) .unwrap(); diff --git a/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal b/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal index 9b5ddff02fc17..8f439be9b16b3 100644 --- a/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal +++ b/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal @@ -28,11 +28,21 @@ select 1::Decimal(17, 3), 2::Decimal(19, 4), 3::Decimal(20, 5), cast(10 as Decim ---- 1.000 2.0000 3.00000 10.00 +query IIII +select 1::Decimal(39, 3), 2::Decimal(40, 4), 3::Decimal(75, 5), cast(10 as Decimal(76, 2)); +---- +1.000 2.0000 3.00000 10.00 + query IIII select 1::Decimal(17, 3)::Float64, 2::Decimal(19, 4)::Float64, 3::Decimal(20, 5)::Float64, cast(10 as Decimal(7, 2))::Float64; ---- 1.0 2.0 3.0 10.0 +query IIII +select 1::Decimal(41, 3)::Float64, 2::Decimal(42, 4)::Float64, 3::Decimal(55, 5)::Float64, cast(10 as Decimal(76, 2))::Float64; +---- +1.0 2.0 3.0 10.0 + ## parser overflow statement error 1005 select 2::Decimal(2000, 3); @@ -54,17 +64,30 @@ SELECT CAST(1.23 AS DECIMAL(6, 2)) + CAST(4.56 AS DECIMAL(6, 2)) AS result; ---- 5.78 +query I +SELECT CAST(1.23 AS DECIMAL(76, 2)) + CAST(4.56 AS DECIMAL(76, 2)) AS result; +---- +5.78 + query I SELECT CAST(123456789.12 AS DECIMAL(20, 2)) + CAST(987654321.34 AS DECIMAL(20, 2)) AS result; ---- 1111111110.46 +query I +SELECT CAST(123456789.12 AS DECIMAL(76, 2)) + CAST(987654321.34 AS DECIMAL(76, 2)) AS result; +---- +1111111110.46 + query I SELECT CAST(1.23 AS DECIMAL(6, 2)) + CAST(987654321.34 AS DECIMAL(20, 2)) AS result; ---- 987654322.57 - +query I +SELECT CAST(12345678912345678900.23 AS DECIMAL(76, 2)) + CAST(987654321.34 AS DECIMAL(76, 2)) AS result; +---- +12345678913333334439.10 ## sub @@ -73,16 +96,40 @@ SELECT CAST(4.56 AS DECIMAL(6, 2)) - CAST(1.23 AS DECIMAL(6, 2)) AS result; ---- 3.32 +onlyif mysql +statement error 1068 +SELECT CAST(12345678912345678900.23 AS DECIMAL(76, 2)) - CAST(1.23 AS DECIMAL(6, 2)) AS result + +query I +SELECT CAST(123456789.23 AS DECIMAL(76, 2)) - CAST(1.23 AS DECIMAL(39, 2)) +---- +123456788.00 + +query I +SELECT CAST(12345678912345678900.23 AS DECIMAL(76, 2)) - CAST(12345678912345678900.23 AS DECIMAL(76, 2)) AS result; +---- +0.00 + query I SELECT CAST(987654321.34 AS DECIMAL(20, 2)) - CAST(123456789.12 AS DECIMAL(20, 2)) AS result; ---- 864197532.22 +query I +SELECT CAST(987654321.34 AS DECIMAL(76, 2)) - CAST(123456789.12 AS DECIMAL(76, 2)) AS result; +---- +864197532.22 + query I SELECT CAST(987654321.34 AS DECIMAL(20, 2)) - CAST(1.23 AS DECIMAL(6, 2)) AS result; ---- 987654320.11 +query I +SELECT CAST(987654321.34 AS DECIMAL(39, 2)) - CAST(1.23 AS DECIMAL(39, 2)) AS result; +---- +987654320.11 + ## mul query I @@ -90,16 +137,31 @@ SELECT CAST(1.23 AS DECIMAL(6, 2)) * CAST(4.56 AS DECIMAL(6, 2)) AS result; ---- 5.5965 +query I +SELECT CAST(1.23 AS DECIMAL(76, 2)) * CAST(4.56 AS DECIMAL(76, 2)) AS result; +---- +5.5965 + query I SELECT CAST(123456789.12 AS DECIMAL(20, 2)) * CAST(987654321.34 AS DECIMAL(20, 2)) AS result; ---- 121932631273129095.8208 +query I +SELECT CAST(123456789.12 AS DECIMAL(39, 2)) * CAST(987654321.34 AS DECIMAL(39, 2)) AS result; +---- +121932631273129095.8208 + query I SELECT CAST(1.23 AS DECIMAL(6, 2)) * CAST(987654321.34 AS DECIMAL(20, 2)) AS result; ---- 1214814815.2482 +query I +SELECT CAST(1.23 AS DECIMAL(76, 2)) * CAST(987654321.34 AS DECIMAL(20, 2)) AS result; +---- +1214814815.2482 + ## div @@ -108,18 +170,34 @@ SELECT CAST(4.56 AS DECIMAL(6, 2)) / CAST(1.23 AS DECIMAL(6, 2)) AS result; ---- 3.69 +query I +SELECT CAST(4.56 AS DECIMAL(46, 2)) / CAST(1.23 AS DECIMAL(46, 2)) AS result; +---- +3.69 + query I SELECT CAST(987654321.34 AS DECIMAL(20, 2)) / CAST(123456789.12 AS DECIMAL(20, 2)) AS result; ---- 8.00 +query I +SELECT CAST(987654321.34 AS DECIMAL(52, 2)) / CAST(123456789.12 AS DECIMAL(52, 2)) AS result; +---- +8.00 + query I SELECT CAST(987654321.34 AS DECIMAL(20, 2)) / CAST(1.23 AS DECIMAL(6, 2)) AS result; - ---- 802970992.95 +onlyif mysql +statement error 1068 +SELECT CAST(987654321.34 AS DECIMAL(76, 2)) / CAST(1.23 AS DECIMAL(6, 2)) AS result; +query I +SELECT CAST(987654321.34 AS DECIMAL(76, 2)) / CAST(1.23 AS DECIMAL(76, 2)) AS result; +---- +802970992.95 ## negative @@ -128,52 +206,105 @@ SELECT CAST(-1.23 AS DECIMAL(6, 2)) + CAST(-4.56 AS DECIMAL(6, 2)) AS result; ---- -5.78 +query I +SELECT CAST(-1.23 AS DECIMAL(39, 2)) + CAST(-4.56 AS DECIMAL(39, 2)) AS result; +---- +-5.78 + query I SELECT CAST(-4.56 AS DECIMAL(6, 2)) - CAST(1.23 AS DECIMAL(6, 2)) AS result; ---- -5.78 +query I +SELECT CAST(-4.56 AS DECIMAL(76, 2)) - CAST(1.23 AS DECIMAL(76, 2)) AS result; +---- +-5.78 + query I SELECT CAST(-1.23 AS DECIMAL(6, 2)) * CAST(-4.56 AS DECIMAL(6, 2)) AS result; ---- 5.5965 +query I +SELECT CAST(-1.23 AS DECIMAL(76, 2)) * CAST(-4.56 AS DECIMAL(76, 2)) AS result; +---- +5.5965 + query I SELECT CAST(-4.56 AS DECIMAL(6, 2)) / CAST(-1.23 AS DECIMAL(6, 2)) AS result; ---- 3.69 +query I +SELECT CAST(-4.56 AS DECIMAL(76, 2)) / CAST(-1.23 AS DECIMAL(76, 2)) AS result; +---- +3.69 + query I SELECT MIN(CAST(-4.56 AS DECIMAL(6, 2))) ---- -4.55 +query I +SELECT MIN(CAST(-4.56 AS DECIMAL(55, 2))) +---- +-4.55 + query I SELECT MAX(CAST(4.26 AS DECIMAL(6, 2))) ---- 4.26 +query I +SELECT MAX(CAST(4.26 AS DECIMAL(76, 2))) +---- +4.26 + query I select max(number::Decimal(6,2)) as result from numbers(10); ---- 9.00 +query I +select max(number::Decimal(76,2)) as result from numbers(10); +---- +9.00 + query I select min(number::Decimal(6,2)) as result from numbers(10); ---- 0.00 +query I +select min(number::Decimal(76,3)) as result from numbers(10); +---- +0.000 + query I SELECT ANY(CAST(2.34 AS DECIMAL(6, 2))) ---- 2.34 +query I +SELECT ANY(CAST(2.34 AS DECIMAL(76, 2))) +---- +2.34 query TTTT select typeof(a+b), typeof(a-b), typeof(a*b), typeof(a/b) from (select 3::Decimal(13,2) a , 3.1::Decimal(8,3) b); ---- DECIMAL(15, 3) DECIMAL(15, 3) DECIMAL(21, 5) DECIMAL(16, 2) +query TTTT +select typeof(a+b), typeof(a-b), typeof(a*b), typeof(a/b) from (select 3::Decimal(76,2) a , 3.1::Decimal(76,3) b); +---- +DECIMAL(76, 3) DECIMAL(76, 3) DECIMAL(76, 5) DECIMAL(76, 2) + +query TTTT +select typeof(a+b), typeof(a-b), typeof(a*b), typeof(a/b) from (select 3::Decimal(39,2) a , 3.1::Decimal(39,3) b); +---- +DECIMAL(41, 3) DECIMAL(41, 3) DECIMAL(76, 5) DECIMAL(42, 2) ## compare @@ -182,27 +313,51 @@ select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(13,2) a , 3.1 ---- 0 1 0 1 0 +query IIIII +select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(76,2) a , 3.1::Decimal(76,2) b); +---- +0 1 0 1 0 query IIIII select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(13,2) a , 3 b); ---- 0 0 1 1 1 +query IIIII +select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(76,2) a , 3 b); +---- +0 0 1 1 1 + query IIIII select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(13,2) a , 3.1 b); ---- 0 1 0 1 0 +query IIIII +select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(76,2) a , 3.1 b); +---- +0 1 0 1 0 + query IIIII select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(13,2) a , 2.9 b); ---- 1 0 0 0 1 +query IIIII +select a > b, a < b, a = b, a <= b, a >= b from (select 3::Decimal(76,3) a , 2.9 b); +---- +1 0 0 0 1 + query T select typeof(a = b) from (select 3::Decimal(13,2) a , 2.9 b); ---- BOOLEAN +query T +select typeof(a = b) from (select 3::Decimal(52,2) a , 2.9 b); +---- +BOOLEAN + ## insert statement ok @@ -347,6 +502,151 @@ select * from t_decimal_4_2; 11.11 0.11 +statement ok +drop table if exists t_decimal_76_2; + +statement ok +create table t_decimal_76_2(a Decimal(76,2)); + +## illegal +statement error 1065 +insert into t_decimal_76_2 values (1.1.1); + +statement error 1303 +insert into t_decimal_76_2 values (.); + +statement error 1303 +insert into t_decimal_76_2 values (+); + +statement error 1303 +insert into t_decimal_76_2 values (-); + +statement error 1303 +insert into t_decimal_76_2 values (); + +statement error 1065 +insert into t_decimal_76_2 values (a); + +query I +select * from t_decimal_76_2; +---- + +## overflow +## this will panic +## because of https://github.com/datafuselabs/databend/blob/389f520ac65911b0ce84c9502ee242924a2661eb/src/query/expression/src/types/decimal.rs#L492 +## statement error 1104 +## insert into t_decimal_76_2 values (1e76); +## query I +## select count(*) from t_decimal_76_2; +## ---- +## 0 + +statement ok +truncate table t_decimal_76_2; + +statement ok +insert into t_decimal_76_2 values +(0), (000000), (0.000000), (0e1), (0.), (.0), (.00), +(-0), (-000000), (-0.000000), (-0e1), (-0.), (-.0), (-.00), +(+0), (+000000), (+0.000000), (+0e1), (+0.), (+.0), (+.00); + +query I +select * from t_decimal_76_2; +---- +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 +0.00 + +statement ok +truncate table t_decimal_76_2; + +statement ok +insert into t_decimal_76_2 values +(1.1), (0.1), (1.0), +(1.1e1), (1.1e-1), +(.1), (1.), (0.), (.0), +(10.00); + +query I +select * from t_decimal_76_2; +---- +1.10 +0.10 +1.00 +11.00 +0.11 +0.10 +1.00 +0.00 +0.00 +10.00 + +statement ok +truncate table t_decimal_76_2 + +statement ok +insert into t_decimal_76_2 values (+1.1), (+0.1), (+.0), (+1.1e+1), (+1e-1) ; + +query I +select * from t_decimal_76_2; +---- +1.10 +0.10 +0.00 +11.00 +0.10 + +statement ok +truncate table t_decimal_76_2 + +statement ok +insert into t_decimal_76_2 values (-1.1), (-0.1), (-1.1e+1), (-1e-1) ; + +query I +select * from t_decimal_76_2; +---- +-1.10 +-0.10 +-11.00 +-0.10 + +statement ok +truncate table t_decimal_76_2 + +## cut +statement ok +insert into t_decimal_76_2 values (1.1111), (0.011111e3), (111e-3) ; + +query I +select * from t_decimal_76_2; +---- +1.11 +11.11 +0.11 + +statement ok +drop table t_decimal_76_2 + +statement ok +drop table t_decimal_4_2 statement ok drop table if exists t @@ -399,17 +699,44 @@ select * from t order by c1 asc limit 1,2 2.34 statement ok -create table t1(c0 int, c1 decimal(39,2)) +create table t1(c0 int, c1 decimal(39,2) null) statement ok select * from t order by c1 -## https://github.com/jorgecarleitao/parquet2/blob/main/src/schema/types/converted_type.rs#L25 -## https://github.com/jorgecarleitao/arrow2/blob/main/src/io/parquet/write/schema.rs#L323 -## the parquet2 and arrow2 not impl decimal256 -statement error 1002 +statement ok select * from t1 order by c1 +statement ok +insert into t1(c1) values(1.2) + +statement ok +insert into t1(c1) values(null) + +query T +select c1 from t1 order by c1 asc +---- +1.20 +NULL + +statement ok +insert into t1(c1) values(1.23) + +statement ok +insert into t1(c1) values(-1.23) + +statement ok +insert into t1(c1) values(-1.2) + +query T +select c1 from t1 order by c1 asc +---- +-1.23 +-1.20 +1.20 +1.23 +NULL + statement ok create table t2(c1 decimal(6,2) null)