@polyglot-sql/sdk 0.1.9 → 0.1.10

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/index.d.ts CHANGED
@@ -1524,6 +1524,10 @@ declare type Column = {
1524
1524
  * Trailing comments that appeared after this column reference
1525
1525
  */
1526
1526
  trailing_comments: Array<string>;
1527
+ /**
1528
+ * Source position span
1529
+ */
1530
+ span?: Span | null;
1527
1531
  };
1528
1532
 
1529
1533
  /**
@@ -3131,6 +3135,7 @@ declare const _default: {
3131
3135
  isInitialized: typeof isInitialized;
3132
3136
  transpile: typeof transpile;
3133
3137
  parse: typeof parse;
3138
+ tokenize: typeof tokenize;
3134
3139
  generate: typeof generate;
3135
3140
  format: typeof format;
3136
3141
  getDialects: typeof getDialects;
@@ -6200,6 +6205,10 @@ declare type Function_2 = {
6200
6205
  * Whether the function name was quoted (e.g., `p.d.UdF` in BigQuery)
6201
6206
  */
6202
6207
  quoted: boolean;
6208
+ /**
6209
+ * Source position span
6210
+ */
6211
+ span?: Span | null;
6203
6212
  };
6204
6213
 
6205
6214
  /**
@@ -6862,6 +6871,10 @@ declare type Identifier = {
6862
6871
  */
6863
6872
  quoted: boolean;
6864
6873
  trailing_comments: Array<string>;
6874
+ /**
6875
+ * Source position span (populated during parsing, None for programmatically constructed nodes)
6876
+ */
6877
+ span?: Span | null;
6865
6878
  };
6866
6879
 
6867
6880
  /**
@@ -9730,6 +9743,10 @@ export declare interface ParseResult {
9730
9743
  errorLine?: number;
9731
9744
  /** 1-based column number where the error occurred */
9732
9745
  errorColumn?: number;
9746
+ /** Start byte offset of the error range */
9747
+ errorStart?: number;
9748
+ /** End byte offset of the error range (exclusive) */
9749
+ errorEnd?: number;
9733
9750
  }
9734
9751
 
9735
9752
  /**
@@ -10049,6 +10066,10 @@ export declare class Polyglot {
10049
10066
  * Parse SQL into an AST.
10050
10067
  */
10051
10068
  parse(sql: string, dialect?: Dialect): ParseResult;
10069
+ /**
10070
+ * Tokenize SQL into a token stream.
10071
+ */
10072
+ tokenize(sql: string, dialect?: Dialect): TokenizeResult;
10052
10073
  /**
10053
10074
  * Generate SQL from an AST.
10054
10075
  */
@@ -11715,6 +11736,38 @@ export declare interface SourceTablesResult {
11715
11736
  error?: string;
11716
11737
  }
11717
11738
 
11739
+ /**
11740
+ * Represents a position in the source SQL
11741
+ */
11742
+ declare type Span = {
11743
+ /**
11744
+ * Starting byte offset
11745
+ */
11746
+ start: number;
11747
+ /**
11748
+ * Ending byte offset (exclusive)
11749
+ */
11750
+ end: number;
11751
+ /**
11752
+ * Line number (1-based)
11753
+ */
11754
+ line: number;
11755
+ /**
11756
+ * Column number (1-based)
11757
+ */
11758
+ column: number;
11759
+ };
11760
+
11761
+ /**
11762
+ * Span information for a token, indicating its position in the source SQL.
11763
+ */
11764
+ export declare interface SpanInfo {
11765
+ start: number;
11766
+ end: number;
11767
+ line: number;
11768
+ column: number;
11769
+ }
11770
+
11718
11771
  /**
11719
11772
  * SPLIT function
11720
11773
  */
@@ -11842,6 +11895,10 @@ declare type Star = {
11842
11895
  * Trailing comments that appeared after the star
11843
11896
  */
11844
11897
  trailing_comments?: Array<string>;
11898
+ /**
11899
+ * Source position span
11900
+ */
11901
+ span?: Span | null;
11845
11902
  };
11846
11903
 
11847
11904
  /** Create a star (*) expression. */
@@ -12421,6 +12478,10 @@ declare type TableRef = {
12421
12478
  * Time travel version clause: FOR VERSION AS OF / FOR TIMESTAMP AS OF (Presto/Trino, BigQuery, Databricks)
12422
12479
  */
12423
12480
  version?: Version | null;
12481
+ /**
12482
+ * Source position span
12483
+ */
12484
+ span?: Span | null;
12424
12485
  };
12425
12486
 
12426
12487
  /**
@@ -12711,6 +12772,53 @@ declare type ToFile = {
12711
12772
  safe: Expression | null;
12712
12773
  };
12713
12774
 
12775
+ /**
12776
+ * A single token from the SQL token stream.
12777
+ */
12778
+ export declare interface TokenInfo {
12779
+ tokenType: string;
12780
+ text: string;
12781
+ span: SpanInfo;
12782
+ comments: string[];
12783
+ trailingComments: string[];
12784
+ }
12785
+
12786
+ /**
12787
+ * Tokenize SQL into a token stream.
12788
+ *
12789
+ * @param sql - The SQL string to tokenize
12790
+ * @param dialect - The dialect to use for tokenization
12791
+ * @returns The token stream
12792
+ *
12793
+ * @example
12794
+ * ```typescript
12795
+ * const result = tokenize("SELECT a, b FROM t", Dialect.PostgreSQL);
12796
+ * if (result.success) {
12797
+ * for (const token of result.tokens!) {
12798
+ * console.log(token.tokenType, token.text, token.span);
12799
+ * }
12800
+ * }
12801
+ * ```
12802
+ */
12803
+ export declare function tokenize(sql: string, dialect?: Dialect): TokenizeResult;
12804
+
12805
+ /**
12806
+ * Result of a tokenize operation
12807
+ */
12808
+ export declare interface TokenizeResult {
12809
+ success: boolean;
12810
+ tokens?: TokenInfo[];
12811
+ error?: string;
12812
+ /** 1-based line number where the error occurred */
12813
+ errorLine?: number;
12814
+ /** 1-based column number where the error occurred */
12815
+ errorColumn?: number;
12816
+ /** Start byte offset of the error range */
12817
+ errorStart?: number;
12818
+ /** End byte offset of the error range (exclusive) */
12819
+ errorEnd?: number;
12820
+ }
12821
+
12714
12822
  /**
12715
12823
  * ToMap - Materialize-style map constructor
12716
12824
  * Can hold either:
@@ -12899,6 +13007,10 @@ export declare interface TranspileResult {
12899
13007
  errorLine?: number;
12900
13008
  /** 1-based column number where the error occurred */
12901
13009
  errorColumn?: number;
13010
+ /** Start byte offset of the error range */
13011
+ errorStart?: number;
13012
+ /** End byte offset of the error range (exclusive) */
13013
+ errorEnd?: number;
12902
13014
  }
12903
13015
 
12904
13016
  /**
@@ -13586,6 +13698,10 @@ export declare interface ValidationError {
13586
13698
  severity: ValidationSeverity;
13587
13699
  /** Error code (e.g., "E001", "W001") */
13588
13700
  code: string;
13701
+ /** Start byte offset of the error range */
13702
+ start?: number;
13703
+ /** End byte offset of the error range (exclusive) */
13704
+ end?: number;
13589
13705
  }
13590
13706
 
