Skip to content

Commit d59cca2

Browse files
committed
feat: Implement dql Distinct
1 parent 2924f29 commit d59cca2

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Storage Support:
5454
- DQL
5555
- [x] Select
5656
- [x] Where
57-
- [ ] Distinct
57+
- [x] Distinct
5858
- [x] Alias
5959
- [x] Aggregation: count()/sum()/avg()/min()/max()
6060
- [ ] Subquery

src/binder/distinct.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::binder::Binder;
2+
use crate::expression::ScalarExpression;
3+
use crate::planner::LogicalPlan;
4+
use crate::planner::operator::aggregate::AggregateOperator;
5+
use crate::storage::Storage;
6+
7+
impl<S: Storage> Binder<S> {
8+
pub fn bind_distinct(
9+
&mut self,
10+
children: LogicalPlan,
11+
select_list: Vec<ScalarExpression>,
12+
) -> LogicalPlan {
13+
AggregateOperator::new(children, vec![], select_list)
14+
}
15+
}

src/binder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod update;
77
mod delete;
88
mod drop_table;
99
mod truncate;
10+
mod distinct;
1011

1112
use std::collections::BTreeMap;
1213
use sqlparser::ast::{Ident, ObjectName, ObjectType, SetExpr, Statement};

src/binder/select.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ use super::Binder;
2020
use crate::catalog::{ColumnCatalog, DEFAULT_DATABASE_NAME, DEFAULT_SCHEMA_NAME, TableCatalog, TableName};
2121
use itertools::Itertools;
2222
use sqlparser::ast;
23-
use sqlparser::ast::{
24-
Expr, Ident, Join, JoinConstraint, JoinOperator, Offset, OrderByExpr, Query, Select,
25-
SelectItem, SetExpr, TableFactor, TableWithJoins,
26-
};
23+
use sqlparser::ast::{Distinct, Expr, Ident, Join, JoinConstraint, JoinOperator, Offset, OrderByExpr, Query, Select, SelectItem, SetExpr, TableFactor, TableWithJoins};
2724
use crate::binder::BindError;
2825
use crate::execution::executor::dql::join::joins_nullable;
2926
use crate::expression::BinaryOperator;
@@ -98,9 +95,9 @@ impl<S: Storage> Binder<S> {
9895
plan = self.bind_having(plan, having)?;
9996
}
10097

101-
// if select.distinct {
102-
// plan = self.bind_distinct(plan, select_list.clone())?;
103-
// }
98+
if let Some(Distinct::Distinct) = select.distinct {
99+
plan = self.bind_distinct(plan, select_list.clone());
100+
}
104101

105102
if let Some(orderby) = having_orderby.1 {
106103
plan = self.bind_sort(plan, orderby);

src/db.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ mod test {
182182
async fn test_crud_sql() -> Result<(), DatabaseError> {
183183
let temp_dir = TempDir::new().expect("unable to create temporary working directory");
184184
let kipsql = Database::with_kipdb(temp_dir.path()).await?;
185-
let _ = kipsql.run("create table t1 (a int primary key, b int)").await?;
185+
let _ = kipsql.run("create table t1 (a int primary key, b int, k int)").await?;
186186
let _ = kipsql.run("create table t2 (c int primary key, d int unsigned null, e datetime)").await?;
187-
let _ = kipsql.run("insert into t1 (a, b) values (1, 1), (4, 3), (5, 2)").await?;
187+
let _ = kipsql.run("insert into t1 (a, b, k) values (1, 1, 1), (4, 2, 2), (5, 2, 2)").await?;
188188
let _ = kipsql.run("insert into t2 (d, c, e) values (2, 1, '2021-05-20 21:00:00'), (3, 4, '2023-09-10 00:00:00')").await?;
189189

190190
println!("full t1:");
@@ -265,6 +265,10 @@ mod test {
265265

266266
assert!(kipsql.run("select max(d) from t2 group by c").await.is_err());
267267

268+
println!("distinct t1:");
269+
let tuples_distinct_t1 = kipsql.run("select distinct b, k from t1").await?;
270+
println!("{}", create_table(&tuples_distinct_t1));
271+
268272
println!("update t1 with filter:");
269273
let _ = kipsql.run("update t1 set a = 0 where b > 1").await?;
270274
println!("after t1:");

0 commit comments

Comments
 (0)