-
Notifications
You must be signed in to change notification settings - Fork 7
Stream binary scans without coroutines #165
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
Open
iskakaushik
wants to merge
13
commits into
main
Choose a base branch
from
streaming-scan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aab762e
Stream query results to bound memory during scans
iskakaushik f0b8788
Fix stale comments referencing old streaming design
iskakaushik b452dab
Fix CI: remove unused variable, apply pgindent formatting
iskakaushik f6442fb
Remove unused variable in binary_streaming_fetch_row
iskakaushik 7ce8bdb
Address review feedback on streaming scan PR
iskakaushik 698c470
Extract shared cursor_create helper for HTTP and binary paths
iskakaushik 1a75e36
Remove unnecessary aside from streaming HTTP comment
iskakaushik 3900f18
Teach Makefile to automatically clone minicoro
theory 6b183b4
Run `make indent`
theory 49b27b5
Split streaming scan code from buffered paths
iskakaushik 38b0759
Make binary streaming errors const-correct
iskakaushik 5889363
Stream binary scans without coroutines
iskakaushik 38600b9
Limit streaming scans to the binary driver
iskakaushik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| [submodule "clickhouse-cpp"] | ||
| path = vendor/clickhouse-cpp | ||
| url = https://github.com/ClickHouse/clickhouse-cpp.git | ||
| url = git@github.com:iskakaushik/clickhouse-cpp.git |
This file contains hidden or 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 hidden or 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 hidden or 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,272 @@ | ||
| #include <memory> | ||
| #include <new> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| #include <clickhouse/client.h> | ||
| #include <clickhouse/query.h> | ||
|
|
||
| extern "C" | ||
| { | ||
|
|
||
| #include "postgres.h" | ||
| #include "internal.h" | ||
| #include "engine.h" | ||
|
|
||
| } | ||
|
|
||
| #include "binary_internal.hh" | ||
|
|
||
| using namespace clickhouse; | ||
|
|
||
| static const char kBinaryStreamingCanceled[] = "query was canceled"; | ||
| static const char kBinaryStreamingOom[] = "out of memory"; | ||
|
|
||
| struct ch_binary_streaming_state | ||
| { | ||
| /* Current block returned by clickhouse-cpp. */ | ||
| std::optional<Block> current_block; | ||
| size_t current_row = 0; | ||
| bool have_block = false; | ||
| bool done = false; | ||
|
|
||
| std::unique_ptr<Oid[]> coltypes; | ||
| std::unique_ptr<Datum[]> values; | ||
| std::unique_ptr<bool[]> nulls; | ||
| size_t columns_count = 0; | ||
|
|
||
| std::optional<std::string> error; | ||
| bool (*check_cancel) (void) = nullptr; | ||
|
|
||
| Client *client = nullptr; | ||
| std::string sql; | ||
| QuerySettings settings; | ||
| QueryParams params; | ||
|
|
||
| void | ||
| SetError(const char *message) | ||
| { | ||
| if (error) | ||
| return; | ||
| error.emplace(message ? message : kBinaryStreamingOom); | ||
| } | ||
|
|
||
| const char * | ||
| GetError() const | ||
| { | ||
| return error ? error->c_str() : nullptr; | ||
| } | ||
| }; | ||
|
|
||
| static bool | ||
| binary_streaming_fill_block(ch_binary_streaming_state * st) | ||
| { | ||
| std::optional<Block> block; | ||
|
|
||
| for (;;) | ||
| { | ||
| try | ||
| { | ||
| if (st->check_cancel && st->check_cancel()) | ||
| { | ||
| st->SetError(kBinaryStreamingCanceled); | ||
| st->done = true; | ||
| return false; | ||
| } | ||
|
|
||
| block = st->client->ReceiveSelectBlock(); | ||
| } | ||
| catch (const std::exception & e) | ||
| { | ||
| st->SetError(e.what()); | ||
| st->done = true; | ||
| return false; | ||
| } | ||
|
|
||
| if (!block) | ||
| { | ||
| try | ||
| { | ||
| st->client->EndSelect(); | ||
| } | ||
| catch (const std::exception & e) | ||
| { | ||
| st->SetError(e.what()); | ||
| } | ||
| st->current_block.reset(); | ||
| st->have_block = false; | ||
| st->done = true; | ||
| return false; | ||
| } | ||
|
|
||
| /* Match the old callback path, which ignored zero-column blocks. */ | ||
| if (block->GetColumnCount() == 0) | ||
| continue; | ||
|
|
||
| if (st->columns_count != 0 && | ||
| block->GetColumnCount() != st->columns_count) | ||
| { | ||
| st->SetError("columns mismatch in blocks"); | ||
| st->done = true; | ||
| return false; | ||
| } | ||
|
|
||
| st->current_block = std::move(block); | ||
| st->columns_count = st->current_block->GetColumnCount(); | ||
| st->have_block = true; | ||
| st->current_row = 0; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| extern "C" | ||
| { | ||
|
|
||
| ch_binary_streaming_state * | ||
| ch_binary_begin_streaming(ch_binary_connection_t * conn, | ||
| const ch_query * query, | ||
| bool (*check_cancel) (void)) | ||
| { | ||
| ch_binary_streaming_state *st; | ||
|
|
||
| st = new (std::nothrow) ch_binary_streaming_state(); | ||
| if (!st) | ||
| return NULL; | ||
|
|
||
| st->check_cancel = check_cancel; | ||
| st->client = (Client *) conn->client; | ||
| st->sql = query->sql; | ||
| st->settings = ch_binary_settings(query); | ||
| st->params = ch_binary_params(query); | ||
|
|
||
| try | ||
| { | ||
| st->client->BeginSelect(clickhouse::Query(st->sql) | ||
| .SetQuerySettings(st->settings) | ||
| .SetParams(st->params)); | ||
| } | ||
| catch (const std::exception & e) | ||
| { | ||
| st->SetError(e.what()); | ||
| st->done = true; | ||
| return st; | ||
| } | ||
|
|
||
| (void) binary_streaming_fill_block(st); | ||
|
|
||
| return st; | ||
| } | ||
|
|
||
| bool | ||
| ch_binary_fetch_block(ch_binary_streaming_state * st) | ||
| { | ||
| if (!st || st->done) | ||
| return false; | ||
| if (st->have_block) | ||
| return true; | ||
|
|
||
| return binary_streaming_fill_block(st); | ||
| } | ||
|
|
||
| bool | ||
| ch_binary_streaming_read_row(ch_binary_streaming_state * st) | ||
| { | ||
| Block *block; | ||
| size_t row_count; | ||
|
|
||
| if (!st || !st->have_block || !st->current_block) | ||
| return false; | ||
|
|
||
| block = &*st->current_block; | ||
| row_count = block->GetRowCount(); | ||
| if (st->current_row >= row_count) | ||
| { | ||
| st->have_block = false; | ||
| return false; | ||
| } | ||
|
|
||
| if (!st->coltypes && st->columns_count > 0) | ||
| { | ||
| st->coltypes.reset(new (std::nothrow) Oid[st->columns_count]); | ||
| st->values.reset(new (std::nothrow) Datum[st->columns_count]); | ||
| st->nulls.reset(new (std::nothrow) bool[st->columns_count]); | ||
| if (!st->coltypes || !st->values || !st->nulls) | ||
| { | ||
| st->SetError(kBinaryStreamingOom); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| for (size_t i = 0; i < st->columns_count; i++) | ||
| { | ||
| st->values[i] = ch_binary_make_datum((*block)[i], st->current_row, | ||
| &st->coltypes[i], &st->nulls[i]); | ||
| } | ||
| } | ||
| catch (const std::exception & e) | ||
| { | ||
| st->SetError(e.what()); | ||
| return false; | ||
| } | ||
|
|
||
| st->current_row++; | ||
| return true; | ||
| } | ||
|
|
||
| size_t | ||
| ch_binary_streaming_columns(ch_binary_streaming_state * st) | ||
| { | ||
| return st ? st->columns_count : 0; | ||
| } | ||
|
|
||
| Datum | ||
| ch_binary_streaming_value(ch_binary_streaming_state * st, size_t col, | ||
| Oid * valtype, bool * is_null) | ||
| { | ||
| if (!st || col >= st->columns_count) | ||
| { | ||
| *is_null = true; | ||
| *valtype = InvalidOid; | ||
| return (Datum) 0; | ||
| } | ||
|
|
||
| *valtype = st->coltypes[col]; | ||
| *is_null = st->nulls[col]; | ||
| return st->values[col]; | ||
| } | ||
|
|
||
| const char * | ||
| ch_binary_streaming_error(ch_binary_streaming_state * st) | ||
| { | ||
| return st ? st->GetError() : NULL; | ||
| } | ||
|
|
||
| void | ||
| ch_binary_end_streaming(ch_binary_streaming_state * st) | ||
| { | ||
| if (!st) | ||
| return; | ||
|
|
||
| try | ||
| { | ||
| if (st->client) | ||
| st->client->EndSelect(); | ||
| } | ||
| catch (const std::exception &) | ||
| { | ||
| try | ||
| { | ||
| if (st->client) | ||
| st->client->ResetConnection(); | ||
| } | ||
| catch (const std::exception &) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| delete st; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I couldn't figure out how to get clang-format not to do that. You can ignore it.