Skip to content

fix(usage): converge drifted usage columns and give new tables their TTL - #14561

Merged
abnegate merged 3 commits into
mainfrom
fix-usage-column-shapes
Oct 15, 2026
Merged

abnegate merged 3 commits into
mainfrom
fix-usage-column-shapes

Conversation

@abnegate

@abnegate abnegate commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

This closes the gaps Usage::migrate() (#14553) left, so cloud can drop its bespoke task-patch-add-usage-events-attributes. That task opens its own ClickHouse connection and runs these ALTERs by hand.

Column shapes. A table created by an older release kept its old column shapes:

  • protocol, timeZone and weatherCode stayed Nullable(String) after they became LowCardinality(Nullable(String)).
  • accept, acceptLanguage and queryKeys stayed without CODEC(ZSTD(3)).
  • Columns that migrate() added came without their codec.

migrate() (and so setup()) now reads each events and gauges table's columns from system.columns. It adds missing columns with their full definition and runs MODIFY COLUMN on columns whose type or codec differs. A column the library gives no codec keeps its own. Once a table has converged, migrate() sends no ALTER.

Indexed columns. ClickHouse refuses to change a column that a skip index covers (code 524, ALTER_OF_COLUMN_IS_FORBIDDEN). That is the real state of a table #14553's migrate() has already indexed, e.g. protocol with index-protocol. For each drifted column, migrate():

  1. reads system.data_skipping_indices and drops the indexes covering it (DROP INDEX IF EXISTS);
  2. runs the MODIFY COLUMN;
  3. adds the same indexes back with the recorded expression, type and granularity (ADD INDEX IF NOT EXISTS);
  4. runs MATERIALIZE INDEX so existing parts are indexed again.

A run interrupted midway resumes on the next run: the column is still drifted, and ensureIndexes() restores the library's indexes.

Retention on new tables. migrate() created a missing events or daily table without the TTL, so it grew without bound. A table migrate() creates now carries the configured retention TTL in its CREATE TABLE. An existing table's TTL is still never altered by migrate(); only setup() changes it. Gauges never get a TTL.

Operational note: a type change schedules a ClickHouse mutation that rewrites that column, and so does materializing a restored index. Both run asynchronously. A codec change only applies to parts written or merged afterwards.

Test Plan

All E2E tests run against real ClickHouse 25.11. Each new test fails on main.

  • testMigrateConvergesColumnsThatPredateTheirTypeAndCodec starts from the legacy shape with the indexes present: protocol as Nullable(String) with index-protocol (set(0)), and accept without its codec but with index-accept (bloom_filter). After migrate(), both columns have converged, both indexes exist, and a further migrate() adds no entry to system.mutations. Without the fix it fails with code 524.
  • testMigrateCreatesNewTablesWithTheRetention: with a 30-day retention, migrate() on an empty namespace creates the events and daily tables with TTL toDateTime(time) + toIntervalDay(30), and the gauges table without a TTL.
  • testMigrateAddsMissingColumnsWithoutApplyingRetention: an existing events table with no TTL still has none after migrate(), while the daily table that migrate() creates gets the TTL.
  • bin/monorepo check usage passes (Pint, Rector, PHPStan). bin/monorepo test usage passes: 112 unit and 275 E2E tests.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (No API changes.)

🤖 Generated with Claude Code

migrate() only added missing columns. A table created by an older release
kept the old shape: `protocol`, `timeZone` and `weatherCode` stayed
Nullable(String) after they became LowCardinality, and `accept`,
`acceptLanguage` and `queryKeys` stayed without CODEC(ZSTD(3)). Columns that
migrate() did add came without their codec, unlike a freshly created table.
Cloud carried a bespoke task with its own ALTERs to close that gap.

migrate() now reads each table's columns from system.columns. It adds the
missing ones with their full definition and modifies the ones whose type or
codec differs. A converged table gets no ALTER. Columns are converged before
ensureIndexes() adds their skip index, because ClickHouse refuses to change
the type of an indexed column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 CLICKHOUSE_SECURE=false casts to true in test adapter (ClickHouseSchemaTest.php:329) — The new convergence test builds its adapter with (bool) (getenv('CLICKHOUSE_SECURE') ?: false) (packages/usage/tests/E2E/Adapter/ClickHouseSchemaTest.php:329), which treats the string 'false' as truthy and enables TLS when the operator explicitly disables it.

The change extends ClickHouse::migrate() beyond create-if-missing: it inspects system.columns and issues a mixed ALTER that adds missing columns and modifies divergent column types/codecs, plus a dedicated e2e test exercising convergence on a pre-existing table. The core mechanism is sound and the test coverage is welcome, but the convergence path ignores skip-index interactions: ClickHouse refuses to MODIFY COLUMN a column that a data-skipping index depends on, and this adapter attaches skip indexes to nearly every dim column via ensureIndexes() — the exact legacy tables the convergence targets. The test also mis-parses a boolean env var, enabling TLS when operators explicitly disable it.

Files Change
packages/usage/src/Adapter/ClickHouse.php Adds schema-convergence logic: reads system.columns, builds a mixed ADD COLUMN/MODIFY COLUMN ALTER for type/codec drift on existing tables.
packages/usage/tests/E2E/Adapter/ClickHouseSchemaTest.php New e2e test covering convergence on a pre-existing table, including dropping a skip index before modifying its column.
packages/usage/CHANGELOG.md Changelog entry for the convergence feature.

Reviewed commit: 6b555e4

@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

No actionable defects were found in the changes reviewed.

This PR makes ClickHouse usage migrations converge legacy event and gauge dimension column types and codecs, temporarily rebuilding skip indexes that cover columns being modified. Newly created event and daily tables receive the configured retention TTL, while existing tables keep their policy until setup() applies it. The tests cover schema convergence, index restoration, idempotency, and retention behavior; the latest change replaces regex-based index-expression matching with identifier scanning.

Latest changes: The newest commit replaces regex-based index-expression matching with a small identifier scanner, preserving covering-index detection without using regular expressions.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
packages/usage/CHANGELOG.md Documents legacy schema convergence, skip-index restoration, and migration-time retention behavior.
packages/usage/src/Adapter.php Clarifies that migration does not alter the retention policy of existing tables.
packages/usage/src/Adapter/ClickHouse.php Converges dimension column types and codecs, restores covering skip indexes, applies TTL to newly created event and daily tables, and now scans index expressions for identifiers without regex.
packages/usage/src/Usage.php Clarifies retention behavior for tables created by migration and existing tables.
packages/usage/tests/E2E/Adapter/ClickHouseSchemaTest.php Covers column convergence, skip-index preservation, migration idempotency, and TTL behavior.

Reviewed the commits since 3ef1040 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

ghost
ghost previously requested changes Oct 15, 2026

@ghost ghost 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.

🟡 Tier B · 1 blocking finding to address. Summary

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-usage-column-shapes (after).

Metric Before After Change
🚀 Requests/sec 160.94 179.98 🟢 +11.8%
⏱️ Latency P50 107.96 ms 97.57 ms 🟢 -9.6%
⏱️ Latency P95 251.46 ms 219.18 ms 🟢 -12.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 97.57 219.18 11,400 179.98 -32.28
Account 176.16 319.89 600 10.15 -36.95
TablesDB 94.07 175.88 6,200 99.67 -30.68
Storage 92.54 197.27 3,000 49.94 -18.6
Functions 129.7 249.92 1,600 27.33 -33.5

Top API waits (after)

API request Max wait (ms)
account.name.update 542.5
account.prefs.update 443.49
tablesdb.rows.create 425.04
storage.buckets.create 421.54
functions.variables.create 390.64

Two gaps in migrate():

- ClickHouse refuses to change a column that a skip index covers (code 524,
  ALTER_OF_COLUMN_IS_FORBIDDEN). A table that #14553's migrate() had already
  indexed, such as `protocol` with `index-protocol`, aborted the
  convergence. migrate() now drops the indexes covering a drifted column,
  modifies the column, then adds the same indexes back and materializes them
  for existing parts. A run interrupted midway resumes on the next run.
- migrate() created a missing events or daily table without the retention
  TTL, so a table it created grew without bound. A table migrate() creates
  now carries the configured TTL in its CREATE TABLE. An existing table's TTL
  is still never altered; only setup() changes it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@abnegate abnegate changed the title fix(usage): converge usage columns that predate their type or codec fix(usage): converge drifted usage columns and give new tables their TTL Oct 15, 2026
@ghost
ghost dismissed their stale review October 15, 2026 12:39

The findings that requested changes are resolved. See the summary comment for what is still open.

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

@luxor-code-review luxor-code-review Bot 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.

This PR adds schema-convergence DDL to the usage ClickHouse adapter (type/codec MODIFY COLUMN plus pending ADD COLUMN in one ALTER) with a new e2e convergence test, but the convergence ALTER can fail on legacy tables whose columns are covered by skip indexes.

Key findings

The repository does not add regex. getSkipIndexesCovering() now splits each
index expression from system.data_skipping_indices into identifiers with
strspn/strcspn and compares them with the drifted columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

@abnegate
abnegate merged commit 216111d into main Oct 15, 2026
68 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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