@deepagents/text2sql 0.32.0 → 0.34.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.
Files changed (41) hide show
  1. package/dist/index.js +69 -11
  2. package/dist/index.js.map +3 -3
  3. package/dist/lib/adapters/adapter.d.ts.map +1 -1
  4. package/dist/lib/adapters/bigquery/index.d.ts +0 -3
  5. package/dist/lib/adapters/bigquery/index.d.ts.map +1 -1
  6. package/dist/lib/adapters/bigquery/index.js +0 -108
  7. package/dist/lib/adapters/bigquery/index.js.map +4 -4
  8. package/dist/lib/adapters/groundings/context.d.ts +0 -2
  9. package/dist/lib/adapters/groundings/context.d.ts.map +1 -1
  10. package/dist/lib/adapters/groundings/index.d.ts +0 -1
  11. package/dist/lib/adapters/groundings/index.d.ts.map +1 -1
  12. package/dist/lib/adapters/groundings/index.js +0 -101
  13. package/dist/lib/adapters/groundings/index.js.map +3 -3
  14. package/dist/lib/adapters/mysql/index.d.ts +0 -3
  15. package/dist/lib/adapters/mysql/index.d.ts.map +1 -1
  16. package/dist/lib/adapters/mysql/index.js +0 -108
  17. package/dist/lib/adapters/mysql/index.js.map +4 -4
  18. package/dist/lib/adapters/postgres/index.d.ts +2 -5
  19. package/dist/lib/adapters/postgres/index.d.ts.map +1 -1
  20. package/dist/lib/adapters/postgres/index.js +142 -250
  21. package/dist/lib/adapters/postgres/index.js.map +4 -4
  22. package/dist/lib/adapters/spreadsheet/index.js +34 -52
  23. package/dist/lib/adapters/spreadsheet/index.js.map +3 -3
  24. package/dist/lib/adapters/sqlite/index.d.ts +2 -5
  25. package/dist/lib/adapters/sqlite/index.d.ts.map +1 -1
  26. package/dist/lib/adapters/sqlite/index.js +101 -209
  27. package/dist/lib/adapters/sqlite/index.js.map +4 -4
  28. package/dist/lib/adapters/sqlserver/index.d.ts +2 -5
  29. package/dist/lib/adapters/sqlserver/index.d.ts.map +1 -1
  30. package/dist/lib/adapters/sqlserver/index.js +100 -208
  31. package/dist/lib/adapters/sqlserver/index.js.map +4 -4
  32. package/dist/lib/agents/result-tools.d.ts.map +1 -1
  33. package/dist/lib/synthesis/decorators/deduplicated-producer.d.ts +1 -0
  34. package/dist/lib/synthesis/decorators/deduplicated-producer.d.ts.map +1 -1
  35. package/dist/lib/synthesis/decorators/filtered-producer.d.ts +3 -0
  36. package/dist/lib/synthesis/decorators/filtered-producer.d.ts.map +1 -1
  37. package/dist/lib/synthesis/index.js +31 -6
  38. package/dist/lib/synthesis/index.js.map +4 -4
  39. package/package.json +4 -4
  40. package/dist/lib/adapters/groundings/report.grounding.d.ts +0 -38
  41. package/dist/lib/adapters/groundings/report.grounding.d.ts.map +0 -1
@@ -1,18 +1,17 @@
1
1
  import { type Adapter } from '../adapter.ts';
2
2
  import { type ColumnStatsGroundingConfig } from '../groundings/column-stats.grounding.ts';
3
+ import { type ColumnValuesGroundingConfig } from '../groundings/column-values.grounding.ts';
3
4
  import { type ConstraintGroundingConfig } from '../groundings/constraint.grounding.ts';
4
5
  import { type IndexesGroundingConfig } from '../groundings/indexes.grounding.ts';
5
6
  import { type InfoGroundingConfig } from '../groundings/info.grounding.ts';
6
- import { type ColumnValuesGroundingConfig } from '../groundings/column-values.grounding.ts';
7
- import { ReportGrounding, type ReportGroundingConfig } from '../groundings/report.grounding.ts';
8
7
  import { type RowCountGroundingConfig } from '../groundings/row-count.grounding.ts';
9
8
  import { type TableGroundingConfig } from '../groundings/table.grounding.ts';
10
9
  import type { ViewGroundingConfig } from '../groundings/view.grounding.ts';
11
10
  import { PostgresColumnStatsGrounding } from './column-stats.postgres.grounding.ts';
11
+ import { PostgresColumnValuesGrounding } from './column-values.postgres.grounding.ts';
12
12
  import { PostgresConstraintGrounding } from './constraint.postgres.grounding.ts';
13
13
  import { PostgresIndexesGrounding } from './indexes.postgres.grounding.ts';
14
14
  import { PostgresInfoGrounding } from './info.postgres.grounding.ts';
15
- import { PostgresColumnValuesGrounding } from './column-values.postgres.grounding.ts';
16
15
  import { Postgres } from './postgres.ts';
17
16
  import { PostgresRowCountGrounding } from './row-count.postgres.grounding.ts';
18
17
  import { PostgresTableGrounding } from './table.postgres.grounding.ts';
@@ -26,7 +25,6 @@ export declare function columnValues(config?: ColumnValuesGroundingConfig): (ada
26
25
  export declare function indexes(config?: IndexesGroundingConfig): (adapter: Adapter) => PostgresIndexesGrounding;
27
26
  export declare function rowCount(config?: RowCountGroundingConfig): (adapter: Adapter) => PostgresRowCountGrounding;
28
27
  export declare function constraints(config?: ConstraintGroundingConfig): (adapter: Adapter) => PostgresConstraintGrounding;
29
- export declare function report(config?: ReportGroundingConfig): (adapter: Adapter) => ReportGrounding;
30
28
  declare const _default: {
31
29
  tables: typeof tables;
32
30
  info: typeof info;
@@ -36,7 +34,6 @@ declare const _default: {
36
34
  indexes: typeof indexes;
37
35
  rowCount: typeof rowCount;
38
36
  constraints: typeof constraints;
39
- report: typeof report;
40
37
  Postgres: typeof Postgres;
41
38
  };
42
39
  export default _default;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/adapters/postgres/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,KAAK,0BAA0B,EAAE,MAAM,yCAAyC,CAAC;AAC1F,OAAO,EAAE,KAAK,yBAAyB,EAAE,MAAM,uCAAuC,CAAC;AACvF,OAAO,EAAE,KAAK,sBAAsB,EAAE,MAAM,oCAAoC,CAAC;AACjF,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAAE,KAAK,2BAA2B,EAAE,MAAM,0CAA0C,CAAC;AAC5F,OAAO,EACL,eAAe,EACf,KAAK,qBAAqB,EAC3B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,kCAAkC,CAA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AM,GAAE,mBAAwB,IAC3C,SAAS,OAAO,2BACzB;AAED,wBAAgB,KAAK,CAAC,MAAM,GAAE,mBAAwB,IAC5C,SAAS,OAAO,2BAGzB;AAED,wBAAgB,WAAW,CAAC,MAAM,GAAE,0BAA+B,IACzD,SAAS,OAAO,kCAGzB;AAED,wBAAgB,YAAY,CAAC,MAAM,GAAE,2BAAgC,IAC3D,SAAS,OAAO,mCAGzB;AAED,wBAAgB,OAAO,CAAC,MAAM,GAAE,sBAA2B,IACjD,SAAS,OAAO,8BAGzB;AAED,wBAAgB,QAAQ,CAAC,MAAM,GAAE,uBAA4B,IACnD,SAAS,OAAO,+BAGzB;AAED,wBAAgB,WAAW,CAAC,MAAM,GAAE,yBAA8B,IACxD,SAAS,OAAO,iCAGzB;;;;;;;;;;;;AAED,wBAUE"}
@@ -346,9 +346,6 @@ var Adapter = class {
346
346
  this.#relationshipToFragment(rel, sourceTable, targetTable)
347
347
  );
348
348
  }
349
- if (ctx.report) {
350
- fragments.push({ name: "businessContext", data: ctx.report });
351
- }
352
349
  return fragments;
353
350
  }
354
351
  /**
@@ -658,64 +655,6 @@ var ColumnStatsGrounding = class extends AbstractGrounding {
658
655
  }
659
656
  };
660
657
 
661
- // packages/text2sql/src/lib/adapters/groundings/constraint.grounding.ts
662
- var ConstraintGrounding = class extends AbstractGrounding {
663
- constructor(config = {}) {
664
- super("constraint");
665
- }
666
- /**
667
- * Execute the grounding process.
668
- * Annotates tables in ctx.tables with their constraints.
669
- */
670
- async execute(ctx) {
671
- for (const table2 of ctx.tables) {
672
- try {
673
- table2.constraints = await this.getConstraints(table2.name);
674
- } catch (error) {
675
- console.warn("Error collecting constraints for", table2.name, error);
676
- }
677
- }
678
- }
679
- };
680
-
681
- // packages/text2sql/src/lib/adapters/groundings/indexes.grounding.ts
682
- var IndexesGrounding = class extends AbstractGrounding {
683
- constructor(config = {}) {
684
- super("index");
685
- }
686
- /**
687
- * Execute the grounding process.
688
- * Annotates tables in ctx.tables with their indexes and marks indexed columns.
689
- */
690
- async execute(ctx) {
691
- for (const table2 of ctx.tables) {
692
- table2.indexes = await this.getIndexes(table2.name);
693
- for (const index2 of table2.indexes ?? []) {
694
- for (const colName of index2.columns) {
695
- const column2 = table2.columns.find((c) => c.name === colName);
696
- if (column2) {
697
- column2.isIndexed = true;
698
- }
699
- }
700
- }
701
- }
702
- }
703
- };
704
-
705
- // packages/text2sql/src/lib/adapters/groundings/info.grounding.ts
706
- var InfoGrounding = class extends AbstractGrounding {
707
- constructor(config = {}) {
708
- super("dialectInfo");
709
- }
710
- /**
711
- * Execute the grounding process.
712
- * Writes database info to ctx.info.
713
- */
714
- async execute(ctx) {
715
- ctx.info = await this.collectInfo();
716
- }
717
- };
718
-
719
658
  // packages/text2sql/src/lib/adapters/groundings/column-values.grounding.ts
720
659
  var ColumnValuesGrounding = class extends AbstractGrounding {
721
660
  lowCardinalityLimit;
@@ -855,103 +794,61 @@ var ColumnValuesGrounding = class extends AbstractGrounding {
855
794
  }
856
795
  };
857
796
 
