-
Notifications
You must be signed in to change notification settings - Fork 0
fix(schema): quote column names atomically + feat(clickhouse): bulk-insert builder #13
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
lohanidamodar
wants to merge
5
commits into
main
Choose a base branch
from
feat/clickhouse-nested-column-quoting
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
71bf687
feat(quoting): add quoteLiteral() for atomic identifiers
lohanidamodar d036e0b
fix(schema): use quoteLiteral() when emitting column names in CREATE/…
lohanidamodar 19e02cf
feat(clickhouse): add bulk-insert builder for FORMAT envelope payloads
lohanidamodar b26baf9
refactor(clickhouse): rationalize insertFormat/bulkInsert boundary
lohanidamodar a0eb584
refactor(clickhouse): drop builder state mutation in bulkInsert(); do…
lohanidamodar 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
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,117 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Query\Builder\ClickHouse; | ||
|
|
||
| use Utopia\Query\Exception\ValidationException; | ||
|
|
||
| /** | ||
| * ClickHouse bulk-ingest format identifiers. | ||
| * | ||
| * The values map 1:1 to the names ClickHouse accepts after the `FORMAT` | ||
| * keyword in an `INSERT INTO <table> FORMAT <name>` envelope. Each case | ||
| * knows how to serialize a row iterable into the request body that | ||
| * ClickHouse expects for that format. | ||
| */ | ||
| enum Format: string | ||
| { | ||
| case JSONEachRow = 'JSONEachRow'; | ||
| case TabSeparated = 'TabSeparated'; | ||
|
|
||
| /** | ||
| * Serialize an iterable of associative rows into the body payload for | ||
| * this format. Columns are derived from the first row; subsequent rows | ||
| * use the same column ordering. An empty iterable yields an empty | ||
| * string — ClickHouse accepts an empty body as a zero-row insert. | ||
| * | ||
| * @param iterable<array<string, mixed>> $rows | ||
| * @param list<string>|null $columns Optional explicit column ordering. When null, derived from the first row. | ||
| */ | ||
| public function serialize(iterable $rows, ?array $columns = null): string | ||
| { | ||
| return match ($this) { | ||
| self::JSONEachRow => $this->serializeJsonEachRow($rows, $columns), | ||
| self::TabSeparated => $this->serializeTabSeparated($rows, $columns), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param iterable<array<string, mixed>> $rows | ||
| * @param list<string>|null $columns | ||
| */ | ||
| private function serializeJsonEachRow(iterable $rows, ?array $columns): string | ||
| { | ||
| $lines = []; | ||
| foreach ($rows as $row) { | ||
| if ($columns !== null) { | ||
| $ordered = []; | ||
| foreach ($columns as $col) { | ||
| $ordered[$col] = $row[$col] ?? null; | ||
| } | ||
| $row = $ordered; | ||
| } | ||
|
|
||
| $lines[] = \json_encode( | ||
| (object) $row, | ||
| JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, | ||
| ); | ||
| } | ||
|
|
||
| return \implode("\n", $lines); | ||
| } | ||
|
|
||
| /** | ||
| * @param iterable<array<string, mixed>> $rows | ||
| * @param list<string>|null $columns | ||
| */ | ||
| private function serializeTabSeparated(iterable $rows, ?array $columns): string | ||
| { | ||
| $lines = []; | ||
| foreach ($rows as $row) { | ||
| $values = []; | ||
|
|
||
| if ($columns === null) { | ||
| foreach ($row as $value) { | ||
| $values[] = $this->escapeTabSeparatedValue($value); | ||
| } | ||
| } else { | ||
| foreach ($columns as $col) { | ||
| $values[] = $this->escapeTabSeparatedValue($row[$col] ?? null); | ||
| } | ||
| } | ||
|
|
||
| $lines[] = \implode("\t", $values); | ||
| } | ||
|
|
||
| return \implode("\n", $lines); | ||
| } | ||
|
|
||
| private function escapeTabSeparatedValue(mixed $value): string | ||
| { | ||
| if ($value === null) { | ||
| return '\\N'; | ||
| } | ||
|
|
||
| if (\is_bool($value)) { | ||
| return $value ? '1' : '0'; | ||
| } | ||
|
|
||
| if (\is_int($value) || \is_float($value)) { | ||
| return (string) $value; | ||
| } | ||
|
|
||
| if (! \is_string($value)) { | ||
| if (\is_object($value) && \method_exists($value, '__toString')) { | ||
| $value = (string) $value; | ||
| } else { | ||
| throw new ValidationException('TabSeparated values must be scalar, null, or stringable. Received: ' . \get_debug_type($value)); | ||
| } | ||
| } | ||
|
|
||
| return \strtr($value, [ | ||
| '\\' => '\\\\', | ||
| "\t" => '\\t', | ||
| "\n" => '\\n', | ||
| "\r" => '\\r', | ||
| ]); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.