Skip to content

Apply column defaults for inline INSERT into table functions - #109258

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-inline-insert-table-function-defaults-109253
Jul 10, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-inline-insert-table-function-defaults-109253

Conversation

@groeneai

@groeneai groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a bug where column DEFAULT values were not applied for INSERT INTO TABLE FUNCTION (for example remote(...) or file(...)) with inline VALUES data when the server parses the inline data itself (send_table_structure_on_insert_with_inline_data = 0). An explicit NULL inserted into a non-Nullable column with a DEFAULT became 0 instead of the declared default. It now behaves like a plain table INSERT and the HTTP protocol.

Description

Addresses gap 1 of #109253 (table-function defaults). The trailing-comment data-loss gap is not touched here.

Root cause: for inline (non-SELECT) INSERT, defaults are applied by the AddingDefaultsTransform added in getSourceFromInputFormat (src/Processors/Transforms/getSourceFromASTInsertQuery.cpp). That transform was added only when ast_insert_query->table_id is set. table_id is empty for table functions, so the transform (which also performs input_format_null_as_default during parsing) was skipped and the explicit NULL was not converted to the column default. The downstream InsertDependenciesBuilder::createPreSink -> addMissingDefaults path only fills columns missing from the input header, and its null-as-default is gated to INSERT ... SELECT, so it does not cover inline VALUES. The asynchronous insert path was unaffected because it adds its own AddingDefaultsTransform, which is why the synchronous case (async_insert = 0, as CI runs) was the one that reproduced.

Fix: when table_id is empty, fall back to the resolved destination columns saved in the context by InterpreterInsertQuery::setInsertContextValues (Context::getInsertionTableColumnsDescription()), which is populated for table functions as well. The input() path stays gated out by !input_function.

Verified in both directions on a local debug build (server-side inline path, async_insert = 0, send_table_structure_on_insert_with_inline_data = 0):

Case Before After
remote() Int DEFAULT 7, VALUES (NULL) 0 7
file() CSV Int DEFAULT 77, VALUES (4, NULL) 4, 0 4, 77
file() CSV LowCardinality(String) DEFAULT 'x' (empty) x
file() CSV Array(Int) DEFAULT [1, 2] [] [1, 2]
file() CSV Nullable(Int) DEFAULT 77, explicit NULL NULL NULL (unchanged, correct)

CI report showing the failure this fixes (03667 on PR #104640 head 8ed06ca, send_table_structure_on_insert_with_inline_data = 0, 183/183 failing): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104640&sha=8ed06ca93081214a31acef155c8df020fd950600&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29

Version info

  • Merged into: 26.7.1.781 (included in 26.7 and later)
  • Backported to: 26.6.2.62

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse-client --async_insert 0 --send_table_structure_on_insert_with_inline_data 0 -q "INSERT INTO FUNCTION remote('127.0.0.1:PORT', currentDatabase(), 't0') VALUES (NULL)" into t0 (c Int DEFAULT 7) yields 0 (should be 7), every time. async_insert = 0 is the required trigger (CI sets it via disable_async_inserts.xml).
b Root cause explained? Inline (non-SELECT) INSERT defaults come only from AddingDefaultsTransform added in getSourceFromInputFormat, gated on ast_insert_query->table_id. For table functions table_id is empty so the transform (which also does input_format_null_as_default during parsing) is skipped and NULL is not converted to the default. The downstream createPreSink->addMissingDefaults only fills omitted columns and its null-as-default is gated to INSERT...SELECT, so it does not cover inline VALUES. The async path adds its own transform, which is why only sync (async_insert=0) reproduced.
c Fix matches root cause? Yes. When table_id is empty, fall back to the resolved destination columns saved in the context by InterpreterInsertQuery::setInsertContextValues (getInsertionTableColumnsDescription()), which is populated for table functions. This adds the same AddingDefaultsTransform the plain-table path already gets.
d Test intent preserved / new tests added? New regression test 03816_inline_insert_table_function_defaults added (remote() and file() table functions, async_insert=0 + setting=0). No existing test weakened.
e Both directions demonstrated? Yes. Baseline binary (master, Build ID 74ef79fa): remote() -> 0, file() Int -> 4 0, LC -> empty, Array -> []. Fixed binary (8bd98746): 7, 4 77, x, [1,2]. Test FAILS without fix, PASSES with fix.
f Fix is general across code paths? Yes. getSourceFromInputFormat is the single inline-INSERT default site; both its callers (buildInsertPipeline inline path, executeQuery inline-completion path) have setInsertContextValues called before them, verified. The input() caller is gated out by !input_function. Not a symptom guard: the transform is now correctly added at the point where defaults are applied.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Verified for Int, LowCardinality(String), Array(Int) (all get DEFAULT applied) and Nullable(Int) (explicit NULL correctly preserved, not forced to default). Both remote() and file() table functions covered; explicit-NULL and omitted-field cases covered.
h Backward compatible? (maintainer-approved exception only) Yes. No setting default, on-disk, wire, or metadata format change. Behavior only moves toward correctness (matching plain-table INSERT and HTTP), gated by the existing input_format_defaults_for_omitted_fields exactly as the plain-table path.
i Invariants and contracts preserved? Yes. getInsertionTableColumnsDescription() is populated by setInsertContextValues (InterpreterInsertQuery.cpp:1003) before buildInsertPipeline for table functions too; the returned pointer is per-query and null-checked (if (columns && columns->hasDefaults())). No lifetime, ownership, or block-structure invariant changed; the AddingDefaultsTransform contract is identical to the existing plain-table branch.

Session id: cron:clickhouse-worker-slot-0:20260703-053400

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai @alexey-milovidov — could you review this? It fixes gap 1 of #109253: for server-side inline INSERT (send_table_structure_on_insert_with_inline_data = 0, sync path), getSourceFromInputFormat skipped AddingDefaultsTransform when ast_insert_query->table_id is empty, which it always is for table functions, so an explicit NULL into a DEFAULT column became 0 instead of the default. The fix falls back to context->getInsertionTableColumnsDescription() (populated for table functions by setInsertContextValues), matching the plain-table and HTTP behavior.

@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1ae85cd]

