Skip to content

Fix usage of insertion table in table functions when optimize_trivial_insert_select is enabled - #105555

Merged
Avogar merged 3 commits into
masterfrom
fix-trivial-insert-select-structure-hint
Jun 8, 2026
Merged

Avogar merged 3 commits into
masterfrom
fix-trivial-insert-select-structure-hint

Conversation

@Avogar

@Avogar Avogar commented May 21, 2026 •

Copy link
Copy Markdown
Member

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):

Fix usage of insertion table in table functions when optimize_trivial_insert_select is enabled. Closes #103083

Version info

  • Merged into: 26.6.1.513

…ion` from context

When `optimize_trivial_insert_select` is enabled (the default),
`applyTrivialInsertSelectOptimization` creates a copy of the select
context and then calls `setInsertionTable` with only two arguments,
omitting the `columns_description` parameter. This overwrites the
`columns_description` that was already correctly copied by
`Context::createCopy`, causing table functions like `file()` with
schema-less formats (e.g. RowBinary) to fail with BAD_ARGUMENTS
because the structure hint from the INSERT table is lost.

Fix by removing the redundant `setInsertionTable` call in
`applyTrivialInsertSelectOptimization` since `Context::createCopy`
already preserves the full `insertion_table_info` including
`columns_description`.

Also fix the same pattern in `InterpreterExecuteAsQuery` and
`StorageInMemoryMetadata` where `setInsertionTable` is called
without the `columns_description` parameter when creating a new
context from the global context.

Closes #103083

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f545887]

Summary: ✅


AI Review

Summary

This PR fixes optimize_trivial_insert_select losing insertion-table structure metadata while preparing trivial INSERT SELECT queries, and preserves the same metadata through copied contexts used by EXECUTE AS and SQL-security overridden execution. The added regression test exercises INSERT INTO ... SELECT * FROM file(..., RowBinary) with the optimization both disabled and enabled, and I did not find unresolved correctness, safety, or metadata issues.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 21, 2026
@CheSema CheSema self-assigned this May 27, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The failures of 00175_obfuscator_schema_inference and 00096_obfuscator_save_load in Stateless tests (amd_tsan, parallel) are NOT caused by this PR.

They were introduced by #104690 ("Add UntrackedMemory asynchronous metric"), which made clickhouse-obfuscator abort (SIGABRT) on process teardown: the query results are correct, but the Aborted line on stderr fails the test. #104690 has now been reverted (#106365).

#104690 was merged in violation of the ClickHouse team rules: its own CI already showed these two tests failing (10 times between May 12 and June 1) before it was merged.

Please update your branch to pick up the revert; the tests should pass again.

@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 13/13 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 25 line(s) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue Jun 8, 2026
Merged via the queue into master with commit 7839780 Jun 8, 2026
166 of 167 checks passed
@Avogar
Avogar deleted the fix-trivial-insert-select-structure-hint branch June 8, 2026 17:56
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 8, 2026
@clickgapai

clickgapai commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Fix usage of insertion table in table functions when optimize_trivial_insert_select is enabled

ClickGap verified: reproduced it with the script below on 26.5; tested 5 version(s) — 3 affected, 2 clean (see Affects).

Reproduction

-- PR #105555 / issue #103083: when optimize_trivial_insert_select is enabled,
-- INSERT ... SELECT from a table function reading a non-self-describing format
-- (e.g. RowBinary) loses the structure hint taken from the insertion table.
-- The optimization copied the context and reset the insertion-table info WITHOUT
-- the ColumnsDescription, so the table function no longer knew the column types
-- and the read failed.

-- Produce a RowBinary fixture with three Int32 values: 0, 5, 10.
-- RowBinary is NOT self-describing, so reading it back requires a structure hint.
SELECT (5 * number)::Int32 FROM numbers(3)
INTO OUTFILE 'data.bin' TRUNCATE FORMAT RowBinary;

CREATE TABLE t (c Int32 DEFAULT 7) ENGINE = MergeTree() ORDER BY tuple();

-- Control: with the optimization off the structure hint from t is kept, so the
-- file() table function reads the RowBinary column as Int32 and this succeeds.
SET optimize_trivial_insert_select = 0;
INSERT INTO t SELECT * FROM file('data.bin', RowBinary);

-- Bug path: with the optimization on the structure hint is lost and the same
-- INSERT fails (the table function can no longer infer the column type for the
-- non-self-describing RowBinary input).
SET optimize_trivial_insert_select = 1;
INSERT INTO t SELECT * FROM file('data.bin', RowBinary);

SELECT c FROM t ORDER BY c;
-- Expected (both inserts succeed): 0, 0, 5, 5, 10, 10

Bisect

  • Bisected on master in several steps (confidence: MEDIUM); a single introducing PR could not be pinned (untested merges with no prebuilt binary in the boundary range).
  • Release-level boundary (official release binaries): first reproduces at v25.12.7.21-stable.
  • Candidate range: the introducing PR is one of those merged between Docs: move documentation for mathematical functions to source #85738 and Implement new input output format Buffers #91156.
  • A source-build bisect over the range was attempted but could not produce a verdict; the range above is the most precise information available.

Affects

  • Verified against master build 84d36b6f9381 (aarch64)
  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.5, 26.4, 26.3
  • Does NOT reproduce on: master, 25.8
  • Backport needed: 26.5, 26.4, 26.3

Suggested next step

The fix has landed on master; the affected release branches above still need the backport.

Component: comp-query-execution
Severity: P2

cc fix PR author: @Avogar
cc fix PR reviewer: @CheSema


ClickGap · Finding: phase_d_pr105555

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 13, 2026
…_columns_description

The inline-server-parsed INSERT path (send_table_structure_on_insert_with_inline_data=0)
now applies destination column DEFAULTs through remote()/file() table functions, matching
the legacy and HTTP paths. The behavior was fixed upstream by ClickHouse#105555 (insertion table
columns_description is preserved in the context for table functions), so the test no longer
needs to pin the setting to 1. Remove the pin so CI exercises both values, and replace the
now-obsolete comment.

Verified locally: the test output matches the reference with the setting at both 0 and 1
(15/15 alternating runs).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

applyTrivialInsertSelectOptimization drops columns_description from context, breaking table function structure hints

5 participants