sqlingo 0.15.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/chunk-2JAK34HX.js +3 -0
  3. package/dist/{chunk-QBQHDSEC.js.map → chunk-2JAK34HX.js.map} +1 -1
  4. package/dist/{chunk-I3W3TYLQ.js → chunk-4OP3W4FC.js} +2 -2
  5. package/dist/{chunk-SBCEJTHD.js → chunk-4OTG4X3J.js} +2 -2
  6. package/dist/{chunk-EZ7VKF2X.js → chunk-5FKQ7A6G.js} +2 -2
  7. package/dist/chunk-5THLNRNM.cjs +2 -0
  8. package/dist/{chunk-KCIBVGFH.cjs.map → chunk-5THLNRNM.cjs.map} +1 -1
  9. package/dist/{chunk-XHLLQOF5.js → chunk-64O4OPUU.js} +2 -2
  10. package/dist/chunk-67DW3T46.js +4 -0
  11. package/dist/chunk-67DW3T46.js.map +1 -0
  12. package/dist/chunk-6KUVOUCW.cjs +2 -0
  13. package/dist/{chunk-3QM5KG7E.cjs.map → chunk-6KUVOUCW.cjs.map} +1 -1
  14. package/dist/chunk-7UD5CTLX.cjs +19 -0
  15. package/dist/chunk-7UD5CTLX.cjs.map +1 -0
  16. package/dist/{chunk-QZPWUNHV.js → chunk-A5G7IMQK.js} +2 -2
  17. package/dist/chunk-BITCYYS5.cjs +2 -0
  18. package/dist/{chunk-OEILKTSQ.cjs.map → chunk-BITCYYS5.cjs.map} +1 -1
  19. package/dist/chunk-CUS24L3N.js +2 -0
  20. package/dist/{chunk-RCVHOYS7.js.map → chunk-CUS24L3N.js.map} +1 -1
  21. package/dist/chunk-D25ZGOM2.cjs +2 -0
  22. package/dist/{chunk-AHVZOF3N.cjs.map → chunk-D25ZGOM2.cjs.map} +1 -1
  23. package/dist/chunk-EHRFM7IA.cjs +3 -0
  24. package/dist/{chunk-T4W65P5Z.cjs.map → chunk-EHRFM7IA.cjs.map} +1 -1
  25. package/dist/chunk-G26NEHSB.cjs +2 -0
  26. package/dist/{chunk-Z3MZONPC.cjs.map → chunk-G26NEHSB.cjs.map} +1 -1
  27. package/dist/chunk-G7GKKZ2Z.cjs +2 -0
  28. package/dist/{chunk-L3QLTSDN.cjs.map → chunk-G7GKKZ2Z.cjs.map} +1 -1
  29. package/dist/{chunk-OY3TUTWZ.js → chunk-GGYOWED7.js} +2 -2
  30. package/dist/chunk-GMKLNR4T.cjs +2 -0
  31. package/dist/{chunk-FERVZ62T.cjs.map → chunk-GMKLNR4T.cjs.map} +1 -1
  32. package/dist/{chunk-6KKXEKNM.js → chunk-H6VPT4VP.js} +2 -2
  33. package/dist/{chunk-5UEGDJPA.js → chunk-IJFK5335.js} +2 -2
  34. package/dist/chunk-LCBD2UXS.js +19 -0
  35. package/dist/chunk-LCBD2UXS.js.map +1 -0
  36. package/dist/{chunk-AXLGFQDQ.js → chunk-LLQJXXH6.js} +2 -2
  37. package/dist/{chunk-3AQZGJOE.js → chunk-NAVY4G2K.js} +2 -2
  38. package/dist/chunk-NDIDRDXO.cjs +2 -0
  39. package/dist/{chunk-WEST5GIF.cjs.map → chunk-NDIDRDXO.cjs.map} +1 -1
  40. package/dist/chunk-OV5NMNJ2.cjs +2 -0
  41. package/dist/{chunk-3UWWG7AW.cjs.map → chunk-OV5NMNJ2.cjs.map} +1 -1
  42. package/dist/{chunk-HN5OT37U.js → chunk-PMAPVROX.js} +2 -2
  43. package/dist/chunk-Q3PM5YRJ.cjs +2 -0
  44. package/dist/{chunk-GVRTGQV6.cjs.map → chunk-Q3PM5YRJ.cjs.map} +1 -1
  45. package/dist/chunk-QEQ3Y6LY.cjs +2 -0
  46. package/dist/{chunk-MVIZ3OGF.cjs.map → chunk-QEQ3Y6LY.cjs.map} +1 -1
  47. package/dist/chunk-R77F352G.cjs +2 -0
  48. package/dist/{chunk-MFCEAG7X.cjs.map → chunk-R77F352G.cjs.map} +1 -1
  49. package/dist/chunk-RGHZFBPX.cjs +2 -0
  50. package/dist/{chunk-3QPO4PHG.cjs.map → chunk-RGHZFBPX.cjs.map} +1 -1
  51. package/dist/{chunk-4GEW5JA6.js → chunk-RNKKNWL6.js} +2 -2
  52. package/dist/{chunk-U2DYJPRT.js → chunk-RZCZUHMK.js} +2 -2
  53. package/dist/chunk-UMHZAUZU.cjs +2 -0
  54. package/dist/{chunk-4OWUZIBT.cjs.map → chunk-UMHZAUZU.cjs.map} +1 -1
  55. package/dist/{chunk-BLUVFIA7.cjs → chunk-VAXCS6SH.cjs} +2 -2
  56. package/dist/{chunk-BLUVFIA7.cjs.map → chunk-VAXCS6SH.cjs.map} +1 -1
  57. package/dist/{chunk-JLPP24SP.js → chunk-W2TEB56Y.js} +2 -2
  58. package/dist/{chunk-UN6XWES6.js → chunk-YAQ7F76P.js} +2 -2
  59. package/dist/chunk-YBZUKWAG.cjs +4 -0
  60. package/dist/chunk-YBZUKWAG.cjs.map +1 -0
  61. package/dist/chunk-ZJVBOKDB.cjs +2 -0
  62. package/dist/{chunk-57QCCXIJ.cjs.map → chunk-ZJVBOKDB.cjs.map} +1 -1
  63. package/dist/dialects/athena.cjs +1 -1
  64. package/dist/dialects/athena.d.cts +1 -1
  65. package/dist/dialects/athena.d.ts +1 -1
  66. package/dist/dialects/athena.js +1 -1
  67. package/dist/dialects/bigquery.cjs +1 -1
  68. package/dist/dialects/bigquery.d.cts +1 -1
  69. package/dist/dialects/bigquery.d.ts +1 -1
  70. package/dist/dialects/bigquery.js +1 -1
  71. package/dist/dialects/clickhouse.cjs +1 -1
  72. package/dist/dialects/clickhouse.d.cts +1 -1
  73. package/dist/dialects/clickhouse.d.ts +1 -1
  74. package/dist/dialects/clickhouse.js +1 -1
  75. package/dist/dialects/databricks.cjs +1 -1
  76. package/dist/dialects/databricks.d.cts +1 -1
  77. package/dist/dialects/databricks.d.ts +1 -1
  78. package/dist/dialects/databricks.js +1 -1
  79. package/dist/dialects/doris.cjs +1 -1
  80. package/dist/dialects/doris.d.cts +1 -1
  81. package/dist/dialects/doris.d.ts +1 -1
  82. package/dist/dialects/doris.js +1 -1
  83. package/dist/dialects/dremio.cjs +1 -1
  84. package/dist/dialects/dremio.d.cts +1 -1
  85. package/dist/dialects/dremio.d.ts +1 -1
  86. package/dist/dialects/dremio.js +1 -1
  87. package/dist/dialects/drill.cjs +1 -1
  88. package/dist/dialects/drill.d.cts +1 -1
  89. package/dist/dialects/drill.d.ts +1 -1
  90. package/dist/dialects/drill.js +1 -1
  91. package/dist/dialects/druid.cjs +1 -1
  92. package/dist/dialects/druid.d.cts +1 -1
  93. package/dist/dialects/druid.d.ts +1 -1
  94. package/dist/dialects/druid.js +1 -1
  95. package/dist/dialects/duckdb.cjs +13 -13
  96. package/dist/dialects/duckdb.d.cts +1 -1
  97. package/dist/dialects/duckdb.d.ts +1 -1
  98. package/dist/dialects/duckdb.js +1 -1
  99. package/dist/dialects/dune.cjs +1 -1
  100. package/dist/dialects/dune.d.cts +1 -1
  101. package/dist/dialects/dune.d.ts +1 -1
  102. package/dist/dialects/dune.js +1 -1
  103. package/dist/dialects/exasol.cjs +1 -1
  104. package/dist/dialects/exasol.d.cts +1 -1
  105. package/dist/dialects/exasol.d.ts +1 -1
  106. package/dist/dialects/exasol.js +1 -1
  107. package/dist/dialects/fabric.cjs +1 -1
  108. package/dist/dialects/fabric.d.cts +1 -1
  109. package/dist/dialects/fabric.d.ts +1 -1
  110. package/dist/dialects/fabric.js +1 -1
  111. package/dist/dialects/hive.cjs +1 -1
  112. package/dist/dialects/hive.d.cts +1 -1
  113. package/dist/dialects/hive.d.ts +1 -1
  114. package/dist/dialects/hive.js +1 -1
  115. package/dist/dialects/materialize.cjs +1 -1
  116. package/dist/dialects/materialize.d.cts +1 -1
  117. package/dist/dialects/materialize.d.ts +1 -1
  118. package/dist/dialects/materialize.js +1 -1
  119. package/dist/dialects/mysql.cjs +1 -1
  120. package/dist/dialects/mysql.d.cts +1 -1
  121. package/dist/dialects/mysql.d.ts +1 -1
  122. package/dist/dialects/mysql.js +1 -1
  123. package/dist/dialects/oracle.cjs +1 -1
  124. package/dist/dialects/oracle.cjs.map +1 -1
  125. package/dist/dialects/oracle.d.cts +1 -1
  126. package/dist/dialects/oracle.d.ts +1 -1
  127. package/dist/dialects/oracle.js +1 -1
  128. package/dist/dialects/oracle.js.map +1 -1
  129. package/dist/dialects/postgres.cjs +1 -1
  130. package/dist/dialects/postgres.d.cts +1 -1
  131. package/dist/dialects/postgres.d.ts +1 -1
  132. package/dist/dialects/postgres.js +1 -1
  133. package/dist/dialects/presto.cjs +1 -1
  134. package/dist/dialects/presto.d.cts +1 -1
  135. package/dist/dialects/presto.d.ts +1 -1
  136. package/dist/dialects/presto.js +1 -1
  137. package/dist/dialects/prql.cjs +1 -1
  138. package/dist/dialects/prql.d.cts +1 -1
  139. package/dist/dialects/prql.d.ts +1 -1
  140. package/dist/dialects/prql.js +1 -1
  141. package/dist/dialects/redshift.cjs +1 -1
  142. package/dist/dialects/redshift.d.cts +1 -1
  143. package/dist/dialects/redshift.d.ts +1 -1
  144. package/dist/dialects/redshift.js +1 -1
  145. package/dist/dialects/risingwave.cjs +1 -1
  146. package/dist/dialects/risingwave.d.cts +1 -1
  147. package/dist/dialects/risingwave.d.ts +1 -1
  148. package/dist/dialects/risingwave.js +1 -1
  149. package/dist/dialects/singlestore.cjs +1 -1
  150. package/dist/dialects/singlestore.d.cts +1 -1
  151. package/dist/dialects/singlestore.d.ts +1 -1
  152. package/dist/dialects/singlestore.js +1 -1
  153. package/dist/dialects/snowflake.cjs +1 -1
  154. package/dist/dialects/snowflake.d.cts +1 -1
  155. package/dist/dialects/snowflake.d.ts +1 -1
  156. package/dist/dialects/snowflake.js +1 -1
  157. package/dist/dialects/solr.cjs +1 -1
  158. package/dist/dialects/solr.d.cts +1 -1
  159. package/dist/dialects/solr.d.ts +1 -1
  160. package/dist/dialects/solr.js +1 -1
  161. package/dist/dialects/spark.cjs +1 -1
  162. package/dist/dialects/spark.d.cts +1 -1
  163. package/dist/dialects/spark.d.ts +1 -1
  164. package/dist/dialects/spark.js +1 -1
  165. package/dist/dialects/spark2.cjs +1 -1
  166. package/dist/dialects/spark2.d.cts +1 -1
  167. package/dist/dialects/spark2.d.ts +1 -1
  168. package/dist/dialects/spark2.js +1 -1
  169. package/dist/dialects/sqlite.cjs +1 -1
  170. package/dist/dialects/sqlite.d.cts +1 -1
  171. package/dist/dialects/sqlite.d.ts +1 -1
  172. package/dist/dialects/sqlite.js +1 -1
  173. package/dist/dialects/starrocks.cjs +1 -1
  174. package/dist/dialects/starrocks.d.cts +1 -1
  175. package/dist/dialects/starrocks.d.ts +1 -1
  176. package/dist/dialects/starrocks.js +1 -1
  177. package/dist/dialects/tableau.cjs +1 -1
  178. package/dist/dialects/tableau.d.cts +1 -1
  179. package/dist/dialects/tableau.d.ts +1 -1
  180. package/dist/dialects/tableau.js +1 -1
  181. package/dist/dialects/teradata.cjs +1 -1
  182. package/dist/dialects/teradata.d.cts +1 -1
  183. package/dist/dialects/teradata.d.ts +1 -1
  184. package/dist/dialects/teradata.js +1 -1
  185. package/dist/dialects/trino.cjs +1 -1
  186. package/dist/dialects/trino.d.cts +1 -1
  187. package/dist/dialects/trino.d.ts +1 -1
  188. package/dist/dialects/trino.js +1 -1
  189. package/dist/dialects/tsql.cjs +1 -1
  190. package/dist/dialects/tsql.d.cts +1 -1
  191. package/dist/dialects/tsql.d.ts +1 -1
  192. package/dist/dialects/tsql.js +1 -1
  193. package/dist/index.cjs +5 -5
  194. package/dist/index.cjs.map +1 -1
  195. package/dist/index.d.cts +4 -46
  196. package/dist/index.d.ts +4 -46
  197. package/dist/index.js +4 -4
  198. package/dist/index.js.map +1 -1
  199. package/dist/optimizer/index.cjs +2 -0
  200. package/dist/optimizer/index.cjs.map +1 -0
  201. package/dist/optimizer/index.d.cts +799 -0
  202. package/dist/optimizer/index.d.ts +799 -0
  203. package/dist/optimizer/index.js +2 -0
  204. package/dist/optimizer/index.js.map +1 -0
  205. package/dist/optimizer-D0bukogM.d.ts +50 -0
  206. package/dist/optimizer-DJ7THBIw.d.cts +50 -0
  207. package/dist/{tokens-BXP7BR0y.d.ts → tokens-CkPMs-EM.d.cts} +67 -5
  208. package/dist/{tokens-BXP7BR0y.d.cts → tokens-CkPMs-EM.d.ts} +67 -5
  209. package/package.json +6 -1
  210. package/dist/chunk-3QM5KG7E.cjs +0 -2
  211. package/dist/chunk-3QPO4PHG.cjs +0 -2
  212. package/dist/chunk-3UWWG7AW.cjs +0 -2
  213. package/dist/chunk-4OWUZIBT.cjs +0 -2
  214. package/dist/chunk-57QCCXIJ.cjs +0 -2
  215. package/dist/chunk-AHVZOF3N.cjs +0 -2
  216. package/dist/chunk-DI2QZRFR.cjs +0 -4
  217. package/dist/chunk-DI2QZRFR.cjs.map +0 -1
  218. package/dist/chunk-FERVZ62T.cjs +0 -2
  219. package/dist/chunk-GVRTGQV6.cjs +0 -2
  220. package/dist/chunk-KCIBVGFH.cjs +0 -2
  221. package/dist/chunk-L3QLTSDN.cjs +0 -2
  222. package/dist/chunk-LXNDZZBH.js +0 -19
  223. package/dist/chunk-LXNDZZBH.js.map +0 -1
  224. package/dist/chunk-MFCEAG7X.cjs +0 -2
  225. package/dist/chunk-MVIZ3OGF.cjs +0 -2
  226. package/dist/chunk-NJEMK7XN.js +0 -4
  227. package/dist/chunk-NJEMK7XN.js.map +0 -1
  228. package/dist/chunk-OEILKTSQ.cjs +0 -2
  229. package/dist/chunk-QBQHDSEC.js +0 -3
  230. package/dist/chunk-RCVHOYS7.js +0 -2
  231. package/dist/chunk-T4W65P5Z.cjs +0 -3
  232. package/dist/chunk-WEST5GIF.cjs +0 -2
  233. package/dist/chunk-Y3TUKXK4.cjs +0 -19
  234. package/dist/chunk-Y3TUKXK4.cjs.map +0 -1
  235. package/dist/chunk-Z3MZONPC.cjs +0 -2
  236. /package/dist/{chunk-I3W3TYLQ.js.map → chunk-4OP3W4FC.js.map} +0 -0
  237. /package/dist/{chunk-SBCEJTHD.js.map → chunk-4OTG4X3J.js.map} +0 -0
  238. /package/dist/{chunk-EZ7VKF2X.js.map → chunk-5FKQ7A6G.js.map} +0 -0
  239. /package/dist/{chunk-XHLLQOF5.js.map → chunk-64O4OPUU.js.map} +0 -0
  240. /package/dist/{chunk-QZPWUNHV.js.map → chunk-A5G7IMQK.js.map} +0 -0
  241. /package/dist/{chunk-OY3TUTWZ.js.map → chunk-GGYOWED7.js.map} +0 -0
  242. /package/dist/{chunk-6KKXEKNM.js.map → chunk-H6VPT4VP.js.map} +0 -0
  243. /package/dist/{chunk-5UEGDJPA.js.map → chunk-IJFK5335.js.map} +0 -0
  244. /package/dist/{chunk-AXLGFQDQ.js.map → chunk-LLQJXXH6.js.map} +0 -0
  245. /package/dist/{chunk-3AQZGJOE.js.map → chunk-NAVY4G2K.js.map} +0 -0
  246. /package/dist/{chunk-HN5OT37U.js.map → chunk-PMAPVROX.js.map} +0 -0
  247. /package/dist/{chunk-4GEW5JA6.js.map → chunk-RNKKNWL6.js.map} +0 -0
  248. /package/dist/{chunk-U2DYJPRT.js.map → chunk-RZCZUHMK.js.map} +0 -0
  249. /package/dist/{chunk-JLPP24SP.js.map → chunk-W2TEB56Y.js.map} +0 -0
  250. /package/dist/{chunk-UN6XWES6.js.map → chunk-YAQ7F76P.js.map} +0 -0
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkY3TUKXK4cjs = require('./chunk-Y3TUKXK4.cjs');var v,s;v=[_chunkY3TUKXK4cjs.s];var t=class t{static get EXPRESSION_METADATA(){let r=new Map(_chunkY3TUKXK4cjs.$v.EXPRESSION_METADATA),e=_chunkY3TUKXK4cjs.a.call(void 0, (p,a)=>{for(let z of p)r.set(z,a)},"extend");return e([_chunkY3TUKXK4cjs.Kp,_chunkY3TUKXK4cjs.en,_chunkY3TUKXK4cjs.Wn,_chunkY3TUKXK4cjs.on,_chunkY3TUKXK4cjs.mn,_chunkY3TUKXK4cjs.nn,_chunkY3TUKXK4cjs.pn,_chunkY3TUKXK4cjs.Dn,_chunkY3TUKXK4cjs.Go,_chunkY3TUKXK4cjs.Ip,_chunkY3TUKXK4cjs.Ho,_chunkY3TUKXK4cjs.eq,_chunkY3TUKXK4cjs.Dq,_chunkY3TUKXK4cjs.Io,_chunkY3TUKXK4cjs.ts,_chunkY3TUKXK4cjs.ys],{returns:"bigint"}),e([_chunkY3TUKXK4cjs.ut,_chunkY3TUKXK4cjs.fo],{returns:"int128"}),e([_chunkY3TUKXK4cjs.ok,_chunkY3TUKXK4cjs.Gk,_chunkY3TUKXK4cjs.Qr],{returns:"double"}),e([_chunkY3TUKXK4cjs.Vo,_chunkY3TUKXK4cjs.Hp],{returns:"varchar"}),r.set(_chunkY3TUKXK4cjs.cn,{annotator:_chunkY3TUKXK4cjs.a.call(void 0, (p,a)=>p.annotateByArgs(a,["expression"]),"annotator")}),r.set(_chunkY3TUKXK4cjs.Mm,{returns:"timestamp"}),r.set(_chunkY3TUKXK4cjs.rn,{returns:"interval"}),r.set(_chunkY3TUKXK4cjs.Rn,{returns:"time"}),r}};s=_chunkY3TUKXK4cjs.b.call(void 0, null),_chunkY3TUKXK4cjs.e.call(void 0, s,10,"EXPRESSION_METADATA",v,t),_chunkY3TUKXK4cjs.c.call(void 0, s,t),_chunkY3TUKXK4cjs.d.call(void 0, s,3,t),_chunkY3TUKXK4cjs.a.call(void 0, t,"DuckDbTyping");var g=t;exports.a = g;
2
- //# sourceMappingURL=chunk-MFCEAG7X.cjs.map
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkY3TUKXK4cjs = require('./chunk-Y3TUKXK4.cjs');var w,n;w=[_chunkY3TUKXK4cjs.s];var r=class r{static get EXPRESSION_METADATA(){let t=new Map(_chunkY3TUKXK4cjs.$v.EXPRESSION_METADATA),o=_chunkY3TUKXK4cjs.a.call(void 0, (s,p)=>{for(let z of s)t.set(z,p)},"extend");return o([_chunkY3TUKXK4cjs.ik,_chunkY3TUKXK4cjs.kk,_chunkY3TUKXK4cjs.mk,_chunkY3TUKXK4cjs.ok,_chunkY3TUKXK4cjs.rk,_chunkY3TUKXK4cjs.pk,_chunkY3TUKXK4cjs.wk,_chunkY3TUKXK4cjs.yk],{returns:"float"}),o([_chunkY3TUKXK4cjs.pr,_chunkY3TUKXK4cjs.Lr],{returns:"varchar"}),o([_chunkY3TUKXK4cjs.Ak,_chunkY3TUKXK4cjs.Xq],{annotator:_chunkY3TUKXK4cjs.a.call(void 0, (s,p)=>s.annotateByArgs(p,["this"]),"annotator")}),t.set(_chunkY3TUKXK4cjs.Qm,{returns:"nvarchar"}),t.set(_chunkY3TUKXK4cjs.Om,{returns:"datetime"}),t}};n=_chunkY3TUKXK4cjs.b.call(void 0, null),_chunkY3TUKXK4cjs.e.call(void 0, n,10,"EXPRESSION_METADATA",w,r),_chunkY3TUKXK4cjs.c.call(void 0, n,r),_chunkY3TUKXK4cjs.d.call(void 0, n,3,r),_chunkY3TUKXK4cjs.a.call(void 0, r,"TSQLTyping");var h=r;exports.a = h;
2
- //# sourceMappingURL=chunk-MVIZ3OGF.cjs.map
@@ -1,4 +0,0 @@
1
- import{$a as gs,$c as Q,At as Pe,Au as _,Av as Ms,Bb as Ae,Bj as fe,Bs as De,Bt as Le,Bu as ts,Cd as Xe,Cs as he,Dd as hs,Dj as W,En as Ee,Fa as H,Ff as es,Fi as ne,Fu as I,Ga as q,Gb as Es,Gj as ws,Ha as D,Hi as w,Hu as ue,Ia as V,Ii as P,It as Oe,Ja as Qe,Ji as le,Jr as Os,La as re,Lf as O,Ma as Ge,Mf as As,Na as He,Ne as Z,Oa as ms,Pa as de,Pd as se,Pe as Ss,Qa as Re,Ra as ds,Ri as ee,Sa as L,Si as Cs,Ta as Ye,Ue as ys,Uu as qs,Va as be,Vu as Rs,Wj as Ns,Xa as _e,Xc as z,Yc as ae,Yi as ge,Zi as M,a as h,at as vs,aw as $,bv as Je,cb as ie,ce as U,ck as Ne,cv as B,fk as pe,gc as Y,go as Ue,io as te,jc as F,je as we,jj as Ts,k as J,kv as oe,lc as Ke,m as Be,n as fs,ne as X,o as j,oe as Ze,oj as Me,p as me,pm as xe,q as ps,qe as je,r as qe,rd as Ce,ru as R,s as us,tt as ss,tu as ns,uu as Fe,vg as ce,vu as Is,vv as _s,wd as Te,wf as bs,xb as T,xv as Ks,yb as xs,yd as N,yu as ke,zb as Ve,zv as js}from"./chunk-LXNDZZBH.js";function Us(e){let t=V(e);if(t){let a=t.refCount(),s=[];for(let o of t.traverse())s.push(o);for(let o=s.length-1;0<=o;o--){let r=s[o];if(r.isCte){let c=r;if((a.get(c)||0)<=0){let n=r.expression.parent;if(!n)continue;let l=n.parent;if(n.pop(),l){let f=l.args.expressions;(!f||f.length===0)&&l.pop()}for(let[,f]of Object.entries(r.selectedSources)){let[,p]=f;if(p instanceof q){let m=a.get(p)||0;a.set(p,m-1)}}}}}}return e}h(Us,"eliminateCtes");function os(e){for(let t of D(e)){if(0<t.unqualifiedColumns.length)continue;let a=t.expression.args.joins;if(a)for(let s=a.length-1;0<=s;s--){let o=a[s];if(me(o,N),o.isSemiOrAntiJoin)continue;let r=o.aliasOrName;r&&hn(t,o,r)&&(o.pop(),t.removeSource(r))}}return e}h(os,"eliminateJoins");function hn(e,t,a){let s=e.sources.get(a);if(!(s instanceof q)||Sn(e,t,a))return!1;let o=t.args.side,r=t.args.on;return o==="left"&&yn(s,t)||!r&&Ps(s)}h(hn,"shouldEliminateJoin");function Sn(e,t,a){let s=t.args.on,o=new Set;if(s)for(let c of s.findAll(O))o.add(c);return e.sourceColumns(a).some(c=>!o.has(c))}h(Sn,"joinIsUsed");function yn(e,t){let a=bn(e);if(!a||a.size===0)return!1;let{joinKeys:s}=Ls(t),o=new Set(s.map(r=>r.name));for(let r of a)if(!o.has(r))return!1;return!0}h(yn,"isJoinedOnAllUniqueOutputs");function bn(e){let t=e.expression;if(!(t instanceof w))return;let a=t;if(a.args.distinct)return new Set(a.namedSelects);let s=a.args.group;if(s){let o=new Set((s.args.expressions??[]).filter(n=>n instanceof T).map(n=>n.sql())),r=new Set,c=new Set;for(let n of a.selects){if(!(n instanceof T))continue;let l=n.unalias().sql();o.has(l)&&(r.add(l),c.add(n.aliasOrName))}return[...o].every(n=>r.has(n))?c:new Set}return Ps(e)?new Set(a.namedSelects):new Set}h(bn,"uniqueOutputs");function Ps(e){let t=e.expression;if(!(t instanceof w))return!1;let a=t;return!!(!a.args.from||a.selects.every(o=>o instanceof T?o.unalias()instanceof M:!1)||An(e))}h(Ps,"hasSingleOutputRow");function An(e){let t=e.expression.getArgKey("limit");return t instanceof Te?t.args.expression?.args.this==="1":!1}h(An,"isLimit1");function Ls(e){let t=e.aliasOrName,s=(e.args.on||B()).copy(),o=[],r=[];function c(i){let[n,l]=i.unnestOperands();if(!n||!l)return;let f=Ds(n),p=Ds(l);t&&f.has(t)&&!p.has(t)?(r.push(n),o.push(l),i.replace(B())):t&&p.has(t)&&!f.has(t)&&(r.push(l),o.push(n),i.replace(B()))}if(h(c,"extractCondition"),de(s)){let i=s instanceof Pe?s:Fe([s,B()],{copy:!1});for(let n of i.flatten())n instanceof W&&c(n)}else if(de(s,{dnf:!0})){let i;for(let n of s.flatten()){let l=Array.from(n.flatten()).filter(f=>f instanceof W);if(i===void 0)i=l;else{let f=[];for(let p of l){let m=i.filter(d=>p.sql()===d.sql());0<m.length&&(f.push(p),f.push(...m))}i=f}}if(i)for(let n of i)c(n)}return{sourceKeys:o,joinKeys:r,on:s}}h(Ls,"joinCondition");function Ds(e){let t=new Set;for(let a of e.findAll(O))a.table&&t.add(a.table);return t}h(Ds,"columnTableNames");function rs(e){if(e instanceof P&&e.args.this)return rs(e.args.this),e;let t=V(e);if(!t)return e;let a=new Map;for(let n of t.cteScopes){let l=n.expression.parent;l&&a.set(l.alias,n)}for(let n of t.traverse())for(let[,l]of n.sources)l instanceof U&&a.set(l.name,l);let s=new Map,o=t.expression.args.with,r=!1;if(o){me(o,Y),r=!!o.args.recursive;for(let n of o.args.expressions??[])j(n,es)&&n.args.this&&n.args.this&&s.set(n.args.this.sqlKey,n.alias)}let c=[];for(let n of t.cteScopes){for(let f of n.traverse()){if(f===n)continue;let p=Fs(f,s,a);p&&c.push(p)}let l=n.expression.parent;l&&c.push(l)}let i=[...t.unionScopes,...t.subqueryScopes,...t.tableScopes];for(let n of i)for(let l of n.traverse()){let f=Fs(l,s,a);f&&c.push(f)}return 0<c.length&&(e instanceof Es?e.args.expression:e)?.setArgKey("with",new Y({expressions:c,recursive:r})),e}h(rs,"eliminateSubqueries");function Fs(e,t,a){if(e.isDerivedTable)return Cn(e,t,a);if(e.isCte)return Tn(e,t,a)}h(Fs,"eliminate");function Cn(e,t,a){if(e.parent&&(0<e.parent.pivots.length||e.parent.expression instanceof Ts))return;let s=e.expression.parent;if(!s)return;s instanceof P&&(s=s.unwrap());let[o,r]=ks(e,t,a),c=I(ns(o),s.alias||o,{copy:!1}),n=s.args.joins;return n&&Array.isArray(n)&&c.setArgKey("joins",n),s.replace(c),r}h(Cn,"eliminateDerivedTable");function Tn(e,t,a){let s=e.expression.parent;if(!s)return;let[o,r]=ks(e,t,a),c=s.parent;if(s.pop(),c){let i=c.args.expressions;(!i||i.length===0)&&c.pop()}if(e.parent)for(let i of e.parent.traverse())for(let[,n]of Object.entries(i.selectedSources)){let[l,f]=n;if(f===e){let p=I(ns(o),l.aliasOrName,{copy:!1});l.replace(p)}}return r}h(Tn,"eliminateCte");function ks(e,t,a){let s=t.get(e.expression.sqlKey),r=e.expression.parent?.alias||"";r||(r=be(Array.from(a.keys()),"cte")),s?r=s:a.has(r)&&(r=be(Array.from(a.keys()),r)),a.set(r,e);let c;return s||(t.set(e.expression.sqlKey,r),c=new es({this:e.expression,alias:new F({this:_(r)})})),[r,c]}h(ks,"newCte");function Js(e,t={}){let{leaveTablesIsolated:a=!1}=t;return e=Nn(e,{leaveTablesIsolated:a}),e=On(e,{leaveTablesIsolated:a}),e}h(Js,"mergeSubqueries");var wn=us(()=>[O,W,ge,Ns,Ne]);function Nn(e,t){let{leaveTablesIsolated:a}=t,s=Array.from(D(e)),o=new Map;for(let c of s)for(let[,i]of Object.entries(c.selectedSources)){let[n,l]=i;if(l instanceof q&&l.isCte){let f=o.get(l);f||(f=[],o.set(l,f)),f.push([c,l,n])}}let r=[];for(let[,c]of o)c.length===1&&r.push(c[0]);for(let[c,i,n]of r){let l=n.findAncestor(z,N);if(l&&zs(c,i,{leaveTablesIsolated:a},l)){let f=n.aliasOrName;$s(c,i,f),Ws(c,i,n,f),Qs(c,i,f),Hs(c,i),Bs(c,i,l),Gs(c,i,l),Ys(c,i),vn(i),c.clearCache()}}return e}h(Nn,"mergeCtes");function On(e,t){let{leaveTablesIsolated:a}=t;for(let s of D(e))for(let o of s.derivedTables){let r=o.findAncestor(z,N),c=o.aliasOrName,i=s.sources.get(c);i instanceof q&&r&&zs(s,i,{leaveTablesIsolated:a},r)&&($s(s,i,c),Ws(s,i,o,c),Qs(s,i,c),Hs(s,i),Bs(s,i,r),Gs(s,i,r),Ys(s,i),s.clearCache())}return e}h(On,"mergeDerivedTables");function zs(e,t,a,s){let{leaveTablesIsolated:o}=a,r=t.expression.unnest();function c(){let u=new Set;if(!(r instanceof w))return!1;let g=r;for(let b of g.selects)b instanceof T&&b.find(le)&&u.add(b.aliasOrName);let A=s.aliasOrName,x=[];for(let b of e.columns)b.findAncestor(X,Ce,hs,N,ae,M)!==void 0&&x.push(b);return 0<x.filter(b=>b.table===A&&u.has(b.name)).length}h(c,"isWindowExpressionInUnmergableOperation");function i(){if(!(s instanceof N))return!1;let u=s.aliasOrName,g=s.args.on;if(!g)return!1;let A=Array.from(g.findAll(O)).filter(S=>S.table===u).map(S=>S.name),x=t.expression.getArgKey("from");if(!x)return!1;let E=x.aliasOrName;if(!(t.expression instanceof w))return!1;let b=t.expression,y=new Map;for(let S of b.selects)S instanceof T&&y.set(S.aliasOrName,S);return A.some(S=>{let C=y.get(S);return C?Array.from(C.findAll(O)).some(K=>K.table!==E):!1})}h(i,"outerSelectJoinsOnInnerSelectJoin");function n(){let u=t.expression.parent,g=e.expression.parent;for(;g;){if(g===u)return!0;g=g.parent}return!1}if(h(n,"isRecursive"),!(e.expression instanceof w))return!1;let l=e.expression;if(l.isStar||!(r instanceof w))return!1;let f=r;for(let u of $.UNMERGABLE_ARGS)if(f.getArgKey(u))return!1;if(!f.args.from||0<e.pivots.length)return!1;for(let u of f.args.expressions??[])if(u instanceof T&&(u.find(M)||u.find(w)||u.find(Ue)))return!1;if(o&&1<Object.keys(e.selectedSources).length||s instanceof N&&f.args.joins&&0<f.args.joins.length)return!1;let p=s instanceof N?s.args.side:void 0;if(s instanceof N&&f.args.where&&(p==="full"||p==="left"||p==="right"))return!1;let m=l.args.joins;return!(s instanceof z&&f.args.where&&m&&m.some(u=>j(u,N)&&(u.args.side==="full"||u.args.side==="right"))||i()||c()||n()||f.args.order&&e.isUnion||L(f.args.expressions??[],0)instanceof se)}h(zs,"mergeable");function $s(e,t,a){let s=new Set(Object.keys(t.selectedSources)),o=new Set(Object.keys(e.selectedSources)),r=new Set(Array.from(s).filter(i=>o.has(i)));r.delete(a);let c=new Set([...o,...s]);for(let i of r){let n=be(Array.from(c),i),l=t.selectedSources[i];if(!l)continue;let[f]=l,p=_(n);f instanceof U?f.alias?f.setArgKey("alias",p):f.replace(I(f,p,{copy:!1})):f?.parent instanceof P&&f.parent.setArgKey("alias",new F({this:p}));for(let m of t.sourceColumns(i))m.setArgKey("table",_(n));t.renameSource(i,n),c.add(n)}}h($s,"renameInnerSources");function Ws(e,t,a,s){let r=t.expression.getArgKey("from").args.this;r.setArgKey("joins",a.args.joins),a.replace(r);for(let i of e.joinHints){let n=Array.from(i.findAll(U));for(let l of n)l.aliasOrName===a.aliasOrName&&l.setArgKey("this",_(r.aliasOrName))}e.removeSource(s);let c=t.sources.get(r.aliasOrName);c!==void 0&&e.addSource(r.aliasOrName,c)}h(Ws,"mergeFrom");function Bs(e,t,a){let s=[],o=t.expression.getArgKey("joins");if(o)for(let r of o){s.push(r);let c=t.sources.get(r.aliasOrName);c&&e.addSource(r.aliasOrName,c)}if(0<s.length){let r=e.expression.getArgKey("joins")||[],c;a instanceof z?c=0:c=r.indexOf(a)+1,r.splice(c,0,...s),e.expression.setArgKey("joins",r)}}h(Bs,"mergeJoins");function Qs(e,t,a){let s=new Map;for(let r of e.columns)if(r.table===a){let c=r.name,i=s.get(c);i||(i=[],s.set(c,i)),i.push(r)}if(!(t.expression instanceof w))return;let o=t.expression;for(let r of o.args.expressions??[]){if(!(r instanceof T))continue;let c=r.aliasOrName;if(!c)continue;let i=s.get(c)||[],n=r.unalias(),l=!wn().some(p=>n instanceof p),f=n.isNumber;for(let p of i){let m=p.parent;if(f&&m instanceof Ce){p.replace(_(p.name));continue}let d=n;m&&(m instanceof Cs||m instanceof ee)&&l&&(d=ke(d,{copy:!1})),m instanceof w&&p.name!==d.name&&(d=I(d,p.name,{copy:!1})),p.replace(d.copy())}}}h(Qs,"mergeExpressions");function Gs(e,t,a){let s=t.expression.getArgKey("where");if(!s)return;let o=s.args.this;if(!(o instanceof T))return;let r=e.expression;if(a instanceof N){let c=r.args.from,i=new Set;c&&i.add(c.aliasOrName);let n=r.args.joins;if(n)for(let p of n){let m=p.aliasOrName;if(i.add(m),m===a.aliasOrName)break}let l=oe(o);if(Array.from(l).every(p=>i.has(p))){a.on(o,{copy:!1});return}}r.where(o,{copy:!1})}h(Gs,"mergeWhere");function Hs(e,t){let a=e.expression;a.args.group||a.args.distinct||a.args.having||a.args.order||Object.keys(e.selectedSources).length!==1||a.args.expressions?.some(o=>o instanceof T?o.find(M)!==void 0:!1)||a.setArgKey("order",t.expression.getArgKey("order"))}h(Hs,"mergeOrder");function Ys(e,t){let a=t.expression.getArgKey("hint");if(!a)return;let s=e.expression,o=s.args.hint;if(o){let r=a.args.expressions;for(let c of r??[])c instanceof T&&o.append("expressions",c)}else s.setArgKey("hint",a)}h(Ys,"mergeHints");function vn(e){let t=e.expression.parent;if(!t)return;let a=t.parent;if(!a)return;a.args.expressions?.length===1?a.pop():t.pop()}h(vn,"popCte");function Xs(e,t={}){let{dialect:a}=t,s=V(e),o=$.getOrRaise(a),r=o.constructor.UNNEST_REQUIRES_CROSS_JOIN;if(s){let c=s.refCount();for(let i of Array.from(s.traverse()).reverse()){let n=i.expression,l=n.getArgKey("where");if(l){let p=i.selectedSources,m=n.getArgKey("joins"),d=new Map;m&&m.forEach((g,A)=>{let x=g.aliasOrName;x&&d.set(x,A)});let u=!0;for(let[g,A]of Object.entries(p)){let[x]=A;if(!x)continue;let E=x.findAncestor(N,z);if(E instanceof N){if(E.args.side==="right"){p={[g]:A};break}if(x instanceof te&&r){u=!1;break}}}if(u){let g=l.args.this;g instanceof T&&Vs(g,p,c,o,d)}}let f=n.getArgKey("joins");if(f)for(let p of f){let m=p.aliasOrName;if(m&&m in i.selectedSources){let d=p.args.on;Vs(d,{[m]:i.selectedSources[m]},c,o)}}}}return e}h(Xs,"pushdownPredicates");function Vs(e,t,a,s,o){if(!e)return;let r=Re(e,{dialect:s});e=e.replace(r);let c=de(e)||!de(e,{dnf:!0}),i=(c?e instanceof Pe:e instanceof Le)?Array.from(e.flatten()):[e];c?In(i,t,a,o):qn(i,t,a,o)}h(Vs,"pushdown");function In(e,t,a,s){for(let o of e){let r=Zs(o,t,a);for(let[c,i]of Object.entries(r))if(i instanceof N){let n=oe(o,{exclude:c});if(s){let l=s.get(c)??-1;if(Array.from(n).every(p=>(s.get(p)??-1)<l)){o.replace(B()),i.on(o,{copy:!1});break}}}else if(i instanceof w){o.replace(B());let n=en(i,o);re(n,M)?i.having(n,{copy:!1}):i.where(n,{copy:!1})}}}h(In,"pushdownCnf");function qn(e,t,a,s){let o=new Set;for(let i of e){let n=oe(i);for(let l of e){let f=oe(l);n=new Set(Array.from(n).filter(p=>f.has(p)))}for(let l of n)o.add(l)}let r=new Map,c={};for(let i of Array.from(o).sort()){for(let n of e){if(c=Zs(n,t,a),!(i in c))continue;let l=r.get(i);r.set(i,l?Is([l,n]):n)}for(let[n,l]of Object.entries(c)){let f=r.get(n);if(f){if(l instanceof N){if(s){let p=s.get(n)??-1,m=oe(f,{exclude:n});if(!Array.from(m).every(d=>(s.get(d)??-1)<p))continue}l.on(f,{copy:!1})}else if(l instanceof w){let p=en(l,f);re(p,M)?l.having(p,{copy:!1}):l.where(p,{copy:!1})}}}}}h(qn,"pushdownDnf");function Zs(e,t,a){let s={},o=oe(e),r=e.findAncestor(N,X)instanceof X;for(let c of Array.from(o).sort()){let i=t[c];if(!i)continue;let n=i[0],[,l]=i;if(n&&r&&(n=n.findAncestor(N,z)),n instanceof z&&!(l instanceof U)){let f=ps((l instanceof q?l.parent:l.parent?.args)?.expression,T)?.getArgKey("with");if(f instanceof Y&&f?.recursive)return{};n=l instanceof q?l.expression:l.args.expression}if(n instanceof N){let f=n.side;if(f&&f!=="right")return{};s[c]=n}else if(n instanceof w&&o.size===1){let f=n.selects.some(d=>d instanceof T?d.find(le)!==void 0:!1),p=!!n.args.group,m=a.get(l)||0;!p&&m<2&&!f&&(s[c]=n)}}return s}h(Zs,"nodesForPredicate");function en(e,t){let a=new Map;for(let o of e.selects)if(o instanceof T)if(o instanceof Z){let r=o.args.this;r instanceof T&&a.set(o.alias,r)}else a.set(o.name,o);function s(o){if(o instanceof O){let r=a.get(o.name);if(r)return r.copy()}return o}return h(s,"replaceAlias"),t.transform(s)}h(en,"replaceAliases");var $e=class $e{constructor(t,a,s={}){let{inferSchema:o=!0}=s;this.scope=t,this.schema=a,this.dialect=a.dialect||new $,this.inferSchema=o,this.getSourceColumnsCache=new fs}getTable(t){let a=typeof t=="string"?t:t.name,s=this.getTableNameFromSources(a);if(!s&&typeof t!="string"){let i=this.getColumnJoinContext(t);if(i)try{let n=this.getAvailableSourceColumns(i);s=this.getTableNameFromSources(a,n)}catch(n){if(!(n instanceof J))throw n}}if(!s&&this.inferSchema){let i=this.getAllSourceColumns(),n=[];for(let[l,f]of i)(!f||f.length===0||f.includes("*"))&&n.push(l);n.length===1&&(s=n[0])}if(!s)return;let o=this.scope.selectedSources[s];if(!o)return _(s);let[r]=o,c=r;if(c instanceof Ae)for(;c&&c.alias!==s;)c=c.parent;if(c){let i=c.getArgKey("alias");if(i instanceof T&&(i.args.this instanceof Q||typeof i.args.this=="string"))return _(i.args.this)}return _(s)}get allColumns(){if(!this.allColumnsCache){this.allColumnsCache=new Set;for(let t of this.getAllSourceColumns().values())for(let a of t)this.allColumnsCache.add(a)}return this.allColumnsCache}getSourceColumnsFromSetOp(t){if(t instanceof w)return t.namedSelects;if(t instanceof P){let c=t.args.this;if(c instanceof ne)return this.getSourceColumnsFromSetOp(c)}if(!(t instanceof ne))throw new Error(`Unknown set operation: ${t}`);let a=t,s=a.args.on;if(s)return s.map(c=>c.name);let o=a.args.side,r=a.args.kind;if(o||r){let c=a.args.this,i=a.args.expression;if(!c||!i)return[];let n=this.getSourceColumnsFromSetOp(c),l=this.getSourceColumnsFromSetOp(i);if(o==="left")return n;if(o==="full"){let f=[...n,...l];return Array.from(new Set(f))}else if(r==="inner"){let f=new Set(n),p=new Set(l);return Array.from(f).filter(m=>p.has(m))}}return t.namedSelects}getSourceColumns(t,a={}){let{onlyVisible:s=!1}=a;if(this.getSourceColumnsCache.has(t,s))return this.getSourceColumnsCache.get(t,s)??[];let o=this.scope.sources.get(t);if(!o)throw new J(`Unknown table: ${t}`);let r=[];if(o instanceof U)r=this.schema.columnNames?.(o,{onlyVisible:s})||[];else if(o instanceof q){let n=o.expression;if(n instanceof Me||n instanceof te){if(r=n.namedSelects,this.dialect._constructor.UNNEST_COLUMN_ONLY&&n instanceof te){let l=n;if(!l.type||Ks(l.type,"unknown")){let f=l.args.expressions,p=L(f??[],0);if(p instanceof O&&this.scope.parent){let m=this.getUnnestColumnType(p);if(m?.isType("array")){let d=m.args.expressions;d&&0<d.length?l.type=d[0].copy():l.type=m.copy()}}}if(l.isType("struct"))for(let f of(l.type instanceof T?l.type.args.expressions:void 0)||[])j(f,T)&&r.push(f.name)}}else if(n instanceof ne)r=this.getSourceColumnsFromSetOp(n);else{let l=L(n.args.expressions??[],0);if(l instanceof se){let f=l.args.schema;r=f?f.args.expressions?.map(p=>p instanceof T?p.name:String(p))??[]:["key","value"]}else r=n.namedSelects}}let[c]=this.scope.selectedSources[t]||[void 0,void 0],i;if(c instanceof q?i=c.expression.aliasColumnNames:c instanceof T?i=c.aliasColumnNames:i=[],i.length){let n=[];for(let l=0;l<Math.max(r.length,i.length);l++){let f=L(i,l),p=L(r,l);n.push(f||p||"")}r=n}return this.getSourceColumnsCache.set(t,s,r),r}getAllSourceColumns(){if(!this.sourceColumns){this.sourceColumns=new Map;let t={...this.scope.selectedSources,...Object.fromEntries(this.scope.lateralSources.entries())};for(let a of Object.keys(t))this.sourceColumns.set(a,this.getSourceColumns(a))}return this.sourceColumns}getTableNameFromSources(t,a){let s;return a?s=this.getUnambiguousColumns(a):(this.unambiguousColumns||(this.unambiguousColumns=this.getUnambiguousColumns(this.getAllSourceColumns())),s=this.unambiguousColumns),s.get(t)}getColumnJoinContext(t){if(!this.scope.expression.args.joins||this.scope.expression.getArgKey("laterals")||this.scope.expression.getArgKey("pivots"))return;let s=t.findAncestor(N,w);if(s instanceof N&&Object.keys(this.scope.selectedSources).includes(s.aliasOrName))return s}getAvailableSourceColumns(t){let{from:a,joins:s}=this.scope.expression.args;if(!a||!s)return new Map;let o=new Map,r=a instanceof T?a.aliasOrName:a;r&&o.set(r,this.getSourceColumns(r));for(let c of s.slice(0,s.indexOf(t)+1)){let i=c.aliasOrName;i&&o.set(i,this.getSourceColumns(i))}return o}getUnambiguousColumns(t){if(t.size===0)return new Map;let a=Array.from(t.entries()),[s,o]=a[0];if(a.length===1)return new gs(o,s);let r=new Map;if(this.dialect._constructor.UNNEST_COLUMN_ONLY){for(let[n,l]of this.scope.sources)if(l instanceof q&&l.expression instanceof te){let f=l.expression.args.alias;j(f,F)&&f.columns.length&&r.set(f.columns[0].name,n)}}let c=new Map;for(let n of o)c.set(n,s);let i=new Set(o);for(let[n,l]of a.slice(1)){let f=new Set(l),p=new Set([...i].filter(m=>f.has(m)));for(let m of l)i.add(m);for(let m of p){let d=r.get(m);if(d!==void 0){c.set(m,d);continue}c.delete(m)}for(let m of f)p.has(m)||c.set(m,n)}return c}getUnnestColumnType(t){let a=this.scope.parent;if(!a)return;let s;if(t.table)s=t.table;else{let c=new $e(a,this.schema,{inferSchema:this.inferSchema}).getTable(t);if(!c)return;s=c.name}let o=a.sources.get(s);return o?this.getColumnTypeFromScope(o,t):void 0}getColumnTypeFromScope(t,a){if(t instanceof U){let s=this.schema.getColumnType?.(t,a);if(s&&(typeof s.args.this=="string"?s.args.this:"unknown")!=="unknown")return s}else if(t instanceof q)for(let[,s]of t.sources){let o=this.getColumnTypeFromScope(s,a);if(o&&(typeof o.args.this=="string"?o.args.this:"unknown")!=="unknown")return o}}};h($e,"Resolver");var Se=$e;var ve=Symbol("SELECT_ALL");function _n(e){let{isAgg:t}=e;return I(t?new Oe({this:ce.number(1)}):"1","_")}h(_n,"defaultSelection");function sn(e,t={}){let{schema:a,removeUnusedSelections:s=!0,dialect:o}=t,r=H(a,{dialect:o}),c=new Map,i=new Map,n=Array.from(D(e));for(let l=n.length-1;0<=l;l--){let f=n[l],p=i.get(f)||new Set([ve]),m=c.get(f)||0,d=f.expression;if(d.getArgKey("distinct")&&(p=new Set([ve])),d instanceof ne){let u=d.args.kind,g=d.args.side;if(!u&&!g){let[A,x]=f.unionScopes;if(A.expression.selects.length!==x.expression.selects.length)throw new Error(`Invalid set operation due to column mismatch: ${f.expression.sql({dialect:o})}.`);if(i.set(A,p),x.expression.selects.some(E=>E instanceof T&&E.isStar))i.set(x,p);else if(!A.expression.selects.some(E=>E instanceof T&&E.isStar))if(d.args.byName)i.set(x,i.get(A)||new Set);else{let E=new Set;i.set(x,E);for(let b=0;b<A.expression.selects.length;b++){let y=A.expression.selects[b];if(y instanceof T&&(p.has(ve)||p.has(y.aliasOrName))){let S=x.expression.selects[b];S instanceof T&&E.add(S.aliasOrName)}}}}}if(d instanceof w){if(s&&Kn(f,p,r,m),d.isStar)continue;let u=new Map;for(let g of f.columns){let A=g.table||"",x=g.name,E=u.get(A);E||(E=new Set,u.set(A,E)),E.add(x)}for(let[g,A]of Object.entries(f.selectedSources)){let[x,E]=A;if(E instanceof q){let y=L(E.expression.selects,0),S;0<f.pivots.length||y instanceof se?S=new Set([ve]):S=u.get(g)||new Set;let C=i.get(E);C||(C=new Set,i.set(E,C));for(let v of S)C.add(v)}let b=x.aliasColumnNames;b&&0<b.length&&c.set(E,b.length)}}}return e}h(sn,"pushdownProjections");function Kn(e,t,a,s){let r=e.expression.args.order,c=new Set;if(r)for(let u of r.findAll(O))u.table||c.add(u.name);let i=[],n=!1,l=!1,f=!1,p=s,m=t.has(ve),d=e.expression;for(let u of d.selects){if(typeof u=="string"||typeof u=="number"||typeof u=="boolean")continue;let g=u,A=g.aliasOrName;m||t.has(A)||c.has(A)||0<p?(i.push(g),p-=1):(g.isStar&&(l=!0),n=!0),!f&&g.find(M)&&(f=!0)}if(l){let u=new Se(e,a,{}),g=new Set(i.map(x=>x.aliasOrName)),A=Array.from(t).filter(x=>typeof x=="string").sort();for(let x of A)if(!g.has(x)){let E=u.getTable(x);i.push(I(R({col:x,table:E?.aliasOrName}),x,{copy:!1}))}}i.length===0&&i.push(_n({isAgg:f})),d.select(i,{append:!1,copy:!1}),n&&e.clearCache()}h(Kn,"removeUnusedSelections_");function nn(e,t={}){let{schema:a,dialect:s}=t,o=H(a,{dialect:s});for(let r of D(e))if(Object.keys(r.selectedSources).length!==1)for(let[,c]of Object.entries(r.selectedSources)){let[i,n]=c;if(!(n instanceof U))continue;let l=i;if(!l.parent)continue;let f=o.columnNames?.(n);if(!f||f.length===0||l.parent instanceof P||l.parent?.parent instanceof U)continue;if(!l.alias)throw new J("Tables require an alias. Run qualify_tables optimization.");let m=l.aliasOrName,d=ue("*").from(I(l,m,{table:!0}),{copy:!1}).subquery(m,{copy:!1});l.replace(d)}return e}h(nn,"isolateTableSelects");function ln(e,t={}){let{schema:a,expandAliasRefs:s=!0,expandStars:o=!0,inferSchema:r,allowPartialQualification:c=!1,dialect:i}=t,n=H(a,{dialect:i}),l=new He({schema:n}),f=r??!!n.empty,p=n.dialect||new $,m=p._constructor,d=m.PSEUDOCOLUMNS;for(let u of D(e)){m.PREFER_CTE_ALIAS_COLUMN&&$n(u);let A=u.expression instanceof w;jn(u,d);let x=new Se(u,n,{inferSchema:f});tn(u.ctes),tn(u.derivedTables);let E=Mn(u,x);(n.empty||m.FORCE_EARLY_ALIAS_REF_EXPANSION)&&s&&on(u,x,p,{expandOnlyGroupby:m.EXPAND_ONLY_GROUP_ALIAS_REF}),Un(u,x),Dn(u,x,{allowPartialQualification:c}),!n.empty&&s&&on(u,x,p,{expandOnlyGroupby:!1}),A&&(o&&Fn(u,x,E,d,l),kn(u)),Jn(u,p),zn(u,x),m.ANNOTATE_ALL_SCOPES&&l.annotateScope(u)}return e}h(ln,"qualifyColumns");function fn(e,t){let a=[];for(let s of D(e)){if(!(s.expression instanceof w))continue;let o=s.unqualifiedColumns;if(0<s.externalColumns.length&&!s.isCorrelatedSubquery&&s.pivots.length===0){let r=s.externalColumns[0],c=r.table?` for table: '${r.table}'`:"",i=r.args.this?.meta?.line,n=r.args.this?.meta?.col,l=r.args.this?.meta?.start,f=r.args.this?.meta?.end,p=`Column '${r.name}' could not be resolved${c}.`;if(i&&n&&(p+=` Line: ${i}, Col: ${n}`),t&&l!==void 0&&f!==void 0){let{formattedSql:m}=Be({sql:t,positions:[[l,f]]});p+=`
2
- ${m}`}throw new J(p)}if(0<o.length&&0<s.pivots.length&&s.pivots[0].unpivot){let r=new Set(pn(s.pivots[0]));o=o.filter(c=>!r.has(c))}a.push(...o)}if(0<a.length){let s=a[0],o=s.args.this,r=j(o,T)?o.meta.line:void 0,c=j(o,T)?o.meta.col:void 0,i=j(o,T)?o.meta.start:void 0,n=j(o,T)?o.meta.end:void 0,l=`Ambiguous column '${s.name}'`;if(r&&c&&(l+=` (Line: ${r}, Col: ${c})`),t&&i!==void 0&&n!==void 0){let{formattedSql:f}=Be({sql:t,positions:[[i,n]]});l+=`
3
- ${f}`}throw new J(l)}return e}h(fn,"validateQualifyColumns");function jn(e,t){if(t.size===0)return;let a=!1,s=e.expression;for(let o of e.columns){let r=o.name.toUpperCase();t.has(r)&&(r!=="LEVEL"||s instanceof w&&s.args.connect)&&(o.replace(new As({...o.args})),a=!0)}a&&e.clearCache()}h(jn,"separatePseudocolumns");function pn(e){let a=(e.args.fields||[]).filter(o=>o instanceof pe&&o.args.this instanceof O).map(o=>o.args.this),s=[];for(let o of e.args.expressions)for(let r of o.findAll(O))s.push(r);return[...a,...s]}h(pn,"unpivotColumns");function tn(e){for(let t of e){if(t.parent instanceof Y&&t.parent.args.recursive)continue;let a=t.getArgKey("alias");a instanceof F&&a.setArgKey("columns",void 0)}}h(tn,"popTableColumnAliases");function Mn(e,t){let a=e.expression.args.joins||[];if(a.length===0)return new Map;let s=new Set(a.map(n=>n.aliasOrName)),o=Object.keys(e.selectedSources).filter(n=>!s.has(n));if(0<s.size&&o.length===0)throw new J(`Joins ${[...s].join(",")} missing source table ${e.expression}`);let r={},c=h(n=>{for(let l of t.getSourceColumns(n))l in r||(r[l]=n)},"updateSourceColumns");for(let n of o)c(n);let i=new Map;for(let n=0;n<a.length;n++){let l=a[n];me(l,N);let f=o[o.length-1];f&&c(f);let p=l.aliasOrName;o.push(p);let m=l.args.using;if(!m)continue;let d=t.getSourceColumns(p),u=[],g=m.length,A=l.isSemiOrAntiJoin;for(let x of m){let E=x.name,b=r[E];if((!b||!d.includes(E))&&0<Object.keys(r).length&&!("*"in r)&&0<d.length)throw new J(`Cannot automatically join: ${E}`);b=b||f;let y;if(n===0||g===1)y=R({col:E,table:b});else{let S=o.slice(0,-1).filter(C=>t.getSourceColumns(C).includes(E)).map(C=>R({col:E,table:C}));1<S.length?y=new xe({this:S[0],expressions:S.slice(1)}):y=R({col:E,table:b})}if(u.push(y.eq(R({col:E,table:p}))),!A){let S=i.get(E);S||(S=[],i.set(E,S)),S.includes(b)||S.push(b),S.includes(p)||S.push(p)}}l.setArgKey("using",void 0),l.setArgKey("on",Fe(u,{copy:!1}))}if(0<i.size)for(let n of e.columns){let l=n.table?void 0:i.get(n.name);if(l!==void 0){let f=l.map(m=>R({col:n.name,table:m})),p=new xe({this:f[0],expressions:f.slice(1)});n.parent instanceof w?p=I(p,n.name,{copy:!1}):n.parent instanceof Os&&(p=new ws({this:_(n.name),expression:p})),e.replace(n,p)}}return i}h(Mn,"expandUsing");function on(e,t,a,s){let{expandOnlyGroupby:o}=s,r=e.expression,c=a._constructor;if(!(r instanceof w)||c.DISABLES_ALIAS_REF_EXPANSION)return;let i=new Map,n=new Set(r.selects.map(d=>d.aliasOrName)),l=!1,f=h((d,u={})=>{let{resolveTable:g=!1,literalIndex:A=!1}=u,x=d instanceof Ce,E=d instanceof ae;if(!(!d||o&&!x))for(let b of Qe(d,{prune:h(y=>y.isStar,"prune")})){if(!(b instanceof O)||o&&x&&b.parent!==d)continue;let y=!1,S=g&&!b.table?t.getTable(b.name):void 0,C=i.get(b.name),v=C?C[0]:void 0,K=C?C[1]:1;if(v)y=!!(v.find(M)&&b.findAncestor(M)&&!(b.findAncestor(le,w)instanceof le)),E&&c.PROJECTION_ALIASES_SHADOW_SOURCE_NAMES&&(y=y||Array.from(v.findAll(O)).some(k=>n.has(k.parts[0]?.name||"")));else if(c.PROJECTION_ALIASES_SHADOW_SOURCE_NAMES&&(x||E)){let k=S?S.name:b.table;if(k&&n.has(k)){b.replace(_(b.name)),l=!0;continue}}if(S&&(!v||y))b.setArgKey("table",S);else if(!b.table&&v&&!y)if((v instanceof ce||v.isNumber)&&(A||g))A&&(b.replace(ce.number(K)),l=!0);else{l=!0;let k=b.replace(ke(v)),ye=ds(k,a);ye!==k&&k.replace(ye)}}},"replaceColumns");for(let d=0;d<r.selects.length;d++){let u=r.selects[d];f(u),u instanceof Z&&i.set(u.alias,[u.args.this,d+1])}let p=e,m=!1;for(;p&&!p.isCte;)p.isUnion&&p.parent&&(m=p.parent.expression.args.expression===p.expression),p=p.parent;if(p&&m){let d=p.expression.parent;if(d&&d.findAncestor(Y)?.args.recursive){let g=d.args.alias,A=g instanceof F?g.columns:[],x=0<A.length?A:d.args.this?.selects||[];for(let E of x)E instanceof T&&i.delete(E.outputName)}}if(f(r.args.where),f(r.args.group,{literalIndex:!0}),f(r.args.having,{resolveTable:!0}),f(r.args.qualify,{resolveTable:!0}),c.SUPPORTS_ALIAS_REFS_IN_JOIN_CONDITIONS)for(let d of r.args.joins||[])f(d);l&&e.clearCache()}h(on,"expandAliasRefs_");function Un(e,t){let a=!1,s=[...e.columns,...e.stars];for(let o of s){if(o instanceof fe)continue;let r=o.table,c=o.meta.dotParts||[];if(delete o.meta.dotParts,r&&!e.sources.has(r)&&(!e.parent||!e.parent.sources.has(r)||!e.isCorrelatedSubquery)){let i=o.parts;if(i.length<2)continue;let n=i[0],l=i.slice(1);if(!n)continue;let f,p,m,d;if(e.sources.has(n.name)){if(l.length===0)continue;m=n,f=l[0],p=l.slice(1),d=!0}else m=t.getTable(n.name),f=n,p=l,d=!1;if(m){a=!0;let u=R({col:f,table:m});0<c.length&&(u.meta.dotParts=c.slice(d?2:1)),0<p.length?o.replace(fe.build([u,...p])):o.replace(u)}}}a&&e.clearCache()}h(Un,"convertColumnsToDots");function Dn(e,t,a){let{allowPartialQualification:s}=a,o=t.dialect._constructor;for(let r of e.columns){let c=r.table,i=r.name;if(c&&e.sources.has(c)){let n=t.getSourceColumns(c);if(!s&&0<n.length&&!n.includes(i)&&!n.includes("*"))throw new J(`Unknown column: ${i}`)}if(!c){if(0<e.pivots.length&&!r.findAncestor(we)){r.setArgKey("table",_(e.pivots[0].alias));continue}let n=t.getTable(r);if(n){let l=e.sources.get(n.name);if(l instanceof q&&l.columnIndex.has(r))continue}if(n)r.setArgKey("table",n);else if(o.TABLES_REFERENCEABLE_AS_COLUMNS&&r.parts.length===1&&i in e.selectedSources){let l=r.args.this;e.replace(r,new bs({this:j(l,T)?l:void 0}))}}}for(let r of e.pivots)if(r instanceof we){for(let c of r.findAll(O))if(!c.table&&t.allColumns.has(c.name)){let i=t.getTable(c.name);i&&c.setArgKey("table",i)}}}h(Dn,"qualifyColumnsInScope");function rn(e,t,a){let s=e.getArgKey("except")??e.getArgKey("except");if(!s)return;let o=Array.isArray(s)?s:[s],r=new Set(o.filter(c=>c instanceof T).map(c=>c.name));for(let c of t)a.set(c,r)}h(rn,"addExceptColumns");function an(e,t,a){let s=e.getArgKey("rename");if(!s||s.length===0)return;let o={};for(let r of s)if(r instanceof T){let c=r.args.this;c instanceof T&&(o[c.name]=r.alias)}for(let r of t)a.set(r,o)}h(an,"addRenameColumns");function cn(e,t,a){let s=e.getArgKey("replace");if(!s||s.length===0)return;let o={};for(let r of s)r instanceof Z&&(o[r.alias]=r);for(let r of t)a.set(r,o)}h(cn,"addReplaceColumns");function Pn(e){let t=e.find(O);if(!(t instanceof O)||!t.isType("struct"))return[];let a=t.copy(),s=a.type,o=j(s,je)?s:void 0,r=e.parts.slice(1,-1);e:for(let n of r.slice(1)){let l=o?.args.expressions||[];for(let f of l){if(!(f.args.this instanceof Q))return[];if(!j(f,Ke))return[];let p=f.args.kind,m=j(p,je)?p:void 0;if(f.name===n.name&&m?.isType("struct")){o=m;break e}}return[]}let c=new Set,i=[];for(let n of o?.args.expressions||[]){let l=n.name,f=n.args.this;if(c.has(l)||!(f instanceof Q))return[];c.add(l);let p=f.copy(),m=[...r.map(A=>A.copy()),p],[d,...u]=m,g=R({col:d,table:a.args.table},{fields:u});i.push(I(g,p,{copy:!1}))}return i}h(Pn,"expandStructStarsNoParens");function Ln(e){if(!(e.args.this instanceof Ne))return[];let t=e.find(O);if(!(t instanceof O)||!t.isType("struct"))return[];let a=t.parent,s=t.type,o=j(s,je)?s:void 0;for(;a!==void 0;){if(a instanceof Ne){a=a.parent;continue}if(!(a instanceof fe))return[];let n=a.right;if(n instanceof Ze)break;if(!(n instanceof Q))return[];let l=!1,f=qe(o.args.expressions||[],Ke);for(let p of f)if(p.name===n.name){l=!0,o=p.args.kind;break}if(!l)return[];a=a.parent}let r=[],c=e.args.this,i=qe(o.args.expressions||[],Ke);for(let n of i){let l=n.args.this instanceof Q?n.args.this.copy():new Q({this:n.args.this?.toString()}),f=fe.build([c.copy(),l]),p=I(f,l,{copy:!1});r.push(p)}return r}h(Ln,"expandStructStarsWithParens");function Fn(e,t,a,s,o){let r=[],c=new Map,i=new Map,n=new Map,l=new Set,f=t.dialect._constructor,p,m=new Set,d=L(e.pivots,0);if(d instanceof we&&!d.aliasColumnNames.length)if(d.unpivot){p=pn(d).map(u=>u.outputName);for(let u of d.args.fields||[])if(u instanceof pe)for(let g of u.args.expressions)for(let A of g.findAll(O))m.add(A.outputName)}else{for(let g of d.findAll(O))m.add(g.outputName);p=(d.getArgKey("columns")||[]).map(g=>g.outputName),p.length||(p=d.args.expressions.map(g=>g.aliasOrName))}f.SUPPORTS_STRUCT_STAR_EXPANSION&&e.stars.some(u=>u instanceof fe)&&o.annotateScope(e);for(let u of e.expression.selects){let g=[];if(u instanceof Ze)g.push(...Object.keys(e.selectedSources)),rn(u,g,c),cn(u,g,i),an(u,g,n);else if(u.isStar)if(u instanceof fe){if(f.SUPPORTS_STRUCT_STAR_EXPANSION&&!f.REQUIRES_PARENTHESIZED_STRUCT_ACCESS){let A=Pn(u);if(0<A.length){r.push(...A);continue}}else if(f.REQUIRES_PARENTHESIZED_STRUCT_ACCESS){let A=Ln(u);if(0<A.length){r.push(...A);continue}}}else{let A=u instanceof O?u.table:"";A&&g.push(A);let x=u.args.this;x instanceof T&&(rn(x,g,c),cn(x,g,i),an(x,g,n))}if(g.length===0){r.push(u);continue}for(let A of g){if(!e.sources.has(A))throw new J(`Unknown table: ${A}`);let x=t.getSourceColumns(A,{onlyVisible:!0});if(x.length||(x=e.outerColumns),0<s.size&&f.EXCLUDES_PSEUDOCOLUMNS_FROM_STAR&&(x=x.filter(S=>!s.has(S.toUpperCase()))),!x.length||x.includes("*"))return;let E=c.get(A)||new Set,b=n.get(A)||{},y=i.get(A)||{};if(d instanceof we){let S;if(p&&0<m.size?(S=x.filter(C=>!m.has(C)),S.push(...p)):S=d.aliasColumnNames,0<S.length){for(let C of S)E.has(C)||r.push(I(R({col:C,table:d.alias}),C,{copy:!1}));continue}}for(let S of x){if(E.has(S)||l.has(S))continue;let C=a.get(S);if(C?.includes(A)){l.add(S);let v=C.map(K=>R({col:S,table:K}));r.push(I(new xe({this:v[0],expressions:v.slice(1)}),S,{copy:!1}))}else{let v=b[S]??S,K=y[S]||R({col:S,table:A});r.push(v!==S?I(K,v,{copy:!1}):K)}}}}0<r.length&&e.expression instanceof w&&e.expression.setArgKey("expressions",r)}h(Fn,"expandStars_");function kn(e){let t;if(e instanceof q)t=e;else{let c=V(e);if(!(c instanceof q))return;t=c}if(!(t.expression instanceof w))return;let a=t.expression.selects,s=t.outerColumns,o=[],r=Math.max(a.length,s.length);for(let c=0;c<r;c++){let i=a[c],n=s[c];if(!i||i instanceof se)break;i instanceof P?i.outputName||i.setArgKey("alias",new F({this:_(`_col_${c}`)})):!(i instanceof Z)&&!(i instanceof Ss)&&!i.isStar&&(i=I(i,i.outputName||`_col_${c}`,{copy:!1})),n&&i.setArgKey("alias",_(n)),o.push(i)}0<o.length&&t.expression instanceof w&&t.expression.setArgKey("expressions",o)}h(kn,"qualifyOutputs");function un(e,t){let a=Number(t.args.this)-1,s=e.expression.selects[a];if(!(s instanceof Z))throw new J(`Unknown output column: ${t.name}`);return s}h(un,"selectByPos");function mn(e,t,a,s={}){let{alias:o=!1}=s,r=a._constructor,c=[],i;for(let n of t)if(n.isInteger){let l=un(e,n);if(o){let f=l.alias;c.push(f?R({col:f}):n)}else{let f=l.args.this,p=!1;r.PROJECTION_ALIASES_SHADOW_SOURCE_NAMES&&(i===void 0&&(i=new Set(e.expression.selects.map(m=>m.aliasOrName).filter(m=>m in e.selectedSources))),p=Array.from(f.findAll(O)).some(m=>i?.has(m.parts[0]?.name||""))),_s.some(m=>f instanceof m)||f.isNumber||f.find(Ue)||f.find(te)||p?c.push(n):c.push(f.copy())}}else c.push(n);return c}h(mn,"expandPositionalReferences");function Jn(e,t){let a=e.expression.args.group;if(!a)return;let s=qe(a.args.expressions||[],T);a.setArgKey("expressions",mn(e,s,t)),e.expression.setArgKey("group",a)}h(Jn,"expandGroupBy");function zn(e,t){for(let a of["order","distinct"]){let s=e.expression.getArgKey(a);if(s instanceof ys&&(s=s.args.on),!(s instanceof T))continue;let o=s.args.expressions;a==="order"&&(o=o.map(c=>c.args.this));let r=mn(e,o,t.dialect,{alias:!0});for(let c=0;c<o.length;c++){let i=o[c],n=r[c];for(let l of i.findAll(M))for(let f of l.findAll(O))if(!f.table){let p=t.getTable(f.name);p&&f.setArgKey("table",p)}i.replace(n)}if(e.expression.getArgKey("group")){let c=new Map(e.expression.selects.filter(i=>i instanceof Z).map(i=>[i.args.this.sql(),R({col:i.aliasOrName})]));for(let i of o)if(i.isInteger)i.replace(_(un(e,i).alias));else{let n=c.get(i.sql());n&&i.replace(n)}}}}h(zn,"expandOrderByAndDistinctOn");function We(e,t={}){let{dialect:a,identify:s=!0}=t,o=$.getOrRaise(a);return e.transform(o.quoteIdentifier.bind(o),{identify:s,copy:!1})}h(We,"quoteIdentifiers");function $n(e){for(let t of e.ctes){let a=t.aliasColumnNames;if(0<a.length&&t.args.this instanceof w){let s=t.args.this,o=s.args.expressions||[],r=[];for(let c=0;c<a.length;c++){let i=a[c],n=o[c];if(!n)break;let l;n instanceof Z?(n.setArgKey("alias",_(i)),l=n):l=I(n,i),r.push(l)}s.setArgKey("expressions",r)}}}h($n,"pushdownCteAliasColumns");function dn(e,t={}){let{db:a,catalog:s,onQualify:o,dialect:r,canonicalizeTableAliases:c=!1}=t,i=$.getOrRaise(r),n=_e("_"),l;a&&(l=ts(a,{dialect:i}),l.meta.isTable=!0,l=ie(l,{dialect:i}));let f;s&&(f=ts(s,{dialect:i}),f.meta.isTable=!0,f=ie(f,{dialect:i}));let p=h(d=>{d.args.this instanceof Q&&(l&&!d.args.db&&d.setArgKey("db",l.copy()),f&&!d.args.catalog&&d.args.db&&d.setArgKey("catalog",f.copy()))},"qualify");if((l||f)&&!(e instanceof Ae)){let u=e.getArgKey("with")||new Y({expressions:[]}),g=new Set((u.args.expressions||[]).map(A=>A.aliasOrName));for(let A of e.walk({prune:h(x=>x instanceof Ae,"prune")}))A instanceof U&&(g.has(A.name)||p(A))}let m=h((d,u,g={})=>{let{targetAlias:A,scope:x,normalize:E=!1,columns:b}=g,y=d.getArgKey("alias");y||(y=new F({}));let S;if(c)S=n(),u.set(y.name||A||"",S);else if(!y.name)S=A||n(),E&&A&&(S=ie(S,{dialect:i}).name);else return;y.setArgKey("this",_(S)),b&&y.setArgKey("columns",b.map(C=>_(C))),d.setArgKey("alias",y),x&&x.renameSource("",S)},"setAlias");for(let d of D(e)){let u=d.localColumns,g=new Map;for(let x of d.subqueries){let E=x.parent;E&&E instanceof P&&E.unwrap().replace(E)}for(let x of d.derivedTables){let E=x.unnest();if(E instanceof U){let y=E.args.joins;E.setArgKey("joins",void 0);let S=x.args.this;if(S instanceof T){let C=ue("*").from(E.copy(),{copy:!1});S.replace(C),C.setArgKey("joins",y)}}m(x,g,{scope:d});let b=L(x.getArgKey("pivots")||[],0);b&&m(b,g)}let A=new Map;for(let[x,E]of d.sources)if(E instanceof U){let b=!!x,y=E.args.pivots,S=y?L(y,0):void 0,C=x;S&&(C=E.name);let v=E.args.this,K=E.args.alias,k=[];if(v&&v instanceof ge){let cs=v.constructor.name;if(K)K instanceof F&&K.args.columns?.length?k=K.columns:i._constructor.DEFAULT_FUNCTIONS_COLUMN_NAMES.has(cs)&&(k=Array.from(Ye(E.aliasOrName)),E.setArgKey("alias",void 0),C="");else{let ls=i._constructor.DEFAULT_FUNCTIONS_COLUMN_NAMES.get(cs);k=ls?Array.from(Ye(ls)):[]}}m(E,g,{targetAlias:C||E.name||void 0,normalize:!0,columns:k});let ye=E.parts?.map(Ie=>Ie.name).join("."),as=E.args.alias?.args.this,En=K&&K.name;if(as instanceof Q&&(!En||!A.has(ye))&&A.set(ye,as.copy()),S){let Ie=S.getArgKey("unpivot")?E.alias:void 0;if(m(S,g,{targetAlias:Ie,normalize:!0}),d.sources.get(E.aliasOrName)instanceof q)continue}b&&(p(E),o&&o(E))}else if(E instanceof q&&E.isUdtf){let b=E.expression;if(m(b,g),b instanceof Me){let y=b.getArgKey("alias");y instanceof F&&!y.args.columns?.length&&y.setArgKey("columns",i.generateValuesAliases(b).map(S=>ie(S,{dialect:i})))}}for(let x of d.tables)if(!x.alias&&x.parent){let E=x.parent;(E instanceof z||E instanceof N)&&m(x,g,{targetAlias:x.name})}for(let x of u){let E=x.table;if(x.args.db){let b=x.parts.slice(0,-1).map(S=>S.name).join("."),y=A.get(b);y&&(x.setArgKey("table",void 0),x.setArgKey("db",void 0),x.setArgKey("catalog",void 0),x.setArgKey("table",y.copy()))}else if(0<g.size&&E){let b=g.get(E);b&&b!==x.table&&x.setArgKey("table",_(b))}}}return e}h(dn,"qualifyTables");function gn(e,t={}){let{dialect:a,db:s,catalog:o,schema:r,expandAliasRefs:c=!0,expandStars:i=!0,inferSchema:n,isolateTables:l=!1,qualifyColumns:f=!0,allowPartialQualification:p=!1,validateQualifyColumns:m=!0,quoteIdentifiers:d=!0,identify:u=!0,canonicalizeTableAliases:g=!1,onQualify:A,sql:x}=t,E=H(r,{dialect:a}),b=$.getOrRaise(a);return e=ie(e,{dialect:b,storeOriginalColumnIdentifiers:!0}),e=dn(e,{db:s,catalog:o,dialect:b,onQualify:A,canonicalizeTableAliases:g}),l&&(e=nn(e,{schema:E,dialect:b})),f&&(e=ln(e,{schema:E,expandAliasRefs:c,expandStars:i,inferSchema:n,allowPartialQualification:p})),d&&(e=We(e,{dialect:b,identify:u})),m&&fn(e,x),e}h(gn,"qualify");function xn(e){let t=_e("_u_");for(let a of D(e)){let s=a.expression,o=s.parentSelect;if(!o||!j(o,w)||!j(s,w))continue;let r=s;0<a.externalColumns.length?Qn(r,o,a.externalColumns,t):a.scopeType==="subquery"&&Bn(r,o,t)}return e}h(xn,"unnestSubqueries");function Bn(e,t,a){if(1<e.selects.length)return;let s=e.findAncestor(xs);if(!s||s instanceof ge&&(s.parent instanceof U||s.parent instanceof z||s.parent instanceof N)||t!==s.parentSelect||!t.args.from)return;let o=e;e instanceof ne&&(o=ue(e.selects).from(e.subquery(a()),{copy:!1}));let r=a(),c=s.findAncestor(ae,X,N);if(!(s instanceof pe||s instanceof he)){let u=R({col:o.selects[0].aliasOrName,table:r}),g=c?.parentSelect;if(c instanceof ae&&g===t||(!c||g!==t)&&(t.args.group||t.selects.some(E=>re(E,M))))u=new Oe({this:u});else if(!(e.parent instanceof P))return;let A="cross",x;s instanceof Ee&&(u=u.is(Je()).not(),A="left",x=B()),G(e.parent,u),t.join(o,{on:x,joinType:A,joinAlias:r,copy:!1});return}if(o.find([Te,Xe]))return;let i=s;if(s instanceof he){let u=s.findAncestor(W);if(!u||t!==u.parentSelect)return;i=u}let n=is(i),l=o.selects[0],f=l.args.this;if(!(f instanceof T))return;let p=R({col:l.alias,table:r}),m=p.is(Je()).not();c instanceof N?(G(i,B()),t.where(m,{copy:!1})):G(i,m);let d=o.args.group;if(d){let u=new Set((d.args.expressions??[]).map(g=>g instanceof T?g.sql():String(g)));(u.size!==1||!u.has(l.args.this.sql()))&&(o=ue(I(R({col:l.alias,table:"_q"}),l.alias,{copy:!1})).from(o.subquery("_q",{copy:!1}),{copy:!1}).groupBy(R({col:l.alias,table:"_q"}),{copy:!1}))}else re(f,M)||(o=o.groupBy(f,{copy:!1}));n&&t.join(o,{on:n.eq(p),joinType:"left",joinAlias:r,copy:!1})}h(Bn,"unnest");function Qn(e,t,a,s){let o=e.args.where;if(!o||o.find(Le)||e.find([Te,Xe]))return;let r=s(),c=[];for(let y of a){if(y.findAncestor(X)!==o)return;let S=y.findAncestor(Ve);if(!S||S.findAncestor(X)!==o)return;let C;if(S instanceof ee)C=Array.from(S.left?.walk()||[]).some(v=>v===y)?S.right:S.left;else return;if(!C)return;c.push([C,y,S])}if(!c.some(([,,y])=>y instanceof W))return;let i=t.selects.map(y=>y.unalias()).some(y=>y instanceof P&&y===e.parent),n=e.selects[0],l=n.args.this;if(!(l instanceof T))return;let f=new Map,p=new Map,m=new Set,d=[],u=h(y=>m.has(y.sql()),"exprInGroupBy");for(let[y,,S]of c){let C=y.sql();C===l.sql()?(f.set(C,n.alias),p.set(C,y),m.has(C)||(m.add(C),d.push(y))):(f.has(C)||(f.set(C,s()),p.set(C,y)),S instanceof W&&!m.has(C)&&(m.add(C),d.push(y)))}let g=e.findAncestor(Ve);if(g===void 0&&!i)return;let A=i?Oe:vs;!n.find(M)&&!u(l)&&e.select(I(new A({this:l}),n.alias,{quoted:!1}),{append:!1,copy:!1}),g instanceof Ee&&e.setArgKey("expressions",[]);for(let[y,S]of f){let C=p.get(y);m.has(y)?(g instanceof Ee||y!==n.args.this.sql())&&e.select(`${C} AS ${S}`,{copy:!1}):e.select(I(new A({this:C.copy()}),S,{quoted:!1}),{copy:!1})}let x=R({col:n.alias,table:r}),E=is(g),b=g?.parent?._constructor;if(g instanceof Ee)x=R({col:Array.from(f.values())[0],table:r}),g=G(g,`NOT ${x} IS NULL`);else if(g instanceof De){if(!b||!(b.prototype instanceof ee||b===ee))return;let y=new b({this:E,expression:R({col:"_x"})});g.parent&&(g=G(g.parent,`ARRAY_ALL(${x}, _x -> ${y})`))}else if(g instanceof he){if(!b||!(b.prototype instanceof ee||b===ee))return;if(u(n.args.this)){let y=new b({this:E,expression:x});g.parent&&(g=G(g.parent,y))}else{let y=new b({this:E,expression:R({col:"_x"})});g=G(g,`ARRAY_ANY(${x}, _x -> ${y})`)}}else if(g instanceof pe)u(n.args.this)?g=G(g,`${E} = ${x}`):g=G(g,`ARRAY_ANY(${x}, _x -> _x = ${g.args.this})`);else{if(i&&e.parent?.alias&&(x=I(x,e.parent.alias,{copy:!1})),n.find(ss)){let y=h(S=>S instanceof ss?ce.number(0):S instanceof M?Je():S,"removeAggs");x=new xe({this:x,expressions:[n.args.this.transform(y)]})}e.parent?.replace(x)}for(let[y,S,C]of c){C.replace(B());let v=f.get(y.sql());if(!v)continue;let K=R({col:v,table:r});if(i){y.replace(K),C instanceof W||t.where(C,{copy:!1});continue}u(y)?y.replace(K):C instanceof W?g&&(g=G(g,`(${g} AND ARRAY_CONTAINS(${K}, ${S}))`)):(y.replace(_("_x")),g&&(g=G(g,`(${g} AND ARRAY_ANY(${K}, _x -> ${C}))`)))}t.join(e.groupBy(d,{copy:!1}),{on:c.filter(([,,y])=>y instanceof W).map(([,,y])=>y),joinType:"left",joinAlias:r,copy:!1})}h(Qn,"decorrelate");function G(e,t){return e.replace(qs(t))}h(G,"replace");function is(e){if(e instanceof pe)return e.args.this;if(e instanceof he||e instanceof De)return is(e.parent);if(e instanceof ee)return e.left instanceof P||e.left instanceof he||e.left instanceof Ee||e.left instanceof De?e.right:e.left}h(is,"otherOperand");var Gn=[gn,sn,ms,xn,Xs,Ms,rs,Js,os,Us,We,Ge,js,Re];function $o(e,t={}){let{schema:a,db:s,catalog:o,dialect:r,rules:c=Gn,sql:i,isolateTables:n=!0,quoteIdentifiers:l=!1,...f}=t,p=H(a,{dialect:r}),m={db:s,catalog:o,schema:p,dialect:r,sql:i,isolateTables:n,quoteIdentifiers:l,...f},d=Rs(e,{dialect:r,copy:!0});for(let u of c)d=u(d,m);return d}h($o,"optimize");export{gn as a,Ls as b,$o as c};
4
- //# sourceMappingURL=chunk-NJEMK7XN.js.map