|
13 | 13 |
|
14 | 14 | from __future__ import annotations |
15 | 15 |
|
| 16 | +import secrets |
| 17 | + |
| 18 | +import sqlglot |
16 | 19 | from sqlglot import exp |
17 | 20 |
|
18 | 21 | from fakesnow.transforms.transforms import SUCCESS_NOP |
19 | 22 |
|
20 | 23 |
|
21 | | -def alter_table_add_multiple_columns(expression: exp.Expression) -> list[exp.Expression]: |
| 24 | +def alter_table_add_multiple_columns( |
| 25 | + expression: exp.Expression, |
| 26 | +) -> list[exp.Expression]: |
22 | 27 | """Transform ALTER TABLE ADD COLUMN with multiple columns into separate statements. |
23 | 28 |
|
24 | 29 | Snowflake supports: ALTER TABLE IF EXISTS tab1 ADD COLUMN IF NOT EXISTS col1 INT, col2 VARCHAR(50), col3 BOOLEAN; |
@@ -82,3 +87,99 @@ def alter_table_strip_cluster_by(expression: exp.Expression) -> exp.Expression: |
82 | 87 | ): |
83 | 88 | return SUCCESS_NOP |
84 | 89 | return expression |
| 90 | + |
| 91 | + |
| 92 | +def create_table_autoincrement( |
| 93 | + expression: exp.Expression, |
| 94 | +) -> list[exp.Expression]: |
| 95 | + """Split CREATE TABLE with AUTOINCREMENT into CREATE SEQUENCE + CREATE TABLE with DEFAULT NEXTVAL. |
| 96 | +
|
| 97 | + Example transform: |
| 98 | + CREATE TABLE test_table (id NUMERIC NOT NULL AUTOINCREMENT, name VARCHAR) |
| 99 | + -> |
| 100 | + CREATE SEQUENCE test_table_id_seq START 1; |
| 101 | + CREATE TABLE test_table (id NUMERIC NOT NULL DEFAULT NEXTVAL('test_table_id_seq'), name VARCHAR) |
| 102 | + """ |
| 103 | + |
| 104 | + if not ( |
| 105 | + isinstance(expression, exp.Create) |
| 106 | + and expression.kind == "TABLE" |
| 107 | + and (schema := expression.this) |
| 108 | + and (table := schema.this) |
| 109 | + and isinstance(schema, exp.Schema) |
| 110 | + and isinstance(table, exp.Table) |
| 111 | + # Find AUTOINCREMENT/IDENTITY columns |
| 112 | + and ( |
| 113 | + auto_cols := [ |
| 114 | + cd |
| 115 | + for cd in (schema.expressions or []) |
| 116 | + if isinstance(cd, exp.ColumnDef) |
| 117 | + and (cd.find(exp.AutoIncrementColumnConstraint) or cd.find(exp.GeneratedAsIdentityColumnConstraint)) |
| 118 | + ] |
| 119 | + ) |
| 120 | + ): |
| 121 | + return [expression] |
| 122 | + |
| 123 | + if len(auto_cols) > 1: |
| 124 | + raise NotImplementedError("Multiple AUTOINCREMENT columns") |
| 125 | + |
| 126 | + auto = auto_cols[0] |
| 127 | + col_name = auto.this.name |
| 128 | + table_name = table.name |
| 129 | + # When recreating the same table with a sequence, we need to give the sequence a unique name to avoid |
| 130 | + # Dependency Error: Cannot drop entry "_FS_SEQ_..." because there are entries that depend on it. |
| 131 | + random_suffix = secrets.token_hex(4) |
| 132 | + seq_name = f"_fs_seq_{table_name}_{col_name}_{random_suffix}" |
| 133 | + |
| 134 | + # Build CREATE SEQUENCE, using START/INCREMENT if provided |
| 135 | + start_val = "1" |
| 136 | + increment_val = "1" |
| 137 | + |
| 138 | + identity = auto.find(exp.GeneratedAsIdentityColumnConstraint) |
| 139 | + if identity: |
| 140 | + s = identity.args.get("start") |
| 141 | + i = identity.args.get("increment") |
| 142 | + if isinstance(s, exp.Literal): |
| 143 | + start_val = s.this |
| 144 | + if isinstance(i, exp.Literal): |
| 145 | + increment_val = i.this |
| 146 | + |
| 147 | + seq_stmt = sqlglot.parse_one( |
| 148 | + f"CREATE SEQUENCE {seq_name} START WITH {start_val} INCREMENT BY {increment_val}", |
| 149 | + read="duckdb", |
| 150 | + ) |
| 151 | + |
| 152 | + # Build modified CREATE TABLE with DEFAULT NEXTVAL('<seq_name>') and without AUTOINCREMENT |
| 153 | + new_create: exp.Create = expression.copy() |
| 154 | + new_schema: exp.Schema = new_create.this |
| 155 | + |
| 156 | + # Find the corresponding column in the copied schema |
| 157 | + target_col = next( |
| 158 | + cd for cd in (new_schema.expressions or []) if isinstance(cd, exp.ColumnDef) and cd.this.name == col_name |
| 159 | + ) |
| 160 | + |
| 161 | + existing_constraints: list[exp.Expression] = target_col.args.get("constraints", []) or [] |
| 162 | + |
| 163 | + # Replace the AUTOINCREMENT/IDENTITY constraint in-place with DEFAULT NEXTVAL('<seq_name>') |
| 164 | + for c in existing_constraints: |
| 165 | + if isinstance(c, exp.ColumnConstraint) and isinstance( |
| 166 | + c.args.get("kind"), |
| 167 | + ( |
| 168 | + exp.AutoIncrementColumnConstraint, |
| 169 | + exp.GeneratedAsIdentityColumnConstraint, |
| 170 | + ), |
| 171 | + ): |
| 172 | + c.set( |
| 173 | + "kind", |
| 174 | + exp.DefaultColumnConstraint( |
| 175 | + this=exp.Anonymous( |
| 176 | + this="NEXTVAL", |
| 177 | + expressions=[exp.Literal(this=seq_name, is_string=True)], |
| 178 | + ) |
| 179 | + ), |
| 180 | + ) |
| 181 | + break |
| 182 | + |
| 183 | + target_col.set("constraints", existing_constraints) |
| 184 | + |
| 185 | + return [seq_stmt, new_create] |
0 commit comments