Skip to content

Add INSERT ... SELECT and ClickHouse insert settings - #12

Merged
Makisuo merged 1 commit into
feat/insert-on-conflictfrom
feat/insert-select
Oct 4, 2026
Merged

Makisuo merged 1 commit into
feat/insert-on-conflictfrom
feat/insert-select

Conversation

@Makisuo

@Makisuo Makisuo commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Phase 4 of design/writes.md, the last insert phase. Stacked on #11 → #10 → #9.

CH.insertInto(Daily)
	.select(
		CH.from(Spans)
			.select(($) => ({ OrgId: $.OrgId, Name: $.Name, Total: CH.sum($.Ms) }))
			.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
			.groupBy("OrgId", "Name"),
	)
	.settings({ async_insert: 0 })

What changes

  • select(query) takes a query or a unionAll. The selected aliases name the columns in select order, so ClickHouse and Postgres agree.
  • Type check. The selected row is checked against the table at the type level: an unknown or computed column, a value of the wrong type, or a missing required column is a type error naming the columns. A nullable sum into a NOT NULL column is rejected, so wrap it in coalesce.
  • Tenant scope. Into an untenanted table, the insert has the SELECT's scope (what it reads). Into a tenant table, it is single-tenant only when the read is and each row takes its tenant from a source tenant column or from the same param that pins the read.
  • Combinations. returning and onConflict* work with select; select and values replace each other.
  • settings(record) writes INSERT ... SETTINGS name = value on ClickHouse. New optional DialectClauses.insertSettings; Postgres refuses with a defect.
  • Not built: encodeInsertRows (§7), since no consumer has asked for it.

Testing

  • bun run typecheck and bun run test: 510 unit tests and the doc checks pass.
  • Unit tests cover the SQL on both dialects, shared param numbering with ON CONFLICT, unions, the tenant-scope cases and settings; type tests cover the fit check and settings.
  • PGlite: an INSERT ... SELECT ... GROUP BY rollup with onConflictDoUpdate and returning, run twice to check that it accumulates.
  • Live ClickHouse suite (249 tests) on 26.2.19.43 and 26.8.2.7, including INSERT ... SELECT and SETTINGS.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

select(query) inserts the rows a query or union selects. The selected
aliases name the columns, in select order, so positional and named
matching agree; the row is checked against the table at the type level
(targetCannotTake, missingColumns). Tenant scope is the SELECT's for an
untenanted target, and single-tenant for a tenant target only when the
read is and each row takes its tenant from a source tenant column or
the same param. returning and onConflict work with select as with
values.

settings(record) writes INSERT ... SETTINGS on ClickHouse;
DialectClauses.insertSettings is optional and Postgres refuses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 12 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 053415d0-84fe-40a4-af8c-176139a15ae9
📥 Commits

Reviewing files that changed from the base of the PR and between eab33d4 and db39f5e.

📒 Files selected for processing (12)
  • CHANGELOG.md
  • design/writes.md
  • docs/inserts.md
  • docs/reference.md
  • src/ch/compile.ts
  • src/ch/dialect.ts
  • src/ch/index.ts
  • src/ch/insert.test-d.ts
  • src/ch/insert.test.ts
  • src/ch/insert.ts
  • src/database/database.test.ts
  • tests/database.clickhouse.test.ts
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Makisuo
Makisuo added this pull request to stack #13 October 3, 2026 23:07
@Makisuo
Makisuo merged commit 70f674a into main Oct 4, 2026
4 checks passed
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