The "DB::Exception: Nested table structure is empty" error in ClickHouse is raised when a Nested data structure is defined or referenced but contains no columns. ClickHouse requires every Nested type to have at least one sub-column, so an empty definition is rejected at table creation or alteration time. The error code associated with this issue is EMPTY_NESTED_TABLE.
Impact
When this error fires, the DDL statement that triggered it (CREATE TABLE, ALTER TABLE, or similar) fails outright. No table is created or modified, which can block schema migrations and deployment pipelines that depend on the operation succeeding.
Common Causes
- Declaring a
Nestedcolumn with an empty parenthesized list, such ascol Nested(). - Generating DDL programmatically where the nested column list is built dynamically but the list ends up empty due to a filtering bug or missing configuration.
- Copying a table definition from documentation or another system and accidentally removing all sub-columns from a Nested block.
- Using an ORM or migration tool that does not properly serialize Nested column definitions for ClickHouse.
Troubleshooting and Resolution Steps
Inspect the DDL statement: Look at the exact CREATE TABLE or ALTER TABLE statement that produced the error. Locate every
Nested(...)declaration and verify it contains at least one sub-column.Add the required sub-columns: A valid Nested column needs one or more typed sub-columns:
CREATE TABLE example ( id UInt64, attributes Nested( key String, value String ) ) ENGINE = MergeTree() ORDER BY id;Check code-generation logic: If the DDL is generated by an application, trace the code path that builds the Nested definition. Ensure the list of sub-columns is never empty before the statement is sent to ClickHouse.
Validate migrations before applying: Run the DDL through
clickhouse-formator a dry-run mechanism to catch structural issues before they hit the server.Review ALTER TABLE additions: If you are adding a Nested column via ALTER TABLE, the same rule applies — the column must include at least one sub-column definition.
Best Practices
- Always define at least one sub-column inside every Nested type.
- Use schema validation in your CI/CD pipeline to catch empty Nested definitions before deployment.
- Prefer explicit column listings over dynamic generation when the schema is known ahead of time.
- Consider whether
TupleorMaptypes may be a better fit if the nested structure is simple or highly variable.
Frequently Asked Questions
Q: Can I create a Nested column and add sub-columns later with ALTER TABLE?
A: No. ClickHouse requires at least one sub-column at the time the Nested column is created. You cannot start with an empty Nested and populate it afterward.
Q: Is there a minimum or maximum number of sub-columns allowed in a Nested type?
A: The minimum is one. There is no hard-coded maximum, though extremely wide Nested structures can affect performance and memory usage.
Q: Does this error apply to Map or Tuple types as well?
A: Map requires exactly two type arguments (key and value), and Tuple requires at least one. Similar validation errors exist for those types, but they use different error codes.