@nongt/visql 0.9.0 → 0.10.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.
@@ -1,41 +1,41 @@
1
- import { D as Q, h as U, q as S, t as T, i as C, I as _e, u as ne, C as jn, g as te, o as re, J as Te, k as A, a as oe, m as P, r as x, K as Je, n as E, L as qn, N as _, y as q, O as Rn, M as D, e as Ve, s as Gn, d as M, j as se, P as ve, b as Y, H as Fn, x as Qn, Q as We } from "./schema-dP_ISylQ.js";
2
- import { c as Pn, s as _n, b as He } from "./calendarDate-CYlr9TvX.js";
3
- const Mt = 1e3, Jn = 15, Ot = 40, Ut = 100;
4
- function Vn(e) {
1
+ import { D as Q, h as U, q as S, t as v, i as C, J as Je, u as ne, F as jn, e as te, n as re, K as Te, j as A, y as oe, m as _, o as x, L as Pe, l as E, N as qn, O as J, z as q, r as Rn, M as D, s as Gn, P as Fn, d as M, g as se, Q as Ne, b as Y, I as Qn, x as _n, R as Ve } from "./schema-KDJ1rpG5.js";
2
+ import { c as Jn, s as Pn, b as We } from "./calendarDate-CYlr9TvX.js";
3
+ const Ot = 1e3, Vn = 15, Ut = 40, Lt = 100;
4
+ function Wn(e) {
5
5
  const [n] = e.replace(/^[+-]/, "").split(/e/i), [r, t = ""] = n.split(".");
6
6
  return `${r}${t.replace(/0+$/, "")}`.replace(/^0+/, "").length;
7
7
  }
8
- function Wn(e) {
9
- return Number.isFinite(e) && Vn(String(e)) <= Jn;
8
+ function Hn(e) {
9
+ return Number.isFinite(e) && Wn(String(e)) <= Vn;
10
10
  }
11
- const Lt = [];
11
+ const Dt = [];
12
12
  function O(e) {
13
13
  return S(e).filter((n) => n.query !== void 0);
14
14
  }
15
- function me(e, n = []) {
15
+ function pe(e, n = []) {
16
16
  var r;
17
17
  return e.query ? e.query : e.shared === void 0 || (r = n.find((t) => t.id === e.shared)) == null ? void 0 : r.query;
18
18
  }
19
- function Xe(e) {
19
+ function He(e) {
20
20
  const n = [], r = (t) => {
21
21
  t.type === "subquery" ? n.push(t) : oe(t).forEach(r);
22
22
  };
23
- return _e(e).forEach(r), n;
23
+ return Je(e).forEach(r), n;
24
24
  }
25
- function Ke(e, n) {
25
+ function Xe(e, n) {
26
26
  let r = !1;
27
27
  const t = (s) => {
28
- if (s.type !== "subquery") return P(s, t);
28
+ if (s.type !== "subquery") return _(s, t);
29
29
  const i = n(s);
30
30
  return i !== s && (r = !0), i;
31
31
  }, o = te(e, t, (s) => s);
32
32
  return r ? o : e;
33
33
  }
34
- const Dt = (e, n) => `${e}#${n}`;
35
- function Hn(e) {
34
+ const jt = (e, n) => `${e}#${n}`;
35
+ function Xn(e) {
36
36
  return !("type" in e) && "of" in e;
37
37
  }
38
- const Xn = (e) => ({
38
+ const Kn = (e) => ({
39
39
  id: e.id,
40
40
  alias: e.name,
41
41
  query: e.query
@@ -45,12 +45,12 @@ function Ie(e, n) {
45
45
  const r = O(e).find((s) => s.id === n);
46
46
  if (r) return r;
47
47
  const t = (o = e.shared) == null ? void 0 : o.find((s) => s.id === n);
48
- return t && Xn(t);
48
+ return t && Kn(t);
49
49
  }
50
- function Ye(e, n) {
50
+ function Ke(e, n) {
51
51
  const r = Ie(e, n);
52
52
  if (r) return C(r.query) ? void 0 : r;
53
- const t = Xe(e).find((a) => a.id === n);
53
+ const t = He(e).find((a) => a.id === n);
54
54
  if (t) return t;
55
55
  const o = /^(.*)#(\d+)$/.exec(n), s = o ? Ie(e, o[1]) : void 0, i = Number(o == null ? void 0 : o[2]), u = s && C(s.query) ? s.query.union[i] : void 0;
56
56
  return s && u ? {
@@ -61,10 +61,10 @@ function Ye(e, n) {
61
61
  index: i
62
62
  } : void 0;
63
63
  }
64
- function ze(e) {
64
+ function Ye(e) {
65
65
  return { id: e.id, ...re(e.query) };
66
66
  }
67
- function le(e) {
67
+ function ce(e) {
68
68
  const {
69
69
  from: n,
70
70
  joins: r,
@@ -84,41 +84,41 @@ function le(e) {
84
84
  ...u !== void 0 ? { distinct: u } : {}
85
85
  };
86
86
  }
87
- function jt(e, n) {
87
+ function qt(e, n) {
88
88
  return n.reduce((r, t) => {
89
- const o = r && Ye(r, t);
90
- return o ? ze(o) : void 0;
89
+ const o = r && Ke(r, t);
90
+ return o ? Ye(o) : void 0;
91
91
  }, e);
92
92
  }
93
- function Ze(e, n, r) {
93
+ function ze(e, n, r) {
94
94
  if (n.length === 0) return r(e);
95
- const [t, ...o] = n, s = Ye(e, t);
95
+ const [t, ...o] = n, s = Ke(e, t);
96
96
  if (!s) return e;
97
- const i = ze(s), u = Ze(i, o, r);
97
+ const i = Ye(s), u = ze(i, o, r);
98
98
  if (u === i || !U(u)) return e;
99
99
  if ("type" in s && s.type === "subquery") {
100
- const l = { ...s, query: le(u) };
101
- return Ke(e, (c) => c === s ? l : c);
100
+ const l = { ...s, query: ce(u) };
101
+ return Xe(e, (c) => c === s ? l : c);
102
102
  }
103
103
  const a = (l, c) => {
104
- if (!O(e).some((d) => d.id === l.id))
105
- return Te(e, (d, m) => m.id === l.id ? c : d);
106
- const f = { ...l, query: c };
107
- return l === e.from ? { ...e, from: f } : {
104
+ if (!O(e).some((f) => f.id === l.id))
105
+ return Te(e, (f, m) => m.id === l.id ? c : f);
106
+ const d = { ...l, query: c };
107
+ return l === e.from ? { ...e, from: d } : {
108
108
  ...e,
109
- joins: e.joins.map((d) => d === l ? f : d)
109
+ joins: e.joins.map((f) => f === l ? d : f)
110
110
  };
111
111
  };
112
- if (Hn(s)) {
113
- const { of: l, index: c } = s, { union: f } = l.query;
112
+ if (Xn(s)) {
113
+ const { of: l, index: c } = s, { union: d } = l.query;
114
114
  return a(l, {
115
115
  ...l.query,
116
- union: f.map((d, m) => m === c ? le(u) : d)
116
+ union: d.map((f, m) => m === c ? ce(u) : f)
117
117
  });
118
118
  }
119
- return a(s, le(u));
119
+ return a(s, ce(u));
120
120
  }
121
- const Kn = {
121
+ const Yn = {
122
122
  text: "varchar",
123
123
  integer: "integer",
124
124
  number: "decimal",
@@ -127,19 +127,19 @@ const Kn = {
127
127
  time: "time",
128
128
  boolean: "boolean"
129
129
  };
130
- function pe(e, n, r = []) {
130
+ function ye(e, n, r = []) {
131
131
  const t = re(n);
132
132
  if (t.select.length === 0)
133
133
  return S(t).flatMap((u) => {
134
134
  var l;
135
135
  if (u.table !== void 0) return ((l = A(e, u.table)) == null ? void 0 : l.columns) ?? [];
136
- const a = me(u, r);
137
- return a ? pe(e, a, r) : [];
136
+ const a = pe(u, r);
137
+ return a ? ye(e, a, r) : [];
138
138
  });
139
- const o = new Map(S(t).map((u) => [T(u), u.table])), s = (u) => {
139
+ const o = new Map(S(t).map((u) => [v(u), u.table])), s = (u) => {
140
140
  var a;
141
141
  if (u.type === "field") {
142
- const l = u.table === void 0 ? void 0 : o.get(u.table), c = l === void 0 || (a = A(e, l)) == null ? void 0 : a.columns.find((f) => f.name === u.column);
142
+ const l = u.table === void 0 ? void 0 : o.get(u.table), c = l === void 0 || (a = A(e, l)) == null ? void 0 : a.columns.find((d) => d.name === u.column);
143
143
  return (c == null ? void 0 : c.type) ?? "varchar";
144
144
  }
145
145
  if (u.type === "function") {
@@ -148,14 +148,14 @@ function pe(e, n, r = []) {
148
148
  if (l === "SUM" || l === "AVG") return "decimal";
149
149
  if ((l === "MIN" || l === "MAX") && u.args[0]) return s(u.args[0]);
150
150
  }
151
- return u.type === "literal" && typeof u.value == "number" ? "decimal" : u.type === "literal" && u.as ? Kn[u.as] : "varchar";
151
+ return u.type === "literal" && typeof u.value == "number" ? "decimal" : u.type === "literal" && u.as ? Yn[u.as] : "varchar";
152
152
  }, i = (u) => {
153
- var f, d;
153
+ var d, f;
154
154
  if (u.type !== "field" || u.table === void 0) return !1;
155
- const a = S(t).find((m) => T(m) === u.table);
155
+ const a = S(t).find((m) => v(m) === u.table);
156
156
  if (!a) return !1;
157
- const l = a.table === void 0 ? me(a, r) : void 0, c = a.table !== void 0 ? (f = A(e, a.table)) == null ? void 0 : f.columns : l && pe(e, l, r);
158
- return !!((d = c == null ? void 0 : c.find((m) => m.name === u.column)) != null && d.joinOnly);
157
+ const l = a.table === void 0 ? pe(a, r) : void 0, c = a.table !== void 0 ? (d = A(e, a.table)) == null ? void 0 : d.columns : l && ye(e, l, r);
158
+ return !!((f = c == null ? void 0 : c.find((m) => m.name === u.column)) != null && f.joinOnly);
159
159
  };
160
160
  return t.select.flatMap((u) => {
161
161
  const a = x(u);
@@ -167,23 +167,23 @@ function pe(e, n, r = []) {
167
167
  }] : [];
168
168
  });
169
169
  }
170
- function en(e, n, r = []) {
171
- const t = me(n, r), o = (t ? pe(e, t, r) : []).filter((s, i, u) => u.findIndex((a) => a.name === s.name) === i);
170
+ function Ze(e, n, r = []) {
171
+ const t = pe(n, r), o = (t ? ye(e, t, r) : []).filter((s, i, u) => u.findIndex((a) => a.name === s.name) === i);
172
172
  return { name: n.alias, label: n.alias, columns: o };
173
173
  }
174
- function qt(e, n, r = []) {
175
- return S(n).filter((t) => t.table === void 0).map((t) => en(e, t, r));
174
+ function Rt(e, n, r = []) {
175
+ return S(n).filter((t) => t.table === void 0).map((t) => Ze(e, t, r));
176
176
  }
177
- function Rt(e, n = Q) {
177
+ function Gt(e, n = Q) {
178
178
  if (C(e)) return !1;
179
179
  const r = e;
180
180
  return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => ne(t.expression, n)) && jn(r, n).select.length === 0;
181
181
  }
182
182
  function ke(e, n, r) {
183
- const t = (o) => o.type === "field" ? o.table === n ? { ...o, table: r } : o : o.type === "exists" && Je(o.query).has(n) ? { ...o, link: t(o.link) } : P(o, t);
183
+ const t = (o) => o.type === "field" ? o.table === n ? { ...o, table: r } : o : o.type === "exists" && Pe(o.query).has(n) ? { ...o, link: t(o.link) } : _(o, t);
184
184
  return te(e, t, (o) => o);
185
185
  }
186
- function nn(e, n) {
186
+ function en(e, n) {
187
187
  const r = re(n), t = S(r).flatMap((o) => o.table === void 0 ? [] : [{ table: o.table, alias: o.alias }]);
188
188
  return r.select.length === 0 ? t.flatMap(({ table: o }) => {
189
189
  var s;
@@ -191,12 +191,12 @@ function nn(e, n) {
191
191
  }) : r.select.flatMap((o) => {
192
192
  const { expression: s } = o;
193
193
  if (s.type !== "field" || !s.column) return [];
194
- const i = t.find((a) => T(a) === s.table), u = o.alias || s.column;
194
+ const i = t.find((a) => v(a) === s.table), u = o.alias || s.column;
195
195
  return i ? [{ name: u, table: i.table, column: s.column }] : [];
196
196
  });
197
197
  }
198
- function tn(e, n) {
199
- return O(n).flatMap((r) => nn(e, r.query).flatMap(({ name: t, table: o, column: s }) => e.relations.flatMap((i) => {
198
+ function nn(e, n) {
199
+ return O(n).flatMap((r) => en(e, r.query).flatMap(({ name: t, table: o, column: s }) => e.relations.flatMap((i) => {
200
200
  const u = i.fromTable === o && i.fromColumn === s;
201
201
  if (!u && !(i.toTable === o && i.toColumn === s)) return [];
202
202
  const a = u ? i.toTable : i.fromTable, l = {
@@ -208,16 +208,16 @@ function tn(e, n) {
208
208
  return [{ table: a, on: l }];
209
209
  })));
210
210
  }
211
- function Yn(e, n, r) {
211
+ function zn(e, n, r) {
212
212
  var t;
213
- return (t = tn(e, n).find((o) => o.table === r)) == null ? void 0 : t.on;
213
+ return (t = nn(e, n).find((o) => o.table === r)) == null ? void 0 : t.on;
214
214
  }
215
- function rn(e, n, r) {
215
+ function tn(e, n, r) {
216
216
  var a;
217
- const t = S(n).flatMap((l) => l !== r && l.table !== void 0 ? [{ table: l.table, alias: l.alias }] : []), o = (l, c) => c === void 0 ? { type: "field", table: l } : { type: "field", table: l, column: c }, i = nn(e, r.query).flatMap(({ name: l, table: c, column: f }) => t.flatMap((d) => e.relations.filter((m) => m.fromTable === c && m.fromColumn === f && m.toTable === d.table || m.toTable === c && m.toColumn === f && m.fromTable === d.table).map((m) => ({
217
+ const t = S(n).flatMap((l) => l !== r && l.table !== void 0 ? [{ table: l.table, alias: l.alias }] : []), o = (l, c) => c === void 0 ? { type: "field", table: l } : { type: "field", table: l, column: c }, i = en(e, r.query).flatMap(({ name: l, table: c, column: d }) => t.flatMap((f) => e.relations.filter((m) => m.fromTable === c && m.fromColumn === d && m.toTable === f.table || m.toTable === c && m.toColumn === d && m.fromTable === f.table).map((m) => ({
218
218
  name: l,
219
- right: o(T(d), m.fromTable === c ? m.toColumn : m.fromColumn)
220
- }))))[0], u = (a = en(e, r).columns[0]) == null ? void 0 : a.name;
219
+ right: o(v(f), m.fromTable === c ? m.toColumn : m.fromColumn)
220
+ }))))[0], u = (a = Ze(e, r).columns[0]) == null ? void 0 : a.name;
221
221
  return {
222
222
  type: "binary",
223
223
  operator: "=",
@@ -226,7 +226,7 @@ function rn(e, n, r) {
226
226
  };
227
227
  }
228
228
  const H = (e, n) => `${e} ${n}${e === 1 ? "" : "s"}`;
229
- function zn({ unit: e, offset: n, anchor: r }) {
229
+ function Zn({ unit: e, offset: n, anchor: r }) {
230
230
  const t = Math.abs(n);
231
231
  if (r) {
232
232
  const o = r === "start" ? "Start" : "End";
@@ -234,7 +234,7 @@ function zn({ unit: e, offset: n, anchor: r }) {
234
234
  }
235
235
  return n === 0 ? "Today" : e === "day" && t === 1 ? n < 0 ? "Yesterday" : "Tomorrow" : n < 0 ? `${H(t, e)} ago` : `In ${H(t, e)}`;
236
236
  }
237
- const Zn = {
237
+ const et = {
238
238
  // Section captions on the main canvas
239
239
  sectionFrom: "From Tables",
240
240
  sectionWhere: "Conditions",
@@ -314,7 +314,7 @@ const Zn = {
314
314
  * item, its words ("30 days ago", "Start of last month"), its presets'
315
315
  * "Custom", and its custom parts. */
316
316
  typeRelativeDate: "Relative date",
317
- relativeDate: zn,
317
+ relativeDate: Zn,
318
318
  relativeCustom: "Custom…",
319
319
  relativeUnits: {
320
320
  day: "days",
@@ -665,7 +665,7 @@ function G() {
665
665
  return Oe += 1, `join-${Date.now().toString(36)}-${Oe}`;
666
666
  }
667
667
  let Ue = 0;
668
- function et() {
668
+ function nt() {
669
669
  return Ue += 1, `select-${Date.now().toString(36)}-${Ue}`;
670
670
  }
671
671
  let Le = 0;
@@ -673,25 +673,25 @@ function De() {
673
673
  return Le += 1, `group-${Date.now().toString(36)}-${Le}`;
674
674
  }
675
675
  let je = 0;
676
- function nt() {
676
+ function tt() {
677
677
  return je += 1, `order-${Date.now().toString(36)}-${je}`;
678
678
  }
679
- function ce(e, n, r) {
679
+ function de(e, n, r) {
680
680
  if (n < 0 || n >= e.length || r < 0 || r >= e.length || n === r) return e;
681
681
  const t = [...e], [o] = t.splice(n, 1);
682
682
  return t.splice(r, 0, o), t;
683
683
  }
684
- const qe = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, de = (e) => (e ?? "").length <= D;
684
+ const qe = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, fe = (e) => (e ?? "").length <= D;
685
685
  function R(e, n, r) {
686
686
  return e.map((t) => t.id === n ? r(t) : t);
687
687
  }
688
- function on(e, n, r) {
688
+ function rn(e, n, r) {
689
689
  return e.map((t) => t.id === n ? r(t) : t);
690
690
  }
691
- function tt(e) {
691
+ function rt(e) {
692
692
  return e.query ? { id: e.id, alias: e.alias, query: e.query } : e.shared !== void 0 ? { shared: e.shared, alias: e.alias } : { table: e.table, alias: e.alias };
693
693
  }
694
- function rt(e, n, r) {
694
+ function ot(e, n, r) {
695
695
  return e.query ? {
696
696
  id: e.id,
697
697
  alias: e.alias,
@@ -712,9 +712,9 @@ function rt(e, n, r) {
712
712
  on: n
713
713
  };
714
714
  }
715
- function sn(e, n) {
715
+ function on(e, n) {
716
716
  const [r, ...t] = n;
717
- return { ...e, from: tt(r), joins: t };
717
+ return { ...e, from: rt(r), joins: t };
718
718
  }
719
719
  function I(e) {
720
720
  const n = /* @__PURE__ */ new Set(), r = (t) => {
@@ -751,7 +751,7 @@ function I(e) {
751
751
  // tables are joined inside it (withExistsJoins), never to the query
752
752
  // around it — that would repeat its rows, which EXISTS is there to avoid.
753
753
  case "exists": {
754
- const s = Je(t.query);
754
+ const s = Pe(t.query);
755
755
  I(t.link).filter((i) => !s.has(i)).forEach((i) => n.add(i));
756
756
  break;
757
757
  }
@@ -759,31 +759,37 @@ function I(e) {
759
759
  };
760
760
  return r(e), [...n];
761
761
  }
762
- function Ne(e, n, r, t, o) {
763
- const s = _(n), i = t === void 0 && o === void 0, u = i ? Gn(e, s, r) : void 0, a = {
762
+ function ie(e, n, r, t, o, s) {
763
+ const i = J(n), u = t === void 0 && o === void 0, a = n.joins.flatMap((d) => (d.joinType === "left" || d.joinType === "full") && d.table !== void 0 ? [d.table] : []), l = u ? Gn(e, i, r, { after: s, leftJoined: a }) : void 0, c = {
764
764
  id: G(),
765
765
  table: r,
766
- joinType: t ?? (u == null ? void 0 : u.joinType) ?? "inner",
766
+ joinType: t ?? (l == null ? void 0 : l.joinType) ?? "inner",
767
767
  // Else matched to a derived table holding its key, if one does.
768
- on: o ?? (u == null ? void 0 : u.on) ?? (i ? Yn(e, n, r) : void 0)
768
+ on: o ?? (l == null ? void 0 : l.on) ?? (u ? zn(e, n, r) : void 0)
769
769
  };
770
- return { ...n, joins: [...n.joins, a] };
770
+ return { ...n, joins: [...n.joins, c] };
771
+ }
772
+ function sn(e, n, r) {
773
+ const t = J(n);
774
+ if (t.includes(r)) return;
775
+ const o = Fn(e, t, r);
776
+ if (!o) return;
777
+ const s = [o.start, ...o.tables];
778
+ return o.tables.reduce(
779
+ (i, u, a) => ie(e, i, u, void 0, void 0, s[a]),
780
+ n
781
+ );
771
782
  }
772
- function ot(e, n, r) {
773
- const t = _(n), o = Ve(e, t, r);
774
- return (o && o.length > 0 ? o : [r]).reduce((i, u) => Ne(e, i, u), n);
783
+ function st(e, n, r) {
784
+ return sn(e, n, r) ?? ie(e, n, r);
775
785
  }
776
786
  function L(e, n, r) {
777
- return I(r).reduce((t, o) => {
778
- if (S(t).some((u) => u.alias === o)) return t;
779
- const s = _(t);
780
- return (Ve(e, s, o) ?? [o]).reduce((u, a) => Ne(e, u, a), t);
781
- }, n);
787
+ return I(r).reduce((t, o) => S(t).some((s) => s.alias === o) || J(t).includes(o) ? t : sn(e, t, o) ?? ie(e, t, o), n);
782
788
  }
783
- function st(e, n, r, t) {
789
+ function it(e, n, r, t) {
784
790
  return {
785
791
  ...L(e, n, t),
786
- select: on(n.select, r, (o) => ({ ...o, expression: t }))
792
+ select: rn(n.select, r, (o) => ({ ...o, expression: t }))
787
793
  };
788
794
  }
789
795
  function Ee(e, n, r) {
@@ -794,43 +800,43 @@ function Ee(e, n, r) {
794
800
  const [i, u] = t;
795
801
  let a, l;
796
802
  if (n === 0 ? (l = i, a = u) : r === 0 && (a = t[n], l = i), a && l) {
797
- const { on: c, joinType: f } = a;
798
- o[o.indexOf(l)] = rt(l, c, f);
803
+ const { on: c, joinType: d } = a;
804
+ o[o.indexOf(l)] = ot(l, c, d);
799
805
  }
800
- return sn(e, o);
806
+ return on(e, o);
801
807
  }
802
808
  function an(e) {
803
- const n = /* @__PURE__ */ new Set([T(e.from)]);
809
+ const n = /* @__PURE__ */ new Set([v(e.from)]);
804
810
  let r;
805
811
  return e.joins.some((t) => {
806
- n.add(T(t));
812
+ n.add(v(t));
807
813
  const o = t.joinType === "cross" ? void 0 : t.on, s = I(o).find((i) => !n.has(i));
808
- return s && (r = { table: t.table ?? T(t), needs: s }), s !== void 0;
814
+ return s && (r = { table: t.table ?? v(t), needs: s }), s !== void 0;
809
815
  }), r;
810
816
  }
811
- function Gt(e, n, r) {
817
+ function Ft(e, n, r) {
812
818
  const t = Ee(e, n, r);
813
819
  return t === e ? void 0 : an(t);
814
820
  }
815
- function it(e, n, r) {
821
+ function at(e, n, r) {
816
822
  const t = Ee(e, n, r);
817
823
  return t !== e && an(t) === void 0;
818
824
  }
819
- function at(e, n, r, t) {
825
+ function ut(e, n, r, t) {
820
826
  return {
821
827
  ...L(e, n, t),
822
828
  orderBy: (n.orderBy ?? []).map((o) => o.id === r ? { ...o, expression: t } : o)
823
829
  };
824
830
  }
825
- function ut(e, n, r, t) {
831
+ function lt(e, n, r, t) {
826
832
  return {
827
833
  ...L(e, n, t),
828
834
  groupBy: (n.groupBy ?? []).map((o) => o.id === r ? { ...o, expression: t } : o)
829
835
  };
830
836
  }
831
- function ie(e, n) {
837
+ function ae(e, n) {
832
838
  if (!n || !qn(n)) return n;
833
- const r = oe(n), t = r.map((a) => ie(e, a)), s = t.some((a, l) => a !== r[l]) ? P(n, (a) => t[r.indexOf(a)]) : n;
839
+ const r = oe(n), t = r.map((a) => ae(e, a)), s = t.some((a, l) => a !== r[l]) ? _(n, (a) => t[r.indexOf(a)]) : n;
834
840
  if (s.type !== "exists") return s;
835
841
  const { query: i } = s, { joins: u } = L(
836
842
  e,
@@ -844,20 +850,20 @@ function ie(e, n) {
844
850
  );
845
851
  return u === i.joins ? s : { ...s, query: { ...i, joins: u } };
846
852
  }
847
- function Ft(e, n, r) {
848
- const t = ie(e, r);
853
+ function Qt(e, n, r) {
854
+ const t = ae(e, r);
849
855
  return { ...L(e, n, t), having: t };
850
856
  }
851
857
  function Re(e, n) {
852
858
  const r = /* @__PURE__ */ new Set([
853
- ...n ? S(n).map(T) : [],
859
+ ...n ? S(n).map(v) : [],
854
860
  ...q(e).map((o) => o.name)
855
861
  ]);
856
862
  let t = 1;
857
863
  for (; r.has(`sub_query_${t}`); ) t += 1;
858
864
  return `sub_query_${t}`;
859
865
  }
860
- function Qt() {
866
+ function _t() {
861
867
  return { id: E() };
862
868
  }
863
869
  function he(e, n, r) {
@@ -882,27 +888,27 @@ function he(e, n, r) {
882
888
  }
883
889
  };
884
890
  case "changeBaseAlias":
885
- return de(r.alias) ? {
891
+ return fe(r.alias) ? {
886
892
  ...n,
887
893
  // A derived table (or a shared query's place) goes by its alias: it
888
894
  // can't be left without one.
889
895
  from: n.from.table === void 0 ? { ...n.from, alias: r.alias || n.from.alias } : { ...n.from, alias: r.alias }
890
896
  } : n;
891
897
  case "addJoin":
892
- return r.joinType === void 0 && r.on === void 0 ? ot(e, n, r.table) : Ne(e, n, r.table, r.joinType, r.on);
898
+ return r.joinType === void 0 && r.on === void 0 ? st(e, n, r.table) : ie(e, n, r.table, r.joinType, r.on);
893
899
  case "addDerivedJoin": {
894
900
  const t = {
895
901
  id: r.id ?? G(),
896
902
  alias: Re(e, n),
897
903
  joinType: "left",
898
904
  query: { from: Me(r), joins: [], select: [] }
899
- }, o = rn(e, n, t);
905
+ }, o = tn(e, n, t);
900
906
  return { ...n, joins: [...n.joins, { ...t, on: o }] };
901
907
  }
902
908
  case "renameDerivedTable": {
903
909
  const t = O(n).find((a) => a.id === r.derivedId), o = r.alias.trim();
904
910
  if (!t || o === t.alias) return n;
905
- const s = S(n).filter((a) => a !== t).map(T);
911
+ const s = S(n).filter((a) => a !== t).map(v);
906
912
  if (!o || o.length > D || s.includes(o) || A(e, o)) return n;
907
913
  const i = ke(n, t.alias, o), { from: u } = i;
908
914
  return {
@@ -914,7 +920,7 @@ function he(e, n, r) {
914
920
  case "renameSharedPlace": {
915
921
  const t = r.joinId === void 0 ? n.from : n.joins.find((u) => u.id === r.joinId), o = r.alias.trim();
916
922
  if ((t == null ? void 0 : t.shared) === void 0 || o === t.alias) return n;
917
- const s = S(n).filter((u) => u !== t).map(T);
923
+ const s = S(n).filter((u) => u !== t).map(v);
918
924
  if (!o || o.length > D || s.includes(o) || A(e, o)) return n;
919
925
  const i = ke(n, t.alias, o);
920
926
  return r.joinId === void 0 ? { ...i, from: { ...i.from, alias: o } } : { ...i, joins: R(i.joins, r.joinId, (u) => ({ ...u, alias: o })) };
@@ -933,7 +939,7 @@ function he(e, n, r) {
933
939
  joins: R(n.joins, r.joinId, (t) => ({ ...t, joinType: r.joinType }))
934
940
  };
935
941
  case "changeJoinAlias":
936
- return de(r.alias) ? {
942
+ return fe(r.alias) ? {
937
943
  ...n,
938
944
  // A derived table (or a shared query's place) goes by its alias: it
939
945
  // can't be left without one.
@@ -944,7 +950,7 @@ function he(e, n, r) {
944
950
  ...n,
945
951
  joins: R(n.joins, r.joinId, (t) => ({
946
952
  ...t,
947
- on: ie(e, r.on)
953
+ on: ae(e, r.on)
948
954
  }))
949
955
  };
950
956
  case "reorderJoin": {
@@ -956,26 +962,26 @@ function he(e, n, r) {
956
962
  return [s[t], s[o]] = [s[o], s[t]], { ...n, joins: s };
957
963
  }
958
964
  case "reorderTables":
959
- return it(n, r.fromIndex, r.toIndex) ? Ee(n, r.fromIndex, r.toIndex) : n;
965
+ return at(n, r.fromIndex, r.toIndex) ? Ee(n, r.fromIndex, r.toIndex) : n;
960
966
  case "removeBaseTable":
961
- return n.joins.length > 0 ? sn(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
967
+ return n.joins.length > 0 ? on(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
962
968
  case "addSelectItem": {
963
- const t = { id: et(), expression: { type: "field" } };
969
+ const t = { id: nt(), expression: { type: "field" } };
964
970
  return { ...n, select: [...n.select, t] };
965
971
  }
966
972
  case "removeSelectItem":
967
973
  return { ...n, select: n.select.filter((t) => t.id !== r.selectId) };
968
974
  case "reorderSelectItems": {
969
- const t = ce(n.select, r.fromIndex, r.toIndex);
975
+ const t = de(n.select, r.fromIndex, r.toIndex);
970
976
  return t === n.select ? n : { ...n, select: t };
971
977
  }
972
978
  case "setSelectAlias":
973
- return de(r.alias) ? {
979
+ return fe(r.alias) ? {
974
980
  ...n,
975
- select: on(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
981
+ select: rn(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
976
982
  } : n;
977
983
  case "setSelectExpression":
978
- return st(e, n, r.selectId, r.expression);
984
+ return it(e, n, r.selectId, r.expression);
979
985
  case "addGroupByItem": {
980
986
  const t = { id: De(), expression: { type: "field" } };
981
987
  return { ...n, groupBy: [...n.groupBy ?? [], t] };
@@ -985,11 +991,11 @@ function he(e, n, r) {
985
991
  return { ...n, groupBy: t.length > 0 ? t : void 0 };
986
992
  }
987
993
  case "reorderGroupByItems": {
988
- const t = n.groupBy ?? [], o = ce(t, r.fromIndex, r.toIndex);
994
+ const t = n.groupBy ?? [], o = de(t, r.fromIndex, r.toIndex);
989
995
  return o === t ? n : { ...n, groupBy: o };
990
996
  }
991
997
  case "setGroupByExpression":
992
- return ut(e, n, r.groupById, r.expression);
998
+ return lt(e, n, r.groupById, r.expression);
993
999
  case "groupBySelectItem": {
994
1000
  const t = n.select.find((s) => s.id === r.selectId);
995
1001
  if (!t) return n;
@@ -1002,7 +1008,7 @@ function he(e, n, r) {
1002
1008
  return { ...n, distinct: r.distinct || void 0 };
1003
1009
  case "addOrderByItem": {
1004
1010
  const t = {
1005
- id: nt(),
1011
+ id: tt(),
1006
1012
  expression: { type: "field" },
1007
1013
  direction: "asc"
1008
1014
  };
@@ -1013,11 +1019,11 @@ function he(e, n, r) {
1013
1019
  return { ...n, orderBy: t.length > 0 ? t : void 0 };
1014
1020
  }
1015
1021
  case "reorderOrderByItems": {
1016
- const t = n.orderBy ?? [], o = ce(t, r.fromIndex, r.toIndex);
1022
+ const t = n.orderBy ?? [], o = de(t, r.fromIndex, r.toIndex);
1017
1023
  return o === t ? n : { ...n, orderBy: o };
1018
1024
  }
1019
1025
  case "setOrderByExpression":
1020
- return at(e, n, r.orderById, r.expression);
1026
+ return ut(e, n, r.orderById, r.expression);
1021
1027
  case "setOrderByDirection":
1022
1028
  return {
1023
1029
  ...n,
@@ -1036,65 +1042,65 @@ function he(e, n, r) {
1036
1042
  return n;
1037
1043
  }
1038
1044
  }
1039
- const lt = {
1045
+ const ct = {
1040
1046
  inner: "INNER",
1041
1047
  left: "LEFT",
1042
1048
  right: "RIGHT",
1043
1049
  full: "FULL",
1044
1050
  cross: "CROSS"
1045
1051
  };
1046
- function Pt(e) {
1047
- return `${lt[e.joinType]} JOIN ${T(e)}`;
1052
+ function Jt(e) {
1053
+ return `${ct[e.joinType]} JOIN ${v(e)}`;
1048
1054
  }
1049
- function _t(e) {
1055
+ function Pt(e) {
1050
1056
  return e.alias || "Column";
1051
1057
  }
1052
- function Jt() {
1058
+ function Vt() {
1053
1059
  return "Sort";
1054
1060
  }
1055
- function Vt() {
1061
+ function Wt() {
1056
1062
  return "Group";
1057
1063
  }
1058
- function Wt(e, n, r) {
1064
+ function Ht(e, n, r) {
1059
1065
  const t = S(n);
1060
1066
  if (r !== void 0) {
1061
1067
  const a = t.findIndex((l) => "id" in l && l.id === r);
1062
- return new Set(t.slice(0, a + 1).map(T));
1068
+ return new Set(t.slice(0, a + 1).map(v));
1063
1069
  }
1064
- const o = new Set(_(n)), s = Rn(e, [...o]), i = new Set(tn(e, n).map((a) => a.table)), u = q(e).filter((a) => !o.has(a.name) && (s.has(a.name) || i.has(a.name)));
1065
- return /* @__PURE__ */ new Set([...t.map(T), ...u.map((a) => a.name)]);
1070
+ const o = new Set(J(n)), s = Rn(e, [...o]), i = new Set(nn(e, n).map((a) => a.table)), u = q(e).filter((a) => !o.has(a.name) && (s.has(a.name) || i.has(a.name)));
1071
+ return /* @__PURE__ */ new Set([...t.map(v), ...u.map((a) => a.name)]);
1066
1072
  }
1067
- function Ht(e, n) {
1073
+ function Xt(e, n) {
1068
1074
  const r = S(n);
1069
1075
  return q(e).flatMap((t) => {
1070
- const o = r.filter((s) => s.table === t.name).map(T);
1076
+ const o = r.filter((s) => s.table === t.name).map(v);
1071
1077
  return (o.length > 0 ? o : [t.name]).map((s) => ({ ref: s, table: t }));
1072
1078
  });
1073
1079
  }
1074
- function Xt(e, n) {
1075
- const r = new Set(_(n));
1080
+ function Kt(e, n) {
1081
+ const r = new Set(J(n));
1076
1082
  return q(e).filter((t) => !r.has(t.name));
1077
1083
  }
1078
- function ye(e) {
1079
- return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + ye(r), 0) : e.type === "not" ? ye(e.operand) : 1 : 0;
1084
+ function be(e) {
1085
+ return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + be(r), 0) : e.type === "not" ? be(e.operand) : 1 : 0;
1080
1086
  }
1081
1087
  function Ge(e) {
1082
1088
  return e.column ? e.table ? `${e.table}.${e.column}` : e.column : "?";
1083
1089
  }
1084
- function Kt(e, n = Zn) {
1090
+ function Yt(e, n = et) {
1085
1091
  var r;
1086
- return e ? e.type === "binary" && e.left.type === "field" && ((r = e.right) == null ? void 0 : r.type) === "field" ? !e.left.column && !e.right.column ? n.setJoinCondition : `${Ge(e.left)} ${e.operator ?? "="} ${Ge(e.right)}` : n.conditionCount(ye(e)) : n.setJoinCondition;
1092
+ return e ? e.type === "binary" && e.left.type === "field" && ((r = e.right) == null ? void 0 : r.type) === "field" ? !e.left.column && !e.right.column ? n.setJoinCondition : `${Ge(e.left)} ${e.operator ?? "="} ${Ge(e.right)}` : n.conditionCount(be(e)) : n.setJoinCondition;
1087
1093
  }
1088
- function Yt(e, n) {
1094
+ function zt(e, n) {
1089
1095
  const r = new Set(I(e.where));
1090
1096
  return I(e.having).forEach((t) => r.add(t)), e.joins.filter((t) => t.id !== n).forEach((t) => I(t.on).forEach((o) => r.add(o))), [...e.select, ...e.groupBy ?? [], ...e.orderBy ?? []].forEach((t) => I(t.expression).forEach((o) => r.add(o))), r;
1091
1097
  }
1092
- function zt(e, n, r) {
1093
- const t = ie(e, r);
1098
+ function Zt(e, n, r) {
1099
+ const t = ae(e, r);
1094
1100
  return { ...L(e, n, t), where: t };
1095
1101
  }
1096
- function Zt(e, n, r, t) {
1097
- return r.length === 0 ? he(e, n, t) : U(n) ? Ze(n, r, (o) => he(e, o, t)) : n;
1102
+ function er(e, n, r, t) {
1103
+ return r.length === 0 ? he(e, n, t) : U(n) ? ze(n, r, (o) => he(e, o, t)) : n;
1098
1104
  }
1099
1105
  const un = (e) => e.query !== void 0 && !C(e.query);
1100
1106
  function ln(e) {
@@ -1103,7 +1109,7 @@ function ln(e) {
1103
1109
  n.set(r.fromTable, [...n.get(r.fromTable) ?? [], r.toTable]);
1104
1110
  }), n;
1105
1111
  }
1106
- function J(e, n, r = ln(e)) {
1112
+ function P(e, n, r = ln(e)) {
1107
1113
  const t = /* @__PURE__ */ new Set([n]), o = [n];
1108
1114
  for (let s = o.shift(); s !== void 0; s = o.shift())
1109
1115
  (r.get(s) ?? []).forEach((i) => {
@@ -1113,7 +1119,7 @@ function J(e, n, r = ln(e)) {
1113
1119
  }
1114
1120
  const j = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [];
1115
1121
  function cn(e, n) {
1116
- const r = new Map(S(e).map((t) => [T(t), t.table]));
1122
+ const r = new Map(S(e).map((t) => [v(t), t.table]));
1117
1123
  return I(n).map((t) => ({
1118
1124
  ref: t,
1119
1125
  table: r.has(t) ? r.get(t) : t
@@ -1121,8 +1127,8 @@ function cn(e, n) {
1121
1127
  }
1122
1128
  const Ce = (e, n) => cn(e, n).map((r) => r.table);
1123
1129
  function Fe(e, n) {
1124
- const r = new Map(S(e).map((t) => [T(t), t.table]));
1125
- return ve(n, (t) => {
1130
+ const r = new Map(S(e).map((t) => [v(t), t.table]));
1131
+ return Ne(n, (t) => {
1126
1132
  if (t.type !== "field" || t.table === void 0) return t;
1127
1133
  const o = r.get(t.table);
1128
1134
  return o === void 0 || o === t.table ? t : { ...t, table: o };
@@ -1131,19 +1137,19 @@ function Fe(e, n) {
1131
1137
  function dn(e, n) {
1132
1138
  const r = (n.groupBy ?? []).flatMap((o) => cn(n, o.expression)), t = ln(e);
1133
1139
  return q(e).map((o) => {
1134
- const s = J(e, o.name, t), i = r.find((u) => u.table === void 0 || !s.has(u.table));
1140
+ const s = P(e, o.name, t), i = r.find((u) => u.table === void 0 || !s.has(u.table));
1135
1141
  return i ? { table: o, unlinked: i.table ?? i.ref } : { table: o };
1136
1142
  });
1137
1143
  }
1138
- function ct(e, n, r) {
1139
- const t = J(e, r);
1144
+ function dt(e, n, r) {
1145
+ const t = P(e, r);
1140
1146
  return j(n.where).filter((o) => {
1141
1147
  const s = Ce(n, o);
1142
1148
  return s.length > 0 && s.every((i) => i !== void 0 && t.has(i));
1143
1149
  });
1144
1150
  }
1145
- function er(e, n, r) {
1146
- const t = J(e, r);
1151
+ function nr(e, n, r) {
1152
+ const t = P(e, r);
1147
1153
  return j(n.where).some((o) => {
1148
1154
  const s = Ce(n, o);
1149
1155
  return s.some((i) => i !== void 0 && t.has(i)) && s.some((i) => i === void 0 || !t.has(i));
@@ -1154,7 +1160,7 @@ function X(e, n) {
1154
1160
  for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
1155
1161
  return r;
1156
1162
  }
1157
- function be(e, n) {
1163
+ function ge(e, n) {
1158
1164
  if (e.type === "field" && e.column) return e.column;
1159
1165
  if (e.type === "function") {
1160
1166
  const r = e.args[0];
@@ -1162,8 +1168,8 @@ function be(e, n) {
1162
1168
  }
1163
1169
  return n;
1164
1170
  }
1165
- const ge = (e, n) => ({ type: "field", table: e, column: n });
1166
- function dt(e, n) {
1171
+ const we = (e, n) => ({ type: "field", table: e, column: n });
1172
+ function ft(e, n) {
1167
1173
  return {
1168
1174
  type: "logical",
1169
1175
  operator: "OR",
@@ -1185,60 +1191,60 @@ function dt(e, n) {
1185
1191
  ]
1186
1192
  };
1187
1193
  }
1188
- function ft(e, n, r) {
1194
+ function mt(e, n, r) {
1189
1195
  var s;
1190
1196
  if (r.type !== "field" || r.table === void 0 || r.column === void 0) return !1;
1191
1197
  const t = Ce(n, r)[0], o = (s = A(e, t)) == null ? void 0 : s.columns.find((i) => i.name === r.column);
1192
1198
  return o !== void 0 && !o.nullable;
1193
1199
  }
1194
1200
  function fn(e, n, r, t) {
1195
- const o = n.groupBy.map((i) => i.expression), s = t.map((i, u) => ft(e, n, o[u]) ? {
1201
+ const o = n.groupBy.map((i) => i.expression), s = t.map((i, u) => mt(e, n, o[u]) ? {
1196
1202
  type: "binary",
1197
1203
  operator: "=",
1198
- left: ge(r, i),
1204
+ left: we(r, i),
1199
1205
  right: o[u]
1200
- } : dt(ge(r, i), o[u]));
1206
+ } : ft(we(r, i), o[u]));
1201
1207
  return s.length === 1 ? s[0] : { type: "logical", operator: "AND", operands: s };
1202
1208
  }
1203
1209
  function mn(e, n, r, t, o, s) {
1204
1210
  var b;
1205
- const i = n.groupBy.map((h) => Fe(n, h.expression)), u = ct(e, n, r).map((h) => Fe(n, h)), a = new Set(o.map((h) => h.alias).filter((h) => !!h)), l = i.map((h, w) => {
1206
- const v = X(be(h, `key_${w + 1}`), a);
1207
- return a.add(v), v;
1211
+ const i = n.groupBy.map((y) => Fe(n, y.expression)), u = dt(e, n, r).map((y) => Fe(n, y)), a = new Set(o.map((y) => y.alias).filter((y) => !!y)), l = i.map((y, w) => {
1212
+ const T = X(ge(y, `key_${w + 1}`), a);
1213
+ return a.add(T), T;
1208
1214
  }), c = [...u, ...t];
1209
- let f = {
1215
+ let d = {
1210
1216
  id: "",
1211
1217
  from: { table: r },
1212
1218
  joins: [],
1213
1219
  select: []
1214
1220
  };
1215
- [...i, ...c, ...o.map((h) => h.expression)].forEach((h) => {
1216
- f = L(e, f, h);
1221
+ [...i, ...c, ...o.map((y) => y.expression)].forEach((y) => {
1222
+ d = L(e, d, y);
1217
1223
  });
1218
- const d = ((b = s == null ? void 0 : s.groupBy) == null ? void 0 : b.length) ?? 0, m = [...(s == null ? void 0 : s.joins) ?? []], g = f.joins.map((h) => {
1219
- const w = m.findIndex(($) => $.table !== void 0 && $.table === h.table);
1220
- if (w === -1) return h;
1221
- const [v] = m.splice(w, 1);
1222
- return { ...h, id: v.id };
1224
+ const f = ((b = s == null ? void 0 : s.groupBy) == null ? void 0 : b.length) ?? 0, m = [...(s == null ? void 0 : s.joins) ?? []], g = d.joins.map((y) => {
1225
+ const w = m.findIndex(($) => $.table !== void 0 && $.table === y.table);
1226
+ if (w === -1) return y;
1227
+ const [T] = m.splice(w, 1);
1228
+ return { ...y, id: T.id };
1223
1229
  }), p = {
1224
1230
  ...s,
1225
- from: f.from,
1231
+ from: d.from,
1226
1232
  joins: g,
1227
1233
  where: c.length > 0 ? { type: "logical", operator: "AND", operands: c } : void 0,
1228
- groupBy: i.map((h, w) => {
1229
- var v, $;
1234
+ groupBy: i.map((y, w) => {
1235
+ var T, $;
1230
1236
  return {
1231
- id: (($ = (v = s == null ? void 0 : s.groupBy) == null ? void 0 : v[w]) == null ? void 0 : $.id) ?? E(),
1232
- expression: h
1237
+ id: (($ = (T = s == null ? void 0 : s.groupBy) == null ? void 0 : T[w]) == null ? void 0 : $.id) ?? E(),
1238
+ expression: y
1233
1239
  };
1234
1240
  }),
1235
1241
  select: [
1236
- ...i.map((h, w) => {
1237
- var v;
1242
+ ...i.map((y, w) => {
1243
+ var T;
1238
1244
  return {
1239
- id: w < d && ((v = s == null ? void 0 : s.select[w]) == null ? void 0 : v.id) || E(),
1245
+ id: w < f && ((T = s == null ? void 0 : s.select[w]) == null ? void 0 : T.id) || E(),
1240
1246
  alias: l[w],
1241
- expression: h
1247
+ expression: y
1242
1248
  };
1243
1249
  }),
1244
1250
  ...o
@@ -1246,7 +1252,7 @@ function mn(e, n, r, t, o, s) {
1246
1252
  };
1247
1253
  return p.where === void 0 && delete p.where, { query: p, copied: u.length, keyNames: l };
1248
1254
  }
1249
- function nr(e, n, r) {
1255
+ function tr(e, n, r) {
1250
1256
  var g;
1251
1257
  if ((n.groupBy ?? []).length === 0)
1252
1258
  throw new Error("addMeasure: the query isn't grouped (a measure is per group)");
@@ -1261,25 +1267,25 @@ function nr(e, n, r) {
1261
1267
  let { joins: i } = n, u, a;
1262
1268
  if (s) {
1263
1269
  u = s.alias;
1264
- const p = ((g = s.query.groupBy) == null ? void 0 : g.length) ?? 0, b = s.query.select.slice(p).find((h) => M(h.expression, r.value));
1270
+ const p = ((g = s.query.groupBy) == null ? void 0 : g.length) ?? 0, b = s.query.select.slice(p).find((y) => M(y.expression, r.value));
1265
1271
  if (b)
1266
1272
  a = b.alias;
1267
1273
  else {
1268
1274
  a = X(
1269
- be(r.value, "value"),
1275
+ ge(r.value, "value"),
1270
1276
  new Set(s.query.select.map((w) => w.alias ?? ""))
1271
1277
  );
1272
- const h = { id: E(), alias: a, expression: r.value };
1273
- i = i.map((w) => w === s ? { ...s, query: { ...s.query, select: [...s.query.select, h] } } : w);
1278
+ const y = { id: E(), alias: a, expression: r.value };
1279
+ i = i.map((w) => w === s ? { ...s, query: { ...s.query, select: [...s.query.select, y] } } : w);
1274
1280
  }
1275
1281
  } else {
1276
- a = be(r.value, "value");
1277
- const p = { id: E(), alias: a, expression: r.value }, b = mn(e, n, r.table, o, [p]), h = /* @__PURE__ */ new Set([
1278
- ...S(n).map(T),
1282
+ a = ge(r.value, "value");
1283
+ const p = { id: E(), alias: a, expression: r.value }, b = mn(e, n, r.table, o, [p]), y = /* @__PURE__ */ new Set([
1284
+ ...S(n).map(v),
1279
1285
  ...q(e).map(($) => $.name)
1280
1286
  ]), w = `${r.table}_per_${b.keyNames.join("_and_")}`.slice(0, D - 4);
1281
- u = X(w, h);
1282
- const v = {
1287
+ u = X(w, y);
1288
+ const T = {
1283
1289
  id: E(),
1284
1290
  alias: u,
1285
1291
  joinType: "left",
@@ -1287,24 +1293,24 @@ function nr(e, n, r) {
1287
1293
  on: fn(e, n, u, b.keyNames),
1288
1294
  drillAcross: { synced: !0, copied: b.copied }
1289
1295
  };
1290
- i = [...i, v];
1296
+ i = [...i, T];
1291
1297
  }
1292
1298
  const l = {
1293
1299
  type: "function",
1294
1300
  name: "MAX",
1295
- args: [ge(u, a)]
1296
- }, c = new Set(n.select.map((p) => p.alias).filter((p) => !!p)), f = X(`${r.table}_${a}`.slice(0, D - 4), c);
1297
- let d = r.selectId !== void 0 ? n.select.map((p) => p.id === r.selectId ? { ...p, expression: l, alias: p.alias || f } : p) : [...n.select, { id: E(), expression: l, alias: f }];
1298
- return d = [...n.groupBy.filter((p) => !d.some((b) => M(b.expression, p.expression))).map((p) => ({ id: E(), expression: p.expression })), ...d], { ...n, joins: i, select: d };
1301
+ args: [we(u, a)]
1302
+ }, c = new Set(n.select.map((p) => p.alias).filter((p) => !!p)), d = X(`${r.table}_${a}`.slice(0, D - 4), c);
1303
+ let f = r.selectId !== void 0 ? n.select.map((p) => p.id === r.selectId ? { ...p, expression: l, alias: p.alias || d } : p) : [...n.select, { id: E(), expression: l, alias: d }];
1304
+ return f = [...n.groupBy.filter((p) => !f.some((b) => M(b.expression, p.expression))).map((p) => ({ id: E(), expression: p.expression })), ...f], { ...n, joins: i, select: f };
1299
1305
  }
1300
1306
  function pn(e, n, r) {
1301
- var c, f;
1307
+ var c, d;
1302
1308
  if (!un(r)) return;
1303
1309
  const t = r, o = t.query.from.table, s = n.groupBy ?? [];
1304
1310
  if (o === void 0 || s.length === 0) return;
1305
- const i = dn(e, n).find((d) => d.table.name === o);
1311
+ const i = dn(e, n).find((f) => f.table.name === o);
1306
1312
  if (!i || i.unlinked !== void 0) return;
1307
- const u = ((c = t.query.groupBy) == null ? void 0 : c.length) ?? 0, a = j(t.query.where).slice(((f = t.drillAcross) == null ? void 0 : f.copied) ?? 0), l = mn(e, n, o, a, t.query.select.slice(u), t.query);
1313
+ const u = ((c = t.query.groupBy) == null ? void 0 : c.length) ?? 0, a = j(t.query.where).slice(((d = t.drillAcross) == null ? void 0 : d.copied) ?? 0), l = mn(e, n, o, a, t.query.select.slice(u), t.query);
1308
1314
  return {
1309
1315
  ...t,
1310
1316
  query: l.query,
@@ -1322,15 +1328,15 @@ function z(e) {
1322
1328
  on: e.on
1323
1329
  };
1324
1330
  }
1325
- function tr(e, n, r) {
1331
+ function rr(e, n, r) {
1326
1332
  const t = pn(e, n, r);
1327
1333
  return t !== void 0 && M(z(t), z(r));
1328
1334
  }
1329
- function hn(e, n, r) {
1335
+ function yn(e, n, r) {
1330
1336
  let t = !1;
1331
1337
  const o = (u, a) => se(
1332
1338
  a,
1333
- (l, c) => hn(
1339
+ (l, c) => yn(
1334
1340
  e,
1335
1341
  u && C(u) === C(a) ? Y(u)[c] : void 0,
1336
1342
  l
@@ -1341,25 +1347,25 @@ function hn(e, n, r) {
1341
1347
  })() : r.from;
1342
1348
  s !== r.from && (t = !0);
1343
1349
  const i = r.joins.map((u) => {
1344
- var f;
1350
+ var d;
1345
1351
  if (!u.query) return u;
1346
- const a = n == null ? void 0 : n.joins.find((d) => d.id === u.id && d.query !== void 0), l = o(a == null ? void 0 : a.query, u.query);
1352
+ const a = n == null ? void 0 : n.joins.find((f) => f.id === u.id && f.query !== void 0), l = o(a == null ? void 0 : a.query, u.query);
1347
1353
  let c = l === u.query ? u : { ...u, query: l };
1348
- if ((f = c.drillAcross) != null && f.synced) {
1349
- const d = pn(e, r, c);
1350
- d && !M(d, c) && (c = d);
1354
+ if ((d = c.drillAcross) != null && d.synced) {
1355
+ const f = pn(e, r, c);
1356
+ f && !M(f, c) && (c = f);
1351
1357
  } else if (c.drillAcross && (a != null && a.drillAcross) && !M(z(a), z(c))) {
1352
- const d = { ...c };
1353
- delete d.drillAcross, c = d;
1358
+ const f = { ...c };
1359
+ delete f.drillAcross, c = f;
1354
1360
  }
1355
1361
  return c !== u && (t = !0), c;
1356
1362
  });
1357
1363
  return t ? { ...r, from: s, joins: i } : r;
1358
1364
  }
1359
- function mt(e, n, r) {
1360
- return U(r) ? hn(e, n && U(n) ? n : void 0, r) : r;
1365
+ function pt(e, n, r) {
1366
+ return U(r) ? yn(e, n && U(n) ? n : void 0, r) : r;
1361
1367
  }
1362
- const rr = ["text", "number", "date", "boolean"], pt = {
1368
+ const or = ["text", "number", "date", "boolean"], yt = {
1363
1369
  varchar: "text",
1364
1370
  integer: "integer",
1365
1371
  decimal: "number",
@@ -1369,9 +1375,9 @@ const rr = ["text", "number", "date", "boolean"], pt = {
1369
1375
  boolean: "boolean"
1370
1376
  };
1371
1377
  function ht(e) {
1372
- return pt[e];
1378
+ return yt[e];
1373
1379
  }
1374
- function yt(e) {
1380
+ function bt(e) {
1375
1381
  const n = e.trim().toUpperCase();
1376
1382
  if (/^((TINY|SMALL|MEDIUM|BIG)?INT|SIGNED|UNSIGNED)/.test(n)) return "integer";
1377
1383
  if (/^(DECIMAL|NUMERIC|FLOAT|DOUBLE|REAL|MONEY)/.test(n)) return "number";
@@ -1381,14 +1387,14 @@ function yt(e) {
1381
1387
  if (/^BOOL|^BIT$/.test(n)) return "boolean";
1382
1388
  if (/^(N?VAR)?CHAR|^N?TEXT|^STRING/.test(n)) return "text";
1383
1389
  }
1384
- function yn(e) {
1390
+ function hn(e) {
1385
1391
  if (e.as) return e.as;
1386
1392
  const { value: n } = e;
1387
1393
  if (typeof n == "string") return n === "" ? void 0 : "text";
1388
1394
  if (typeof n == "number") return Number.isInteger(n) ? "integer" : "number";
1389
1395
  if (typeof n == "bigint") return "integer";
1390
1396
  if (typeof n == "boolean") return "boolean";
1391
- if (n instanceof Date) return He(n) ? "date" : "dateTime";
1397
+ if (n instanceof Date) return We(n) ? "date" : "dateTime";
1392
1398
  }
1393
1399
  function bn(e, n, r) {
1394
1400
  if (!e) return;
@@ -1397,11 +1403,11 @@ function bn(e, n, r) {
1397
1403
  case "field":
1398
1404
  return n(e);
1399
1405
  case "literal":
1400
- return yn(e);
1406
+ return hn(e);
1401
1407
  case "function": {
1402
- const o = Fn(e.name, r), s = (o == null ? void 0 : o.args.findIndex((a) => a.kind === "type-name")) ?? -1, i = s >= 0 ? e.args[s] : void 0;
1408
+ const o = Qn(e.name, r), s = (o == null ? void 0 : o.args.findIndex((a) => a.kind === "type-name")) ?? -1, i = s >= 0 ? e.args[s] : void 0;
1403
1409
  if ((i == null ? void 0 : i.type) === "literal" && typeof i.value == "string")
1404
- return yt(i.value);
1410
+ return bt(i.value);
1405
1411
  const u = o == null ? void 0 : o.returns;
1406
1412
  return u === "arg" ? t(e.args[0]) : u;
1407
1413
  }
@@ -1413,28 +1419,28 @@ function bn(e, n, r) {
1413
1419
  return;
1414
1420
  }
1415
1421
  }
1416
- const bt = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, gt = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, wt = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
1422
+ const gt = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, wt = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, St = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
1417
1423
  function gn(e, n) {
1418
1424
  if (e === "" || e === null) return e;
1419
1425
  const r = typeof e == "string" ? e.trim() : void 0;
1420
1426
  switch (n) {
1421
1427
  case "text":
1422
- return e instanceof Date ? _n(e) : String(e);
1428
+ return e instanceof Date ? Pn(e) : String(e);
1423
1429
  case "integer":
1424
1430
  case "number": {
1425
1431
  let t;
1426
- return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && bt.test(r) && (t = Number(r)), t === void 0 || !Wn(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
1432
+ return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && gt.test(r) && (t = Number(r)), t === void 0 || !Hn(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
1427
1433
  }
1428
1434
  case "date":
1429
1435
  case "dateTime": {
1430
- if (e instanceof Date) return n === "date" && !He(e) ? void 0 : e;
1431
- const t = r === void 0 ? null : wt.exec(r);
1436
+ if (e instanceof Date) return n === "date" && !We(e) ? void 0 : e;
1437
+ const t = r === void 0 ? null : St.exec(r);
1432
1438
  if (!t) return;
1433
- const [, o, s, i, u, a, l] = t.map(Number), c = Pn(o, s, i);
1439
+ const [, o, s, i, u, a, l] = t.map(Number), c = Jn(o, s, i);
1434
1440
  return c.getUTCMonth() !== s - 1 || c.getUTCDate() !== i ? void 0 : t[4] === void 0 ? c : n === "date" ? void 0 : new Date(Date.UTC(o, s - 1, i, u, a, Number.isNaN(l) ? 0 : l));
1435
1441
  }
1436
1442
  case "time": {
1437
- const t = r === void 0 ? null : gt.exec(r);
1443
+ const t = r === void 0 ? null : wt.exec(r);
1438
1444
  return t ? `${t[1]}:${t[2]}:${t[3] ?? "00"}` : void 0;
1439
1445
  }
1440
1446
  case "boolean":
@@ -1444,28 +1450,28 @@ function gn(e, n) {
1444
1450
  return;
1445
1451
  }
1446
1452
  }
1447
- function St(e, n, r) {
1453
+ function vt(e, n, r) {
1448
1454
  var c;
1449
1455
  if (n === void 0) return e;
1450
1456
  const { as: t, empty: o, ...s } = e;
1451
1457
  let i = gn(e.value, n);
1452
- r != null && r.length && i !== "" && i !== null && (i = (c = r.find((f) => String(f.value) === String(e.value))) == null ? void 0 : c.value);
1458
+ r != null && r.length && i !== "" && i !== null && (i = (c = r.find((d) => String(d.value) === String(e.value))) == null ? void 0 : c.value);
1453
1459
  const u = o && n === "text", a = i === void 0 ? { type: "literal", value: "" } : { ...s, value: i, ...u ? { empty: o } : {} };
1454
1460
  return t === void 0 && !!o == !!u && i === e.value ? e : a;
1455
1461
  }
1456
- function or(e, n, r, t = !1) {
1462
+ function sr(e, n, r, t = !1) {
1457
1463
  const o = e.items.flatMap((i) => {
1458
1464
  if (i.type !== "literal") return [i];
1459
- const u = St(i, n, r);
1465
+ const u = vt(i, n, r);
1460
1466
  return u.value === "" && !u.empty && !t ? [] : [u];
1461
1467
  });
1462
1468
  return o.length === e.items.length && o.every((i, u) => i === e.items[u]) ? e : { ...e, items: o };
1463
1469
  }
1464
- function sr(e) {
1465
- const n = yn(e);
1470
+ function ir(e) {
1471
+ const n = hn(e);
1466
1472
  return n === "integer" || n === "number" ? "number" : n === "date" || n === "dateTime" ? "date" : n;
1467
1473
  }
1468
- function ir(e, n) {
1474
+ function ar(e, n) {
1469
1475
  const t = (e.type === "literal" ? gn(e.value, n) : void 0) ?? "", o = e.type === "literal" && e.empty && n === "text";
1470
1476
  return {
1471
1477
  type: "literal",
@@ -1474,31 +1480,31 @@ function ir(e, n) {
1474
1480
  ...o ? { empty: o } : {}
1475
1481
  };
1476
1482
  }
1477
- const fe = "IS NOT DISTINCT FROM";
1478
- function ar(e, n = !1) {
1483
+ const me = "IS NOT DISTINCT FROM";
1484
+ function ur(e, n = !1) {
1479
1485
  if (e !== void 0)
1480
1486
  return e === "text" ? [
1481
1487
  "=",
1482
1488
  "!=",
1483
- fe,
1489
+ me,
1484
1490
  "LIKE",
1485
1491
  "REGEXP",
1486
1492
  "IN",
1487
1493
  ...n ? ["BETWEEN"] : [],
1488
1494
  "IS NULL",
1489
1495
  "IS NOT NULL"
1490
- ] : e === "boolean" ? ["=", "!=", fe, "IS NULL", "IS NOT NULL"] : ["=", "!=", fe, "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
1496
+ ] : e === "boolean" ? ["=", "!=", me, "IS NULL", "IS NOT NULL"] : ["=", "!=", me, "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
1491
1497
  }
1492
1498
  const wn = "source", Sn = "rows_", B = (e) => e ? { type: "literal", value: null, as: e } : { type: "literal", value: null }, F = (e) => e.type === "literal" && e.value === null, $e = (e) => new Map(
1493
- S(e).map((n) => [T(n), n.table])
1499
+ S(e).map((n) => [v(n), n.table])
1494
1500
  );
1495
- function we(e, n) {
1501
+ function Se(e, n) {
1496
1502
  const r = $e(e);
1497
1503
  return I(n).map((t) => r.get(t) ?? t);
1498
1504
  }
1499
- function Tn(e, n) {
1505
+ function vn(e, n) {
1500
1506
  const r = $e(e);
1501
- return ve(n, (t) => {
1507
+ return Ne(n, (t) => {
1502
1508
  if (t.type !== "field" || t.table === void 0) return t;
1503
1509
  const o = r.get(t.table);
1504
1510
  return o === void 0 || o === t.table ? t : { ...t, table: o };
@@ -1517,16 +1523,16 @@ function xe(e, n) {
1517
1523
  for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
1518
1524
  return r;
1519
1525
  }
1520
- const ee = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], vn = (e) => {
1526
+ const ee = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], Tn = (e) => {
1521
1527
  if (e.length !== 0)
1522
1528
  return e.length === 1 ? e[0] : { type: "logical", operator: "AND", operands: e };
1523
1529
  }, k = (e) => e.from.table, Qe = (e, n) => A(e, n).label;
1524
- function ae(e) {
1530
+ function ue(e) {
1525
1531
  const { from: n } = e;
1526
1532
  return n.query && C(n.query) ? n : void 0;
1527
1533
  }
1528
1534
  function V(e, n) {
1529
- if (n === void 0) return ae(e);
1535
+ if (n === void 0) return ue(e);
1530
1536
  const r = O(e).find((t) => t.id === n);
1531
1537
  return r && C(r.query) ? r : void 0;
1532
1538
  }
@@ -1534,20 +1540,20 @@ function W(e, n, r) {
1534
1540
  const t = { ...n, query: r };
1535
1541
  return n === e.from ? { ...e, from: t } : { ...e, joins: e.joins.map((o) => o === n ? t : o) };
1536
1542
  }
1537
- function ue(e, n) {
1543
+ function le(e, n) {
1538
1544
  const [r] = n;
1539
1545
  return r.select.map((t, o) => {
1540
- var f;
1541
- const s = x(t) ?? "", i = n.map((d) => {
1546
+ var d;
1547
+ const s = x(t) ?? "", i = n.map((f) => {
1542
1548
  var m;
1543
- return { branch: d, expression: (m = d.select[o]) == null ? void 0 : m.expression };
1544
- }).filter((d) => d.expression !== void 0 && !F(d.expression)), a = ((f = n.map((d) => {
1549
+ return { branch: f, expression: (m = f.select[o]) == null ? void 0 : m.expression };
1550
+ }).filter((f) => f.expression !== void 0 && !F(f.expression)), a = ((d = n.map((f) => {
1545
1551
  var m;
1546
- return (m = d.select[o]) == null ? void 0 : m.expression;
1552
+ return (m = f.select[o]) == null ? void 0 : m.expression;
1547
1553
  }).filter(
1548
- (d) => (d == null ? void 0 : d.type) === "literal" && d.value === null
1549
- ).find((d) => d.as)) == null ? void 0 : f.as) ?? (i[0] && Z(e, i[0].branch, i[0].expression));
1550
- if (s === wn && i.length === n.length && i.every((d) => d.expression.type === "literal"))
1554
+ (f) => (f == null ? void 0 : f.type) === "literal" && f.value === null
1555
+ ).find((f) => f.as)) == null ? void 0 : d.as) ?? (i[0] && Z(e, i[0].branch, i[0].expression));
1556
+ if (s === wn && i.length === n.length && i.every((f) => f.expression.type === "literal"))
1551
1557
  return {
1552
1558
  name: s,
1553
1559
  kind: "source",
@@ -1569,8 +1575,8 @@ function ue(e, n) {
1569
1575
  tables: [],
1570
1576
  type: a
1571
1577
  };
1572
- const c = we(l.branch, l.expression);
1573
- return i.some((d) => we(d.branch, d.expression).includes(k(d.branch))) ? {
1578
+ const c = Se(l.branch, l.expression);
1579
+ return i.some((f) => Se(f.branch, f.expression).includes(k(f.branch))) ? {
1574
1580
  name: s,
1575
1581
  kind: "own",
1576
1582
  tables: c,
@@ -1580,15 +1586,15 @@ function ue(e, n) {
1580
1586
  kind: "shared",
1581
1587
  tables: c,
1582
1588
  type: a,
1583
- expression: Tn(l.branch, l.expression)
1589
+ expression: vn(l.branch, l.expression)
1584
1590
  };
1585
1591
  });
1586
1592
  }
1587
- function ur(e, n) {
1588
- const r = ae(n);
1593
+ function lr(e, n) {
1594
+ const r = ue(n);
1589
1595
  if (!r) return [];
1590
1596
  const { union: t } = r.query;
1591
- return ue(e, t).map((o, s) => {
1597
+ return le(e, t).map((o, s) => {
1592
1598
  const i = t.map((u) => !F(u.select[s].expression));
1593
1599
  return {
1594
1600
  name: o.name,
@@ -1599,79 +1605,79 @@ function ur(e, n) {
1599
1605
  });
1600
1606
  }
1601
1607
  function Nn(e, n, r, t, o) {
1602
- const s = J(e, n), i = r.filter((c) => c.kind === "shared" && c.expression), u = (c) => c.tables.every((f) => s.has(f));
1608
+ const s = P(e, n), i = r.filter((c) => c.kind === "shared" && c.expression), u = (c) => c.tables.every((d) => s.has(d));
1603
1609
  if (!i.some(u)) return `"${n}" shares no column with the union`;
1604
1610
  const a = i.find((c) => !u(c));
1605
1611
  if (a && o === "refuse")
1606
1612
  return `"${n}" can't reach column "${a.name}" (${a.tables.join(", ")})`;
1607
- const l = t.find((c) => !c.tables.every((f) => s.has(f)));
1613
+ const l = t.find((c) => !c.tables.every((d) => s.has(d)));
1608
1614
  if (l)
1609
1615
  return `"${n}" can't follow a condition on ${l.tables.join(", ")}`;
1610
1616
  }
1611
1617
  function En(e) {
1612
1618
  const [n] = e, r = new Set(e.map(k));
1613
1619
  return ee(n.where).flatMap((t) => {
1614
- const o = we(n, t), s = o.filter((i) => r.has(i));
1615
- if (s.length === 0) return [{ operand: Tn(n, t), tables: o }];
1620
+ const o = Se(n, t), s = o.filter((i) => r.has(i));
1621
+ if (s.length === 0) return [{ operand: vn(n, t), tables: o }];
1616
1622
  if (s.length === o.length) return [];
1617
1623
  throw new Error("a condition naming both a branch's own table and shared ones can't be placed: split it, or put it on the union instead");
1618
1624
  });
1619
1625
  }
1620
- function Pe(e, n, r, t) {
1621
- const o = t.unshared ?? "refuse", s = ue(e, n), i = En(n);
1626
+ function _e(e, n, r, t) {
1627
+ const o = t.unshared ?? "refuse", s = le(e, n), i = En(n);
1622
1628
  if (!A(e, r.table))
1623
1629
  throw new Error(`addUnionTable: unknown table "${r.table}"`);
1624
- if (n.some((y) => k(y) === r.table))
1630
+ if (n.some((h) => k(h) === r.table))
1625
1631
  throw new Error(`addUnionTable: "${r.table}" is in the union already`);
1626
1632
  const u = Nn(e, r.table, s, i, o);
1627
1633
  if (u) throw new Error(`addUnionTable: ${u}`);
1628
- const a = J(e, r.table), l = r.label ?? Qe(e, r.table), c = s.map((y) => y.kind === "source" ? { type: "literal", value: l } : y.kind === "shared" && y.expression && y.tables.every((N) => a.has(N)) ? y.expression : B(y.type)), f = new Set(s.map((y) => y.name)), d = [], m = (y) => {
1629
- const N = xe(y, f);
1630
- return f.add(N), d.push(N), N;
1634
+ const a = P(e, r.table), l = r.label ?? Qe(e, r.table), c = s.map((h) => h.kind === "source" ? { type: "literal", value: l } : h.kind === "shared" && h.expression && h.tables.every((N) => a.has(N)) ? h.expression : B(h.type)), d = new Set(s.map((h) => h.name)), f = [], m = (h) => {
1635
+ const N = xe(h, d);
1636
+ return d.add(N), f.push(N), N;
1631
1637
  }, g = [], p = {
1632
1638
  from: { table: r.table },
1633
1639
  joins: []
1634
1640
  };
1635
- r.columns.forEach((y) => {
1636
- const N = y.alias ?? (y.expression.type === "field" ? y.expression.column : void 0) ?? "value", Dn = Z(e, p, y.expression);
1637
- g.push({ name: m(N), own: y.expression, others: () => B(Dn) });
1638
- }), t.source && !s.some((y) => y.kind === "source") && g.push({
1641
+ r.columns.forEach((h) => {
1642
+ const N = h.alias ?? (h.expression.type === "field" ? h.expression.column : void 0) ?? "value", Dn = Z(e, p, h.expression);
1643
+ g.push({ name: m(N), own: h.expression, others: () => B(Dn) });
1644
+ }), t.source && !s.some((h) => h.kind === "source") && g.push({
1639
1645
  name: m(wn),
1640
1646
  own: { type: "literal", value: l },
1641
- others: (y) => ({ type: "literal", value: Qe(e, k(y)) })
1647
+ others: (h) => ({ type: "literal", value: Qe(e, k(h)) })
1642
1648
  });
1643
- const b = (y) => `${Sn}${y}`;
1644
- (t.rowMarkers || s.some((y) => y.kind === "rows")) && (n.filter((y) => !f.has(b(k(y)))).forEach((y) => {
1649
+ const b = (h) => `${Sn}${h}`;
1650
+ (t.rowMarkers || s.some((h) => h.kind === "rows")) && (n.filter((h) => !d.has(b(k(h)))).forEach((h) => {
1645
1651
  g.push({
1646
- name: m(b(k(y))),
1652
+ name: m(b(k(h))),
1647
1653
  own: B("integer"),
1648
- others: (N) => N === y ? { type: "literal", value: 1 } : B("integer")
1654
+ others: (N) => N === h ? { type: "literal", value: 1 } : B("integer")
1649
1655
  });
1650
1656
  }), g.push({
1651
1657
  name: m(b(r.table)),
1652
1658
  own: { type: "literal", value: 1 },
1653
1659
  others: () => B("integer")
1654
1660
  }));
1655
- const h = (y, N) => ({
1661
+ const y = (h, N) => ({
1656
1662
  id: E(),
1657
- alias: y,
1663
+ alias: h,
1658
1664
  expression: N
1659
- }), w = n.map((y) => ({
1660
- ...y,
1661
- select: [...y.select, ...g.map((N) => h(N.name, N.others(y)))]
1662
- })), v = vn([...i.map((y) => y.operand), ...ee(r.where)]);
1665
+ }), w = n.map((h) => ({
1666
+ ...h,
1667
+ select: [...h.select, ...g.map((N) => y(N.name, N.others(h)))]
1668
+ })), T = Tn([...i.map((h) => h.operand), ...ee(r.where)]);
1663
1669
  let $ = {
1664
1670
  id: "",
1665
1671
  from: { table: r.table },
1666
1672
  joins: [],
1667
- ...v ? { where: v } : {},
1673
+ ...T ? { where: T } : {},
1668
1674
  select: [
1669
- ...s.map((y, N) => h(y.name, c[N])),
1670
- ...g.map((y) => h(y.name, y.own))
1675
+ ...s.map((h, N) => y(h.name, c[N])),
1676
+ ...g.map((h) => y(h.name, h.own))
1671
1677
  ]
1672
1678
  };
1673
- [v, ...$.select.map((y) => y.expression)].forEach((y) => {
1674
- $ = L(e, $, y);
1679
+ [T, ...$.select.map((h) => h.expression)].forEach((h) => {
1680
+ $ = L(e, $, h);
1675
1681
  });
1676
1682
  const {
1677
1683
  from: Mn,
@@ -1684,7 +1690,7 @@ function Pe(e, n, r, t) {
1684
1690
  select: Un,
1685
1691
  ...Be ? { where: Be } : {}
1686
1692
  };
1687
- return { union: [...w, Ln], added: d };
1693
+ return { union: [...w, Ln], added: f };
1688
1694
  }
1689
1695
  function Cn(e) {
1690
1696
  const n = (o) => new Error(`addUnionTable: ${o}`);
@@ -1703,19 +1709,19 @@ function Cn(e) {
1703
1709
  select: t
1704
1710
  };
1705
1711
  }
1706
- function lr(e, n, r, t = {}) {
1707
- const o = ae(n);
1712
+ function cr(e, n, r, t = {}) {
1713
+ const o = ue(n);
1708
1714
  if (o) {
1709
- const { union: l, added: c } = Pe(e, o.query.union, r, t), f = { ...o, query: { ...o.query, union: l } }, d = n.select.length === 0 ? [] : [
1715
+ const { union: l, added: c } = _e(e, o.query.union, r, t), d = { ...o, query: { ...o.query, union: l } }, f = n.select.length === 0 ? [] : [
1710
1716
  ...n.select,
1711
1717
  ...c.map((m) => ({
1712
1718
  id: E(),
1713
1719
  expression: { type: "field", table: o.alias, column: m }
1714
1720
  }))
1715
1721
  ];
1716
- return { ...n, from: f, select: d };
1722
+ return { ...n, from: d, select: f };
1717
1723
  }
1718
- const s = Cn(n), { union: i } = Pe(e, [s], r, t), u = t.alias ?? "stacked", a = i[0].select.map((l) => l.alias);
1724
+ const s = Cn(n), { union: i } = _e(e, [s], r, t), u = t.alias ?? "stacked", a = i[0].select.map((l) => l.alias);
1719
1725
  return {
1720
1726
  id: n.id,
1721
1727
  from: { id: E(), alias: u, query: { union: i, all: !0 } },
@@ -1728,15 +1734,15 @@ function lr(e, n, r, t = {}) {
1728
1734
  ...n.offset !== void 0 ? { offset: n.offset } : {}
1729
1735
  };
1730
1736
  }
1731
- function cr(e, n, r = {}) {
1732
- const t = ae(n);
1737
+ function dr(e, n, r = {}) {
1738
+ const t = ue(n);
1733
1739
  let o;
1734
1740
  try {
1735
1741
  o = t ? t.query.union : [Cn(n)];
1736
1742
  } catch {
1737
1743
  return [];
1738
1744
  }
1739
- const s = ue(e, o);
1745
+ const s = le(e, o);
1740
1746
  let i;
1741
1747
  try {
1742
1748
  i = En(o);
@@ -1754,32 +1760,32 @@ function Tt(e, n, r) {
1754
1760
  if (t === -1) return r;
1755
1761
  const o = r.union[t], s = n.union[t].select.map((a) => a.id), i = r.union.map((a, l) => {
1756
1762
  if (l === t) return a;
1757
- const c = o.select.map((d) => {
1758
- const m = x(d), g = Z(e, o, d.expression), p = s.indexOf(d.id), b = p === -1 ? void 0 : a.select[p];
1763
+ const c = o.select.map((f) => {
1764
+ const m = x(f), g = Z(e, o, f.expression), p = s.indexOf(f.id), b = p === -1 ? void 0 : a.select[p];
1759
1765
  if (!b)
1760
1766
  return { id: E(), ...m ? { alias: m } : {}, expression: B(g) };
1761
- let h = b;
1762
- x(b) !== m && (h = { ...h, alias: m });
1767
+ let y = b;
1768
+ x(b) !== m && (y = { ...y, alias: m });
1763
1769
  const { expression: w } = b;
1764
- return F(w) && w.type === "literal" && w.as !== g && (h = { ...h, expression: B(g) }), h;
1770
+ return F(w) && w.type === "literal" && w.as !== g && (y = { ...y, expression: B(g) }), y;
1765
1771
  });
1766
- return c.length === a.select.length && c.every((d, m) => d === a.select[m]) ? a : { ...a, select: c };
1772
+ return c.length === a.select.length && c.every((f, m) => f === a.select[m]) ? a : { ...a, select: c };
1767
1773
  }), u = i.map((a) => {
1768
- const l = a.select.map((c, f) => {
1769
- const { expression: d } = c;
1770
- if (d.type !== "literal" || d.value !== null) return c;
1774
+ const l = a.select.map((c, d) => {
1775
+ const { expression: f } = c;
1776
+ if (f.type !== "literal" || f.value !== null) return c;
1771
1777
  const m = i.find((p) => {
1772
- var h;
1773
- const b = (h = p.select[f]) == null ? void 0 : h.expression;
1778
+ var y;
1779
+ const b = (y = p.select[d]) == null ? void 0 : y.expression;
1774
1780
  return b !== void 0 && !F(b);
1775
- }), g = m && Z(e, m, m.select[f].expression);
1776
- return g && d.as !== g ? { ...c, expression: B(g) } : c;
1781
+ }), g = m && Z(e, m, m.select[d].expression);
1782
+ return g && f.as !== g ? { ...c, expression: B(g) } : c;
1777
1783
  });
1778
- return l.every((c, f) => c === a.select[f]) ? a : { ...a, select: l };
1784
+ return l.every((c, d) => c === a.select[d]) ? a : { ...a, select: l };
1779
1785
  });
1780
1786
  return u.every((a, l) => a === r.union[l]) ? r : { ...r, union: u };
1781
1787
  }
1782
- function vt(e, n) {
1788
+ function Nt(e, n) {
1783
1789
  const r = (s) => s.union[0].select.map((i) => x(i)), t = r(e), o = r(n);
1784
1790
  return t.length !== o.length ? [] : t.flatMap((s, i) => {
1785
1791
  const u = o[i];
@@ -1797,35 +1803,35 @@ function $n(e, n, r) {
1797
1803
  ));
1798
1804
  }
1799
1805
  function Ae(e, n, r) {
1800
- const t = (d) => {
1806
+ const t = (f) => {
1801
1807
  var m;
1802
1808
  return $n(
1803
1809
  e,
1804
- n && ((m = O(n).find((g) => g.id === d.id)) == null ? void 0 : m.query),
1805
- d.query
1810
+ n && ((m = O(n).find((g) => g.id === f.id)) == null ? void 0 : m.query),
1811
+ f.query
1806
1812
  );
1807
1813
  }, { from: o } = r, s = o.query && t(o), i = s && s !== o.query ? { ...o, query: s } : o;
1808
1814
  let u = !1;
1809
- const a = r.joins.map((d) => {
1810
- if (!d.query) return d;
1811
- const m = t(d);
1812
- return m === d.query ? d : (u = !0, { ...d, query: m });
1815
+ const a = r.joins.map((f) => {
1816
+ if (!f.query) return f;
1817
+ const m = t(f);
1818
+ return m === f.query ? f : (u = !0, { ...f, query: m });
1813
1819
  });
1814
1820
  if (i === o && !u) return r;
1815
- const l = { ...r, from: i, joins: a }, c = O(l).flatMap((d) => {
1821
+ const l = { ...r, from: i, joins: a }, c = O(l).flatMap((f) => {
1816
1822
  var g;
1817
- const m = n && ((g = O(n).find((p) => p.id === d.id)) == null ? void 0 : g.query);
1818
- return m && C(m) && C(d.query) ? vt(m, d.query).map((p) => ({ ...p, alias: d.alias })) : [];
1823
+ const m = n && ((g = O(n).find((p) => p.id === f.id)) == null ? void 0 : g.query);
1824
+ return m && C(m) && C(f.query) ? Nt(m, f.query).map((p) => ({ ...p, alias: f.alias })) : [];
1819
1825
  });
1820
1826
  if (c.length === 0) return l;
1821
- const f = (d) => {
1822
- if (d.type !== "field") return P(d, f);
1823
- const m = c.find((g) => g.alias === d.table && g.from === d.column);
1824
- return m ? { ...d, column: m.to } : d;
1827
+ const d = (f) => {
1828
+ if (f.type !== "field") return _(f, d);
1829
+ const m = c.find((g) => g.alias === f.table && g.from === f.column);
1830
+ return m ? { ...f, column: m.to } : f;
1825
1831
  };
1826
- return te(l, f, (d) => d);
1832
+ return te(l, d, (f) => f);
1827
1833
  }
1828
- function Nt(e, n, r) {
1834
+ function Et(e, n, r) {
1829
1835
  if (!U(r)) return r;
1830
1836
  const t = n && U(n) ? n : void 0;
1831
1837
  return Te(Ae(e, t, r), (o, s) => {
@@ -1841,7 +1847,7 @@ function xn(e) {
1841
1847
  return e.type === "field" ? [e] : oe(e).flatMap(xn);
1842
1848
  }
1843
1849
  const An = (e, n, r) => xn(e).some((t) => t.table === n && r.has(t.column ?? ""));
1844
- function Et(e, n, r) {
1850
+ function Ct(e, n, r) {
1845
1851
  const t = n.query.union.filter((a, l) => l !== r), o = [
1846
1852
  e.where,
1847
1853
  e.having,
@@ -1851,32 +1857,32 @@ function Et(e, n, r) {
1851
1857
  ].filter((a) => a !== void 0), s = (a) => o.some(
1852
1858
  (l) => An(l, n.alias, /* @__PURE__ */ new Set([a]))
1853
1859
  ), i = new Set(t[0].select.flatMap((a, l) => {
1854
- const c = t.every((d) => d.select[l] && F(d.select[l].expression)), f = x(a);
1855
- return c && !(f && s(f)) ? [l] : [];
1860
+ const c = t.every((f) => f.select[l] && F(f.select[l].expression)), d = x(a);
1861
+ return c && !(d && s(d)) ? [l] : [];
1856
1862
  })), u = new Set([...i].flatMap((a) => {
1857
1863
  const l = x(t[0].select[a]);
1858
1864
  return l ? [l] : [];
1859
1865
  }));
1860
1866
  return { at: i, names: u };
1861
1867
  }
1862
- function dr(e, n, r) {
1868
+ function fr(e, n, r) {
1863
1869
  const t = V(e, r);
1864
1870
  if (!t || !t.query.union[n] || t.query.union.length < 2) return e;
1865
- const o = Et(e, t, n), s = t.query.union.filter((l, c) => c !== n), i = o.at.size < s[0].select.length ? o : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, u = e.select.filter(
1871
+ const o = Ct(e, t, n), s = t.query.union.filter((l, c) => c !== n), i = o.at.size < s[0].select.length ? o : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, u = e.select.filter(
1866
1872
  (l) => !An(l.expression, t.alias, i.names)
1867
- ), a = s.map((l) => ({ ...l, select: l.select.filter((c, f) => !i.at.has(f)) }));
1873
+ ), a = s.map((l) => ({ ...l, select: l.select.filter((c, d) => !i.at.has(d)) }));
1868
1874
  return W({ ...e, select: u }, t, { ...t.query, union: a });
1869
1875
  }
1870
- function Ct(e, n) {
1876
+ function $t(e, n) {
1871
1877
  const r = /* @__PURE__ */ new Set([
1872
- ...n ? S(n).map(T) : [],
1878
+ ...n ? S(n).map(v) : [],
1873
1879
  ...e.tables.map((o) => o.name)
1874
1880
  ]);
1875
1881
  let t = 1;
1876
1882
  for (; r.has(`union_${t}`); ) t += 1;
1877
1883
  return `union_${t}`;
1878
1884
  }
1879
- function fr(e, n, r, t) {
1885
+ function mr(e, n, r, t) {
1880
1886
  return {
1881
1887
  id: e,
1882
1888
  from: { id: n, alias: r, query: { union: [{ from: t, joins: [], select: [] }], all: !0 } },
@@ -1884,10 +1890,10 @@ function fr(e, n, r, t) {
1884
1890
  select: []
1885
1891
  };
1886
1892
  }
1887
- function mr(e, n, r) {
1893
+ function pr(e, n, r) {
1888
1894
  const t = /* @__PURE__ */ new Set(), o = e.select.map((p, b) => {
1889
- const h = xe(x(p) ?? `column_${b + 1}`, t);
1890
- return t.add(h), { ...p, alias: h };
1895
+ const y = xe(x(p) ?? `column_${b + 1}`, t);
1896
+ return t.add(y), { ...p, alias: y };
1891
1897
  }), {
1892
1898
  from: s,
1893
1899
  joins: i,
@@ -1895,7 +1901,7 @@ function mr(e, n, r) {
1895
1901
  groupBy: a,
1896
1902
  having: l,
1897
1903
  distinct: c
1898
- } = e, f = {
1904
+ } = e, d = {
1899
1905
  from: s,
1900
1906
  joins: i,
1901
1907
  select: o,
@@ -1903,27 +1909,27 @@ function mr(e, n, r) {
1903
1909
  ...a ? { groupBy: a } : {},
1904
1910
  ...l ? { having: l } : {},
1905
1911
  ...c !== void 0 ? { distinct: c } : {}
1906
- }, d = (p) => ({ type: "field", table: r, column: p }), m = (p) => {
1912
+ }, f = (p) => ({ type: "field", table: r, column: p }), m = (p) => {
1907
1913
  var b;
1908
- return (b = o.find((h) => M(h.expression, p))) == null ? void 0 : b.alias;
1914
+ return (b = o.find((y) => M(y.expression, p))) == null ? void 0 : b.alias;
1909
1915
  }, g = {
1910
1916
  ...e,
1911
- from: { id: n, alias: r, query: { union: [f], all: !0 } },
1917
+ from: { id: n, alias: r, query: { union: [d], all: !0 } },
1912
1918
  joins: [],
1913
- select: o.map((p) => ({ id: E(), expression: d(p.alias) })),
1919
+ select: o.map((p) => ({ id: E(), expression: f(p.alias) })),
1914
1920
  ...e.orderBy ? {
1915
1921
  orderBy: e.orderBy.map((p) => {
1916
1922
  const b = m(p.expression);
1917
- return b ? { ...p, expression: d(b) } : p;
1923
+ return b ? { ...p, expression: f(b) } : p;
1918
1924
  })
1919
1925
  } : {}
1920
1926
  };
1921
1927
  return ["where", "groupBy", "having", "distinct"].forEach((p) => delete g[p]), g;
1922
1928
  }
1923
- function pr(e, n, r, t) {
1929
+ function yr(e, n, r, t) {
1924
1930
  const o = V(n, r);
1925
1931
  if (!o) return n;
1926
- const s = ue(e, o.query.union).map((i) => ({
1932
+ const s = le(e, o.query.union).map((i) => ({
1927
1933
  id: E(),
1928
1934
  ...i.name ? { alias: i.name } : {},
1929
1935
  expression: B(i.type)
@@ -1936,66 +1942,66 @@ function pr(e, n, r, t) {
1936
1942
  function hr(e, n, r, t) {
1937
1943
  const o = {
1938
1944
  id: r,
1939
- alias: Ct(e, n),
1945
+ alias: $t(e, n),
1940
1946
  joinType: "left",
1941
1947
  query: { union: [{ from: t, joins: [], select: [] }], all: !0 }
1942
1948
  };
1943
- return { ...n, joins: [...n.joins, { ...o, on: rn(e, n, o) }] };
1949
+ return { ...n, joins: [...n.joins, { ...o, on: tn(e, n, o) }] };
1944
1950
  }
1945
- function yr(e, n, r) {
1951
+ function br(e, n, r) {
1946
1952
  const t = V(e, n);
1947
1953
  return !t || t.query.all === r ? e : W(e, t, { ...t.query, all: r });
1948
1954
  }
1949
- function br(e, n, r, t, o) {
1955
+ function gr(e, n, r, t, o) {
1950
1956
  const s = V(n, r), [i, ...u] = (s == null ? void 0 : s.query.union) ?? [], a = i == null ? void 0 : i.select[t], l = o.trim();
1951
- if (!s || !a || !l || l.length > D || i.select.some((f) => x(f) === l)) return n;
1957
+ if (!s || !a || !l || l.length > D || i.select.some((d) => x(d) === l)) return n;
1952
1958
  const c = W(n, s, {
1953
1959
  ...s.query,
1954
1960
  union: [
1955
- { ...i, select: i.select.map((f, d) => d === t ? { ...f, alias: l } : f) },
1961
+ { ...i, select: i.select.map((d, f) => f === t ? { ...d, alias: l } : d) },
1956
1962
  ...u
1957
1963
  ]
1958
1964
  });
1959
1965
  return Ae(e, n, c);
1960
1966
  }
1961
- function gr(e, n) {
1967
+ function wr(e, n) {
1962
1968
  var c;
1963
1969
  const r = V(e, n);
1964
1970
  if (!r || r.query.union.length !== 1) return e;
1965
- const [t] = r.query.union, o = new Map(t.select.flatMap((f) => {
1966
- const d = x(f);
1967
- return d ? [[d, f.expression]] : [];
1968
- })), s = (f) => f.type === "field" && f.table === r.alias && o.has(f.column ?? ""), i = (f) => ve(f, (d) => s(d) ? o.get(d.column) : d), u = e.select.map((f) => {
1969
- const d = i(f.expression);
1970
- if (f.alias || !s(f.expression)) return { ...f, expression: d };
1971
- const m = f.expression.column;
1972
- return x({ ...f, expression: d }) === m ? { ...f, expression: d } : { ...f, expression: d, alias: m };
1973
- }), a = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((f) => ne(f.expression, Q));
1971
+ const [t] = r.query.union, o = new Map(t.select.flatMap((d) => {
1972
+ const f = x(d);
1973
+ return f ? [[f, d.expression]] : [];
1974
+ })), s = (d) => d.type === "field" && d.table === r.alias && o.has(d.column ?? ""), i = (d) => Ne(d, (f) => s(f) ? o.get(f.column) : f), u = e.select.map((d) => {
1975
+ const f = i(d.expression);
1976
+ if (d.alias || !s(d.expression)) return { ...d, expression: f };
1977
+ const m = d.expression.column;
1978
+ return x({ ...d, expression: f }) === m ? { ...d, expression: f } : { ...d, expression: f, alias: m };
1979
+ }), a = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((d) => ne(d.expression, Q));
1974
1980
  if (r === e.from && !a) {
1975
- const f = te({ ...e, select: u }, i, (d) => d);
1981
+ const d = te({ ...e, select: u }, i, (f) => f);
1976
1982
  return {
1977
- ...f,
1983
+ ...d,
1978
1984
  from: t.from,
1979
- joins: [...t.joins, ...f.joins],
1980
- where: vn([...ee(t.where), ...ee(f.where)]),
1985
+ joins: [...t.joins, ...d.joins],
1986
+ where: Tn([...ee(t.where), ...ee(d.where)]),
1981
1987
  // Nothing picked: the query's own columns, as the union had them.
1982
- select: f.select.length > 0 ? f.select : t.select
1988
+ select: d.select.length > 0 ? d.select : t.select
1983
1989
  };
1984
1990
  }
1985
- const l = e.joins.length === 0 && !e.where && !((c = e.groupBy) != null && c.length) && !e.having && !e.distinct && [...e.select, ...e.orderBy ?? []].every((f) => s(f.expression));
1991
+ const l = e.joins.length === 0 && !e.where && !((c = e.groupBy) != null && c.length) && !e.having && !e.distinct && [...e.select, ...e.orderBy ?? []].every((d) => s(d.expression));
1986
1992
  return r === e.from && l ? {
1987
1993
  ...e,
1988
1994
  ...t,
1989
1995
  select: u.length > 0 ? u : t.select,
1990
1996
  ...e.orderBy ? {
1991
- orderBy: e.orderBy.map((f) => ({ ...f, expression: i(f.expression) }))
1997
+ orderBy: e.orderBy.map((d) => ({ ...d, expression: i(d.expression) }))
1992
1998
  } : {}
1993
1999
  } : W(e, r, t);
1994
2000
  }
1995
- function $t(e, n) {
2001
+ function xt(e, n) {
1996
2002
  return e.select.filter((r) => ne(r.expression, n));
1997
2003
  }
1998
- function xt(e) {
2004
+ function At(e) {
1999
2005
  return { ...e.expression, fromSelect: e.id };
2000
2006
  }
2001
2007
  function K(e) {
@@ -2003,14 +2009,14 @@ function K(e) {
2003
2009
  return n === void 0 ? e : r;
2004
2010
  }
2005
2011
  function Bn(e, n) {
2006
- const r = (o) => typeof o.fromSelect == "string" ? n(o, o.fromSelect) : P(o, r), t = r(e);
2012
+ const r = (o) => typeof o.fromSelect == "string" ? n(o, o.fromSelect) : _(o, r), t = r(e);
2007
2013
  return M(t, e) ? e : t;
2008
2014
  }
2009
- function At(e, n) {
2015
+ function Bt(e, n) {
2010
2016
  var u;
2011
- const r = new Map($t(e, n).map((a) => [a.id, a])), t = (a) => Bn(a, (l, c) => {
2012
- const f = r.get(c);
2013
- return f ? xt(f) : K(l);
2017
+ const r = new Map(xt(e, n).map((a) => [a.id, a])), t = (a) => Bn(a, (l, c) => {
2018
+ const d = r.get(c);
2019
+ return d ? At(d) : K(l);
2014
2020
  }), o = e.having && t(e.having);
2015
2021
  let s = !1;
2016
2022
  const i = (u = e.orderBy) == null ? void 0 : u.map((a) => {
@@ -2019,33 +2025,33 @@ function At(e, n) {
2019
2025
  });
2020
2026
  return o === e.having && !s ? e : { ...e, having: o, ...i ? { orderBy: i } : {} };
2021
2027
  }
2022
- function wr(e, n) {
2028
+ function Sr(e, n) {
2023
2029
  const r = new Set((n.groupBy ?? []).flatMap(({ expression: t }) => t.type === "field" && t.column ? [t.column.toLowerCase()] : []));
2024
2030
  return Bn(e, (t, o) => {
2025
2031
  const s = n.select.find((i) => i.id === o);
2026
2032
  return !(s != null && s.alias) || r.has(s.alias.toLowerCase()) || !M(K(t), s.expression) ? K(t) : { type: "field", column: s.alias };
2027
2033
  });
2028
2034
  }
2029
- function Se(e, n) {
2030
- const r = (c) => Se(c, n), t = (c) => se(c, r), o = e.from.query && t(e.from.query), s = o && o !== e.from.query ? { ...e.from, query: o } : e.from;
2035
+ function ve(e, n) {
2036
+ const r = (c) => ve(c, n), t = (c) => se(c, r), o = e.from.query && t(e.from.query), s = o && o !== e.from.query ? { ...e.from, query: o } : e.from;
2031
2037
  let i = !1;
2032
2038
  const u = e.joins.map((c) => {
2033
2039
  if (!c.query) return c;
2034
- const f = t(c.query);
2035
- return f === c.query ? c : (i = !0, { ...c, query: f });
2036
- }), a = s === e.from && !i ? e : { ...e, from: s, joins: u }, l = Ke(a, (c) => {
2037
- const f = r(c.query);
2038
- return f === c.query ? c : { ...c, query: f };
2040
+ const d = t(c.query);
2041
+ return d === c.query ? c : (i = !0, { ...c, query: d });
2042
+ }), a = s === e.from && !i ? e : { ...e, from: s, joins: u }, l = Xe(a, (c) => {
2043
+ const d = r(c.query);
2044
+ return d === c.query ? c : { ...c, query: d };
2039
2045
  });
2040
- return Qn(At(l, n), n);
2046
+ return _n(Bt(l, n), n);
2041
2047
  }
2042
- function Sr(e, n, r, t = Q) {
2048
+ function vr(e, n, r, t = Q) {
2043
2049
  if (!U(r)) return r;
2044
2050
  const o = Te(
2045
- Se(r, t),
2046
- (i) => se(i, (u) => Se(u, t))
2047
- ), s = Nt(e, n, o);
2048
- return mt(e, n, s);
2051
+ ve(r, t),
2052
+ (i) => se(i, (u) => ve(u, t))
2053
+ ), s = Et(e, n, o);
2054
+ return pt(e, n, s);
2049
2055
  }
2050
2056
  const Tr = [
2051
2057
  "=",
@@ -2061,44 +2067,44 @@ const Tr = [
2061
2067
  "REGEXP",
2062
2068
  "IS NULL",
2063
2069
  "IS NOT NULL"
2064
- ], Bt = ["IS NULL", "IS NOT NULL"];
2065
- function vr(e) {
2066
- return Bt.includes(e);
2070
+ ], It = ["IS NULL", "IS NOT NULL"];
2071
+ function Nr(e) {
2072
+ return It.includes(e);
2067
2073
  }
2068
2074
  const In = (e) => {
2069
2075
  const n = /* @__PURE__ */ new Map();
2070
- return We(e, (r) => (r.type === "param" && r.relative && (n.has(r.name) || n.set(r.name, /* @__PURE__ */ new Set()), n.get(r.name).add(JSON.stringify(r.relative))), r)), n;
2076
+ return Ve(e, (r) => (r.type === "param" && r.relative && (n.has(r.name) || n.set(r.name, /* @__PURE__ */ new Set()), n.get(r.name).add(JSON.stringify(r.relative))), r)), n;
2071
2077
  };
2072
2078
  function kn(e, n) {
2073
2079
  const r = /* @__PURE__ */ new Map(), t = (a) => (r.has(a) || r.set(a, { types: /* @__PURE__ */ new Set(), columns: /* @__PURE__ */ new Map() }), r.get(a)), o = (a, l) => {
2074
2080
  const c = new Map(a);
2075
- return S(l).forEach((f) => c.set(T(f), f.table)), c;
2081
+ return S(l).forEach((d) => c.set(v(d), d.table)), c;
2076
2082
  }, s = (a, l) => {
2077
2083
  if (!n || l.type !== "field" || !l.table || !l.column) return;
2078
- const c = a.has(l.table) ? a.get(l.table) : l.table, f = c === void 0 ? void 0 : A(n, c), d = f == null ? void 0 : f.columns.find((m) => m.name === l.column);
2079
- return f && d ? { table: f.name, column: d } : void 0;
2084
+ const c = a.has(l.table) ? a.get(l.table) : l.table, d = c === void 0 ? void 0 : A(n, c), f = d == null ? void 0 : d.columns.find((m) => m.name === l.column);
2085
+ return d && f ? { table: d.name, column: f } : void 0;
2080
2086
  }, i = (a, l) => {
2081
2087
  if (a.type === "param" && t(a.name), a.type === "binary" && a.right) {
2082
- const f = a.operator === "LIKE" || a.operator === "REGEXP", d = (g, p) => {
2088
+ const d = a.operator === "LIKE" || a.operator === "REGEXP", f = (g, p) => {
2083
2089
  const b = s(l, p);
2084
2090
  if (g.type !== "param" || !b) return;
2085
- const h = t(g.name);
2086
- if (h.types.add(b.column.type), !f) {
2091
+ const y = t(g.name);
2092
+ if (y.types.add(b.column.type), !d) {
2087
2093
  const w = `${b.table}.${b.column.name}`;
2088
- h.columns.set(w, { table: b.table, column: b.column.name });
2094
+ y.columns.set(w, { table: b.table, column: b.column.name });
2089
2095
  }
2090
2096
  };
2091
- d(a.left, a.right), (a.right.type === "list" ? a.right.items : [a.right]).forEach((g) => d(g, a.left));
2097
+ f(a.left, a.right), (a.right.type === "list" ? a.right.items : [a.right]).forEach((g) => f(g, a.left));
2092
2098
  }
2093
2099
  const c = a.type === "exists" ? o(l, a.query) : l;
2094
- oe(a).forEach((f) => i(f, c));
2100
+ oe(a).forEach((d) => i(d, c));
2095
2101
  }, u = (a) => {
2096
2102
  const l = o(/* @__PURE__ */ new Map(), a);
2097
- _e(a).forEach((c) => i(c, l)), S(a).forEach((c) => c.query && Y(c.query).forEach(u)), Xe(a).forEach((c) => u(c.query));
2103
+ Je(a).forEach((c) => i(c, l)), S(a).forEach((c) => c.query && Y(c.query).forEach(u)), He(a).forEach((c) => u(c.query));
2098
2104
  };
2099
2105
  return (e.shared ?? []).forEach((a) => Y(a.query).forEach(u)), u(e), r;
2100
2106
  }
2101
- function Nr(e, n) {
2107
+ function Er(e, n) {
2102
2108
  const r = In(e);
2103
2109
  return [...kn(e, n)].map(([t, { types: o, columns: s }]) => {
2104
2110
  const i = r.get(t);
@@ -2111,15 +2117,15 @@ function Nr(e, n) {
2111
2117
  };
2112
2118
  });
2113
2119
  }
2114
- function Er(e) {
2120
+ function Cr(e) {
2115
2121
  return [...In(e)].filter(([, n]) => n.size > 1).map(([n]) => n);
2116
2122
  }
2117
- function Cr(e, n) {
2123
+ function $r(e, n) {
2118
2124
  return [...kn(e, n)].filter(([, r]) => r.types.size > 1).map(([r]) => r);
2119
2125
  }
2120
- function $r(e, n) {
2126
+ function xr(e, n) {
2121
2127
  const r = new Map(Object.entries(n));
2122
- return We(e, (t) => {
2128
+ return Ve(e, (t) => {
2123
2129
  if (t.type !== "param") return t;
2124
2130
  if (!r.has(t.name)) throw new Error(`no value for parameter "${t.name}"`);
2125
2131
  const o = r.get(t.name);
@@ -2127,91 +2133,91 @@ function $r(e, n) {
2127
2133
  });
2128
2134
  }
2129
2135
  export {
2130
- Dt as $,
2131
- vr as A,
2132
- Tr as B,
2133
- jt as C,
2134
- Zn as D,
2136
+ jt as $,
2137
+ Tr as A,
2138
+ ke as B,
2139
+ qt as C,
2140
+ et as D,
2135
2141
  O as E,
2136
- Ze as F,
2137
- me as G,
2138
- Rt as H,
2142
+ ze as F,
2143
+ pe as G,
2144
+ Gt as H,
2139
2145
  K as I,
2140
- pe as J,
2141
- Cr as K,
2142
- Er as L,
2143
- Jn as M,
2146
+ ye as J,
2147
+ $r as K,
2148
+ Cr as L,
2149
+ Vn as M,
2144
2150
  an as N,
2145
- tr as O,
2146
- er as P,
2147
- Wn as Q,
2148
- yn as R,
2151
+ rr as O,
2152
+ nr as P,
2153
+ Hn as Q,
2154
+ hn as R,
2149
2155
  bn as S,
2150
2156
  ht as T,
2151
- zn as U,
2152
- Ut as V,
2153
- Ye as W,
2154
- Pt as X,
2155
- _t as Y,
2156
- Vt as Z,
2157
- Jt as _,
2158
- Mt as a,
2159
- sr as a0,
2160
- rr as a1,
2161
- ir as a2,
2162
- ar as a3,
2163
- or as a4,
2164
- St as a5,
2165
- xt as a6,
2166
- Vn as a7,
2167
- tn as a8,
2168
- Gt as a9,
2169
- Xt as aa,
2170
- Yt as ab,
2171
- Kt as ac,
2172
- en as ad,
2173
- Lt as ae,
2174
- Zt as af,
2175
- ut as ag,
2176
- Ft as ah,
2177
- at as ai,
2178
- st as aj,
2179
- ie as ak,
2180
- zt as al,
2157
+ Zn as U,
2158
+ Lt as V,
2159
+ Ke as W,
2160
+ Jt as X,
2161
+ Pt as Y,
2162
+ Wt as Z,
2163
+ Vt as _,
2164
+ Ot as a,
2165
+ ir as a0,
2166
+ or as a1,
2167
+ ar as a2,
2168
+ ur as a3,
2169
+ sr as a4,
2170
+ vt as a5,
2171
+ At as a6,
2172
+ Wn as a7,
2173
+ nn as a8,
2174
+ Ft as a9,
2175
+ Kt as aa,
2176
+ zt as ab,
2177
+ Yt as ac,
2178
+ Ze as ad,
2179
+ Dt as ae,
2180
+ er as af,
2181
+ lt as ag,
2182
+ Qt as ah,
2183
+ ut as ai,
2184
+ it as aj,
2185
+ ae as ak,
2186
+ Zt as al,
2181
2187
  Re as am,
2182
- rn as an,
2183
- $t as ao,
2184
- qt as ap,
2185
- Wt as aq,
2186
- Ht as ar,
2187
- Hn as as,
2188
- Xe as at,
2189
- ae as au,
2190
- J as av,
2188
+ tn as an,
2189
+ xt as ao,
2190
+ Rt as ap,
2191
+ Ht as aq,
2192
+ Xt as ar,
2193
+ Xn as as,
2194
+ He as at,
2195
+ ue as au,
2196
+ P as av,
2191
2197
  he as aw,
2192
- Ot as b,
2193
- Qt as c,
2194
- nr as d,
2195
- pr as e,
2198
+ Ut as b,
2199
+ _t as c,
2200
+ tr as d,
2201
+ yr as e,
2196
2202
  hr as f,
2197
- lr as g,
2198
- fr as h,
2199
- dr as i,
2200
- br as j,
2201
- mt as k,
2202
- Ct as l,
2203
+ cr as g,
2204
+ mr as h,
2205
+ gr as i,
2206
+ $t as j,
2207
+ pt as k,
2208
+ pr as l,
2203
2209
  dn as m,
2204
- mr as n,
2205
- cr as o,
2206
- ur as p,
2207
- Nr as q,
2208
- ke as r,
2209
- yr as s,
2210
- Sr as t,
2211
- gr as u,
2212
- I as v,
2213
- G as w,
2214
- gn as x,
2215
- $r as y,
2216
- wr as z
2210
+ dr as n,
2211
+ lr as o,
2212
+ vr as p,
2213
+ Er as q,
2214
+ fr as r,
2215
+ br as s,
2216
+ I as t,
2217
+ wr as u,
2218
+ G as v,
2219
+ gn as w,
2220
+ xr as x,
2221
+ Sr as y,
2222
+ Nr as z
2217
2223
  };