sqllang 0.0.1 → 0.0.2
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.
- package/dist/cjs/index.js +67 -4
- package/dist/esm/index.js +67 -4
- package/dist/esm/index.min.js +5 -5
- package/package.json +1 -1
- package/src/compile.js +25 -1
- package/src/execute.js +53 -3
package/dist/cjs/index.js
CHANGED
|
@@ -11993,7 +11993,22 @@ function planSelect(sel, outer, cteNames, cx, orderBy) {
|
|
|
11993
11993
|
addItem(term.base, i === 0 ? "FROM" : "CROSS", null, null);
|
|
11994
11994
|
for (const j of term.joins) addItem(j.item, j.joinType, j.on, j.using);
|
|
11995
11995
|
});
|
|
11996
|
-
|
|
11996
|
+
let where = null;
|
|
11997
|
+
if (sel.where != null) {
|
|
11998
|
+
const parts = steps.length > 1 ? splitAnd(sel.where) : [sel.where];
|
|
11999
|
+
const residual = [];
|
|
12000
|
+
for (const part of parts) {
|
|
12001
|
+
const deps = /* @__PURE__ */ new Set();
|
|
12002
|
+
const logic = expr(part, scope, cx, { deps });
|
|
12003
|
+
const alias = deps.size === 1 ? [...deps][0] : null;
|
|
12004
|
+
const i = alias === null ? -1 : steps.findIndex((s) => s.alias === alias);
|
|
12005
|
+
const step2 = steps[i];
|
|
12006
|
+
const pushable = steps.length > 1 && step2 && step2.source.kind !== "expr" && (step2.join === "FROM" || step2.join === "CROSS" || step2.join === "INNER") && !steps.slice(i + 1).some((s) => s.join === "RIGHT" || s.join === "FULL");
|
|
12007
|
+
if (pushable) step2.filter = step2.filter ? { AND: [step2.filter, logic] } : logic;
|
|
12008
|
+
else residual.push(logic);
|
|
12009
|
+
}
|
|
12010
|
+
where = residual.length === 0 ? null : residual.length === 1 ? residual[0] : { AND: residual };
|
|
12011
|
+
}
|
|
11997
12012
|
const columns = [];
|
|
11998
12013
|
const outputs = /* @__PURE__ */ new Map();
|
|
11999
12014
|
const taken = /* @__PURE__ */ new Set();
|
|
@@ -12052,6 +12067,8 @@ function planSelect(sel, outer, cteNames, cx, orderBy) {
|
|
|
12052
12067
|
having,
|
|
12053
12068
|
windows: scope.windows,
|
|
12054
12069
|
columns,
|
|
12070
|
+
// The select list as one expression, when it is all expressions with safe names.
|
|
12071
|
+
row: columns.length && columns.every((c) => c.kind === "expr" && c.name !== "__proto__") ? { ROW: columns.flatMap((c) => [c.name, c.expr]) } : null,
|
|
12055
12072
|
orderBy: order,
|
|
12056
12073
|
needsOutv
|
|
12057
12074
|
};
|
|
@@ -12330,6 +12347,24 @@ function syntaxError(source, error) {
|
|
|
12330
12347
|
var import_json_logic_engine = require("json-logic-engine");
|
|
12331
12348
|
var active = null;
|
|
12332
12349
|
var internal = {
|
|
12350
|
+
// One output row from `[name, value, name, value, ...]`. Compiled, it is a single
|
|
12351
|
+
// object literal, so a whole select list costs one call per row. Not
|
|
12352
|
+
// deterministic: each row needs its own object, never a folded shared one.
|
|
12353
|
+
ROW: {
|
|
12354
|
+
method: (args) => {
|
|
12355
|
+
const out = {};
|
|
12356
|
+
for (let i = 0; i < args.length; i += 2) setKey(out, args[i], args[i + 1] === void 0 ? null : args[i + 1]);
|
|
12357
|
+
return out;
|
|
12358
|
+
},
|
|
12359
|
+
compile: (args, bs) => {
|
|
12360
|
+
let res = bs.compile`({`;
|
|
12361
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
12362
|
+
res = bs.compile`${res}${i ? bs.compile`,` : bs.compile``}${args[i]}: (${args[i + 1]}) ?? null`;
|
|
12363
|
+
}
|
|
12364
|
+
return bs.compile`${res}})`;
|
|
12365
|
+
},
|
|
12366
|
+
deterministic: false
|
|
12367
|
+
},
|
|
12333
12368
|
PARAM: {
|
|
12334
12369
|
method: ([key]) => {
|
|
12335
12370
|
const params = active?.params;
|
|
@@ -12734,6 +12769,7 @@ function runSelect(p, state, outer, keep = null) {
|
|
|
12734
12769
|
if (projector.hasStar) {
|
|
12735
12770
|
for (let i = 0; i < items.length; i++) {
|
|
12736
12771
|
const o = items[i].row;
|
|
12772
|
+
if (sameKeys(o, columns)) continue;
|
|
12737
12773
|
const r = {};
|
|
12738
12774
|
for (let j = 0; j < columns.length; j++) {
|
|
12739
12775
|
const c = columns[j];
|
|
@@ -12788,7 +12824,8 @@ function makeProjector(p, state) {
|
|
|
12788
12824
|
};
|
|
12789
12825
|
if (!hasStar) for (const c of cols) addColumn(c.name);
|
|
12790
12826
|
const plain = cols.map((c) => c.kind === "expr" && c.name !== "__proto__");
|
|
12791
|
-
const
|
|
12827
|
+
const rowFn = !hasStar && p.row ? fnOf(state, p.row) : null;
|
|
12828
|
+
const project = rowFn ?? ((env) => {
|
|
12792
12829
|
const out = {};
|
|
12793
12830
|
for (let j = 0; j < cols.length; j++) {
|
|
12794
12831
|
const c = cols[j];
|
|
@@ -12803,6 +12840,15 @@ function makeProjector(p, state) {
|
|
|
12803
12840
|
const row = env[alias];
|
|
12804
12841
|
if (row !== null && typeof row === "object" && !Array.isArray(row)) {
|
|
12805
12842
|
const keys = Object.keys(row);
|
|
12843
|
+
if (c.aliases.length === 1) {
|
|
12844
|
+
for (let k = 0; k < keys.length; k++) {
|
|
12845
|
+
const key = keys[k];
|
|
12846
|
+
const v = row[key];
|
|
12847
|
+
if (key === "__proto__") setKey(out, key, nullish(v));
|
|
12848
|
+
else out[key] = v === void 0 ? null : v;
|
|
12849
|
+
}
|
|
12850
|
+
continue;
|
|
12851
|
+
}
|
|
12806
12852
|
for (let k = 0; k < keys.length; k++) {
|
|
12807
12853
|
const key = keys[k];
|
|
12808
12854
|
const name = Object.prototype.hasOwnProperty.call(out, key) ? `${alias}.${key}` : key;
|
|
@@ -12817,7 +12863,7 @@ function makeProjector(p, state) {
|
|
|
12817
12863
|
for (const k in out) if (!columnSet.has(k)) addColumn(k);
|
|
12818
12864
|
}
|
|
12819
12865
|
return out;
|
|
12820
|
-
};
|
|
12866
|
+
});
|
|
12821
12867
|
return { project, columns, hasStar };
|
|
12822
12868
|
}
|
|
12823
12869
|
var comparators = /* @__PURE__ */ new WeakMap();
|
|
@@ -12932,11 +12978,28 @@ function readsOutput(orderBy) {
|
|
|
12932
12978
|
return r;
|
|
12933
12979
|
}
|
|
12934
12980
|
var nullish = (v) => v === void 0 ? null : v;
|
|
12981
|
+
function sameKeys(o, columns) {
|
|
12982
|
+
const keys = Object.keys(o);
|
|
12983
|
+
if (keys.length !== columns.length) return false;
|
|
12984
|
+
for (let i = 0; i < keys.length; i++) if (keys[i] !== columns[i]) return false;
|
|
12985
|
+
return true;
|
|
12986
|
+
}
|
|
12935
12987
|
function joinStep(step2, envs, base, state) {
|
|
12936
12988
|
const { alias, source, join, leftAliases } = step2;
|
|
12937
12989
|
const lateral = source.kind === "expr";
|
|
12938
12990
|
const sourceFn = lateral ? fnOf(state, source.expr) : null;
|
|
12939
|
-
|
|
12991
|
+
let right = lateral ? null : materialize(source, state);
|
|
12992
|
+
if (step2.filter) {
|
|
12993
|
+
const keep = fnOf(state, step2.filter);
|
|
12994
|
+
const e = snapshot(base);
|
|
12995
|
+
const kept = [];
|
|
12996
|
+
for (let j = 0; j < right.length; j++) {
|
|
12997
|
+
tick();
|
|
12998
|
+
e[alias] = right[j];
|
|
12999
|
+
if (isTrue(keep(e))) kept.push(right[j]);
|
|
13000
|
+
}
|
|
13001
|
+
right = kept;
|
|
13002
|
+
}
|
|
12940
13003
|
const on = step2.on === null ? null : fnOf(state, step2.on);
|
|
12941
13004
|
const out = [];
|
|
12942
13005
|
if (join === "FROM" || join === "CROSS") {
|
package/dist/esm/index.js
CHANGED
|
@@ -11954,7 +11954,22 @@ function planSelect(sel, outer, cteNames, cx, orderBy) {
|
|
|
11954
11954
|
addItem(term.base, i === 0 ? "FROM" : "CROSS", null, null);
|
|
11955
11955
|
for (const j of term.joins) addItem(j.item, j.joinType, j.on, j.using);
|
|
11956
11956
|
});
|
|
11957
|
-
|
|
11957
|
+
let where = null;
|
|
11958
|
+
if (sel.where != null) {
|
|
11959
|
+
const parts = steps.length > 1 ? splitAnd(sel.where) : [sel.where];
|
|
11960
|
+
const residual = [];
|
|
11961
|
+
for (const part of parts) {
|
|
11962
|
+
const deps = /* @__PURE__ */ new Set();
|
|
11963
|
+
const logic = expr(part, scope, cx, { deps });
|
|
11964
|
+
const alias = deps.size === 1 ? [...deps][0] : null;
|
|
11965
|
+
const i = alias === null ? -1 : steps.findIndex((s) => s.alias === alias);
|
|
11966
|
+
const step2 = steps[i];
|
|
11967
|
+
const pushable = steps.length > 1 && step2 && step2.source.kind !== "expr" && (step2.join === "FROM" || step2.join === "CROSS" || step2.join === "INNER") && !steps.slice(i + 1).some((s) => s.join === "RIGHT" || s.join === "FULL");
|
|
11968
|
+
if (pushable) step2.filter = step2.filter ? { AND: [step2.filter, logic] } : logic;
|
|
11969
|
+
else residual.push(logic);
|
|
11970
|
+
}
|
|
11971
|
+
where = residual.length === 0 ? null : residual.length === 1 ? residual[0] : { AND: residual };
|
|
11972
|
+
}
|
|
11958
11973
|
const columns = [];
|
|
11959
11974
|
const outputs = /* @__PURE__ */ new Map();
|
|
11960
11975
|
const taken = /* @__PURE__ */ new Set();
|
|
@@ -12013,6 +12028,8 @@ function planSelect(sel, outer, cteNames, cx, orderBy) {
|
|
|
12013
12028
|
having,
|
|
12014
12029
|
windows: scope.windows,
|
|
12015
12030
|
columns,
|
|
12031
|
+
// The select list as one expression, when it is all expressions with safe names.
|
|
12032
|
+
row: columns.length && columns.every((c) => c.kind === "expr" && c.name !== "__proto__") ? { ROW: columns.flatMap((c) => [c.name, c.expr]) } : null,
|
|
12016
12033
|
orderBy: order,
|
|
12017
12034
|
needsOutv
|
|
12018
12035
|
};
|
|
@@ -12291,6 +12308,24 @@ function syntaxError(source, error) {
|
|
|
12291
12308
|
import { LogicEngine } from "json-logic-engine";
|
|
12292
12309
|
var active = null;
|
|
12293
12310
|
var internal = {
|
|
12311
|
+
// One output row from `[name, value, name, value, ...]`. Compiled, it is a single
|
|
12312
|
+
// object literal, so a whole select list costs one call per row. Not
|
|
12313
|
+
// deterministic: each row needs its own object, never a folded shared one.
|
|
12314
|
+
ROW: {
|
|
12315
|
+
method: (args) => {
|
|
12316
|
+
const out = {};
|
|
12317
|
+
for (let i = 0; i < args.length; i += 2) setKey(out, args[i], args[i + 1] === void 0 ? null : args[i + 1]);
|
|
12318
|
+
return out;
|
|
12319
|
+
},
|
|
12320
|
+
compile: (args, bs) => {
|
|
12321
|
+
let res = bs.compile`({`;
|
|
12322
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
12323
|
+
res = bs.compile`${res}${i ? bs.compile`,` : bs.compile``}${args[i]}: (${args[i + 1]}) ?? null`;
|
|
12324
|
+
}
|
|
12325
|
+
return bs.compile`${res}})`;
|
|
12326
|
+
},
|
|
12327
|
+
deterministic: false
|
|
12328
|
+
},
|
|
12294
12329
|
PARAM: {
|
|
12295
12330
|
method: ([key]) => {
|
|
12296
12331
|
const params = active?.params;
|
|
@@ -12695,6 +12730,7 @@ function runSelect(p, state, outer, keep = null) {
|
|
|
12695
12730
|
if (projector.hasStar) {
|
|
12696
12731
|
for (let i = 0; i < items.length; i++) {
|
|
12697
12732
|
const o = items[i].row;
|
|
12733
|
+
if (sameKeys(o, columns)) continue;
|
|
12698
12734
|
const r = {};
|
|
12699
12735
|
for (let j = 0; j < columns.length; j++) {
|
|
12700
12736
|
const c = columns[j];
|
|
@@ -12749,7 +12785,8 @@ function makeProjector(p, state) {
|
|
|
12749
12785
|
};
|
|
12750
12786
|
if (!hasStar) for (const c of cols) addColumn(c.name);
|
|
12751
12787
|
const plain = cols.map((c) => c.kind === "expr" && c.name !== "__proto__");
|
|
12752
|
-
const
|
|
12788
|
+
const rowFn = !hasStar && p.row ? fnOf(state, p.row) : null;
|
|
12789
|
+
const project = rowFn ?? ((env) => {
|
|
12753
12790
|
const out = {};
|
|
12754
12791
|
for (let j = 0; j < cols.length; j++) {
|
|
12755
12792
|
const c = cols[j];
|
|
@@ -12764,6 +12801,15 @@ function makeProjector(p, state) {
|
|
|
12764
12801
|
const row = env[alias];
|
|
12765
12802
|
if (row !== null && typeof row === "object" && !Array.isArray(row)) {
|
|
12766
12803
|
const keys = Object.keys(row);
|
|
12804
|
+
if (c.aliases.length === 1) {
|
|
12805
|
+
for (let k = 0; k < keys.length; k++) {
|
|
12806
|
+
const key = keys[k];
|
|
12807
|
+
const v = row[key];
|
|
12808
|
+
if (key === "__proto__") setKey(out, key, nullish(v));
|
|
12809
|
+
else out[key] = v === void 0 ? null : v;
|
|
12810
|
+
}
|
|
12811
|
+
continue;
|
|
12812
|
+
}
|
|
12767
12813
|
for (let k = 0; k < keys.length; k++) {
|
|
12768
12814
|
const key = keys[k];
|
|
12769
12815
|
const name = Object.prototype.hasOwnProperty.call(out, key) ? `${alias}.${key}` : key;
|
|
@@ -12778,7 +12824,7 @@ function makeProjector(p, state) {
|
|
|
12778
12824
|
for (const k in out) if (!columnSet.has(k)) addColumn(k);
|
|
12779
12825
|
}
|
|
12780
12826
|
return out;
|
|
12781
|
-
};
|
|
12827
|
+
});
|
|
12782
12828
|
return { project, columns, hasStar };
|
|
12783
12829
|
}
|
|
12784
12830
|
var comparators = /* @__PURE__ */ new WeakMap();
|
|
@@ -12893,11 +12939,28 @@ function readsOutput(orderBy) {
|
|
|
12893
12939
|
return r;
|
|
12894
12940
|
}
|
|
12895
12941
|
var nullish = (v) => v === void 0 ? null : v;
|
|
12942
|
+
function sameKeys(o, columns) {
|
|
12943
|
+
const keys = Object.keys(o);
|
|
12944
|
+
if (keys.length !== columns.length) return false;
|
|
12945
|
+
for (let i = 0; i < keys.length; i++) if (keys[i] !== columns[i]) return false;
|
|
12946
|
+
return true;
|
|
12947
|
+
}
|
|
12896
12948
|
function joinStep(step2, envs, base, state) {
|
|
12897
12949
|
const { alias, source, join, leftAliases } = step2;
|
|
12898
12950
|
const lateral = source.kind === "expr";
|
|
12899
12951
|
const sourceFn = lateral ? fnOf(state, source.expr) : null;
|
|
12900
|
-
|
|
12952
|
+
let right = lateral ? null : materialize(source, state);
|
|
12953
|
+
if (step2.filter) {
|
|
12954
|
+
const keep = fnOf(state, step2.filter);
|
|
12955
|
+
const e = snapshot(base);
|
|
12956
|
+
const kept = [];
|
|
12957
|
+
for (let j = 0; j < right.length; j++) {
|
|
12958
|
+
tick();
|
|
12959
|
+
e[alias] = right[j];
|
|
12960
|
+
if (isTrue(keep(e))) kept.push(right[j]);
|
|
12961
|
+
}
|
|
12962
|
+
right = kept;
|
|
12963
|
+
}
|
|
12901
12964
|
const on = step2.on === null ? null : fnOf(state, step2.on);
|
|
12902
12965
|
const out = [];
|
|
12903
12966
|
if (join === "FROM" || join === "CROSS") {
|