Skip to content

Commit 8ea462a

Browse files
authored
Merge pull request #9120 from ClSlaid/select_builder
chore: make SelectBuilder public
2 parents e00d3de + 4617bef commit 8ea462a

File tree

8 files changed

+224
-64
lines changed

8 files changed

+224
-64
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod select_builder;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use common_sql::SelectBuilder;
16+
17+
struct SelectBuilderCase {
18+
pub columns: Vec<String>,
19+
pub order_bys: Vec<String>,
20+
pub filters: Vec<String>,
21+
pub expect: String,
22+
}
23+
type SelectBuilderCaseTuple<'a> = (Vec<&'a str>, Vec<&'a str>, Vec<&'a str>, &'a str);
24+
25+
impl From<SelectBuilderCaseTuple<'_>> for SelectBuilderCase {
26+
fn from((columns, order_bys, filters, expect): SelectBuilderCaseTuple) -> Self {
27+
Self {
28+
columns: columns.into_iter().map(|s| s.to_string()).collect(),
29+
order_bys: order_bys.into_iter().map(|s| s.to_string()).collect(),
30+
filters: filters.into_iter().map(|s| s.to_string()).collect(),
31+
expect: expect.to_string(),
32+
}
33+
}
34+
}
35+
36+
#[test]
37+
fn test_select_builder() {
38+
let cases: Vec<SelectBuilderCase> = vec![
39+
(vec![], vec![], vec![], "SELECT * FROM tbl "),
40+
(
41+
vec!["col0", "col1"],
42+
vec![],
43+
vec![],
44+
"SELECT col0,col1 FROM tbl ",
45+
),
46+
(
47+
vec!["col0 AS Col"],
48+
vec![],
49+
vec![],
50+
"SELECT col0 AS Col FROM tbl ",
51+
),
52+
(
53+
vec![],
54+
vec!["col0"],
55+
vec![],
56+
"SELECT * FROM tbl ORDER BY col0 ",
57+
),
58+
(
59+
vec![],
60+
vec!["col0", "col1"],
61+
vec![],
62+
"SELECT * FROM tbl ORDER BY col0,col1 ",
63+
),
64+
(
65+
vec![],
66+
vec![],
67+
vec!["col0 = '1'"],
68+
"SELECT * FROM tbl where col0 = '1' ",
69+
),
70+
(
71+
vec![],
72+
vec![],
73+
vec!["col0 = '1'", "col1 = '2'"],
74+
"SELECT * FROM tbl where col0 = '1' and col1 = '2' ",
75+
),
76+
(
77+
vec!["col3", "col4"],
78+
vec!["col0", "col1", "col2"],
79+
vec!["col0 = '1'", "col1 = '2'"],
80+
"SELECT col3,col4 FROM tbl where col0 = '1' and col1 = '2' ORDER BY col0,col1,col2 ",
81+
),
82+
]
83+
.into_iter()
84+
.map(SelectBuilderCase::from)
85+
.collect();
86+
87+
for SelectBuilderCase {
88+
columns,
89+
order_bys,
90+
filters,
91+
expect,
92+
} in cases
93+
{
94+
let mut builder = SelectBuilder::from("tbl");
95+
for col in columns {
96+
builder.with_column(col);
97+
}
98+
for order_by in order_bys {
99+
builder.with_order_by(&order_by);
100+
}
101+
for filter in filters {
102+
builder.with_filter(filter);
103+
}
104+
let query = builder.build();
105+
assert_eq!(query, expect);
106+
}
107+
}

src/query/service/tests/it/sql/planner/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
mod builders;
1516
mod format;
1617
mod semantic;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod select_builder;
16+
pub use select_builder::SelectBuilder;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! some useful utils to create inner SQLs
16+
17+
use core::fmt::Write;
18+
19+
pub struct SelectBuilder {
20+
from: String,
21+
columns: Vec<String>,
22+
filters: Vec<String>,
23+
order_bys: Vec<String>,
24+
}
25+
26+
impl SelectBuilder {
27+
pub fn from(table_name: &str) -> SelectBuilder {
28+
SelectBuilder {
29+
from: table_name.to_owned(),
30+
columns: vec![],
31+
filters: vec![],
32+
order_bys: vec![],
33+
}
34+
}
35+
pub fn with_column(&mut self, col_name: impl Into<String>) -> &mut Self {
36+
self.columns.push(col_name.into());
37+
self
38+
}
39+
40+
pub fn with_filter(&mut self, col_name: impl Into<String>) -> &mut Self {
41+
self.filters.push(col_name.into());
42+
self
43+
}
44+
45+
pub fn with_order_by(&mut self, order_by: &str) -> &mut Self {
46+
self.order_bys.push(order_by.to_owned());
47+
self
48+
}
49+
50+
pub fn build(self) -> String {
51+
let mut query = String::new();
52+
let mut columns = String::new();
53+
let s = self.columns.join(",");
54+
if s.is_empty() {
55+
write!(columns, "*").unwrap();
56+
} else {
57+
write!(columns, "{s}").unwrap();
58+
}
59+
60+
let mut order_bys = String::new();
61+
let s = self.order_bys.join(",");
62+
if s.is_empty() {
63+
write!(order_bys, "{s}").unwrap();
64+
} else {
65+
write!(order_bys, "ORDER BY {s}").unwrap();
66+
}
67+
68+
let mut filters = String::new();
69+
let s = self.filters.join(" and ");
70+
if !s.is_empty() {
71+
write!(filters, "where {s}").unwrap();
72+
} else {
73+
write!(filters, "").unwrap();
74+
}
75+
76+
let from = self.from;
77+
write!(query, "SELECT {columns} FROM {from} {filters} {order_bys} ").unwrap();
78+
query
79+
}
80+
}

src/query/sql/src/planner/binder/ddl/table.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use core::fmt::Write;
1615
use std::collections::BTreeMap;
1716
use std::collections::HashSet;
1817
use std::sync::Arc;
@@ -91,69 +90,7 @@ use crate::plans::UndropTablePlan;
9190
use crate::BindContext;
9291
use crate::ColumnBinding;
9392
use crate::ScalarExpr;
94-
95-
struct SelectBuilder {
96-
from: String,
97-
columns: Vec<String>,
98-
filters: Vec<String>,
99-
order_bys: Vec<String>,
100-
}
101-
102-
impl SelectBuilder {
103-
fn from(table_name: &str) -> SelectBuilder {
104-
SelectBuilder {
105-
from: table_name.to_owned(),
106-
columns: vec![],
107-
filters: vec![],
108-
order_bys: vec![],
109-
}
110-
}
111-
fn with_column(&mut self, col_name: impl Into<String>) -> &mut Self {
112-
self.columns.push(col_name.into());
113-
self
114-
}
115-
116-
fn with_filter(&mut self, col_name: impl Into<String>) -> &mut Self {
117-
self.filters.push(col_name.into());
118-
self
119-
}
120-
121-
fn with_order_by(&mut self, order_by: &str) -> &mut Self {
122-
self.order_bys.push(order_by.to_owned());
123-
self
124-
}
125-
126-
fn build(self) -> String {
127-
let mut query = String::new();
128-
let mut columns = String::new();
129-
let s = self.columns.join(",");
130-
if s.is_empty() {
131-
write!(columns, "*").unwrap();
132-
} else {
133-
write!(columns, "{s}").unwrap();
134-
}
135-
136-
let mut order_bys = String::new();
137-
let s = self.order_bys.join(",");
138-
if s.is_empty() {
139-
write!(order_bys, "{s}").unwrap();
140-
} else {
141-
write!(order_bys, "ORDER BY {s}").unwrap();
142-
}
143-
144-
let mut filters = String::new();
145-
let s = self.filters.join(" and ");
146-
if !s.is_empty() {
147-
write!(filters, "where {s}").unwrap();
148-
} else {
149-
write!(filters, "").unwrap();
150-
}
151-
152-
let from = self.from;
153-
write!(query, "SELECT {columns} FROM {from} {filters} {order_bys} ").unwrap();
154-
query
155-
}
156-
}
93+
use crate::SelectBuilder;
15794

15895
impl<'a> Binder {
15996
pub(in crate::planner::binder) async fn bind_show_tables(

src/query/sql/src/planner/binder/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ mod aggregate;
1616
mod bind_context;
1717
#[allow(clippy::module_inception)]
1818
mod binder;
19+
/// SQL builders;
20+
mod builders;
1921
mod copy;
2022
mod ddl;
2123
mod delete;
@@ -40,5 +42,6 @@ mod update;
4042
pub use aggregate::AggregateInfo;
4143
pub use bind_context::*;
4244
pub use binder::Binder;
45+
pub use builders::*;
4346
pub use scalar::ScalarBinder;
4447
pub use scalar_common::*;

src/query/sql/src/planner/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub use binder::BindContext;
2727
pub use binder::Binder;
2828
pub use binder::ColumnBinding;
2929
pub use binder::ScalarBinder;
30+
pub use binder::SelectBuilder;
3031
pub use binder::Visibility;
3132
pub use expression_parser::ExpressionParser;
3233
pub use metadata::*;

0 commit comments

Comments
 (0)