sqlingo 0.15.2 → 0.16.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.
- package/CHANGELOG.md +9 -0
- package/dist/chunk-2JAK34HX.js +3 -0
- package/dist/{chunk-QBQHDSEC.js.map → chunk-2JAK34HX.js.map} +1 -1
- package/dist/{chunk-I3W3TYLQ.js → chunk-4OP3W4FC.js} +2 -2
- package/dist/{chunk-SBCEJTHD.js → chunk-4OTG4X3J.js} +2 -2
- package/dist/{chunk-EZ7VKF2X.js → chunk-5FKQ7A6G.js} +2 -2
- package/dist/chunk-5THLNRNM.cjs +2 -0
- package/dist/{chunk-KCIBVGFH.cjs.map → chunk-5THLNRNM.cjs.map} +1 -1
- package/dist/{chunk-XHLLQOF5.js → chunk-64O4OPUU.js} +2 -2
- package/dist/chunk-67DW3T46.js +4 -0
- package/dist/chunk-67DW3T46.js.map +1 -0
- package/dist/chunk-6KUVOUCW.cjs +2 -0
- package/dist/{chunk-3QM5KG7E.cjs.map → chunk-6KUVOUCW.cjs.map} +1 -1
- package/dist/chunk-7UD5CTLX.cjs +19 -0
- package/dist/chunk-7UD5CTLX.cjs.map +1 -0
- package/dist/{chunk-QZPWUNHV.js → chunk-A5G7IMQK.js} +2 -2
- package/dist/chunk-BITCYYS5.cjs +2 -0
- package/dist/{chunk-OEILKTSQ.cjs.map → chunk-BITCYYS5.cjs.map} +1 -1
- package/dist/chunk-CUS24L3N.js +2 -0
- package/dist/{chunk-RCVHOYS7.js.map → chunk-CUS24L3N.js.map} +1 -1
- package/dist/chunk-D25ZGOM2.cjs +2 -0
- package/dist/{chunk-AHVZOF3N.cjs.map → chunk-D25ZGOM2.cjs.map} +1 -1
- package/dist/chunk-EHRFM7IA.cjs +3 -0
- package/dist/{chunk-T4W65P5Z.cjs.map → chunk-EHRFM7IA.cjs.map} +1 -1
- package/dist/chunk-G26NEHSB.cjs +2 -0
- package/dist/{chunk-Z3MZONPC.cjs.map → chunk-G26NEHSB.cjs.map} +1 -1
- package/dist/chunk-G7GKKZ2Z.cjs +2 -0
- package/dist/{chunk-L3QLTSDN.cjs.map → chunk-G7GKKZ2Z.cjs.map} +1 -1
- package/dist/{chunk-OY3TUTWZ.js → chunk-GGYOWED7.js} +2 -2
- package/dist/chunk-GMKLNR4T.cjs +2 -0
- package/dist/{chunk-FERVZ62T.cjs.map → chunk-GMKLNR4T.cjs.map} +1 -1
- package/dist/{chunk-6KKXEKNM.js → chunk-H6VPT4VP.js} +2 -2
- package/dist/{chunk-5UEGDJPA.js → chunk-IJFK5335.js} +2 -2
- package/dist/chunk-LCBD2UXS.js +19 -0
- package/dist/chunk-LCBD2UXS.js.map +1 -0
- package/dist/{chunk-AXLGFQDQ.js → chunk-LLQJXXH6.js} +2 -2
- package/dist/{chunk-3AQZGJOE.js → chunk-NAVY4G2K.js} +2 -2
- package/dist/chunk-NDIDRDXO.cjs +2 -0
- package/dist/{chunk-WEST5GIF.cjs.map → chunk-NDIDRDXO.cjs.map} +1 -1
- package/dist/chunk-OV5NMNJ2.cjs +2 -0
- package/dist/{chunk-3UWWG7AW.cjs.map → chunk-OV5NMNJ2.cjs.map} +1 -1
- package/dist/{chunk-HN5OT37U.js → chunk-PMAPVROX.js} +2 -2
- package/dist/chunk-Q3PM5YRJ.cjs +2 -0
- package/dist/{chunk-GVRTGQV6.cjs.map → chunk-Q3PM5YRJ.cjs.map} +1 -1
- package/dist/chunk-QEQ3Y6LY.cjs +2 -0
- package/dist/{chunk-MVIZ3OGF.cjs.map → chunk-QEQ3Y6LY.cjs.map} +1 -1
- package/dist/chunk-R77F352G.cjs +2 -0
- package/dist/{chunk-MFCEAG7X.cjs.map → chunk-R77F352G.cjs.map} +1 -1
- package/dist/chunk-RGHZFBPX.cjs +2 -0
- package/dist/{chunk-3QPO4PHG.cjs.map → chunk-RGHZFBPX.cjs.map} +1 -1
- package/dist/{chunk-4GEW5JA6.js → chunk-RNKKNWL6.js} +2 -2
- package/dist/{chunk-U2DYJPRT.js → chunk-RZCZUHMK.js} +2 -2
- package/dist/chunk-UMHZAUZU.cjs +2 -0
- package/dist/{chunk-4OWUZIBT.cjs.map → chunk-UMHZAUZU.cjs.map} +1 -1
- package/dist/{chunk-BLUVFIA7.cjs → chunk-VAXCS6SH.cjs} +2 -2
- package/dist/{chunk-BLUVFIA7.cjs.map → chunk-VAXCS6SH.cjs.map} +1 -1
- package/dist/{chunk-JLPP24SP.js → chunk-W2TEB56Y.js} +2 -2
- package/dist/{chunk-UN6XWES6.js → chunk-YAQ7F76P.js} +2 -2
- package/dist/chunk-YBZUKWAG.cjs +4 -0
- package/dist/chunk-YBZUKWAG.cjs.map +1 -0
- package/dist/chunk-ZJVBOKDB.cjs +2 -0
- package/dist/{chunk-57QCCXIJ.cjs.map → chunk-ZJVBOKDB.cjs.map} +1 -1
- package/dist/dialects/athena.cjs +1 -1
- package/dist/dialects/athena.d.cts +1 -1
- package/dist/dialects/athena.d.ts +1 -1
- package/dist/dialects/athena.js +1 -1
- package/dist/dialects/bigquery.cjs +1 -1
- package/dist/dialects/bigquery.d.cts +1 -1
- package/dist/dialects/bigquery.d.ts +1 -1
- package/dist/dialects/bigquery.js +1 -1
- package/dist/dialects/clickhouse.cjs +1 -1
- package/dist/dialects/clickhouse.d.cts +1 -1
- package/dist/dialects/clickhouse.d.ts +1 -1
- package/dist/dialects/clickhouse.js +1 -1
- package/dist/dialects/databricks.cjs +1 -1
- package/dist/dialects/databricks.d.cts +1 -1
- package/dist/dialects/databricks.d.ts +1 -1
- package/dist/dialects/databricks.js +1 -1
- package/dist/dialects/doris.cjs +1 -1
- package/dist/dialects/doris.d.cts +1 -1
- package/dist/dialects/doris.d.ts +1 -1
- package/dist/dialects/doris.js +1 -1
- package/dist/dialects/dremio.cjs +1 -1
- package/dist/dialects/dremio.d.cts +1 -1
- package/dist/dialects/dremio.d.ts +1 -1
- package/dist/dialects/dremio.js +1 -1
- package/dist/dialects/drill.cjs +1 -1
- package/dist/dialects/drill.d.cts +1 -1
- package/dist/dialects/drill.d.ts +1 -1
- package/dist/dialects/drill.js +1 -1
- package/dist/dialects/druid.cjs +1 -1
- package/dist/dialects/druid.d.cts +1 -1
- package/dist/dialects/druid.d.ts +1 -1
- package/dist/dialects/druid.js +1 -1
- package/dist/dialects/duckdb.cjs +13 -13
- package/dist/dialects/duckdb.d.cts +1 -1
- package/dist/dialects/duckdb.d.ts +1 -1
- package/dist/dialects/duckdb.js +1 -1
- package/dist/dialects/dune.cjs +1 -1
- package/dist/dialects/dune.d.cts +1 -1
- package/dist/dialects/dune.d.ts +1 -1
- package/dist/dialects/dune.js +1 -1
- package/dist/dialects/exasol.cjs +1 -1
- package/dist/dialects/exasol.d.cts +1 -1
- package/dist/dialects/exasol.d.ts +1 -1
- package/dist/dialects/exasol.js +1 -1
- package/dist/dialects/fabric.cjs +1 -1
- package/dist/dialects/fabric.d.cts +1 -1
- package/dist/dialects/fabric.d.ts +1 -1
- package/dist/dialects/fabric.js +1 -1
- package/dist/dialects/hive.cjs +1 -1
- package/dist/dialects/hive.d.cts +1 -1
- package/dist/dialects/hive.d.ts +1 -1
- package/dist/dialects/hive.js +1 -1
- package/dist/dialects/materialize.cjs +1 -1
- package/dist/dialects/materialize.d.cts +1 -1
- package/dist/dialects/materialize.d.ts +1 -1
- package/dist/dialects/materialize.js +1 -1
- package/dist/dialects/mysql.cjs +1 -1
- package/dist/dialects/mysql.d.cts +1 -1
- package/dist/dialects/mysql.d.ts +1 -1
- package/dist/dialects/mysql.js +1 -1
- package/dist/dialects/oracle.cjs +1 -1
- package/dist/dialects/oracle.cjs.map +1 -1
- package/dist/dialects/oracle.d.cts +1 -1
- package/dist/dialects/oracle.d.ts +1 -1
- package/dist/dialects/oracle.js +1 -1
- package/dist/dialects/oracle.js.map +1 -1
- package/dist/dialects/postgres.cjs +1 -1
- package/dist/dialects/postgres.d.cts +1 -1
- package/dist/dialects/postgres.d.ts +1 -1
- package/dist/dialects/postgres.js +1 -1
- package/dist/dialects/presto.cjs +1 -1
- package/dist/dialects/presto.d.cts +1 -1
- package/dist/dialects/presto.d.ts +1 -1
- package/dist/dialects/presto.js +1 -1
- package/dist/dialects/prql.cjs +1 -1
- package/dist/dialects/prql.d.cts +1 -1
- package/dist/dialects/prql.d.ts +1 -1
- package/dist/dialects/prql.js +1 -1
- package/dist/dialects/redshift.cjs +1 -1
- package/dist/dialects/redshift.d.cts +1 -1
- package/dist/dialects/redshift.d.ts +1 -1
- package/dist/dialects/redshift.js +1 -1
- package/dist/dialects/risingwave.cjs +1 -1
- package/dist/dialects/risingwave.d.cts +1 -1
- package/dist/dialects/risingwave.d.ts +1 -1
- package/dist/dialects/risingwave.js +1 -1
- package/dist/dialects/singlestore.cjs +1 -1
- package/dist/dialects/singlestore.d.cts +1 -1
- package/dist/dialects/singlestore.d.ts +1 -1
- package/dist/dialects/singlestore.js +1 -1
- package/dist/dialects/snowflake.cjs +1 -1
- package/dist/dialects/snowflake.d.cts +1 -1
- package/dist/dialects/snowflake.d.ts +1 -1
- package/dist/dialects/snowflake.js +1 -1
- package/dist/dialects/solr.cjs +1 -1
- package/dist/dialects/solr.d.cts +1 -1
- package/dist/dialects/solr.d.ts +1 -1
- package/dist/dialects/solr.js +1 -1
- package/dist/dialects/spark.cjs +1 -1
- package/dist/dialects/spark.d.cts +1 -1
- package/dist/dialects/spark.d.ts +1 -1
- package/dist/dialects/spark.js +1 -1
- package/dist/dialects/spark2.cjs +1 -1
- package/dist/dialects/spark2.d.cts +1 -1
- package/dist/dialects/spark2.d.ts +1 -1
- package/dist/dialects/spark2.js +1 -1
- package/dist/dialects/sqlite.cjs +1 -1
- package/dist/dialects/sqlite.d.cts +1 -1
- package/dist/dialects/sqlite.d.ts +1 -1
- package/dist/dialects/sqlite.js +1 -1
- package/dist/dialects/starrocks.cjs +1 -1
- package/dist/dialects/starrocks.d.cts +1 -1
- package/dist/dialects/starrocks.d.ts +1 -1
- package/dist/dialects/starrocks.js +1 -1
- package/dist/dialects/tableau.cjs +1 -1
- package/dist/dialects/tableau.d.cts +1 -1
- package/dist/dialects/tableau.d.ts +1 -1
- package/dist/dialects/tableau.js +1 -1
- package/dist/dialects/teradata.cjs +1 -1
- package/dist/dialects/teradata.d.cts +1 -1
- package/dist/dialects/teradata.d.ts +1 -1
- package/dist/dialects/teradata.js +1 -1
- package/dist/dialects/trino.cjs +1 -1
- package/dist/dialects/trino.d.cts +1 -1
- package/dist/dialects/trino.d.ts +1 -1
- package/dist/dialects/trino.js +1 -1
- package/dist/dialects/tsql.cjs +1 -1
- package/dist/dialects/tsql.d.cts +1 -1
- package/dist/dialects/tsql.d.ts +1 -1
- package/dist/dialects/tsql.js +1 -1
- package/dist/index.cjs +5 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -46
- package/dist/index.d.ts +4 -46
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/optimizer/index.cjs +2 -0
- package/dist/optimizer/index.cjs.map +1 -0
- package/dist/optimizer/index.d.cts +799 -0
- package/dist/optimizer/index.d.ts +799 -0
- package/dist/optimizer/index.js +2 -0
- package/dist/optimizer/index.js.map +1 -0
- package/dist/optimizer-D0bukogM.d.ts +50 -0
- package/dist/optimizer-DJ7THBIw.d.cts +50 -0
- package/dist/{tokens-BXP7BR0y.d.ts → tokens-CkPMs-EM.d.cts} +67 -5
- package/dist/{tokens-BXP7BR0y.d.cts → tokens-CkPMs-EM.d.ts} +67 -5
- package/package.json +6 -1
- package/dist/chunk-3QM5KG7E.cjs +0 -2
- package/dist/chunk-3QPO4PHG.cjs +0 -2
- package/dist/chunk-3UWWG7AW.cjs +0 -2
- package/dist/chunk-4OWUZIBT.cjs +0 -2
- package/dist/chunk-57QCCXIJ.cjs +0 -2
- package/dist/chunk-AHVZOF3N.cjs +0 -2
- package/dist/chunk-DI2QZRFR.cjs +0 -4
- package/dist/chunk-DI2QZRFR.cjs.map +0 -1
- package/dist/chunk-FERVZ62T.cjs +0 -2
- package/dist/chunk-GVRTGQV6.cjs +0 -2
- package/dist/chunk-KCIBVGFH.cjs +0 -2
- package/dist/chunk-L3QLTSDN.cjs +0 -2
- package/dist/chunk-LXNDZZBH.js +0 -19
- package/dist/chunk-LXNDZZBH.js.map +0 -1
- package/dist/chunk-MFCEAG7X.cjs +0 -2
- package/dist/chunk-MVIZ3OGF.cjs +0 -2
- package/dist/chunk-NJEMK7XN.js +0 -4
- package/dist/chunk-NJEMK7XN.js.map +0 -1
- package/dist/chunk-OEILKTSQ.cjs +0 -2
- package/dist/chunk-QBQHDSEC.js +0 -3
- package/dist/chunk-RCVHOYS7.js +0 -2
- package/dist/chunk-T4W65P5Z.cjs +0 -3
- package/dist/chunk-WEST5GIF.cjs +0 -2
- package/dist/chunk-Y3TUKXK4.cjs +0 -19
- package/dist/chunk-Y3TUKXK4.cjs.map +0 -1
- package/dist/chunk-Z3MZONPC.cjs +0 -2
- /package/dist/{chunk-I3W3TYLQ.js.map → chunk-4OP3W4FC.js.map} +0 -0
- /package/dist/{chunk-SBCEJTHD.js.map → chunk-4OTG4X3J.js.map} +0 -0
- /package/dist/{chunk-EZ7VKF2X.js.map → chunk-5FKQ7A6G.js.map} +0 -0
- /package/dist/{chunk-XHLLQOF5.js.map → chunk-64O4OPUU.js.map} +0 -0
- /package/dist/{chunk-QZPWUNHV.js.map → chunk-A5G7IMQK.js.map} +0 -0
- /package/dist/{chunk-OY3TUTWZ.js.map → chunk-GGYOWED7.js.map} +0 -0
- /package/dist/{chunk-6KKXEKNM.js.map → chunk-H6VPT4VP.js.map} +0 -0
- /package/dist/{chunk-5UEGDJPA.js.map → chunk-IJFK5335.js.map} +0 -0
- /package/dist/{chunk-AXLGFQDQ.js.map → chunk-LLQJXXH6.js.map} +0 -0
- /package/dist/{chunk-3AQZGJOE.js.map → chunk-NAVY4G2K.js.map} +0 -0
- /package/dist/{chunk-HN5OT37U.js.map → chunk-PMAPVROX.js.map} +0 -0
- /package/dist/{chunk-4GEW5JA6.js.map → chunk-RNKKNWL6.js.map} +0 -0
- /package/dist/{chunk-U2DYJPRT.js.map → chunk-RZCZUHMK.js.map} +0 -0
- /package/dist/{chunk-JLPP24SP.js.map → chunk-W2TEB56Y.js.map} +0 -0
- /package/dist/{chunk-UN6XWES6.js.map → chunk-YAQ7F76P.js.map} +0 -0
|
@@ -0,0 +1,799 @@
|
|
|
1
|
+
export { R as RULES } from '../optimizer-D0bukogM.js';
|
|
2
|
+
import { E as Expression, eS as DialectType, ai as JoinExpr, eR as Schema, a9 as IdentifierExpr, D as Dialect, aa as TypeAnnotator, u3 as LtExpr, u5 as LteExpr, q0 as GtExpr, q2 as GteExpr, H as EqExpr, aB as NeqExpr, aL as IsExpr, c2 as RandExpr, y5 as RandnExpr, g4 as AndExpr, wh as OrExpr, vI as NullSafeEqExpr, vK as NullSafeNeqExpr, cS as PropertyEqExpr, k6 as ConcatExpr, ej as DPipeExpr, aD as DataTypeExpr, aP as ColumnDefExpr, x as DateTruncExpr, aM as TimestampTruncExpr, kh as ConnectorExpr, eL as BooleanExpr, eT as Scope, s as TableExpr, z as ColumnExpr } from '../tokens-CkPMs-EM.js';
|
|
3
|
+
export { He as ScopeType, Hf as annotateTypes, Hg as buildScope, Hh as findAllInScope, Hi as findInScope, Hj as traverseScope, Hk as walkInScope } from '../tokens-CkPMs-EM.js';
|
|
4
|
+
import { DateTime } from 'luxon';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a SQL expression into a standard form.
|
|
8
|
+
*
|
|
9
|
+
* This method relies on annotate_types because many of the
|
|
10
|
+
* conversions rely on type inference.
|
|
11
|
+
*
|
|
12
|
+
* Example:
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { parseOne } from 'sqlglot';
|
|
15
|
+
* import { canonicalize } from 'sqlglot/optimizer';
|
|
16
|
+
*
|
|
17
|
+
* const expression = parseOne("SELECT CAST('2020-01-01' AS DATE)");
|
|
18
|
+
* canonicalize(expression).sql();
|
|
19
|
+
* // Removes redundant cast if possible
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param expression - The expression to canonicalize
|
|
23
|
+
* @param options - Canonicalization options
|
|
24
|
+
* @param options.dialect - SQL dialect
|
|
25
|
+
* @returns The canonicalized expression
|
|
26
|
+
*/
|
|
27
|
+
declare function canonicalize(expression: Expression, options?: {
|
|
28
|
+
dialect?: DialectType;
|
|
29
|
+
}): Expression;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Remove unused CTEs from an expression.
|
|
33
|
+
*
|
|
34
|
+
* Example:
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { parseOne } from 'sqlglot';
|
|
37
|
+
* import { eliminateCtes } from 'sqlglot/optimizer';
|
|
38
|
+
*
|
|
39
|
+
* const sql = "WITH y AS (SELECT a FROM x) SELECT a FROM z";
|
|
40
|
+
* const expression = parseOne(sql);
|
|
41
|
+
* eliminateCtes(expression).sql();
|
|
42
|
+
* // 'SELECT a FROM z'
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param expression - Expression to optimize
|
|
46
|
+
* @returns The optimized expression
|
|
47
|
+
*/
|
|
48
|
+
declare function eliminateCtes<E extends Expression>(expression: E): E;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Remove unused joins from an expression.
|
|
52
|
+
*
|
|
53
|
+
* This only removes joins when we know that the join condition doesn't produce duplicate rows.
|
|
54
|
+
*
|
|
55
|
+
* Example:
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { parseOne } from 'sqlglot';
|
|
58
|
+
* import { eliminateJoins } from 'sqlglot/optimizer';
|
|
59
|
+
*
|
|
60
|
+
* const sql = "SELECT x.a FROM x LEFT JOIN (SELECT DISTINCT y.b FROM y) AS y ON x.b = y.b";
|
|
61
|
+
* const expression = parseOne(sql);
|
|
62
|
+
* eliminateJoins(expression).sql();
|
|
63
|
+
* // 'SELECT x.a FROM x'
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @param expression - Expression to optimize
|
|
67
|
+
* @returns The optimized expression
|
|
68
|
+
*/
|
|
69
|
+
declare function eliminateJoins<E extends Expression>(expression: E): E;
|
|
70
|
+
/**
|
|
71
|
+
* Extract the join condition from a join expression.
|
|
72
|
+
*
|
|
73
|
+
* @param join - The join expression
|
|
74
|
+
* @returns Tuple of (source key, join key, remaining predicate)
|
|
75
|
+
*/
|
|
76
|
+
declare function joinCondition(join: JoinExpr): {
|
|
77
|
+
sourceKeys: Expression[];
|
|
78
|
+
joinKeys: Expression[];
|
|
79
|
+
on: Expression;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Rewrite derived tables as CTEs, deduplicating if possible.
|
|
84
|
+
*
|
|
85
|
+
* Example:
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { parseOne } from 'sqlglot';
|
|
88
|
+
* import { eliminateSubqueries } from 'sqlglot/optimizer';
|
|
89
|
+
*
|
|
90
|
+
* const expression = parseOne("SELECT a FROM (SELECT * FROM x) AS y");
|
|
91
|
+
* eliminateSubqueries(expression).sql();
|
|
92
|
+
* // 'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y'
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* This also deduplicates common subqueries:
|
|
96
|
+
* ```ts
|
|
97
|
+
* const expression = parseOne("SELECT a FROM (SELECT * FROM x) AS y CROSS JOIN (SELECT * FROM x) AS z");
|
|
98
|
+
* eliminateSubqueries(expression).sql();
|
|
99
|
+
* // 'WITH y AS (SELECT * FROM x) SELECT a FROM y AS y CROSS JOIN y AS z'
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @param expression - Expression to optimize
|
|
103
|
+
* @returns The optimized expression
|
|
104
|
+
*/
|
|
105
|
+
declare function eliminateSubqueries<E extends Expression>(expression: E): E;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Isolate table references in queries with multiple sources.
|
|
109
|
+
*
|
|
110
|
+
* This wraps table references in a subquery to avoid ambiguous column references
|
|
111
|
+
* when the same table appears multiple times.
|
|
112
|
+
*
|
|
113
|
+
* Example:
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { parseOne } from 'sqlglot';
|
|
116
|
+
* import { isolateTableSelects } from 'sqlglot/optimizer';
|
|
117
|
+
*
|
|
118
|
+
* const sql = "SELECT a FROM t AS t1 JOIN t AS t2 ON t1.id = t2.id";
|
|
119
|
+
* const expression = parseOne(sql);
|
|
120
|
+
* isolateTableSelects(expression).sql();
|
|
121
|
+
* // Wraps t references in subqueries for isolation
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @param expression - Expression to optimize
|
|
125
|
+
* @param options - Optimization options
|
|
126
|
+
* @param options.schema - Database schema
|
|
127
|
+
* @param options.dialect - SQL dialect
|
|
128
|
+
* @returns The optimized expression
|
|
129
|
+
*/
|
|
130
|
+
declare function isolateTableSelects<E extends Expression>(expression: E, options?: {
|
|
131
|
+
schema?: Record<string, unknown> | Schema;
|
|
132
|
+
dialect?: DialectType;
|
|
133
|
+
}): E;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Rewrite sqlglot AST to merge derived tables into the outer query.
|
|
137
|
+
*
|
|
138
|
+
* This also merges CTEs if they are selected from only once.
|
|
139
|
+
*
|
|
140
|
+
* Example:
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { parseOne } from 'sqlglot';
|
|
143
|
+
* import { mergeSubqueries } from 'sqlglot/optimizer';
|
|
144
|
+
*
|
|
145
|
+
* const expression = parseOne("SELECT a FROM (SELECT x.a FROM x) CROSS JOIN y");
|
|
146
|
+
* mergeSubqueries(expression).sql();
|
|
147
|
+
* // 'SELECT x.a FROM x CROSS JOIN y'
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* If `leaveTablesIsolated` is True, this will not merge inner queries into outer
|
|
151
|
+
* queries if it would result in multiple table selects in a single query.
|
|
152
|
+
*
|
|
153
|
+
* Inspired by https://dev.mysql.com/doc/refman/8.0/en/derived-table-optimization.html
|
|
154
|
+
*
|
|
155
|
+
* @param expression - Expression to optimize
|
|
156
|
+
* @param options - Optimization options
|
|
157
|
+
* @param options.leaveTablesIsolated - Don't merge if it creates multiple table selects (default: false)
|
|
158
|
+
* @returns The optimized expression
|
|
159
|
+
*/
|
|
160
|
+
declare function mergeSubqueries<E extends Expression>(expression: E, options?: {
|
|
161
|
+
leaveTablesIsolated?: boolean;
|
|
162
|
+
}): E;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Rewrite sqlglot AST into conjunctive normal form or disjunctive normal form.
|
|
166
|
+
*
|
|
167
|
+
* Example:
|
|
168
|
+
* ```ts
|
|
169
|
+
* import { parseOne } from 'sqlglot';
|
|
170
|
+
* import { normalize } from 'sqlglot/optimizer';
|
|
171
|
+
*
|
|
172
|
+
* const expression = parseOne("(x AND y) OR z");
|
|
173
|
+
* normalize(expression, { dnf: false }).sql();
|
|
174
|
+
* // '(x OR z) AND (y OR z)'
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @param expression - Expression to normalize
|
|
178
|
+
* @param options - Normalization options
|
|
179
|
+
* @param options.dnf - Rewrite in disjunctive normal form instead of CNF (default: false)
|
|
180
|
+
* @param options.maxDistance - Max estimated distance from CNF/DNF to attempt conversion (default: 128)
|
|
181
|
+
* @returns Normalized expression
|
|
182
|
+
*/
|
|
183
|
+
declare function normalize$1(expression: Expression, options?: {
|
|
184
|
+
dnf?: boolean;
|
|
185
|
+
maxDistance?: number;
|
|
186
|
+
[index: string]: unknown;
|
|
187
|
+
}): Expression;
|
|
188
|
+
/**
|
|
189
|
+
* Checks whether a given expression is in a normal form of interest.
|
|
190
|
+
*
|
|
191
|
+
* Example:
|
|
192
|
+
* ```ts
|
|
193
|
+
* import { parseOne } from 'sqlglot';
|
|
194
|
+
* import { normalized } from 'sqlglot/optimizer';
|
|
195
|
+
*
|
|
196
|
+
* normalized(parseOne("(a AND b) OR c OR (d AND e)"), true); // DNF - true
|
|
197
|
+
* normalized(parseOne("(a OR b) AND c")); // CNF - true
|
|
198
|
+
* normalized(parseOne("a AND (b OR c)"), true); // DNF - false
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* @param expression - The expression to check if it's normalized
|
|
202
|
+
* @param dnf - Whether to check for DNF (default: false = check for CNF)
|
|
203
|
+
* @returns True if normalized
|
|
204
|
+
*/
|
|
205
|
+
declare function normalized(expression: Expression, options?: {
|
|
206
|
+
dnf?: boolean;
|
|
207
|
+
}): boolean;
|
|
208
|
+
/**
|
|
209
|
+
* The difference in the number of predicates between a given expression and its normalized form.
|
|
210
|
+
*
|
|
211
|
+
* This is used as an estimate of the cost of the conversion which is exponential in complexity.
|
|
212
|
+
*
|
|
213
|
+
* Example:
|
|
214
|
+
* ```ts
|
|
215
|
+
* import { parseOne } from 'sqlglot';
|
|
216
|
+
* import { normalizationDistance } from 'sqlglot/optimizer';
|
|
217
|
+
*
|
|
218
|
+
* const expression = parseOne("(a AND b) OR (c AND d)");
|
|
219
|
+
* normalizationDistance(expression); // 4
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* @param expression - The expression to compute the normalization distance for
|
|
223
|
+
* @param dnf - Whether to check for DNF (default: false = check for CNF)
|
|
224
|
+
* @param max - Stop early if count exceeds this (default: Infinity)
|
|
225
|
+
* @returns The normalization distance
|
|
226
|
+
*/
|
|
227
|
+
declare function normalizationDistance(expression: Expression, options?: {
|
|
228
|
+
dnf?: boolean;
|
|
229
|
+
max?: number;
|
|
230
|
+
}): number;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Normalize identifiers by converting them to either lower or upper case,
|
|
234
|
+
* ensuring the semantics are preserved in each case (e.g. by respecting
|
|
235
|
+
* case-sensitivity).
|
|
236
|
+
*
|
|
237
|
+
* This transformation reflects how identifiers would be resolved by the engine corresponding
|
|
238
|
+
* to each SQL dialect, and plays a very important role in the standardization of the AST.
|
|
239
|
+
*
|
|
240
|
+
* It's possible to make this a no-op by adding a special comment next to the
|
|
241
|
+
* identifier of interest:
|
|
242
|
+
*
|
|
243
|
+
* SELECT a /* sqlglot.meta case_sensitive *\/ FROM table
|
|
244
|
+
*
|
|
245
|
+
* In this example, the identifier `a` will not be normalized.
|
|
246
|
+
*
|
|
247
|
+
* Note:
|
|
248
|
+
* Some dialects (e.g. DuckDB) treat all identifiers as case-insensitive even
|
|
249
|
+
* when they're quoted, so in these cases all identifiers are normalized.
|
|
250
|
+
*
|
|
251
|
+
* Example:
|
|
252
|
+
* ```ts
|
|
253
|
+
* import { parseOne } from 'sqlglot';
|
|
254
|
+
* import { normalizeIdentifiers } from 'sqlglot/optimizer';
|
|
255
|
+
*
|
|
256
|
+
* const expression = parseOne('SELECT Bar.A AS A FROM "Foo".Bar');
|
|
257
|
+
* normalizeIdentifiers(expression).sql();
|
|
258
|
+
* // 'SELECT bar.a AS a FROM "Foo".bar'
|
|
259
|
+
*
|
|
260
|
+
* normalizeIdentifiers("foo", { dialect: "snowflake" }).sql({ dialect: "snowflake" });
|
|
261
|
+
* // 'FOO'
|
|
262
|
+
* ```
|
|
263
|
+
*
|
|
264
|
+
* @param expression - The expression to transform (Expression or string)
|
|
265
|
+
* @param options - Normalization options
|
|
266
|
+
* @param options.dialect - The dialect to use in order to decide how to normalize identifiers
|
|
267
|
+
* @param options.storeOriginalColumnIdentifiers - Whether to store the original column identifiers
|
|
268
|
+
* in the meta data of the expression in case we want to undo the normalization at a later point
|
|
269
|
+
* @returns The transformed expression
|
|
270
|
+
*/
|
|
271
|
+
declare function normalizeIdentifiers(expression: string, options?: {
|
|
272
|
+
dialect?: DialectType;
|
|
273
|
+
storeOriginalColumnIdentifiers?: boolean;
|
|
274
|
+
}): IdentifierExpr;
|
|
275
|
+
declare function normalizeIdentifiers<E extends Expression>(expression: E, options?: {
|
|
276
|
+
dialect?: DialectType;
|
|
277
|
+
storeOriginalColumnIdentifiers?: boolean;
|
|
278
|
+
}): E;
|
|
279
|
+
declare function normalizeIdentifiers<E extends Expression>(expression: E | string, options?: {
|
|
280
|
+
dialect?: DialectType;
|
|
281
|
+
storeOriginalColumnIdentifiers?: boolean;
|
|
282
|
+
}): E | IdentifierExpr;
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Rewrite sqlglot AST to pushdown predicates in FROMs and JOINs.
|
|
286
|
+
*
|
|
287
|
+
* Example:
|
|
288
|
+
* ```ts
|
|
289
|
+
* import { parseOne } from 'sqlglot';
|
|
290
|
+
* import { pushdownPredicates } from 'sqlglot/optimizer';
|
|
291
|
+
*
|
|
292
|
+
* const sql = "SELECT y.a AS a FROM (SELECT x.a AS a FROM x AS x) AS y WHERE y.a = 1";
|
|
293
|
+
* const expression = parseOne(sql);
|
|
294
|
+
* pushdownPredicates(expression).sql();
|
|
295
|
+
* // 'SELECT y.a AS a FROM (SELECT x.a AS a FROM x AS x WHERE x.a = 1) AS y WHERE TRUE'
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @param expression - Expression to optimize
|
|
299
|
+
* @param options - Optimization options
|
|
300
|
+
* @param options.dialect - SQL dialect
|
|
301
|
+
* @returns The optimized expression
|
|
302
|
+
*/
|
|
303
|
+
declare function pushdownPredicates<E extends Expression>(expression: E, options?: {
|
|
304
|
+
dialect?: DialectType;
|
|
305
|
+
}): E;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Rewrite sqlglot AST to remove unused column projections.
|
|
309
|
+
*
|
|
310
|
+
* Example:
|
|
311
|
+
* ```ts
|
|
312
|
+
* import { parseOne } from 'sqlglot';
|
|
313
|
+
* import { pushdownProjections } from 'sqlglot/optimizer';
|
|
314
|
+
*
|
|
315
|
+
* const sql = "SELECT y.a AS a FROM (SELECT x.a AS a, x.b AS b FROM x) AS y";
|
|
316
|
+
* const expression = parseOne(sql);
|
|
317
|
+
* pushdownProjections(expression).sql();
|
|
318
|
+
* // 'SELECT y.a AS a FROM (SELECT x.a AS a FROM x) AS y'
|
|
319
|
+
* ```
|
|
320
|
+
*
|
|
321
|
+
* @param expression - Expression to optimize
|
|
322
|
+
* @param options - Optimization options
|
|
323
|
+
* @param options.schema - Database schema
|
|
324
|
+
* @param options.removeUnusedSelections - Remove unused SELECT columns (default: true)
|
|
325
|
+
* @param options.dialect - SQL dialect
|
|
326
|
+
* @returns The optimized expression
|
|
327
|
+
*/
|
|
328
|
+
declare function pushdownProjections<E extends Expression>(expression: E, options?: {
|
|
329
|
+
schema?: Record<string, unknown> | Schema;
|
|
330
|
+
removeUnusedSelections?: boolean;
|
|
331
|
+
dialect?: DialectType;
|
|
332
|
+
}): E;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Rewrite sqlglot AST to simplify expressions.
|
|
336
|
+
*
|
|
337
|
+
* Example:
|
|
338
|
+
* ```ts
|
|
339
|
+
* import { parseOne } from 'sqlglot';
|
|
340
|
+
* import { simplify } from 'sqlglot/optimizer';
|
|
341
|
+
*
|
|
342
|
+
* const expression = parseOne("TRUE AND TRUE");
|
|
343
|
+
* simplify(expression).sql();
|
|
344
|
+
* // 'TRUE'
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* @param expression - Expression to simplify
|
|
348
|
+
* @param options - Simplification options
|
|
349
|
+
* @param options.constantPropagation - Whether the constant propagation rule should be used
|
|
350
|
+
* @param options.coalesceSimplification - Whether the simplify coalesce rule should be used.
|
|
351
|
+
* This rule tries to remove coalesce functions, which can be useful in certain analyses but
|
|
352
|
+
* can leave the query more verbose.
|
|
353
|
+
* @param options.dialect - Dialect to use for simplification
|
|
354
|
+
* @returns Simplified expression
|
|
355
|
+
*/
|
|
356
|
+
declare function simplify<E extends Expression>(expression: E, options?: {
|
|
357
|
+
constantPropagation?: boolean;
|
|
358
|
+
coalesceSimplification?: boolean;
|
|
359
|
+
dialect?: DialectType;
|
|
360
|
+
}): E;
|
|
361
|
+
/**
|
|
362
|
+
* Flatten nested connectors (AND/OR).
|
|
363
|
+
*
|
|
364
|
+
* A AND (B AND C) -> A AND B AND C
|
|
365
|
+
* A OR (B OR C) -> A OR B OR C
|
|
366
|
+
*/
|
|
367
|
+
declare function flatten(expression: Expression): Expression;
|
|
368
|
+
/**
|
|
369
|
+
* Normalize/canonicalize dependencies
|
|
370
|
+
*/
|
|
371
|
+
declare class Simplifier {
|
|
372
|
+
readonly dialect: Dialect;
|
|
373
|
+
readonly annotateNewExpressions: boolean;
|
|
374
|
+
readonly _annotator: TypeAnnotator;
|
|
375
|
+
constructor(options?: {
|
|
376
|
+
dialect?: DialectType;
|
|
377
|
+
annotateNewExpressions?: boolean;
|
|
378
|
+
});
|
|
379
|
+
static get SIMPLIFIABLE(): (typeof Expression)[];
|
|
380
|
+
static TINYINT_MIN: number;
|
|
381
|
+
static TINYINT_MAX: number;
|
|
382
|
+
static UTINYINT_MIN: number;
|
|
383
|
+
static UTINYINT_MAX: number;
|
|
384
|
+
static get COMPLEMENT_COMPARISONS(): Record<string, typeof Expression>;
|
|
385
|
+
static get COMPLEMENT_SUBQUERY_PREDICATES(): Record<string, typeof Expression>;
|
|
386
|
+
static get LT_LTE(): readonly [typeof LtExpr, typeof LteExpr];
|
|
387
|
+
static get GT_GTE(): readonly [typeof GtExpr, typeof GteExpr];
|
|
388
|
+
static get COMPARISONS(): readonly [typeof LtExpr, typeof LteExpr, typeof GtExpr, typeof GteExpr, typeof EqExpr, typeof NeqExpr, typeof IsExpr];
|
|
389
|
+
static get INVERSE_COMPARISONS(): Record<string, typeof Expression>;
|
|
390
|
+
static get NONDETERMINISTIC(): readonly [typeof RandExpr, typeof RandnExpr];
|
|
391
|
+
static get AND_OR(): readonly [typeof AndExpr, typeof OrExpr];
|
|
392
|
+
static get INVERSE_DATE_OPS(): Record<string, typeof Expression>;
|
|
393
|
+
static get INVERSE_OPS(): Record<string, typeof Expression>;
|
|
394
|
+
static get NULL_OK(): readonly [typeof NullSafeEqExpr, typeof NullSafeNeqExpr, typeof PropertyEqExpr];
|
|
395
|
+
static get CONCATS(): readonly [typeof ConcatExpr, typeof DPipeExpr];
|
|
396
|
+
static get DATETRUNC_BINARY_COMPARISONS(): Record<string, (l: Expression, dt: DateTime, u: string, d: Dialect, t?: DataTypeExpr | ColumnDefExpr) => Expression | undefined>;
|
|
397
|
+
static get DATETRUNC_COMPARISONS(): Set<string>;
|
|
398
|
+
static get DATETRUNCS(): readonly [typeof DateTruncExpr, typeof TimestampTruncExpr];
|
|
399
|
+
static get SAFE_CONNECTOR_ELIMINATION_RESULT(): readonly [typeof ConnectorExpr, typeof BooleanExpr];
|
|
400
|
+
static get JOINS(): readonly (readonly [string, string])[];
|
|
401
|
+
simplify<E extends Expression>(expression: E, options?: {
|
|
402
|
+
constantPropagation?: boolean;
|
|
403
|
+
coalesceSimplification?: boolean;
|
|
404
|
+
}): E;
|
|
405
|
+
_simplify(expression: Expression, options: {
|
|
406
|
+
constantPropagation: boolean;
|
|
407
|
+
coalesceSimplification: boolean;
|
|
408
|
+
}): Expression;
|
|
409
|
+
/**
|
|
410
|
+
* Absorption and elimination.
|
|
411
|
+
*
|
|
412
|
+
* absorption:
|
|
413
|
+
* A AND (A OR B) -> A
|
|
414
|
+
* A OR (A AND B) -> A
|
|
415
|
+
* A AND (NOT A OR B) -> A AND B
|
|
416
|
+
* A OR (NOT A AND B) -> A OR B
|
|
417
|
+
* elimination:
|
|
418
|
+
* (A AND B) OR (A AND NOT B) -> A
|
|
419
|
+
* (A OR B) AND (A OR NOT B) -> A
|
|
420
|
+
*/
|
|
421
|
+
absorbAndEliminate(expression: Expression, options?: {
|
|
422
|
+
root?: boolean;
|
|
423
|
+
}): Expression;
|
|
424
|
+
simplifyConcat(expression: Expression): Expression;
|
|
425
|
+
simplifyConditionals(expression: Expression): Expression;
|
|
426
|
+
/**
|
|
427
|
+
* Demorgan's Law
|
|
428
|
+
* NOT (x OR y) -> NOT x AND NOT y
|
|
429
|
+
* NOT (x AND y) -> NOT x OR NOT y
|
|
430
|
+
*/
|
|
431
|
+
simplifyNot(expression: Expression): Expression;
|
|
432
|
+
/**
|
|
433
|
+
* Simplify connector expressions (AND/OR)
|
|
434
|
+
*/
|
|
435
|
+
simplifyConnectors(expression: Expression, options?: {
|
|
436
|
+
root?: boolean;
|
|
437
|
+
}): Expression;
|
|
438
|
+
simplifyComparison(expression: Expression, left: Expression, right: Expression, options: {
|
|
439
|
+
or: boolean;
|
|
440
|
+
}): Expression | undefined;
|
|
441
|
+
flatSimplify(expression: Expression, simplifyFunc: (expr: Expression, left: Expression, right: Expression) => Expression | undefined, options: {
|
|
442
|
+
root?: boolean;
|
|
443
|
+
}): Expression;
|
|
444
|
+
/**
|
|
445
|
+
* Removing complements.
|
|
446
|
+
*
|
|
447
|
+
* A AND NOT A -> FALSE (only for non-NULL A)
|
|
448
|
+
* A OR NOT A -> TRUE (only for non-NULL A)
|
|
449
|
+
*/
|
|
450
|
+
removeComplements(expression: Expression, options?: {
|
|
451
|
+
root?: boolean;
|
|
452
|
+
}): Expression;
|
|
453
|
+
simplifyCoalesce(expression: Expression): Expression;
|
|
454
|
+
simplifyLiterals(expression: Expression, options?: {
|
|
455
|
+
root?: boolean;
|
|
456
|
+
}): Expression;
|
|
457
|
+
simplifyIntegerCast(expr: Expression): Expression;
|
|
458
|
+
simplifyBinary(expression: Expression, a: Expression, b: Expression): Expression | undefined;
|
|
459
|
+
/**
|
|
460
|
+
* Use the subtraction and addition properties of equality to simplify expressions:
|
|
461
|
+
*
|
|
462
|
+
* x + 1 = 3 becomes x = 2
|
|
463
|
+
*
|
|
464
|
+
* There are two binary operations in the above expression: + and =
|
|
465
|
+
* Here's how we reference all the operands in the code below:
|
|
466
|
+
*
|
|
467
|
+
* l r
|
|
468
|
+
* x + 1 = 3
|
|
469
|
+
* a b
|
|
470
|
+
*/
|
|
471
|
+
simplifyEquality(expression: Expression): Expression;
|
|
472
|
+
private isDatetruncPredicate;
|
|
473
|
+
simplifyDateTrunc(expression: Expression): Expression;
|
|
474
|
+
sortComparison(expression: Expression): Expression;
|
|
475
|
+
/**
|
|
476
|
+
* Reduces a prefix check to either TRUE or FALSE if both the string and the
|
|
477
|
+
* prefix are statically known.
|
|
478
|
+
*
|
|
479
|
+
* Example:
|
|
480
|
+
* simplifyStartswith(parseOne("STARTSWITH('foo', 'f')")).sql()
|
|
481
|
+
* // 'TRUE'
|
|
482
|
+
*/
|
|
483
|
+
simplifyStartsWith(expression: Expression): Expression;
|
|
484
|
+
/**
|
|
485
|
+
* Rewrite x BETWEEN y AND z to x >= y AND x <= z.
|
|
486
|
+
*
|
|
487
|
+
* This is done because comparison simplification is only done on lt/lte/gt/gte
|
|
488
|
+
*/
|
|
489
|
+
rewriteBetween(expression: Expression): Expression;
|
|
490
|
+
/**
|
|
491
|
+
* Uniq and sort a connector.
|
|
492
|
+
*
|
|
493
|
+
* C AND A AND B AND B -> A AND B AND C
|
|
494
|
+
*/
|
|
495
|
+
uniqSort(expression: Expression, options?: {
|
|
496
|
+
root?: boolean;
|
|
497
|
+
}): Expression;
|
|
498
|
+
/**
|
|
499
|
+
* Simple SQL generation for sorting and deduplication.
|
|
500
|
+
* Uses the Gen class for faster generation than the full generator
|
|
501
|
+
*/
|
|
502
|
+
private genSql;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Rewrite sqlglot AST to convert some predicates with subqueries into joins.
|
|
507
|
+
*
|
|
508
|
+
* - Convert scalar subqueries into cross joins
|
|
509
|
+
* - Convert correlated or vectorized subqueries into a group by so it is not a many-to-many left join
|
|
510
|
+
*
|
|
511
|
+
* Example:
|
|
512
|
+
* ```ts
|
|
513
|
+
* import { parseOne } from 'sqlglot';
|
|
514
|
+
* import { unnestSubqueries } from 'sqlglot/optimizer';
|
|
515
|
+
*
|
|
516
|
+
* const expression = parseOne("SELECT * FROM x AS x WHERE (SELECT y.a AS a FROM y AS y WHERE x.a = y.a) = 1");
|
|
517
|
+
* unnestSubqueries(expression).sql();
|
|
518
|
+
* // 'SELECT * FROM x AS x LEFT JOIN (SELECT y.a AS a FROM y AS y WHERE TRUE GROUP BY y.a) AS _u_0 ON x.a = _u_0.a WHERE _u_0.a = 1'
|
|
519
|
+
* ```
|
|
520
|
+
*
|
|
521
|
+
* @param expression - Expression to unnest
|
|
522
|
+
* @returns The unnested expression
|
|
523
|
+
*/
|
|
524
|
+
declare function unnestSubqueries<E extends Expression>(expression: E): E;
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Removes cross joins if possible and reorder joins based on predicate dependencies.
|
|
528
|
+
*
|
|
529
|
+
* Example:
|
|
530
|
+
* ```ts
|
|
531
|
+
* import { parseOne } from 'sqlglot';
|
|
532
|
+
* import { optimizeJoins } from 'sqlglot/optimizer';
|
|
533
|
+
*
|
|
534
|
+
* const expr = parseOne("SELECT * FROM x CROSS JOIN y JOIN z ON x.a = z.a AND y.a = z.a");
|
|
535
|
+
* optimizeJoins(expr).sql();
|
|
536
|
+
* // 'SELECT * FROM x JOIN z ON x.a = z.a AND TRUE JOIN y ON y.a = z.a'
|
|
537
|
+
* ```
|
|
538
|
+
*
|
|
539
|
+
* @param expression - The expression to optimize
|
|
540
|
+
* @returns The optimized expression
|
|
541
|
+
*/
|
|
542
|
+
declare function optimizeJoins(expression: Expression): Expression;
|
|
543
|
+
/**
|
|
544
|
+
* Reorder joins by topological sort order based on predicate references.
|
|
545
|
+
*
|
|
546
|
+
* @param expression - The expression to reorder joins in
|
|
547
|
+
* @returns The expression with reordered joins
|
|
548
|
+
*/
|
|
549
|
+
declare function reorderJoins(expression: Expression): Expression;
|
|
550
|
+
/**
|
|
551
|
+
* Remove INNER and OUTER from joins as they are optional.
|
|
552
|
+
*
|
|
553
|
+
* @param expression - The expression to normalize
|
|
554
|
+
* @returns The normalized expression
|
|
555
|
+
*/
|
|
556
|
+
declare function normalize(expression: Expression): Expression;
|
|
557
|
+
/**
|
|
558
|
+
* Get the set of table names referenced in a join's ON clause,
|
|
559
|
+
* excluding the join's own table.
|
|
560
|
+
*
|
|
561
|
+
* @param join - The join expression
|
|
562
|
+
* @returns Set of referenced table names
|
|
563
|
+
*/
|
|
564
|
+
declare function otherTableNames(join: JoinExpr): Set<string>;
|
|
565
|
+
/**
|
|
566
|
+
* Checks if joins can be reordered without changing query semantics.
|
|
567
|
+
*
|
|
568
|
+
* Joins with a side (LEFT, RIGHT, FULL) cannot be reordered easily,
|
|
569
|
+
* the order affects which rows are included in the result.
|
|
570
|
+
*
|
|
571
|
+
* Example:
|
|
572
|
+
* ```ts
|
|
573
|
+
* import { parseOne } from 'sqlglot';
|
|
574
|
+
* import { isReorderable } from 'sqlglot/optimizer/optimizeJoins';
|
|
575
|
+
*
|
|
576
|
+
* const ast = parseOne("SELECT * FROM x JOIN y ON x.id = y.id JOIN z ON y.id = z.id");
|
|
577
|
+
* const select = ast.find(SelectExpr);
|
|
578
|
+
* isReorderable(select.args.joins ?? []);
|
|
579
|
+
* // true
|
|
580
|
+
*
|
|
581
|
+
* const ast2 = parseOne("SELECT * FROM x LEFT JOIN y ON x.id = y.id JOIN z ON y.id = z.id");
|
|
582
|
+
* const select2 = ast2.find(SelectExpr);
|
|
583
|
+
* isReorderable(select2.args.joins ?? []);
|
|
584
|
+
* // false
|
|
585
|
+
* ```
|
|
586
|
+
*
|
|
587
|
+
* @param joins - Array of join expressions
|
|
588
|
+
* @returns True if joins can be safely reordered
|
|
589
|
+
*/
|
|
590
|
+
declare function isReorderable(joins: Iterable<JoinExpr>): boolean;
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Rewrite sqlglot AST to have normalized and qualified tables and columns.
|
|
594
|
+
*
|
|
595
|
+
* This step is necessary for all further SQLGlot optimizations.
|
|
596
|
+
*
|
|
597
|
+
* Example:
|
|
598
|
+
* ```ts
|
|
599
|
+
* import { parseOne } from 'sqlglot';
|
|
600
|
+
* import { qualify } from 'sqlglot/optimizer';
|
|
601
|
+
*
|
|
602
|
+
* const schema = { tbl: { col: "INT" } };
|
|
603
|
+
* const expression = parseOne("SELECT col FROM tbl");
|
|
604
|
+
* qualify(expression, { schema }).sql();
|
|
605
|
+
* // 'SELECT "tbl"."col" AS "col" FROM "tbl" AS "tbl"'
|
|
606
|
+
* ```
|
|
607
|
+
*
|
|
608
|
+
* @param expression - Expression to qualify
|
|
609
|
+
* @param options - Qualification options
|
|
610
|
+
* @param options.dialect - SQL dialect
|
|
611
|
+
* @param options.db - Default database name for tables
|
|
612
|
+
* @param options.catalog - Default catalog name for tables
|
|
613
|
+
* @param options.schema - Schema to infer column names and types
|
|
614
|
+
* @param options.expandAliasRefs - Whether to expand references to aliases (default: true)
|
|
615
|
+
* @param options.expandStars - Whether to expand star queries (default: true). This is necessary for most optimizer rules!
|
|
616
|
+
* @param options.inferSchema - Whether to infer the schema if missing
|
|
617
|
+
* @param options.isolateTables - Whether to isolate table selects (default: false)
|
|
618
|
+
* @param options.qualifyColumns - Whether to qualify columns (default: true)
|
|
619
|
+
* @param options.allowPartialQualification - Whether to allow partial qualification (default: false)
|
|
620
|
+
* @param options.validateQualifyColumns - Whether to validate columns (default: true)
|
|
621
|
+
* @param options.quoteIdentifiers - Whether to quote identifiers (default: true). This is necessary for case-sensitive queries!
|
|
622
|
+
* @param options.identify - If true, quote all identifiers, else only necessary ones (default: true)
|
|
623
|
+
* @param options.canonicalizeTableAliases - Use canonical aliases (_0, _1, ...) instead of preserving table names (default: false)
|
|
624
|
+
* @param options.onQualify - Callback after a table has been qualified
|
|
625
|
+
* @param options.sql - Original SQL string for error highlighting
|
|
626
|
+
* @returns The qualified expression
|
|
627
|
+
*/
|
|
628
|
+
declare function qualify<E extends Expression>(expression: E, options?: {
|
|
629
|
+
dialect?: DialectType;
|
|
630
|
+
db?: string;
|
|
631
|
+
catalog?: string;
|
|
632
|
+
schema?: Record<string, unknown> | Schema;
|
|
633
|
+
expandAliasRefs?: boolean;
|
|
634
|
+
expandStars?: boolean;
|
|
635
|
+
inferSchema?: boolean;
|
|
636
|
+
isolateTables?: boolean;
|
|
637
|
+
qualifyColumns?: boolean;
|
|
638
|
+
allowPartialQualification?: boolean;
|
|
639
|
+
validateQualifyColumns?: boolean;
|
|
640
|
+
quoteIdentifiers?: boolean;
|
|
641
|
+
identify?: boolean;
|
|
642
|
+
canonicalizeTableAliases?: boolean;
|
|
643
|
+
onQualify?: (expression: Expression) => void;
|
|
644
|
+
sql?: string;
|
|
645
|
+
}): E;
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Rewrite sqlglot AST to have fully qualified columns.
|
|
649
|
+
*
|
|
650
|
+
* Example:
|
|
651
|
+
* ```ts
|
|
652
|
+
* import { parseOne } from 'sqlglot';
|
|
653
|
+
* import { qualifyColumns } from 'sqlglot/optimizer';
|
|
654
|
+
*
|
|
655
|
+
* const schema = { tbl: { col: "INT" } };
|
|
656
|
+
* const expression = parseOne("SELECT col FROM tbl");
|
|
657
|
+
* qualifyColumns(expression, schema).sql();
|
|
658
|
+
* // 'SELECT tbl.col AS col FROM tbl'
|
|
659
|
+
* ```
|
|
660
|
+
*
|
|
661
|
+
* @param expression - Expression to qualify
|
|
662
|
+
* @param options - Qualification options
|
|
663
|
+
* @param options.schema - Database schema
|
|
664
|
+
* @param options.expandAliasRefs - Whether to expand references to aliases (default: true)
|
|
665
|
+
* @param options.expandStars - Whether to expand star queries (default: true)
|
|
666
|
+
* @param options.inferSchema - Whether to infer the schema if missing
|
|
667
|
+
* @param options.allowPartialQualification - Whether to allow partial qualification (default: false)
|
|
668
|
+
* @param options.dialect - SQL dialect
|
|
669
|
+
* @returns The qualified expression
|
|
670
|
+
*
|
|
671
|
+
* Notes:
|
|
672
|
+
* - Currently only handles a single PIVOT or UNPIVOT operator
|
|
673
|
+
*/
|
|
674
|
+
declare function qualifyColumns<E extends Expression>(expression: E, options?: {
|
|
675
|
+
schema?: Record<string, unknown> | Schema;
|
|
676
|
+
expandAliasRefs?: boolean;
|
|
677
|
+
expandStars?: boolean;
|
|
678
|
+
inferSchema?: boolean;
|
|
679
|
+
allowPartialQualification?: boolean;
|
|
680
|
+
dialect?: DialectType;
|
|
681
|
+
}): E;
|
|
682
|
+
/**
|
|
683
|
+
* Raise an error if any columns aren't qualified
|
|
684
|
+
*
|
|
685
|
+
* @param expression - Expression to validate
|
|
686
|
+
* @param sql - Optional SQL string for error highlighting
|
|
687
|
+
* @returns The validated expression
|
|
688
|
+
* @throws OptimizeError if unqualified columns are found
|
|
689
|
+
*/
|
|
690
|
+
declare function validateQualifyColumns<E extends Expression>(expression: E, sql?: string): E;
|
|
691
|
+
declare function qualifyOutputs(scopeOrExpression: Scope | Expression): void;
|
|
692
|
+
declare function quoteIdentifiers(expression: Expression, options?: {
|
|
693
|
+
dialect?: DialectType;
|
|
694
|
+
identify?: boolean;
|
|
695
|
+
}): Expression;
|
|
696
|
+
declare function pushdownCteAliasColumns(scope: Scope): void;
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* Rewrite sqlglot AST to have fully qualified tables. Join constructs such as
|
|
700
|
+
* (t1 JOIN t2) AS t will be expanded into (SELECT * FROM t1 AS t1, t2 AS t2) AS t.
|
|
701
|
+
*
|
|
702
|
+
* Examples:
|
|
703
|
+
* ```ts
|
|
704
|
+
* import { parseOne } from 'sqlglot';
|
|
705
|
+
* import { qualifyTables } from 'sqlglot/optimizer';
|
|
706
|
+
*
|
|
707
|
+
* const expression = parseOne("SELECT 1 FROM tbl");
|
|
708
|
+
* qualifyTables(expression, { db: "db" }).sql();
|
|
709
|
+
* // 'SELECT 1 FROM db.tbl AS tbl'
|
|
710
|
+
*
|
|
711
|
+
* const expression2 = parseOne("SELECT 1 FROM (t1 JOIN t2) AS t");
|
|
712
|
+
* qualifyTables(expression2).sql();
|
|
713
|
+
* // 'SELECT 1 FROM (SELECT * FROM t1 AS t1, t2 AS t2) AS t'
|
|
714
|
+
* ```
|
|
715
|
+
*
|
|
716
|
+
* @param expression - Expression to qualify
|
|
717
|
+
* @param options - Qualification options
|
|
718
|
+
* @param options.db - Database name
|
|
719
|
+
* @param options.catalog - Catalog name
|
|
720
|
+
* @param options.onQualify - Callback after a table has been qualified
|
|
721
|
+
* @param options.dialect - The dialect to parse catalog and schema into
|
|
722
|
+
* @param options.canonicalizeTableAliases - Whether to use canonical aliases (_0, _1, ...)
|
|
723
|
+
* for all sources instead of preserving table names. Defaults to false.
|
|
724
|
+
* @returns The qualified expression
|
|
725
|
+
*/
|
|
726
|
+
declare function qualifyTables<E extends Expression>(expression: E, options?: {
|
|
727
|
+
db?: string | IdentifierExpr;
|
|
728
|
+
catalog?: string | IdentifierExpr;
|
|
729
|
+
onQualify?: (table: TableExpr) => void;
|
|
730
|
+
dialect?: DialectType;
|
|
731
|
+
canonicalizeTableAliases?: boolean;
|
|
732
|
+
}): E;
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Helper class for resolving columns to their source tables.
|
|
736
|
+
*
|
|
737
|
+
* This class provides methods to determine which table a column belongs to,
|
|
738
|
+
* handling ambiguous column names, join contexts, and schema inference.
|
|
739
|
+
*
|
|
740
|
+
* Example:
|
|
741
|
+
* ```ts
|
|
742
|
+
* import { buildScope } from 'sqlglot/optimizer';
|
|
743
|
+
* import { Resolver } from 'sqlglot/optimizer';
|
|
744
|
+
*
|
|
745
|
+
* const scope = buildScope(expression);
|
|
746
|
+
* const resolver = new Resolver(scope, schema);
|
|
747
|
+
* const table = resolver.getTable('column_name');
|
|
748
|
+
* ```
|
|
749
|
+
*/
|
|
750
|
+
declare class Resolver {
|
|
751
|
+
scope: Scope;
|
|
752
|
+
schema: Schema;
|
|
753
|
+
dialect: Dialect;
|
|
754
|
+
private inferSchema;
|
|
755
|
+
private sourceColumns?;
|
|
756
|
+
private unambiguousColumns?;
|
|
757
|
+
private allColumnsCache?;
|
|
758
|
+
private getSourceColumnsCache;
|
|
759
|
+
constructor(scope: Scope, schema: Schema, options?: {
|
|
760
|
+
inferSchema?: boolean;
|
|
761
|
+
});
|
|
762
|
+
/**
|
|
763
|
+
* Get the table for a column name.
|
|
764
|
+
*
|
|
765
|
+
* @param column - The column expression or column name to find the table for
|
|
766
|
+
* @returns The table identifier if it can be found/inferred
|
|
767
|
+
*/
|
|
768
|
+
getTable(column: string | ColumnExpr): Expression | undefined;
|
|
769
|
+
/**
|
|
770
|
+
* All available columns of all sources in this scope
|
|
771
|
+
*/
|
|
772
|
+
get allColumns(): Set<string>;
|
|
773
|
+
/**
|
|
774
|
+
* Get source columns from a set operation (UNION, INTERSECT, EXCEPT).
|
|
775
|
+
*
|
|
776
|
+
* @param expression - The set operation expression
|
|
777
|
+
* @returns List of column names
|
|
778
|
+
*/
|
|
779
|
+
getSourceColumnsFromSetOp(expression: Expression): string[];
|
|
780
|
+
/**
|
|
781
|
+
* Resolve the source columns for a given source name.
|
|
782
|
+
*
|
|
783
|
+
* @param name - The source name
|
|
784
|
+
* @param onlyVisible - Whether to only return visible columns
|
|
785
|
+
* @returns List of column names
|
|
786
|
+
*/
|
|
787
|
+
getSourceColumns(name: string, options?: {
|
|
788
|
+
onlyVisible?: boolean;
|
|
789
|
+
}): string[];
|
|
790
|
+
private getAllSourceColumns;
|
|
791
|
+
private getTableNameFromSources;
|
|
792
|
+
private getColumnJoinContext;
|
|
793
|
+
private getAvailableSourceColumns;
|
|
794
|
+
private getUnambiguousColumns;
|
|
795
|
+
private getUnnestColumnType;
|
|
796
|
+
private getColumnTypeFromScope;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
export { Resolver, Scope, Simplifier, TypeAnnotator, canonicalize, eliminateCtes, eliminateJoins, eliminateSubqueries, flatten, isReorderable, isolateTableSelects, joinCondition, mergeSubqueries, normalizationDistance, normalize$1 as normalize, normalizeIdentifiers, normalize as normalizeJoins, normalized, optimizeJoins, otherTableNames, pushdownCteAliasColumns, pushdownPredicates, pushdownProjections, qualify, qualifyColumns, qualifyOutputs, qualifyTables, quoteIdentifiers, reorderJoins, simplify, unnestSubqueries, validateQualifyColumns };
|