Skip to content

fix(column): parse Tuple element names that contain a space - #2074

Open
spicysharkco wants to merge 4 commits into
ClickHouse:mainfrom
spicysharkco:fix-tuple-name-with-space
Open

spicysharkco wants to merge 4 commits into
ClickHouse:mainfrom
spicysharkco:fix-tuple-name-with-space

Conversation

@spicysharkco

@spicysharkco spicysharkco commented Oct 16, 2026 •

Copy link
Copy Markdown

Summary

Fixes #2073.

A named Tuple element with a space in its name, such as Tuple(`a b` Int64), made every query on that column fail with unsupported column type "b Int64". (*Tuple).parsesplit each element at its first space, so the name became ``a and the typeb Int64``. The backquote handling from #2046 ran after that split. When the name is backquoted, the element is now split at the first space after the closing backtick, skipping an escaped backtick inside the name. Parentheses and commas inside a backquoted name, as in ``Tuple(price (usd)` Float64)``, no longer end it or mark it as a type.

Covered by a new row in TestTupleParseElementNames and a native and HTTP regression test in tests/issues/issue_2073_test.go. Both fail without the change.

Checklist

  • Unit and integration tests covering the common scenarios were added

Note

Medium Risk
Touches core Tuple type parsing used on every query involving named tuples; behavior change is targeted and heavily tested but affects a frequently used column code path.

Overview
Fixes Tuple type parsing so named elements with spaces, parentheses, or commas in backquoted or double-quoted names no longer break column decoding (e.g. Tuple(`a b` Int64)).

(*Tuple).parse now splits elements at top-level commas via existing indexTopLevelComma, then uses new splitTupleElement to separate name and type. Quoted names are read as a whole (with tupleNameUnescape for server escapes); unquoted names still split on whitespace unless the first token contains ( (typed parameters like Decimal(9, 2)).

Unit coverage in TestTupleParseElementNames is expanded; tests/issues/issue_2073_test.go adds native/HTTP round-trip for tuple maps with spaced names.

Reviewed by Cursor Bugbot for commit aa99677. Bugbot is set up for automated code reviews on this repo. Configure here.

A backquoted element name such as `a b` Int64 was split at its first
space, giving name "`a" and type "b` Int64". When the name is
backquoted, split at the first space after the closing backtick.

Fixes ClickHouse#2073
…mmas

The guard that tells a name from an unnamed type such as
Decimal(10, 2) looked for a parenthesis before the first space, so
`price (usd)` Float64 was read as a type. The element split also
counted brackets and commas inside a backquoted name.

Apply the guard only to unquoted names, and do not split inside a
backquoted name that opens an element.
Comment thread lib/column/tuple.go Resolved
Comment thread tests/issues/issue_2073_test.go Resolved
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Claude review

Fixes Tuple element names that the server backquotes because they contain a space, comma or bracket (e.g. Tuple(`a b` Int64)), which previously failed every query on the column with unsupported column type. The parser change is correct for everything the server actually emits, and the fix is covered by unit rows plus a native and HTTP regression test. Approve.

What I checked:

  • Traced the new name scan and the inName loop with concrete inputs: escaped backtick in the name (`c\` d`), a server-escaped backslash (`a\\b` → a\b), non-ASCII names, several spaces after the closing backtick, a quoted outer name wrapping a nested Tuple, a backtick inside an Enum8 literal, and a quoted-name tuple nested in Map (which already splits with indexTopLevelComma). All resolve to the expected name/type pairs.
  • Byte-indexed scanning in appendElement is safe for UTF-8 since ` and \ never appear as continuation bytes; the rune loop and the byte scan agree on where a name starts (first non-space character).
  • The existing Tuple(` String) row still passes; the only behavior shift is a lone unterminated backtick followed by a second element, which the server never produces.

Minor points (inline, nits only):

  • The new inName loop is a third hand-rolled quote-aware splitter next to indexTopLevelComma (map.go) and splitWithDelimiters (json.go); the Variant and Nested parsers still split on brackets inside a backquoted name, so a Tuple(`z)` Int8) inside a Variant remains broken.
  • The regression test discards the DROP TABLE error in its cleanup.

Blind spots:

  • Could not run go test or the integration suite in this environment (shell restricted to read-only commands); correctness is based on manual tracing of the diff.
  • No database/sql variant of the regression test, though the parser is shared so the std surface takes the same path.

Verdict: ✅ Approve

Inline comments are attached to the relevant lines. This summary updates in place on re-review.

The Tuple parser carried its own copy of the bracket and quote-aware
comma split. indexTopLevelComma in map.go already handles backquoted
names, quoted strings and backslash escapes, so use it and drop the
name and escape state kept here. The existing Tuple rows pass as
before. Because indexTopLevelComma also skips single-quoted strings,
a parenthesis inside a quoted Enum value no longer ends the element;
a test row covers it.
@spicysharkco
spicysharkco requested review from a team and polyglotAI-bot as code owners November 20, 2026 01:41

@chernser chernser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you @spicysharkco !

However please do:

  • simplify name extraction. Task really in getting everything between back quouts and double quotes (back quotes case is missing, btw)
  • extend tests with different case like double escape sequence, double quotes, etc.

Here is also what LLM found:

Redundant + buggy split after nameEnd: Once the loop finds the closing quote at i, the name is already cType[:i+1] and the type is strings.TrimSpace(cType[i+1:]). Searching cType[nameEnd:] for " " afterward fails whenever the separator is \n or \t (such as HTTP pretty-printed types, tested in lib/column/tuple_test.go:42-46) or omitted:
Trace Tuple(a b\nDecimal(10, 2)): nameEnd = 5, strings.Index(cType[5:], " ") skips \nDecimal(10, and matches the space inside Decimal(10, 2). Because nameEnd > 0 bypasses !strings.Contains(cType[:sep], "("), it sets name = "a b\nDecimal(10," and cType = "2)", failing to parse.
Trace Tuple(a b\tInt64) or Tuple(a\tInt64): strings.Index(..., " ") returns -1, leaving the name attached to the type.

Thanks!

splitTupleElement takes a name in backticks or double quotes as the
text between the quotes, unescaped, and the rest of the element as
the type. The second search for a space is gone, so a newline, a tab
or no separator after the closing quote works, and an unquoted name
ends at any whitespace. Inside the quotes a backslash escapes the
next character, and \t, \n, \r, \b, \f and \0 decode to the control
characters the server writes that way.
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.

Named Tuple element with a space in its name fails to parse

3 participants