Skip to content

Added escaping for column name when backup is created#327

Open
EilRoviSoft wants to merge 2 commits into
yandex:mainfrom
EilRoviSoft:dev/mdb-45169
Open

Added escaping for column name when backup is created#327
EilRoviSoft wants to merge 2 commits into
yandex:mainfrom
EilRoviSoft:dev/mdb-45169

Conversation

@EilRoviSoft

@EilRoviSoft EilRoviSoft commented May 27, 2026

Copy link
Copy Markdown
Contributor

Summary by Sourcery

Handle ClickHouse DDL with unescaped reserved keywords during restore to prevent failures when attaching views or tables.

Bug Fixes:

  • Correct backup restore of tables/views whose stored CREATE/ATTACH DDL uses reserved keywords like TOP as identifiers without proper escaping.

Tests:

  • Add unit tests covering DDL fixing for unescaped TOP keyword and ensuring valid statements remain unchanged.

@sourcery-ai

sourcery-ai Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds a preprocessing step to sanitize ClickHouse CREATE/ATTACH statements by backtick-escaping specific unescaped reserved keywords (currently TOP) before executing them during table restore, along with unit tests to validate the behavior and ensure no changes for already valid DDL.

Sequence diagram for ClickhouseCTL.create_table DDL fixing on restore

sequenceDiagram
    participant ClickhouseCTL
    participant _fix_create_statement
    participant CHClient as ClickhouseClient

    ClickhouseCTL->>ClickhouseCTL: create_table(table)
    ClickhouseCTL->>_fix_create_statement: _fix_create_statement(table.create_statement)
    _fix_create_statement-->>ClickhouseCTL: fixed_statement
    ClickhouseCTL->>CHClient: query(fixed_statement)
Loading

File-Level Changes

Change Details Files
Introduce DDL sanitization helper to escape specific unescaped reserved keywords in serialized ClickHouse CREATE/ATTACH statements before execution.
  • Define UNESCAPED_KEYWORDS list seeded with TOP to enumerate problematic reserved identifiers.
  • Implement _fix_create_statement to regex-rewrite occurrences of reserved keywords used as identifiers (e.g., TOP AS c) into backtick-escaped form, logging a warning when a statement is modified.
  • Apply _fix_create_statement to table.create_statement in ClickhouseCTL.create_table before issuing the query to ClickHouse.
ch_backup/clickhouse/control.py
Add unit tests to cover DDL fixing behavior and ensure no changes for valid DDL.
  • Add test that verifies a ClickHouse ATTACH VIEW statement using TOP as an alias is rewritten to use TOP in the SELECT clause.
  • Add test that verifies valid DDL without reserved keyword misuse is left unchanged by _fix_create_statement.
tests/unit/test_control.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@aalexfvk
aalexfvk marked this pull request as ready for review May 28, 2026 07:40

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The logging.warning call in _fix_create_statement uses {!r}-style placeholders, but logging expects %-style formatting; this will log the braces literally, so switch to %r placeholders or explicit str.format before passing the message.
  • Since _fix_create_statement is applied to every create_statement, consider constraining the regex or adding an early guard so that only statements containing the affected keyword patterns (e.g., TOP AS) are processed, reducing the chance of unintended rewrites in other DDL forms.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `logging.warning` call in `_fix_create_statement` uses `{!r}`-style placeholders, but `logging` expects `%`-style formatting; this will log the braces literally, so switch to `%r` placeholders or explicit `str.format` before passing the message.
- Since `_fix_create_statement` is applied to every `create_statement`, consider constraining the regex or adding an early guard so that only statements containing the affected keyword patterns (e.g., ` TOP AS `) are processed, reducing the chance of unintended rewrites in other DDL forms.

## Individual Comments

### Comment 1
<location path="tests/unit/test_control.py" line_range="58-67" />
<code_context>
     assert _parse_version(version) == expected
+
+
+def test_fix_create_statement_top_keyword():
+    broken = (
+        "ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
+        "(c String) AS WITH A AS (SELECT TOP AS c FROM default.test_3) "
+        "SELECT c FROM A"
+    )
+    fixed = _fix_create_statement(broken)
+
+    assert "`TOP`" in fixed
+    assert "SELECT `TOP` AS c" in fixed
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen assertions by checking the full fixed statement rather than only substrings.

Substring checks would still pass if `_fix_create_statement` made other unintended changes to the DDL. Define a full `expected_fixed` statement and assert `fixed == expected_fixed` so the test fully specifies the transformation and better catches regressions.

```suggestion
def test_fix_create_statement_top_keyword() -> None:
    broken = (
        "ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
        "(c String) AS WITH A AS (SELECT TOP AS c FROM default.test_3) "
        "SELECT c FROM A"
    )
    expected_fixed = (
        "ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
        "(c String) AS WITH A AS (SELECT `TOP` AS c FROM default.test_3) "
        "SELECT c FROM A"
    )

    fixed = _fix_create_statement(broken)

    assert fixed == expected_fixed
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +58 to +67
def test_fix_create_statement_top_keyword():
broken = (
"ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
"(c String) AS WITH A AS (SELECT TOP AS c FROM default.test_3) "
"SELECT c FROM A"
)
fixed = _fix_create_statement(broken)

assert "`TOP`" in fixed
assert "SELECT `TOP` AS c" in fixed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (testing): Strengthen assertions by checking the full fixed statement rather than only substrings.

Substring checks would still pass if _fix_create_statement made other unintended changes to the DDL. Define a full expected_fixed statement and assert fixed == expected_fixed so the test fully specifies the transformation and better catches regressions.

Suggested change
def test_fix_create_statement_top_keyword():
broken = (
"ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
"(c String) AS WITH A AS (SELECT TOP AS c FROM default.test_3) "
"SELECT c FROM A"
)
fixed = _fix_create_statement(broken)
assert "`TOP`" in fixed
assert "SELECT `TOP` AS c" in fixed
def test_fix_create_statement_top_keyword() -> None:
broken = (
"ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
"(c String) AS WITH A AS (SELECT TOP AS c FROM default.test_3) "
"SELECT c FROM A"
)
expected_fixed = (
"ATTACH VIEW test_4 UUID '1c465905-164d-400b-8a39-ff382c92fb81' "
"(c String) AS WITH A AS (SELECT `TOP` AS c FROM default.test_3) "
"SELECT c FROM A"
)
fixed = _fix_create_statement(broken)
assert fixed == expected_fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant