Class: SQLLogicalPlanBuilder
Defined in: lang-sql/src/visitors/builder.ts:88
Translates a SQL AST into a logical plan tree by implementing SQLVisitor and LogicalPlanBuilder.
Implements
Constructors
Constructor
new SQLLogicalPlanBuilder(
db):SQLLogicalPlanBuilder
Defined in: lang-sql/src/visitors/builder.ts:112
Parameters
| Parameter | Type | Description |
|---|---|---|
db | DortDBAsFriend | Reference to the database instance used to resolve functions, operators, aggregates, and casts. |
Returns
SQLLogicalPlanBuilder
Properties
calcBuilders
protectedcalcBuilders:Record<string,PlanVisitor<CalculationParams>>
Defined in: lang-sql/src/visitors/builder.ts:96
Per-language calculation builders used to convert plan fragments into plan.Calculation nodes.
dataAdapter
protecteddataAdapter:SQLDataAdapter<unknown>
Defined in: lang-sql/src/visitors/builder.ts:110
SQL data adapter used to construct row values.
db
protecteddb:DortDBAsFriend
Defined in: lang-sql/src/visitors/builder.ts:114
Reference to the database instance used to resolve functions, operators, aggregates, and casts.
eqCheckers
protectedeqCheckers:Record<string,EqualityChecker>
Defined in: lang-sql/src/visitors/builder.ts:98
Per-language equality checkers for plan-node structural comparison.
inferrerMap
protectedinferrerMap:Record<string,SchemaInferrer> ={}
Defined in: lang-sql/src/visitors/builder.ts:94
Per-language SchemaInferrer instances keyed by language name.
langCtx
protectedlangCtx:Record<string,unknown> &object
Defined in: lang-sql/src/visitors/builder.ts:103
Shared language context passed in via buildPlan; the sql entry holds the outer SQLLangCtx.
Type Declaration
sql
sql:
SQLLangCtx
The SQL-specific language context for the current query scope.
localLangCtx
protectedlocalLangCtx:SQLLangCtx
Defined in: lang-sql/src/visitors/builder.ts:108
copied to keep outer lang ctx clean
renamers
protectedrenamers:Record<string,AttributeRenamer>
Defined in: lang-sql/src/visitors/builder.ts:100
Per-language attribute renamers applied to non-local cross-boundary references.
stringifier
protectedstringifier:ASTDeterministicStringifier
Defined in: lang-sql/src/visitors/builder.ts:92
Produces deterministic string keys for subexpressions used as plan identifiers.
Methods
buildLimit()
protectedbuildLimit(node,op):Limit
Defined in: lang-sql/src/visitors/builder.ts:437
Builds a plan.Limit operator from LIMIT/OFFSET clauses.
Parameters
| Parameter | Type |
|---|---|
node | SelectStatement |
op | PlanTupleOperator |
Returns
Throws
If either value is not a numeric constant.
buildPlan()
buildPlan(
node,ctx,langCtx):BuildPlanResult
Defined in: lang-sql/src/visitors/builder.ts:147
Builds a logical plan from an AST node.
Parameters
| Parameter | Type | Description |
|---|---|---|
node | ASTNode | The root node of the AST. |
ctx | IdSet | The context to use for the plan. |
langCtx | Record<string, unknown> | The per-language additional context. For example, SQL may use this to pass in definitions of CTEs created in an outer language switch. |
Returns
The plan operator, as well as the inferred context (@see toInfer).
Implementation of
checkRecursionValidity()
protectedcheckRecursionValidity(node):SelectSet
Defined in: lang-sql/src/visitors/builder.ts:918
Validates that a WITH RECURSIVE query only uses supported features.
Parameters
| Parameter | Type |
|---|---|
node | WithQuery |
Returns
The top-level AST.SelectSet for further processing.
Throws
If the query is not a UNION, or if unsupported cycle or search features are used.
cloneLangCtx()
protectedcloneLangCtx(ctx):SQLLangCtx
Defined in: lang-sql/src/visitors/builder.ts:140
Returns a shallow copy of ctx with the CTE maps cloned so sub-queries can extend them independently.
Parameters
| Parameter | Type |
|---|---|
ctx | SQLLangCtx |
Returns
getAllTableNames()
protectedgetAllTableNames(node):ASTIdentifier[]
Defined in: lang-sql/src/visitors/builder.ts:888
Get all table names forming a join (before aliasing)
Parameters
| Parameter | Type |
|---|---|
node | ASTTableAlias | ASTIdentifier | JoinClause |
Returns
getTableName()
protectedgetTableName(node): [PlanTupleOperator,ASTIdentifier]
Defined in: lang-sql/src/visitors/builder.ts:638
Resolves a FROM-clause item to a tuple operator and optional relation name.
Parameters
| Parameter | Type |
|---|---|
node | ASTTableAlias | ASTIdentifier | JoinClause |
Returns
[PlanTupleOperator, ASTIdentifier]
handleRecursion()
protectedhandleRecursion(node):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:943
Builds an plan.IndexedRecursion plan for a WITH RECURSIVE query after validating it via checkRecursionValidity.
Parameters
| Parameter | Type |
|---|---|
node | WithQuery |
Returns
idToTupleSource()
protectedidToTupleSource(name): [PlanTupleOperator,ASTIdentifier]
Defined in: lang-sql/src/visitors/builder.ts:604
Also handles CTEs and materialized CTEs
Parameters
| Parameter | Type | Description |
|---|---|---|
name | ASTIdentifier | name of the tuple source |
Returns
[PlanTupleOperator, ASTIdentifier]
initLangCtx()
protectedinitLangCtx():SQLLangCtx
Defined in: lang-sql/src/visitors/builder.ts:133
Creates a fresh, empty SQLLangCtx.
Returns
isRecursive()
protectedisRecursive(node):boolean
Defined in: lang-sql/src/visitors/builder.ts:860
Determines if a WITH query is recursive.
Parameters
| Parameter | Type |
|---|---|
node | WithQuery |
Returns
boolean
materializeCte()
protectedmaterializeCte(node,subq):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:1004
Wraps subq in a GroupBy that collects all rows into an array and registers the CTE name in localLangCtx.materializedCtes.
Parameters
| Parameter | Type |
|---|---|
node | WithQuery |
subq | PlanTupleOperator |
Returns
optimizeInOp()
protectedoptimizeInOp(expr,subq):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:1030
Rewrites an IN subquery by pushing the needle value into the subquery as a
selection, enabling the executor to apply an index scan when available.
Parameters
| Parameter | Type |
|---|---|
expr | ASTIdentifier | PlanOpAsArg |
subq | PlanOperator |
Returns
opToCalc()
protectedopToCalc(op):Calculation
Defined in: lang-sql/src/visitors/builder.ts:200
Converts an already-built plan operator into a plan.Calculation.
Parameters
| Parameter | Type |
|---|---|
op | PlanOperator |
Returns
processAttr()
protectedprocessAttr(attr):Aliased<ASTIdentifier|Calculation>
Defined in: lang-sql/src/visitors/builder.ts:562
Bound to this; converts a SELECT item to an [expression, alias] pair.
Parameters
| Parameter | Type |
|---|---|
attr | ASTNode |
Returns
Aliased<ASTIdentifier | Calculation>
processFnArg()
protectedprocessFnArg(item):ASTIdentifier|PlanOpAsArg
Defined in: lang-sql/src/visitors/builder.ts:188
Bound to this; wraps non-identifier nodes as { op } for use as function arguments.
Parameters
| Parameter | Type |
|---|---|
item | ASTNode |
Returns
processNode()
protectedprocessNode(item):OpOrId
Defined in: lang-sql/src/visitors/builder.ts:184
Bound to this for array map usage; passes identifiers through and visits all other nodes.
Parameters
| Parameter | Type |
|---|---|
item | ASTNode |
Returns
processOrderItem()
protectedprocessOrderItem(item):Order
Defined in: lang-sql/src/visitors/builder.ts:393
Bound to this; converts an ORDER BY item to a plan.Order descriptor.
Parameters
| Parameter | Type |
|---|---|
item | OrderByItem |
Returns
renameWithQuery()
protectedrenameWithQuery(node,subq):Projection
Defined in: lang-sql/src/visitors/builder.ts:817
Rename with query according to the with query name. If column names are provided, use them. Additionally, provide schemaless aliases for all columns.
Parameters
| Parameter | Type |
|---|---|
node | WithQuery |
subq | PlanTupleOperator |
Returns
toCalc()
protectedtoCalc(node):ASTIdentifier|Calculation
Defined in: lang-sql/src/visitors/builder.ts:192
Converts an AST node to a plan.Calculation or identifier; extracts the field name for aggregate calls.
Parameters
| Parameter | Type |
|---|---|
node | ASTNode |
Returns
visitAggregate()
visitAggregate(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:736
Parameters
| Parameter | Type |
|---|---|
node | ASTAggregate |
Returns
Implementation of
visitArray()
visitArray(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:210
Parameters
| Parameter | Type |
|---|---|
node | ASTArray |
Returns
Implementation of
visitCase()
visitCase(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:705
Parameters
| Parameter | Type |
|---|---|
node | ASTCase |
Returns
Implementation of
visitCast()
visitCast(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:252
Parameters
| Parameter | Type |
|---|---|
node | ASTCast |
Returns
Implementation of
visitExists()
visitExists(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:283
Parameters
| Parameter | Type |
|---|---|
node | ASTExists |
Returns
Implementation of
visitExpressionAlias()
visitExpressionAlias(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:388
Parameters
| Parameter | Type |
|---|---|
node | ASTExpressionAlias |
Returns
Implementation of
SQLVisitor.visitExpressionAlias
visitFunction()
visitFunction(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:1119
Parameters
| Parameter | Type |
|---|---|
node | ASTFunction |
Returns
Implementation of
LogicalPlanBuilder.visitFunction
visitGroupByClause()
visitGroupByClause(
node,src?,aggregates?):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:583
should be called only by SQLLogicalPlanBuilder#visitSelectSet,
which provides the src and aggregates args
Parameters
| Parameter | Type | Default value |
|---|---|---|
node | GroupByClause | undefined |
src | PlanTupleOperator | null |
aggregates | AggregateCall[] | [] |
Returns
Implementation of
visitIdentifier()
visitIdentifier(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:336
Parameters
| Parameter | Type |
|---|---|
node | ASTIdentifier |
Returns
Implementation of
LogicalPlanBuilder.visitIdentifier
visitJoinClause()
visitJoinClause(
node):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:649
Parameters
| Parameter | Type |
|---|---|
node | JoinClause |
Returns
Implementation of
visitLangSwitch()
visitLangSwitch(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:1143
Propagates the current CTE maps back to the shared langCtx before creating a
PlanLangSwitch, making locally defined CTEs visible to the embedded language.
Parameters
| Parameter | Type |
|---|---|
node | LangSwitch |
Returns
Implementation of
LogicalPlanBuilder.visitLangSwitch
visitLiteral()
visitLiteral<
U>(node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:1022
Type Parameters
| Type Parameter |
|---|
U |
Parameters
| Parameter | Type |
|---|---|
node | ASTLiteral<U> |
Returns
Implementation of
LogicalPlanBuilder.visitLiteral
visitNumberLiteral()
visitNumberLiteral(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:207
Parameters
| Parameter | Type |
|---|---|
node | ASTNumberLiteral |
Returns
Implementation of
visitOperator()
visitOperator(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:1072
Parameters
| Parameter | Type |
|---|---|
node | ASTOperator |
Returns
Implementation of
LogicalPlanBuilder.visitOperator
visitQuantifier()
visitQuantifier(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:294
Parameters
| Parameter | Type |
|---|---|
node | ASTQuantifier |
Returns
Implementation of
visitRow()
visitRow(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:232
Parameters
| Parameter | Type |
|---|---|
node | ASTRow |
Returns
Implementation of
visitRowsFrom()
visitRowsFrom(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:793
Parameters
| Parameter | Type |
|---|---|
node | RowsFrom |
Returns
Implementation of
visitSelectSet()
visitSelectSet(
node,_?,orderByAggs?):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:486
Builds the plan for a single SELECT body, merging any outer ORDER BY aggregate calls supplied by the caller.
Parameters
| Parameter | Type | Description |
|---|---|---|
node | SelectSet | - |
_? | unknown | only for compliance with PlanVisitor |
orderByAggs? | AggregateCall[] | aggregate calls inferred from an enclosing ORDER BY clause. |
Returns
Throws
UnsupportedError when the SELECT uses window functions.
Implementation of
visitSelectSetOp()
visitSelectSetOp(
node,left?):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:456
should be called only by SQLLogicalPlanBuilder#visitSelectSet,
which provides the left arg
Parameters
| Parameter | Type | Default value |
|---|---|---|
node | SelectSetOp | undefined |
left | PlanTupleOperator | null |
Returns
Implementation of
visitSelectStatement()
visitSelectStatement(
node):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:400
Parameters
| Parameter | Type |
|---|---|
node | SelectStatement |
Returns
Implementation of
SQLVisitor.visitSelectStatement
visitSQLIdentifier()
visitSQLIdentifier(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:339
Parameters
| Parameter | Type |
|---|---|
node | SQLIdentifier |
Returns
Implementation of
visitStringLiteral()
visitStringLiteral(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:204
Parameters
| Parameter | Type |
|---|---|
node | ASTStringLiteral |
Returns
Implementation of
visitSubscript()
visitSubscript(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:266
Parameters
| Parameter | Type |
|---|---|
node | ASTSubscript |
Returns
Implementation of
visitTableAlias()
visitTableAlias(
node):PlanTupleOperator
Defined in: lang-sql/src/visitors/builder.ts:342
Parameters
| Parameter | Type |
|---|---|
node | ASTTableAlias |
Returns
Implementation of
visitTableFn()
visitTableFn(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:776
Parameters
| Parameter | Type |
|---|---|
node | TableFn |
Returns
Implementation of
visitTuple()
visitTuple(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:224
Parameters
| Parameter | Type |
|---|---|
node | ASTTuple |
Returns
Implementation of
visitValues()
visitValues(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:713
Parameters
| Parameter | Type |
|---|---|
node | ValuesClause |
Returns
Implementation of
visitWindowFn()
visitWindowFn(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:773
Parameters
| Parameter | Type |
|---|---|
node | ASTWindowFn |
Returns
Implementation of
visitWindowSpec()
visitWindowSpec(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:770
Parameters
| Parameter | Type |
|---|---|
node | WindowSpec |
Returns
Implementation of
visitWithQuery()
visitWithQuery(
node):PlanOperator
Defined in: lang-sql/src/visitors/builder.ts:796
Parameters
| Parameter | Type |
|---|---|
node | WithQuery |