@polyglot-sql/sdk 0.1.11 → 0.1.12

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.cjs CHANGED
@@ -1507,6 +1507,38 @@ class WasmWindowDefBuilder {
1507
1507
  }
1508
1508
  }
1509
1509
  if (Symbol.dispose) WasmWindowDefBuilder.prototype[Symbol.dispose] = WasmWindowDefBuilder.prototype.free;
1510
+ function annotate_types$1(sql, dialect, schema_json) {
1511
+ let deferred4_0;
1512
+ let deferred4_1;
1513
+ try {
1514
+ const retptr = wasm$3.__wbindgen_add_to_stack_pointer(-16);
1515
+ const ptr0 = passStringToWasm0(sql, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
1516
+ const len0 = WASM_VECTOR_LEN;
1517
+ const ptr1 = passStringToWasm0(dialect, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
1518
+ const len1 = WASM_VECTOR_LEN;
1519
+ const ptr2 = passStringToWasm0(schema_json, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
1520
+ const len2 = WASM_VECTOR_LEN;
1521
+ wasm$3.annotate_types(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
1522
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1523
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1524
+ deferred4_0 = r0;
1525
+ deferred4_1 = r1;
1526
+ return getStringFromWasm0(r0, r1);
1527
+ } finally {
1528
+ wasm$3.__wbindgen_add_to_stack_pointer(16);
1529
+ wasm$3.__wbindgen_export4(deferred4_0, deferred4_1, 1);
1530
+ }
1531
+ }
1532
+ function annotate_types_value$1(sql, dialect, schema_json) {
1533
+ const ptr0 = passStringToWasm0(sql, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
1534
+ const len0 = WASM_VECTOR_LEN;
1535
+ const ptr1 = passStringToWasm0(dialect, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
1536
+ const len1 = WASM_VECTOR_LEN;
1537
+ const ptr2 = passStringToWasm0(schema_json, wasm$3.__wbindgen_export, wasm$3.__wbindgen_export2);
1538
+ const len2 = WASM_VECTOR_LEN;
1539
+ const ret = wasm$3.annotate_types_value(ptr0, len0, ptr1, len1, ptr2, len2);
1540
+ return takeObject(ret);
1541
+ }
1510
1542
  function ast_add_where$1(ast_json, condition_json, use_or) {
1511
1543
  let deferred3_0;
1512
1544
  let deferred3_1;
@@ -2840,6 +2872,8 @@ async function __polyglot_init_wasm() {
2840
2872
  __wbg_wasmsetopbuilder_free = __vite__wasmModule.__wbg_wasmsetopbuilder_free;
2841
2873
  __wbg_wasmupdatebuilder_free = __vite__wasmModule.__wbg_wasmupdatebuilder_free;
2842
2874
  __wbg_wasmwindowdefbuilder_free = __vite__wasmModule.__wbg_wasmwindowdefbuilder_free;
2875
+ annotate_types = __vite__wasmModule.annotate_types;
2876
+ annotate_types_value = __vite__wasmModule.annotate_types_value;
2843
2877
  ast_add_where = __vite__wasmModule.ast_add_where;
2844
2878
  ast_get_aggregate_functions = __vite__wasmModule.ast_get_aggregate_functions;
2845
2879
  ast_get_column_names = __vite__wasmModule.ast_get_column_names;
@@ -3024,6 +3058,8 @@ let __wbg_wasmselectbuilder_free;
3024
3058
  let __wbg_wasmsetopbuilder_free;
3025
3059
  let __wbg_wasmupdatebuilder_free;
3026
3060
  let __wbg_wasmwindowdefbuilder_free;
3061
+ let annotate_types;
3062
+ let annotate_types_value;
3027
3063
  let ast_add_where;
3028
3064
  let ast_get_aggregate_functions;
3029
3065
  let ast_get_column_names;
@@ -3210,6 +3246,8 @@ const wasm$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProper
3210
3246
  __wbindgen_export2,
3211
3247
  __wbindgen_export3,
3212
3248
  __wbindgen_export4,
3249
+ annotate_types,
3250
+ annotate_types_value,
3213
3251
  ast_add_where,
3214
3252
  ast_get_aggregate_functions,
3215
3253
  ast_get_column_names,
@@ -3388,6 +3426,8 @@ const wasmModule = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePr
3388
3426
  WasmSetOpBuilder,
3389
3427
  WasmUpdateBuilder,
3390
3428
  WasmWindowDefBuilder,
3429
+ annotate_types: annotate_types$1,
3430
+ annotate_types_value: annotate_types_value$1,
3391
3431
  ast_add_where: ast_add_where$1,
3392
3432
  ast_get_aggregate_functions: ast_get_aggregate_functions$1,
3393
3433
  ast_get_column_names: ast_get_column_names$1,
@@ -3463,6 +3503,16 @@ function isExpressionValue(value) {
3463
3503
  function makeExpr(type, data) {
3464
3504
  return { [type]: data };
3465
3505
  }
3506
+ function getInferredType(expr) {
3507
+ const data = getExprData(expr);
3508
+ if (data && typeof data === "object" && "inferred_type" in data) {
3509
+ const it = data["inferred_type"];
3510
+ if (it != null) {
3511
+ return it;
3512
+ }
3513
+ }
3514
+ return void 0;
3515
+ }
3466
3516
  function isType(type) {
3467
3517
  return (expr) => type in expr;
3468
3518
  }
@@ -4041,6 +4091,7 @@ const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePrope
4041
4091
  getExprType,
4042
4092
  getFunctions,
4043
4093
  getIdentifiers,
4094
+ getInferredType,
4044
4095
  getLiterals,
4045
4096
  getNodeDepth,
4046
4097
  getParent,
@@ -5215,6 +5266,30 @@ function getDialects() {
5215
5266
  function getVersion() {
5216
5267
  return wasm.version();
5217
5268
  }
5269
+ function annotateTypes(sql, dialect = "generic", schema) {
5270
+ try {
5271
+ const schemaJson = schema ? JSON.stringify(schema) : "";
5272
+ if (typeof wasm.annotate_types_value === "function") {
5273
+ return decodeWasmPayload(
5274
+ wasm.annotate_types_value(sql, dialect, schemaJson)
5275
+ );
5276
+ }
5277
+ if (typeof wasm.annotate_types === "function") {
5278
+ return JSON.parse(
5279
+ wasm.annotate_types(sql, dialect, schemaJson)
5280
+ );
5281
+ }
5282
+ return {
5283
+ success: false,
5284
+ error: "annotate_types not available in this WASM build"
5285
+ };
5286
+ } catch (error) {
5287
+ return {
5288
+ success: false,
5289
+ error: `WASM annotate_types failed: ${errorMessage(error)}`
5290
+ };
5291
+ }
5292
+ }
5218
5293
  class Polyglot {
5219
5294
  static instance = null;
5220
5295
  constructor() {
@@ -5282,6 +5357,12 @@ class Polyglot {
5282
5357
  getVersion() {
5283
5358
  return getVersion();
5284
5359
  }
5360
+ /**
5361
+ * Parse SQL and annotate the AST with inferred type information.
5362
+ */
5363
+ annotateTypes(sql, dialect = "generic", schema) {
5364
+ return annotateTypes(sql, dialect, schema);
5365
+ }
5285
5366
  }
5286
5367
  const index = {
5287
5368
  init,
@@ -5291,6 +5372,7 @@ const index = {
5291
5372
  tokenize,
5292
5373
  generate,
5293
5374
  format,
5375
+ annotateTypes,
5294
5376
  getDialects,
5295
5377
  getVersion,
5296
5378
  lineage,
@@ -5318,6 +5400,7 @@ exports.WindowDefBuilder = WindowDefBuilder;
5318
5400
  exports.abs = abs;
5319
5401
  exports.alias = alias;
5320
5402
  exports.and = and;
5403
+ exports.annotateTypes = annotateTypes;
5321
5404
  exports.ast = index$1;
5322
5405
  exports.avg = avg;
5323
5406
  exports.boolean = boolean;
@@ -5351,6 +5434,7 @@ exports.func = func;
5351
5434
  exports.generate = generate;
5352
5435
  exports.getColumns = getColumns;
5353
5436
  exports.getDialects = getDialects;
5437
+ exports.getInferredType = getInferredType;
5354
5438
  exports.getSourceTables = getSourceTables;
5355
5439
  exports.getVersion = getVersion;
5356
5440
  exports.greatest = greatest;
package/dist/index.d.cts CHANGED
@@ -49,6 +49,10 @@ declare type AggFunc = {
49
49
  * LIMIT inside aggregate (e.g., ARRAY_AGG(x ORDER BY y LIMIT 2))
50
50
  */
51
51
  limit?: Expression | null;
52
+ /**
53
+ * Inferred data type from type annotation
54
+ */
55
+ inferred_type?: DataType | null;
52
56
  };
53
57
 
54
58
  /**
@@ -93,6 +97,10 @@ declare type AggregateFunction = {
93
97
  * IGNORE NULLS / RESPECT NULLS
94
98
  */
95
99
  ignore_nulls?: boolean | null;
100
+ /**
101
+ * Inferred data type from type annotation
102
+ */
103
+ inferred_type?: DataType | null;
96
104
  };
97
105
 
98
106
  /**
@@ -146,6 +154,10 @@ declare type Alias = {
146
154
  * Trailing comments that appeared after the alias
147
155
  */
148
156
  trailing_comments: Array<string>;
157
+ /**
158
+ * Inferred data type from type annotation
159
+ */
160
+ inferred_type?: DataType | null;
149
161
  };
150
162
 
151
163
  /** Create an expr AS name alias expression. */
@@ -614,6 +626,51 @@ declare type Annotated = {
614
626
  trailing_comments: Array<string>;
615
627
  };
616
628
 
629
+ /**
630
+ * Parse SQL and annotate the AST with inferred type information.
631
+ *
632
+ * Parses the given SQL and runs bottom-up type inference, populating the
633
+ * `inferred_type` field on value-producing AST nodes (columns, operators,
634
+ * functions, casts, etc.). Use `ast.getInferredType(expr)` to read the
635
+ * inferred type from any expression node.
636
+ *
637
+ * @param sql - The SQL string to parse and annotate
638
+ * @param dialect - The dialect to use for parsing (default: Generic)
639
+ * @param schema - Optional schema for column type resolution
640
+ *
641
+ * @example
642
+ * ```typescript
643
+ * import { annotateTypes, ast, Dialect } from '@polyglot-sql/sdk';
644
+ *
645
+ * const result = annotateTypes(
646
+ * "SELECT a + b FROM t",
647
+ * Dialect.PostgreSQL,
648
+ * { tables: { t: { a: "INT", b: "DOUBLE" } } }
649
+ * );
650
+ *
651
+ * if (result.success) {
652
+ * // Walk the AST and inspect inferred types
653
+ * ast.walk(result.ast![0], (node) => {
654
+ * const dt = ast.getInferredType(node);
655
+ * if (dt) {
656
+ * console.log(ast.getExprType(node), "=>", dt);
657
+ * }
658
+ * });
659
+ * }
660
+ * ```
661
+ */
662
+ export declare function annotateTypes(sql: string, dialect?: Dialect, schema?: Schema): AnnotateTypesResult;
663
+
664
+ /**
665
+ * Result of an annotateTypes operation
666
+ */
667
+ export declare interface AnnotateTypesResult {
668
+ success: boolean;
669
+ /** The AST with `inferred_type` fields populated on value-producing nodes */
670
+ ast?: Expression[];
671
+ error?: string;
672
+ }
673
+
617
674
  /**
618
675
  * Anonymous
619
676
  */
@@ -834,6 +891,7 @@ export declare namespace ast {
834
891
  ExpressionType,
835
892
  getExprData,
836
893
  getExprType,
894
+ getInferredType,
837
895
  isExpressionValue,
838
896
  makeExpr,
839
897
  Alias,
@@ -1139,6 +1197,10 @@ declare type BinaryFunc = {
1139
1197
  * Original function name for round-trip preservation (e.g., NVL vs IFNULL)
1140
1198
  */
1141
1199
  original_name?: string | null;
1200
+ /**
1201
+ * Inferred data type from type annotation
1202
+ */
1203
+ inferred_type?: DataType | null;
1142
1204
  };
1143
1205
 
1144
1206
  /**
@@ -1165,6 +1227,10 @@ declare type BinaryOp = {
1165
1227
  * Comments after the right operand
1166
1228
  */
1167
1229
  trailing_comments: Array<string>;
1230
+ /**
1231
+ * Inferred data type from type annotation
1232
+ */
1233
+ inferred_type?: DataType | null;
1168
1234
  };
1169
1235
 
1170
1236
  /**
@@ -1270,6 +1336,10 @@ declare type Case = {
1270
1336
  * Comments from the CASE keyword (emitted after END)
1271
1337
  */
1272
1338
  comments?: Array<string>;
1339
+ /**
1340
+ * Inferred data type from type annotation
1341
+ */
1342
+ inferred_type?: DataType | null;
1273
1343
  };
1274
1344
 
1275
1345
  /**
@@ -1339,6 +1409,10 @@ declare type Cast = {
1339
1409
  * DEFAULT value ON CONVERSION ERROR (Oracle): CAST(x AS type DEFAULT val ON CONVERSION ERROR)
1340
1410
  */
1341
1411
  default?: Expression | null;
1412
+ /**
1413
+ * Inferred data type from type annotation
1414
+ */
1415
+ inferred_type?: DataType | null;
1342
1416
  };
1343
1417
 
1344
1418
  /** Create a CAST(expr AS type) expression. */
@@ -1535,6 +1609,10 @@ declare type Column = {
1535
1609
  * Source position span
1536
1610
  */
1537
1611
  span?: Span | null;
1612
+ /**
1613
+ * Inferred data type from type annotation
1614
+ */
1615
+ inferred_type?: DataType | null;
1538
1616
  };
1539
1617
 
1540
1618
  /**
@@ -3145,6 +3223,7 @@ declare const _default: {
3145
3223
  tokenize: typeof tokenize;
3146
3224
  generate: typeof generate;
3147
3225
  format: typeof format;
3226
+ annotateTypes: typeof annotateTypes;
3148
3227
  getDialects: typeof getDialects;
3149
3228
  getVersion: typeof getVersion;
3150
3229
  lineage: typeof lineage;
@@ -3334,12 +3413,6 @@ declare type Detach = {
3334
3413
  exists: boolean;
3335
3414
  };
3336
3415
 
3337
- /**
3338
- * Polyglot SQL Dialect Translator
3339
- *
3340
- * A WebAssembly-powered SQL dialect translator that can convert SQL
3341
- * between different database dialects (PostgreSQL, MySQL, BigQuery, etc.)
3342
- */
3343
3416
  /**
3344
3417
  * Supported SQL dialects
3345
3418
  */
@@ -6223,6 +6296,10 @@ declare type Function_2 = {
6223
6296
  * Source position span
6224
6297
  */
6225
6298
  span?: Span | null;
6299
+ /**
6300
+ * Inferred data type from type annotation
6301
+ */
6302
+ inferred_type?: DataType | null;
6226
6303
  };
6227
6304
 
6228
6305
  /**
@@ -6545,6 +6622,27 @@ declare function getFunctions(node: Expression): Expression[];
6545
6622
  */
6546
6623
  declare function getIdentifiers(node: Expression): Expression[];
6547
6624
 
6625
+ /**
6626
+ * Get the inferred data type from an Expression, if it has been type-annotated.
6627
+ *
6628
+ * After calling `annotateTypes()`, value-producing expressions (columns, operators,
6629
+ * functions, casts, etc.) carry an `inferred_type` field with their resolved SQL type.
6630
+ *
6631
+ * @example
6632
+ * ```typescript
6633
+ * const result = annotateTypes("SELECT 1 + 2", Dialect.Generic);
6634
+ * if (result.success) {
6635
+ * const addExpr = result.ast![0]; // the SELECT
6636
+ * // Navigate to the "1 + 2" expression and check its type:
6637
+ * const dt = getInferredType(someExpr);
6638
+ * // dt => { data_type: "int", length: null, integer_spelling: false }
6639
+ * }
6640
+ * ```
6641
+ *
6642
+ * @returns The inferred DataType, or `undefined` if not annotated or not a value-producing expression.
6643
+ */
6644
+ export declare function getInferredType(expr: Expression): DataType | undefined;
6645
+
6548
6646
  /**
6549
6647
  * Get all literals in the AST (via WASM)
6550
6648
  */
@@ -8535,6 +8633,10 @@ declare type LikeOp = {
8535
8633
  * Quantifier: ANY, ALL, or SOME
8536
8634
  */
8537
8635
  quantifier: string | null;
8636
+ /**
8637
+ * Inferred data type from type annotation
8638
+ */
8639
+ inferred_type?: DataType | null;
8538
8640
  };
8539
8641
 
8540
8642
  /**
@@ -8614,11 +8716,30 @@ export declare interface LineageResult {
8614
8716
  /**
8615
8717
  * Trace the lineage of a column through a SQL query using schema metadata.
8616
8718
  *
8719
+ * When a schema is provided, columns are fully qualified and type-annotated.
8720
+ * Each `LineageNode.expression` will have its `inferred_type` field populated
8721
+ * with the resolved SQL data type. Use `ast.getInferredType(node.expression)`
8722
+ * to read it.
8723
+ *
8617
8724
  * @param column - Column name to trace
8618
8725
  * @param sql - SQL string to analyze
8619
8726
  * @param schema - ValidationSchema-compatible schema object
8620
8727
  * @param dialect - Dialect for parsing/qualification (default: 'generic')
8621
8728
  * @param trimSelects - Trim SELECT to only target column (default: false)
8729
+ *
8730
+ * @example
8731
+ * ```typescript
8732
+ * import { lineageWithSchema, ast } from '@polyglot-sql/sdk';
8733
+ *
8734
+ * const result = lineageWithSchema("name", "SELECT name FROM users", {
8735
+ * tables: { users: { name: "TEXT", id: "INT" } }
8736
+ * });
8737
+ *
8738
+ * if (result.success) {
8739
+ * const dt = ast.getInferredType(result.lineage!.expression);
8740
+ * // dt => { data_type: "text" }
8741
+ * }
8742
+ * ```
8622
8743
  */
8623
8744
  export declare function lineageWithSchema(column: string, sql: string, schema: Schema, dialect?: string, trimSelects?: boolean): LineageResult;
8624
8745
 
@@ -10138,6 +10259,10 @@ export declare class Polyglot {
10138
10259
  * Get library version.
10139
10260
  */
10140
10261
  getVersion(): string;
10262
+ /**
10263
+ * Parse SQL and annotate the AST with inferred type information.
10264
+ */
10265
+ annotateTypes(sql: string, dialect?: Dialect, schema?: Schema): AnnotateTypesResult;
10141
10266
  }
10142
10267
 
10143
10268
  /**
@@ -12176,6 +12301,10 @@ declare type Subquery = {
12176
12301
  * Trailing comments after the closing paren
12177
12302
  */
12178
12303
  trailing_comments: Array<string>;
12304
+ /**
12305
+ * Inferred data type from type annotation
12306
+ */
12307
+ inferred_type?: DataType | null;
12179
12308
  };
12180
12309
 
12181
12310
  /**
@@ -13312,6 +13441,10 @@ declare type UnaryFunc = {
13312
13441
  * Original function name for round-trip preservation (e.g., CHAR_LENGTH vs LENGTH)
13313
13442
  */
13314
13443
  original_name?: string | null;
13444
+ /**
13445
+ * Inferred data type from type annotation
13446
+ */
13447
+ inferred_type?: DataType | null;
13315
13448
  };
13316
13449
 
13317
13450
  /**
@@ -13324,6 +13457,10 @@ declare type UnaryOp = {
13324
13457
  * The operand expression.
13325
13458
  */
13326
13459
  this: Expression;
13460
+ /**
13461
+ * Inferred data type from type annotation
13462
+ */
13463
+ inferred_type?: DataType | null;
13327
13464
  };
13328
13465
 
13329
13466
  /**
@@ -13848,6 +13985,10 @@ declare type VarArgFunc = {
13848
13985
  * Original function name for round-trip preservation (e.g., COALESCE vs IFNULL)
13849
13986
  */
13850
13987
  original_name?: string | null;
13988
+ /**
13989
+ * Inferred data type from type annotation
13990
+ */
13991
+ inferred_type?: DataType | null;
13851
13992
  };
13852
13993
 
13853
13994
  /**
@@ -14624,6 +14765,10 @@ declare type WindowFunction = {
14624
14765
  * Oracle KEEP clause: KEEP (DENSE_RANK FIRST|LAST ORDER BY ...)
14625
14766
  */
14626
14767
  keep?: Keep | null;
14768
+ /**
14769
+ * Inferred data type from type annotation
14770
+ */
14771
+ inferred_type?: DataType | null;
14627
14772
  };
14628
14773
 
14629
14774
  /**