Commit 26e9294
authored
Add post-processing SQL over semantic query results (#135)
* Add post-processing SQL over semantic query results
Support arbitrary SQL (CASE, window functions, arithmetic, etc.) on top
of semantic query results via subquery wrapping. The rewriter now walks
the query tree recursively so nested subqueries and JOIN subqueries that
reference semantic models are compiled correctly.
Also adds a post_process parameter to compile() and query() for the
Python API path, with automatic CTE hoisting.
* Preserve root semantic rewrite when JOIN has subquery
When the root FROM references a semantic model and a JOIN contains a
subquery, the routing into _rewrite_with_ctes_or_subqueries must still
apply _rewrite_simple_query to the root SELECT so the explicit JOIN
guard and semantic rewriting are not bypassed.
* Keep CTE scope in root semantic rewrite and merge post_process CTEs
Fix two issues from PR review:
1. When the root SELECT references a semantic model and has user-defined
CTEs, _rewrite_simple_query was discarding them. Now user CTEs are
saved before rewriting and merged back into the generated SQL so
filter references like IN (SELECT ... FROM user_cte) remain valid.
2. When post_process SQL has its own WITH clause and the inner semantic
query also produces CTEs, the two WITH clauses are now merged into
one instead of producing invalid double-WITH SQL.
* Preserve WITH RECURSIVE when merging root CTEs
Propagate the recursive flag from user-defined CTEs to the merged WITH
clause so self-referencing CTEs execute correctly.
* Handle CTE name collisions between user and generated CTEs
Two changes:
1. post_process path: remove CTE hoisting entirely. Inner SQL (with
CTEs) is placed directly in the subquery position. CTEs inside
subqueries are valid in all target databases and naturally scoped,
so name collisions with post_process CTEs cannot occur.
2. Root semantic + user CTEs: detect name collisions between user CTEs
and generated CTEs, raising a clear error instead of producing
invalid SQL. Walk-based renaming was too aggressive (renamed user
CTE references inside filter subqueries).1 parent 98406be commit 26e9294
3 files changed
Lines changed: 493 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
435 | 435 | | |
436 | 436 | | |
437 | 437 | | |
| 438 | + | |
438 | 439 | | |
439 | 440 | | |
440 | 441 | | |
| |||
448 | 449 | | |
449 | 450 | | |
450 | 451 | | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
451 | 455 | | |
452 | 456 | | |
453 | 457 | | |
| |||
462 | 466 | | |
463 | 467 | | |
464 | 468 | | |
| 469 | + | |
465 | 470 | | |
466 | 471 | | |
467 | 472 | | |
| |||
479 | 484 | | |
480 | 485 | | |
481 | 486 | | |
| 487 | + | |
482 | 488 | | |
483 | 489 | | |
484 | 490 | | |
| |||
493 | 499 | | |
494 | 500 | | |
495 | 501 | | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
496 | 505 | | |
497 | 506 | | |
498 | 507 | | |
| |||
520 | 529 | | |
521 | 530 | | |
522 | 531 | | |
523 | | - | |
| 532 | + | |
524 | 533 | | |
525 | 534 | | |
526 | 535 | | |
| |||
533 | 542 | | |
534 | 543 | | |
535 | 544 | | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
536 | 563 | | |
537 | 564 | | |
538 | 565 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
121 | 122 | | |
122 | | - | |
| 123 | + | |
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
| |||
1851 | 1852 | | |
1852 | 1853 | | |
1853 | 1854 | | |
1854 | | - | |
1855 | | - | |
1856 | | - | |
1857 | | - | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
1858 | 1859 | | |
1859 | | - | |
1860 | | - | |
1861 | | - | |
1862 | | - | |
1863 | | - | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
1864 | 1910 | | |
1865 | 1911 | | |
1866 | | - | |
| 1912 | + | |
1867 | 1913 | | |
1868 | | - | |
1869 | | - | |
1870 | | - | |
1871 | | - | |
1872 | | - | |
1873 | | - | |
1874 | | - | |
1875 | | - | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
1876 | 1919 | | |
1877 | 1920 | | |
1878 | 1921 | | |
1879 | | - | |
1880 | | - | |
1881 | | - | |
1882 | | - | |
1883 | | - | |
1884 | | - | |
1885 | | - | |
1886 | | - | |
1887 | | - | |
1888 | | - | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
1889 | 1938 | | |
1890 | 1939 | | |
1891 | 1940 | | |
| |||
0 commit comments