Skip to content

Forbid to create not insertable tables - #81835

Merged
GrigoryPervakov merged 2 commits into
masterfrom
noninsertable_tables
Jul 9, 2025
Merged

GrigoryPervakov merged 2 commits into
masterfrom
noninsertable_tables

Conversation

@GrigoryPervakov

Copy link
Copy Markdown
Member

Materialized columns are physical, but data can't be inserted into such tables without a Default or Ephemeral column.
It can break clickhouse-client in cases like the linked issue.

Closes #72139

Changelog category (leave one):

  • Backward Incompatible Change

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

Forbid the creation of a table without insertable columns.

@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b4bf7d7]

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label Jun 13, 2025
}

if (res.getAllPhysical().empty())
if (!hasPhysicalColumns)

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.

We should be able to attach them or execute secondary create.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added check and test

@mstetsyuk mstetsyuk assigned mstetsyuk and unassigned mstetsyuk Jun 23, 2025
if (!hasPhysicalColumns)
throw Exception(ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED, "Cannot CREATE table without physical columns");

if (mode <= LoadingStrictnessLevel::SECONDARY_CREATE && !is_restore_from_backup && !hasInsertableColumns)

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.

Suggested change
if (mode <= LoadingStrictnessLevel::SECONDARY_CREATE && !is_restore_from_backup && !hasInsertableColumns)
if (mode <= LoadingStrictnessLevel::CREATE && !is_restore_from_backup && !hasInsertableColumns)

I assume we still want to be able to execute a secondary create?

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.

Now that we allow secondary create of such tables, the change should not be backwards incompatible, correct? If so, I suggest we remove the pr-backwards-incompatible tag.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@mstetsyuk
I have forgotten why I chose SECONDARY_CREATE here. It applied during alter table calls, so in such a case it is still possible to create table by deleting all insertable columns.

So I think it's better to revert this to CREATE, restores are handled, a replicated database would check it with CREATE, so all SECONDARY_CREATE on replicas should satisfy the condition.

@mstetsyuk mstetsyuk self-assigned this Jun 23, 2025
@clickhouse-gh

clickhouse-gh Bot commented Jun 23, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [297ad6c]

Summary: ✅

if (res.getAllPhysical().empty())
throw Exception(ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED, "Cannot CREATE table without physical columns");

if (mode <= LoadingStrictnessLevel::SECONDARY_CREATE && !is_restore_from_backup && res.getInsertable().empty())

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.

This would still break adding new replicas to the cluster (which would break releases and horizontal scaling), right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I forgot to change it back, fixed

@GrigoryPervakov
GrigoryPervakov enabled auto-merge July 9, 2025 14:12
@GrigoryPervakov
GrigoryPervakov added this pull request to the merge queue Jul 9, 2025
Merged via the queue into master with commit 7509b03 Jul 9, 2025
@GrigoryPervakov
GrigoryPervakov deleted the noninsertable_tables branch July 9, 2025 15:09
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2025
@@ -0,0 +1,12 @@
-- Tags: no-parallel, no-ordinary-database
-- Tag no-parallel: static UUID

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.

You can use no-flaky-check instead. The test itself is parallel with any other test, just not with itself.

@maxknv maxknv mentioned this pull request Aug 30, 2025
1 task
@ramazanpolat

Copy link
Copy Markdown
Contributor

BUT WHY?

This was a feature, not a bug.

Although it may look like “having a non-insertable table,” that’s not actually the case. You could create a table, insert some data, and then make it non-insertable—which has its own use cases and valid reasons, the first being freedom to use features as needed.

This change feels unnecessary from a technical perspective, unintuitive for users, and reduces the overall completeness of the system.

Who would expect to create a table with only materialized columns and then be able to insert data into it?

It made sense to allow creating the table—why not let people use it the way they want?

Practical use cases:

  • Some may want to lock down writes after an initial bootstrap of data.
  • Others may use materialized columns to enforce immutability without relying on external logic.
  • It can serve as a lightweight safeguard for preventing accidental inserts or updates.
  • It supports progressive schema evolution: start with a regular column, insert data, then convert it to materialized once the schema stabilizes.
  • For testing or debugging, this flexibility allows reproducing edge cases that would otherwise be impossible.

