Skip to content

Cut dependencies of the SQL parser, and fix its 32-bit and ODR issues - #111952

Closed
alexey-milovidov wants to merge 4 commits into
masterfrom
parser-cut-dependencies
Closed

alexey-milovidov wants to merge 4 commits into
masterfrom
parser-cut-dependencies

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Groundwork found while trying to build src/Parsers on its own, outside the server. Each
change stands alone as a fix or a build-time improvement; the experiment that motivated them
is not part of this PR.

Dependencies the parser does not need

  • DumpASTNode.h included Poco/Util/Application.h and never used it. That header reaches
    Poco::ThreadPool and Poco::SignalHandler, and DumpASTNode.h is pulled in by
    InDepthNodeVisitor.h, so over 60 translation units were paying for it.
  • ExpressionListParsers.cpp included AggregateFunctions/AggregateFunctionFactory.h and
    never used it either, dragging IAggregateFunction and all of Columns into one of the
    heaviest translation units in the tree.
  • ParserSystemQuery.cpp included the whole Keeper client for three path-string helpers.
    Those move to a new Common/ZooKeeper/ZooKeeperPathUtils.h, which ZooKeeper.h includes,
    so every existing user is unaffected.

A latent ODR violation

ASTSQLSecurity::formatImpl was defined identically in both ASTSQLSecurity.cpp and
ASTCreateQuery.cpp. Linking against a static library never pulls both objects in, so it
stayed invisible; it becomes a duplicate-symbol error as soon as the objects are linked
directly. Removed the copy in ASTCreateQuery.cpp.

The keyword table

CommonParsers.cpp built it at startup: 645 inlined resize, std::string assignment and
unordered_set::emplace sequences, compiling to a 57 KB constructor - the single largest
function in the parser - and costing 645 string allocations plus a hash-set build in every
process. It is a constexpr std::array<std::string_view, N> now, indexed directly by
Keyword. The two consistency checks the constructor performed became static_asserts, so
they now also run in release builds, where the second one used to be compiled out entirely.
getAllKeyWords and getKeyWordSet only feed system tables and query obfuscation, so they
materialize on first use.

32-bit correctness (all latent today, since no supported platform is 32-bit)

  • ASTs asked Boost for a uint32_t stored size, which Boost rejects unless that type is
    strictly smaller than size_t. Pick size_t when there is nothing to shrink.
  • DEFAULT_NATIVE_BINARY_MAX_NUM_ROWS was a uz literal that does not fit in a 32-bit
    size_t, and is compared against a UInt64 read off the wire.
  • ProfileEvents::Count was size_t while Increment was Int64, tripping the
    static_assert that the two have the same size.
  • itoa and iota guarded their long and size_t overloads on OS_DARWIN. The actual
    condition is that long is a distinct type from the ones already listed, equally true on
    any 32-bit platform, so the guard now covers !defined(__LP64__) as well.
  • CLOCK_MONOTONIC_COARSE is a Linux extension; base/time.h enumerated the platforms
    needing a substitute one by one and now also has a generic fallback.

base/defines.h and Poco's Platform.h learn about __wasm__, so the headers that
enumerate supported platforms stop erroring out on it.

Testing

No behaviour change is intended, so no new tests. The keyword table is the one change with
real risk, and it is covered by the static_asserts (all 645 keywords pass at compile time)
plus the existing parser tests and system.keywords.

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Reduced the dependencies and the code size of the SQL parser. The table of SQL keywords is now built at compile time instead of on every server start.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

These came out of an experiment in building `src/Parsers` on its own, outside the server.
Each one stands alone as a fix or a build-time improvement; none of them depends on that
experiment, and the experiment itself is not part of this change.

Dependencies the parser does not need:

* `DumpASTNode.h` included `Poco/Util/Application.h` and never used it. That header reaches
  `Poco::ThreadPool` and `Poco::SignalHandler`, and `DumpASTNode.h` is pulled in by
  `InDepthNodeVisitor.h`, so over 60 translation units were paying for it.
