Repository navigation
Cut dependencies of the SQL parser, and fix its 32-bit and ODR issues - #111952
alexey-milovidov wants to merge 4 commits into
Conversation
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]>
|
Workflow [PR], commit [9f0941a] Summary: ❌
AI ReviewSummaryThis PR removes unnecessary parser dependencies, moves the SQL keyword table to a compile-time representation, fixes the duplicated Findings
Final VerdictChanges required before merge. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
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]>
…eeper.cpp` The path helpers that threw it moved to `ZooKeeperPathUtils.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__) |
There was a problem hiding this comment.
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.
|
|
||
| #include <ctime> | ||
|
|
||
| #if defined (OS_DARWIN) || defined (OS_SUNOS) |
There was a problem hiding this comment.
Let's combine and simplify this block
| /// 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__) |
There was a problem hiding this comment.
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
| 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__) |
| { | ||
| return mapping; | ||
| } | ||
| constexpr bool containsUnderscore(std::string_view value) |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 36/54 (66.67%) · Uncovered code |
|
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. |
… 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.
Groundwork found while trying to build
src/Parserson its own, outside the server. Eachchange 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.hincludedPoco/Util/Application.hand never used it. That header reachesPoco::ThreadPoolandPoco::SignalHandler, andDumpASTNode.his pulled in byInDepthNodeVisitor.h, so over 60 translation units were paying for it.ExpressionListParsers.cppincludedAggregateFunctions/AggregateFunctionFactory.handnever used it either, dragging
IAggregateFunctionand all ofColumnsinto one of theheaviest translation units in the tree.
ParserSystemQuery.cppincluded the whole Keeper client for three path-string helpers.Those move to a new
Common/ZooKeeper/ZooKeeperPathUtils.h, whichZooKeeper.hincludes,so every existing user is unaffected.
A latent ODR violation
ASTSQLSecurity::formatImplwas defined identically in bothASTSQLSecurity.cppandASTCreateQuery.cpp. Linking against a static library never pulls both objects in, so itstayed 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.cppbuilt it at startup: 645 inlinedresize,std::stringassignment andunordered_set::emplacesequences, compiling to a 57 KB constructor - the single largestfunction 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 byKeyword. The two consistency checks the constructor performed becamestatic_asserts, sothey now also run in release builds, where the second one used to be compiled out entirely.
getAllKeyWordsandgetKeyWordSetonly feed system tables and query obfuscation, so theymaterialize on first use.
32-bit correctness (all latent today, since no supported platform is 32-bit)
ASTsasked Boost for auint32_tstored size, which Boost rejects unless that type isstrictly smaller than
size_t. Picksize_twhen there is nothing to shrink.DEFAULT_NATIVE_BINARY_MAX_NUM_ROWSwas auzliteral that does not fit in a 32-bitsize_t, and is compared against aUInt64read off the wire.ProfileEvents::Countwassize_twhileIncrementwasInt64, tripping thestatic_assertthat the two have the same size.itoaandiotaguarded theirlongandsize_toverloads onOS_DARWIN. The actualcondition is that
longis a distinct type from the ones already listed, equally true onany 32-bit platform, so the guard now covers
!defined(__LP64__)as well.CLOCK_MONOTONIC_COARSEis a Linux extension;base/time.henumerated the platformsneeding a substitute one by one and now also has a generic fallback.
base/defines.hand Poco'sPlatform.hlearn about__wasm__, so the headers thatenumerate 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):
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