Skip to content

Default expressions in INSERT query #51509

@alexey-milovidov

Description

@alexey-milovidov

Use case

Specify an expression, or at least a constant, in the INSERT query that will be used instead of the input data.

Describe the solution you'd like

We should support two use cases:

  1. When an expression is specified instead of providing a column in the inserted data;
  2. When an expression is specified as a default value, but the column is still parsed from the inserted data, but the values can be omitted in certain records (e.g., from JSONEachRow format or DEFAULT in the Values format).

The first use case is more important.

I'm not sure what syntax is better for that and what are the alternatives in other databases:

INSERT INTO t (c1, c2, 'Hello' AS c3, now() AS c4) ...
INSERT INTO t (c1, c2, c3 DEFAULT 'Hello', c4 MATERIALIZED now()) ...
INSERT INTO t (c1, c2, c3 DEFAULT 'Hello', c4 = now()) ...

Describe alternatives you've considered

It is already possible with INSERT ... SELECT FROM input, just slightly more cumbersome, but more versatile:

INSERT INTO t SELECT c1, c2, c3, 'Hello' AS c4 FROM input('c1 UInt8, c2 String, c3 String, c4 DateTime DEFAULT now())

PS. Please find duplicate issues in the repository and link here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions