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