Summary: ✅


AI Review

Summary

This PR restores AddingDefaultsTransform for server-parsed inline INSERT INTO FUNCTION ... by reusing the resolved destination ColumnsDescription from Context when table_id is empty. I did not find an unresolved correctness or coverage gap in the current diff: the fallback stays confined to the existing inline non-input() path, plain-table behavior is unchanged, and the new regression test exercises both remote() and file() with explicit NULL into DEFAULT columns on the synchronous server-parsed path.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 3, 2026
@groeneai
groeneai force-pushed the groeneai/fix-inline-insert-table-function-defaults-109253 branch from 495a342 to 02ed24b Compare July 3, 2026 07:44
@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased onto current master and fixed a build break from base drift.

Master's storage-metadata accessor now returns a StorageMetadataHandle whose implicit operator StorageMetadataPtr() const && is deleted (lifetime-safety guard, StorageInMemoryMetadata.h), so the StorageMetadataPtr metadata_snapshot; local no longer compiles from the rvalue getInMemoryMetadataPtr(...). Changed the local to StorageMetadataHandle (the StorageSystemTables.cpp pattern); the handle owns the shared_ptr internally and keeps the metadata alive across the if/else, so the columns pointer stays valid. No behavior change.

Re-verified both directions on the merged base:

  • without the fix (baseline build): remote()->0, file() Int->0, LC->empty, Array->[] (bug)
  • with the fix: 7, 77, x, [1,2]; Nullable keeps explicit NULL

03816_inline_insert_table_function_defaults FAILS without the fix, PASSES with it.

When the server parses INSERT ... VALUES inline data itself
(send_table_structure_on_insert_with_inline_data = 0), an explicit NULL
inserted into a non-Nullable column with a DEFAULT was not replaced by the
default for INSERT INTO TABLE FUNCTION remote()/file(), producing 0 instead
of the declared default. A plain table INSERT and the HTTP protocol apply it
correctly.

Root cause: getSourceFromInputFormat added AddingDefaultsTransform (which
applies input_format_null_as_default during parsing) only when
ast_insert_query->table_id was set. table_id is empty for table functions,
so the transform was skipped. The synchronous path is the affected one; the
asynchronous path adds its own AddingDefaultsTransform.

Fix: when table_id is empty, fall back to the resolved destination columns
saved in the context by InterpreterInsertQuery
(getInsertionTableColumnsDescription), which is populated for table functions
too. The input() path is unaffected (still gated by !input_function).

Related: ClickHouse#109253
Caused by: ClickHouse#101034

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the groeneai/fix-inline-insert-table-function-defaults-109253 branch from 02ed24b to 1ae85cd Compare July 3, 2026 12:31
@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased onto master (head 1ae85cda) to re-run Bugfix validation on the current head.

Finish Workflow failed on the previous head 02ed24b because new_tests_check.py saw all four per-arch Bugfix-validation jobs as SKIPPED: after the earlier rebase they were cache-reused from head 495a3426 (where they had already run and reported OK), so no job actually executed on 02ed24b and the hook counted passed: none. praktika's job digest is content-based over include_paths (which includes tests/queries), so rebasing onto current master changes the digest and forces the jobs to re-execute. The regression test and fix are unchanged (still 3 files, +75/-6); no test weakening.

@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.60% +0.10%
Functions 92.70% 92.70% +0.00%
Branches 77.70% 77.80% +0.10%

Changed lines: Changed C/C++ lines covered: 15/15 (100.00%) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 1ae85cd

CI fully finished (Finish Workflow SUCCESS). No failed checks on this head.

The Bugfix-validation new_tests_check.py blocker seen on the prior head (02ed24b, caused by post-rebase cache reuse marking the per-arch Bugfix-validation jobs as skipped) is resolved: the rebase onto current master busted the digest so those jobs re-executed and validated the fix. No failures to attribute.

Session id: cron:our-pr-ci-monitor:20260703-173000

@alexey-milovidov alexey-milovidov left a comment

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.

Very good.

@alexey-milovidov alexey-milovidov self-assigned this Jul 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 10, 2026
Merged via the queue into ClickHouse:master with commit a2a6fe8 Jul 10, 2026
175 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 10, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 13, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 13, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 13, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 13, 2026
Backport #109258 to 26.6: Apply column defaults for inline INSERT into table functions
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…e-insert-table-function-defaults-109253

Apply column defaults for inline INSERT into table functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants