@deepagents/text2sql 7.12.0 → 7.13.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.
- package/README.md +57 -1
- package/dist/bin/sql.js +10 -13
- package/dist/index.js +31 -15
- package/dist/index.js.map +2 -2
- package/dist/lib/adapters/adapter.d.ts +17 -0
- package/dist/lib/adapters/adapter.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/bigquery.d.ts +2 -2
- package/dist/lib/adapters/bigquery/bigquery.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/bigquery.sql-policy.d.ts +3 -8
- package/dist/lib/adapters/bigquery/bigquery.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/index.d.ts +1 -1
- package/dist/lib/adapters/bigquery/index.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/index.js +230 -207
- package/dist/lib/adapters/bigquery/index.js.map +4 -4
- package/dist/lib/adapters/clickhouse/clickhouse.d.ts +2 -2
- package/dist/lib/adapters/clickhouse/clickhouse.d.ts.map +1 -1
- package/dist/lib/adapters/clickhouse/clickhouse.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/clickhouse/index.js +32 -8
- package/dist/lib/adapters/clickhouse/index.js.map +3 -3
- package/dist/lib/adapters/duckdb/duckdb.d.ts +2 -2
- package/dist/lib/adapters/duckdb/duckdb.d.ts.map +1 -1
- package/dist/lib/adapters/duckdb/duckdb.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/duckdb/index.js +32 -8
- package/dist/lib/adapters/duckdb/index.js.map +3 -3
- package/dist/lib/adapters/mysql/index.d.ts +1 -1
- package/dist/lib/adapters/mysql/index.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/index.js +256 -221
- package/dist/lib/adapters/mysql/index.js.map +4 -4
- package/dist/lib/adapters/mysql/mysql.d.ts +2 -2
- package/dist/lib/adapters/mysql/mysql.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/mysql.sql-policy.d.ts +3 -10
- package/dist/lib/adapters/mysql/mysql.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/policy/denied-calls.d.ts +17 -0
- package/dist/lib/adapters/policy/denied-calls.d.ts.map +1 -0
- package/dist/lib/adapters/policy/facts.d.ts +24 -0
- package/dist/lib/adapters/policy/facts.d.ts.map +1 -0
- package/dist/lib/adapters/policy/forbid.d.ts +9 -0
- package/dist/lib/adapters/policy/forbid.d.ts.map +1 -0
- package/dist/lib/adapters/policy/in-scope.d.ts +25 -0
- package/dist/lib/adapters/policy/in-scope.d.ts.map +1 -0
- package/dist/lib/adapters/policy/index.d.ts +8 -0
- package/dist/lib/adapters/policy/index.d.ts.map +1 -0
- package/dist/lib/adapters/policy/index.js +368 -0
- package/dist/lib/adapters/policy/index.js.map +7 -0
- package/dist/lib/adapters/policy/one-select.d.ts +7 -0
- package/dist/lib/adapters/policy/one-select.d.ts.map +1 -0
- package/dist/lib/adapters/policy/read-only.d.ts +4 -0
- package/dist/lib/adapters/policy/read-only.d.ts.map +1 -0
- package/dist/lib/adapters/policy/source-policy.d.ts +45 -0
- package/dist/lib/adapters/policy/source-policy.d.ts.map +1 -0
- package/dist/lib/adapters/policy/sql-text.d.ts +43 -0
- package/dist/lib/adapters/policy/sql-text.d.ts.map +1 -0
- package/dist/lib/adapters/postgres/index.d.ts +1 -1
- package/dist/lib/adapters/postgres/index.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/index.js +318 -223
- package/dist/lib/adapters/postgres/index.js.map +4 -4
- package/dist/lib/adapters/postgres/postgres.d.ts +2 -2
- package/dist/lib/adapters/postgres/postgres.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/postgres.sql-policy.d.ts +3 -9
- package/dist/lib/adapters/postgres/postgres.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/posthog/index.js +24 -5
- package/dist/lib/adapters/posthog/index.js.map +2 -2
- package/dist/lib/adapters/posthog/posthog.d.ts +2 -2
- package/dist/lib/adapters/posthog/posthog.d.ts.map +1 -1
- package/dist/lib/adapters/spreadsheet/index.js +237 -207
- package/dist/lib/adapters/spreadsheet/index.js.map +4 -4
- package/dist/lib/adapters/spreadsheet/spreadsheet.d.ts +2 -2
- package/dist/lib/adapters/spreadsheet/spreadsheet.d.ts.map +1 -1
- package/dist/lib/adapters/sqlite/index.d.ts +1 -1
- package/dist/lib/adapters/sqlite/index.d.ts.map +1 -1
- package/dist/lib/adapters/sqlite/index.js +236 -207
- package/dist/lib/adapters/sqlite/index.js.map +4 -4
- package/dist/lib/adapters/sqlite/sqlite.d.ts +2 -2
- package/dist/lib/adapters/sqlite/sqlite.d.ts.map +1 -1
- package/dist/lib/adapters/sqlite/sqlite.sql-policy.d.ts +3 -7
- package/dist/lib/adapters/sqlite/sqlite.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.d.ts +1 -1
- package/dist/lib/adapters/sqlserver/index.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.js +244 -214
- package/dist/lib/adapters/sqlserver/index.js.map +4 -4
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts +2 -2
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/sqlserver.sql-policy.d.ts +6 -10
- package/dist/lib/adapters/sqlserver/sqlserver.sql-policy.d.ts.map +1 -1
- package/dist/lib/sql-scope-error.d.ts +1 -0
- package/dist/lib/sql-scope-error.d.ts.map +1 -1
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/synthesis/index.js.map +1 -1
- package/package.json +10 -4
- package/dist/lib/adapters/parser-sql-policy.d.ts +0 -23
- package/dist/lib/adapters/parser-sql-policy.d.ts.map +0 -1
|
@@ -418,16 +418,35 @@ var Adapter = class {
|
|
|
418
418
|
resolveAllowedEntities: () => this.#resolveScope()
|
|
419
419
|
});
|
|
420
420
|
}
|
|
421
|
-
|
|
421
|
+
/** The decoded SQL when the policy allows it, or the policy's message. */
|
|
422
|
+
async #admit(sql) {
|
|
422
423
|
const decoded = this.#decodeShellEscapes(sql);
|
|
423
424
|
const violation = await this.#analyzePolicy(decoded);
|
|
424
|
-
if (violation?.kind === "read-only") return violation.message;
|
|
425
|
-
if (violation?.kind === "scope")
|
|
426
|
-
|
|
425
|
+
if (violation?.kind === "read-only") return { error: violation.message };
|
|
426
|
+
if (violation?.kind === "scope") {
|
|
427
|
+
return { error: JSON.stringify(violation.payload) };
|
|
428
|
+
}
|
|
429
|
+
return { sql: decoded };
|
|
430
|
+
}
|
|
431
|
+
async validate(sql) {
|
|
432
|
+
const admitted = await this.#admit(sql);
|
|
433
|
+
if ("error" in admitted) return admitted.error;
|
|
434
|
+
return this.validateImpl(admitted.sql);
|
|
427
435
|
}
|
|
428
436
|
async execute(sql) {
|
|
429
437
|
return this.executeImpl(await this.enforceExecutionPolicy(sql));
|
|
430
438
|
}
|
|
439
|
+
/**
|
|
440
|
+
* Validates and then executes SQL, checking it against the policy once.
|
|
441
|
+
* Gives the policy's or the validator's error in place of a result.
|
|
442
|
+
*/
|
|
443
|
+
async run(sql) {
|
|
444
|
+
const admitted = await this.#admit(sql);
|
|
445
|
+
if ("error" in admitted) return admitted;
|
|
446
|
+
const error = await this.validateImpl(admitted.sql);
|
|
447
|
+
if (error) return { error };
|
|
448
|
+
return { result: await this.executeImpl(admitted.sql) };
|
|
449
|
+
}
|
|
431
450
|
async enforceExecutionPolicy(sql) {
|
|
432
451
|
const decoded = this.#decodeShellEscapes(sql);
|
|
433
452
|
const violation = await this.#analyzePolicy(decoded);
|
|
@@ -842,8 +861,33 @@ var TableGrounding = class extends AbstractGrounding {
|
|
|
842
861
|
}
|
|
843
862
|
};
|
|
844
863
|
|
|
845
|
-
// packages/text2sql/src/lib/adapters/
|
|
846
|
-
|
|
864
|
+
// packages/text2sql/src/lib/adapters/policy/read-only.ts
|
|
865
|
+
var readOnly = Object.freeze({
|
|
866
|
+
kind: "read-only",
|
|
867
|
+
message: "only SELECT or WITH queries allowed"
|
|
868
|
+
});
|
|
869
|
+
|
|
870
|
+
// packages/text2sql/src/lib/adapters/policy/denied-calls.ts
|
|
871
|
+
var DeniedCalls = class {
|
|
872
|
+
#names;
|
|
873
|
+
#prefixes;
|
|
874
|
+
#qualified;
|
|
875
|
+
constructor(denials) {
|
|
876
|
+
this.#names = new Set(denials.names?.map((name) => name.toLowerCase()));
|
|
877
|
+
this.#prefixes = denials.prefixes?.map((prefix) => prefix.toLowerCase()) ?? [];
|
|
878
|
+
this.#qualified = denials.qualified === true;
|
|
879
|
+
}
|
|
880
|
+
check = async (facts) => {
|
|
881
|
+
const calls2 = await facts.calls();
|
|
882
|
+
return calls2.some((parts) => this.#denies(parts)) ? readOnly : null;
|
|
883
|
+
};
|
|
884
|
+
#denies(parts) {
|
|
885
|
+
if (this.#qualified && parts.length > 1) return true;
|
|
886
|
+
const name = parts.at(-1)?.toLowerCase();
|
|
887
|
+
if (name === void 0) return false;
|
|
888
|
+
return this.#names.has(name) || this.#prefixes.some((prefix) => name.startsWith(prefix));
|
|
889
|
+
}
|
|
890
|
+
};
|
|
847
891
|
|
|
848
892
|
// packages/text2sql/src/lib/sql-scope-error.ts
|
|
849
893
|
function buildScopeParseErrorPayload(sql, parserDialect, error) {
|
|
@@ -857,154 +901,152 @@ function buildScopeParseErrorPayload(sql, parserDialect, error) {
|
|
|
857
901
|
parser_error: parserError
|
|
858
902
|
};
|
|
859
903
|
}
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
let literalStart = errorOffset;
|
|
870
|
-
let literalEnd = findLiteralEnd(sql, literalStart);
|
|
871
|
-
let typeMatch = TYPE_BEFORE_LITERAL.exec(prefix);
|
|
872
|
-
let type = typeMatch?.groups?.type;
|
|
873
|
-
if (literalEnd === null) {
|
|
874
|
-
const suffixMatch = TIME_ZONE_SUFFIX.exec(sql.slice(errorOffset));
|
|
875
|
-
typeMatch = TIME_BEFORE_SUFFIX.exec(prefix);
|
|
876
|
-
type = typeMatch?.groups?.type && typeMatch.groups.qualifier && suffixMatch ? `${typeMatch.groups.type} ${typeMatch.groups.qualifier} TIME ZONE` : void 0;
|
|
877
|
-
literalStart = errorOffset + (suffixMatch?.[0].length ?? 0);
|
|
878
|
-
literalEnd = findLiteralEnd(sql, literalStart);
|
|
879
|
-
}
|
|
880
|
-
if (!type || typeMatch?.index === void 0 || literalEnd === null)
|
|
881
|
-
return null;
|
|
882
|
-
return prefix.slice(0, typeMatch.index) + `CAST(${sql.slice(literalStart, literalEnd)} AS ${parserType(type)})` + sql.slice(literalEnd);
|
|
883
|
-
}
|
|
884
|
-
function parserErrorOffset(error) {
|
|
885
|
-
if (typeof error !== "object" || error === null) return null;
|
|
886
|
-
const location = Reflect.get(error, "location");
|
|
887
|
-
if (typeof location !== "object" || location === null) return null;
|
|
888
|
-
const start = Reflect.get(location, "start");
|
|
889
|
-
if (typeof start !== "object" || start === null) return null;
|
|
890
|
-
const offset = Reflect.get(start, "offset");
|
|
891
|
-
return typeof offset === "number" ? offset : null;
|
|
892
|
-
}
|
|
893
|
-
function findLiteralEnd(sql, start) {
|
|
894
|
-
if (sql[start] !== "'") {
|
|
895
|
-
const delimiter = /^\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$/.exec(
|
|
896
|
-
sql.slice(start)
|
|
897
|
-
)?.[0];
|
|
898
|
-
if (!delimiter) return null;
|
|
899
|
-
const end = sql.indexOf(delimiter, start + delimiter.length);
|
|
900
|
-
return end === -1 ? null : end + delimiter.length;
|
|
901
|
-
}
|
|
902
|
-
let offset = start + 1;
|
|
903
|
-
while (offset < sql.length) {
|
|
904
|
-
if (sql[offset] === "\\") {
|
|
905
|
-
offset += 2;
|
|
906
|
-
continue;
|
|
907
|
-
}
|
|
908
|
-
if (sql[offset] !== "'") {
|
|
909
|
-
offset++;
|
|
910
|
-
continue;
|
|
911
|
-
}
|
|
912
|
-
if (sql[offset + 1] === "'") {
|
|
913
|
-
offset += 2;
|
|
914
|
-
continue;
|
|
915
|
-
}
|
|
916
|
-
return offset + 1;
|
|
917
|
-
}
|
|
918
|
-
return null;
|
|
919
|
-
}
|
|
920
|
-
function parserType(type) {
|
|
921
|
-
const unqualified = type.replace(/^pg_catalog\./i, "");
|
|
922
|
-
const timetz = /^timetz(\s*\([^)]*\))?$/i.exec(unqualified);
|
|
923
|
-
return timetz ? `TIME${timetz[1] ?? ""} WITH TIME ZONE` : unqualified;
|
|
904
|
+
function buildOutOfScopePayload(sql, referencedEntities, allowedEntities) {
|
|
905
|
+
return {
|
|
906
|
+
error: `Query references entities outside grounded scope: ${referencedEntities.join(", ")}`,
|
|
907
|
+
error_type: "OUT_OF_SCOPE",
|
|
908
|
+
suggestion: "Restrict the query to grounded tables/views or expand grounding to include the referenced entities.",
|
|
909
|
+
sql_attempted: sql,
|
|
910
|
+
referenced_entities: referencedEntities,
|
|
911
|
+
allowed_entities: allowedEntities
|
|
912
|
+
};
|
|
924
913
|
}
|
|
925
914
|
|
|
926
|
-
// packages/text2sql/src/lib/adapters/
|
|
927
|
-
var
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
async
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
const
|
|
937
|
-
|
|
915
|
+
// packages/text2sql/src/lib/adapters/policy/in-scope.ts
|
|
916
|
+
var InScope = class {
|
|
917
|
+
#names;
|
|
918
|
+
constructor(names) {
|
|
919
|
+
this.#names = names;
|
|
920
|
+
}
|
|
921
|
+
check = async (facts, check) => {
|
|
922
|
+
const relations = await facts.relations();
|
|
923
|
+
if (relations.length === 0) return null;
|
|
924
|
+
const allowed = await check.resolveAllowedEntities();
|
|
925
|
+
const isAllowed = await this.#names.allowing(allowed);
|
|
926
|
+
const outOfScope = relations.filter((relation) => !isAllowed(relation));
|
|
927
|
+
return outOfScope.length === 0 ? null : {
|
|
928
|
+
kind: "scope",
|
|
929
|
+
payload: buildOutOfScopePayload(check.sql, outOfScope, [...allowed])
|
|
930
|
+
};
|
|
931
|
+
};
|
|
932
|
+
};
|
|
933
|
+
var GroundedNames = class {
|
|
934
|
+
async allowing(allowed) {
|
|
935
|
+
const qualified = new Set(allowed.map((entity) => entity.toLowerCase()));
|
|
936
|
+
const unqualified = new Set(
|
|
937
|
+
allowed.map(
|
|
938
|
+
(entity) => entity.slice(entity.lastIndexOf(".") + 1).toLowerCase()
|
|
939
|
+
)
|
|
940
|
+
);
|
|
941
|
+
return (relation) => {
|
|
942
|
+
const name = relation.toLowerCase();
|
|
943
|
+
if (qualified.has(name)) return true;
|
|
944
|
+
if (!name.includes(".")) return unqualified.has(name);
|
|
945
|
+
const parts = name.split(".");
|
|
946
|
+
return parts.length >= 3 && qualified.has(parts.slice(-2).join("."));
|
|
947
|
+
};
|
|
938
948
|
}
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
const
|
|
945
|
-
return
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
// packages/text2sql/src/lib/adapters/policy/one-select.ts
|
|
952
|
+
var OneSelect = class {
|
|
953
|
+
check = async (facts) => {
|
|
954
|
+
const statements = await facts.statements();
|
|
955
|
+
return statements.length === 1 && statements[0]?.kind === "select" ? null : readOnly;
|
|
956
|
+
};
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
// packages/text2sql/src/lib/adapters/policy/source-policy.ts
|
|
960
|
+
var UnreadableSql = class extends Error {
|
|
961
|
+
name = "UnreadableSql";
|
|
962
|
+
};
|
|
963
|
+
var SourcePolicy = class {
|
|
964
|
+
#source;
|
|
965
|
+
#rules;
|
|
966
|
+
constructor(source, rules) {
|
|
967
|
+
this.#source = source;
|
|
968
|
+
this.#rules = rules;
|
|
946
969
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
970
|
+
async analyze(sql, context) {
|
|
971
|
+
const check = {
|
|
972
|
+
sql,
|
|
973
|
+
resolveAllowedEntities: () => context.resolveAllowedEntities()
|
|
974
|
+
};
|
|
975
|
+
try {
|
|
976
|
+
const facts = this.#source.read(sql);
|
|
977
|
+
for (const rule of this.#rules) {
|
|
978
|
+
const violation = await rule.check(facts, check);
|
|
979
|
+
if (violation) return violation;
|
|
952
980
|
}
|
|
981
|
+
return null;
|
|
982
|
+
} catch (error) {
|
|
983
|
+
if (!(error instanceof UnreadableSql)) throw error;
|
|
984
|
+
return {
|
|
985
|
+
kind: "scope",
|
|
986
|
+
payload: buildScopeParseErrorPayload(sql, this.#source.dialect, error)
|
|
987
|
+
};
|
|
953
988
|
}
|
|
954
|
-
return null;
|
|
955
989
|
}
|
|
956
|
-
|
|
957
|
-
|
|
990
|
+
};
|
|
991
|
+
|
|
992
|
+
// packages/text2sql/src/lib/adapters/policy/sql-text.ts
|
|
993
|
+
import nodeSqlParser from "node-sql-parser";
|
|
994
|
+
var { Parser } = nodeSqlParser;
|
|
995
|
+
var parser = new Parser();
|
|
996
|
+
var SqlText = class {
|
|
997
|
+
dialect;
|
|
998
|
+
#dialects;
|
|
999
|
+
#recover;
|
|
1000
|
+
constructor(options) {
|
|
1001
|
+
this.#dialects = options.dialects;
|
|
1002
|
+
this.#recover = options.recover;
|
|
1003
|
+
[this.dialect] = options.dialects.slice(-1);
|
|
1004
|
+
}
|
|
1005
|
+
read(sql) {
|
|
1006
|
+
let parsed;
|
|
1007
|
+
const parse = () => parsed ??= this.#parse(sql);
|
|
1008
|
+
const ast = () => {
|
|
1009
|
+
const result = parse();
|
|
1010
|
+
if ("error" in result) throw result.error;
|
|
1011
|
+
return result.ast;
|
|
1012
|
+
};
|
|
1013
|
+
return {
|
|
1014
|
+
statements: async () => {
|
|
1015
|
+
const result = parse();
|
|
1016
|
+
if ("ast" in result) return statementNodes(result.ast).map(toStatement);
|
|
1017
|
+
const keyword = firstStatementKeyword(sql);
|
|
1018
|
+
if (keyword === "SELECT" || keyword === "WITH") throw result.error;
|
|
1019
|
+
return keyword === null ? [] : [{ kind: "other", name: keyword }];
|
|
1020
|
+
},
|
|
1021
|
+
relations: async () => baseRelations(ast()),
|
|
1022
|
+
calls: async () => calls(ast()),
|
|
1023
|
+
constructs: async () => constructs(ast())
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
#parse(sql) {
|
|
958
1027
|
let lastError;
|
|
959
|
-
|
|
960
|
-
for (const dialect of this.dialects) {
|
|
1028
|
+
for (const dialect of this.#dialects) {
|
|
961
1029
|
try {
|
|
962
|
-
|
|
963
|
-
break;
|
|
1030
|
+
return { ast: this.#astify(sql, dialect) };
|
|
964
1031
|
} catch (error) {
|
|
965
|
-
lastDialect = dialect;
|
|
966
1032
|
lastError = error;
|
|
967
1033
|
}
|
|
968
1034
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
allowedEntities.map((entity) => entity.toLowerCase())
|
|
976
|
-
);
|
|
977
|
-
const allowedUnqualified = /* @__PURE__ */ new Set();
|
|
978
|
-
for (const entity of allowedEntities) {
|
|
979
|
-
const dot = entity.lastIndexOf(".");
|
|
980
|
-
allowedUnqualified.add(
|
|
981
|
-
(dot === -1 ? entity : entity.slice(dot + 1)).toLowerCase()
|
|
982
|
-
);
|
|
983
|
-
}
|
|
984
|
-
const outOfScope = references.map(
|
|
985
|
-
(reference) => reference.db ? `${reference.db}.${reference.table}` : reference.table
|
|
986
|
-
).filter((name) => {
|
|
987
|
-
const lower = name.toLowerCase();
|
|
988
|
-
if (!name.includes(".")) {
|
|
989
|
-
return !allowedQualified.has(lower) && !allowedUnqualified.has(lower);
|
|
990
|
-
}
|
|
991
|
-
if (allowedQualified.has(lower)) return false;
|
|
992
|
-
const parts = lower.split(".");
|
|
993
|
-
return parts.length < 3 || !allowedQualified.has(parts.slice(-2).join("."));
|
|
994
|
-
});
|
|
995
|
-
return outOfScope.length === 0 ? null : buildOutOfScopePayload(sql, outOfScope, [...allowedEntities]);
|
|
996
|
-
}
|
|
997
|
-
#parseStatements(sql, dialect) {
|
|
998
|
-
return parseStatements(this.#parseAst(sql, dialect));
|
|
1035
|
+
return {
|
|
1036
|
+
error: new UnreadableSql(
|
|
1037
|
+
Error.isError(lastError) ? lastError.message : String(lastError),
|
|
1038
|
+
{ cause: lastError }
|
|
1039
|
+
)
|
|
1040
|
+
};
|
|
999
1041
|
}
|
|
1000
|
-
#
|
|
1042
|
+
#astify(sql, dialect) {
|
|
1001
1043
|
let parserSql = sql;
|
|
1002
1044
|
const attempted = /* @__PURE__ */ new Set([sql]);
|
|
1003
1045
|
while (true) {
|
|
1004
1046
|
try {
|
|
1005
1047
|
return parser.astify(parserSql, { database: dialect });
|
|
1006
1048
|
} catch (error) {
|
|
1007
|
-
const recovered =
|
|
1049
|
+
const recovered = this.#recover?.(parserSql, error);
|
|
1008
1050
|
if (!recovered || attempted.has(recovered)) throw error;
|
|
1009
1051
|
attempted.add(recovered);
|
|
1010
1052
|
parserSql = recovered;
|
|
@@ -1012,70 +1054,56 @@ var ParserSqlPolicyAnalyzer = class {
|
|
|
1012
1054
|
}
|
|
1013
1055
|
}
|
|
1014
1056
|
};
|
|
1015
|
-
function
|
|
1057
|
+
function statementNodes(ast) {
|
|
1016
1058
|
const statements = Array.isArray(ast) ? ast : [ast];
|
|
1017
1059
|
return statements.filter(isAstLike);
|
|
1018
1060
|
}
|
|
1019
|
-
function
|
|
1020
|
-
if (
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
return true;
|
|
1037
|
-
}
|
|
1038
|
-
if (functionIdentifier && policy.blockedFunctions?.some(
|
|
1039
|
-
(blocked) => blocked.toLowerCase() === functionIdentifier.parts.at(-1)
|
|
1040
|
-
)) {
|
|
1041
|
-
return true;
|
|
1042
|
-
}
|
|
1043
|
-
if (functionIdentifier && policy.blockedFunctionPrefixes?.some(
|
|
1044
|
-
(prefix) => functionIdentifier.parts.at(-1)?.startsWith(prefix.toLowerCase())
|
|
1045
|
-
)) {
|
|
1046
|
-
return true;
|
|
1061
|
+
function toStatement(node) {
|
|
1062
|
+
if (node.type !== "select") return { kind: "other", name: String(node.type) };
|
|
1063
|
+
return nodes(node).some(
|
|
1064
|
+
(child) => child.type === "select" && isAstLike(child.into) && child.into.type === "into"
|
|
1065
|
+
) ? { kind: "select-into" } : { kind: "select" };
|
|
1066
|
+
}
|
|
1067
|
+
function calls(ast) {
|
|
1068
|
+
return nodes(ast).filter((node) => node.type === "function").map(functionNameParts).filter((parts) => parts.length > 0).toArray();
|
|
1069
|
+
}
|
|
1070
|
+
function constructs(ast) {
|
|
1071
|
+
const found = /* @__PURE__ */ new Set();
|
|
1072
|
+
for (const node of nodes(ast)) {
|
|
1073
|
+
if (node.table_hint != null) found.add("table-hint");
|
|
1074
|
+
if (node.locking_read != null) found.add("locking-read");
|
|
1075
|
+
if (node.type === "assign") found.add("assignment");
|
|
1076
|
+
if (isTableReferenceNode(node) && typeof node.server === "string") {
|
|
1077
|
+
found.add("server-relation");
|
|
1047
1078
|
}
|
|
1048
1079
|
}
|
|
1049
|
-
return
|
|
1050
|
-
|
|
1051
|
-
|
|
1080
|
+
return found;
|
|
1081
|
+
}
|
|
1082
|
+
function* nodes(node) {
|
|
1083
|
+
if (Array.isArray(node)) {
|
|
1084
|
+
for (const item of node) yield* nodes(item);
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
if (!isAstLike(node)) return;
|
|
1088
|
+
yield node;
|
|
1089
|
+
for (const value of Object.values(node)) yield* nodes(value);
|
|
1052
1090
|
}
|
|
1053
|
-
function
|
|
1054
|
-
if (!isAstLike(node.name)) return
|
|
1091
|
+
function functionNameParts(node) {
|
|
1092
|
+
if (!isAstLike(node.name)) return [];
|
|
1055
1093
|
const parts = [];
|
|
1056
1094
|
if (isAstLike(node.name.schema) && typeof node.name.schema.value === "string") {
|
|
1057
|
-
|
|
1095
|
+
parts.push(...identifierParts(node.name.schema.value));
|
|
1058
1096
|
}
|
|
1059
1097
|
const names = Array.isArray(node.name.name) ? node.name.name : [];
|
|
1060
1098
|
for (const name of names) {
|
|
1061
1099
|
if (isAstLike(name) && typeof name.value === "string") {
|
|
1062
|
-
|
|
1100
|
+
parts.push(...identifierParts(name.value));
|
|
1063
1101
|
}
|
|
1064
1102
|
}
|
|
1065
|
-
return parts
|
|
1103
|
+
return parts;
|
|
1066
1104
|
}
|
|
1067
|
-
function
|
|
1068
|
-
|
|
1069
|
-
...value.toLowerCase().split(".").filter((part) => part.length > 0)
|
|
1070
|
-
);
|
|
1071
|
-
}
|
|
1072
|
-
function extractBaseEntityReferences(ast) {
|
|
1073
|
-
const state = {
|
|
1074
|
-
cteNames: /* @__PURE__ */ new Set(),
|
|
1075
|
-
references: /* @__PURE__ */ new Map()
|
|
1076
|
-
};
|
|
1077
|
-
visitNode(ast, state);
|
|
1078
|
-
return Array.from(state.references.values());
|
|
1105
|
+
function identifierParts(value) {
|
|
1106
|
+
return value.split(".").filter((part) => part.length > 0);
|
|
1079
1107
|
}
|
|
1080
1108
|
function firstStatementKeyword(sql) {
|
|
1081
1109
|
let offset = 0;
|
|
@@ -1102,15 +1130,13 @@ function firstStatementKeyword(sql) {
|
|
|
1102
1130
|
}
|
|
1103
1131
|
return null;
|
|
1104
1132
|
}
|
|
1105
|
-
function
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
suggestion: "Restrict the query to grounded tables/views or expand grounding to include the referenced entities.",
|
|
1110
|
-
sql_attempted: sql,
|
|
1111
|
-
referenced_entities: referencedEntities,
|
|
1112
|
-
allowed_entities: allowedEntities
|
|
1133
|
+
function baseRelations(ast) {
|
|
1134
|
+
const state = {
|
|
1135
|
+
cteNames: /* @__PURE__ */ new Set(),
|
|
1136
|
+
references: /* @__PURE__ */ new Set()
|
|
1113
1137
|
};
|
|
1138
|
+
visitNode(ast, state);
|
|
1139
|
+
return [...state.references];
|
|
1114
1140
|
}
|
|
1115
1141
|
function visitNode(node, state) {
|
|
1116
1142
|
if (Array.isArray(node)) {
|
|
@@ -1134,7 +1160,7 @@ function visitStatement(node, parentState) {
|
|
|
1134
1160
|
for (const item of withItems) {
|
|
1135
1161
|
if (!isAstLike(item)) continue;
|
|
1136
1162
|
const cteName = readCteName(item);
|
|
1137
|
-
if (cteName) localState.cteNames.add(
|
|
1163
|
+
if (cteName) localState.cteNames.add(cteName.toLowerCase());
|
|
1138
1164
|
}
|
|
1139
1165
|
for (const item of withItems) {
|
|
1140
1166
|
if (isAstLike(item)) visitNode(item.stmt, localState);
|
|
@@ -1145,10 +1171,9 @@ function visitStatement(node, parentState) {
|
|
|
1145
1171
|
}
|
|
1146
1172
|
function addReference(node, state) {
|
|
1147
1173
|
const table2 = typeof node.table === "string" ? node.table : null;
|
|
1148
|
-
if (!table2 || state.cteNames.has(
|
|
1174
|
+
if (!table2 || state.cteNames.has(table2.toLowerCase())) return;
|
|
1149
1175
|
const db = typeof node.db === "string" ? node.db : null;
|
|
1150
|
-
|
|
1151
|
-
if (!state.references.has(key)) state.references.set(key, { db, table: table2 });
|
|
1176
|
+
state.references.add(db ? `${db}.${table2}` : table2);
|
|
1152
1177
|
}
|
|
1153
1178
|
function readCteName(node) {
|
|
1154
1179
|
const name = node.name;
|
|
@@ -1167,18 +1192,16 @@ function isTableReferenceNode(node) {
|
|
|
1167
1192
|
function isAstLike(value) {
|
|
1168
1193
|
return typeof value === "object" && value !== null;
|
|
1169
1194
|
}
|
|
1170
|
-
function caseFold(value) {
|
|
1171
|
-
return value.toLowerCase();
|
|
1172
|
-
}
|
|
1173
1195
|
|
|
1174
1196
|
// packages/text2sql/src/lib/adapters/bigquery/bigquery.sql-policy.ts
|
|
1175
|
-
|
|
1176
|
-
dialects
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1197
|
+
function policy() {
|
|
1198
|
+
return new SourcePolicy(new SqlText({ dialects: ["bigquery"] }), [
|
|
1199
|
+
new OneSelect(),
|
|
1200
|
+
// A qualified call names a persistent routine, which can run anything.
|
|
1201
|
+
new DeniedCalls({ names: ["EXTERNAL_QUERY"], qualified: true }),
|
|
1202
|
+
new InScope(new GroundedNames())
|
|
1203
|
+
]);
|
|
1204
|
+
}
|
|
1182
1205
|
|
|
1183
1206
|
// packages/text2sql/src/lib/adapters/bigquery/bigquery.ts
|
|
1184
1207
|
function formatBigQueryError(sql, error) {
|
|
@@ -1198,10 +1221,10 @@ var BigQuery = class extends Adapter {
|
|
|
1198
1221
|
systemSchemas = [];
|
|
1199
1222
|
formatterLanguage = "bigquery";
|
|
1200
1223
|
constructor(options) {
|
|
1201
|
-
super(new BigQuerySqlPolicyAnalyzer());
|
|
1202
1224
|
if (!options || typeof options.execute !== "function") {
|
|
1203
1225
|
throw new Error("BigQuery adapter requires an execute(sql) function.");
|
|
1204
1226
|
}
|
|
1227
|
+
super(options.policy ?? policy());
|
|
1205
1228
|
if (typeof options.validate !== "function") {
|
|
1206
1229
|
throw new Error(
|
|
1207
1230
|
"BigQuery adapter requires a validate(sql) function. Provide a BigQuery dry-run validator (recommended) so generated SQL can be validated before execution."
|
|
@@ -2218,11 +2241,11 @@ var bigquery_default = {
|
|
|
2218
2241
|
};
|
|
2219
2242
|
export {
|
|
2220
2243
|
BigQuery,
|
|
2221
|
-
BigQuerySqlPolicyAnalyzer,
|
|
2222
2244
|
constraints,
|
|
2223
2245
|
bigquery_default as default,
|
|
2224
2246
|
indexes,
|
|
2225
2247
|
info,
|
|
2248
|
+
policy,
|
|
2226
2249
|
rowCount,
|
|
2227
2250
|
tables,
|
|
2228
2251
|
views
|