From 74a7d3e3c938f53ffa78ffec8f6978695b409072 Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Sun, 28 Apr 2024 16:12:08 +0800 Subject: [PATCH 1/3] test: add crdb/order_by.slt --- src/binder/aggregate.rs | 2 +- src/execution/volcano/dql/sort.rs | 3 +- src/types/mod.rs | 3 + tests/slt/crdb/join.slt | 48 ++-- tests/slt/crdb/order_by.slt | 406 ++++++++++++++++++++++++++++++ tests/slt/order_by.slt | 10 +- 6 files changed, 440 insertions(+), 32 deletions(-) create mode 100644 tests/slt/crdb/order_by.slt diff --git a/src/binder/aggregate.rs b/src/binder/aggregate.rs index d240d852..a860bade 100644 --- a/src/binder/aggregate.rs +++ b/src/binder/aggregate.rs @@ -80,7 +80,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> { return_orderby.push(SortField::new( expr, asc.map_or(true, |asc| asc), - nulls_first.map_or(true, |first| first), + nulls_first.map_or(false, |first| first), )); } Some(return_orderby) diff --git a/src/execution/volcano/dql/sort.rs b/src/execution/volcano/dql/sort.rs index b0097449..a8e6764c 100644 --- a/src/execution/volcano/dql/sort.rs +++ b/src/execution/volcano/dql/sort.rs @@ -52,13 +52,12 @@ pub(crate) fn sort( let mut key = Vec::new(); expr.eval(&tuple, schema)?.memcomparable_encode(&mut key)?; - key.push(if *nulls_first { u8::MIN } else { u8::MAX }); - if !asc { for byte in key.iter_mut() { *byte ^= 0xFF; } } + key.push(if *nulls_first { u8::MIN } else { u8::MAX }); full_key.extend(key); } Ok::<(Tuple, Vec), DatabaseError>((tuple, full_key)) diff --git a/src/types/mod.rs b/src/types/mod.rs index b8b5734e..8f541159 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -358,6 +358,9 @@ impl TryFrom for LogicalType { char_unit.unwrap_or(CharLengthUnits::Characters), )) } + sqlparser::ast::DataType::String => { + Ok(LogicalType::Varchar(None, CharLengthUnits::Characters)) + } sqlparser::ast::DataType::Float(_) => Ok(LogicalType::Float), sqlparser::ast::DataType::Double | sqlparser::ast::DataType::DoublePrecision => { Ok(LogicalType::Double) diff --git a/tests/slt/crdb/join.slt b/tests/slt/crdb/join.slt index 0bd832c4..94e2b93c 100644 --- a/tests/slt/crdb/join.slt +++ b/tests/slt/crdb/join.slt @@ -10,15 +10,15 @@ INSERT INTO onecolumn(id, x) VALUES (0, 44), (1, NULL), (2, 42) query II SELECT * FROM onecolumn AS a(aid, x) CROSS JOIN onecolumn AS b(bid, y) order by x ---- -1 null 0 44 -1 null 1 null -1 null 2 42 2 42 0 44 2 42 1 null 2 42 2 42 0 44 0 44 0 44 1 null 0 44 2 42 +1 null 0 44 +1 null 1 null +1 null 2 42 # FIXME # statement error 1065 @@ -45,16 +45,16 @@ SELECT * FROM onecolumn AS a NATURAL JOIN onecolumn as b order by a.x desc query II SELECT * FROM onecolumn AS a(aid, x) LEFT OUTER JOIN onecolumn AS b(bid, y) ON a.x = b.y order by a.x ---- -1 null null null 2 42 2 42 0 44 0 44 +1 null null null query I SELECT * FROM onecolumn AS a LEFT OUTER JOIN onecolumn AS b USING(x) ORDER BY x ---- -1 null null 2 42 2 0 44 0 +1 null null # FIXME # statement error 1065 @@ -63,31 +63,31 @@ SELECT * FROM onecolumn AS a LEFT OUTER JOIN onecolumn AS b USING(x) ORDER BY x query I SELECT * FROM onecolumn AS a NATURAL LEFT OUTER JOIN onecolumn AS b order by a.x ---- -1 null 2 42 0 44 +1 null query II SELECT * FROM onecolumn AS a(aid, x) RIGHT OUTER JOIN onecolumn AS b(bid, y) ON a.x = b.y order by x ---- -null null 1 null 2 42 2 42 0 44 0 44 +null null 1 null query I SELECT * FROM onecolumn AS a RIGHT OUTER JOIN onecolumn AS b USING(x) ORDER BY x ---- -null null 1 2 42 2 0 44 0 +null null 1 # FIXME: The fields output by Using are determined by JoinType. At this time, because it is a Right Outer Join, the first row of results should be (1 null). query I SELECT * FROM onecolumn AS a NATURAL RIGHT OUTER JOIN onecolumn AS b order by x ---- -null null 2 42 0 44 +null null statement ok drop table if exists onecolumn_w @@ -120,29 +120,29 @@ INSERT INTO othercolumn(o_id, x) VALUES (0, 43),(1, 42),(2, 16) query II SELECT * FROM onecolumn AS a FULL OUTER JOIN othercolumn AS b ON a.x = b.x ORDER BY a.x,b.x ---- -1 null null null -null null 2 16 -null null 0 43 2 42 1 42 0 44 null null +null null 2 16 +null null 0 43 +1 null null null query II SELECT * FROM onecolumn AS a full OUTER JOIN othercolumn AS b ON a.x = b.x and a.x > 16 order by a.x ---- +2 42 1 42 +0 44 null null null null 0 43 null null 2 16 1 null null null -2 42 1 42 -0 44 null null query II SELECT * FROM onecolumn AS a full OUTER JOIN othercolumn AS b ON a.x = b.x and b.x > 16 order by b.x,a.x ---- -1 null null null -0 44 null null null null 2 16 2 42 1 42 null null 0 43 +0 44 null null +1 null null null # TODO: Full Join on nested loop join # query II @@ -212,9 +212,9 @@ SELECT * FROM empty AS a JOIN onecolumn AS b USING(x) query IT SELECT * FROM onecolumn AS a(aid, x) LEFT OUTER JOIN empty AS b(bid, y) ON a.x = b.y ORDER BY a.x ---- -null null 1 null null null 2 42 null null 0 44 +null null 1 null # FIXME: The fields output by Using are determined by JoinType. # query I @@ -240,9 +240,9 @@ SELECT * FROM onecolumn AS a RIGHT OUTER JOIN empty AS b USING(x) query II SELECT * FROM empty AS a(aid, x) FULL OUTER JOIN onecolumn AS b(bid, y) ON a.x = b.y ORDER BY b.y ---- -null null 1 null null null 2 42 null null 0 44 +null null 1 null statement ok SELECT * FROM empty AS a FULL OUTER JOIN onecolumn AS b USING(x) ORDER BY x @@ -302,23 +302,23 @@ SELECT o.x, t.y FROM onecolumn o INNER JOIN twocolumn t ON (o.x=t.x AND t.y=53) query IT SELECT o.x, t.y FROM onecolumn o LEFT OUTER JOIN twocolumn t ON (o.x=t.x AND t.y=53) order by o.x ---- -null null 42 53 44 null +null null query II SELECT o.x, t.y FROM onecolumn o LEFT OUTER JOIN twocolumn t ON (o.x=t.x AND o.x=44) order by o.x ---- -null null 42 null 44 51 +null null query II SELECT o.x, t.y FROM onecolumn o LEFT OUTER JOIN twocolumn t ON (o.x=t.x AND t.x=44) order by o.x ---- -null null 42 null 44 51 +null null # TODO: Full Join on nested loop join # query @@ -365,18 +365,18 @@ null null 2 4 false query III SELECT * FROM a FULL OUTER JOIN b ON a.i = b.i order by b ---- -0 1 null null null null null 2 4 false 1 2 0 2 true 2 3 1 3 true +0 1 null null null query III SELECT * FROM a FULL OUTER JOIN b ON (a.i = b.i and a.i>2) ORDER BY a.i, b.i ---- -null null 2 4 false 0 1 null null null 1 2 0 2 true 2 3 1 3 true +null null 2 4 false statement ok INSERT INTO b VALUES (3, 3, false) @@ -392,11 +392,11 @@ null null 2 4 false query III SELECT * FROM a FULL OUTER JOIN b ON a.i=b.i ORDER BY b.i, b.b ---- -0 1 null null null 1 2 0 2 true 2 3 3 3 false 2 3 1 3 true null null 2 4 false +0 1 null null null # TODO # query IIIIII diff --git a/tests/slt/crdb/order_by.slt b/tests/slt/crdb/order_by.slt new file mode 100644 index 00000000..716d6ab3 --- /dev/null +++ b/tests/slt/crdb/order_by.slt @@ -0,0 +1,406 @@ +statement ok +drop table if exists t + +statement ok +CREATE TABLE t ( a INT PRIMARY KEY, b INT, c BOOLEAN NULL) + +statement ok +INSERT INTO t VALUES (1, 9, true), (2, 8, false), (3, 7, NULL) + +query I +SELECT c FROM t ORDER BY c +---- +false +true +null + +query I +SELECT c FROM t ORDER BY c +---- +false +true +null + +query I +SELECT c FROM t ORDER BY c DESC +---- +true +false +null + +query II +SELECT a, b FROM t ORDER BY b +---- +3 7 +2 8 +1 9 + +query II +SELECT a, b FROM t ORDER BY b DESC +---- +1 9 +2 8 +3 7 + +# TODO: Order By +# query I +# SELECT a FROM t ORDER BY 1 DESC +# ---- +# 3 +# 2 +# 1 + +query II +SELECT a, b FROM t ORDER BY b DESC LIMIT 2 +---- +1 9 +2 8 + +query II +SELECT DISTINCT c, b FROM t ORDER BY b DESC LIMIT 2 +---- +true 9 +false 8 + +query II +SELECT a AS foo, b FROM t ORDER BY foo DESC +---- +3 7 +2 8 +1 9 + +statement error 1065 +SELECT a AS foo, b AS foo FROM t ORDER BY foo + +query II +SELECT a AS foo, (a) AS foo FROM t ORDER BY foo LIMIT 1 +---- +1 1 + +onlyif mysql +query II +SELECT a AS "foo.bar", b FROM t ORDER BY "foo.bar" DESC +---- +3 7 +2 8 +1 9 + +query II +SELECT a AS foo, b FROM t ORDER BY a DESC +---- +3 7 +2 8 +1 9 + +query I +SELECT b FROM t ORDER BY a DESC +---- +7 +8 +9 + +statement ok +INSERT INTO t(a,b) VALUES (4, 7), (5, 7) + +query II +SELECT a, b FROM t WHERE b = 7 ORDER BY b, a +---- +3 7 +4 7 +5 7 + +query II +SELECT a, b FROM t ORDER BY b, a DESC +---- +5 7 +4 7 +3 7 +2 8 +1 9 + +query III +SELECT a, b, a+b AS ab FROM t WHERE b = 7 ORDER BY ab DESC, a +---- +5 7 12 +4 7 11 +3 7 10 + +query I +SELECT a FROM t ORDER BY a+b DESC, a +---- +5 +4 +1 +2 +3 + +query I +SELECT a FROM t ORDER BY (((a))) +---- +1 +2 +3 +4 +5 + +# TODO: Order By +# query I +# (((SELECT a FROM t))) ORDER BY a DESC LIMIT 4 +# ---- +# 5 +# 4 +# 3 +# 2 + +statement error 1005 +((SELECT a FROM t ORDER BY a)) ORDER BY a + +statement error +SELECT CASE a WHEN 1 THEN b ELSE c END as val FROM t ORDER BY val + +statement error +SELECT * FROM t ORDER BY 0 + +query III +SELECT * FROM t ORDER BY true,a +---- +1 9 true +2 8 false +3 7 null +4 7 null +5 7 null + +query III +SELECT * FROM t ORDER BY "a" +---- +1 9 true +2 8 false +3 7 null +4 7 null +5 7 null + +query III +SELECT * FROM t ORDER BY 2.5,a +---- +1 9 true +2 8 false +3 7 null +4 7 null +5 7 null + +statement error +SELECT * FROM t ORDER BY foo + +statement error +SELECT a FROM t ORDER BY a.b + +statement ok +drop table if exists abc + +statement ok +CREATE TABLE abc ( a INT PRIMARY KEY, b INT, c INT, d VARCHAR) + +statement ok +INSERT INTO abc VALUES (1, 2, 3, 'one'), (4, 5, 6, 'Two') + +# TODO: lower() +# query T +# SELECT d FROM abc ORDER BY lower(d) +# ---- +# one +# Two + +query I +SELECT a FROM abc ORDER BY a DESC +---- +4 +1 + +query I +SELECT a FROM abc ORDER BY a DESC LIMIT 1 +---- +4 + +query I +SELECT a FROM abc ORDER BY a DESC OFFSET 1 +---- +1 + +statement ok +drop table if exists bar + +statement ok +CREATE TABLE bar (id INT PRIMARY KEY, baz STRING NULL) + +statement ok +INSERT INTO bar VALUES (0, NULL), (1, NULL) + +query IT +SELECT * FROM bar ORDER BY baz, id +---- +0 null +1 null + +statement ok +drop table if exists abcd + +statement ok +CREATE TABLE abcd ( a INT PRIMARY KEY, b INT, c INT, d INT) + +statement ok +INSERT INTO abcd VALUES (1, 4, 2, 3), (2, 3, 4, 1), (3, 2, 1, 2), (4, 4, 1, 1) + +query I +SELECT a+b FROM (SELECT * FROM abcd ORDER BY d) +---- +5 +8 +5 +5 + +query I +SELECT b+d FROM (SELECT * FROM abcd ORDER BY a,d) +---- +7 +4 +4 +5 + +statement ok +drop table if exists store + +statement ok +CREATE TABLE store ( id INT PRIMARY KEY, baz STRING NULL, extra INT) + +statement ok +INSERT INTO store VALUES (0, NULL, 10), (1, NULL, 5) + +query ITI +SELECT * FROM store ORDER BY baz, extra +---- +1 null 5 +0 null 10 + +statement ok +drop table if exists xy + +statement ok +CREATE TABLE xy(id INT PRIMARY KEY, x INT NULL, y INT NULL) + +statement ok +INSERT INTO xy VALUES (0, 2, NULL), (1, NULL, 6), (2, 2, 5), (3, 4, 8) + +query IT +SELECT x, y FROM xy ORDER BY y NULLS FIRST +---- +2 null +2 5 +null 6 +4 8 + +query II +SELECT x, y FROM xy ORDER BY y NULLS LAST +---- +2 5 +null 6 +4 8 +2 null + +query IT +SELECT x, y FROM xy ORDER BY y DESC NULLS FIRST +---- +2 null +4 8 +null 6 +2 5 + +query II +SELECT x, y FROM xy ORDER BY y DESC NULLS LAST +---- +4 8 +null 6 +2 5 +2 null + +query II +SELECT x, y FROM xy ORDER BY y NULLS LAST +---- +2 5 +null 6 +4 8 +2 null + +statement ok +INSERT INTO xy VALUES (4, NULL, NULL) + +query TI +SELECT x, y FROM xy ORDER BY x NULLS FIRST, y NULLS LAST +---- +null 6 +null null +2 5 +2 null +4 8 + +query IT +SELECT x, y FROM xy ORDER BY x NULLS LAST, y DESC NULLS FIRST +---- +2 null +2 5 +4 8 +null null +null 6 + +query II +SELECT x, y FROM xy ORDER BY x, y +---- +2 5 +2 null +4 8 +null 6 +null null + +query IT +SELECT x, y FROM xy ORDER BY x, y DESC NULLS FIRST +---- +2 null +2 5 +4 8 +null null +null 6 + +query IT +SELECT x, y FROM xy ORDER BY x NULLS LAST, y DESC NULLS FIRST +---- +2 null +2 5 +4 8 +null null +null 6 + +query TT +SELECT x, y FROM xy ORDER BY x NULLS FIRST, y DESC NULLS LAST +---- +null 6 +null null +2 5 +2 null +4 8 + +query TT +SELECT x, y FROM xy ORDER BY x NULLS FIRST, y DESC +---- +null 6 +null null +2 5 +2 null +4 8 + +query TI +SELECT x, y FROM xy ORDER BY x NULLS FIRST, y DESC NULLS FIRST +---- +null null +null 6 +2 null +2 5 +4 8 \ No newline at end of file diff --git a/tests/slt/order_by.slt b/tests/slt/order_by.slt index e8cc4599..12745df5 100644 --- a/tests/slt/order_by.slt +++ b/tests/slt/order_by.slt @@ -55,20 +55,20 @@ insert into t values (0, 1, 0), (1, 2, 2), (2, null, 5), (3, 2, null), (4, null, query II select v1, v2 from t order by v1 asc, v2 asc ---- -null null -null 5 1 0 -2 null 2 2 +2 null +null 5 +null null query II select v1, v2 from t order by v1 asc nulls last, v2 asc ---- 1 0 -2 null 2 2 -null null +2 null null 5 +null null statement ok drop table t From 1b20a1150cbbc80dd92c4d04ebc0a2d5fe4aca46 Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Sun, 28 Apr 2024 17:06:30 +0800 Subject: [PATCH 2/3] config: update version --- Cargo.toml | 4 ++-- rust-toolchain | 2 +- src/lib.rs | 1 + src/optimizer/heuristic/batch.rs | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 81f8132b..6b148e0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,10 +44,10 @@ csv = { version = "1.3.0" } dirs = { version = "5.0.1" } env_logger = { version = "0.11.3", optional = true } futures = { version = "0.3.30" } -futures-async-stream = { version = "0.2.10" } +futures-async-stream = { version = "0.2.11" } integer-encoding = { version = "3.0.4" } itertools = { version = "0.12.1" } -kip_db = { version = "0.1.2-alpha.26" } +kip_db = { version = "0.1.2-alpha.26.fix1" } lazy_static = { version = "1.4.0" } log = { version = "0.4.21", optional = true } ordered-float = { version = "4.2.0" } diff --git a/rust-toolchain b/rust-toolchain index 8d5e8e76..7df939e8 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2024-01-18 \ No newline at end of file +nightly-2024-04-27 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 13f943c1..964f3625 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ #![feature(iterator_try_collect)] #![feature(slice_pattern)] #![feature(is_sorted)] +#![feature(stmt_expr_attributes)] extern crate core; pub mod binder; pub mod catalog; diff --git a/src/optimizer/heuristic/batch.rs b/src/optimizer/heuristic/batch.rs index fb09a87c..84fbc763 100644 --- a/src/optimizer/heuristic/batch.rs +++ b/src/optimizer/heuristic/batch.rs @@ -3,6 +3,7 @@ use crate::optimizer::rule::normalization::NormalizationRuleImpl; /// A batch of rules. #[derive(Clone)] pub struct HepBatch { + #[allow(dead_code)] pub name: String, pub strategy: HepBatchStrategy, pub rules: Vec, From ab4468643868fbc1cefec33a037197d5c6dd77dd Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Sun, 28 Apr 2024 17:09:45 +0800 Subject: [PATCH 3/3] ci: update toolchain --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3cec258c..2775a7e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - name: Install latest nightly uses: actions-rs/toolchain@v1 with: - toolchain: nightly-2024-01-17 + toolchain: nightly-2024-04-26 override: true components: rustfmt, clippy @@ -53,7 +53,7 @@ jobs: - name: Install latest nightly uses: actions-rs/toolchain@v1 with: - toolchain: nightly-2024-01-17 + toolchain: nightly-2024-04-26 override: true components: rustfmt, clippy @@ -74,7 +74,7 @@ jobs: - name: Install latest nightly uses: actions-rs/toolchain@v1 with: - toolchain: nightly-2024-01-17 + toolchain: nightly-2024-04-26 override: true components: rustfmt, clippy