13591
13707
  /**
package/dist/index.js CHANGED
@@ -2342,6 +2342,55 @@ function source_tables$1(sql, column, dialect) {
2342
2342
  }
2343
2343
  }
2344
2344
 
2345
+ /**
2346
+ * Tokenize SQL into a token stream (returned as JSON).
2347
+ *
2348
+ * # Arguments
2349
+ * * `sql` - The SQL string to tokenize
2350
+ * * `dialect` - The dialect to use for tokenization
2351
+ *
2352
+ * # Returns
2353
+ * A JSON string containing the TokenizeResult
2354
+ * @param {string} sql
2355
+ * @param {string} dialect
2356
+ * @returns {string}
2357
+ */
2358
+ function tokenize$2(sql, dialect) {
2359
+ let deferred3_0;
2360
+ let deferred3_1;
2361
+ try {
2362
+ const retptr = wasm$3.__wbindgen_add_to_stack_pointer(-16);
2363
+ const ptr0 = passStringToWasm0(sql, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
2364
+ const len0 = WASM_VECTOR_LEN;
2365
+ const ptr1 = passStringToWasm0(dialect, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
2366
+ const len1 = WASM_VECTOR_LEN;
2367
+ wasm$3.tokenize(retptr, ptr0, len0, ptr1, len1);
2368
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
2369
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
2370
+ deferred3_0 = r0;
2371
+ deferred3_1 = r1;
2372
+ return getStringFromWasm0(r0, r1);
2373
+ } finally {
2374
+ wasm$3.__wbindgen_add_to_stack_pointer(16);
2375
+ wasm$3.__wbindgen_export4(deferred3_0, deferred3_1, 1);
2376
+ }
2377
+ }
2378
+
2379
+ /**
2380
+ * Tokenize SQL and return a structured JS object.
2381
+ * @param {string} sql
2382
+ * @param {string} dialect
2383
+ * @returns {any}
2384
+ */
2385
+ function tokenize_value$1(sql, dialect) {
2386
+ const ptr0 = passStringToWasm0(sql, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
2387
+ const len0 = WASM_VECTOR_LEN;
2388
+ const ptr1 = passStringToWasm0(dialect, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
2389
+ const len1 = WASM_VECTOR_LEN;
2390
+ const ret = wasm$3.tokenize_value(ptr0, len0, ptr1, len1);
2391
+ return takeObject(ret);
2392
+ }
2393
+
2345
2394
  /**
2346
2395
  * Transpile SQL from one dialect to another.
2347
2396
  *
@@ -3359,6 +3408,8 @@ const parse$1 = __vite__wasmModule.parse;
3359
3408
  const parse_value = __vite__wasmModule.parse_value;
3360
3409
  const plan$1 = __vite__wasmModule.plan;
3361
3410
  const source_tables = __vite__wasmModule.source_tables;
3411
+ const tokenize$1 = __vite__wasmModule.tokenize;
3412
+ const tokenize_value = __vite__wasmModule.tokenize_value;
3362
3413
  const transpile$1 = __vite__wasmModule.transpile;
3363
3414
  const transpile_value = __vite__wasmModule.transpile_value;
3364
3415
  const validate$1 = __vite__wasmModule.validate;
@@ -3544,6 +3595,8 @@ const wasm$2 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
3544
3595
  parse_value,
3545
3596
  plan: plan$1,
3546
3597
  source_tables,
3598
+ tokenize: tokenize$1,
3599
+ tokenize_value,
3547
3600
  transpile: transpile$1,
3548
3601
  transpile_value,
3549
3602
  validate: validate$1,
@@ -3723,6 +3776,8 @@ const wasmModule = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty
3723
3776
  parse_value: parse_value$1,
3724
3777
  plan: plan$2,
3725
3778
  source_tables: source_tables$1,
3779
+ tokenize: tokenize$2,
3780
+ tokenize_value: tokenize_value$1,
3726
3781
  transpile: transpile$2,
3727
3782
  transpile_value: transpile_value$1,
3728
3783
  validate: validate$2,
@@ -5186,10 +5241,16 @@ function validateSelect(select, _options) {
5186
5241
  const errors = [];
5187
5242
  const stars = findByType(select, "star");
5188
5243
  if (stars.length > 0) {
5244
+ const starData = getExprData(stars[0]);
5245
+ const span = starData?.span;
5189
5246
  errors.push({
5190
5247
  message: "SELECT * is discouraged; specify columns explicitly for better performance and maintainability",
5191
5248
  severity: "warning",
5192
- code: "W001"
5249
+ code: "W001",
5250
+ line: span?.line,
5251
+ column: span?.column,
5252
+ start: span?.start,
5253
+ end: span?.end
5193
5254
  });
5194
5255
  }
5195
5256
  const selectData = getExprData(select);
@@ -5205,10 +5266,22 @@ function validateSelect(select, _options) {
5205
5266
  return false;
5206
5267
  });
5207
5268
  if (hasNonAggregateColumn && aggregateFunctions.length > 0) {
5269
+ const firstNonAgg = selectCols.find((col) => {
5270
+ const colType = getExprType(col);
5271
+ if (colType === "column" || colType === "identifier") {
5272
+ return getAggregateFunctions(col).length === 0;
5273
+ }
5274
+ return false;
5275
+ });
5276
+ const colSpan = firstNonAgg ? getExprData(firstNonAgg)?.span : void 0;
5208
5277
  errors.push({
5209
5278
  message: "Mixing aggregate functions with non-aggregated columns without GROUP BY may cause errors in strict SQL mode",
5210
5279
  severity: "warning",
5211
- code: "W002"
5280
+ code: "W002",
5281
+ line: colSpan?.line,
5282
+ column: colSpan?.column,
5283
+ start: colSpan?.start,
5284
+ end: colSpan?.end
5212
5285
  });
5213
5286
  }
5214
5287
  }
@@ -5220,10 +5293,16 @@ function validateSelect(select, _options) {
5220
5293
  });
5221
5294
  }
5222
5295
  if (selectData.limit && !selectData.order_by) {
5296
+ const limitExpr = selectData.limit;
5297
+ const limitSpan = limitExpr ? getExprData(limitExpr)?.span : void 0;
5223
5298
  errors.push({
5224
5299
  message: "LIMIT without ORDER BY produces non-deterministic results",
5225
5300
  severity: "warning",
5226
- code: "W004"
5301
+ code: "W004",
5302
+ line: limitSpan?.line,
5303
+ column: limitSpan?.column,
5304
+ start: limitSpan?.start,
5305
+ end: limitSpan?.end
5227
5306
  });
5228
5307
  }
5229
5308
  return errors;
@@ -5435,6 +5514,22 @@ function parse(sql, dialect = "generic" /* Generic */) {
5435
5514
  return parseFailure("parse", error);
5436
5515
  }
5437
5516
  }
5517
+ function tokenize(sql, dialect = "generic" /* Generic */) {
5518
+ try {
5519
+ if (typeof wasm.tokenize_value === "function") {
5520
+ return decodeWasmPayload(
5521
+ wasm.tokenize_value(sql, dialect)
5522
+ );
5523
+ }
5524
+ return JSON.parse(wasm.tokenize(sql, dialect));
5525
+ } catch (error) {
5526
+ return {
5527
+ success: false,
5528
+ tokens: void 0,
5529
+ error: `WASM tokenize failed: ${errorMessage(error)}`
5530
+ };
5531
+ }
5532
+ }
5438
5533
  function generate(ast2, dialect = "generic" /* Generic */) {
5439
5534
  try {
5440
5535
  if (typeof wasm.generate_value === "function" && Array.isArray(ast2)) {
@@ -5512,6 +5607,12 @@ class Polyglot {
5512
5607
  parse(sql, dialect = "generic" /* Generic */) {
5513
5608
  return parse(sql, dialect);
5514
5609
  }
5610
+ /**
5611
+ * Tokenize SQL into a token stream.
5612
+ */
5613
+ tokenize(sql, dialect = "generic" /* Generic */) {
5614
+ return tokenize(sql, dialect);
5615
+ }
5515
5616
  /**
5516
5617
  * Generate SQL from an AST.
5517
5618
  */
@@ -5549,6 +5650,7 @@ const index = {
5549
5650
  isInitialized,
5550
5651
  transpile,
5551
5652
  parse,
5653
+ tokenize,
5552
5654
  generate,
5553
5655
  format,
5554
5656
  getDialects,
@@ -5563,4 +5665,4 @@ const index = {
5563
5665
  Polyglot
5564
5666
  };
5565
5667
 
5566
- export { CaseBuilder, DeleteBuilder, Dialect, Expr, InsertBuilder, MergeBuilder, Polyglot, SelectBuilder, SetOpBuilder, UpdateBuilder, ValidationSeverity, WindowDefBuilder, abs, alias, and, index$1 as ast, avg, boolean, caseOf, caseWhen, cast, ceil, changesOnly, coalesce, col, concatWs, condition, count, countDistinct, currentDate, currentTime, currentTimestamp, index as default, del, deleteFrom, denseRank, diff, except, exp, extract, findAll, floor, format, formatWithOptions, func, generate, getColumns, getDialects, getSourceTables, getVersion, greatest, hasChanges, ifNull, init, initcap, insert, insertInto, intersect, isColumn, isFunction, isInitialized, isLiteral, isSelect, least, length, lineage, lit, ln, lower, ltrim, max, mergeInto, min, not, nullIf, or, parse, plan, power, rank, renameColumns, replace, reverse, round, rowNumber, rtrim, select, sign, sqlExpr, sqlNull, sqrt, star, subquery, substring, sum, table, transform, transpile, trim, union, unionAll, update, upper, validate, validateWithSchema, walk };
5668
+ export { CaseBuilder, DeleteBuilder, Dialect, Expr, InsertBuilder, MergeBuilder, Polyglot, SelectBuilder, SetOpBuilder, UpdateBuilder, ValidationSeverity, WindowDefBuilder, abs, alias, and, index$1 as ast, avg, boolean, caseOf, caseWhen, cast, ceil, changesOnly, coalesce, col, concatWs, condition, count, countDistinct, currentDate, currentTime, currentTimestamp, index as default, del, deleteFrom, denseRank, diff, except, exp, extract, findAll, floor, format, formatWithOptions, func, generate, getColumns, getDialects, getSourceTables, getVersion, greatest, hasChanges, ifNull, init, initcap, insert, insertInto, intersect, isColumn, isFunction, isInitialized, isLiteral, isSelect, least, length, lineage, lit, ln, lower, ltrim, max, mergeInto, min, not, nullIf, or, parse, plan, power, rank, renameColumns, replace, reverse, round, rowNumber, rtrim, select, sign, sqlExpr, sqlNull, sqrt, star, subquery, substring, sum, table, tokenize, transform, transpile, trim, union, unionAll, update, upper, validate, validateWithSchema, walk };
Binary file
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "@polyglot-sql/sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "SQL dialect translator powered by WebAssembly",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
6
+ "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
9
9
  "unpkg": "./dist/cdn/polyglot.esm.js",
10
10
  "jsdelivr": "./dist/cdn/polyglot.esm.js",
11
11
  "exports": {
12
12
  ".": {
13
- "types": "./dist/index.d.ts",
14
- "import": "./dist/index.js"
13
+ "types": {
14
+ "import": "./dist/index.d.ts",
15
+ "require": "./dist/index.d.cts"
16
+ },
17
+ "import": "./dist/index.js",
18
+ "require": "./dist/index.cjs"
15
19
  },
16
20
  "./cdn": {
17
21
  "types": "./dist/index.d.ts",
@@ -62,10 +66,11 @@
62
66
  "build:bindings": "cd ../.. && make generate-bindings",
63
67
  "build:wasm": "cd ../../crates/polyglot-sql-wasm && wasm-pack build --target bundler --release --out-dir ../../packages/sdk/wasm",
64
68
  "build:esm": "vite build",
69
+ "build:cjs": "vite build --config vite.config.cjs.ts && cp dist/index.d.ts dist/index.d.cts",
65
70
  "build:umd": "vite build --config vite.config.umd.ts",
66
71
  "build:dialect": "vite build --config vite.config.dialect.ts",
67
72
  "build:dialects": "for d in postgresql mysql bigquery snowflake duckdb tsql clickhouse; do POLYGLOT_DIALECT=$d pnpm run build:dialect; done",
68
- "build": "pnpm run build:esm && pnpm run build:umd",
73
+ "build": "pnpm run build:esm && pnpm run build:cjs && pnpm run build:umd",
69
74
  "build:all": "pnpm run build && pnpm run build:dialects",
70
75
  "test": "vitest run",
71
76
  "test:watch": "vitest",