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