-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(batch): initial cursor implementation (#15968)
- Loading branch information
1 parent
5897c22
commit f975030
Showing
15 changed files
with
566 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO true; | ||
|
||
statement ok | ||
create table a(aid int, c1 int); | ||
|
||
statement ok | ||
insert into a values(1, 11), (2, 12), (3, 13); | ||
|
||
statement ok | ||
create table test(a int,b varchar); | ||
|
||
statement ok | ||
insert into test values(1, 'hello'), (2, 'world'), (3, 'risingwave'), (4, 'labs'); | ||
|
||
# Currently, we allow declaring cursors out of TRANSACTION, because we don't have RW txn support. In PG, it's not allowed | ||
statement ok | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test where a > 2 ORDER BY a; | ||
|
||
statement ok | ||
CLOSE test_cursor; | ||
|
||
statement ok | ||
START TRANSACTION ISOLATION LEVEL REPEATABLE READ; | ||
|
||
statement ok | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test where a > 2 ORDER BY a; | ||
|
||
statement error cursor "test_cursor" already exists | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test where a > 2; | ||
|
||
statement error table or source not found: test | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test_non_existent where a > 2; | ||
|
||
statement error cursor "test_cursor_non_existent" does not exist | ||
FETCH NEXT from test_cursor_non_existent; | ||
|
||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
3 risingwave | ||
|
||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
4 labs | ||
|
||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
|
||
statement error cursor "test_cursor_non_existent" does not exist | ||
CLOSE test_cursor_non_existent; | ||
|
||
statement ok | ||
CLOSE test_cursor; | ||
|
||
statement error cursor "test_cursor" does not exist | ||
FETCH NEXT from test_cursor; | ||
|
||
statement ok | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test JOIN a ON test.a > 1 and a.aid = test.a ORDER BY test.a; | ||
|
||
query IIII | ||
FETCH NEXT from test_cursor; | ||
---- | ||
2 world 2 12 | ||
|
||
query IIII | ||
FETCH NEXT from test_cursor; | ||
---- | ||
3 risingwave 3 13 | ||
|
||
query IIII | ||
FETCH NEXT from test_cursor; | ||
---- | ||
|
||
statement ok | ||
COMMIT; | ||
|
||
statement ok | ||
drop table test; | ||
|
||
statement ok | ||
drop table a; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO true; | ||
|
||
statement ok | ||
create table test(a int,b varchar); | ||
|
||
statement ok | ||
insert into test values(1, 'hello'), (2, 'world'), (3, 'risingwave'); | ||
|
||
statement ok | ||
START TRANSACTION ISOLATION LEVEL REPEATABLE READ; | ||
|
||
statement ok | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test where a > 2 ORDER BY a; | ||
|
||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
3 risingwave | ||
|
||
connection other | ||
statement ok | ||
flush; | ||
|
||
connection other | ||
statement ok | ||
insert into test values(4, 'labs'); | ||
|
||
connection other | ||
statement ok | ||
flush; | ||
|
||
connection other | ||
query II | ||
SELECT * FROM test where a > 2 ORDER BY a; | ||
---- | ||
3 risingwave | ||
4 labs | ||
|
||
connection other | ||
statement ok | ||
START TRANSACTION ISOLATION LEVEL REPEATABLE READ; | ||
|
||
connection other | ||
statement ok | ||
DECLARE | ||
test_cursor CURSOR FOR | ||
SELECT * FROM test where a > 2 ORDER BY a; | ||
|
||
connection other | ||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
3 risingwave | ||
|
||
connection other | ||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
4 labs | ||
|
||
connection other | ||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
|
||
connection other | ||
statement ok | ||
COMMIT; | ||
|
||
query II | ||
FETCH NEXT from test_cursor; | ||
---- | ||
|
||
statement ok | ||
COMMIT; | ||
|
||
query II | ||
SELECT * FROM test where a > 2 ORDER BY a; | ||
---- | ||
3 risingwave | ||
4 labs | ||
|
||
statement ok | ||
drop table test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use pgwire::pg_response::{PgResponse, StatementType}; | ||
use risingwave_sqlparser::ast::ObjectName; | ||
|
||
use super::RwPgResponse; | ||
use crate::error::Result; | ||
use crate::handler::HandlerArgs; | ||
|
||
pub async fn handle_close_cursor( | ||
handler_args: HandlerArgs, | ||
cursor_name: Option<ObjectName>, | ||
) -> Result<RwPgResponse> { | ||
if let Some(name) = cursor_name { | ||
handler_args.session.drop_cursor(name).await?; | ||
} else { | ||
handler_args.session.drop_all_cursors().await; | ||
} | ||
Ok(PgResponse::empty_result(StatementType::CLOSE_CURSOR)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use pgwire::pg_response::{PgResponse, StatementType}; | ||
use risingwave_sqlparser::ast::{ObjectName, Query, Statement}; | ||
|
||
use super::query::{gen_batch_plan_by_statement, gen_batch_plan_fragmenter}; | ||
use super::RwPgResponse; | ||
use crate::error::Result; | ||
use crate::handler::HandlerArgs; | ||
use crate::session::cursor::Cursor; | ||
use crate::OptimizerContext; | ||
|
||
pub async fn handle_declare_cursor( | ||
handler_args: HandlerArgs, | ||
cursor_name: ObjectName, | ||
query: Query, | ||
) -> Result<RwPgResponse> { | ||
let session = handler_args.session.clone(); | ||
|
||
let plan_fragmenter_result = { | ||
let context = OptimizerContext::from_handler_args(handler_args); | ||
let plan_result = gen_batch_plan_by_statement( | ||
&session, | ||
context.into(), | ||
Statement::Query(Box::new(query.clone())), | ||
)?; | ||
gen_batch_plan_fragmenter(&session, plan_result)? | ||
}; | ||
|
||
session | ||
.add_cursor( | ||
cursor_name, | ||
Cursor::new(plan_fragmenter_result, session.clone()).await?, | ||
) | ||
.await?; | ||
Ok(PgResponse::empty_result(StatementType::DECLARE_CURSOR)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use pgwire::pg_response::{PgResponse, StatementType}; | ||
use risingwave_sqlparser::ast::ObjectName; | ||
|
||
use super::RwPgResponse; | ||
use crate::error::Result; | ||
use crate::handler::HandlerArgs; | ||
|
||
pub async fn handle_fetch_cursor( | ||
handler_args: HandlerArgs, | ||
cursor_name: ObjectName, | ||
count: Option<i32>, | ||
) -> Result<RwPgResponse> { | ||
let session = handler_args.session; | ||
let (rows, pg_descs) = session.cursor_next(&cursor_name, count).await?; | ||
Ok(PgResponse::builder(StatementType::FETCH_CURSOR) | ||
.values(rows.into(), pg_descs) | ||
.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.