858
- // packages/text2sql/src/lib/adapters/groundings/report.grounding.ts
859
- import { groq } from "@ai-sdk/groq";
860
- import { tool } from "ai";
861
- import dedent from "dedent";
862
- import z from "zod";
863
- import "@deepagents/agent";
864
- import {
865
- ContextEngine,
866
- InMemoryContextStore,
867
- agent,
868
- fragment,
869
- persona,
870
- user
871
- } from "@deepagents/context";
872
- var ReportGrounding = class extends AbstractGrounding {
873
- #adapter;
874
- #model;
875
- #cache;
876
- #forceRefresh;
877
- constructor(adapter, config = {}) {
878
- super("business_context");
879
- this.#adapter = adapter;
880
- this.#model = config.model ?? groq("openai/gpt-oss-20b");
881
- this.#cache = config.cache;
882
- this.#forceRefresh = config.forceRefresh ?? false;
797
+ // packages/text2sql/src/lib/adapters/groundings/constraint.grounding.ts
798
+ var ConstraintGrounding = class extends AbstractGrounding {
799
+ constructor(config = {}) {
800
+ super("constraint");
883
801
  }
802
+ /**
803
+ * Execute the grounding process.
804
+ * Annotates tables in ctx.tables with their constraints.
805
+ */
884
806
  async execute(ctx) {
885
- if (!this.#forceRefresh && this.#cache) {
886
- const cached = await this.#cache.get();
887
- if (cached) {
888
- ctx.report = cached;
889
- return;
807
+ for (const table2 of ctx.tables) {
808
+ try {
809
+ table2.constraints = await this.getConstraints(table2.name);
810
+ } catch (error) {
811
+ console.warn("Error collecting constraints for", table2.name, error);
890
812
  }
891
813
  }
892
- const report2 = await this.#generateReport();
893
- ctx.report = report2;
894
- if (this.#cache) {
895
- await this.#cache.set(report2);
896
- }
897
814
  }
898
- async #generateReport() {
899
- const context = new ContextEngine({
900
- store: new InMemoryContextStore(),
901
- chatId: `report-gen-${crypto.randomUUID()}`,
902
- userId: "system"
903
- });
904
- context.set(
905
- persona({
906
- name: "db-report-agent",
907
- role: "Database analyst",
908
- objective: "Analyze the database and write a contextual report about what it represents"
909
- }),
910
- fragment(
911
- "instructions",
912
- dedent`
913
- Write a business context that helps another agent answer questions accurately.
914
-
915
- For EACH table, do queries ONE AT A TIME:
916
- 1. SELECT COUNT(*) to get row count
917
- 2. SELECT * LIMIT 3 to see sample data
918
-
919
- Then write a report with:
920
- - What business this database is for
921
- - For each table: purpose, row count, and example of what the data looks like
922
-
923
- Include concrete examples like "Track prices are $0.99",
924
- "Customer names like 'Luís Gonçalves'", etc.
815
+ };
925
816
 
926
- Keep it 400-600 words, conversational style.
927
- `
928
- ),
929
- user(
930
- "Please analyze the database and write a contextual report about what this database represents."
931
- )
932
- );
933
- const adapter = this.#adapter;
934
- const reportAgent = agent({
935
- name: "db-report-agent",
936
- model: this.#model,
937
- context,
938
- tools: {
939
- query_database: tool({
940
- description: "Execute a SELECT query to explore the database and gather insights.",
941
- inputSchema: z.object({
942
- sql: z.string().describe("The SELECT query to execute"),
943
- purpose: z.string().describe(
944
- "What insight you are trying to gather with this query"
945
- )
946
- }),
947
- execute: ({ sql }) => {
948
- return adapter.execute(sql);
817
+ // packages/text2sql/src/lib/adapters/groundings/indexes.grounding.ts
818
+ var IndexesGrounding = class extends AbstractGrounding {
819
+ constructor(config = {}) {
820
+ super("index");
821
+ }
822
+ /**
823
+ * Execute the grounding process.
824
+ * Annotates tables in ctx.tables with their indexes and marks indexed columns.
825
+ */
826
+ async execute(ctx) {
827
+ for (const table2 of ctx.tables) {
828
+ table2.indexes = await this.getIndexes(table2.name);
829
+ for (const index2 of table2.indexes ?? []) {
830
+ for (const colName of index2.columns) {
831
+ const column2 = table2.columns.find((c) => c.name === colName);
832
+ if (column2) {
833
+ column2.isIndexed = true;
949
834
  }
950
- })
835
+ }
951
836
  }
952
- });
953
- const result = await reportAgent.generate({});
954
- return result.text;
837
+ }
838
+ }
839
+ };
840
+
841
+ // packages/text2sql/src/lib/adapters/groundings/info.grounding.ts
842
+ var InfoGrounding = class extends AbstractGrounding {
843
+ constructor(config = {}) {
844
+ super("dialectInfo");
845
+ }
846
+ /**
847
+ * Execute the grounding process.
848
+ * Writes database info to ctx.info.
849
+ */
850
+ async execute(ctx) {
851
+ ctx.info = await this.collectInfo();
955
852
  }
956
853
  };
957
854
 
@@ -1259,6 +1156,101 @@ var PostgresColumnStatsGrounding = class extends ColumnStatsGrounding {
1259
1156
  }
1260
1157
  };
1261
1158
 
1159
+ // packages/text2sql/src/lib/adapters/postgres/column-values.postgres.grounding.ts
1160
+ var PostgresColumnValuesGrounding = class extends ColumnValuesGrounding {
1161
+ #adapter;
1162
+ #enumCache = /* @__PURE__ */ new Map();
1163
+ #enumCacheLoaded = false;
1164
+ constructor(adapter, config = {}) {
1165
+ super(config);
1166
+ this.#adapter = adapter;
1167
+ }
1168
+ /**
1169
+ * Load all ENUM types and their values into cache.
1170
+ * This is more efficient than querying per-column.
1171
+ */
1172
+ async #loadEnumCache() {
1173
+ if (this.#enumCacheLoaded) {
1174
+ return;
1175
+ }
1176
+ const rows = await this.#adapter.runQuery(`
1177
+ SELECT
1178
+ t.typname AS type_name,
1179
+ n.nspname AS type_schema,
1180
+ e.enumlabel AS enum_value
1181
+ FROM pg_type t
1182
+ JOIN pg_enum e ON t.oid = e.enumtypid
1183
+ JOIN pg_namespace n ON n.oid = t.typnamespace
1184
+ ORDER BY t.typname, e.enumsortorder
1185
+ `);
1186
+ for (const row of rows) {
1187
+ const key = `${row.type_schema}.${row.type_name}`;
1188
+ const existing = this.#enumCache.get(key) ?? [];
1189
+ existing.push(row.enum_value);
1190
+ this.#enumCache.set(key, existing);
1191
+ const simpleKey = row.type_name;
1192
+ const simpleExisting = this.#enumCache.get(simpleKey) ?? [];
1193
+ simpleExisting.push(row.enum_value);
1194
+ this.#enumCache.set(simpleKey, simpleExisting);
1195
+ }
1196
+ this.#enumCacheLoaded = true;
1197
+ }
1198
+ async collectEnumValues(tableName, column2) {
1199
+ if (column2.type.toLowerCase() !== "user-defined") {
1200
+ return void 0;
1201
+ }
1202
+ await this.#loadEnumCache();
1203
+ const { schema, table: table2 } = this.#adapter.parseTableName(tableName);
1204
+ const rows = await this.#adapter.runQuery(`
1205
+ SELECT udt_name, udt_schema
1206
+ FROM information_schema.columns
1207
+ WHERE table_schema = '${this.#adapter.escapeString(schema)}'
1208
+ AND table_name = '${this.#adapter.escapeString(table2)}'
1209
+ AND column_name = '${this.#adapter.escapeString(column2.name)}'
1210
+ `);
1211
+ if (!rows.length) {
1212
+ return void 0;
1213
+ }
1214
+ const { udt_name, udt_schema } = rows[0];
1215
+ const fullKey = `${udt_schema}.${udt_name}`;
1216
+ const values = this.#enumCache.get(fullKey) ?? this.#enumCache.get(udt_name);
1217
+ return values?.length ? values : void 0;
1218
+ }
1219
+ async collectLowCardinality(tableName, column2) {
1220
+ if (this.#isHighCardinality(column2)) {
1221
+ return void 0;
1222
+ }
1223
+ const { schema, table: table2 } = this.#adapter.parseTableName(tableName);
1224
+ const tableIdentifier = `${this.#adapter.quoteIdentifier(schema)}.${this.#adapter.quoteIdentifier(table2)}`;
1225
+ const columnIdentifier = this.#adapter.quoteIdentifier(column2.name);
1226
+ const limit = this.lowCardinalityLimit + 1;
1227
+ const sql = `
1228
+ SELECT DISTINCT ${columnIdentifier}::text AS value
1229
+ FROM ${tableIdentifier}
1230
+ WHERE ${columnIdentifier} IS NOT NULL
1231
+ LIMIT ${limit}
1232
+ `;
1233
+ const rows = await this.#adapter.runQuery(sql);
1234
+ if (!rows.length || rows.length > this.lowCardinalityLimit) {
1235
+ return void 0;
1236
+ }
1237
+ const values = [];
1238
+ for (const row of rows) {
1239
+ if (row.value == null) {
1240
+ return void 0;
1241
+ }
1242
+ values.push(row.value);
1243
+ }
1244
+ return values.length ? values : void 0;
1245
+ }
1246
+ #isHighCardinality(column2) {
1247
+ const nDistinct = column2.stats?.nDistinct;
1248
+ if (nDistinct == null) return false;
1249
+ if (nDistinct > 0) return nDistinct > this.lowCardinalityLimit;
1250
+ return true;
1251
+ }
1252
+ };
1253
+
1262
1254
  // packages/text2sql/src/lib/adapters/postgres/constraint.postgres.grounding.ts
1263
1255
  var PostgresConstraintGrounding = class extends ConstraintGrounding {
1264
1256
  #adapter;
@@ -1452,101 +1444,6 @@ var PostgresInfoGrounding = class extends InfoGrounding {
1452
1444
  }
1453
1445
  };
1454
1446
 
1455
- // packages/text2sql/src/lib/adapters/postgres/column-values.postgres.grounding.ts
1456
- var PostgresColumnValuesGrounding = class extends ColumnValuesGrounding {
1457
- #adapter;
1458
- #enumCache = /* @__PURE__ */ new Map();
1459
- #enumCacheLoaded = false;
1460
- constructor(adapter, config = {}) {
1461
- super(config);
1462
- this.#adapter = adapter;
1463
- }
1464
- /**
1465
- * Load all ENUM types and their values into cache.
1466
- * This is more efficient than querying per-column.
1467
- */
1468
- async #loadEnumCache() {
1469
- if (this.#enumCacheLoaded) {
1470
- return;
1471
- }
1472
- const rows = await this.#adapter.runQuery(`
1473
- SELECT
1474
- t.typname AS type_name,
1475
- n.nspname AS type_schema,
1476
- e.enumlabel AS enum_value
1477
- FROM pg_type t
1478
- JOIN pg_enum e ON t.oid = e.enumtypid
1479
- JOIN pg_namespace n ON n.oid = t.typnamespace
1480
- ORDER BY t.typname, e.enumsortorder
1481
- `);
1482
- for (const row of rows) {
1483
- const key = `${row.type_schema}.${row.type_name}`;
1484
- const existing = this.#enumCache.get(key) ?? [];
1485
- existing.push(row.enum_value);
1486
- this.#enumCache.set(key, existing);
1487
- const simpleKey = row.type_name;
1488
- const simpleExisting = this.#enumCache.get(simpleKey) ?? [];
1489
- simpleExisting.push(row.enum_value);
1490
- this.#enumCache.set(simpleKey, simpleExisting);
1491
- }
1492
- this.#enumCacheLoaded = true;
1493
- }
1494
- async collectEnumValues(tableName, column2) {
1495
- if (column2.type.toLowerCase() !== "user-defined") {
1496
- return void 0;
1497
- }
1498
- await this.#loadEnumCache();
1499
- const { schema, table: table2 } = this.#adapter.parseTableName(tableName);
1500
- const rows = await this.#adapter.runQuery(`
1501
- SELECT udt_name, udt_schema
1502
- FROM information_schema.columns
1503
- WHERE table_schema = '${this.#adapter.escapeString(schema)}'
1504
- AND table_name = '${this.#adapter.escapeString(table2)}'
1505
- AND column_name = '${this.#adapter.escapeString(column2.name)}'
1506
- `);
1507
- if (!rows.length) {
1508
- return void 0;
1509
- }
1510
- const { udt_name, udt_schema } = rows[0];
1511
- const fullKey = `${udt_schema}.${udt_name}`;
1512
- const values = this.#enumCache.get(fullKey) ?? this.#enumCache.get(udt_name);
1513
- return values?.length ? values : void 0;
1514
- }
1515
- async collectLowCardinality(tableName, column2) {
1516
- if (this.#isHighCardinality(column2)) {
1517
- return void 0;
1518
- }
1519
- const { schema, table: table2 } = this.#adapter.parseTableName(tableName);
1520
- const tableIdentifier = `${this.#adapter.quoteIdentifier(schema)}.${this.#adapter.quoteIdentifier(table2)}`;
1521
- const columnIdentifier = this.#adapter.quoteIdentifier(column2.name);
1522
- const limit = this.lowCardinalityLimit + 1;
1523
- const sql = `
1524
- SELECT DISTINCT ${columnIdentifier}::text AS value
1525
- FROM ${tableIdentifier}
1526
- WHERE ${columnIdentifier} IS NOT NULL
1527
- LIMIT ${limit}
1528
- `;
1529
- const rows = await this.#adapter.runQuery(sql);
1530
- if (!rows.length || rows.length > this.lowCardinalityLimit) {
1531
- return void 0;
1532
- }
1533
- const values = [];
1534
- for (const row of rows) {
1535
- if (row.value == null) {
1536
- return void 0;
1537
- }
1538
- values.push(row.value);
1539
- }
1540
- return values.length ? values : void 0;
1541
- }
1542
- #isHighCardinality(column2) {
1543
- const nDistinct = column2.stats?.nDistinct;
1544
- if (nDistinct == null) return false;
1545
- if (nDistinct > 0) return nDistinct > this.lowCardinalityLimit;
1546
- return true;
1547
- }
1548
- };
1549
-
1550
1447
  // packages/text2sql/src/lib/adapters/postgres/postgres.ts
1551
1448
  var POSTGRES_ERROR_MAP = {
1552
1449
  "42P01": {
@@ -2307,9 +2204,6 @@ function constraints(config = {}) {
2307
2204
  return new PostgresConstraintGrounding(adapter, config);
2308
2205
  };
2309
2206
  }
2310
- function report(config = {}) {
2311
- return (adapter) => new ReportGrounding(adapter, config);
2312
- }
2313
2207
  var postgres_default = {
2314
2208
  tables,
2315
2209
  info,
@@ -2319,7 +2213,6 @@ var postgres_default = {
2319
2213
  indexes,
2320
2214
  rowCount,
2321
2215
  constraints,
2322
- report,
2323
2216
  Postgres
2324
2217
  };
2325
2218
  export {
@@ -2331,7 +2224,6 @@ export {
2331
2224
  formatPostgresError,
2332
2225
  indexes,
2333
2226
  info,
2334
- report,
2335
2227
  rowCount,
2336
2228
  tables,
2337
2229
  views