@nongt/visql 0.7.0 → 0.8.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,12 +1,12 @@
1
- import { D as $e, h as ue, q as Q, t as D, i as H, s as Xn, l as le, u as Kn, w as Pe, e as Ve, x as dn, k as z, r as fe, o as xe, j as K, y as Yn, d as W, z as Jt, A as Be, B as me, C as Pt, M as Y, F as zn, G as Vt, g as Z, H as We, I as fn, b as be, J as Zn, n as Wt, K as et, f as Ht, L as Xt, N as Mn, O as On, P as Ge, Q as Kt } from "./schema-DFC26AhZ.js";
2
- import { c as Yt, s as zt, b as nt } from "./calendarDate-CYlr9TvX.js";
3
- const Zt = 1e3, tt = 15, er = 40, ls = 100;
1
+ import { D as $e, h as ue, q as Q, t as D, i as H, s as Kn, l as le, u as Yn, w as Pe, e as Ve, x as fn, k as z, r as fe, o as xe, j as K, y as zn, d as W, z as Jt, A as Be, B as me, C as Pt, M as Y, F as Zn, G as Vt, g as Z, H as We, I as mn, b as be, J as et, n as Wt, K as nt, f as Ht, L as Xt, N as On, O as Un, P as Ge, Q as Kt } from "./schema-DFC26AhZ.js";
2
+ import { c as Yt, s as zt, b as tt } from "./calendarDate-CYlr9TvX.js";
3
+ const Zt = 1e3, rt = 15, er = 40, ls = 100;
4
4
  function nr(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 nn(e) {
9
- return Number.isFinite(e) && nr(String(e)) <= tt;
8
+ function tn(e) {
9
+ return Number.isFinite(e) && nr(String(e)) <= rt;
10
10
  }
11
11
  const us = [];
12
12
  function oe(e) {
@@ -16,18 +16,18 @@ function Te(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 rt(e) {
19
+ function st(e) {
20
20
  const n = [], r = (t) => {
21
21
  t.type === "subquery" ? n.push(t) : fe(t).forEach(r);
22
22
  };
23
- return Xn(e).forEach(r), n;
23
+ return Kn(e).forEach(r), n;
24
24
  }
25
- function st(e, n) {
25
+ function it(e, n) {
26
26
  let r = !1;
27
27
  const t = (i) => {
28
28
  if (i.type !== "subquery") return xe(i, t);
29
- const a = n(i);
30
- return a !== i && (r = !0), a;
29
+ const l = n(i);
30
+ return l !== i && (r = !0), l;
31
31
  }, s = Pe(e, t, (i) => i);
32
32
  return r ? s : e;
33
33
  }
@@ -40,28 +40,28 @@ const rr = (e) => ({
40
40
  alias: e.name,
41
41
  query: e.query
42
42
  });
43
- function Un(e, n) {
43
+ function jn(e, n) {
44
44
  var s;
45
45
  const r = oe(e).find((i) => i.id === n);
46
46
  if (r) return r;
47
47
  const t = (s = e.shared) == null ? void 0 : s.find((i) => i.id === n);
48
48
  return t && rr(t);
49
49
  }
50
- function it(e, n) {
51
- const r = Un(e, n);
50
+ function ot(e, n) {
51
+ const r = jn(e, n);
52
52
  if (r) return H(r.query) ? void 0 : r;
53
- const t = rt(e).find((l) => l.id === n);
53
+ const t = st(e).find((o) => o.id === n);
54
54
  if (t) return t;
55
- const s = /^(.*)#(\d+)$/.exec(n), i = s ? Un(e, s[1]) : void 0, a = Number(s == null ? void 0 : s[2]), o = i && H(i.query) ? i.query.union[a] : void 0;
56
- return i && o ? {
55
+ const s = /^(.*)#(\d+)$/.exec(n), i = s ? jn(e, s[1]) : void 0, l = Number(s == null ? void 0 : s[2]), a = i && H(i.query) ? i.query.union[l] : void 0;
56
+ return i && a ? {
57
57
  id: n,
58
58
  alias: i.alias,
59
- query: o,
59
+ query: a,
60
60
  of: i,
61
- index: a
61
+ index: l
62
62
  } : void 0;
63
63
  }
64
- function ot(e) {
64
+ function at(e) {
65
65
  return { id: e.id, ...Ve(e.query) };
66
66
  }
67
67
  function Ye(e) {
@@ -71,8 +71,8 @@ function Ye(e) {
71
71
  where: t,
72
72
  select: s,
73
73
  groupBy: i,
74
- having: a,
75
- distinct: o
74
+ having: l,
75
+ distinct: a
76
76
  } = e;
77
77
  return {
78
78
  from: n,
@@ -80,29 +80,29 @@ function Ye(e) {
80
80
  select: s,
81
81
  ...t !== void 0 ? { where: t } : {},
82
82
  ...i !== void 0 ? { groupBy: i } : {},
83
- ...a !== void 0 ? { having: a } : {},
84
- ...o !== void 0 ? { distinct: o } : {}
83
+ ...l !== void 0 ? { having: l } : {},
84
+ ...a !== void 0 ? { distinct: a } : {}
85
85
  };
86
86
  }
87
87
  function sr(e, n) {
88
88
  return n.reduce((r, t) => {
89
- const s = r && it(r, t);
90
- return s ? ot(s) : void 0;
89
+ const s = r && ot(r, t);
90
+ return s ? at(s) : void 0;
91
91
  }, e);
92
92
  }
93
- function mn(e, n, r) {
93
+ function yn(e, n, r) {
94
94
  if (n.length === 0) return r(e);
95
- const [t, ...s] = n, i = it(e, t);
95
+ const [t, ...s] = n, i = ot(e, t);
96
96
  if (!i) return e;
97
- const a = ot(i), o = mn(a, s, r);
98
- if (o === a || !ue(o)) return e;
97
+ const l = at(i), a = yn(l, s, r);
98
+ if (a === l || !ue(a)) return e;
99
99
  if ("type" in i && i.type === "subquery") {
100
- const c = { ...i, query: Ye(o) };
101
- return st(e, (m) => m === i ? c : m);
100
+ const c = { ...i, query: Ye(a) };
101
+ return it(e, (m) => m === i ? c : m);
102
102
  }
103
- const l = (c, m) => {
103
+ const o = (c, m) => {
104
104
  if (!oe(e).some((f) => f.id === c.id))
105
- return dn(e, (f, p) => p.id === c.id ? m : f);
105
+ return fn(e, (f, h) => h.id === c.id ? m : f);
106
106
  const d = { ...c, query: m };
107
107
  return c === e.from ? { ...e, from: d } : {
108
108
  ...e,
@@ -111,12 +111,12 @@ function mn(e, n, r) {
111
111
  };
112
112
  if (tr(i)) {
113
113
  const { of: c, index: m } = i, { union: d } = c.query;
114
- return l(c, {
114
+ return o(c, {
115
115
  ...c.query,
116
- union: d.map((f, p) => p === m ? Ye(o) : f)
116
+ union: d.map((f, h) => h === m ? Ye(a) : f)
117
117
  });
118
118
  }
119
- return l(i, Ye(o));
119
+ return o(i, Ye(a));
120
120
  }
121
121
  const ir = {
122
122
  text: "varchar",
@@ -130,103 +130,103 @@ const ir = {
130
130
  function Ne(e, n, r = []) {
131
131
  const t = Ve(n);
132
132
  if (t.select.length === 0)
133
- return Q(t).flatMap((o) => {
133
+ return Q(t).flatMap((a) => {
134
134
  var c;
135
- if (o.table !== void 0) return ((c = z(e, o.table)) == null ? void 0 : c.columns) ?? [];
136
- const l = Te(o, r);
137
- return l ? Ne(e, l, r) : [];
135
+ if (a.table !== void 0) return ((c = z(e, a.table)) == null ? void 0 : c.columns) ?? [];
136
+ const o = Te(a, r);
137
+ return o ? Ne(e, o, r) : [];
138
138
  });
139
- const s = new Map(Q(t).map((o) => [D(o), o.table])), i = (o) => {
140
- var l;
141
- if (o.type === "field") {
142
- const c = o.table === void 0 ? void 0 : s.get(o.table), m = c === void 0 || (l = z(e, c)) == null ? void 0 : l.columns.find((d) => d.name === o.column);
139
+ const s = new Map(Q(t).map((a) => [D(a), a.table])), i = (a) => {
140
+ var o;
141
+ if (a.type === "field") {
142
+ const c = a.table === void 0 ? void 0 : s.get(a.table), m = c === void 0 || (o = z(e, c)) == null ? void 0 : o.columns.find((d) => d.name === a.column);
143
143
  return (m == null ? void 0 : m.type) ?? "varchar";
144
144
  }
145
- if (o.type === "function") {
146
- const c = o.name.toUpperCase();
145
+ if (a.type === "function") {
146
+ const c = a.name.toUpperCase();
147
147
  if (c === "COUNT") return "integer";
148
148
  if (c === "SUM" || c === "AVG") return "decimal";
149
- if ((c === "MIN" || c === "MAX") && o.args[0]) return i(o.args[0]);
149
+ if ((c === "MIN" || c === "MAX") && a.args[0]) return i(a.args[0]);
150
150
  }
151
- return o.type === "literal" && typeof o.value == "number" ? "decimal" : o.type === "literal" && o.as ? ir[o.as] : "varchar";
152
- }, a = (o) => {
151
+ return a.type === "literal" && typeof a.value == "number" ? "decimal" : a.type === "literal" && a.as ? ir[a.as] : "varchar";
152
+ }, l = (a) => {
153
153
  var d, f;
154
- if (o.type !== "field" || o.table === void 0) return !1;
155
- const l = Q(t).find((p) => D(p) === o.table);
156
- if (!l) return !1;
157
- const c = l.table === void 0 ? Te(l, r) : void 0, m = l.table !== void 0 ? (d = z(e, l.table)) == null ? void 0 : d.columns : c && Ne(e, c, r);
158
- return !!((f = m == null ? void 0 : m.find((p) => p.name === o.column)) != null && f.joinOnly);
154
+ if (a.type !== "field" || a.table === void 0) return !1;
155
+ const o = Q(t).find((h) => D(h) === a.table);
156
+ if (!o) return !1;
157
+ const c = o.table === void 0 ? Te(o, r) : void 0, m = o.table !== void 0 ? (d = z(e, o.table)) == null ? void 0 : d.columns : c && Ne(e, c, r);
158
+ return !!((f = m == null ? void 0 : m.find((h) => h.name === a.column)) != null && f.joinOnly);
159
159
  };
160
- return t.select.flatMap((o) => {
161
- const l = K(o);
162
- return l ? [{
163
- name: l,
164
- label: l,
165
- type: i(o.expression),
166
- ...a(o.expression) ? { joinOnly: !0 } : {}
160
+ return t.select.flatMap((a) => {
161
+ const o = K(a);
162
+ return o ? [{
163
+ name: o,
164
+ label: o,
165
+ type: i(a.expression),
166
+ ...l(a.expression) ? { joinOnly: !0 } : {}
167
167
  }] : [];
168
168
  });
169
169
  }
170
- function at(e, n, r = []) {
171
- const t = Te(n, r), s = (t ? Ne(e, t, r) : []).filter((i, a, o) => o.findIndex((l) => l.name === i.name) === a);
170
+ function lt(e, n, r = []) {
171
+ const t = Te(n, r), s = (t ? Ne(e, t, r) : []).filter((i, l, a) => a.findIndex((o) => o.name === i.name) === l);
172
172
  return { name: n.alias, label: n.alias, columns: s };
173
173
  }
174
174
  function ds(e, n, r = []) {
175
- return Q(n).filter((t) => t.table === void 0).map((t) => at(e, t, r));
175
+ return Q(n).filter((t) => t.table === void 0).map((t) => lt(e, t, r));
176
176
  }
177
- function lt(e, n = $e) {
177
+ function ut(e, n = $e) {
178
178
  if (H(e)) return !1;
179
179
  const r = e;
180
- return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => le(t.expression, n)) && Kn(r, n).select.length === 0;
180
+ return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => le(t.expression, n)) && Yn(r, n).select.length === 0;
181
181
  }
182
- function jn(e, n, r) {
183
- const t = (s) => s.type === "field" ? s.table === n ? { ...s, table: r } : s : s.type === "exists" && Yn(s.query).has(n) ? { ...s, link: t(s.link) } : xe(s, t);
182
+ function Ln(e, n, r) {
183
+ const t = (s) => s.type === "field" ? s.table === n ? { ...s, table: r } : s : s.type === "exists" && zn(s.query).has(n) ? { ...s, link: t(s.link) } : xe(s, t);
184
184
  return Pe(e, t, (s) => s);
185
185
  }
186
- function ut(e, n) {
186
+ function ct(e, n) {
187
187
  const r = Ve(n), t = Q(r).flatMap((s) => s.table === void 0 ? [] : [{ table: s.table, alias: s.alias }]);
188
188
  return r.select.length === 0 ? t.flatMap(({ table: s }) => {
189
189
  var i;
190
- return (((i = z(e, s)) == null ? void 0 : i.columns) ?? []).map((a) => ({ name: a.name, table: s, column: a.name }));
190
+ return (((i = z(e, s)) == null ? void 0 : i.columns) ?? []).map((l) => ({ name: l.name, table: s, column: l.name }));
191
191
  }) : r.select.flatMap((s) => {
192
192
  const { expression: i } = s;
193
193
  if (i.type !== "field" || !i.column) return [];
194
- const a = t.find((l) => D(l) === i.table), o = s.alias || i.column;
195
- return a ? [{ name: o, table: a.table, column: i.column }] : [];
194
+ const l = t.find((o) => D(o) === i.table), a = s.alias || i.column;
195
+ return l ? [{ name: a, table: l.table, column: i.column }] : [];
196
196
  });
197
197
  }
198
- function ct(e, n) {
199
- return oe(n).flatMap((r) => ut(e, r.query).flatMap(({ name: t, table: s, column: i }) => e.relations.flatMap((a) => {
200
- const o = a.fromTable === s && a.fromColumn === i;
201
- if (!o && !(a.toTable === s && a.toColumn === i)) return [];
202
- const l = o ? a.toTable : a.fromTable, c = {
198
+ function dt(e, n) {
199
+ return oe(n).flatMap((r) => ct(e, r.query).flatMap(({ name: t, table: s, column: i }) => e.relations.flatMap((l) => {
200
+ const a = l.fromTable === s && l.fromColumn === i;
201
+ if (!a && !(l.toTable === s && l.toColumn === i)) return [];
202
+ const o = a ? l.toTable : l.fromTable, c = {
203
203
  type: "binary",
204
204
  operator: "=",
205
- left: { type: "field", table: l, column: o ? a.toColumn : a.fromColumn },
205
+ left: { type: "field", table: o, column: a ? l.toColumn : l.fromColumn },
206
206
  right: { type: "field", table: r.alias, column: t }
207
207
  };
208
- return [{ table: l, on: c }];
208
+ return [{ table: o, on: c }];
209
209
  })));
210
210
  }
211
211
  function or(e, n, r) {
212
212
  var t;
213
- return (t = ct(e, n).find((s) => s.table === r)) == null ? void 0 : t.on;
213
+ return (t = dt(e, n).find((s) => s.table === r)) == null ? void 0 : t.on;
214
214
  }
215
- function dt(e, n, r) {
216
- var l;
217
- const t = Q(n).flatMap((c) => c !== r && c.table !== void 0 ? [{ table: c.table, alias: c.alias }] : []), s = (c, m) => m === void 0 ? { type: "field", table: c } : { type: "field", table: c, column: m }, a = ut(e, r.query).flatMap(({ name: c, table: m, column: d }) => t.flatMap((f) => e.relations.filter((p) => p.fromTable === m && p.fromColumn === d && p.toTable === f.table || p.toTable === m && p.toColumn === d && p.fromTable === f.table).map((p) => ({
215
+ function ft(e, n, r) {
216
+ var o;
217
+ const t = Q(n).flatMap((c) => c !== r && c.table !== void 0 ? [{ table: c.table, alias: c.alias }] : []), s = (c, m) => m === void 0 ? { type: "field", table: c } : { type: "field", table: c, column: m }, l = ct(e, r.query).flatMap(({ name: c, table: m, column: d }) => t.flatMap((f) => e.relations.filter((h) => h.fromTable === m && h.fromColumn === d && h.toTable === f.table || h.toTable === m && h.toColumn === d && h.fromTable === f.table).map((h) => ({
218
218
  name: c,
219
- right: s(D(f), p.fromTable === m ? p.toColumn : p.fromColumn)
220
- }))))[0], o = (l = at(e, r).columns[0]) == null ? void 0 : l.name;
219
+ right: s(D(f), h.fromTable === m ? h.toColumn : h.fromColumn)
220
+ }))))[0], a = (o = lt(e, r).columns[0]) == null ? void 0 : o.name;
221
221
  return {
222
222
  type: "binary",
223
223
  operator: "=",
224
- left: s(r.alias, (a == null ? void 0 : a.name) ?? o),
225
- right: (a == null ? void 0 : a.right) ?? { type: "field" }
224
+ left: s(r.alias, (l == null ? void 0 : l.name) ?? a),
225
+ right: (l == null ? void 0 : l.right) ?? { type: "field" }
226
226
  };
227
227
  }
228
228
  const Le = (e, n) => `${e} ${n}${e === 1 ? "" : "s"}`;
229
- function ft({ unit: e, offset: n, anchor: r }) {
229
+ function mt({ unit: e, offset: n, anchor: r }) {
230
230
  const t = Math.abs(n);
231
231
  if (r) {
232
232
  const s = r === "start" ? "Start" : "End";
@@ -314,7 +314,7 @@ const ar = {
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: ft,
317
+ relativeDate: mt,
318
318
  relativeCustom: "Custom…",
319
319
  relativeUnits: {
320
320
  day: "days",
@@ -641,33 +641,33 @@ const ar = {
641
641
  outlineEmpty: "Nothing yet: pick a table to start.",
642
642
  outlineLimit: (e) => `At most ${e} row${e === 1 ? "" : "s"}`,
643
643
  outlineOffset: (e) => `Skip ${e} row${e === 1 ? "" : "s"}`
644
- }, Ln = (e) => "from" in e ? e.from : { table: e.table };
645
- let Dn = 0;
644
+ }, Dn = (e) => "from" in e ? e.from : { table: e.table };
645
+ let Gn = 0;
646
646
  function Ae() {
647
- return Dn += 1, `join-${Date.now().toString(36)}-${Dn}`;
647
+ return Gn += 1, `join-${Date.now().toString(36)}-${Gn}`;
648
648
  }
649
- let Gn = 0;
649
+ let Rn = 0;
650
650
  function lr() {
651
- return Gn += 1, `select-${Date.now().toString(36)}-${Gn}`;
651
+ return Rn += 1, `select-${Date.now().toString(36)}-${Rn}`;
652
652
  }
653
- let Rn = 0;
654
- function qn() {
655
- return Rn += 1, `group-${Date.now().toString(36)}-${Rn}`;
653
+ let qn = 0;
654
+ function Qn() {
655
+ return qn += 1, `group-${Date.now().toString(36)}-${qn}`;
656
656
  }
657
- let Qn = 0;
657
+ let _n = 0;
658
658
  function ur() {
659
- return Qn += 1, `order-${Date.now().toString(36)}-${Qn}`;
659
+ return _n += 1, `order-${Date.now().toString(36)}-${_n}`;
660
660
  }
661
661
  function ze(e, n, r) {
662
662
  if (n < 0 || n >= e.length || r < 0 || r >= e.length || n === r) return e;
663
663
  const t = [...e], [s] = t.splice(n, 1);
664
664
  return t.splice(r, 0, s), t;
665
665
  }
666
- const _n = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, Ze = (e) => (e ?? "").length <= Y;
666
+ const Fn = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, Ze = (e) => (e ?? "").length <= Y;
667
667
  function Se(e, n, r) {
668
668
  return e.map((t) => t.id === n ? r(t) : t);
669
669
  }
670
- function mt(e, n, r) {
670
+ function yt(e, n, r) {
671
671
  return e.map((t) => t.id === n ? r(t) : t);
672
672
  }
673
673
  function cr(e) {
@@ -694,7 +694,7 @@ function dr(e, n, r) {
694
694
  on: n
695
695
  };
696
696
  }
697
- function yt(e, n) {
697
+ function pt(e, n) {
698
698
  const [r, ...t] = n;
699
699
  return { ...e, from: cr(r), joins: t };
700
700
  }
@@ -733,39 +733,39 @@ function ne(e) {
733
733
  // tables are joined inside it (withExistsJoins), never to the query
734
734
  // around it — that would repeat its rows, which EXISTS is there to avoid.
735
735
  case "exists": {
736
- const i = Yn(t.query);
737
- ne(t.link).filter((a) => !i.has(a)).forEach((a) => n.add(a));
736
+ const i = zn(t.query);
737
+ ne(t.link).filter((l) => !i.has(l)).forEach((l) => n.add(l));
738
738
  break;
739
739
  }
740
740
  }
741
741
  };
742
742
  return r(e), [...n];
743
743
  }
744
- function yn(e, n, r, t, s) {
745
- const i = Be(n), a = t === void 0 && s === void 0, o = a ? Vt(e, i, r) : void 0, l = {
744
+ function pn(e, n, r, t, s) {
745
+ const i = Be(n), l = t === void 0 && s === void 0, a = l ? Vt(e, i, r) : void 0, o = {
746
746
  id: Ae(),
747
747
  table: r,
748
- joinType: t ?? (o == null ? void 0 : o.joinType) ?? "inner",
748
+ joinType: t ?? (a == null ? void 0 : a.joinType) ?? "inner",
749
749
  // Else matched to a derived table holding its key, if one does.
750
- on: s ?? (o == null ? void 0 : o.on) ?? (a ? or(e, n, r) : void 0)
750
+ on: s ?? (a == null ? void 0 : a.on) ?? (l ? or(e, n, r) : void 0)
751
751
  };
752
- return { ...n, joins: [...n.joins, l] };
752
+ return { ...n, joins: [...n.joins, o] };
753
753
  }
754
754
  function fr(e, n, r) {
755
- const t = Be(n), s = zn(e, t, r);
756
- return (s && s.length > 0 ? s : [r]).reduce((a, o) => yn(e, a, o), n);
755
+ const t = Be(n), s = Zn(e, t, r);
756
+ return (s && s.length > 0 ? s : [r]).reduce((l, a) => pn(e, l, a), n);
757
757
  }
758
758
  function ce(e, n, r) {
759
759
  return ne(r).reduce((t, s) => {
760
- if (Q(t).some((o) => o.alias === s)) return t;
760
+ if (Q(t).some((a) => a.alias === s)) return t;
761
761
  const i = Be(t);
762
- return (zn(e, i, s) ?? [s]).reduce((o, l) => yn(e, o, l), t);
762
+ return (Zn(e, i, s) ?? [s]).reduce((a, o) => pn(e, a, o), t);
763
763
  }, n);
764
764
  }
765
765
  function mr(e, n, r, t) {
766
766
  return {
767
767
  ...ce(e, n, t),
768
- select: mt(n.select, r, (s) => ({ ...s, expression: t }))
768
+ select: yt(n.select, r, (s) => ({ ...s, expression: t }))
769
769
  };
770
770
  }
771
771
  function hn(e, n, r) {
@@ -773,38 +773,38 @@ function hn(e, n, r) {
773
773
  if (n < 0 || n >= t.length || r < 0 || r >= t.length || n === r) return e;
774
774
  const s = [...t], [i] = s.splice(n, 1);
775
775
  s.splice(r, 0, i);
776
- const [a, o] = t;
777
- let l, c;
778
- if (n === 0 ? (c = a, l = o) : r === 0 && (l = t[n], c = a), l && c) {
779
- const { on: m, joinType: d } = l;
776
+ const [l, a] = t;
777
+ let o, c;
778
+ if (n === 0 ? (c = l, o = a) : r === 0 && (o = t[n], c = l), o && c) {
779
+ const { on: m, joinType: d } = o;
780
780
  s[s.indexOf(c)] = dr(c, m, d);
781
781
  }
782
- return yt(e, s);
782
+ return pt(e, s);
783
783
  }
784
- function pn(e) {
784
+ function gn(e) {
785
785
  const n = /* @__PURE__ */ new Set([D(e.from)]);
786
786
  let r;
787
787
  return e.joins.some((t) => {
788
788
  n.add(D(t));
789
- const s = t.joinType === "cross" ? void 0 : t.on, i = ne(s).find((a) => !n.has(a));
789
+ const s = t.joinType === "cross" ? void 0 : t.on, i = ne(s).find((l) => !n.has(l));
790
790
  return i && (r = { table: t.table ?? D(t), needs: i }), i !== void 0;
791
791
  }), r;
792
792
  }
793
793
  function fs(e, n, r) {
794
794
  const t = hn(e, n, r);
795
- return t === e ? void 0 : pn(t);
795
+ return t === e ? void 0 : gn(t);
796
796
  }
797
797
  function yr(e, n, r) {
798
798
  const t = hn(e, n, r);
799
- return t !== e && pn(t) === void 0;
799
+ return t !== e && gn(t) === void 0;
800
800
  }
801
- function hr(e, n, r, t) {
801
+ function pr(e, n, r, t) {
802
802
  return {
803
803
  ...ce(e, n, t),
804
804
  orderBy: (n.orderBy ?? []).map((s) => s.id === r ? { ...s, expression: t } : s)
805
805
  };
806
806
  }
807
- function pr(e, n, r, t) {
807
+ function hr(e, n, r, t) {
808
808
  return {
809
809
  ...ce(e, n, t),
810
810
  groupBy: (n.groupBy ?? []).map((s) => s.id === r ? { ...s, expression: t } : s)
@@ -812,25 +812,25 @@ function pr(e, n, r, t) {
812
812
  }
813
813
  function He(e, n) {
814
814
  if (!n || !Jt(n)) return n;
815
- const r = fe(n), t = r.map((l) => He(e, l)), i = t.some((l, c) => l !== r[c]) ? xe(n, (l) => t[r.indexOf(l)]) : n;
815
+ const r = fe(n), t = r.map((o) => He(e, o)), i = t.some((o, c) => o !== r[c]) ? xe(n, (o) => t[r.indexOf(o)]) : n;
816
816
  if (i.type !== "exists") return i;
817
- const { query: a } = i, { joins: o } = ce(
817
+ const { query: l } = i, { joins: a } = ce(
818
818
  e,
819
819
  {
820
820
  id: "",
821
- from: a.from,
822
- joins: a.joins,
821
+ from: l.from,
822
+ joins: l.joins,
823
823
  select: []
824
824
  },
825
- a.where
825
+ l.where
826
826
  );
827
- return o === a.joins ? i : { ...i, query: { ...a, joins: o } };
827
+ return a === l.joins ? i : { ...i, query: { ...l, joins: a } };
828
828
  }
829
829
  function ms(e, n, r) {
830
830
  const t = He(e, r);
831
831
  return { ...ce(e, n, t), having: t };
832
832
  }
833
- function Fn(e, n) {
833
+ function Jn(e, n) {
834
834
  const r = /* @__PURE__ */ new Set([
835
835
  ...n ? Q(n).map(D) : [],
836
836
  ...me(e).map((s) => s.name)
@@ -842,9 +842,9 @@ function Fn(e, n) {
842
842
  function ys() {
843
843
  return { id: W() };
844
844
  }
845
- function tn(e, n, r) {
845
+ function rn(e, n, r) {
846
846
  if (!ue(n))
847
- return r.type !== "setBaseTable" && r.type !== "setDerivedBaseTable" ? n : tn(e, {
847
+ return r.type !== "setBaseTable" && r.type !== "setDerivedBaseTable" ? n : rn(e, {
848
848
  id: n.id,
849
849
  ...n.shared ? { shared: n.shared } : {},
850
850
  from: { table: "" },
@@ -859,8 +859,8 @@ function tn(e, n, r) {
859
859
  ...n,
860
860
  from: {
861
861
  id: r.id ?? Ae(),
862
- alias: Fn(e, n),
863
- query: { from: Ln(r), joins: [], select: [] }
862
+ alias: Jn(e, n),
863
+ query: { from: Dn(r), joins: [], select: [] }
864
864
  }
865
865
  };
866
866
  case "changeBaseAlias":
@@ -871,35 +871,35 @@ function tn(e, n, r) {
871
871
  from: n.from.table === void 0 ? { ...n.from, alias: r.alias || n.from.alias } : { ...n.from, alias: r.alias }
872
872
  } : n;
873
873
  case "addJoin":
874
- return r.joinType === void 0 && r.on === void 0 ? fr(e, n, r.table) : yn(e, n, r.table, r.joinType, r.on);
874
+ return r.joinType === void 0 && r.on === void 0 ? fr(e, n, r.table) : pn(e, n, r.table, r.joinType, r.on);
875
875
  case "addDerivedJoin": {
876
876
  const t = {
877
877
  id: r.id ?? Ae(),
878
- alias: Fn(e, n),
878
+ alias: Jn(e, n),
879
879
  joinType: "left",
880
- query: { from: Ln(r), joins: [], select: [] }
881
- }, s = dt(e, n, t);
880
+ query: { from: Dn(r), joins: [], select: [] }
881
+ }, s = ft(e, n, t);
882
882
  return { ...n, joins: [...n.joins, { ...t, on: s }] };
883
883
  }
884
884
  case "renameDerivedTable": {
885
- const t = oe(n).find((l) => l.id === r.derivedId), s = r.alias.trim();
885
+ const t = oe(n).find((o) => o.id === r.derivedId), s = r.alias.trim();
886
886
  if (!t || s === t.alias) return n;
887
- const i = Q(n).filter((l) => l !== t).map(D);
887
+ const i = Q(n).filter((o) => o !== t).map(D);
888
888
  if (!s || s.length > Y || i.includes(s) || z(e, s)) return n;
889
- const a = jn(n, t.alias, s), { from: o } = a;
889
+ const l = Ln(n, t.alias, s), { from: a } = l;
890
890
  return {
891
- ...a,
892
- from: o.query && o.id === t.id ? { ...o, alias: s } : o,
893
- joins: a.joins.map((l) => l.id === t.id ? { ...l, alias: s } : l)
891
+ ...l,
892
+ from: a.query && a.id === t.id ? { ...a, alias: s } : a,
893
+ joins: l.joins.map((o) => o.id === t.id ? { ...o, alias: s } : o)
894
894
  };
895
895
  }
896
896
  case "renameSharedPlace": {
897
- const t = r.joinId === void 0 ? n.from : n.joins.find((o) => o.id === r.joinId), s = r.alias.trim();
897
+ const t = r.joinId === void 0 ? n.from : n.joins.find((a) => a.id === r.joinId), s = r.alias.trim();
898
898
  if ((t == null ? void 0 : t.shared) === void 0 || s === t.alias) return n;
899
- const i = Q(n).filter((o) => o !== t).map(D);
899
+ const i = Q(n).filter((a) => a !== t).map(D);
900
900
  if (!s || s.length > Y || i.includes(s) || z(e, s)) return n;
901
- const a = jn(n, t.alias, s);
902
- return r.joinId === void 0 ? { ...a, from: { ...a.from, alias: s } } : { ...a, joins: Se(a.joins, r.joinId, (o) => ({ ...o, alias: s })) };
901
+ const l = Ln(n, t.alias, s);
902
+ return r.joinId === void 0 ? { ...l, from: { ...l.from, alias: s } } : { ...l, joins: Se(l.joins, r.joinId, (a) => ({ ...a, alias: s })) };
903
903
  }
904
904
  case "setMeasureSynced":
905
905
  return {
@@ -930,7 +930,7 @@ function tn(e, n, r) {
930
930
  }))
931
931
  };
932
932
  case "reorderJoin": {
933
- const t = n.joins.findIndex((a) => a.id === r.joinId);
933
+ const t = n.joins.findIndex((l) => l.id === r.joinId);
934
934
  if (t === -1) return n;
935
935
  const s = r.direction === "up" ? t - 1 : t + 1;
936
936
  if (s < 0 || s >= n.joins.length) return n;
@@ -940,7 +940,7 @@ function tn(e, n, r) {
940
940
  case "reorderTables":
941
941
  return yr(n, r.fromIndex, r.toIndex) ? hn(n, r.fromIndex, r.toIndex) : n;
942
942
  case "removeBaseTable":
943
- return n.joins.length > 0 ? yt(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
943
+ return n.joins.length > 0 ? pt(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
944
944
  case "addSelectItem": {
945
945
  const t = { id: lr(), expression: { type: "field" } };
946
946
  return { ...n, select: [...n.select, t] };
@@ -954,12 +954,12 @@ function tn(e, n, r) {
954
954
  case "setSelectAlias":
955
955
  return Ze(r.alias) ? {
956
956
  ...n,
957
- select: mt(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
957
+ select: yt(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
958
958
  } : n;
959
959
  case "setSelectExpression":
960
960
  return mr(e, n, r.selectId, r.expression);
961
961
  case "addGroupByItem": {
962
- const t = { id: qn(), expression: { type: "field" } };
962
+ const t = { id: Qn(), expression: { type: "field" } };
963
963
  return { ...n, groupBy: [...n.groupBy ?? [], t] };
964
964
  }
965
965
  case "removeGroupByItem": {
@@ -971,11 +971,11 @@ function tn(e, n, r) {
971
971
  return s === t ? n : { ...n, groupBy: s };
972
972
  }
973
973
  case "setGroupByExpression":
974
- return pr(e, n, r.groupById, r.expression);
974
+ return hr(e, n, r.groupById, r.expression);
975
975
  case "groupBySelectItem": {
976
976
  const t = n.select.find((i) => i.id === r.selectId);
977
977
  if (!t) return n;
978
- const s = { id: qn(), expression: t.expression };
978
+ const s = { id: Qn(), expression: t.expression };
979
979
  return { ...n, groupBy: [...n.groupBy ?? [], s] };
980
980
  }
981
981
  case "removeHaving":
@@ -999,16 +999,16 @@ function tn(e, n, r) {
999
999
  return s === t ? n : { ...n, orderBy: s };
1000
1000
  }
1001
1001
  case "setOrderByExpression":
1002
- return hr(e, n, r.orderById, r.expression);
1002
+ return pr(e, n, r.orderById, r.expression);
1003
1003
  case "setOrderByDirection":
1004
1004
  return {
1005
1005
  ...n,
1006
1006
  orderBy: (n.orderBy ?? []).map((t) => t.id === r.orderById ? { ...t, direction: r.direction } : t)
1007
1007
  };
1008
1008
  case "setLimit":
1009
- return { ...n, limit: _n(r.limit) };
1009
+ return { ...n, limit: Fn(r.limit) };
1010
1010
  case "setOffset":
1011
- return { ...n, offset: _n(r.offset) };
1011
+ return { ...n, offset: Fn(r.offset) };
1012
1012
  case "clearPaging":
1013
1013
  return { ...n, limit: void 0, offset: void 0 };
1014
1014
  /* v8 ignore start -- PipelineAction is a closed union; every real case is
@@ -1025,10 +1025,10 @@ const gr = {
1025
1025
  full: "FULL",
1026
1026
  cross: "CROSS"
1027
1027
  };
1028
- function hs(e) {
1028
+ function ps(e) {
1029
1029
  return `${gr[e.joinType]} JOIN ${D(e)}`;
1030
1030
  }
1031
- function ps(e) {
1031
+ function hs(e) {
1032
1032
  return e.alias || "Column";
1033
1033
  }
1034
1034
  function gs() {
@@ -1040,11 +1040,11 @@ function bs() {
1040
1040
  function ws(e, n, r) {
1041
1041
  const t = Q(n);
1042
1042
  if (r !== void 0) {
1043
- const l = t.findIndex((c) => "id" in c && c.id === r);
1044
- return new Set(t.slice(0, l + 1).map(D));
1043
+ const o = t.findIndex((c) => "id" in c && c.id === r);
1044
+ return new Set(t.slice(0, o + 1).map(D));
1045
1045
  }
1046
- const s = new Set(Be(n)), i = Pt(e, [...s]), a = new Set(ct(e, n).map((l) => l.table)), o = me(e).filter((l) => !s.has(l.name) && (i.has(l.name) || a.has(l.name)));
1047
- return /* @__PURE__ */ new Set([...t.map(D), ...o.map((l) => l.name)]);
1046
+ const s = new Set(Be(n)), i = Pt(e, [...s]), l = new Set(dt(e, n).map((o) => o.table)), a = me(e).filter((o) => !s.has(o.name) && (i.has(o.name) || l.has(o.name)));
1047
+ return /* @__PURE__ */ new Set([...t.map(D), ...a.map((o) => o.name)]);
1048
1048
  }
1049
1049
  function $s(e, n) {
1050
1050
  const r = Q(n);
@@ -1057,15 +1057,15 @@ function vs(e, n) {
1057
1057
  const r = new Set(Be(n));
1058
1058
  return me(e).filter((t) => !r.has(t.name));
1059
1059
  }
1060
- function rn(e) {
1061
- return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + rn(r), 0) : e.type === "not" ? rn(e.operand) : 1 : 0;
1060
+ function sn(e) {
1061
+ return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + sn(r), 0) : e.type === "not" ? sn(e.operand) : 1 : 0;
1062
1062
  }
1063
- function Jn(e) {
1063
+ function Pn(e) {
1064
1064
  return e.column ? e.table ? `${e.table}.${e.column}` : e.column : "?";
1065
1065
  }
1066
1066
  function Ss(e, n = ar) {
1067
1067
  var r;
1068
- 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 : `${Jn(e.left)} ${e.operator ?? "="} ${Jn(e.right)}` : n.conditionCount(rn(e)) : n.setJoinCondition;
1068
+ 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 : `${Pn(e.left)} ${e.operator ?? "="} ${Pn(e.right)}` : n.conditionCount(sn(e)) : n.setJoinCondition;
1069
1069
  }
1070
1070
  function Es(e, n) {
1071
1071
  const r = new Set(ne(e.where));
@@ -1076,40 +1076,40 @@ function Ts(e, n, r) {
1076
1076
  return { ...ce(e, n, t), where: t };
1077
1077
  }
1078
1078
  function Ns(e, n, r, t) {
1079
- return r.length === 0 ? tn(e, n, t) : ue(n) ? mn(n, r, (s) => tn(e, s, t)) : n;
1079
+ return r.length === 0 ? rn(e, n, t) : ue(n) ? yn(n, r, (s) => rn(e, s, t)) : n;
1080
1080
  }
1081
- const gn = (e) => typeof e == "object" && e !== null && !Array.isArray(e) && !(e instanceof Date), bn = (e) => typeof e.shared == "string";
1081
+ const bn = (e) => typeof e == "object" && e !== null && !Array.isArray(e) && !(e instanceof Date), wn = (e) => typeof e.shared == "string";
1082
1082
  function qe(e, n) {
1083
1083
  if (Array.isArray(e)) {
1084
1084
  let s = !1;
1085
- const i = e.flatMap((a) => {
1086
- const o = qe(a, n);
1087
- return o !== a && (s = !0), o === void 0 ? [] : [o];
1085
+ const i = e.flatMap((l) => {
1086
+ const a = qe(l, n);
1087
+ return a !== l && (s = !0), a === void 0 ? [] : [a];
1088
1088
  });
1089
1089
  return s ? i : e;
1090
1090
  }
1091
- if (!gn(e)) return e;
1092
- if (bn(e)) return n(e);
1091
+ if (!bn(e)) return e;
1092
+ if (wn(e)) return n(e);
1093
1093
  let r = !1;
1094
1094
  const t = Object.entries(e).map(([s, i]) => {
1095
- const a = qe(i, n);
1096
- return a !== i && (r = !0), [s, a];
1095
+ const l = qe(i, n);
1096
+ return l !== i && (r = !0), [s, l];
1097
1097
  });
1098
1098
  return r ? Object.fromEntries(t) : e;
1099
1099
  }
1100
1100
  function As(e) {
1101
1101
  const n = new Map((e.shared ?? []).map((t) => [t.id, []])), r = (t, s) => {
1102
1102
  if (Array.isArray(t)) {
1103
- t.forEach((i, a) => r(i, [...s, a]));
1103
+ t.forEach((i, l) => r(i, [...s, l]));
1104
1104
  return;
1105
1105
  }
1106
- if (gn(t)) {
1107
- if (bn(t)) {
1106
+ if (bn(t)) {
1107
+ if (wn(t)) {
1108
1108
  const i = t.shared;
1109
1109
  n.set(i, [...n.get(i) ?? [], s]);
1110
1110
  return;
1111
1111
  }
1112
- Object.entries(t).forEach(([i, a]) => r(a, [...s, i]));
1112
+ Object.entries(t).forEach(([i, l]) => r(l, [...s, i]));
1113
1113
  }
1114
1114
  };
1115
1115
  return r(e, []), n;
@@ -1130,10 +1130,10 @@ function br(e, n) {
1130
1130
  return t;
1131
1131
  }
1132
1132
  function wr(e, n, r, t = W(), s) {
1133
- const i = e.shared ?? [], a = i.findIndex((l) => l.id === s), o = { id: t, name: n, query: r };
1133
+ const i = e.shared ?? [], l = i.findIndex((o) => o.id === s), a = { id: t, name: n, query: r };
1134
1134
  return {
1135
1135
  ...e,
1136
- shared: a < 0 ? [...i, o] : [...i.slice(0, a), o, ...i.slice(a)]
1136
+ shared: l < 0 ? [...i, a] : [...i.slice(0, l), a, ...i.slice(l)]
1137
1137
  };
1138
1138
  }
1139
1139
  const ht = (e, n) => {
@@ -1156,14 +1156,14 @@ function Bs(e, n, r) {
1156
1156
  const t = ht(e, r);
1157
1157
  return t ? { ...n, from: { shared: r, alias: t.name } } : n;
1158
1158
  }
1159
- function Cs(e, n, r = W(), t = []) {
1159
+ function Is(e, n, r = W(), t = []) {
1160
1160
  const s = sr(e, t), i = s && oe(s).find((c) => c.id === n);
1161
1161
  if (!i) return e;
1162
- const a = new Set((e.shared ?? []).map((c) => c.name));
1163
- let o = i.alias;
1164
- for (let c = 2; a.has(o); c += 1) o = `${i.alias}_${c}`;
1165
- const l = wr(e, o, i.query, r, t[0]);
1166
- return mn(l, t, (c) => c.from.query && c.from.id === n ? { ...c, from: { shared: r, alias: i.alias } } : {
1162
+ const l = new Set((e.shared ?? []).map((c) => c.name));
1163
+ let a = i.alias;
1164
+ for (let c = 2; l.has(a); c += 1) a = `${i.alias}_${c}`;
1165
+ const o = wr(e, a, i.query, r, t[0]);
1166
+ return yn(o, t, (c) => c.from.query && c.from.id === n ? { ...c, from: { shared: r, alias: i.alias } } : {
1167
1167
  ...c,
1168
1168
  joins: c.joins.map((m) => {
1169
1169
  if (m.id !== n || !m.query) return m;
@@ -1172,18 +1172,18 @@ function Cs(e, n, r = W(), t = []) {
1172
1172
  })
1173
1173
  });
1174
1174
  }
1175
- function pt(e, n) {
1175
+ function gt(e, n) {
1176
1176
  const { shared: r, ...t } = e;
1177
1177
  return "joinType" in t ? { ...t, query: n } : { id: W(), alias: t.alias, query: n };
1178
1178
  }
1179
- function Is(e, n) {
1179
+ function Cs(e, n) {
1180
1180
  const r = new Map((e.shared ?? []).map((s) => [s.id, s.query])), t = n.reduce(
1181
1181
  (s, i) => typeof s == "object" && s !== null ? s[i] : void 0,
1182
1182
  e
1183
1183
  );
1184
- return !gn(t) || !bn(t) ? e : qe(e, (s) => {
1184
+ return !bn(t) || !wn(t) ? e : qe(e, (s) => {
1185
1185
  const i = r.get(s.shared);
1186
- return s === t && i ? pt(s, i) : s;
1186
+ return s === t && i ? gt(s, i) : s;
1187
1187
  });
1188
1188
  }
1189
1189
  function Ms(e, n, r) {
@@ -1199,67 +1199,67 @@ class $r extends Error {
1199
1199
  }
1200
1200
  }
1201
1201
  function Os(e, n, r = "subQueries") {
1202
- var l;
1203
- const t = (l = e.shared) == null ? void 0 : l.find((c) => c.id === n);
1202
+ var o;
1203
+ const t = (o = e.shared) == null ? void 0 : o.find((c) => c.id === n);
1204
1204
  if (!t) return e;
1205
- const s = e.shared.filter((c) => c !== t), { shared: i, ...a } = e, o = s.length > 0 ? { ...a, shared: s } : a;
1206
- return qe(o, (c) => {
1205
+ const s = e.shared.filter((c) => c !== t), { shared: i, ...l } = e, a = s.length > 0 ? { ...l, shared: s } : l;
1206
+ return qe(a, (c) => {
1207
1207
  if (c.shared !== n) return c;
1208
- if (r === "subQueries") return pt(c, t.query);
1208
+ if (r === "subQueries") return gt(c, t.query);
1209
1209
  if (!("joinType" in c)) throw new $r(t.name);
1210
1210
  });
1211
1211
  }
1212
- const gt = (e) => e.query !== void 0 && !H(e.query);
1213
- function bt(e) {
1212
+ const bt = (e) => e.query !== void 0 && !H(e.query);
1213
+ function wt(e) {
1214
1214
  const n = /* @__PURE__ */ new Map();
1215
1215
  return e.relations.forEach((r) => {
1216
1216
  n.set(r.fromTable, [...n.get(r.fromTable) ?? [], r.toTable]);
1217
1217
  }), n;
1218
1218
  }
1219
- function Ce(e, n, r = bt(e)) {
1219
+ function Ie(e, n, r = wt(e)) {
1220
1220
  const t = /* @__PURE__ */ new Set([n]), s = [n];
1221
1221
  for (let i = s.shift(); i !== void 0; i = s.shift())
1222
- (r.get(i) ?? []).forEach((a) => {
1223
- t.has(a) || (t.add(a), s.push(a));
1222
+ (r.get(i) ?? []).forEach((l) => {
1223
+ t.has(l) || (t.add(l), s.push(l));
1224
1224
  });
1225
1225
  return t;
1226
1226
  }
1227
1227
  const we = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [];
1228
- function wt(e, n) {
1228
+ function $t(e, n) {
1229
1229
  const r = new Map(Q(e).map((t) => [D(t), t.table]));
1230
1230
  return ne(n).map((t) => ({
1231
1231
  ref: t,
1232
1232
  table: r.has(t) ? r.get(t) : t
1233
1233
  }));
1234
1234
  }
1235
- const wn = (e, n) => wt(e, n).map((r) => r.table);
1236
- function Pn(e, n) {
1235
+ const $n = (e, n) => $t(e, n).map((r) => r.table);
1236
+ function Vn(e, n) {
1237
1237
  const r = new Map(Q(e).map((t) => [D(t), t.table]));
1238
- return fn(n, (t) => {
1238
+ return mn(n, (t) => {
1239
1239
  if (t.type !== "field" || t.table === void 0) return t;
1240
1240
  const s = r.get(t.table);
1241
1241
  return s === void 0 || s === t.table ? t : { ...t, table: s };
1242
1242
  });
1243
1243
  }
1244
- function $t(e, n) {
1245
- const r = (n.groupBy ?? []).flatMap((s) => wt(n, s.expression)), t = bt(e);
1244
+ function vt(e, n) {
1245
+ const r = (n.groupBy ?? []).flatMap((s) => $t(n, s.expression)), t = wt(e);
1246
1246
  return me(e).map((s) => {
1247
- const i = Ce(e, s.name, t), a = r.find((o) => o.table === void 0 || !i.has(o.table));
1248
- return a ? { table: s, unlinked: a.table ?? a.ref } : { table: s };
1247
+ const i = Ie(e, s.name, t), l = r.find((a) => a.table === void 0 || !i.has(a.table));
1248
+ return l ? { table: s, unlinked: l.table ?? l.ref } : { table: s };
1249
1249
  });
1250
1250
  }
1251
1251
  function vr(e, n, r) {
1252
- const t = Ce(e, r);
1252
+ const t = Ie(e, r);
1253
1253
  return we(n.where).filter((s) => {
1254
- const i = wn(n, s);
1255
- return i.length > 0 && i.every((a) => a !== void 0 && t.has(a));
1254
+ const i = $n(n, s);
1255
+ return i.length > 0 && i.every((l) => l !== void 0 && t.has(l));
1256
1256
  });
1257
1257
  }
1258
1258
  function Sr(e, n, r) {
1259
- const t = Ce(e, r);
1259
+ const t = Ie(e, r);
1260
1260
  return we(n.where).some((s) => {
1261
- const i = wn(n, s);
1262
- return i.some((a) => a !== void 0 && t.has(a)) && i.some((a) => a === void 0 || !t.has(a));
1261
+ const i = $n(n, s);
1262
+ return i.some((l) => l !== void 0 && t.has(l)) && i.some((l) => l === void 0 || !t.has(l));
1263
1263
  });
1264
1264
  }
1265
1265
  function Re(e, n) {
@@ -1267,7 +1267,7 @@ function Re(e, n) {
1267
1267
  for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
1268
1268
  return r;
1269
1269
  }
1270
- function sn(e, n) {
1270
+ function on(e, n) {
1271
1271
  if (e.type === "field" && e.column) return e.column;
1272
1272
  if (e.type === "function") {
1273
1273
  const r = e.args[0];
@@ -1275,7 +1275,7 @@ function sn(e, n) {
1275
1275
  }
1276
1276
  return n;
1277
1277
  }
1278
- const on = (e, n) => ({ type: "field", table: e, column: n });
1278
+ const an = (e, n) => ({ type: "field", table: e, column: n });
1279
1279
  function Er(e, n) {
1280
1280
  return {
1281
1281
  type: "logical",
@@ -1301,127 +1301,127 @@ function Er(e, n) {
1301
1301
  function Tr(e, n, r) {
1302
1302
  var i;
1303
1303
  if (r.type !== "field" || r.table === void 0 || r.column === void 0) return !1;
1304
- const t = wn(n, r)[0], s = (i = z(e, t)) == null ? void 0 : i.columns.find((a) => a.name === r.column);
1304
+ const t = $n(n, r)[0], s = (i = z(e, t)) == null ? void 0 : i.columns.find((l) => l.name === r.column);
1305
1305
  return s !== void 0 && !s.nullable;
1306
1306
  }
1307
- function vt(e, n, r, t) {
1308
- const s = n.groupBy.map((a) => a.expression), i = t.map((a, o) => Tr(e, n, s[o]) ? {
1307
+ function St(e, n, r, t) {
1308
+ const s = n.groupBy.map((l) => l.expression), i = t.map((l, a) => Tr(e, n, s[a]) ? {
1309
1309
  type: "binary",
1310
1310
  operator: "=",
1311
- left: on(r, a),
1312
- right: s[o]
1313
- } : Er(on(r, a), s[o]));
1311
+ left: an(r, l),
1312
+ right: s[a]
1313
+ } : Er(an(r, l), s[a]));
1314
1314
  return i.length === 1 ? i[0] : { type: "logical", operator: "AND", operands: i };
1315
1315
  }
1316
- function St(e, n, r, t, s, i) {
1317
- var k;
1318
- const a = n.groupBy.map((b) => Pn(n, b.expression)), o = vr(e, n, r).map((b) => Pn(n, b)), l = new Set(s.map((b) => b.alias).filter((b) => !!b)), c = a.map((b, I) => {
1319
- const G = Re(sn(b, `key_${I + 1}`), l);
1320
- return l.add(G), G;
1321
- }), m = [...o, ...t];
1316
+ function Et(e, n, r, t, s, i) {
1317
+ var T;
1318
+ const l = n.groupBy.map((b) => Vn(n, b.expression)), a = vr(e, n, r).map((b) => Vn(n, b)), o = new Set(s.map((b) => b.alias).filter((b) => !!b)), c = l.map((b, C) => {
1319
+ const G = Re(on(b, `key_${C + 1}`), o);
1320
+ return o.add(G), G;
1321
+ }), m = [...a, ...t];
1322
1322
  let d = {
1323
1323
  id: "",
1324
1324
  from: { table: r },
1325
1325
  joins: [],
1326
1326
  select: []
1327
1327
  };
1328
- [...a, ...m, ...s.map((b) => b.expression)].forEach((b) => {
1328
+ [...l, ...m, ...s.map((b) => b.expression)].forEach((b) => {
1329
1329
  d = ce(e, d, b);
1330
1330
  });
1331
- const f = ((k = i == null ? void 0 : i.groupBy) == null ? void 0 : k.length) ?? 0, p = [...(i == null ? void 0 : i.joins) ?? []], v = d.joins.map((b) => {
1332
- const I = p.findIndex((R) => R.table !== void 0 && R.table === b.table);
1333
- if (I === -1) return b;
1334
- const [G] = p.splice(I, 1);
1331
+ const f = ((T = i == null ? void 0 : i.groupBy) == null ? void 0 : T.length) ?? 0, h = [...(i == null ? void 0 : i.joins) ?? []], v = d.joins.map((b) => {
1332
+ const C = h.findIndex((R) => R.table !== void 0 && R.table === b.table);
1333
+ if (C === -1) return b;
1334
+ const [G] = h.splice(C, 1);
1335
1335
  return { ...b, id: G.id };
1336
1336
  }), $ = {
1337
1337
  ...i,
1338
1338
  from: d.from,
1339
1339
  joins: v,
1340
1340
  where: m.length > 0 ? { type: "logical", operator: "AND", operands: m } : void 0,
1341
- groupBy: a.map((b, I) => {
1341
+ groupBy: l.map((b, C) => {
1342
1342
  var G, R;
1343
1343
  return {
1344
- id: ((R = (G = i == null ? void 0 : i.groupBy) == null ? void 0 : G[I]) == null ? void 0 : R.id) ?? W(),
1344
+ id: ((R = (G = i == null ? void 0 : i.groupBy) == null ? void 0 : G[C]) == null ? void 0 : R.id) ?? W(),
1345
1345
  expression: b
1346
1346
  };
1347
1347
  }),
1348
1348
  select: [
1349
- ...a.map((b, I) => {
1349
+ ...l.map((b, C) => {
1350
1350
  var G;
1351
1351
  return {
1352
- id: I < f && ((G = i == null ? void 0 : i.select[I]) == null ? void 0 : G.id) || W(),
1353
- alias: c[I],
1352
+ id: C < f && ((G = i == null ? void 0 : i.select[C]) == null ? void 0 : G.id) || W(),
1353
+ alias: c[C],
1354
1354
  expression: b
1355
1355
  };
1356
1356
  }),
1357
1357
  ...s
1358
1358
  ]
1359
1359
  };
1360
- return $.where === void 0 && delete $.where, { query: $, copied: o.length, keyNames: c };
1360
+ return $.where === void 0 && delete $.where, { query: $, copied: a.length, keyNames: c };
1361
1361
  }
1362
1362
  function Us(e, n, r) {
1363
1363
  var v;
1364
1364
  if ((n.groupBy ?? []).length === 0)
1365
1365
  throw new Error("addMeasure: the query isn't grouped (a measure is per group)");
1366
- const t = $t(e, n).find(($) => $.table.name === r.table);
1366
+ const t = vt(e, n).find(($) => $.table.name === r.table);
1367
1367
  if (!t) throw new Error(`addMeasure: unknown table "${r.table}"`);
1368
1368
  if (t.unlinked !== void 0)
1369
1369
  throw new Error(`addMeasure: "${r.table}" doesn't share the query's grouping`);
1370
1370
  const s = we(r.where), i = n.joins.find(($) => {
1371
- var k;
1372
- return gt($) && ((k = $.drillAcross) == null ? void 0 : k.synced) === !0 && $.query.from.table === r.table && Z(we($.query.where).slice($.drillAcross.copied), s);
1371
+ var T;
1372
+ return bt($) && ((T = $.drillAcross) == null ? void 0 : T.synced) === !0 && $.query.from.table === r.table && Z(we($.query.where).slice($.drillAcross.copied), s);
1373
1373
  });
1374
- let { joins: a } = n, o, l;
1374
+ let { joins: l } = n, a, o;
1375
1375
  if (i) {
1376
- o = i.alias;
1377
- const $ = ((v = i.query.groupBy) == null ? void 0 : v.length) ?? 0, k = i.query.select.slice($).find((b) => Z(b.expression, r.value));
1378
- if (k)
1379
- l = k.alias;
1376
+ a = i.alias;
1377
+ const $ = ((v = i.query.groupBy) == null ? void 0 : v.length) ?? 0, T = i.query.select.slice($).find((b) => Z(b.expression, r.value));
1378
+ if (T)
1379
+ o = T.alias;
1380
1380
  else {
1381
- l = Re(
1382
- sn(r.value, "value"),
1383
- new Set(i.query.select.map((I) => I.alias ?? ""))
1381
+ o = Re(
1382
+ on(r.value, "value"),
1383
+ new Set(i.query.select.map((C) => C.alias ?? ""))
1384
1384
  );
1385
- const b = { id: W(), alias: l, expression: r.value };
1386
- a = a.map((I) => I === i ? { ...i, query: { ...i.query, select: [...i.query.select, b] } } : I);
1385
+ const b = { id: W(), alias: o, expression: r.value };
1386
+ l = l.map((C) => C === i ? { ...i, query: { ...i.query, select: [...i.query.select, b] } } : C);
1387
1387
  }
1388
1388
  } else {
1389
- l = sn(r.value, "value");
1390
- const $ = { id: W(), alias: l, expression: r.value }, k = St(e, n, r.table, s, [$]), b = /* @__PURE__ */ new Set([
1389
+ o = on(r.value, "value");
1390
+ const $ = { id: W(), alias: o, expression: r.value }, T = Et(e, n, r.table, s, [$]), b = /* @__PURE__ */ new Set([
1391
1391
  ...Q(n).map(D),
1392
1392
  ...me(e).map((R) => R.name)
1393
- ]), I = `${r.table}_per_${k.keyNames.join("_and_")}`.slice(0, Y - 4);
1394
- o = Re(I, b);
1393
+ ]), C = `${r.table}_per_${T.keyNames.join("_and_")}`.slice(0, Y - 4);
1394
+ a = Re(C, b);
1395
1395
  const G = {
1396
1396
  id: W(),
1397
- alias: o,
1397
+ alias: a,
1398
1398
  joinType: "left",
1399
- query: k.query,
1400
- on: vt(e, n, o, k.keyNames),
1401
- drillAcross: { synced: !0, copied: k.copied }
1399
+ query: T.query,
1400
+ on: St(e, n, a, T.keyNames),
1401
+ drillAcross: { synced: !0, copied: T.copied }
1402
1402
  };
1403
- a = [...a, G];
1403
+ l = [...l, G];
1404
1404
  }
1405
1405
  const c = {
1406
1406
  type: "function",
1407
1407
  name: "MAX",
1408
- args: [on(o, l)]
1409
- }, m = new Set(n.select.map(($) => $.alias).filter(($) => !!$)), d = Re(`${r.table}_${l}`.slice(0, Y - 4), m);
1408
+ args: [an(a, o)]
1409
+ }, m = new Set(n.select.map(($) => $.alias).filter(($) => !!$)), d = Re(`${r.table}_${o}`.slice(0, Y - 4), m);
1410
1410
  let f = r.selectId !== void 0 ? n.select.map(($) => $.id === r.selectId ? { ...$, expression: c, alias: $.alias || d } : $) : [...n.select, { id: W(), expression: c, alias: d }];
1411
- return f = [...n.groupBy.filter(($) => !f.some((k) => Z(k.expression, $.expression))).map(($) => ({ id: W(), expression: $.expression })), ...f], { ...n, joins: a, select: f };
1411
+ return f = [...n.groupBy.filter(($) => !f.some((T) => Z(T.expression, $.expression))).map(($) => ({ id: W(), expression: $.expression })), ...f], { ...n, joins: l, select: f };
1412
1412
  }
1413
- function Et(e, n, r) {
1413
+ function Tt(e, n, r) {
1414
1414
  var m, d;
1415
- if (!gt(r)) return;
1415
+ if (!bt(r)) return;
1416
1416
  const t = r, s = t.query.from.table, i = n.groupBy ?? [];
1417
1417
  if (s === void 0 || i.length === 0) return;
1418
- const a = $t(e, n).find((f) => f.table.name === s);
1419
- if (!a || a.unlinked !== void 0) return;
1420
- const o = ((m = t.query.groupBy) == null ? void 0 : m.length) ?? 0, l = we(t.query.where).slice(((d = t.drillAcross) == null ? void 0 : d.copied) ?? 0), c = St(e, n, s, l, t.query.select.slice(o), t.query);
1418
+ const l = vt(e, n).find((f) => f.table.name === s);
1419
+ if (!l || l.unlinked !== void 0) return;
1420
+ const a = ((m = t.query.groupBy) == null ? void 0 : m.length) ?? 0, o = we(t.query.where).slice(((d = t.drillAcross) == null ? void 0 : d.copied) ?? 0), c = Et(e, n, s, o, t.query.select.slice(a), t.query);
1421
1421
  return {
1422
1422
  ...t,
1423
1423
  query: c.query,
1424
- on: vt(e, n, t.alias, c.keyNames),
1424
+ on: St(e, n, t.alias, c.keyNames),
1425
1425
  drillAcross: { synced: !0, copied: c.copied }
1426
1426
  };
1427
1427
  }
@@ -1436,41 +1436,41 @@ function Qe(e) {
1436
1436
  };
1437
1437
  }
1438
1438
  function Nr(e, n, r) {
1439
- const t = Et(e, n, r);
1439
+ const t = Tt(e, n, r);
1440
1440
  return t !== void 0 && Z(Qe(t), Qe(r));
1441
1441
  }
1442
- function Tt(e, n, r) {
1442
+ function Nt(e, n, r) {
1443
1443
  let t = !1;
1444
- const s = (o, l) => We(
1445
- l,
1446
- (c, m) => Tt(
1444
+ const s = (a, o) => We(
1445
+ o,
1446
+ (c, m) => Nt(
1447
1447
  e,
1448
- o && H(o) === H(l) ? be(o)[m] : void 0,
1448
+ a && H(a) === H(o) ? be(a)[m] : void 0,
1449
1449
  c
1450
1450
  )
1451
1451
  ), i = r.from.query ? (() => {
1452
- const o = n != null && n.from.query && n.from.id === r.from.id ? n.from.query : void 0, l = s(o, r.from.query);
1453
- return l === r.from.query ? r.from : { ...r.from, query: l };
1452
+ const a = n != null && n.from.query && n.from.id === r.from.id ? n.from.query : void 0, o = s(a, r.from.query);
1453
+ return o === r.from.query ? r.from : { ...r.from, query: o };
1454
1454
  })() : r.from;
1455
1455
  i !== r.from && (t = !0);
1456
- const a = r.joins.map((o) => {
1456
+ const l = r.joins.map((a) => {
1457
1457
  var d;
1458
- if (!o.query) return o;
1459
- const l = n == null ? void 0 : n.joins.find((f) => f.id === o.id && f.query !== void 0), c = s(l == null ? void 0 : l.query, o.query);
1460
- let m = c === o.query ? o : { ...o, query: c };
1458
+ if (!a.query) return a;
1459
+ const o = n == null ? void 0 : n.joins.find((f) => f.id === a.id && f.query !== void 0), c = s(o == null ? void 0 : o.query, a.query);
1460
+ let m = c === a.query ? a : { ...a, query: c };
1461
1461
  if ((d = m.drillAcross) != null && d.synced) {
1462
- const f = Et(e, r, m);
1462
+ const f = Tt(e, r, m);
1463
1463
  f && !Z(f, m) && (m = f);
1464
- } else if (m.drillAcross && (l != null && l.drillAcross) && !Z(Qe(l), Qe(m))) {
1464
+ } else if (m.drillAcross && (o != null && o.drillAcross) && !Z(Qe(o), Qe(m))) {
1465
1465
  const f = { ...m };
1466
1466
  delete f.drillAcross, m = f;
1467
1467
  }
1468
- return m !== o && (t = !0), m;
1468
+ return m !== a && (t = !0), m;
1469
1469
  });
1470
- return t ? { ...r, from: i, joins: a } : r;
1470
+ return t ? { ...r, from: i, joins: l } : r;
1471
1471
  }
1472
1472
  function Ar(e, n, r) {
1473
- return ue(r) ? Tt(e, n && ue(n) ? n : void 0, r) : r;
1473
+ return ue(r) ? Nt(e, n && ue(n) ? n : void 0, r) : r;
1474
1474
  }
1475
1475
  const js = ["text", "number", "date", "boolean"], kr = {
1476
1476
  varchar: "text",
@@ -1481,7 +1481,7 @@ const js = ["text", "number", "date", "boolean"], kr = {
1481
1481
  time: "time",
1482
1482
  boolean: "boolean"
1483
1483
  };
1484
- function Nt(e) {
1484
+ function At(e) {
1485
1485
  return kr[e];
1486
1486
  }
1487
1487
  function xr(e) {
@@ -1501,22 +1501,22 @@ function _e(e) {
1501
1501
  if (typeof n == "number") return Number.isInteger(n) ? "integer" : "number";
1502
1502
  if (typeof n == "bigint") return "integer";
1503
1503
  if (typeof n == "boolean") return "boolean";
1504
- if (n instanceof Date) return nt(n) ? "date" : "dateTime";
1504
+ if (n instanceof Date) return tt(n) ? "date" : "dateTime";
1505
1505
  }
1506
- function $n(e, n, r) {
1506
+ function vn(e, n, r) {
1507
1507
  if (!e) return;
1508
- const t = (s) => $n(s, n, r);
1508
+ const t = (s) => vn(s, n, r);
1509
1509
  switch (e.type) {
1510
1510
  case "field":
1511
1511
  return n(e);
1512
1512
  case "literal":
1513
1513
  return _e(e);
1514
1514
  case "function": {
1515
- const s = Zn(e.name, r), i = (s == null ? void 0 : s.args.findIndex((l) => l.kind === "type-name")) ?? -1, a = i >= 0 ? e.args[i] : void 0;
1516
- if ((a == null ? void 0 : a.type) === "literal" && typeof a.value == "string")
1517
- return xr(a.value);
1518
- const o = s == null ? void 0 : s.returns;
1519
- return o === "arg" ? t(e.args[0]) : o;
1515
+ const s = et(e.name, r), i = (s == null ? void 0 : s.args.findIndex((o) => o.kind === "type-name")) ?? -1, l = i >= 0 ? e.args[i] : void 0;
1516
+ if ((l == null ? void 0 : l.type) === "literal" && typeof l.value == "string")
1517
+ return xr(l.value);
1518
+ const a = s == null ? void 0 : s.returns;
1519
+ return a === "arg" ? t(e.args[0]) : a;
1520
1520
  }
1521
1521
  case "if":
1522
1522
  return t(e.then) ?? t(e.else);
@@ -1526,8 +1526,8 @@ function $n(e, n, r) {
1526
1526
  return;
1527
1527
  }
1528
1528
  }
1529
- const Br = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, Cr = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, Ir = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
1530
- function vn(e, n) {
1529
+ const Br = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, Ir = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, Cr = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
1530
+ function Sn(e, n) {
1531
1531
  if (e === "" || e === null) return e;
1532
1532
  const r = typeof e == "string" ? e.trim() : void 0;
1533
1533
  switch (n) {
@@ -1536,18 +1536,18 @@ function vn(e, n) {
1536
1536
  case "integer":
1537
1537
  case "number": {
1538
1538
  let t;
1539
- return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && Br.test(r) && (t = Number(r)), t === void 0 || !nn(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
1539
+ return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && Br.test(r) && (t = Number(r)), t === void 0 || !tn(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
1540
1540
  }
1541
1541
  case "date":
1542
1542
  case "dateTime": {
1543
- if (e instanceof Date) return n === "date" && !nt(e) ? void 0 : e;
1544
- const t = r === void 0 ? null : Ir.exec(r);
1543
+ if (e instanceof Date) return n === "date" && !tt(e) ? void 0 : e;
1544
+ const t = r === void 0 ? null : Cr.exec(r);
1545
1545
  if (!t) return;
1546
- const [, s, i, a, o, l, c] = t.map(Number), m = Yt(s, i, a);
1547
- return m.getUTCMonth() !== i - 1 || m.getUTCDate() !== a ? void 0 : t[4] === void 0 ? m : n === "date" ? void 0 : new Date(Date.UTC(s, i - 1, a, o, l, Number.isNaN(c) ? 0 : c));
1546
+ const [, s, i, l, a, o, c] = t.map(Number), m = Yt(s, i, l);
1547
+ return m.getUTCMonth() !== i - 1 || m.getUTCDate() !== l ? void 0 : t[4] === void 0 ? m : n === "date" ? void 0 : new Date(Date.UTC(s, i - 1, l, a, o, Number.isNaN(c) ? 0 : c));
1548
1548
  }
1549
1549
  case "time": {
1550
- const t = r === void 0 ? null : Cr.exec(r);
1550
+ const t = r === void 0 ? null : Ir.exec(r);
1551
1551
  return t ? `${t[1]}:${t[2]}:${t[3] ?? "00"}` : void 0;
1552
1552
  }
1553
1553
  case "boolean":
@@ -1561,25 +1561,25 @@ function Mr(e, n, r) {
1561
1561
  var m;
1562
1562
  if (n === void 0) return e;
1563
1563
  const { as: t, empty: s, ...i } = e;
1564
- let a = vn(e.value, n);
1565
- r != null && r.length && a !== "" && a !== null && (a = (m = r.find((d) => String(d.value) === String(e.value))) == null ? void 0 : m.value);
1566
- const o = s && n === "text", l = a === void 0 ? { type: "literal", value: "" } : { ...i, value: a, ...o ? { empty: s } : {} };
1567
- return t === void 0 && !!s == !!o && a === e.value ? e : l;
1564
+ let l = Sn(e.value, n);
1565
+ r != null && r.length && l !== "" && l !== null && (l = (m = r.find((d) => String(d.value) === String(e.value))) == null ? void 0 : m.value);
1566
+ const a = s && n === "text", o = l === void 0 ? { type: "literal", value: "" } : { ...i, value: l, ...a ? { empty: s } : {} };
1567
+ return t === void 0 && !!s == !!a && l === e.value ? e : o;
1568
1568
  }
1569
1569
  function Ls(e, n, r, t = !1) {
1570
- const s = e.items.flatMap((a) => {
1571
- if (a.type !== "literal") return [a];
1572
- const o = Mr(a, n, r);
1573
- return o.value === "" && !o.empty && !t ? [] : [o];
1570
+ const s = e.items.flatMap((l) => {
1571
+ if (l.type !== "literal") return [l];
1572
+ const a = Mr(l, n, r);
1573
+ return a.value === "" && !a.empty && !t ? [] : [a];
1574
1574
  });
1575
- return s.length === e.items.length && s.every((a, o) => a === e.items[o]) ? e : { ...e, items: s };
1575
+ return s.length === e.items.length && s.every((l, a) => l === e.items[a]) ? e : { ...e, items: s };
1576
1576
  }
1577
1577
  function Ds(e) {
1578
1578
  const n = _e(e);
1579
1579
  return n === "integer" || n === "number" ? "number" : n === "date" || n === "dateTime" ? "date" : n;
1580
1580
  }
1581
1581
  function Gs(e, n) {
1582
- const t = (e.type === "literal" ? vn(e.value, n) : void 0) ?? "", s = e.type === "literal" && e.empty && n === "text";
1582
+ const t = (e.type === "literal" ? Sn(e.value, n) : void 0) ?? "", s = e.type === "literal" && e.empty && n === "text";
1583
1583
  return {
1584
1584
  type: "literal",
1585
1585
  value: t,
@@ -1587,56 +1587,58 @@ function Gs(e, n) {
1587
1587
  ...s ? { empty: s } : {}
1588
1588
  };
1589
1589
  }
1590
+ const en = "IS NOT DISTINCT FROM";
1590
1591
  function Rs(e, n = !1) {
1591
1592
  if (e !== void 0)
1592
1593
  return e === "text" ? [
1593
1594
  "=",
1594
1595
  "!=",
1596
+ en,
1595
1597
  "LIKE",
1596
1598
  "REGEXP",
1597
1599
  "IN",
1598
1600
  ...n ? ["BETWEEN"] : [],
1599
1601
  "IS NULL",
1600
1602
  "IS NOT NULL"
1601
- ] : e === "boolean" ? ["=", "!=", "IS NULL", "IS NOT NULL"] : ["=", "!=", "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
1603
+ ] : e === "boolean" ? ["=", "!=", en, "IS NULL", "IS NOT NULL"] : ["=", "!=", en, "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
1602
1604
  }
1603
- const At = "source", kt = "rows_", se = (e) => e ? { type: "literal", value: null, as: e } : { type: "literal", value: null }, ke = (e) => e.type === "literal" && e.value === null, Sn = (e) => new Map(
1605
+ const kt = "source", xt = "rows_", se = (e) => e ? { type: "literal", value: null, as: e } : { type: "literal", value: null }, ke = (e) => e.type === "literal" && e.value === null, En = (e) => new Map(
1604
1606
  Q(e).map((n) => [D(n), n.table])
1605
1607
  );
1606
- function an(e, n) {
1607
- const r = Sn(e);
1608
+ function ln(e, n) {
1609
+ const r = En(e);
1608
1610
  return ne(n).map((t) => r.get(t) ?? t);
1609
1611
  }
1610
- function xt(e, n) {
1611
- const r = Sn(e);
1612
- return fn(n, (t) => {
1612
+ function Bt(e, n) {
1613
+ const r = En(e);
1614
+ return mn(n, (t) => {
1613
1615
  if (t.type !== "field" || t.table === void 0) return t;
1614
1616
  const s = r.get(t.table);
1615
1617
  return s === void 0 || s === t.table ? t : { ...t, table: s };
1616
1618
  });
1617
1619
  }
1618
1620
  function Fe(e, n, r) {
1619
- const t = Sn(n);
1620
- return $n(r, (s) => {
1621
- var o;
1622
- const i = t.get(s.table ?? "") ?? s.table ?? "", a = (o = z(e, i)) == null ? void 0 : o.columns.find((l) => l.name === s.column);
1623
- return a && Nt(a.type);
1621
+ const t = En(n);
1622
+ return vn(r, (s) => {
1623
+ var a;
1624
+ const i = t.get(s.table ?? "") ?? s.table ?? "", l = (a = z(e, i)) == null ? void 0 : a.columns.find((o) => o.name === s.column);
1625
+ return l && At(l.type);
1624
1626
  }, $e);
1625
1627
  }
1626
- function En(e, n) {
1628
+ function Tn(e, n) {
1627
1629
  let r = e;
1628
1630
  for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
1629
1631
  return r;
1630
1632
  }
1631
- const Je = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], Bt = (e) => {
1633
+ const Je = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], It = (e) => {
1632
1634
  if (e.length !== 0)
1633
1635
  return e.length === 1 ? e[0] : { type: "logical", operator: "AND", operands: e };
1634
- }, ie = (e) => e.from.table, Vn = (e, n) => z(e, n).label;
1636
+ }, ie = (e) => e.from.table, Wn = (e, n) => z(e, n).label;
1635
1637
  function Xe(e) {
1636
1638
  const { from: n } = e;
1637
1639
  return n.query && H(n.query) ? n : void 0;
1638
1640
  }
1639
- function Ie(e, n) {
1641
+ function Ce(e, n) {
1640
1642
  if (n === void 0) return Xe(e);
1641
1643
  const r = oe(e).find((t) => t.id === n);
1642
1644
  return r && H(r.query) ? r : void 0;
@@ -1649,49 +1651,49 @@ function Ke(e, n) {
1649
1651
  const [r] = n;
1650
1652
  return r.select.map((t, s) => {
1651
1653
  var d;
1652
- const i = K(t) ?? "", a = n.map((f) => {
1653
- var p;
1654
- return { branch: f, expression: (p = f.select[s]) == null ? void 0 : p.expression };
1655
- }).filter((f) => f.expression !== void 0 && !ke(f.expression)), l = ((d = n.map((f) => {
1656
- var p;
1657
- return (p = f.select[s]) == null ? void 0 : p.expression;
1654
+ const i = K(t) ?? "", l = n.map((f) => {
1655
+ var h;
1656
+ return { branch: f, expression: (h = f.select[s]) == null ? void 0 : h.expression };
1657
+ }).filter((f) => f.expression !== void 0 && !ke(f.expression)), o = ((d = n.map((f) => {
1658
+ var h;
1659
+ return (h = f.select[s]) == null ? void 0 : h.expression;
1658
1660
  }).filter(
1659
1661
  (f) => (f == null ? void 0 : f.type) === "literal" && f.value === null
1660
- ).find((f) => f.as)) == null ? void 0 : d.as) ?? (a[0] && Fe(e, a[0].branch, a[0].expression));
1661
- if (i === At && a.length === n.length && a.every((f) => f.expression.type === "literal"))
1662
+ ).find((f) => f.as)) == null ? void 0 : d.as) ?? (l[0] && Fe(e, l[0].branch, l[0].expression));
1663
+ if (i === kt && l.length === n.length && l.every((f) => f.expression.type === "literal"))
1662
1664
  return {
1663
1665
  name: i,
1664
1666
  kind: "source",
1665
1667
  tables: [],
1666
1668
  type: "text"
1667
1669
  };
1668
- if (i.startsWith(kt) && a.length === 1 && a[0].expression.type === "literal")
1670
+ if (i.startsWith(xt) && l.length === 1 && l[0].expression.type === "literal")
1669
1671
  return {
1670
1672
  name: i,
1671
1673
  kind: "rows",
1672
1674
  tables: [],
1673
1675
  type: "integer"
1674
1676
  };
1675
- const c = a[0];
1677
+ const c = l[0];
1676
1678
  if (!c)
1677
1679
  return {
1678
1680
  name: i,
1679
1681
  kind: "shared",
1680
1682
  tables: [],
1681
- type: l
1683
+ type: o
1682
1684
  };
1683
- const m = an(c.branch, c.expression);
1684
- return a.some((f) => an(f.branch, f.expression).includes(ie(f.branch))) ? {
1685
+ const m = ln(c.branch, c.expression);
1686
+ return l.some((f) => ln(f.branch, f.expression).includes(ie(f.branch))) ? {
1685
1687
  name: i,
1686
1688
  kind: "own",
1687
1689
  tables: m,
1688
- type: l
1690
+ type: o
1689
1691
  } : {
1690
1692
  name: i,
1691
1693
  kind: "shared",
1692
1694
  tables: m,
1693
- type: l,
1694
- expression: xt(c.branch, c.expression)
1695
+ type: o,
1696
+ expression: Bt(c.branch, c.expression)
1695
1697
  };
1696
1698
  });
1697
1699
  }
@@ -1700,66 +1702,66 @@ function qs(e, n) {
1700
1702
  if (!r) return [];
1701
1703
  const { union: t } = r.query;
1702
1704
  return Ke(e, t).map((s, i) => {
1703
- const a = t.map((o) => !ke(o.select[i].expression));
1705
+ const l = t.map((a) => !ke(a.select[i].expression));
1704
1706
  return {
1705
1707
  name: s.name,
1706
1708
  kind: s.kind,
1707
- filledBy: t.filter((o, l) => a[l]).map(ie),
1708
- nullIn: t.filter((o, l) => !a[l]).map(ie)
1709
+ filledBy: t.filter((a, o) => l[o]).map(ie),
1710
+ nullIn: t.filter((a, o) => !l[o]).map(ie)
1709
1711
  };
1710
1712
  });
1711
1713
  }
1712
1714
  function Ct(e, n, r, t, s) {
1713
- const i = Ce(e, n), a = r.filter((m) => m.kind === "shared" && m.expression), o = (m) => m.tables.every((d) => i.has(d));
1714
- if (!a.some(o)) return `"${n}" shares no column with the union`;
1715
- const l = a.find((m) => !o(m));
1716
- if (l && s === "refuse")
1717
- return `"${n}" can't reach column "${l.name}" (${l.tables.join(", ")})`;
1715
+ const i = Ie(e, n), l = r.filter((m) => m.kind === "shared" && m.expression), a = (m) => m.tables.every((d) => i.has(d));
1716
+ if (!l.some(a)) return `"${n}" shares no column with the union`;
1717
+ const o = l.find((m) => !a(m));
1718
+ if (o && s === "refuse")
1719
+ return `"${n}" can't reach column "${o.name}" (${o.tables.join(", ")})`;
1718
1720
  const c = t.find((m) => !m.tables.every((d) => i.has(d)));
1719
1721
  if (c)
1720
1722
  return `"${n}" can't follow a condition on ${c.tables.join(", ")}`;
1721
1723
  }
1722
- function It(e) {
1724
+ function Mt(e) {
1723
1725
  const [n] = e, r = new Set(e.map(ie));
1724
1726
  return Je(n.where).flatMap((t) => {
1725
- const s = an(n, t), i = s.filter((a) => r.has(a));
1726
- if (i.length === 0) return [{ operand: xt(n, t), tables: s }];
1727
+ const s = ln(n, t), i = s.filter((l) => r.has(l));
1728
+ if (i.length === 0) return [{ operand: Bt(n, t), tables: s }];
1727
1729
  if (i.length === s.length) return [];
1728
1730
  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");
1729
1731
  });
1730
1732
  }
1731
- function Wn(e, n, r, t) {
1732
- const s = t.unshared ?? "refuse", i = Ke(e, n), a = It(n);
1733
+ function Hn(e, n, r, t) {
1734
+ const s = t.unshared ?? "refuse", i = Ke(e, n), l = Mt(n);
1733
1735
  if (!z(e, r.table))
1734
1736
  throw new Error(`addUnionTable: unknown table "${r.table}"`);
1735
1737
  if (n.some((x) => ie(x) === r.table))
1736
1738
  throw new Error(`addUnionTable: "${r.table}" is in the union already`);
1737
- const o = Ct(e, r.table, i, a, s);
1738
- if (o) throw new Error(`addUnionTable: ${o}`);
1739
- const l = Ce(e, r.table), c = r.label ?? Vn(e, r.table), m = i.map((x) => x.kind === "source" ? { type: "literal", value: c } : x.kind === "shared" && x.expression && x.tables.every((F) => l.has(F)) ? x.expression : se(x.type)), d = new Set(i.map((x) => x.name)), f = [], p = (x) => {
1740
- const F = En(x, d);
1739
+ const a = Ct(e, r.table, i, l, s);
1740
+ if (a) throw new Error(`addUnionTable: ${a}`);
1741
+ const o = Ie(e, r.table), c = r.label ?? Wn(e, r.table), m = i.map((x) => x.kind === "source" ? { type: "literal", value: c } : x.kind === "shared" && x.expression && x.tables.every((F) => o.has(F)) ? x.expression : se(x.type)), d = new Set(i.map((x) => x.name)), f = [], h = (x) => {
1742
+ const F = Tn(x, d);
1741
1743
  return d.add(F), f.push(F), F;
1742
1744
  }, v = [], $ = {
1743
1745
  from: { table: r.table },
1744
1746
  joins: []
1745
1747
  };
1746
1748
  r.columns.forEach((x) => {
1747
- const F = x.alias ?? (x.expression.type === "field" ? x.expression.column : void 0) ?? "value", pe = Fe(e, $, x.expression);
1748
- v.push({ name: p(F), own: x.expression, others: () => se(pe) });
1749
+ const F = x.alias ?? (x.expression.type === "field" ? x.expression.column : void 0) ?? "value", he = Fe(e, $, x.expression);
1750
+ v.push({ name: h(F), own: x.expression, others: () => se(he) });
1749
1751
  }), t.source && !i.some((x) => x.kind === "source") && v.push({
1750
- name: p(At),
1752
+ name: h(kt),
1751
1753
  own: { type: "literal", value: c },
1752
- others: (x) => ({ type: "literal", value: Vn(e, ie(x)) })
1754
+ others: (x) => ({ type: "literal", value: Wn(e, ie(x)) })
1753
1755
  });
1754
- const k = (x) => `${kt}${x}`;
1755
- (t.rowMarkers || i.some((x) => x.kind === "rows")) && (n.filter((x) => !d.has(k(ie(x)))).forEach((x) => {
1756
+ const T = (x) => `${xt}${x}`;
1757
+ (t.rowMarkers || i.some((x) => x.kind === "rows")) && (n.filter((x) => !d.has(T(ie(x)))).forEach((x) => {
1756
1758
  v.push({
1757
- name: p(k(ie(x))),
1759
+ name: h(T(ie(x))),
1758
1760
  own: se("integer"),
1759
1761
  others: (F) => F === x ? { type: "literal", value: 1 } : se("integer")
1760
1762
  });
1761
1763
  }), v.push({
1762
- name: p(k(r.table)),
1764
+ name: h(T(r.table)),
1763
1765
  own: { type: "literal", value: 1 },
1764
1766
  others: () => se("integer")
1765
1767
  }));
@@ -1767,10 +1769,10 @@ function Wn(e, n, r, t) {
1767
1769
  id: W(),
1768
1770
  alias: x,
1769
1771
  expression: F
1770
- }), I = n.map((x) => ({
1772
+ }), C = n.map((x) => ({
1771
1773
  ...x,
1772
1774
  select: [...x.select, ...v.map((F) => b(F.name, F.others(x)))]
1773
- })), G = Bt([...a.map((x) => x.operand), ...Je(r.where)]);
1775
+ })), G = It([...l.map((x) => x.operand), ...Je(r.where)]);
1774
1776
  let R = {
1775
1777
  id: "",
1776
1778
  from: { table: r.table },
@@ -1788,24 +1790,24 @@ function Wn(e, n, r, t) {
1788
1790
  from: X,
1789
1791
  joins: ee,
1790
1792
  select: ye,
1791
- where: he
1793
+ where: pe
1792
1794
  } = R, te = {
1793
1795
  from: X,
1794
1796
  joins: ee,
1795
1797
  select: ye,
1796
- ...he ? { where: he } : {}
1798
+ ...pe ? { where: pe } : {}
1797
1799
  };
1798
- return { union: [...I, te], added: f };
1800
+ return { union: [...C, te], added: f };
1799
1801
  }
1800
- function Mt(e) {
1802
+ function Ot(e) {
1801
1803
  const n = (s) => new Error(`addUnionTable: ${s}`);
1802
1804
  if (e.from.query) throw n("the query reads from a derived table");
1803
1805
  if (e.select.length === 0) throw n("the query picks no columns (SELECT *)");
1804
1806
  if ((e.groupBy ?? []).length > 0 || e.having || e.distinct || (e.orderBy ?? []).length > 0 || e.select.some((s) => le(s.expression, $e)))
1805
1807
  throw n("a union is of detail rows: no aggregates, GROUP BY, HAVING, DISTINCT or ORDER BY");
1806
1808
  const r = /* @__PURE__ */ new Set(), t = e.select.map((s, i) => {
1807
- const a = En(K(s) ?? `column_${i + 1}`, r);
1808
- return r.add(a), { ...s, alias: a };
1809
+ const l = Tn(K(s) ?? `column_${i + 1}`, r);
1810
+ return r.add(l), { ...s, alias: l };
1809
1811
  });
1810
1812
  return {
1811
1813
  from: e.from,
@@ -1817,23 +1819,23 @@ function Mt(e) {
1817
1819
  function Qs(e, n, r, t = {}) {
1818
1820
  const s = Xe(n);
1819
1821
  if (s) {
1820
- const { union: c, added: m } = Wn(e, s.query.union, r, t), d = { ...s, query: { ...s.query, union: c } }, f = n.select.length === 0 ? [] : [
1822
+ const { union: c, added: m } = Hn(e, s.query.union, r, t), d = { ...s, query: { ...s.query, union: c } }, f = n.select.length === 0 ? [] : [
1821
1823
  ...n.select,
1822
- ...m.map((p) => ({
1824
+ ...m.map((h) => ({
1823
1825
  id: W(),
1824
- expression: { type: "field", table: s.alias, column: p }
1826
+ expression: { type: "field", table: s.alias, column: h }
1825
1827
  }))
1826
1828
  ];
1827
1829
  return { ...n, from: d, select: f };
1828
1830
  }
1829
- const i = Mt(n), { union: a } = Wn(e, [i], r, t), o = t.alias ?? "stacked", l = a[0].select.map((c) => c.alias);
1831
+ const i = Ot(n), { union: l } = Hn(e, [i], r, t), a = t.alias ?? "stacked", o = l[0].select.map((c) => c.alias);
1830
1832
  return {
1831
1833
  id: n.id,
1832
- from: { id: W(), alias: o, query: { union: a, all: !0 } },
1834
+ from: { id: W(), alias: a, query: { union: l, all: !0 } },
1833
1835
  joins: [],
1834
- select: l.map((c) => ({
1836
+ select: o.map((c) => ({
1835
1837
  id: W(),
1836
- expression: { type: "field", table: o, column: c }
1838
+ expression: { type: "field", table: a, column: c }
1837
1839
  })),
1838
1840
  ...n.limit !== void 0 ? { limit: n.limit } : {},
1839
1841
  ...n.offset !== void 0 ? { offset: n.offset } : {}
@@ -1843,140 +1845,140 @@ function _s(e, n, r = {}) {
1843
1845
  const t = Xe(n);
1844
1846
  let s;
1845
1847
  try {
1846
- s = t ? t.query.union : [Mt(n)];
1848
+ s = t ? t.query.union : [Ot(n)];
1847
1849
  } catch {
1848
1850
  return [];
1849
1851
  }
1850
1852
  const i = Ke(e, s);
1851
- let a;
1853
+ let l;
1852
1854
  try {
1853
- a = It(s);
1855
+ l = Mt(s);
1854
1856
  } catch {
1855
1857
  return [];
1856
1858
  }
1857
- const o = /* @__PURE__ */ new Set([
1859
+ const a = /* @__PURE__ */ new Set([
1858
1860
  ...s.map(ie),
1859
- ...i.flatMap((l) => l.kind === "shared" ? l.tables : [])
1861
+ ...i.flatMap((o) => o.kind === "shared" ? o.tables : [])
1860
1862
  ]);
1861
- return e.tables.map((l) => l.name).filter((l) => !o.has(l) && Ct(e, l, i, a, r.unshared ?? "refuse") === void 0);
1863
+ return e.tables.map((o) => o.name).filter((o) => !a.has(o) && Ct(e, o, i, l, r.unshared ?? "refuse") === void 0);
1862
1864
  }
1863
1865
  function Or(e, n, r) {
1864
- const t = r.union.findIndex((l, c) => l.select !== n.union[c].select);
1866
+ const t = r.union.findIndex((o, c) => o.select !== n.union[c].select);
1865
1867
  if (t === -1) return r;
1866
- const s = r.union[t], i = n.union[t].select.map((l) => l.id), a = r.union.map((l, c) => {
1867
- if (c === t) return l;
1868
+ const s = r.union[t], i = n.union[t].select.map((o) => o.id), l = r.union.map((o, c) => {
1869
+ if (c === t) return o;
1868
1870
  const m = s.select.map((f) => {
1869
- const p = K(f), v = Fe(e, s, f.expression), $ = i.indexOf(f.id), k = $ === -1 ? void 0 : l.select[$];
1870
- if (!k)
1871
- return { id: W(), ...p ? { alias: p } : {}, expression: se(v) };
1872
- let b = k;
1873
- K(k) !== p && (b = { ...b, alias: p });
1874
- const { expression: I } = k;
1875
- return ke(I) && I.type === "literal" && I.as !== v && (b = { ...b, expression: se(v) }), b;
1871
+ const h = K(f), v = Fe(e, s, f.expression), $ = i.indexOf(f.id), T = $ === -1 ? void 0 : o.select[$];
1872
+ if (!T)
1873
+ return { id: W(), ...h ? { alias: h } : {}, expression: se(v) };
1874
+ let b = T;
1875
+ K(T) !== h && (b = { ...b, alias: h });
1876
+ const { expression: C } = T;
1877
+ return ke(C) && C.type === "literal" && C.as !== v && (b = { ...b, expression: se(v) }), b;
1876
1878
  });
1877
- return m.length === l.select.length && m.every((f, p) => f === l.select[p]) ? l : { ...l, select: m };
1878
- }), o = a.map((l) => {
1879
- const c = l.select.map((m, d) => {
1879
+ return m.length === o.select.length && m.every((f, h) => f === o.select[h]) ? o : { ...o, select: m };
1880
+ }), a = l.map((o) => {
1881
+ const c = o.select.map((m, d) => {
1880
1882
  const { expression: f } = m;
1881
1883
  if (f.type !== "literal" || f.value !== null) return m;
1882
- const p = a.find(($) => {
1884
+ const h = l.find(($) => {
1883
1885
  var b;
1884
- const k = (b = $.select[d]) == null ? void 0 : b.expression;
1885
- return k !== void 0 && !ke(k);
1886
- }), v = p && Fe(e, p, p.select[d].expression);
1886
+ const T = (b = $.select[d]) == null ? void 0 : b.expression;
1887
+ return T !== void 0 && !ke(T);
1888
+ }), v = h && Fe(e, h, h.select[d].expression);
1887
1889
  return v && f.as !== v ? { ...m, expression: se(v) } : m;
1888
1890
  });
1889
- return c.every((m, d) => m === l.select[d]) ? l : { ...l, select: c };
1891
+ return c.every((m, d) => m === o.select[d]) ? o : { ...o, select: c };
1890
1892
  });
1891
- return o.every((l, c) => l === r.union[c]) ? r : { ...r, union: o };
1893
+ return a.every((o, c) => o === r.union[c]) ? r : { ...r, union: a };
1892
1894
  }
1893
1895
  function Ur(e, n) {
1894
- const r = (i) => i.union[0].select.map((a) => K(a)), t = r(e), s = r(n);
1895
- return t.length !== s.length ? [] : t.flatMap((i, a) => {
1896
- const o = s[a];
1897
- return i && o && i !== o && !s.includes(i) && !t.includes(o) ? [{ from: i, to: o }] : [];
1896
+ const r = (i) => i.union[0].select.map((l) => K(l)), t = r(e), s = r(n);
1897
+ return t.length !== s.length ? [] : t.flatMap((i, l) => {
1898
+ const a = s[l];
1899
+ return i && a && i !== a && !s.includes(i) && !t.includes(a) ? [{ from: i, to: a }] : [];
1898
1900
  });
1899
1901
  }
1900
- function Ot(e, n, r) {
1902
+ function Ut(e, n, r) {
1901
1903
  let t = r;
1902
1904
  n && H(n) && H(t) && n.union.length === t.union.length && (t = Or(e, n, t));
1903
1905
  const s = t;
1904
- return We(s, (i, a) => Tn(
1906
+ return We(s, (i, l) => Nn(
1905
1907
  e,
1906
- n && H(n) === H(s) ? be(n)[a] : void 0,
1908
+ n && H(n) === H(s) ? be(n)[l] : void 0,
1907
1909
  i
1908
1910
  ));
1909
1911
  }
1910
- function Tn(e, n, r) {
1912
+ function Nn(e, n, r) {
1911
1913
  const t = (f) => {
1912
- var p;
1913
- return Ot(
1914
+ var h;
1915
+ return Ut(
1914
1916
  e,
1915
- n && ((p = oe(n).find((v) => v.id === f.id)) == null ? void 0 : p.query),
1917
+ n && ((h = oe(n).find((v) => v.id === f.id)) == null ? void 0 : h.query),
1916
1918
  f.query
1917
1919
  );
1918
- }, { from: s } = r, i = s.query && t(s), a = i && i !== s.query ? { ...s, query: i } : s;
1919
- let o = !1;
1920
- const l = r.joins.map((f) => {
1920
+ }, { from: s } = r, i = s.query && t(s), l = i && i !== s.query ? { ...s, query: i } : s;
1921
+ let a = !1;
1922
+ const o = r.joins.map((f) => {
1921
1923
  if (!f.query) return f;
1922
- const p = t(f);
1923
- return p === f.query ? f : (o = !0, { ...f, query: p });
1924
+ const h = t(f);
1925
+ return h === f.query ? f : (a = !0, { ...f, query: h });
1924
1926
  });
1925
- if (a === s && !o) return r;
1926
- const c = { ...r, from: a, joins: l }, m = oe(c).flatMap((f) => {
1927
+ if (l === s && !a) return r;
1928
+ const c = { ...r, from: l, joins: o }, m = oe(c).flatMap((f) => {
1927
1929
  var v;
1928
- const p = n && ((v = oe(n).find(($) => $.id === f.id)) == null ? void 0 : v.query);
1929
- return p && H(p) && H(f.query) ? Ur(p, f.query).map(($) => ({ ...$, alias: f.alias })) : [];
1930
+ const h = n && ((v = oe(n).find(($) => $.id === f.id)) == null ? void 0 : v.query);
1931
+ return h && H(h) && H(f.query) ? Ur(h, f.query).map(($) => ({ ...$, alias: f.alias })) : [];
1930
1932
  });
1931
1933
  if (m.length === 0) return c;
1932
1934
  const d = (f) => {
1933
1935
  if (f.type !== "field") return xe(f, d);
1934
- const p = m.find((v) => v.alias === f.table && v.from === f.column);
1935
- return p ? { ...f, column: p.to } : f;
1936
+ const h = m.find((v) => v.alias === f.table && v.from === f.column);
1937
+ return h ? { ...f, column: h.to } : f;
1936
1938
  };
1937
1939
  return Pe(c, d, (f) => f);
1938
1940
  }
1939
1941
  function jr(e, n, r) {
1940
1942
  if (!ue(r)) return r;
1941
1943
  const t = n && ue(n) ? n : void 0;
1942
- return dn(Tn(e, t, r), (s, i) => {
1943
- var a, o;
1944
- return Ot(
1944
+ return fn(Nn(e, t, r), (s, i) => {
1945
+ var l, a;
1946
+ return Ut(
1945
1947
  e,
1946
- (o = (a = t == null ? void 0 : t.shared) == null ? void 0 : a.find((l) => l.id === i.id)) == null ? void 0 : o.query,
1948
+ (a = (l = t == null ? void 0 : t.shared) == null ? void 0 : l.find((o) => o.id === i.id)) == null ? void 0 : a.query,
1947
1949
  s
1948
1950
  );
1949
1951
  });
1950
1952
  }
1951
- function Ut(e) {
1952
- return e.type === "field" ? [e] : fe(e).flatMap(Ut);
1953
+ function jt(e) {
1954
+ return e.type === "field" ? [e] : fe(e).flatMap(jt);
1953
1955
  }
1954
- const jt = (e, n, r) => Ut(e).some((t) => t.table === n && r.has(t.column ?? ""));
1956
+ const Lt = (e, n, r) => jt(e).some((t) => t.table === n && r.has(t.column ?? ""));
1955
1957
  function Lr(e, n, r) {
1956
- const t = n.query.union.filter((l, c) => c !== r), s = [
1958
+ const t = n.query.union.filter((o, c) => c !== r), s = [
1957
1959
  e.where,
1958
1960
  e.having,
1959
- ...e.joins.map((l) => l.on),
1960
- ...(e.groupBy ?? []).map((l) => l.expression),
1961
- ...(e.orderBy ?? []).map((l) => l.expression)
1962
- ].filter((l) => l !== void 0), i = (l) => s.some(
1963
- (c) => jt(c, n.alias, /* @__PURE__ */ new Set([l]))
1964
- ), a = new Set(t[0].select.flatMap((l, c) => {
1965
- const m = t.every((f) => f.select[c] && ke(f.select[c].expression)), d = K(l);
1961
+ ...e.joins.map((o) => o.on),
1962
+ ...(e.groupBy ?? []).map((o) => o.expression),
1963
+ ...(e.orderBy ?? []).map((o) => o.expression)
1964
+ ].filter((o) => o !== void 0), i = (o) => s.some(
1965
+ (c) => Lt(c, n.alias, /* @__PURE__ */ new Set([o]))
1966
+ ), l = new Set(t[0].select.flatMap((o, c) => {
1967
+ const m = t.every((f) => f.select[c] && ke(f.select[c].expression)), d = K(o);
1966
1968
  return m && !(d && i(d)) ? [c] : [];
1967
- })), o = new Set([...a].flatMap((l) => {
1968
- const c = K(t[0].select[l]);
1969
+ })), a = new Set([...l].flatMap((o) => {
1970
+ const c = K(t[0].select[o]);
1969
1971
  return c ? [c] : [];
1970
1972
  }));
1971
- return { at: a, names: o };
1973
+ return { at: l, names: a };
1972
1974
  }
1973
1975
  function Fs(e, n, r) {
1974
- const t = Ie(e, r);
1976
+ const t = Ce(e, r);
1975
1977
  if (!t || !t.query.union[n] || t.query.union.length < 2) return e;
1976
- const s = Lr(e, t, n), i = t.query.union.filter((c, m) => m !== n), a = s.at.size < i[0].select.length ? s : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, o = e.select.filter(
1977
- (c) => !jt(c.expression, t.alias, a.names)
1978
- ), l = i.map((c) => ({ ...c, select: c.select.filter((m, d) => !a.at.has(d)) }));
1979
- return Me({ ...e, select: o }, t, { ...t.query, union: l });
1978
+ const s = Lr(e, t, n), i = t.query.union.filter((c, m) => m !== n), l = s.at.size < i[0].select.length ? s : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, a = e.select.filter(
1979
+ (c) => !Lt(c.expression, t.alias, l.names)
1980
+ ), o = i.map((c) => ({ ...c, select: c.select.filter((m, d) => !l.at.has(d)) }));
1981
+ return Me({ ...e, select: a }, t, { ...t.query, union: o });
1980
1982
  }
1981
1983
  function Dr(e, n) {
1982
1984
  const r = /* @__PURE__ */ new Set([
@@ -1996,27 +1998,27 @@ function Js(e, n, r, t) {
1996
1998
  };
1997
1999
  }
1998
2000
  function Ps(e, n, r) {
1999
- const t = /* @__PURE__ */ new Set(), s = e.select.map(($, k) => {
2000
- const b = En(K($) ?? `column_${k + 1}`, t);
2001
+ const t = /* @__PURE__ */ new Set(), s = e.select.map(($, T) => {
2002
+ const b = Tn(K($) ?? `column_${T + 1}`, t);
2001
2003
  return t.add(b), { ...$, alias: b };
2002
2004
  }), {
2003
2005
  from: i,
2004
- joins: a,
2005
- where: o,
2006
- groupBy: l,
2006
+ joins: l,
2007
+ where: a,
2008
+ groupBy: o,
2007
2009
  having: c,
2008
2010
  distinct: m
2009
2011
  } = e, d = {
2010
2012
  from: i,
2011
- joins: a,
2013
+ joins: l,
2012
2014
  select: s,
2013
- ...o ? { where: o } : {},
2014
- ...l ? { groupBy: l } : {},
2015
+ ...a ? { where: a } : {},
2016
+ ...o ? { groupBy: o } : {},
2015
2017
  ...c ? { having: c } : {},
2016
2018
  ...m !== void 0 ? { distinct: m } : {}
2017
- }, f = ($) => ({ type: "field", table: r, column: $ }), p = ($) => {
2018
- var k;
2019
- return (k = s.find((b) => Z(b.expression, $))) == null ? void 0 : k.alias;
2019
+ }, f = ($) => ({ type: "field", table: r, column: $ }), h = ($) => {
2020
+ var T;
2021
+ return (T = s.find((b) => Z(b.expression, $))) == null ? void 0 : T.alias;
2020
2022
  }, v = {
2021
2023
  ...e,
2022
2024
  from: { id: n, alias: r, query: { union: [d], all: !0 } },
@@ -2024,20 +2026,20 @@ function Ps(e, n, r) {
2024
2026
  select: s.map(($) => ({ id: W(), expression: f($.alias) })),
2025
2027
  ...e.orderBy ? {
2026
2028
  orderBy: e.orderBy.map(($) => {
2027
- const k = p($.expression);
2028
- return k ? { ...$, expression: f(k) } : $;
2029
+ const T = h($.expression);
2030
+ return T ? { ...$, expression: f(T) } : $;
2029
2031
  })
2030
2032
  } : {}
2031
2033
  };
2032
2034
  return ["where", "groupBy", "having", "distinct"].forEach(($) => delete v[$]), v;
2033
2035
  }
2034
2036
  function Vs(e, n, r, t) {
2035
- const s = Ie(n, r);
2037
+ const s = Ce(n, r);
2036
2038
  if (!s) return n;
2037
- const i = Ke(e, s.query.union).map((a) => ({
2039
+ const i = Ke(e, s.query.union).map((l) => ({
2038
2040
  id: W(),
2039
- ...a.name ? { alias: a.name } : {},
2040
- expression: se(a.type)
2041
+ ...l.name ? { alias: l.name } : {},
2042
+ expression: se(l.type)
2041
2043
  }));
2042
2044
  return Me(n, s, {
2043
2045
  ...s.query,
@@ -2051,44 +2053,44 @@ function Ws(e, n, r, t) {
2051
2053
  joinType: "left",
2052
2054
  query: { union: [{ from: t, joins: [], select: [] }], all: !0 }
2053
2055
  };
2054
- return { ...n, joins: [...n.joins, { ...s, on: dt(e, n, s) }] };
2056
+ return { ...n, joins: [...n.joins, { ...s, on: ft(e, n, s) }] };
2055
2057
  }
2056
2058
  function Hs(e, n, r) {
2057
- const t = Ie(e, n);
2059
+ const t = Ce(e, n);
2058
2060
  return !t || t.query.all === r ? e : Me(e, t, { ...t.query, all: r });
2059
2061
  }
2060
2062
  function Xs(e, n, r, t, s) {
2061
- const i = Ie(n, r), [a, ...o] = (i == null ? void 0 : i.query.union) ?? [], l = a == null ? void 0 : a.select[t], c = s.trim();
2062
- if (!i || !l || !c || c.length > Y || a.select.some((d) => K(d) === c)) return n;
2063
+ const i = Ce(n, r), [l, ...a] = (i == null ? void 0 : i.query.union) ?? [], o = l == null ? void 0 : l.select[t], c = s.trim();
2064
+ if (!i || !o || !c || c.length > Y || l.select.some((d) => K(d) === c)) return n;
2063
2065
  const m = Me(n, i, {
2064
2066
  ...i.query,
2065
2067
  union: [
2066
- { ...a, select: a.select.map((d, f) => f === t ? { ...d, alias: c } : d) },
2067
- ...o
2068
+ { ...l, select: l.select.map((d, f) => f === t ? { ...d, alias: c } : d) },
2069
+ ...a
2068
2070
  ]
2069
2071
  });
2070
- return Tn(e, n, m);
2072
+ return Nn(e, n, m);
2071
2073
  }
2072
2074
  function Ks(e, n) {
2073
2075
  var m;
2074
- const r = Ie(e, n);
2076
+ const r = Ce(e, n);
2075
2077
  if (!r || r.query.union.length !== 1) return e;
2076
2078
  const [t] = r.query.union, s = new Map(t.select.flatMap((d) => {
2077
2079
  const f = K(d);
2078
2080
  return f ? [[f, d.expression]] : [];
2079
- })), i = (d) => d.type === "field" && d.table === r.alias && s.has(d.column ?? ""), a = (d) => fn(d, (f) => i(f) ? s.get(f.column) : f), o = e.select.map((d) => {
2080
- const f = a(d.expression);
2081
+ })), i = (d) => d.type === "field" && d.table === r.alias && s.has(d.column ?? ""), l = (d) => mn(d, (f) => i(f) ? s.get(f.column) : f), a = e.select.map((d) => {
2082
+ const f = l(d.expression);
2081
2083
  if (d.alias || !i(d.expression)) return { ...d, expression: f };
2082
- const p = d.expression.column;
2083
- return K({ ...d, expression: f }) === p ? { ...d, expression: f } : { ...d, expression: f, alias: p };
2084
- }), l = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((d) => le(d.expression, $e));
2085
- if (r === e.from && !l) {
2086
- const d = Pe({ ...e, select: o }, a, (f) => f);
2084
+ const h = d.expression.column;
2085
+ return K({ ...d, expression: f }) === h ? { ...d, expression: f } : { ...d, expression: f, alias: h };
2086
+ }), o = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((d) => le(d.expression, $e));
2087
+ if (r === e.from && !o) {
2088
+ const d = Pe({ ...e, select: a }, l, (f) => f);
2087
2089
  return {
2088
2090
  ...d,
2089
2091
  from: t.from,
2090
2092
  joins: [...t.joins, ...d.joins],
2091
- where: Bt([...Je(t.where), ...Je(d.where)]),
2093
+ where: It([...Je(t.where), ...Je(d.where)]),
2092
2094
  // Nothing picked: the query's own columns, as the union had them.
2093
2095
  select: d.select.length > 0 ? d.select : t.select
2094
2096
  };
@@ -2097,22 +2099,22 @@ function Ks(e, n) {
2097
2099
  return r === e.from && c ? {
2098
2100
  ...e,
2099
2101
  ...t,
2100
- select: o.length > 0 ? o : t.select,
2102
+ select: a.length > 0 ? a : t.select,
2101
2103
  ...e.orderBy ? {
2102
- orderBy: e.orderBy.map((d) => ({ ...d, expression: a(d.expression) }))
2104
+ orderBy: e.orderBy.map((d) => ({ ...d, expression: l(d.expression) }))
2103
2105
  } : {}
2104
2106
  } : Me(e, r, t);
2105
2107
  }
2106
- const Hn = (e, n) => e.type === "binary" && e.operator === "IS NULL" && Z(e.left, n);
2107
- function ln(e) {
2108
+ const Xn = (e, n) => e.type === "binary" && e.operator === "IS NULL" && Z(e.left, n);
2109
+ function un(e) {
2108
2110
  var n;
2109
2111
  if (!e) return [];
2110
- if (e.type === "logical" && e.operator === "AND") return e.operands.flatMap(ln);
2112
+ if (e.type === "logical" && e.operator === "AND") return e.operands.flatMap(un);
2111
2113
  if (e.type === "binary" && (e.operator ?? "=") === "=" && e.left.type === "field" && ((n = e.right) == null ? void 0 : n.type) === "field")
2112
2114
  return [[e.left, e.right]];
2113
2115
  if (e.type === "logical" && e.operator === "OR" && e.operands.length === 2) {
2114
- const [[r] = [], t] = [ln(e.operands[0]), e.operands[1]];
2115
- if (r && t.type === "logical" && t.operator === "AND" && t.operands.length === 2 && Hn(t.operands[0], r[0]) && Hn(t.operands[1], r[1]))
2116
+ const [[r] = [], t] = [un(e.operands[0]), e.operands[1]];
2117
+ if (r && t.type === "logical" && t.operator === "AND" && t.operands.length === 2 && Xn(t.operands[0], r[0]) && Xn(t.operands[1], r[1]))
2116
2118
  return [r];
2117
2119
  }
2118
2120
  return [];
@@ -2123,62 +2125,62 @@ function Gr(e, n) {
2123
2125
  }
2124
2126
  function Rr(e, n) {
2125
2127
  const r = D(e.from), t = /* @__PURE__ */ new Map([[r, e.from.table]]), s = [r], i = e.shared ?? [];
2126
- return e.joins.map((a) => {
2127
- var f, p;
2128
- const { id: o } = a, l = Te(a, i), c = D(a);
2129
- t.set(c, a.table);
2130
- const m = ln(a.on).flatMap(([v, $]) => [[v, $], [$, v]]).filter(([v, $]) => v.table === c && s.includes($.table ?? ""));
2131
- if (s.push(c), a.joinType === "cross")
2128
+ return e.joins.map((l) => {
2129
+ var f, h;
2130
+ const { id: a } = l, o = Te(l, i), c = D(l);
2131
+ t.set(c, l.table);
2132
+ const m = un(l.on).flatMap(([v, $]) => [[v, $], [$, v]]).filter(([v, $]) => v.table === c && s.includes($.table ?? ""));
2133
+ if (s.push(c), l.joinType === "cross")
2132
2134
  return {
2133
- joinId: o,
2135
+ joinId: a,
2134
2136
  key: c,
2135
2137
  parent: r,
2136
- side: l && lt(l) ? "one" : "cross"
2138
+ side: o && ut(o) ? "one" : "cross"
2137
2139
  };
2138
- if (a.table === void 0) {
2139
- const v = m.length > 0 && l !== void 0 && !H(l) && Gr(l, m.map(([$]) => $.column));
2140
+ if (l.table === void 0) {
2141
+ const v = m.length > 0 && o !== void 0 && !H(o) && Gr(o, m.map(([$]) => $.column));
2140
2142
  return {
2141
- joinId: o,
2143
+ joinId: a,
2142
2144
  key: c,
2143
2145
  parent: ((f = m[0]) == null ? void 0 : f[1].table) ?? r,
2144
2146
  side: v ? "one" : "unknown"
2145
2147
  };
2146
2148
  }
2147
2149
  return m.map(([v, $]) => {
2148
- const k = t.get($.table), b = (I, G) => n.relations.some((R) => R.fromTable === I[0] && R.fromColumn === I[1].column && R.toTable === G[0] && R.toColumn === G[1].column);
2149
- if (b([a.table, v], [k, $]))
2150
+ const T = t.get($.table), b = (C, G) => n.relations.some((R) => R.fromTable === C[0] && R.fromColumn === C[1].column && R.toTable === G[0] && R.toColumn === G[1].column);
2151
+ if (b([l.table, v], [T, $]))
2150
2152
  return {
2151
- joinId: o,
2153
+ joinId: a,
2152
2154
  key: c,
2153
2155
  parent: $.table,
2154
2156
  side: "many"
2155
2157
  };
2156
- if (b([k, $], [a.table, v]))
2158
+ if (b([T, $], [l.table, v]))
2157
2159
  return {
2158
- joinId: o,
2160
+ joinId: a,
2159
2161
  key: c,
2160
2162
  parent: $.table,
2161
2163
  side: "one"
2162
2164
  };
2163
2165
  }).find((v) => v !== void 0) ?? {
2164
- joinId: o,
2166
+ joinId: a,
2165
2167
  key: c,
2166
- parent: ((p = m[0]) == null ? void 0 : p[1].table) ?? r,
2168
+ parent: ((h = m[0]) == null ? void 0 : h[1].table) ?? r,
2167
2169
  side: "unknown"
2168
2170
  };
2169
2171
  });
2170
2172
  }
2171
- function un(e, n, r = {}) {
2172
- const t = Rr(e, n).map((o) => o.side === "unknown" && r.unknown === "repeat" ? { ...o, side: "many" } : o), s = [D(e.from), ...t.map((o) => o.key)], i = (o) => {
2173
- const l = /* @__PURE__ */ new Set([o]);
2173
+ function cn(e, n, r = {}) {
2174
+ const t = Rr(e, n).map((a) => a.side === "unknown" && r.unknown === "repeat" ? { ...a, side: "many" } : a), s = [D(e.from), ...t.map((a) => a.key)], i = (a) => {
2175
+ const o = /* @__PURE__ */ new Set([a]);
2174
2176
  return t.forEach((c) => {
2175
- l.has(c.parent) && l.add(c.key);
2176
- }), l;
2177
- }, a = /* @__PURE__ */ new Set();
2178
- return t.forEach((o) => {
2179
- const l = i(o.key);
2180
- o.side === "many" && s.filter((c) => !l.has(c)).forEach((c) => a.add(c)), o.side === "one" && l.forEach((c) => a.add(c)), o.side === "cross" && s.forEach((c) => a.add(c));
2181
- }), a;
2177
+ o.has(c.parent) && o.add(c.key);
2178
+ }), o;
2179
+ }, l = /* @__PURE__ */ new Set();
2180
+ return t.forEach((a) => {
2181
+ const o = i(a.key);
2182
+ a.side === "many" && s.filter((c) => !o.has(c)).forEach((c) => l.add(c)), a.side === "one" && o.forEach((c) => l.add(c)), a.side === "cross" && s.forEach((c) => l.add(c));
2183
+ }), l;
2182
2184
  }
2183
2185
  const qr = /* @__PURE__ */ new Set(["SUM", "AVG", "COUNT"]);
2184
2186
  function Qr(e, n) {
@@ -2186,22 +2188,22 @@ function Qr(e, n) {
2186
2188
  }
2187
2189
  function Ys(e, n) {
2188
2190
  const r = D(e.from);
2189
- return e.joins.some((s) => s.joinType === "right" || s.joinType === "full") || un(e, n).has(r) ? "many" : un(e, n, { unknown: "repeat" }).has(r) ? "unknown" : "one";
2191
+ return e.joins.some((s) => s.joinType === "right" || s.joinType === "full") || cn(e, n).has(r) ? "many" : cn(e, n, { unknown: "repeat" }).has(r) ? "unknown" : "one";
2190
2192
  }
2191
- function Lt(e, n, r = !1) {
2193
+ function Dt(e, n, r = !1) {
2192
2194
  const t = e.type === "function" && qr.has(e.name.toUpperCase()) && !e.distinct, s = r || t;
2193
- return e.type === "field" ? s && e.table && n.has(e.table) ? [e.table] : [] : fe(e).flatMap((i) => Lt(i, n, s));
2195
+ return e.type === "field" ? s && e.table && n.has(e.table) ? [e.table] : [] : fe(e).flatMap((i) => Dt(i, n, s));
2194
2196
  }
2195
2197
  function _r(e, n) {
2196
- const r = un(e, n), t = /* @__PURE__ */ new Set(), s = (l) => {
2197
- const c = l ? Lt(l, r) : [];
2198
+ const r = cn(e, n), t = /* @__PURE__ */ new Set(), s = (o) => {
2199
+ const c = o ? Dt(o, r) : [];
2198
2200
  return c.forEach((m) => t.add(m)), c.length > 0;
2199
- }, i = e.select.filter((l) => s(l.expression)).map((l) => l.id), a = (e.orderBy ?? []).filter((l) => s(l.expression)).map((l) => l.id), o = s(e.having);
2201
+ }, i = e.select.filter((o) => s(o.expression)).map((o) => o.id), l = (e.orderBy ?? []).filter((o) => s(o.expression)).map((o) => o.id), a = s(e.having);
2200
2202
  return {
2201
2203
  tables: [...t],
2202
2204
  select: i,
2203
- orderBy: a,
2204
- having: o
2205
+ orderBy: l,
2206
+ having: a
2205
2207
  };
2206
2208
  }
2207
2209
  function Fr(e, n) {
@@ -2214,38 +2216,38 @@ function Ee(e) {
2214
2216
  const { fromSelect: n, ...r } = e;
2215
2217
  return n === void 0 ? e : r;
2216
2218
  }
2217
- function Dt(e, n) {
2219
+ function Gt(e, n) {
2218
2220
  const r = (s) => typeof s.fromSelect == "string" ? n(s, s.fromSelect) : xe(s, r), t = r(e);
2219
2221
  return Z(t, e) ? e : t;
2220
2222
  }
2221
2223
  function Pr(e, n) {
2222
- var o;
2223
- const r = new Map(Fr(e, n).map((l) => [l.id, l])), t = (l) => Dt(l, (c, m) => {
2224
+ var a;
2225
+ const r = new Map(Fr(e, n).map((o) => [o.id, o])), t = (o) => Gt(o, (c, m) => {
2224
2226
  const d = r.get(m);
2225
2227
  return d ? Jr(d) : Ee(c);
2226
2228
  }), s = e.having && t(e.having);
2227
2229
  let i = !1;
2228
- const a = (o = e.orderBy) == null ? void 0 : o.map((l) => {
2229
- const c = t(l.expression);
2230
- return c === l.expression ? l : (i = !0, { ...l, expression: c });
2230
+ const l = (a = e.orderBy) == null ? void 0 : a.map((o) => {
2231
+ const c = t(o.expression);
2232
+ return c === o.expression ? o : (i = !0, { ...o, expression: c });
2231
2233
  });
2232
- return s === e.having && !i ? e : { ...e, having: s, ...a ? { orderBy: a } : {} };
2234
+ return s === e.having && !i ? e : { ...e, having: s, ...l ? { orderBy: l } : {} };
2233
2235
  }
2234
2236
  function zs(e, n) {
2235
2237
  const r = new Set((n.groupBy ?? []).flatMap(({ expression: t }) => t.type === "field" && t.column ? [t.column.toLowerCase()] : []));
2236
- return Dt(e, (t, s) => {
2237
- const i = n.select.find((a) => a.id === s);
2238
+ return Gt(e, (t, s) => {
2239
+ const i = n.select.find((l) => l.id === s);
2238
2240
  return !(i != null && i.alias) || r.has(i.alias.toLowerCase()) || !Z(Ee(t), i.expression) ? Ee(t) : { type: "field", column: i.alias };
2239
2241
  });
2240
2242
  }
2241
- function cn(e, n) {
2242
- const r = (m) => cn(m, n), t = (m) => We(m, r), s = e.from.query && t(e.from.query), i = s && s !== e.from.query ? { ...e.from, query: s } : e.from;
2243
- let a = !1;
2244
- const o = e.joins.map((m) => {
2243
+ function dn(e, n) {
2244
+ const r = (m) => dn(m, n), t = (m) => We(m, r), s = e.from.query && t(e.from.query), i = s && s !== e.from.query ? { ...e.from, query: s } : e.from;
2245
+ let l = !1;
2246
+ const a = e.joins.map((m) => {
2245
2247
  if (!m.query) return m;
2246
2248
  const d = t(m.query);
2247
- return d === m.query ? m : (a = !0, { ...m, query: d });
2248
- }), l = i === e.from && !a ? e : { ...e, from: i, joins: o }, c = st(l, (m) => {
2249
+ return d === m.query ? m : (l = !0, { ...m, query: d });
2250
+ }), o = i === e.from && !l ? e : { ...e, from: i, joins: a }, c = it(o, (m) => {
2249
2251
  const d = r(m.query);
2250
2252
  return d === m.query ? m : { ...m, query: d };
2251
2253
  });
@@ -2253,15 +2255,16 @@ function cn(e, n) {
2253
2255
  }
2254
2256
  function Zs(e, n, r, t = $e) {
2255
2257
  if (!ue(r)) return r;
2256
- const s = dn(
2257
- cn(r, t),
2258
- (a) => We(a, (o) => cn(o, t))
2258
+ const s = fn(
2259
+ dn(r, t),
2260
+ (l) => We(l, (a) => dn(a, t))
2259
2261
  ), i = jr(e, n, s);
2260
2262
  return Ar(e, n, i);
2261
2263
  }
2262
2264
  const Vr = [
2263
2265
  "=",
2264
2266
  "!=",
2267
+ "IS NOT DISTINCT FROM",
2265
2268
  "<",
2266
2269
  "<=",
2267
2270
  ">",
@@ -2279,43 +2282,49 @@ function Hr(e) {
2279
2282
  function Xr(e) {
2280
2283
  return !e.distinct || !e.orderBy ? [] : e.orderBy.filter(({ expression: n }) => !(n.type === "field" && !n.column)).filter(({ expression: n }) => e.select.length === 0 ? n.type !== "field" : !e.select.some((r) => Z(r.expression, Ee(n)))).map(({ id: n }) => n);
2281
2284
  }
2282
- function Gt(e, n) {
2283
- n(e), fe(e).forEach((r) => Gt(r, n));
2284
- }
2285
2285
  const Rt = (e) => {
2286
2286
  const n = /* @__PURE__ */ new Map();
2287
- return et(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;
2287
+ return nt(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;
2288
2288
  };
2289
2289
  function qt(e, n) {
2290
- const r = /* @__PURE__ */ new Map(), t = (i, a) => Gt(i, (o) => {
2291
- if (o.type === "param" && !r.has(o.name) && r.set(o.name, /* @__PURE__ */ new Set()), o.type !== "binary" || !o.right) return;
2292
- const l = (m, d) => {
2293
- const f = a(d);
2294
- m.type !== "param" || !f || (r.has(m.name) || r.set(m.name, /* @__PURE__ */ new Set()), r.get(m.name).add(f));
2295
- };
2296
- l(o.left, o.right), (o.right.type === "list" ? o.right.items : [o.right]).forEach((m) => l(m, o.left));
2297
- }), s = (i) => {
2298
- const a = /* @__PURE__ */ new Map();
2299
- Q(i).forEach((l) => a.set(D(l), l.table));
2300
- const o = (l) => {
2301
- var d;
2302
- if (!n || l.type !== "field" || !l.table || !l.column) return;
2303
- const c = a.has(l.table) ? a.get(l.table) : l.table, m = c === void 0 ? void 0 : z(n, c);
2304
- return (d = m == null ? void 0 : m.columns.find((f) => f.name === l.column)) == null ? void 0 : d.type;
2305
- };
2306
- Xn(i).forEach((l) => t(l, o)), Q(i).forEach((l) => l.query && be(l.query).forEach(s)), rt(i).forEach((l) => s(l.query));
2290
+ const r = /* @__PURE__ */ new Map(), t = (o) => (r.has(o) || r.set(o, { types: /* @__PURE__ */ new Set(), columns: /* @__PURE__ */ new Map() }), r.get(o)), s = (o, c) => {
2291
+ const m = new Map(o);
2292
+ return Q(c).forEach((d) => m.set(D(d), d.table)), m;
2293
+ }, i = (o, c) => {
2294
+ if (!n || c.type !== "field" || !c.table || !c.column) return;
2295
+ const m = o.has(c.table) ? o.get(c.table) : c.table, d = m === void 0 ? void 0 : z(n, m), f = d == null ? void 0 : d.columns.find((h) => h.name === c.column);
2296
+ return d && f ? { table: d.name, column: f } : void 0;
2297
+ }, l = (o, c) => {
2298
+ if (o.type === "param" && t(o.name), o.type === "binary" && o.right) {
2299
+ const d = o.operator === "LIKE" || o.operator === "REGEXP", f = (v, $) => {
2300
+ const T = i(c, $);
2301
+ if (v.type !== "param" || !T) return;
2302
+ const b = t(v.name);
2303
+ if (b.types.add(T.column.type), !d) {
2304
+ const C = `${T.table}.${T.column.name}`;
2305
+ b.columns.set(C, { table: T.table, column: T.column.name });
2306
+ }
2307
+ };
2308
+ f(o.left, o.right), (o.right.type === "list" ? o.right.items : [o.right]).forEach((v) => f(v, o.left));
2309
+ }
2310
+ const m = o.type === "exists" ? s(c, o.query) : c;
2311
+ fe(o).forEach((d) => l(d, m));
2312
+ }, a = (o) => {
2313
+ const c = s(/* @__PURE__ */ new Map(), o);
2314
+ Kn(o).forEach((m) => l(m, c)), Q(o).forEach((m) => m.query && be(m.query).forEach(a)), st(o).forEach((m) => a(m.query));
2307
2315
  };
2308
- return (e.shared ?? []).forEach((i) => be(i.query).forEach(s)), s(e), r;
2316
+ return (e.shared ?? []).forEach((o) => be(o.query).forEach(a)), a(e), r;
2309
2317
  }
2310
2318
  function ei(e, n) {
2311
2319
  const r = Rt(e);
2312
- return [...qt(e, n)].map(([t, s]) => {
2313
- const i = r.get(t);
2320
+ return [...qt(e, n)].map(([t, { types: s, columns: i }]) => {
2321
+ const l = r.get(t);
2314
2322
  return {
2315
2323
  name: t,
2316
2324
  ...s.size === 1 ? { type: [...s][0] } : {},
2317
2325
  // (Two different ones: validateQuery reports it; the first here.)
2318
- ...i ? { relative: JSON.parse([...i][0]) } : {}
2326
+ ...l ? { relative: JSON.parse([...l][0]) } : {},
2327
+ ...i.size === 1 ? { column: [...i.values()][0] } : {}
2319
2328
  };
2320
2329
  });
2321
2330
  }
@@ -2323,11 +2332,11 @@ function Kr(e) {
2323
2332
  return [...Rt(e)].filter(([, n]) => n.size > 1).map(([n]) => n);
2324
2333
  }
2325
2334
  function Yr(e, n) {
2326
- return [...qt(e, n)].filter(([, r]) => r.size > 1).map(([r]) => r);
2335
+ return [...qt(e, n)].filter(([, r]) => r.types.size > 1).map(([r]) => r);
2327
2336
  }
2328
2337
  function ni(e, n) {
2329
2338
  const r = new Map(Object.entries(n));
2330
- return et(e, (t) => {
2339
+ return nt(e, (t) => {
2331
2340
  if (t.type !== "param") return t;
2332
2341
  if (!r.has(t.name)) throw new Error(`no value for parameter "${t.name}"`);
2333
2342
  const s = r.get(t.name);
@@ -2337,66 +2346,66 @@ function ni(e, n) {
2337
2346
  function zr(e, n, r, t) {
2338
2347
  const s = new Map(t);
2339
2348
  return Q(n).forEach((i) => {
2340
- var o;
2341
- const a = i.table !== void 0 ? (o = z(e, i.table)) == null ? void 0 : o.columns : (() => {
2342
- const l = Te(i, r);
2343
- return l && Ne(e, l, r);
2349
+ var a;
2350
+ const l = i.table !== void 0 ? (a = z(e, i.table)) == null ? void 0 : a.columns : (() => {
2351
+ const o = Te(i, r);
2352
+ return o && Ne(e, o, r);
2344
2353
  })();
2345
- s.set(D(i), new Set((a ?? []).filter((l) => l.joinOnly).map((l) => l.name)));
2354
+ s.set(D(i), new Set((l ?? []).filter((o) => o.joinOnly).map((o) => o.name)));
2346
2355
  }), s;
2347
2356
  }
2348
2357
  function Zr(e, n) {
2349
- const r = [], t = n.shared ?? [], s = (o, l, c, m) => {
2358
+ const r = [], t = n.shared ?? [], s = (a, o, c, m) => {
2350
2359
  var f;
2351
- if (Array.isArray(o)) {
2352
- o.forEach((p, v) => s(p, [...l, v], c, m));
2360
+ if (Array.isArray(a)) {
2361
+ a.forEach((h, v) => s(h, [...o, v], c, m));
2353
2362
  return;
2354
2363
  }
2355
- if (typeof o != "object" || o === null || o instanceof Date) return;
2356
- const d = o;
2364
+ if (typeof a != "object" || a === null || a instanceof Date) return;
2365
+ const d = a;
2357
2366
  if (d.type === "field") {
2358
- const { table: p, column: v } = d;
2359
- !m && p && v && ((f = c.get(p)) != null && f.has(v)) && r.push(l);
2367
+ const { table: h, column: v } = d;
2368
+ !m && h && v && ((f = c.get(h)) != null && f.has(v)) && r.push(o);
2360
2369
  return;
2361
2370
  }
2362
2371
  if (d.type === "subquery") {
2363
- a(d.query, [...l, "query"], !0);
2372
+ l(d.query, [...o, "query"], !0);
2364
2373
  return;
2365
2374
  }
2366
2375
  if (d.type === "exists") {
2367
- const p = d.query;
2368
- a(p, [...l, "query"], !1, c);
2376
+ const h = d.query;
2377
+ l(h, [...o, "query"], !1, c);
2369
2378
  return;
2370
2379
  }
2371
- Object.entries(d).forEach(([p, v]) => s(v, [...l, p], c, m));
2372
- }, i = (o, l) => {
2373
- "union" in o ? be(o).forEach((c, m) => a(c, [...l, "union", m], !0)) : a(o, l, !0);
2380
+ Object.entries(d).forEach(([h, v]) => s(v, [...o, h], c, m));
2381
+ }, i = (a, o) => {
2382
+ "union" in a ? be(a).forEach((c, m) => l(c, [...o, "union", m], !0)) : l(a, o, !0);
2374
2383
  };
2375
- function a(o, l, c, m) {
2376
- var p, v, $;
2377
- const d = zr(e, o, t, m);
2378
- o.from.query && i(o.from.query, [...l, "from", "query"]), o.joins.forEach((k, b) => {
2379
- k.query && i(k.query, [...l, "joins", b, "query"]), k.on && s(k.on, [...l, "joins", b, "on"], d, !0);
2380
- }), o.where && s(o.where, [...l, "where"], d, !1);
2381
- const f = o;
2382
- (p = f.select) == null || p.forEach((k, b) => s(
2383
- k.expression,
2384
- [...l, "select", b, "expression"],
2384
+ function l(a, o, c, m) {
2385
+ var h, v, $;
2386
+ const d = zr(e, a, t, m);
2387
+ a.from.query && i(a.from.query, [...o, "from", "query"]), a.joins.forEach((T, b) => {
2388
+ T.query && i(T.query, [...o, "joins", b, "query"]), T.on && s(T.on, [...o, "joins", b, "on"], d, !0);
2389
+ }), a.where && s(a.where, [...o, "where"], d, !1);
2390
+ const f = a;
2391
+ (h = f.select) == null || h.forEach((T, b) => s(
2392
+ T.expression,
2393
+ [...o, "select", b, "expression"],
2385
2394
  d,
2386
2395
  c
2387
- )), (v = f.groupBy) == null || v.forEach((k, b) => s(
2388
- k.expression,
2389
- [...l, "groupBy", b, "expression"],
2396
+ )), (v = f.groupBy) == null || v.forEach((T, b) => s(
2397
+ T.expression,
2398
+ [...o, "groupBy", b, "expression"],
2390
2399
  d,
2391
2400
  c
2392
- )), f.having && s(f.having, [...l, "having"], d, !1), ($ = f.orderBy) == null || $.forEach((k, b) => s(
2393
- k.expression,
2394
- [...l, "orderBy", b, "expression"],
2401
+ )), f.having && s(f.having, [...o, "having"], d, !1), ($ = f.orderBy) == null || $.forEach((T, b) => s(
2402
+ T.expression,
2403
+ [...o, "orderBy", b, "expression"],
2395
2404
  d,
2396
2405
  !1
2397
2406
  ));
2398
2407
  }
2399
- return t.forEach((o, l) => i(o.query, ["shared", l, "query"])), a(n, [], !1), r;
2408
+ return t.forEach((a, o) => i(a.query, ["shared", o, "query"])), l(n, [], !1), r;
2400
2409
  }
2401
2410
  function ti(e, n) {
2402
2411
  return !(e === "mssql" && n === "REGEXP");
@@ -2464,13 +2473,13 @@ const De = (e) => e === "integer" ? "number" : e === "dateTime" ? "date" : e, de
2464
2473
  dateTime: "a date and time",
2465
2474
  time: "a time of day",
2466
2475
  boolean: "yes / no"
2467
- }, O = (e) => typeof e == "object" && e !== null && !Array.isArray(e), q = (e) => typeof e == "string", ts = ["day", "week", "month", "year"], rs = (e) => O(e) && ts.includes(e.unit) && Number.isSafeInteger(e.offset) && (e.anchor === void 0 || e.unit !== "day" && (e.anchor === "start" || e.anchor === "end")), ge = (e) => e === void 0 || q(e), ss = (e) => e === null || q(e) || typeof e == "boolean" || typeof e == "bigint" || typeof e == "number" && Number.isFinite(e) || e instanceof Date && !Number.isNaN(e.getTime()), en = (e) => O(e) && e.type === "literal" && e.value === "" && e.empty !== !0;
2476
+ }, O = (e) => typeof e == "object" && e !== null && !Array.isArray(e), q = (e) => typeof e == "string", ts = ["day", "week", "month", "year"], rs = (e) => O(e) && ts.includes(e.unit) && Number.isSafeInteger(e.offset) && (e.anchor === void 0 || e.unit !== "day" && (e.anchor === "start" || e.anchor === "end")), ge = (e) => e === void 0 || q(e), ss = (e) => e === null || q(e) || typeof e == "boolean" || typeof e == "bigint" || typeof e == "number" && Number.isFinite(e) || e instanceof Date && !Number.isNaN(e.getTime()), nn = (e) => O(e) && e.type === "literal" && e.value === "" && e.empty !== !0;
2468
2477
  function ii(e, n, r = {}) {
2469
- var xn, Bn;
2470
- const t = [], s = (u, y, g, h = "error") => {
2478
+ var Bn, In;
2479
+ const t = [], s = (u, y, g, p = "error") => {
2471
2480
  t.push({
2472
2481
  kind: u,
2473
- severity: h,
2482
+ severity: p,
2474
2483
  path: y,
2475
2484
  pathSegments: ns(y),
2476
2485
  message: g
@@ -2479,28 +2488,28 @@ function ii(e, n, r = {}) {
2479
2488
  if (!O(n) || !q(n.id) || n.id === "")
2480
2489
  return s("invalidStructure", "", "a query is an object with a non-empty string id"), t;
2481
2490
  if (!("from" in n)) return t;
2482
- const i = Ht(e, r.tableFilter, r.columnFilter), a = Xt(r.functions ?? $e);
2483
- let o = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), c;
2484
- const m = (u, y, g, h) => {
2485
- q(u) && u.length > y && s("tooLong", g, `${h} is longer than ${y} characters`);
2486
- }, d = /* @__PURE__ */ new Set(), f = O(n) && Array.isArray(n.shared) ? n.shared : [], p = new Set(f.flatMap((u) => O(u) && q(u.id) ? [u.id] : [])), v = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Set();
2487
- let k;
2491
+ const i = Ht(e, r.tableFilter, r.columnFilter), l = Xt(r.functions ?? $e);
2492
+ let a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c;
2493
+ const m = (u, y, g, p) => {
2494
+ q(u) && u.length > y && s("tooLong", g, `${p} is longer than ${y} characters`);
2495
+ }, d = /* @__PURE__ */ new Set(), f = O(n) && Array.isArray(n.shared) ? n.shared : [], h = new Set(f.flatMap((u) => O(u) && q(u.id) ? [u.id] : [])), v = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Set();
2496
+ let T;
2488
2497
  const b = (u, y) => {
2489
- o.has(u) && s("duplicateTableName", y, `two tables named "${u}"`);
2490
- }, I = (u, y) => {
2491
- On.includes(u) && !es(r.dialect, u) && s("dialectUnsupported", y, `${u.toUpperCase()} JOIN isn't in this database`);
2498
+ a.has(u) && s("duplicateTableName", y, `two tables named "${u}"`);
2499
+ }, C = (u, y) => {
2500
+ Un.includes(u) && !es(r.dialect, u) && s("dialectUnsupported", y, `${u.toUpperCase()} JOIN isn't in this database`);
2492
2501
  }, G = (u, y, g = !1) => {
2493
2502
  if (g && O(u) && u.query !== void 0) {
2494
- k(u, y);
2503
+ T(u, y);
2495
2504
  return;
2496
2505
  }
2497
2506
  if (g && O(u) && u.shared !== void 0) {
2498
- const { shared: N, alias: w } = u, S = q(w) && w !== "";
2499
- if (!q(N) || !S || u.table !== void 0) {
2500
- s("invalidStructure", y, "a shared query's place has a string `shared` and an `alias`, and no `table`"), S && (b(w, `${y}.alias`), o.set(w, void 0));
2507
+ const { shared: A, alias: w } = u, S = q(w) && w !== "";
2508
+ if (!q(A) || !S || u.table !== void 0) {
2509
+ s("invalidStructure", y, "a shared query's place has a string `shared` and an `alias`, and no `table`"), S && (b(w, `${y}.alias`), a.set(w, void 0));
2501
2510
  return;
2502
2511
  }
2503
- m(w, Y, `${y}.alias`, "an alias"), b(w, `${y}.alias`), $.add(N), p.has(N) ? v.has(N) || s("sharedOrder", `${y}.shared`, "a shared query can use only those before it") : s("unknownTable", `${y}.shared`, `no shared query "${N}"`), o.set(w, v.get(N));
2512
+ m(w, Y, `${y}.alias`, "an alias"), b(w, `${y}.alias`), $.add(A), h.has(A) ? v.has(A) || s("sharedOrder", `${y}.shared`, "a shared query can use only those before it") : s("unknownTable", `${y}.shared`, `no shared query "${A}"`), a.set(w, v.get(A));
2504
2513
  return;
2505
2514
  }
2506
2515
  if (!O(u) || !q(u.table) || !ge(u.alias)) {
@@ -2508,146 +2517,146 @@ function ii(e, n, r = {}) {
2508
2517
  return;
2509
2518
  }
2510
2519
  m(u.alias, Y, `${y}.alias`, "an alias");
2511
- const h = i.tables.find((N) => N.name === u.table);
2512
- h || s("unknownTable", `${y}.table`, `unknown table "${u.table}"`), q(u.alias) && u.alias !== "" ? (b(u.alias, `${y}.alias`), o.set(u.alias, h), l.set(u.table, u.alias)) : (b(u.table, `${y}.table`), o.set(u.table, h));
2520
+ const p = i.tables.find((A) => A.name === u.table);
2521
+ p || s("unknownTable", `${y}.table`, `unknown table "${u.table}"`), q(u.alias) && u.alias !== "" ? (b(u.alias, `${y}.alias`), a.set(u.alias, p), o.set(u.table, u.alias)) : (b(u.table, `${y}.table`), a.set(u.table, p));
2513
2522
  }, R = (u, y, g) => {
2514
- var T;
2523
+ var N;
2515
2524
  if (!u) return;
2516
- const { args: h, name: N } = u, w = h.filter((A) => !A.optional).length, S = (T = h[h.length - 1]) != null && T.variadic ? 1 / 0 : h.length;
2517
- y < w ? s("incomplete", g, `${N} needs ${w} argument(s)`) : y > S && s("argCount", g, `${N} takes ${S} argument(s)`);
2525
+ const { args: p, name: A } = u, w = p.filter((k) => !k.optional).length, S = (N = p[p.length - 1]) != null && N.variadic ? 1 / 0 : p.length;
2526
+ y < w ? s("incomplete", g, `${A} needs ${w} argument(s)`) : y > S && s("argCount", g, `${A} takes ${S} argument(s)`);
2518
2527
  };
2519
2528
  let X, ee, ye;
2520
- const he = (u) => {
2521
- const y = u.table && (o.get(u.table) ?? (c == null ? void 0 : c.get(u.table)));
2529
+ const pe = (u) => {
2530
+ const y = u.table && (a.get(u.table) ?? (c == null ? void 0 : c.get(u.table)));
2522
2531
  if (!y || Array.isArray(y)) return;
2523
- const g = y.columns.find((h) => h.name === u.column);
2524
- return g && Nt(g.type);
2525
- }, te = (u) => $n(u, he, a), x = r.typeMismatch ?? "warning", F = (u, y, g) => {
2532
+ const g = y.columns.find((p) => p.name === u.column);
2533
+ return g && At(g.type);
2534
+ }, te = (u) => vn(u, pe, l), x = r.typeMismatch ?? "warning", F = (u, y, g) => {
2526
2535
  if (u.type === "literal") {
2527
- if (u.value === null || u.value === "" && !u.empty || typeof u.value == "number" && !nn(u.value) || vn(u.value, y) !== void 0) return;
2536
+ if (u.value === null || u.value === "" && !u.empty || typeof u.value == "number" && !tn(u.value) || Sn(u.value, y) !== void 0) return;
2528
2537
  const w = u.value instanceof Date ? "a date" : JSON.stringify(String(u.value));
2529
2538
  s("typeMismatch", g, `${w} isn't ${de[y]}`, x);
2530
2539
  return;
2531
2540
  }
2532
- const h = te(u);
2533
- if (h === void 0 || De(h) === De(y)) return;
2534
- const N = `${de[h]} compared with ${de[y]}`;
2535
- s("typeMismatch", g, N, x);
2536
- }, pe = (u) => u && u.type !== "literal" ? te(u) : void 0, Qt = (u, y) => {
2537
- const { operator: g, left: h, right: N } = u;
2538
- if (!N) return;
2539
- const w = (T, A, M) => {
2540
- if (T.type !== "param" || !T.relative) return;
2541
- const E = pe(A);
2541
+ const p = te(u);
2542
+ if (p === void 0 || De(p) === De(y)) return;
2543
+ const A = `${de[p]} compared with ${de[y]}`;
2544
+ s("typeMismatch", g, A, x);
2545
+ }, he = (u) => u && u.type !== "literal" ? te(u) : void 0, Qt = (u, y) => {
2546
+ const { operator: g, left: p, right: A } = u;
2547
+ if (!A) return;
2548
+ const w = (N, k, M) => {
2549
+ if (N.type !== "param" || !N.relative) return;
2550
+ const E = he(k);
2542
2551
  if (E === void 0 || E === "date" || E === "dateTime") return;
2543
- const C = `a relative date compared with ${de[E]}`;
2544
- s("typeMismatch", M, C, x);
2552
+ const I = `a relative date compared with ${de[E]}`;
2553
+ s("typeMismatch", M, I, x);
2545
2554
  };
2546
- if (w(N, h, `${y}.right`), w(h, N, `${y}.left`), g === "LIKE" || g === "REGEXP") {
2547
- const T = pe(h);
2548
- if (T !== void 0 && T !== "text") {
2549
- const A = `${g} matches text, not ${de[T]}`;
2550
- s("typeMismatch", `${y}.left`, A, x);
2555
+ if (w(A, p, `${y}.right`), w(p, A, `${y}.left`), g === "LIKE" || g === "REGEXP") {
2556
+ const N = he(p);
2557
+ if (N !== void 0 && N !== "text") {
2558
+ const k = `${g} matches text, not ${de[N]}`;
2559
+ s("typeMismatch", `${y}.left`, k, x);
2551
2560
  }
2552
- if (F(N, "text", `${y}.right`), g === "REGEXP" && N.type === "literal" && q(N.value))
2561
+ if (F(A, "text", `${y}.right`), g === "REGEXP" && A.type === "literal" && q(A.value))
2553
2562
  try {
2554
- new RegExp(N.value);
2555
- } catch (A) {
2556
- const M = `not a regular expression: ${A.message}`;
2563
+ new RegExp(A.value);
2564
+ } catch (k) {
2565
+ const M = `not a regular expression: ${k.message}`;
2557
2566
  s("invalidPattern", `${y}.right`, M);
2558
2567
  }
2559
2568
  return;
2560
2569
  }
2561
- const S = pe(h);
2570
+ const S = he(p);
2562
2571
  if (g === "IN" || g === "BETWEEN") {
2563
- if (S === void 0 || N.type !== "list") return;
2564
- N.items.forEach((T, A) => F(T, S, `${y}.right.items[${A}]`));
2572
+ if (S === void 0 || A.type !== "list") return;
2573
+ A.items.forEach((N, k) => F(N, S, `${y}.right.items[${k}]`));
2565
2574
  return;
2566
2575
  }
2567
- if (S !== void 0) F(N, S, `${y}.right`);
2576
+ if (S !== void 0) F(A, S, `${y}.right`);
2568
2577
  else {
2569
- const T = pe(N);
2570
- T !== void 0 && F(h, T, `${y}.left`);
2578
+ const N = he(A);
2579
+ N !== void 0 && F(p, N, `${y}.left`);
2571
2580
  }
2572
- }, Nn = (u) => {
2581
+ }, An = (u) => {
2573
2582
  const y = u.map(([g]) => g.type === "literal" ? _e(g) : te(g)).find((g) => g !== void 0);
2574
- y !== void 0 && u.forEach(([g, h]) => F(g, y, h));
2583
+ y !== void 0 && u.forEach(([g, p]) => F(g, y, p));
2575
2584
  }, V = (u, y, g = !1) => {
2576
2585
  if (!O(u) || !q(u.type)) {
2577
2586
  s("invalidStructure", y, "an expression is an object with a string `type`");
2578
2587
  return;
2579
2588
  }
2580
- const h = (w) => s("invalidStructure", y, w), N = (w, S) => {
2589
+ const p = (w) => s("invalidStructure", y, w), A = (w, S) => {
2581
2590
  u[w] !== void 0 && V(u[w], `${y}.${w}`, S);
2582
2591
  };
2583
- switch (g && u.type === "exists" && h("EXISTS is a condition, not a value"), ge(u.fromSelect) || h("`fromSelect` is a column's id"), u.type) {
2592
+ switch (g && u.type === "exists" && p("EXISTS is a condition, not a value"), ge(u.fromSelect) || p("`fromSelect` is a column's id"), u.type) {
2584
2593
  case "literal":
2585
2594
  if (!ss(u.value))
2586
- h("a literal value is a string, number, boolean, date or null");
2595
+ p("a literal value is a string, number, boolean, date or null");
2587
2596
  else if (u.empty !== void 0 && typeof u.empty != "boolean")
2588
- h("a literal's `empty` is a boolean");
2589
- else if (en(u))
2597
+ p("a literal's `empty` is a boolean");
2598
+ else if (nn(u))
2590
2599
  s("incomplete", y, "a value not filled in");
2591
- else if (typeof u.value == "number" && !nn(u.value)) {
2592
- const w = `a number with more than ${tt} significant digits is written as another number`;
2600
+ else if (typeof u.value == "number" && !tn(u.value)) {
2601
+ const w = `a number with more than ${rt} significant digits is written as another number`;
2593
2602
  s("tooLong", `${y}.value`, w);
2594
2603
  } else
2595
2604
  m(u.value, Zt, `${y}.value`, "a text value");
2596
2605
  break;
2597
2606
  case "param":
2598
- (!q(u.name) || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(u.name)) && h("a param has a plain identifier as its `name`"), u.relative !== void 0 && !rs(u.relative) && h("a relative date has a `unit` (day, week, month or year), a whole `offset`, and an `anchor` (start or end) only for a week, month or year"), m(u.name, Y, `${y}.name`, "a parameter's name");
2607
+ (!q(u.name) || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(u.name)) && p("a param has a plain identifier as its `name`"), u.relative !== void 0 && !rs(u.relative) && p("a relative date has a `unit` (day, week, month or year), a whole `offset`, and an `anchor` (start or end) only for a week, month or year"), m(u.name, Y, `${y}.name`, "a parameter's name");
2599
2608
  break;
2600
2609
  case "field":
2601
2610
  if (!ge(u.table) || !ge(u.column))
2602
- h("a field has string `table` and `column`");
2611
+ p("a field has string `table` and `column`");
2603
2612
  else if (u.column === void 0 || u.column === "")
2604
2613
  s("incomplete", y, "a field with no column picked");
2605
2614
  else if (u.table === void 0 || u.table === "")
2606
2615
  s("unqualifiedField", y, `column "${u.column}" names no table`);
2607
- else if (o.has(u.table)) {
2608
- const w = o.get(u.table);
2609
- if (!(Array.isArray(w) ? w.includes(u.column) : !w || w.columns.some((T) => T.name === u.column))) {
2610
- const T = `${u.table}.${u.column}`;
2611
- s("unknownColumn", `${y}.column`, `unknown column "${T}"`);
2616
+ else if (a.has(u.table)) {
2617
+ const w = a.get(u.table);
2618
+ if (!(Array.isArray(w) ? w.includes(u.column) : !w || w.columns.some((N) => N.name === u.column))) {
2619
+ const N = `${u.table}.${u.column}`;
2620
+ s("unknownColumn", `${y}.column`, `unknown column "${N}"`);
2612
2621
  }
2613
2622
  } else {
2614
- const w = l.get(u.table);
2623
+ const w = o.get(u.table);
2615
2624
  let S = `table "${u.table}" isn't in the query`;
2616
2625
  w ? S = `table "${u.table}" is named "${w}" in the query: use that` : c != null && c.has(u.table) && (S = `table "${u.table}" is the outer query's: only the EXISTS link names it`), s("tableNotInQuery", `${y}.table`, S);
2617
2626
  }
2618
2627
  break;
2619
2628
  case "function": {
2620
2629
  if (!q(u.name) || !Array.isArray(u.args)) {
2621
- h("a function has a string `name` and an `args` array");
2630
+ p("a function has a string `name` and an `args` array");
2622
2631
  break;
2623
2632
  }
2624
- !r.allowUnknownFunctions && !a[u.name.toUpperCase()] && s("unknownFunction", `${y}.name`, `function "${u.name}" isn't allowed`);
2625
- const w = a[u.name.toUpperCase()];
2626
- if (r.dialect && (w != null && w.dialects) && !w.dialects.includes(r.dialect) && s("dialectUnsupported", `${y}.name`, `${w.name} isn't in this database`), m(u.name, Y, `${y}.name`, "a function's name"), R(w, u.args.length, `${y}.args`), u.distinct !== void 0 && typeof u.distinct != "boolean" && h("a function's `distinct` is a boolean"), u.distinct === !0 && r.dialect === "mssql" && u.name.toUpperCase() === "STRING_AGG" && s("dialectUnsupported", y, "SQL Server's STRING_AGG can't join only different values"), u.args.forEach((S, T) => {
2627
- var E, C;
2628
- const A = t.length;
2629
- V(S, `${y}.args[${T}]`, !0), w && ((E = Ge(w, T)) == null ? void 0 : E.kind) === "type-name" && O(S) && m(S.value, er, `${y}.args[${T}].value`, "a type name"), w && ((C = Ge(w, T)) == null ? void 0 : C.kind) === "fixed-text" && O(S) && !(S.type === "literal" && q(S.value)) && h(`${w.name}'s ${Ge(w, T).label} is a text value`);
2630
- const M = !t.slice(A).some((P) => P.kind === "invalidStructure");
2631
- w != null && w.aggregate && M && le(S, a) && s(
2633
+ !r.allowUnknownFunctions && !l[u.name.toUpperCase()] && s("unknownFunction", `${y}.name`, `function "${u.name}" isn't allowed`);
2634
+ const w = l[u.name.toUpperCase()];
2635
+ if (r.dialect && (w != null && w.dialects) && !w.dialects.includes(r.dialect) && s("dialectUnsupported", `${y}.name`, `${w.name} isn't in this database`), m(u.name, Y, `${y}.name`, "a function's name"), R(w, u.args.length, `${y}.args`), u.distinct !== void 0 && typeof u.distinct != "boolean" && p("a function's `distinct` is a boolean"), u.distinct === !0 && r.dialect === "mssql" && u.name.toUpperCase() === "STRING_AGG" && s("dialectUnsupported", y, "SQL Server's STRING_AGG can't join only different values"), u.args.forEach((S, N) => {
2636
+ var E, I;
2637
+ const k = t.length;
2638
+ V(S, `${y}.args[${N}]`, !0), w && ((E = Ge(w, N)) == null ? void 0 : E.kind) === "type-name" && O(S) && m(S.value, er, `${y}.args[${N}].value`, "a type name"), w && ((I = Ge(w, N)) == null ? void 0 : I.kind) === "fixed-text" && O(S) && !(S.type === "literal" && q(S.value)) && p(`${w.name}'s ${Ge(w, N).label} is a text value`);
2639
+ const M = !t.slice(k).some((P) => P.kind === "invalidStructure");
2640
+ w != null && w.aggregate && M && le(S, l) && s(
2632
2641
  "misplacedAggregate",
2633
- `${y}.args[${T}]`,
2642
+ `${y}.args[${N}]`,
2634
2643
  "an aggregate can't be inside another: sum up per group in a sub query first"
2635
2644
  );
2636
2645
  }), u.orderBy !== void 0) {
2637
2646
  if (!Array.isArray(u.orderBy)) {
2638
- h("a function's `orderBy` is an array");
2647
+ p("a function's `orderBy` is an array");
2639
2648
  break;
2640
2649
  }
2641
- u.name.toUpperCase() !== "STRING_AGG" && h("only STRING_AGG takes an `orderBy`"), u.orderBy.forEach((S, T) => {
2642
- const A = `${y}.orderBy[${T}]`;
2650
+ u.name.toUpperCase() !== "STRING_AGG" && p("only STRING_AGG takes an `orderBy`"), u.orderBy.forEach((S, N) => {
2651
+ const k = `${y}.orderBy[${N}]`;
2643
2652
  if (!O(S) || S.direction !== "asc" && S.direction !== "desc") {
2644
- s("invalidStructure", A, "a sort key has an `expression` and a `direction`");
2653
+ s("invalidStructure", k, "a sort key has an `expression` and a `direction`");
2645
2654
  return;
2646
2655
  }
2647
2656
  const M = t.length;
2648
- V(S.expression, `${A}.expression`, !0), !t.slice(M).some((E) => E.kind === "invalidStructure") && (le(S.expression, a) && s("misplacedAggregate", `${A}.expression`, "an aggregate can't be inside another"), u.distinct === !0 && r.dialect === "postgres" && !Z(S.expression, u.args[0]) && s(
2657
+ V(S.expression, `${k}.expression`, !0), !t.slice(M).some((E) => E.kind === "invalidStructure") && (le(S.expression, l) && s("misplacedAggregate", `${k}.expression`, "an aggregate can't be inside another"), u.distinct === !0 && r.dialect === "postgres" && !Z(S.expression, u.args[0]) && s(
2649
2658
  "dialectUnsupported",
2650
- `${A}.expression`,
2659
+ `${k}.expression`,
2651
2660
  "PostgreSQL orders STRING_AGG of different values by the value only"
2652
2661
  ));
2653
2662
  });
@@ -2655,31 +2664,31 @@ function ii(e, n, r = {}) {
2655
2664
  break;
2656
2665
  }
2657
2666
  case "binary": {
2658
- u.operator !== void 0 && !Vr.includes(u.operator) && h(`unknown operator ${JSON.stringify(u.operator)}`), u.operator === "REGEXP" && r.dialect === "mssql" && s("dialectUnsupported", y, "SQL Server has no REGEXP");
2667
+ u.operator !== void 0 && !Vr.includes(u.operator) && p(`unknown operator ${JSON.stringify(u.operator)}`), u.operator === "REGEXP" && r.dialect === "mssql" && s("dialectUnsupported", y, "SQL Server has no REGEXP");
2659
2668
  const w = t.length;
2660
2669
  V(u.left, `${y}.left`, !0);
2661
- const S = t.slice(w).some((T) => T.kind === "incomplete");
2670
+ const S = t.slice(w).some((N) => N.kind === "incomplete");
2662
2671
  if (Hr(u.operator))
2663
- u.right !== void 0 && h(`${String(u.operator)} takes no right side`);
2672
+ u.right !== void 0 && p(`${String(u.operator)} takes no right side`);
2664
2673
  else if (u.right === void 0)
2665
2674
  s("incomplete", `${y}.right`, "a comparison with nothing to compare to");
2666
- else if (O(u.right) && u.right.type === "literal" && u.right.value === null)
2675
+ else if (O(u.right) && u.right.type === "literal" && u.right.value === null && u.operator !== "IS NOT DISTINCT FROM")
2667
2676
  s("incomplete", `${y}.right`, "compared with NULL (never true): use IS NULL");
2668
2677
  else if (O(u.right) && u.right.type === "subquery")
2669
2678
  u.operator === "IN" ? ee(u.right, `${y}.right`) : s("invalidStructure", `${y}.right`, "a sub query is only IN's right side");
2670
2679
  else if (u.operator === "BETWEEN") {
2671
- const { right: T } = u, A = O(T) && T.type === "list" && Array.isArray(T.items) ? T.items : void 0;
2672
- (A == null ? void 0 : A.length) !== 2 ? s("invalidStructure", `${y}.right`, "BETWEEN takes its two bounds as a list (expr.between)") : A.forEach((M, E) => {
2673
- const C = `${y}.right.items[${E}]`;
2674
- O(M) && M.type === "literal" && M.value === null ? s("incomplete", C, "a bound of NULL (never true)") : S && en(M) || V(M, C, !0);
2680
+ const { right: N } = u, k = O(N) && N.type === "list" && Array.isArray(N.items) ? N.items : void 0;
2681
+ (k == null ? void 0 : k.length) !== 2 ? s("invalidStructure", `${y}.right`, "BETWEEN takes its two bounds as a list (expr.between)") : k.forEach((M, E) => {
2682
+ const I = `${y}.right.items[${E}]`;
2683
+ O(M) && M.type === "literal" && M.value === null ? s("incomplete", I, "a bound of NULL (never true)") : S && nn(M) || V(M, I, !0);
2675
2684
  });
2676
- } else O(u.right) && u.right.type === "list" ? u.operator === "IN" ? ye(u.right, `${y}.right`) : s("invalidStructure", `${y}.right`, "a list is only IN's or BETWEEN's right side") : u.operator === "IN" ? s("invalidStructure", `${y}.right`, "IN takes a list (expr.list) or a sub query") : S && en(u.right) || V(u.right, `${y}.right`, !0);
2677
- t.slice(w).some((T) => T.kind === "invalidStructure") || Qt(u, y);
2685
+ } else O(u.right) && u.right.type === "list" ? u.operator === "IN" ? ye(u.right, `${y}.right`) : s("invalidStructure", `${y}.right`, "a list is only IN's or BETWEEN's right side") : u.operator === "IN" ? s("invalidStructure", `${y}.right`, "IN takes a list (expr.list) or a sub query") : S && nn(u.right) || V(u.right, `${y}.right`, !0);
2686
+ t.slice(w).some((N) => N.kind === "invalidStructure") || Qt(u, y);
2678
2687
  break;
2679
2688
  }
2680
2689
  case "logical":
2681
- if (u.operator !== "AND" && u.operator !== "OR" && h("a logical operator is AND or OR"), !Array.isArray(u.operands)) {
2682
- h("a logical node has an `operands` array");
2690
+ if (u.operator !== "AND" && u.operator !== "OR" && p("a logical operator is AND or OR"), !Array.isArray(u.operands)) {
2691
+ p("a logical node has an `operands` array");
2683
2692
  break;
2684
2693
  }
2685
2694
  u.operands.length === 0 && s("incomplete", y, "an empty AND/OR group"), u.operands.forEach((w, S) => V(w, `${y}.operands[${S}]`));
@@ -2695,24 +2704,24 @@ function ii(e, n, r = {}) {
2695
2704
  u[S] === void 0 && s("incomplete", `${y}.${S}`, `an IF with no ${S}`);
2696
2705
  });
2697
2706
  const w = t.length;
2698
- N("condition", !1), N("then", !0), N("else", !0), t.slice(w).some((S) => S.kind === "invalidStructure") || Nn(
2707
+ A("condition", !1), A("then", !0), A("else", !0), t.slice(w).some((S) => S.kind === "invalidStructure") || An(
2699
2708
  ["then", "else"].filter((S) => u[S] !== void 0).map((S) => [u[S], `${y}.${S}`])
2700
2709
  );
2701
2710
  break;
2702
2711
  }
2703
2712
  case "case": {
2704
2713
  const w = t.length;
2705
- N("subject", !0), Array.isArray(u.branches) ? (u.branches.length === 0 && s("incomplete", y, "a CASE with no WHEN"), u.branches.forEach((T, A) => {
2706
- if (!O(T)) {
2707
- const M = `${y}.branches[${A}]`;
2714
+ A("subject", !0), Array.isArray(u.branches) ? (u.branches.length === 0 && s("incomplete", y, "a CASE with no WHEN"), u.branches.forEach((N, k) => {
2715
+ if (!O(N)) {
2716
+ const M = `${y}.branches[${k}]`;
2708
2717
  s("invalidStructure", M, "a branch has `when` and `then`");
2709
2718
  return;
2710
2719
  }
2711
- V(T.when, `${y}.branches[${A}].when`, u.subject !== void 0), V(T.then, `${y}.branches[${A}].then`, !0);
2712
- })) : h("a case node has a `branches` array");
2720
+ V(N.when, `${y}.branches[${k}].when`, u.subject !== void 0), V(N.then, `${y}.branches[${k}].then`, !0);
2721
+ })) : p("a case node has a `branches` array");
2713
2722
  const S = t.length;
2714
- N("else", !0), Array.isArray(u.branches) && t.slice(w).concat(t.slice(S)).every((T) => T.kind !== "invalidStructure") && Nn([
2715
- ...u.branches.map((T, A) => [T.then, `${y}.branches[${A}].then`]),
2723
+ A("else", !0), Array.isArray(u.branches) && t.slice(w).concat(t.slice(S)).every((N) => N.kind !== "invalidStructure") && An([
2724
+ ...u.branches.map((N, k) => [N.then, `${y}.branches[${k}].then`]),
2716
2725
  ...u.else === void 0 ? [] : [[u.else, `${y}.else`]]
2717
2726
  ]);
2718
2727
  break;
@@ -2721,133 +2730,133 @@ function ii(e, n, r = {}) {
2721
2730
  X(u, y);
2722
2731
  break;
2723
2732
  case "subquery":
2724
- h("a sub query is only IN's right side");
2733
+ p("a sub query is only IN's right side");
2725
2734
  break;
2726
2735
  case "list":
2727
- h("a list is only IN's right side");
2736
+ p("a list is only IN's right side");
2728
2737
  break;
2729
2738
  default:
2730
- h(`unknown expression type ${JSON.stringify(u.type)}`);
2739
+ p(`unknown expression type ${JSON.stringify(u.type)}`);
2731
2740
  }
2732
2741
  };
2733
2742
  ye = (u, y) => {
2734
- Array.isArray(u.items) ? u.items.length === 0 ? s("incomplete", y, "IN with no values") : u.items.forEach((g, h) => V(g, `${y}.items[${h}]`, !0)) : s("invalidStructure", y, "a list has an `items` array");
2743
+ Array.isArray(u.items) ? u.items.length === 0 ? s("incomplete", y, "IN with no values") : u.items.forEach((g, p) => V(g, `${y}.items[${p}]`, !0)) : s("invalidStructure", y, "a list has an `items` array");
2735
2744
  }, ee = (u, y) => {
2736
- const { id: g, query: h } = u;
2737
- if (!q(g) || g === "" || !O(h)) {
2745
+ const { id: g, query: p } = u;
2746
+ if (!q(g) || g === "" || !O(p)) {
2738
2747
  s("invalidStructure", y, "a sub query has a non-empty string `id` and a `query`");
2739
2748
  return;
2740
2749
  }
2741
2750
  d.has(g) && s("duplicateId", `${y}.id`, `the id "${g}" is used twice`), d.add(g);
2742
- const N = { inQuery: o, aliasOf: l, outerTables: c };
2743
- o = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), c = void 0;
2751
+ const A = { inQuery: a, aliasOf: o, outerTables: c };
2752
+ a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0;
2744
2753
  const w = t.length;
2745
- Oe(h, `${y}.query`, !1), { inQuery: o, aliasOf: l, outerTables: c } = N;
2754
+ Oe(p, `${y}.query`, !1), { inQuery: a, aliasOf: o, outerTables: c } = A;
2746
2755
  const S = t.length === w;
2747
- (Array.isArray(h.select) ? h.select : []).length !== 1 && s("subqueryColumns", `${y}.query.select`, "a sub query after IN picks exactly one column to compare with"), S && Ue(h, `${y}.query`);
2756
+ (Array.isArray(p.select) ? p.select : []).length !== 1 && s("subqueryColumns", `${y}.query.select`, "a sub query after IN picks exactly one column to compare with"), S && Ue(p, `${y}.query`);
2748
2757
  }, X = (u, y) => {
2749
- const { query: g, link: h } = u;
2758
+ const { query: g, link: p } = u;
2750
2759
  if (!O(g) || !Array.isArray(g.joins)) {
2751
2760
  s("invalidStructure", `${y}.query`, "an EXISTS has a `query` with `from` and `joins`");
2752
2761
  return;
2753
2762
  }
2754
- const N = { inQuery: o, aliasOf: l, outerTables: c };
2755
- o = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), c = N.inQuery, [
2763
+ const A = { inQuery: a, aliasOf: o, outerTables: c };
2764
+ a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = A.inQuery, [
2756
2765
  [g.from, `${y}.query.from`],
2757
- ...g.joins.map((A, M) => [A, `${y}.query.joins[${M}]`])
2758
- ].forEach(([A, M], E) => {
2759
- const C = new Set(o.keys());
2760
- G(A, M, E > 0), E > 0 && O(A) && I(A.joinType, `${M}.joinType`), [...o.keys()].filter((P) => !C.has(P) && N.inQuery.has(P)).forEach((P) => s(
2766
+ ...g.joins.map((k, M) => [k, `${y}.query.joins[${M}]`])
2767
+ ].forEach(([k, M], E) => {
2768
+ const I = new Set(a.keys());
2769
+ G(k, M, E > 0), E > 0 && O(k) && C(k.joinType, `${M}.joinType`), [...a.keys()].filter((P) => !I.has(P) && A.inQuery.has(P)).forEach((P) => s(
2761
2770
  "ambiguousTable",
2762
2771
  `${M}.table`,
2763
2772
  `"${P}" is a table of the query around it too: give it an alias`
2764
2773
  ));
2765
2774
  });
2766
2775
  const S = [
2767
- ...g.joins.map((A, M) => [
2768
- O(A) ? A.on : void 0,
2776
+ ...g.joins.map((k, M) => [
2777
+ O(k) ? k.on : void 0,
2769
2778
  `${y}.query.joins[${M}].on`
2770
2779
  ]),
2771
2780
  [g.where, `${y}.query.where`]
2772
2781
  ];
2773
- S.forEach(([A, M]) => {
2774
- A !== void 0 && V(A, M);
2782
+ S.forEach(([k, M]) => {
2783
+ k !== void 0 && V(k, M);
2775
2784
  });
2776
- const T = { inQuery: o, aliasOf: l };
2777
- o = new Map([...N.inQuery, ...T.inQuery]), l = new Map([...N.aliasOf, ...T.aliasOf]), c = void 0, V(h, `${y}.link`), { inQuery: o, aliasOf: l, outerTables: c } = N, [...S, [h, `${y}.link`]].forEach(([A, M]) => {
2778
- O(A) && le(A, a) && s("misplacedAggregate", M, "an aggregate (SUM, COUNT, …) can't be used here");
2785
+ const N = { inQuery: a, aliasOf: o };
2786
+ a = new Map([...A.inQuery, ...N.inQuery]), o = new Map([...A.aliasOf, ...N.aliasOf]), c = void 0, V(p, `${y}.link`), { inQuery: a, aliasOf: o, outerTables: c } = A, [...S, [p, `${y}.link`]].forEach(([k, M]) => {
2787
+ O(k) && le(k, l) && s("misplacedAggregate", M, "an aggregate (SUM, COUNT, …) can't be used here");
2779
2788
  });
2780
2789
  };
2781
2790
  const ve = (u, y, g = []) => {
2782
- const h = new Set(g);
2783
- u.forEach((N, w) => {
2784
- !O(N) || !q(N.id) || N.id === "" ? s("invalidStructure", `${y}[${w}]`, "every entry has a non-empty string id") : h.has(N.id) ? s("duplicateId", `${y}[${w}].id`, `id "${N.id}" is used twice`) : h.add(N.id);
2791
+ const p = new Set(g);
2792
+ u.forEach((A, w) => {
2793
+ !O(A) || !q(A.id) || A.id === "" ? s("invalidStructure", `${y}[${w}]`, "every entry has a non-empty string id") : p.has(A.id) ? s("duplicateId", `${y}[${w}].id`, `id "${A.id}" is used twice`) : p.add(A.id);
2785
2794
  });
2786
- }, An = (u, y, g) => Array.isArray(u[y]) ? u[y] : (u[y] !== void 0 && s("invalidStructure", g, `\`${y}\` is an array`), []), Oe = (u, y, g) => {
2787
- const h = (E) => y ? `${y}.${E}` : E;
2788
- G(u.from, h("from"), !0);
2789
- const { from: N } = u, w = O(N) && N.query !== void 0 ? N.id : void 0;
2790
- O(N) && N.query !== void 0 && (!q(w) || w === "") && s("invalidStructure", h("from"), "a derived table has a non-empty string id");
2795
+ }, kn = (u, y, g) => Array.isArray(u[y]) ? u[y] : (u[y] !== void 0 && s("invalidStructure", g, `\`${y}\` is an array`), []), Oe = (u, y, g) => {
2796
+ const p = (E) => y ? `${y}.${E}` : E;
2797
+ G(u.from, p("from"), !0);
2798
+ const { from: A } = u, w = O(A) && A.query !== void 0 ? A.id : void 0;
2799
+ O(A) && A.query !== void 0 && (!q(w) || w === "") && s("invalidStructure", p("from"), "a derived table has a non-empty string id");
2791
2800
  const S = Array.isArray(u.joins) ? u.joins : [];
2792
- Array.isArray(u.joins) || s("invalidStructure", h("joins"), "`joins` is an array"), S.forEach((E, C) => G(E, h(`joins[${C}]`), !0)), ve(S, h("joins"), q(w) ? [w] : []), S.forEach((E, C) => {
2801
+ Array.isArray(u.joins) || s("invalidStructure", p("joins"), "`joins` is an array"), S.forEach((E, I) => G(E, p(`joins[${I}]`), !0)), ve(S, p("joins"), q(w) ? [w] : []), S.forEach((E, I) => {
2793
2802
  if (O(E)) {
2794
- if (On.includes(E.joinType))
2795
- I(E.joinType, h(`joins[${C}].joinType`));
2803
+ if (Un.includes(E.joinType))
2804
+ C(E.joinType, p(`joins[${I}].joinType`));
2796
2805
  else {
2797
2806
  const P = JSON.stringify(E.joinType);
2798
- s("invalidStructure", h(`joins[${C}].joinType`), `unknown join type ${P}`);
2807
+ s("invalidStructure", p(`joins[${I}].joinType`), `unknown join type ${P}`);
2799
2808
  }
2800
- E.joinType !== "cross" && (E.on !== void 0 ? V(E.on, h(`joins[${C}].on`)) : s("incomplete", h(`joins[${C}].on`), "a join with no ON condition"));
2809
+ E.joinType !== "cross" && (E.on !== void 0 ? V(E.on, p(`joins[${I}].on`)) : s("incomplete", p(`joins[${I}].on`), "a join with no ON condition"));
2801
2810
  }
2802
- }), u.where !== void 0 && V(u.where, h("where"));
2803
- const T = Array.isArray(u.select) ? u.select : [];
2804
- Array.isArray(u.select) || s("invalidStructure", h("select"), "`select` is an array"), ve(T, h("select")), T.forEach((E, C) => {
2805
- O(E) && (m(E.alias, Y, h(`select[${C}].alias`), "an alias"), ge(E.alias) || s("invalidStructure", h(`select[${C}].alias`), "an alias is a string"), V(E.expression, h(`select[${C}].expression`), !0));
2811
+ }), u.where !== void 0 && V(u.where, p("where"));
2812
+ const N = Array.isArray(u.select) ? u.select : [];
2813
+ Array.isArray(u.select) || s("invalidStructure", p("select"), "`select` is an array"), ve(N, p("select")), N.forEach((E, I) => {
2814
+ O(E) && (m(E.alias, Y, p(`select[${I}].alias`), "an alias"), ge(E.alias) || s("invalidStructure", p(`select[${I}].alias`), "an alias is a string"), V(E.expression, p(`select[${I}].expression`), !0));
2806
2815
  });
2807
- const A = An(u, "groupBy", h("groupBy"));
2808
- if (ve(A, h("groupBy")), A.forEach((E, C) => {
2809
- O(E) && (ge(E.fromSelect) || s("invalidStructure", h(`groupBy[${C}].fromSelect`), "`fromSelect` is a column's id"), V(E.expression, h(`groupBy[${C}].expression`), !0));
2810
- }), u.having !== void 0 && V(u.having, h("having")), u.distinct !== void 0 && typeof u.distinct != "boolean" && s("invalidStructure", h("distinct"), "`distinct` is a boolean"), !g) {
2816
+ const k = kn(u, "groupBy", p("groupBy"));
2817
+ if (ve(k, p("groupBy")), k.forEach((E, I) => {
2818
+ O(E) && (ge(E.fromSelect) || s("invalidStructure", p(`groupBy[${I}].fromSelect`), "`fromSelect` is a column's id"), V(E.expression, p(`groupBy[${I}].expression`), !0));
2819
+ }), u.having !== void 0 && V(u.having, p("having")), u.distinct !== void 0 && typeof u.distinct != "boolean" && s("invalidStructure", p("distinct"), "`distinct` is a boolean"), !g) {
2811
2820
  ["orderBy", "limit", "offset", "shared"].forEach((E) => {
2812
- u[E] !== void 0 && s("invalidStructure", h(E), `a derived table has no \`${E}\``);
2821
+ u[E] !== void 0 && s("invalidStructure", p(E), `a derived table has no \`${E}\``);
2813
2822
  });
2814
2823
  return;
2815
2824
  }
2816
- const M = An(u, "orderBy", "orderBy");
2817
- ve(M, "orderBy"), M.forEach((E, C) => {
2818
- O(E) && (E.direction !== "asc" && E.direction !== "desc" && s("invalidStructure", `orderBy[${C}].direction`, 'a sort direction is "asc" or "desc"'), V(E.expression, `orderBy[${C}].expression`, !0));
2825
+ const M = kn(u, "orderBy", "orderBy");
2826
+ ve(M, "orderBy"), M.forEach((E, I) => {
2827
+ O(E) && (E.direction !== "asc" && E.direction !== "desc" && s("invalidStructure", `orderBy[${I}].direction`, 'a sort direction is "asc" or "desc"'), V(E.expression, `orderBy[${I}].expression`, !0));
2819
2828
  }), ["limit", "offset"].forEach((E) => {
2820
- const C = u[E];
2821
- C !== void 0 && !(Number.isInteger(C) && C >= 0) ? s("invalidStructure", E, `\`${E}\` is a non-negative integer`) : C !== void 0 && !Number.isSafeInteger(C) && s("tooLong", E, `\`${E}\` is past ${Number.MAX_SAFE_INTEGER}, which is exact`);
2829
+ const I = u[E];
2830
+ I !== void 0 && !(Number.isInteger(I) && I >= 0) ? s("invalidStructure", E, `\`${E}\` is a non-negative integer`) : I !== void 0 && !Number.isSafeInteger(I) && s("tooLong", E, `\`${E}\` is past ${Number.MAX_SAFE_INTEGER}, which is exact`);
2822
2831
  });
2823
2832
  }, _t = (u, y, g) => {
2824
- const h = `${y}.query`, { union: N } = u;
2825
- if (!Array.isArray(N) || N.length < 1 || typeof u.all != "boolean") {
2826
- s("invalidStructure", h, "a union has one or more branches in `union`, and a boolean `all`"), b(g, `${y}.alias`), o.set(g, void 0);
2833
+ const p = `${y}.query`, { union: A } = u;
2834
+ if (!Array.isArray(A) || A.length < 1 || typeof u.all != "boolean") {
2835
+ s("invalidStructure", p, "a union has one or more branches in `union`, and a boolean `all`"), b(g, `${y}.alias`), a.set(g, void 0);
2827
2836
  return;
2828
2837
  }
2829
2838
  let w = [];
2830
2839
  const S = [];
2831
- let T = !0;
2832
- N.forEach((A, M) => {
2833
- const E = `${h}.union[${M}]`;
2834
- if (!O(A)) {
2835
- s("invalidStructure", E, "a union's branch is a query"), T = !1;
2840
+ let N = !0;
2841
+ A.forEach((k, M) => {
2842
+ const E = `${p}.union[${M}]`;
2843
+ if (!O(k)) {
2844
+ s("invalidStructure", E, "a union's branch is a query"), N = !1;
2836
2845
  return;
2837
2846
  }
2838
- const C = { inQuery: o, aliasOf: l, outerTables: c };
2839
- o = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), c = void 0;
2847
+ const I = { inQuery: a, aliasOf: o, outerTables: c };
2848
+ a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0;
2840
2849
  const P = t.length;
2841
- Oe(A, E, !1);
2842
- const ae = t.length === P, B = (Array.isArray(A.select) ? A.select : []).filter(O), U = ae ? B.map((j) => j.expression.type === "literal" ? _e(j.expression) : te(j.expression)) : [];
2843
- if ({ inQuery: o, aliasOf: l, outerTables: c } = C, !ae) {
2844
- T = !1;
2850
+ Oe(k, E, !1);
2851
+ const ae = t.length === P, B = (Array.isArray(k.select) ? k.select : []).filter(O), U = ae ? B.map((j) => j.expression.type === "literal" ? _e(j.expression) : te(j.expression)) : [];
2852
+ if ({ inQuery: a, aliasOf: o, outerTables: c } = I, !ae) {
2853
+ N = !1;
2845
2854
  return;
2846
2855
  }
2847
- Ue(A, E);
2856
+ Ue(k, E);
2848
2857
  const _ = B.map((j) => K(j));
2849
2858
  if (B.length === 0) {
2850
- s("unionColumns", `${E}.select`, "a union's branch names its columns"), T = !1;
2859
+ s("unionColumns", `${E}.select`, "a union's branch names its columns"), N = !1;
2851
2860
  return;
2852
2861
  }
2853
2862
  if (M === 0)
@@ -2877,54 +2886,54 @@ function ii(e, n, r = {}) {
2877
2886
  s("unionTypes", `${E}.select[${L}].expression`, re);
2878
2887
  }
2879
2888
  });
2880
- }), b(g, `${y}.alias`), o.set(g, T ? w.filter((A) => A !== void 0) : void 0);
2889
+ }), b(g, `${y}.alias`), a.set(g, N ? w.filter((k) => k !== void 0) : void 0);
2881
2890
  };
2882
- k = (u, y) => {
2883
- const { query: g, alias: h } = u;
2884
- if (u.table === void 0 && O(g) && g.union !== void 0 && q(h) && h !== "") {
2885
- m(h, Y, `${y}.alias`, "an alias"), _t(g, y, h);
2891
+ T = (u, y) => {
2892
+ const { query: g, alias: p } = u;
2893
+ if (u.table === void 0 && O(g) && g.union !== void 0 && q(p) && p !== "") {
2894
+ m(p, Y, `${y}.alias`, "an alias"), _t(g, y, p);
2886
2895
  return;
2887
2896
  }
2888
- if (u.table !== void 0 || !O(g) || !q(h) || h === "") {
2889
- s("invalidStructure", y, "a derived table has a `query` and an `alias`, and no `table`"), q(h) && h !== "" && (b(h, `${y}.alias`), o.set(h, void 0));
2897
+ if (u.table !== void 0 || !O(g) || !q(p) || p === "") {
2898
+ s("invalidStructure", y, "a derived table has a `query` and an `alias`, and no `table`"), q(p) && p !== "" && (b(p, `${y}.alias`), a.set(p, void 0));
2890
2899
  return;
2891
2900
  }
2892
- m(h, Y, `${y}.alias`, "an alias");
2893
- const N = { inQuery: o, aliasOf: l, outerTables: c };
2894
- o = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), c = void 0;
2901
+ m(p, Y, `${y}.alias`, "an alias");
2902
+ const A = { inQuery: a, aliasOf: o, outerTables: c };
2903
+ a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0;
2895
2904
  const w = t.length;
2896
- Oe(g, `${y}.query`, !1), { inQuery: o, aliasOf: l, outerTables: c } = N;
2897
- const S = Array.isArray(g.select) ? g.select : [], T = [], A = (C, P) => {
2898
- T.includes(C) ? s("derivedColumnName", P, `two columns named "${C}"`) : T.push(C);
2905
+ Oe(g, `${y}.query`, !1), { inQuery: a, aliasOf: o, outerTables: c } = A;
2906
+ const S = Array.isArray(g.select) ? g.select : [], N = [], k = (I, P) => {
2907
+ N.includes(I) ? s("derivedColumnName", P, `two columns named "${I}"`) : N.push(I);
2899
2908
  };
2900
- S.forEach((C, P) => {
2901
- if (!O(C) || !O(C.expression)) return;
2902
- const ae = K(C);
2903
- ae ? A(ae, `${y}.query.select[${P}]`) : s("derivedColumnName", `${y}.query.select[${P}]`, "a derived table's column needs an alias: nothing outside could name it");
2909
+ S.forEach((I, P) => {
2910
+ if (!O(I) || !O(I.expression)) return;
2911
+ const ae = K(I);
2912
+ ae ? k(ae, `${y}.query.select[${P}]`) : s("derivedColumnName", `${y}.query.select[${P}]`, "a derived table's column needs an alias: nothing outside could name it");
2904
2913
  });
2905
2914
  const M = t.length === w;
2906
- M && S.length === 0 && Ne(i, g, f).forEach((C) => A(C.name, `${y}.query.select`)), b(h, `${y}.alias`), o.set(h, M || S.length > 0 ? T : void 0), M && Ue(g, `${y}.query`);
2915
+ M && S.length === 0 && Ne(i, g, f).forEach((I) => k(I.name, `${y}.query.select`)), b(p, `${y}.alias`), a.set(p, M || S.length > 0 ? N : void 0), M && Ue(g, `${y}.query`);
2907
2916
  const { drillAcross: E } = u;
2908
2917
  E !== void 0 && !(O(E) && typeof E.synced == "boolean" && Number.isInteger(E.copied) && E.copied >= 0) && s("invalidStructure", `${y}.drillAcross`, "`drillAcross` has a boolean `synced` and a non-negative integer `copied`");
2909
2918
  };
2910
2919
  const Ue = (u, y) => {
2911
- var M, E, C, P, ae;
2912
- const g = (B) => y ? `${y}.${B}` : B, h = pn(u);
2913
- if (h) {
2914
- const B = u.joins.findIndex((U) => (U.table ?? D(U)) === h.table);
2920
+ var M, E, I, P, ae;
2921
+ const g = (B) => y ? `${y}.${B}` : B, p = gn(u);
2922
+ if (p) {
2923
+ const B = u.joins.findIndex((U) => (U.table ?? D(U)) === p.table);
2915
2924
  s(
2916
2925
  "joinOrder",
2917
2926
  g(`joins[${B}].on`),
2918
- `the join condition of "${h.table}" uses "${h.needs}", which is joined after it`
2927
+ `the join condition of "${p.table}" uses "${p.needs}", which is joined after it`
2919
2928
  );
2920
2929
  }
2921
- const N = (B, U) => {
2922
- le(B, a) && s("misplacedAggregate", U, "an aggregate (SUM, COUNT, …) can't be used here");
2930
+ const A = (B, U) => {
2931
+ le(B, l) && s("misplacedAggregate", U, "an aggregate (SUM, COUNT, …) can't be used here");
2923
2932
  };
2924
- N(u.where, g("where")), u.joins.forEach((B, U) => {
2925
- B.joinType !== "cross" && N(B.on, g(`joins[${U}].on`));
2926
- }), (M = u.groupBy) == null || M.forEach((B, U) => N(B.expression, g(`groupBy[${U}].expression`)));
2927
- const w = Kn(u, a), S = "isn't a group key or an aggregate (SUM, COUNT, …)";
2933
+ A(u.where, g("where")), u.joins.forEach((B, U) => {
2934
+ B.joinType !== "cross" && A(B.on, g(`joins[${U}].on`));
2935
+ }), (M = u.groupBy) == null || M.forEach((B, U) => A(B.expression, g(`groupBy[${U}].expression`)));
2936
+ const w = Yn(u, l), S = "isn't a group key or an aggregate (SUM, COUNT, …)";
2928
2937
  if (w.selectAll && s("notGrouped", g("select"), "a grouped query has to pick its columns, not *"), u.select.forEach((B, U) => {
2929
2938
  w.select.includes(B.id) && s("notGrouped", g(`select[${U}].expression`), `this column ${S}`);
2930
2939
  }), (E = u.orderBy) == null || E.forEach((B, U) => {
@@ -2932,7 +2941,7 @@ function ii(e, n, r = {}) {
2932
2941
  }), w.having && s("notGrouped", g("having"), `a column in this condition ${S}`), r.dialect === "mssql") {
2933
2942
  const B = [], U = (L, J) => {
2934
2943
  const [re, je] = L.length <= J.length ? [L, J] : [J, L];
2935
- return re.every((Cn, In) => Cn.direction === je[In].direction && Z(Cn.expression, je[In].expression));
2944
+ return re.every((Cn, Mn) => Cn.direction === je[Mn].direction && Z(Cn.expression, je[Mn].expression));
2936
2945
  }, _ = (L) => {
2937
2946
  var re;
2938
2947
  return [...L.type === "function" && ((re = L.orderBy) != null && re.length) ? [L.orderBy] : [], ...fe(L).flatMap(_)];
@@ -2945,11 +2954,11 @@ function ii(e, n, r = {}) {
2945
2954
  ), B.push(re);
2946
2955
  });
2947
2956
  };
2948
- u.select.forEach((L, J) => j(L.expression, g(`select[${J}].expression`))), u.having && j(u.having, g("having")), (C = u.orderBy) == null || C.forEach((L, J) => j(L.expression, `orderBy[${J}].expression`));
2957
+ u.select.forEach((L, J) => j(L.expression, g(`select[${J}].expression`))), u.having && j(u.having, g("having")), (I = u.orderBy) == null || I.forEach((L, J) => j(L.expression, `orderBy[${J}].expression`));
2949
2958
  }
2950
- if (r.allowUnknownFunctions && Mn(u, a)) {
2959
+ if (r.allowUnknownFunctions && On(u, l)) {
2951
2960
  const B = (U, _) => {
2952
- Kt(U, a) && s(
2961
+ Kt(U, l) && s(
2953
2962
  "unknownAggregate",
2954
2963
  _,
2955
2964
  "a function the editor doesn't know: it isn't made a group key, in case it sums rows up — declare it in `functions` (`aggregate: true` if it does)",
@@ -2983,40 +2992,40 @@ function ii(e, n, r = {}) {
2983
2992
  "warning"
2984
2993
  );
2985
2994
  }), u.joins.forEach((B, U) => {
2986
- B.joinType !== "cross" || B.query && lt(B.query, a) || s(
2995
+ B.joinType !== "cross" || B.query && ut(B.query, l) || s(
2987
2996
  "crossJoin",
2988
2997
  g(`joins[${U}]`),
2989
2998
  `every row of "${D(B)}" meets every row of the query: they multiply`,
2990
2999
  "warning"
2991
3000
  );
2992
3001
  });
2993
- const T = _r(u, i), A = (B) => s(
3002
+ const N = _r(u, i), k = (B) => s(
2994
3003
  "fanOut",
2995
3004
  B,
2996
- `${T.tables.map((U) => `"${U}"`).join(", ")} rows repeat in this join: a sum, average or count of their columns comes out too high`,
3005
+ `${N.tables.map((U) => `"${U}"`).join(", ")} rows repeat in this join: a sum, average or count of their columns comes out too high`,
2997
3006
  "warning"
2998
3007
  );
2999
3008
  u.select.forEach((B, U) => {
3000
- T.select.includes(B.id) && A(g(`select[${U}].expression`));
3001
- }), T.having && A(g("having")), (ae = u.orderBy) == null || ae.forEach((B, U) => {
3002
- T.orderBy.includes(B.id) && A(`orderBy[${U}].expression`);
3009
+ N.select.includes(B.id) && k(g(`select[${U}].expression`));
3010
+ }), N.having && k(g("having")), (ae = u.orderBy) == null || ae.forEach((B, U) => {
3011
+ N.orderBy.includes(B.id) && k(`orderBy[${U}].expression`);
3003
3012
  });
3004
3013
  };
3005
3014
  n.shared !== void 0 && !Array.isArray(n.shared) && s("invalidStructure", "shared", "`shared` is an array"), ve(f, "shared");
3006
- const kn = /* @__PURE__ */ new Set();
3015
+ const xn = /* @__PURE__ */ new Set();
3007
3016
  if (f.forEach((u, y) => {
3008
3017
  if (!O(u)) return;
3009
- const g = `shared[${y}]`, { name: h } = u;
3010
- !q(h) || h === "" ? s("invalidStructure", `${g}.name`, "a shared query has a non-empty string `name`") : (m(h, Y, `${g}.name`, "a name"), kn.has(h) && s("duplicateTableName", `${g}.name`, `two shared queries named "${h}"`), kn.add(h));
3011
- const N = q(h) && h !== "" ? h : g, w = { inQuery: o, aliasOf: l, outerTables: c };
3012
- o = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), c = void 0, k({ query: u.query, alias: N }, g);
3013
- const S = o.get(N);
3014
- ({ inQuery: o, aliasOf: l, outerTables: c } = w), q(u.id) && v.set(u.id, Array.isArray(S) ? S : void 0);
3018
+ const g = `shared[${y}]`, { name: p } = u;
3019
+ !q(p) || p === "" ? s("invalidStructure", `${g}.name`, "a shared query has a non-empty string `name`") : (m(p, Y, `${g}.name`, "a name"), xn.has(p) && s("duplicateTableName", `${g}.name`, `two shared queries named "${p}"`), xn.add(p));
3020
+ const A = q(p) && p !== "" ? p : g, w = { inQuery: a, aliasOf: o, outerTables: c };
3021
+ a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0, T({ query: u.query, alias: A }, g);
3022
+ const S = a.get(A);
3023
+ ({ inQuery: a, aliasOf: o, outerTables: c } = w), q(u.id) && v.set(u.id, Array.isArray(S) ? S : void 0);
3015
3024
  }), Oe(n, "", !0), t.length === 0) {
3016
3025
  const u = n;
3017
3026
  if (Ue(u, ""), r.requireLimit) {
3018
- const { max: g } = r.requireLimit === !0 ? {} : r.requireLimit, h = Mn(u, a) && !((xn = u.groupBy) != null && xn.length);
3019
- u.limit === void 0 && !h ? s("noLimit", "limit", "no LIMIT: the query reads every row", "warning") : g !== void 0 && u.limit !== void 0 && u.limit > g && s("limitTooHigh", "limit", `LIMIT ${u.limit} is above ${g}`, "warning");
3027
+ const { max: g } = r.requireLimit === !0 ? {} : r.requireLimit, p = On(u, l) && !((Bn = u.groupBy) != null && Bn.length);
3028
+ u.limit === void 0 && !p ? s("noLimit", "limit", "no LIMIT: the query reads every row", "warning") : g !== void 0 && u.limit !== void 0 && u.limit > g && s("limitTooHigh", "limit", `LIMIT ${u.limit} is above ${g}`, "warning");
3020
3029
  }
3021
3030
  Yr(u, i).forEach((g) => {
3022
3031
  s("paramConflict", "", `param "${g}" is compared against columns of different types`);
@@ -3024,10 +3033,10 @@ function ii(e, n, r = {}) {
3024
3033
  s("paramConflict", "", `param "${g}" stands for two different relative dates`);
3025
3034
  });
3026
3035
  const y = Xr(u);
3027
- (Bn = u.orderBy) == null || Bn.forEach((g, h) => {
3036
+ (In = u.orderBy) == null || In.forEach((g, p) => {
3028
3037
  y.includes(g.id) && s(
3029
3038
  "distinctSort",
3030
- `orderBy[${h}].expression`,
3039
+ `orderBy[${p}].expression`,
3031
3040
  "with DISTINCT, a sort key has to be one of the selected columns"
3032
3041
  );
3033
3042
  });
@@ -3043,7 +3052,7 @@ function ii(e, n, r = {}) {
3043
3052
  return t.filter((u) => u.severity === "error" || !Ft.includes(u.kind));
3044
3053
  }
3045
3054
  function oi(e, n = {
3046
- relativeDate: ft
3055
+ relativeDate: mt
3047
3056
  }) {
3048
3057
  return n.relativeDate(e);
3049
3058
  }
@@ -3065,106 +3074,106 @@ const is = {
3065
3074
  offset: "order"
3066
3075
  };
3067
3076
  function li(e, n, r = {}) {
3068
- const { schema: t, functions: s } = r, i = (d, f) => typeof f == "number" ? d == null ? void 0 : d[f] : void 0, a = (d) => {
3077
+ const { schema: t, functions: s } = r, i = (d, f) => typeof f == "number" ? d == null ? void 0 : d[f] : void 0, l = (d) => {
3069
3078
  var f;
3070
3079
  return d.alias || t && d.table !== void 0 && ((f = z(t, d.table)) == null ? void 0 : f.label) || d.table || "";
3071
- }, o = (d, f) => {
3072
- var k;
3080
+ }, a = (d, f) => {
3081
+ var T;
3073
3082
  if (!d || f.length === 0) return [];
3074
- const [p, v, $] = f;
3083
+ const [h, v, $] = f;
3075
3084
  switch (d.type) {
3076
3085
  case "logical": {
3077
3086
  const b = i(d.operands, v);
3078
- return b ? [{ kind: "condition", number: v + 1 }, ...o(b, f.slice(2))] : [];
3087
+ return b ? [{ kind: "condition", number: v + 1 }, ...a(b, f.slice(2))] : [];
3079
3088
  }
3080
3089
  case "not":
3081
- return [{ kind: "not" }, ...o(d.operand, f.slice(1))];
3090
+ return [{ kind: "not" }, ...a(d.operand, f.slice(1))];
3082
3091
  case "binary": {
3083
- if (p !== "left" && p !== "right") return [];
3084
- const b = { kind: "side", side: p }, I = d[p];
3085
- return (I == null ? void 0 : I.type) === "subquery" && v === "query" ? [b, { kind: "inSubQuery" }, ...m(I.query, f.slice(2))] : (I == null ? void 0 : I.type) === "list" && v === "items" && i(I.items, $) ? [b, { kind: "listItem", number: $ + 1 }] : [b, ...o(I, f.slice(1))];
3092
+ if (h !== "left" && h !== "right") return [];
3093
+ const b = { kind: "side", side: h }, C = d[h];
3094
+ return (C == null ? void 0 : C.type) === "subquery" && v === "query" ? [b, { kind: "inSubQuery" }, ...m(C.query, f.slice(2))] : (C == null ? void 0 : C.type) === "list" && v === "items" && i(C.items, $) ? [b, { kind: "listItem", number: $ + 1 }] : [b, ...a(C, f.slice(1))];
3086
3095
  }
3087
3096
  case "function": {
3088
3097
  const b = { kind: "function", name: d.name };
3089
- if (p === "args" && i(d.args, v)) {
3090
- const G = Zn(d.name, s), R = G && ((k = Ge(G, v)) == null ? void 0 : k.label);
3098
+ if (h === "args" && i(d.args, v)) {
3099
+ const G = et(d.name, s), R = G && ((T = Ge(G, v)) == null ? void 0 : T.label);
3091
3100
  return [
3092
3101
  b,
3093
3102
  { kind: "argument", number: v + 1, ...R && { label: R } },
3094
- ...o(d.args[v], f.slice(2))
3103
+ ...a(d.args[v], f.slice(2))
3095
3104
  ];
3096
3105
  }
3097
- const I = p === "orderBy" ? i(d.orderBy, v) : void 0;
3098
- return I ? [
3106
+ const C = h === "orderBy" ? i(d.orderBy, v) : void 0;
3107
+ return C ? [
3099
3108
  b,
3100
3109
  { kind: "sortKey", number: v + 1 },
3101
- ...o(I.expression, f.slice(3))
3110
+ ...a(C.expression, f.slice(3))
3102
3111
  ] : [b];
3103
3112
  }
3104
3113
  case "if":
3105
- return p === "condition" || p === "then" || p === "else" ? [{ kind: "if", part: p }, ...o(d[p], f.slice(1))] : [];
3114
+ return h === "condition" || h === "then" || h === "else" ? [{ kind: "if", part: h }, ...a(d[h], f.slice(1))] : [];
3106
3115
  case "case": {
3107
- if (p === "subject" || p === "else")
3108
- return [{ kind: "case", part: p }, ...o(d[p], f.slice(1))];
3109
- const b = p === "branches" ? i(d.branches, v) : void 0;
3116
+ if (h === "subject" || h === "else")
3117
+ return [{ kind: "case", part: h }, ...a(d[h], f.slice(1))];
3118
+ const b = h === "branches" ? i(d.branches, v) : void 0;
3110
3119
  return !b || $ !== "when" && $ !== "then" ? [] : [
3111
3120
  { kind: "case", part: $, branch: v + 1 },
3112
- ...o(b[$], f.slice(3))
3121
+ ...a(b[$], f.slice(3))
3113
3122
  ];
3114
3123
  }
3115
3124
  case "exists": {
3116
- const b = { kind: "exists", table: a(d.query.from) };
3117
- return p === "link" ? [b, { kind: "existsLink" }] : p === "query" ? [b, ...m(d.query, f.slice(1))] : [b];
3125
+ const b = { kind: "exists", table: l(d.query.from) };
3126
+ return h === "link" ? [b, { kind: "existsLink" }] : h === "query" ? [b, ...m(d.query, f.slice(1))] : [b];
3118
3127
  }
3119
3128
  default:
3120
3129
  return [];
3121
3130
  }
3122
- }, l = (d, f) => {
3131
+ }, o = (d, f) => {
3123
3132
  if (d.query && f[0] === "query") return c(d.alias, d.query, f.slice(1));
3124
- const p = d.query ? { kind: "subQuery", name: d.alias } : { kind: "table", name: a(d) };
3125
- return f[0] === "on" ? [p, { kind: "joinCondition" }, ...o(d.on, f.slice(1))] : [p];
3133
+ const h = d.query ? { kind: "subQuery", name: d.alias } : { kind: "table", name: l(d) };
3134
+ return f[0] === "on" ? [h, { kind: "joinCondition" }, ...a(d.on, f.slice(1))] : [h];
3126
3135
  };
3127
- function c(d, f, p, v = "subQuery") {
3136
+ function c(d, f, h, v = "subQuery") {
3128
3137
  const $ = { kind: v, name: d };
3129
- if (!H(f)) return [$, ...m(f, p)];
3130
- const k = p[0] === "union" ? i(be(f), p[1]) : void 0;
3131
- if (!k) return [$];
3132
- const b = p[1] + 1;
3133
- return [$, { kind: "unionQuery", number: b }, ...m(k, p.slice(2))];
3138
+ if (!H(f)) return [$, ...m(f, h)];
3139
+ const T = h[0] === "union" ? i(be(f), h[1]) : void 0;
3140
+ if (!T) return [$];
3141
+ const b = h[1] + 1;
3142
+ return [$, { kind: "unionQuery", number: b }, ...m(T, h.slice(2))];
3134
3143
  }
3135
3144
  function m(d, f) {
3136
3145
  var G, R;
3137
- const [p, v, $] = f, k = is[String(p)];
3138
- if (!k) return [];
3139
- const b = { kind: "section", section: k }, I = f.slice(3);
3140
- switch (p) {
3146
+ const [h, v, $] = f, T = is[String(h)];
3147
+ if (!T) return [];
3148
+ const b = { kind: "section", section: T }, C = f.slice(3);
3149
+ switch (h) {
3141
3150
  // A sub query is a place of its own: no "From Tables" before it.
3142
3151
  case "from":
3143
- return d.from.query ? v === "query" ? c(d.from.alias, d.from.query, f.slice(2)) : [{ kind: "subQuery", name: d.from.alias }] : [b, { kind: "table", name: a(d.from) }];
3152
+ return d.from.query ? v === "query" ? c(d.from.alias, d.from.query, f.slice(2)) : [{ kind: "subQuery", name: d.from.alias }] : [b, { kind: "table", name: l(d.from) }];
3144
3153
  case "joins": {
3145
3154
  const X = i(d.joins, v);
3146
- return X ? X.query ? l(X, f.slice(2)) : [b, ...l(X, f.slice(2))] : [b];
3155
+ return X ? X.query ? o(X, f.slice(2)) : [b, ...o(X, f.slice(2))] : [b];
3147
3156
  }
3148
3157
  case "where":
3149
3158
  case "having":
3150
- return [b, ...o(d[p], f.slice(1))];
3159
+ return [b, ...a(d[h], f.slice(1))];
3151
3160
  case "select": {
3152
3161
  const X = i(d.select, v);
3153
3162
  if (!X) return [b];
3154
- const { expression: ee, alias: ye } = X, he = ee.type === "field" && t && ee.table !== void 0 ? (R = (G = z(t, ee.table)) == null ? void 0 : G.columns.find((x) => x.name === ee.column)) == null ? void 0 : R.label : void 0, te = ye || he;
3163
+ const { expression: ee, alias: ye } = X, pe = ee.type === "field" && t && ee.table !== void 0 ? (R = (G = z(t, ee.table)) == null ? void 0 : G.columns.find((x) => x.name === ee.column)) == null ? void 0 : R.label : void 0, te = ye || pe;
3155
3164
  return [
3156
3165
  b,
3157
3166
  { kind: "column", number: v + 1, ...te && { name: te } },
3158
- ...$ === "expression" ? o(ee, I) : []
3167
+ ...$ === "expression" ? a(ee, C) : []
3159
3168
  ];
3160
3169
  }
3161
3170
  case "groupBy":
3162
3171
  case "orderBy": {
3163
- const X = i(d[p], v);
3172
+ const X = i(d[h], v);
3164
3173
  return X ? [
3165
3174
  b,
3166
- { kind: p === "groupBy" ? "groupKey" : "sortKey", number: v + 1 },
3167
- ...$ === "expression" ? o(X.expression, I) : []
3175
+ { kind: h === "groupBy" ? "groupKey" : "sortKey", number: v + 1 },
3176
+ ...$ === "expression" ? a(X.expression, C) : []
3168
3177
  ] : [b];
3169
3178
  }
3170
3179
  default:
@@ -3183,7 +3192,7 @@ export {
3183
3192
  Rr as B,
3184
3193
  Qr as C,
3185
3194
  ar as D,
3186
- un as E,
3195
+ cn as E,
3187
3196
  Ys as F,
3188
3197
  ei as G,
3189
3198
  oi as H,
@@ -3191,18 +3200,18 @@ export {
3191
3200
  ii as J,
3192
3201
  li as K,
3193
3202
  ls as L,
3194
- tt as M,
3203
+ rt as M,
3195
3204
  sr as N,
3196
- it as O,
3197
- hs as P,
3205
+ ot as O,
3206
+ ps as P,
3198
3207
  ri as Q,
3199
- ps as R,
3208
+ hs as R,
3200
3209
  $r as S,
3201
3210
  bs as T,
3202
3211
  gs as U,
3203
3212
  cs as V,
3204
- $n as W,
3205
- Nt as X,
3213
+ vn as W,
3214
+ At as X,
3206
3215
  Ds as Y,
3207
3216
  js as Z,
3208
3217
  Gs as _,
@@ -3215,39 +3224,39 @@ export {
3215
3224
  Jr as a5,
3216
3225
  _e as a6,
3217
3226
  nr as a7,
3218
- vn as a8,
3227
+ Sn as a8,
3219
3228
  ai as a9,
3220
3229
  $s as aA,
3221
3230
  tr as aB,
3222
- rt as aC,
3231
+ st as aC,
3223
3232
  ne as aD,
3224
3233
  Ae as aE,
3225
3234
  Xe as aF,
3226
- Ce as aG,
3227
- tn as aH,
3235
+ Ie as aG,
3236
+ rn as aH,
3228
3237
  ni as aI,
3229
3238
  zs as aJ,
3230
- ct as aa,
3239
+ dt as aa,
3231
3240
  fs as ab,
3232
3241
  vs as ac,
3233
3242
  Es as ad,
3234
- lt as ae,
3243
+ ut as ae,
3235
3244
  Ss as af,
3236
3245
  es as ag,
3237
3246
  Xr as ah,
3238
- at as ai,
3247
+ lt as ai,
3239
3248
  Zs as aj,
3240
3249
  us as ak,
3241
3250
  Ns as al,
3242
- mn as am,
3243
- pr as an,
3251
+ yn as am,
3252
+ hr as an,
3244
3253
  ms as ao,
3245
- hr as ap,
3254
+ pr as ap,
3246
3255
  mr as aq,
3247
3256
  He as ar,
3248
3257
  Ts as as,
3249
- Fn as at,
3250
- dt as au,
3258
+ Jn as at,
3259
+ ft as au,
3251
3260
  Fr as av,
3252
3261
  _r as aw,
3253
3262
  oe as ax,
@@ -3264,16 +3273,16 @@ export {
3264
3273
  Dr as j,
3265
3274
  Ar as k,
3266
3275
  Ps as l,
3267
- $t as m,
3276
+ vt as m,
3268
3277
  _s as n,
3269
3278
  qs as o,
3270
3279
  wr as p,
3271
3280
  xs as q,
3272
3281
  Fs as r,
3273
3282
  Hs as s,
3274
- Cs as t,
3283
+ Is as t,
3275
3284
  Ks as u,
3276
- Is as v,
3285
+ Cs as v,
3277
3286
  Os as w,
3278
3287
  Ms as x,
3279
3288
  Bs as y,