@polyglot-sql/sdk 0.4.4 → 0.5.1
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 +63 -1
- package/dist/cdn/polyglot.esm.js +1849 -1670
- package/dist/index.cjs +272 -3
- package/dist/index.d.cts +172 -6
- package/dist/index.d.ts +172 -6
- package/dist/index.js +322 -4
- package/dist/index.node.js +264 -3
- package/dist/manual.js +306 -4
- package/dist/polyglot_sql.wasm +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,22 @@ const result = transpile(
|
|
|
25
25
|
console.log(result.sql[0]); // SELECT COALESCE(a, b) FROM t
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
Use `unsupportedLevel: 'raise'` when you want transpilation to fail instead of
|
|
29
|
+
silently preserving known unsupported target-dialect constructs.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
const strict = transpile(
|
|
33
|
+
'SELECT ARRAY_AGG(x) FROM t',
|
|
34
|
+
Dialect.PostgreSQL,
|
|
35
|
+
Dialect.Fabric,
|
|
36
|
+
{ unsupportedLevel: 'raise' },
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
if (!strict.success) {
|
|
40
|
+
console.error(strict.error);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
28
44
|
### Parse + Generate
|
|
29
45
|
|
|
30
46
|
```typescript
|
|
@@ -35,6 +51,25 @@ const { sql } = generate(ast, Dialect.PostgreSQL);
|
|
|
35
51
|
console.log(sql[0]); // SELECT 1 + 2
|
|
36
52
|
```
|
|
37
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
|
+
|
|
38
73
|
### Format
|
|
39
74
|
|
|
40
75
|
```typescript
|
|
@@ -566,6 +601,29 @@ columns are marked as `table`, CTEs as `cte`, derived queries as
|
|
|
566
601
|
`derived_table`, and virtual sources such as BigQuery `UNNEST(...) AS alias`
|
|
567
602
|
are marked as `virtual`.
|
|
568
603
|
|
|
604
|
+
## Compact Query Analysis
|
|
605
|
+
|
|
606
|
+
Use `analyzeQuery` when you need summary facts instead of the full AST or full
|
|
607
|
+
lineage graph:
|
|
608
|
+
|
|
609
|
+
```typescript
|
|
610
|
+
import { analyzeQuery, Dialect } from '@polyglot-sql/sdk';
|
|
611
|
+
|
|
612
|
+
const result = analyzeQuery('SELECT CAST(total AS TEXT) AS total_text FROM orders', {
|
|
613
|
+
dialect: Dialect.Generic,
|
|
614
|
+
schema: {
|
|
615
|
+
tables: [
|
|
616
|
+
{ name: 'orders', columns: [{ name: 'total', type: 'INT' }] },
|
|
617
|
+
],
|
|
618
|
+
},
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
if (result.success) {
|
|
622
|
+
console.log(result.analysis.projections[0].transformKind); // 'cast'
|
|
623
|
+
console.log(result.analysis.relations[0].name); // 'orders'
|
|
624
|
+
}
|
|
625
|
+
```
|
|
626
|
+
|
|
569
627
|
## OpenLineage Output
|
|
570
628
|
|
|
571
629
|
Generate OpenLineage-compatible JSON payloads from SQL analysis. The SDK only
|
|
@@ -652,6 +710,7 @@ import { Polyglot, Dialect } from '@polyglot-sql/sdk';
|
|
|
652
710
|
const pg = Polyglot.getInstance();
|
|
653
711
|
const result = pg.transpile('SELECT 1', Dialect.MySQL, Dialect.PostgreSQL);
|
|
654
712
|
const formatted = pg.format('SELECT a,b FROM t');
|
|
713
|
+
const dataType = pg.parseDataType('VARCHAR(255)', Dialect.DuckDB);
|
|
655
714
|
const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic, {
|
|
656
715
|
maxInputBytes: 2 * 1024 * 1024,
|
|
657
716
|
maxSetOpChain: 128,
|
|
@@ -664,9 +723,11 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
|
|
|
664
723
|
|
|
665
724
|
| Function | Description |
|
|
666
725
|
|----------|-------------|
|
|
667
|
-
| `transpile(sql, read, write)` | Transpile SQL between dialects |
|
|
726
|
+
| `transpile(sql, read, write, options?)` | Transpile SQL between dialects |
|
|
668
727
|
| `parse(sql, dialect?)` | Parse SQL into AST |
|
|
669
728
|
| `generate(ast, dialect?)` | Generate SQL from AST |
|
|
729
|
+
| `parseDataType(sql, dialect?)` | Parse one standalone SQL data type |
|
|
730
|
+
| `generateDataType(dataType, dialect?)` | Generate SQL from a parsed data type |
|
|
670
731
|
| `format(sql, dialect?)` | Pretty-print SQL |
|
|
671
732
|
| `formatWithOptions(sql, dialect?, options?)` | Pretty-print SQL with guard overrides |
|
|
672
733
|
| `tokenize(sql, dialect?)` | Tokenize SQL into a token stream with source spans |
|
|
@@ -682,6 +743,7 @@ const formattedSafe = pg.formatWithOptions('SELECT a,b FROM t', Dialect.Generic,
|
|
|
682
743
|
| `lineage(column, sql, dialect?, trimSelects?)` | Trace column lineage through a query |
|
|
683
744
|
| `lineageWithSchema(column, sql, schema, dialect?, trimSelects?)` | Trace lineage with schema-based qualification |
|
|
684
745
|
| `getSourceTables(column, sql, dialect?)` | Get source tables for a column |
|
|
746
|
+
| `analyzeQuery(sql, options?)` | Return compact projection, relation, CTE, set-operation, and upstream-reference facts |
|
|
685
747
|
| `openLineageColumnLineage(sql, options)` | Build an OpenLineage columnLineage facet and datasets |
|
|
686
748
|
| `openLineageJobEvent(sql, options)` | Build an OpenLineage JobEvent payload |
|
|
687
749
|
| `openLineageRunEvent(sql, options)` | Build an OpenLineage RunEvent payload |
|