* `ExpressionListParsers.cpp` included `AggregateFunctions/AggregateFunctionFactory.h` and
  never used it either, dragging `IAggregateFunction` and all of `Columns` into one of the
  heaviest translation units in the tree.
* `ParserSystemQuery.cpp` included the whole Keeper client for three path-string helpers.
  Those move to a new `Common/ZooKeeper/ZooKeeperPathUtils.h`, which `ZooKeeper.h` includes,
  so every existing user is unaffected.

`ASTSQLSecurity::formatImpl` was defined identically in both `ASTSQLSecurity.cpp` and
`ASTCreateQuery.cpp`. Linking against a static library never pulls both objects in, so the
ODR violation stayed latent; it becomes a duplicate-symbol error as soon as the objects are
linked directly. Removed the copy in `ASTCreateQuery.cpp`.

`CommonParsers.cpp` built the keyword table at startup: 645 inlined `resize`, `std::string`
assignment and `unordered_set::emplace` sequences, which compiled to a 57 KB constructor -
the single largest function in the parser - and cost 645 string allocations plus a hash-set
build in every process. It is a `constexpr std::array<std::string_view, N>` now, indexed
directly by `Keyword`. The two consistency checks the constructor performed became
`static_assert`s, so they now also run in release builds, where the second one used to be
compiled out entirely. `getAllKeyWords` and `getKeyWordSet` only feed system tables and query
obfuscation, so they materialize on first use.

32-bit correctness, all of it latent today:

* `ASTs` asked Boost for a `uint32_t` stored size, which Boost rejects unless that type is
  strictly smaller than `size_t`. Pick `size_t` when there is nothing to shrink.
* `DEFAULT_NATIVE_BINARY_MAX_NUM_ROWS` was a `uz` literal that does not fit in a 32-bit
  `size_t`, and is compared against a `UInt64` read off the wire.
* `ProfileEvents::Count` was `size_t` while `Increment` was `Int64`, tripping the
  `static_assert` that the two have the same size.
* `itoa` and `iota` guarded their `long` and `size_t` overloads on `OS_DARWIN`. The actual
  condition is that `long` is a distinct type from the ones already listed, which is equally
  true on any 32-bit platform, so the guard now covers `!defined(__LP64__)` as well.
* `CLOCK_MONOTONIC_COARSE` is a Linux extension. `base/time.h` enumerated the platforms
  needing a substitute one by one; it now also has a generic fallback.

`base/defines.h` and Poco's `Platform.h` learn about `__wasm__`, so that the headers which
enumerate supported platforms stop erroring out on it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9f0941a]

Summary: ❌

job_name test_name status info comment
Stress test (arm_debug) FAIL
Logical error: Inconsistent KeyCondition behavior (STID: 5182-2f27) FAIL cidb
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb

AI Review

Summary

This PR removes unnecessary parser dependencies, moves the SQL keyword table to a compile-time representation, fixes the duplicated ASTSQLSecurity::formatImpl definition, and tries to unblock latent 32-bit / WebAssembly build issues. The dependency cleanup and parser changes look sound, but one of the new 32-bit fixes is still wrong on common ILP32 ABIs: the added size_t iota instantiations collide with the existing UInt32 ones and keep those builds broken.

Findings

⚠️ Majors

  • [src/Common/iota.cpp:38] The new !defined(__LP64__) guard is correct for the long overloads, but it is too broad for size_t. On ordinary ILP32 targets where size_t is just unsigned int, the new template void iota(size_t *, ...) specialization is the same specialization as UInt32, so this translation unit fails with a duplicate explicit-instantiation error. The size_t path needs a stricter ABI/type check than the long path.
Final Verdict

Changes required before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Jul 26, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Approved. All LGTM.

Removing the unused `Poco/Util/Application.h` from `Parsers/DumpASTNode.h` also removed the
transitive `Poco/Util/AbstractConfiguration.h` that these three visitors were relying on, so
`dbms` stopped building even though `clickhouse_parsers` still did. Include it where it is used.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Comment thread src/Common/iota.cpp
template void iota(UInt32 * begin, size_t count, UInt32 first_value);
template void iota(UInt64 * begin, size_t count, UInt64 first_value);
#if defined(OS_DARWIN)
#if defined(OS_DARWIN) || !defined(__LP64__)

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.

On ordinary ILP32 targets this guard is too broad for the size_t specializations. long is distinct from Int32, but size_t is often just unsigned int, so the new template void iota(size_t *, ...) instantiation collapses to the same specialization as UInt32 and the TU fails with a duplicate explicit-instantiation error. That means the PR's 32-bit fix still leaves common 32-bit ABIs broken. The size_t overloads need a narrower condition than the long ones, or a type check that size_t is actually distinct from UInt32/UInt64.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks valid

@Algunenano Algunenano self-assigned this Jul 27, 2026
Comment thread base/base/time.h

#include <ctime>

#if defined (OS_DARWIN) || defined (OS_SUNOS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's combine and simplify this block

Comment thread base/base/itoa.cpp
/// On platforms where `int64_t` is not `long` (Darwin) and on 32-bit platforms where
/// `int32_t` is not `long` either (WebAssembly), plain `long` is a distinct type that is
/// not covered by the list above, and calls with a `long` argument would be ambiguous.
#if defined(OS_DARWIN) || !defined(__LP64__)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can introduce a macro such as LONG_IS_A_DISTINCT_TYPE or macros if we need to cover size_t too, and use it everywhere instead of extending the condition everywhere

Comment thread src/Common/iota.cpp
template void iota(UInt32 * begin, size_t count, UInt32 first_value);
template void iota(UInt64 * begin, size_t count, UInt64 first_value);
#if defined(OS_DARWIN)
#if defined(OS_DARWIN) || !defined(__LP64__)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks valid

{
return mapping;
}
constexpr bool containsUnderscore(std::string_view value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

❤️

@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.30% +0.10%
Functions 91.90% 92.00% +0.10%
Branches 78.30% 78.40% +0.10%

Changed lines: Changed C/C++ lines covered: 36/54 (66.67%) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Superseded by #111971, which contains these two commits unchanged as its first two, plus the rest of the standalone-parser work. Closing here as that PR's description says.

alexey-milovidov added a commit that referenced this pull request Jul 28, 2026
… macros

Review feedback on the predecessor PR
#111952 (`Algunenano`,
`clickhouse-gh`), which applies verbatim to this branch:

* The `size_t` explicit instantiations in `iota` were guarded by
  `defined(OS_DARWIN) || !defined(__LP64__)`, which is too broad. On an
  ordinary ILP32 target `size_t` is `unsigned int`, i.e. exactly `UInt32`,
  so the `size_t` instantiation collapses onto the `UInt32` one and the
  translation unit fails with a duplicate explicit instantiation. `size_t`
  is a distinct type only where it is `unsigned long` - Darwin and
  WebAssembly - so it needs a narrower condition than `long` does.
  A pair of `static_assert`s now pins the assumption in both directions,
  so a platform where it stops holding fails to compile with a clear
  message instead of silently losing or duplicating the instantiation.

* Instead of repeating the platform condition at every use site, define
  `LONG_IS_A_DISTINCT_TYPE` and `SIZE_T_IS_A_DISTINCT_TYPE` in
  `base/defines.h` and use them in `itoa` and `iota`.

* Combine the `CLOCK_MONOTONIC_COARSE` fallbacks in `base/time.h` into a
  single block: the Darwin and SunOS cases are subsumed by the generic
  fallback to the precise clock, so only FreeBSD's `CLOCK_MONOTONIC_FAST`
  needs a case of its own.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants