Skip to content

fix: preserve TRIM and POSITION syntax in CTAS - #29359

Merged
XuPeng-SH merged 2 commits into
mainfrom
codex/issue-29330-ctas-special-functions
Sep 25, 2026
Merged

XuPeng-SH merged 2 commits into
mainfrom
codex/issue-29330-ctas-special-functions

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • API-change
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #29330

What this PR does / why we need it:

CTAS reparses a generated INSERT ... SELECT. Formatting TRIM(BOTH|LEADING|TRAILING remstr FROM str) under CTAS's string-quoting options incorrectly quoted the direction keyword, and POSITION(substr IN str) was formatted as comma syntax. Both generated invalid SQL despite valid original SELECTs.

Render TRIM direction as a keyword and POSITION with IN. Parser round-trip tests cover default/removal/directional TRIM and column/literal POSITION in ordinary and CTAS formatting modes. A real embedded-MO SQL integration test creates CTAS tables for all five reported forms and verifies their materialized values.

Validation: make cgo; focused and full pkg/sql/parsers/tree tests; TestCTASSpecialFunctionSyntax through the SQL frontend; go vet ./pkg/sql/parsers/tree; git diff --check. GPT-6 medium design and final code review completed.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review — no blocking findings; approval recommendation

Reviewed head 563bd82c7cde9e049d67857711cf62577194bacc, merge-base a61e7710e26c7d7613e2aa19913460ff9b7e2606, against freshly verified main (e914d987641c266162eb607ec072fdb5a2b06da9). All three changed files and every changed hunk were reviewed. This is an ordinary bug fix; a separate design document is unnecessary.

Contract and engineering review

  • Traced MySQL grammar → FuncExpr.Format / trimExprsFormat → CTAS SQL construction → execution of the generated INSERT/SELECT. TRIM directions are parser-created NumVal nodes from the closed BOTH/LEADING/TRAILING grammar. Emitting their keyword text directly is appropriate; source/removal expressions retain normal formatting. POSITION preserves its IN separator, argument order, and parentheses. The !IsGeneric guard preserves whitespace-sensitive generic calls.
  • The change is localized and proportionate. It adds a fixed name/arity check during formatting, with no new per-row execution work, I/O, retained state, or synchronization. No additional abstraction or distributed protocol is warranted. This is source analysis, not a performance benchmark.
  • Tests cover distinct contracts: AST syntax/argument round trips and materialized CTAS results. Reviewed cleanup after failure, independent cleanup context, database/connection ownership, shared fixture reuse and termination. No new unbounded growth or asynchronous lifecycle was introduced. No API/wire/storage-format or authorization contract changes were found.

Evidence

  • Exact-head CI run 35996059270 is green for the required gate, Ubuntu UT, coverage, arm64 SCA, shared build and both active multi-CN BVT jobs. Skipped jobs are excluded from this evidence.
  • Verified the Ubuntu UT checkout SHA and artifact selection: TestCTASSpecialFunctionSyntax passed (37.79 s), including all five named result-checking subtests; the owning integration package passed (242.30 s). Reused that execution instead of starting another cluster locally.
  • Built native artifacts with provenance validation and ran the PR's formatter regression plus 14 review-only boundary cases under both CTAS and ordinary quoting: PASS (pkg/sql/parsers/tree, 0.023 s). Cases included direction-only TRIM, uppercase keywords, empty/NULL operands, nested calls, parenthesized arithmetic, embedded quotes, and generic TRIM/POSITION. Review probes were supplied through a Go overlay; the checkout is clean.
  • Examined the earlier coverage failure: pkg/tests/issues/isolated reached its 10-minute package timeout, with TestSpeedupAbortAllTxn running for only 18 s at the alarm. The current rerun passed. This log does not establish a TRIM/POSITION regression or a 10-minute hang in that individual test.

Existing limitation found during boundary checks

The CTAS double-quote formatter duplicates backslashes: parsing select trim('\\a\\'), formatting with WithQuoteString(true), and reparsing changes the stored string value. The ordinary single-quote mode preserves it. A separate diagnostic fails identically on this head and with the merge-base expr.go restored via overlay. The cause is the unchanged NumVal.Format → FormatString → %q path; this existing literal-escaping defect warrants separate tracking. The approval recommendation is limited to the syntax fix in this PR.

One non-blocking test suggestion is attached inline. GitHub review event is COMMENT because the authenticated account owns this PR; the review recommendation is APPROVE.

}{
{"trim default", "select trim(b) as t from s", "trim(`b`)", "trim(`b`)"},
{"trim removal", "select trim(a from b) as t from s", "trim(`a` from `b`)", "trim(`a` from `b`)"},
{"trim both", "select trim(both a from b) as t from s", "trim(both `a` from `b`)", "trim(both `a` from `b`)"},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Non-blocking test coverage: please consider adding trim(both from b), trim(leading from b), and trim(trailing from b) to this table. They exercise the distinct case "2" branch changed by this PR; the existing direction cases exercise case "3". I verified all three missing forms under both quoting modes with a review-only probe and they pass, so this is regression protection rather than a demonstrated correctness defect.

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CTAS cannot reconstruct TRIM and POSITION special syntax

2 participants