@polyglot-sql/sdk 0.5.0 → 0.5.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/README.md CHANGED
@@ -51,6 +51,25 @@ const { sql } = generate(ast, Dialect.PostgreSQL);
51
51
  console.log(sql[0]); // SELECT 1 + 2
52
52
  ```
53
53
 
54
+ ### Data Types
55
+
56
+ Parse and render standalone SQL data type fragments without wrapping them in a
57
+ statement.
58
+
59
+ ```typescript
60
+ import {
61
+ parseDataType,
62
+ generateDataType,
63
+ Dialect,
64
+ } from '@polyglot-sql/sdk';
65
+
66
+ const parsed = parseDataType('DECIMAL(10, 2)', Dialect.DuckDB);
67
+ if (parsed.success) {
68
+ const rendered = generateDataType(parsed.dataType, Dialect.PostgreSQL);
69
+ console.log(rendered.sql); // DECIMAL(10, 2)
70
+ }
71
+ ```
72
+
54
73
  ### Format
55
74
 
56
75
  ```typescript
@@ -582,6 +601,37 @@ columns are marked as `table`, CTEs as `cte`, derived queries as
582
601
  `derived_table`, and virtual sources such as BigQuery `UNNEST(...) AS alias`
583
602
  are marked as `virtual`.
584
603
 
604
+ ## Compact Query Analysis
605
+
606
+ Use `analyzeQuery` when you need summary facts instead of the full AST or full
607
+ lineage graph. `relations` contains sources visible in the analyzed scope;
608
+ `baseTables` contains deduplicated physical dependencies across nested CTEs,
609
+ derived tables, subqueries, and set-operation branches. With a schema,
610
+ parseable detailed type strings such as `DECIMAL(10,2)` are preserved in
611
+ projection `typeHint` values.
612
+
613
+ ```typescript
614
+ import { analyzeQuery, Dialect } from '@polyglot-sql/sdk';
615
+
616
+ const result = analyzeQuery('SELECT SUM(o.amount) AS total FROM orders AS o', {
617
+ dialect: Dialect.Generic,
618
+ schema: {
619
+ tables: [
620
+ { name: 'orders', columns: [{ name: 'amount', type: 'DECIMAL(10,2)' }] },
621
+ ],
622
+ },
623
+ });
624
+
625
+ if (result.success) {
626
+ console.log(result.analysis.projections[0].transformKind); // 'aggregation'
627
+ console.log(result.analysis.projections[0].typeHint); // 'DECIMAL(10, 2)'
628
+ console.log(result.analysis.baseTables[0].name); // 'orders'
629
+ console.log(result.analysis.baseTables[0].alias); // 'o'
630
+ }
631
+
632
+ const duckdbSummary = analyzeQuery('SELECT 1', Dialect.DuckDB);
633
+ ```
634
+
585
635
  ## OpenLineage Output
586
636
 
587
637
  Generate OpenLineage-compatible JSON payloads from SQL analysis. The SDK only
@@ -668,6 +718,7 @@ import { Polyglot, Dialect } from '@polyglot-sql/sdk';
668
718
  const pg = Polyglot.getInstance();
669
719
  const result = pg.transpile('SELECT 1', Dialect.MySQL, Dialect.PostgreSQL);
670
720
  const formatted = pg.format('SELECT a,b FROM t');
721
+ const dataType = pg.parseDataType('VARCHAR(255)', Dialect.DuckDB);
671
722
  const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic, {
672
723
  maxInputBytes: 2 * 1024 * 1024,
673
724
  maxSetOpChain: 128,
@@ -683,6 +734,8 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
683
734
  | `transpile(sql, read, write, options?)` | Transpile SQL between dialects |
684
735
  | `parse(sql, dialect?)` | Parse SQL into AST |
685
736
  | `generate(ast, dialect?)` | Generate SQL from AST |
737
+ | `parseDataType(sql, dialect?)` | Parse one standalone SQL data type |
738
+ | `generateDataType(dataType, dialect?)` | Generate SQL from a parsed data type |
686
739
  | `format(sql, dialect?)` | Pretty-print SQL |
687
740
  | `formatWithOptions(sql, dialect?, options?)` | Pretty-print SQL with guard overrides |
688
741
  | `tokenize(sql, dialect?)` | Tokenize SQL into a token stream with source spans |
@@ -698,6 +751,7 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
698
751
  | `lineage(column, sql, dialect?, trimSelects?)` | Trace column lineage through a query |
699
752
  | `lineageWithSchema(column, sql, schema, dialect?, trimSelects?)` | Trace lineage with schema-based qualification |
700
753
  | `getSourceTables(column, sql, dialect?)` | Get source tables for a column |
754
+ | `analyzeQuery(sql, optionsOrDialect?)` | Return compact projection, visible relation, transitive base-table, CTE, set-operation, and upstream-reference facts |
701
755
  | `openLineageColumnLineage(sql, options)` | Build an OpenLineage columnLineage facet and datasets |
702
756
  | `openLineageJobEvent(sql, options)` | Build an OpenLineage JobEvent payload |
703
757
  | `openLineageRunEvent(sql, options)` | Build an OpenLineage RunEvent payload |