Technical arguments:

  • Consistency with ALTER semantics: If ALTER TABLE allows converting between types or defaults, it should also allow converting a regular column into a materialized one after data is inserted.
  • Backward compatibility: This used to work, and removing it silently breaks real-world use cases where people relied on the flexibility.
  • Principle of least surprise: It’s natural to expect that a column can be promoted to MATERIALIZED after initialization, similar to adding constraints in SQL.
  • No inherent technical blocker: There’s no fundamental engine limitation preventing this—it worked before. The restriction seems arbitrary.
  • Symmetry: Since it’s possible to drop or modify a materialized column back into a regular one, the reverse should logically also be allowed.

Previously, it was possible to create a table, insert some data, and then modify a column to be materialized in order to lock down any further write operations until explicitly altering the columns again:

CREATE TABLE t0 ENGINE = Memory() AS SELECT materialize(123)::int AS x;
ALTER TABLE t0 MODIFY COLUMN x MATERIALIZED 0;
SELECT x FROM t0;

Now, this is no longer possible.
Even trying to circumvent it by adding a non-materialized column then dropping it is also forbidden.

ALTER TABLE t0 ADD COLUMN y int;
ALTER TABLE t0 MODIFY COLUMN x MATERIALIZED 0;
ALTER TABLE t0 DROP COLUMN y;

Also, the error message is somewhat misleading:

Received exception from server (version 25.8.2):
Code: 90. DB::Exception: Received from localhost:9000. DB::Exception: Cannot CREATE table without insertable columns. (EMPTY_LIST_OF_COLUMNS_PASSED)
(query: ALTER TABLE t0 MODIFY COLUMN x MATERIALIZED 0;)

alexey-milovidov added a commit to alexey-milovidov/sqlancer that referenced this pull request Mar 1, 2026
ClickHouse PR ClickHouse/ClickHouse#81835
added a check that forbids creating tables without insertable columns
(e.g., tables with only MATERIALIZED or ALIAS columns). Since SQLancer's
random table generator can produce such definitions, ClickHouse now
returns EMPTY_LIST_OF_COLUMNS_PASSED, which should be treated as an
expected error rather than causing an AssertionError.

The table creation retry loop in ClickHouseProvider.generateDatabase
already handles expected errors by regenerating the table definition.
alexey-milovidov added a commit that referenced this pull request Mar 1, 2026
SQLancer was disabled after #81835
which forbids creating tables without insertable columns (e.g., with only
`MATERIALIZED` or `ALIAS` columns). SQLancer's random table generator could
produce such definitions, causing an `AssertionError`.

The fix adds `EMPTY_LIST_OF_COLUMNS_PASSED` to SQLancer's expected errors.
The table creation retry loop in `ClickHouseProvider.generateDatabase` already
handles expected errors by regenerating the table definition.

The fix was submitted upstream: sqlancer/sqlancer#1296
and applied to the ClickHouse fork: https://github.com/ClickHouse/sqlancer

This PR switches CI to use the ClickHouse fork (pinned to a specific commit)
and re-enables the SQLancer job in the master workflow.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@alexey-milovidov alexey-milovidov mentioned this pull request Mar 1, 2026
1 task
def- pushed a commit to MaterializeInc/sqlancer that referenced this pull request Mar 30, 2026
ClickHouse PR ClickHouse/ClickHouse#81835
added a check that forbids creating tables without insertable columns
(e.g., tables with only MATERIALIZED or ALIAS columns). Since SQLancer's
random table generator can produce such definitions, ClickHouse now
returns EMPTY_LIST_OF_COLUMNS_PASSED, which should be treated as an
expected error rather than causing an AssertionError.

The table creation retry loop in ClickHouseProvider.generateDatabase
already handles expected errors by regenerating the table definition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backward-incompatible Pull request with backwards incompatible changes 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.

Logical Error: Column description is empty and it can't be built from sample from table

7 participants