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