Skip to content

Add table schema to SQLInsert output - #114106

Open
mishok2503 wants to merge 17 commits into
ClickHouse:masterfrom
mishok2503:feature/sqlinsert-table-schema-84736
Open

mishok2503 wants to merge 17 commits into
ClickHouse:masterfrom
mishok2503:feature/sqlinsert-table-schema-84736

Conversation

@mishok2503

@mishok2503 mishok2503 commented Aug 10, 2026 •

Copy link
Copy Markdown

Adds the output_format_sql_insert_include_table_schema setting for the SQLInsert output format. When enabled, SQLInsert prepends a CREATE TABLE statement derived from the query result schema, making the generated SQL directly replayable into an empty database.

The table definition uses the result column names and types with MergeTree and ORDER BY tuple(). Source-table metadata such as keys, default expressions, codecs, TTLs, and indexes is not preserved. The setting is disabled by default and cannot be enabled together with output_format_sql_insert_use_replace.

The change includes embedded documentation and stateless coverage for replaying the generated SQL, complex types, identifiers, empty results, batching, and incompatible settings.

Closes: #84736

Changelog category:

  • Improvement

Changelog entry:

The SQLInsert output format can now prepend a CREATE TABLE statement with result column names and types. Enable output_format_sql_insert_include_table_schema to produce a ClickHouse SQL script that recreates the table and loads the exported data.


Workflow [PR]
Sync PR [sync-upstream/pr/114106]

@CLAassistant

CLAassistant commented Aug 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mishok2503 mishok2503 changed the title WIP: Add table schema to SQLInsert output Add table schema to SQLInsert output Aug 10, 2026
@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Aug 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bc3709b]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
05046_sql_insert_schema_non_utf8_type_framing FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 10, 2026
@nikitamikhaylov nikitamikhaylov self-assigned this Aug 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8b2cc2ad4 with master 2abd0f27b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

660 object files changed (+66.75 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/SQLInsertRowOutputFormat.cpp.o 118.76 KiB 158.77 KiB +40.01 KiB (+33.69%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

3170 translation units recompiled, 16073 s compile time in total, 3170 of them have a recent master baseline.

Job report

@mishok2503
mishok2503 force-pushed the feature/sqlinsert-table-schema-84736 branch from faa4a8f to dd6853b Compare August 12, 2026 14:41
Apply `validateDataType` with current query settings before emitting a generated `CREATE TABLE` statement. This keeps schemas generated by `SQLInsert` replayable for setting-gated suspicious and experimental types.

Add a regression for `LowCardinality(UInt8)`.

PR: ClickHouse#114106

Review: ClickHouse#114106 (comment)
Reject schema output for types that require non-default settings, so generated scripts remain replayable in a default session.

Related: ClickHouse#114106
…able-schema-84736

# Conflicts:
#	src/Processors/Formats/Impl/SQLInsertRowOutputFormat.cpp
Treat schema type names as non-textual output when they contain invalid UTF-8, so HTTP framing preserves generated scripts byte-for-byte.

Related: ClickHouse#114106
…hema-comments-114106

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Record `output_format_sql_insert_include_table_schema` under the current compatibility block after merging `master`, so `settings_changes_history` accepts the PR diff.
Keep generated `CREATE TABLE` and `INSERT` statements replayable when `output_format_sql_insert_quote_names` is disabled and headers contain unsafe identifiers.
The generated `CREATE TABLE` uses `MergeTree`, whose persistent virtual column names cannot be used as physical columns. Reject all three reserved header names before emitting an unreplayable script.

Review: ClickHouse#114106 (comment)
Validate `output_format_sql_insert_table_name` with `ParserCompoundIdentifier` before emitting a schema. This rejects values such as `a b` instead of producing an unreplayable script while leaving ordinary `SQLInsert` output unchanged.

Review: ClickHouse#114106 (comment)
@@ -124,13 +247,22 @@ void registerOutputFormatSQLInsert(FormatFactory & factory)
if (is_not_valid_utf8(settings.sql_insert.table_name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output_format_sql_insert_include_table_schema emits the canonical identifier from parseAndFormatSQLInsertTableName(), but this raw-bytes checker still looks at settings.sql_insert.table_name. Over HTTP a value such as output_format_sql_insert_table_name=t%20--%20%FF is formatted back as plain t in both statements, so the actual payload is valid UTF-8; nevertheless outputFormatProducesText() sees the ignored comment byte, marks the format as binary, and JSONEachPacketString rejects the query while EventStream base64-encodes it unnecessarily.

Please canonicalize the table name here as well when schema mode is enabled (using settings.max_parser_depth) and validate the formatted identifier bytes instead of the raw setting string. A focused framing regression for a trailing-comment table name would catch this.

@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Dear @nikitamikhaylov, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

…able-schema-84736

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

- retargeted PR record [email protected] to 26.10 (settings-history check rule)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026

This branch has not been deployed

No deployments
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 comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLInsert Format improvement to also include create schema query

5 participants