Skip to content

test: compare the built-in SQL handling with the official lexer - #1072

Closed
ccoltx wants to merge 1 commit into
lexer/7-docs-and-examplesfrom
lexer/8-compare-with-official-lexer
Closed

ccoltx wants to merge 1 commit into
lexer/7-docs-and-examplesfrom
lexer/8-compare-with-official-lexer

Conversation

@ccoltx

@ccoltx ccoltx commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Tracking issue: #1074. Stack: #1065 → #1066 → #1067 → #1068 → #1069 → #1070 → #1071 → #1072

Part 8 of the lexer stack. Depends on #1071. Tests only, no behavior change.

Why

The client's built-in SQL handling is hand-written, and every fix to it (#1031, #972, …) risks missing a rule of ClickHouse's real lexer. This PR turns @clickhouse/lexer into a reference for that code, so a change to the built-in handling gets immediate feedback in the unit suite, without a server.

What

  • __tests__/utils/sql_corpus.ts:

  • New unit/sql_rewriting_vs_lexer.test.ts (no server): for every corpus query, compares what query() and command()/exec() would send, in each mode.

    • Two rewrites count as the same when the official lexer reads them as the same tokens, ignoring whitespace and comments.
    • It fails when a query differs and isn't listed, showing both outputs. It also fails when a listed query no longer differs, telling you to remove the entry.
  • integration/sql_lexer.test.ts: the same corpus, judged by the server.

    • The expected answer is the original query sent through exec() with default_format, so no FORMAT clause is appended to it.
    • The lexer mode must always match it. The built-in mode must match for every query that isn't a known difference.
  • Existing tests now run in both modes:

    • all of select.test.ts (16 tests per mode);
    • the trailing-semicolon blocks of node_exec.test.ts and web_exec.test.ts.

    Tests that check exact SQL on the default path (tracing, query log) are unchanged.

What the feedback looks like

To check it, I temporarily swapped PR #1031's scanner into sql/legacy.ts. The unit test then:

Testing

  • Unit suites: Node 601 passed, browser 453 passed. The comparison test (100 cases) runs in both.
  • Integration suites: Node 385 passed (three consecutive full runs), web 268 passed.
  • Typecheck, lint and Prettier pass.
  • One case was flaky at first: ... UNION ALL SELECT 2 returns rows in no fixed order. The corpus now uses identical rows.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Adds a shared corpus of tricky queries and checks the built-in,
hand-written SQL handling against @clickhouse/lexer:

- a unit test, with no server, that compares what each would send by
  the lexer's tokens, with an explicit list of known differences that
  fails when a difference appears or disappears;
- an integration test where the server judges both modes against its
  answer for the original query.

select.test.ts and the exec() trailing-semicolon tests now run with
and without sql_lexer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ccoltx
ccoltx force-pushed the lexer/8-compare-with-official-lexer branch from 8f50edf to 91967ab Compare October 21, 2026 18:43
kavirajk pushed a commit that referenced this pull request Oct 22, 2026
…rly (#1076)

## Summary

Runs the ClickHouse Cloud integration tests after a PR is merged, or on
request, instead of on every push to every PR. Also runs them one job at
a time. This follows what
[clickhouse-go](https://github.com/ClickHouse/clickhouse-go/blob/main/.github/workflows/run-tests-cloud.yml)
does against the same Cloud service.

### Why

The Cloud jobs use one ClickHouse Cloud service, which is shared with
other client repositories (clickhouse-go uses the same
`INTEGRATIONS_TEAM_TESTS_CLOUD_HOST_SMT_PROD` secret). On Cloud, every
test file creates its own database, and `clean-up.yml` only drops
databases older than 60 minutes.

Each push to a PR started six Cloud jobs at once. Pushing a stack of
eight PRs (#1065 to #1072) twice within 15 minutes filled the service:

```
Too many databases. The limit (max_database_num_to_throw) is set to 2800,
the current number of databases is 2800. (code 725)
```

From then on, every Cloud job, including #1075's, failed during setup
before running any tests. This likely affects the other repositories
that use the same service too.

### What

- **New `tests-cloud.yml`** with the Node.js and Web Cloud jobs, which
are removed from `tests-node.yml` and `tests-web.yml`. It runs:
- after a merge (push to `main` or `release`, with the same path filters
as before), on the daily schedule, and on `workflow_dispatch`;
- on a PR only when a maintainer adds the **`tests:run-cloud`** label. A
final job removes the label, so adding it again runs the tests again.
- **One Cloud job at a time across the repository:**
- one workflow-level concurrency group, `tests-cloud`, with
`cancel-in-progress: false`, so a whole run holds the lock and other
runs wait. Of the waiting runs, only the newest is kept.
- within a run, `max-parallel: 1` runs the matrix jobs one by one, and
the Web jobs wait for the Node.js jobs.
- runs whose jobs will be skipped, for example a PR that got a different
label, get a group of their own (`tests-cloud-skipped-<run id>`), so
they can't replace a waiting Cloud run.
- **`examples.yml` passes the Cloud credentials only after a merge and
on manual runs.** On PRs they're empty, so the `create_table_cloud`
example (Node.js and Web) skips itself, which it already did when the
credentials were missing. Before this, each of the 4 examples jobs of
every PR hit the Cloud service. With the lexer stack's PRs running at
once, that example timed out in 28 jobs.
- **`clean-up.yml` runs hourly instead of on every push.** It used to
run on every push to every branch, and a new push cancelled one still in
progress. Dropping databases one by one takes a while, so a burst of
pushes kept restarting it. It now runs at :17 every hour, which also
puts a run before the 09:00 scheduled Cloud tests, and on demand
(`workflow_dispatch`). Only one runs at a time, and a running cleanup is
never cancelled.
- **Fork and Dependabot PRs are still skipped**, as before, because
Cloud secrets aren't available to them. It uses `pull_request`, not
`pull_request_target`, so a PR's code never runs with secrets unless it
comes from a branch in this repository.
- **`npm ci` instead of `npm install`** in the new workflow, so
dependencies are installed exactly as locked, verified by hash. This
resolves the Scorecard Pinned-Dependencies alerts on these two steps.
The other workflows still use `npm install`, so their existing alerts
are unchanged.
- **`codecov.yml`:** `after_n_builds` goes from 7 to 5, the uploads
every PR gets. With 7, Codecov never posted a status on fork PRs (for
example #1041 and #1024), which skip the Cloud jobs, and with this
change it wouldn't post on most PRs. The cloud flags are carried forward
(`carryforward: true` was already set).

### Trade-offs

- **Cloud regressions show up after merge** (on `main`), unless the
label is added before merging.
- **A Cloud run takes longer** with jobs one at a time: 4 Node.js
versions and then 2 browsers, about 25–30 minutes instead of about 5.
- **The `tests:run-cloud` label is new.** I've created it in the
repository.

## Testing

- All workflow and Codecov YAML parses, and Prettier passes.
`actionlint` wasn't available locally.
- The new workflow can only really be checked on GitHub: after merge
(push to `main`), and by adding `tests:run-cloud` to a PR once the Cloud
service has been cleaned up.

## Checklist

- [x] CI only; no changelog entry needed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@ccoltx

ccoltx commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Closing: #1085 is merged. query(), command() and exec() now send the SQL as written, with the format passed to the server as the default_format setting, so the client no longer rewrites SQL and this stack has nothing left to do. #973 and #970 are closed as fixed. See #1074 for the wrap-up.

@ccoltx ccoltx closed this Oct 31, 2026
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