Skip to content

ALTER TABLE ... ADD INDEX / ADD PROJECTION / ADD CONSTRAINT IF NOT EXISTS fails to parse #123

Done

Description

@jakubno

SQL Query

Each of these statements fails to parse on its own:

ALTER TABLE events ADD INDEX IF NOT EXISTS idx_name name TYPE bloom_filter(0.01) GRANULARITY 1;
ALTER TABLE events ADD PROJECTION IF NOT EXISTS by_name (SELECT * ORDER BY name);
ALTER TABLE events ADD CONSTRAINT IF NOT EXISTS positive CHECK value > 0;

Expected Behavior

All three parse, as they do in ClickHouse, with IfNotExists set on the alter command.

The same statements without IF NOT EXISTS parse today, and so do ADD COLUMN IF NOT EXISTS, DROP INDEX IF EXISTS and DROP PROJECTION IF EXISTS.

Actual Behavior

parser.Parse returns an error for each statement. The output is the same on v1.0.0 and on main (0905e97):

ALTER TABLE events ADD INDEX IF NOT EXISTS idx_name name TYPE bloom_filter(0.01) GRANULARITY 1;
parse errors: [expected (, got NOT at line 1, column 33 unexpected token NOT at line 1, column 33 unexpected token IDENT at line 1, column 53 unexpected token IDENT at line 1, column 58 unexpected token IDENT at line 1, column 63 unexpected token IDENT at line 1, column 82 unexpected token NUMBER at line 1, column 94]

ALTER TABLE events ADD PROJECTION IF NOT EXISTS by_name (SELECT * ORDER BY name);
parse errors: [unexpected token NOT at line 1, column 38]

ALTER TABLE events ADD CONSTRAINT IF NOT EXISTS positive CHECK value > 0;
parse errors: [unexpected token IF at line 1, column 35 unexpected token NOT at line 1, column 38 unexpected token > at line 1, column 70 unexpected token NUMBER at line 1, column 72]

EXPLAIN AST Output (Optional but Helpful)

-- EXPLAIN AST ALTER TABLE events ADD INDEX IF NOT EXISTS idx_name name TYPE bloom_filter(0.01) GRANULARITY 1
AlterQuery  events (children 2)
 ExpressionList (children 1)
  AlterCommand ADD_INDEX (children 1)
   Index (children 2)
    Identifier name
    Function bloom_filter (children 1)
     ExpressionList (children 1)
      Literal Float64_0.01
 Identifier events

-- EXPLAIN AST ALTER TABLE events ADD PROJECTION IF NOT EXISTS by_name (SELECT * ORDER BY name)
AlterQuery  events (children 2)
 ExpressionList (children 1)
  AlterCommand ADD_PROJECTION (children 1)
   Projection (children 1)
    ProjectionSelectQuery (children 2)
     ExpressionList (children 1)
      Asterisk
     Identifier name
 Identifier events

-- EXPLAIN AST ALTER TABLE events ADD CONSTRAINT IF NOT EXISTS positive CHECK value > 0
AlterQuery  events (children 2)
 ExpressionList (children 1)
  AlterCommand ADD_CONSTRAINT (children 1)
   Constraint (children 1)
    Function greater (children 1)
     ExpressionList (children 2)
      Identifier value
      Literal UInt64_0
 Identifier events

Additional Context

  • ClickHouse version (if relevant): 25.8.30.16 (clickhouse-local)
  • Any other context about the problem:
    • In the ADD case of the alter command parser, only the COLUMN and STATISTICS branches consume IF NOT EXISTS; the INDEX, PROJECTION and CONSTRAINT branches go straight to the name.
    • Found through sqlc (engine: clickhouse, sqlc main at 3ad8b20). A schema file that contains one of these statements fails to load, so sqlc generate stops with the parse error above. Migrations often use ADD INDEX IF NOT EXISTS and ADD PROJECTION IF NOT EXISTS to stay re-runnable, and an applied migration can't be rewritten to drop the clause.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions