Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(planner): select without from #5256

Merged
merged 9 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion query/src/sql/planner/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use std::sync::Arc;

use async_recursion::async_recursion;
use common_ast::ast::Expr;
use common_ast::ast::Indirection;
use common_ast::ast::Query;
use common_ast::ast::SelectStmt;
use common_ast::ast::SelectTarget;
use common_ast::ast::SetExpr;
use common_ast::ast::TableReference;
use common_datavalues::DataTypeImpl;
Expand Down Expand Up @@ -73,7 +75,7 @@ impl Binder {
let mut input_context = if let Some(from) = &stmt.from {
self.bind_table_reference(from, bind_context).await?
} else {
BindContext::new()
self.bind_one_table(stmt).await?
};

if let Some(expr) = &stmt.selection {
Expand Down Expand Up @@ -101,6 +103,32 @@ impl Binder {
Ok(output_context)
}

pub(super) async fn bind_one_table(&mut self, stmt: &SelectStmt) -> Result<BindContext> {
for select_target in &stmt.select_list {
if let SelectTarget::QualifiedName(names) = select_target {
for indirect in names {
if indirect == &Indirection::Star {
return Err(ErrorCode::SemanticError(
"SELECT * with no tables specified is not valid",
));
}
}
}
}
let database = "system";
let tenant = self.ctx.get_tenant();
let table_meta: Arc<dyn Table> = self
.resolve_data_source(tenant.as_str(), database, "one")
.await?;
let source = table_meta.read_plan(self.ctx.clone(), None).await?;
let table_index = self
.metadata
.add_table(database.to_string(), table_meta, source);

let result = self.bind_base_table(table_index).await?;
Ok(result)
}

pub(super) async fn bind_table_reference(
&mut self,
stmt: &TableReference,
Expand Down
12 changes: 12 additions & 0 deletions tests/suites/0_stateless/20+_others/20_0001_planner_v2.result
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
====SELECT_FROM_NUMBERS====
0
1
2
Expand All @@ -8,12 +9,18 @@
7
8
9
====COMPARSION====
5
====CAST====
5
====BINARY_OPERATOR====
-0.75
====FUNCTIONS====
0.8414709848078965
====IN_LIST====
1
3
====AGGREGATER====
2
4
3
Expand Down Expand Up @@ -45,6 +52,7 @@
0
0
9
====INNER_JOIN====
1 1
2 2
3 3
Expand All @@ -64,3 +72,7 @@
1 2
2 3
1000
====SELECT_WITHOUT_FROM====
2
8
new_planner
15 changes: 15 additions & 0 deletions tests/suites/0_stateless/20+_others/20_0001_planner_v2.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
set enable_planner_v2 = 1;

select '====SELECT_FROM_NUMBERS====';
select * from numbers(10);

-- Comparison expressions
select '====COMPARSION====';
select * from numbers(10) where number between 1 and 9 and number > 2 and number < 8 and number is not null and number = 5 and number >= 5 and number <= 5;

-- Cast expression
select '====CAST====';
select * from numbers(10) where cast(number as string) = '5';

-- Binary operator
select '====BINARY_OPERATOR====';
select (number + 1 - 2) * 3 / 4 from numbers(1);

-- Functions
select '====FUNCTIONS====';
select sin(cos(number)) from numbers(1);

-- In list
select '====IN_LIST====';
select * from numbers(5) where number in (1, 3);

-- Aggregator operator
select '====AGGREGATER====';
create table t(a int, b int);
insert into t values(1, 2), (2, 3), (3, 4);
select sum(a) + 1 from t group by a;
Expand Down Expand Up @@ -45,6 +52,7 @@ SELECT max(number) FROM numbers_mt (10) where number > 99999999998;
SELECT max(number) FROM numbers_mt (10) where number > 2;

-- Inner join
select '====INNER_JOIN====';
create table t(a int);
insert into t values(1),(2),(3);
create table t1(b float);
Expand All @@ -67,4 +75,11 @@ drop table t2;

select count(*) from numbers(1000) as t inner join numbers(1000) as t1 on t.number = t1.number;

-- Select without from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like to add select 'test case kind'; for the test cases above? So the test result can be seperated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, will be done now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we should split some small tests for v2 in some conflicts PR, name them like 20_0001_planner_v2_select_without_from.sql? cc @leiysky @xudong963

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When planner v2 is ready, we can backport and merge them to our other stateless tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we should split some small tests for v2 in some conflicts PR, name them like 20_0001_planner_v2_select_without_from.sql? cc @leiysky @xudong963

@BohuTANG This test suite won't exist for too long, we just exploit it to make sure there is no regression of our recent work.

We are running stateless tests with new planner enabled to find out the features we haven't implemented yet. As soon as the new planner can pass all the test suites, this file will be deprecated.

And after migrating to new planner, we are going to migrate the test suites to sqllogic test framework. Which means we won't put more effort on merging this with other tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the migration will be completed soon 🐛

select '====SELECT_WITHOUT_FROM====';
select 1 + 1;
select to_int(8);
select "new_planner";
select *; -- {ErrorCode 1065}

set enable_planner_v2 = 0;