TestTranspile_JSONOperators_CreateTableAs is the regression test for the CREATE TABLE AS / CREATE OR REPLACE TABLE AS path. Before the fix, OperatorTransform.Transform dispatched only on SELECT/INSERT/UPDATE/DELETE, so it never descended into a CreateTableAsStmt's AS-SELECT body. A chained JSON arr
(t *testing.T)
| 2307 | // TABLE through the transpile pipeline. Converting the arrows to |
| 2308 | // json_extract/json_extract_string makes the extraction precedence-immune. |
| 2309 | func TestTranspile_JSONOperators_CreateTableAs(t *testing.T) { |
| 2310 | tr := New(DefaultConfig()) |
| 2311 | |
| 2312 | t.Run("exact: CREATE TABLE AS double arrow", func(t *testing.T) { |
| 2313 | in := "CREATE TABLE x AS SELECT data->>'key' FROM t" |
| 2314 | want := "CREATE TABLE x AS SELECT json_extract_string(data, 'key') FROM t" |
| 2315 | got, err := tr.Transpile(in) |
| 2316 | if err != nil { |
| 2317 | t.Fatalf("Transpile(%q): %v", in, err) |
| 2318 | } |
| 2319 | if got.SQL != want { |
| 2320 | t.Errorf("\n got: %q\n want: %q", got.SQL, want) |
| 2321 | } |
| 2322 | }) |
| 2323 | |
| 2324 | cases := []struct { |
| 2325 | name string |
| 2326 | input string |
| 2327 | contains []string |
| 2328 | excludes []string |
| 2329 | }{ |
| 2330 | { |
| 2331 | name: "CREATE OR REPLACE TABLE AS converts arrows in body + WHERE", |
| 2332 | input: "CREATE OR REPLACE TABLE x AS SELECT id, data->>'name' AS n FROM t WHERE flag AND data->>'k' = 'v'", |
| 2333 | contains: []string{"CREATE OR REPLACE TABLE", "json_extract_string(data, 'name')", "json_extract_string(data, 'k')"}, |
| 2334 | excludes: []string{"->"}, // no raw arrow operator may survive |
| 2335 | }, |
| 2336 | { |
| 2337 | // The SQLMesh shape that failed in prod: chained arrow inside a CASE, |
| 2338 | // inside a CTE, inside CREATE OR REPLACE TABLE AS, in an AND context. |
| 2339 | name: "chained arrow in CASE in CTE in CREATE OR REPLACE TABLE AS", |
| 2340 | input: "CREATE OR REPLACE TABLE core AS WITH ue AS (" + |
| 2341 | "SELECT CASE WHEN NOT b IS NULL AND (b -> '$.id') ->> '$.value' LIKE 'usr_%' " + |
| 2342 | "THEN (b -> '$.id') ->> '$.value' ELSE NULL END AS user_id FROM src" + |
| 2343 | ") SELECT * FROM ue", |
| 2344 | contains: []string{"json_extract_string(json_extract(b, '$.id'), '$.value')"}, |
| 2345 | excludes: []string{"->"}, // the bug: a bare arrow surviving the AND |
| 2346 | }, |
| 2347 | { |
| 2348 | name: "arrow in GROUP BY / ORDER BY inside CTAS", |
| 2349 | input: "CREATE TABLE x AS SELECT data->>'type' AS t, count(*) AS c FROM src GROUP BY data->>'type' ORDER BY data->>'rank'", |
| 2350 | contains: []string{"json_extract_string(data, 'type')", "json_extract_string(data, 'rank')"}, |
| 2351 | excludes: []string{"->"}, |
| 2352 | }, |
| 2353 | { |
| 2354 | name: "CREATE OR REPLACE VIEW converts arrows", |
| 2355 | input: "CREATE OR REPLACE VIEW v AS SELECT id FROM t WHERE flag AND data->>'k' = 'v'", |
| 2356 | contains: []string{"CREATE OR REPLACE VIEW", "json_extract_string(data, 'k')"}, |
| 2357 | excludes: []string{"->"}, |
| 2358 | }, |
| 2359 | } |
| 2360 | for _, tt := range cases { |
| 2361 | t.Run(tt.name, func(t *testing.T) { |
| 2362 | got, err := tr.Transpile(tt.input) |
| 2363 | if err != nil { |
| 2364 | t.Fatalf("Transpile(%q): %v", tt.input, err) |
| 2365 | } |
| 2366 | for _, c := range tt.contains { |
nothing calls this directly
no test coverage detected