From 00a14e72032ea605a9f3514a581dc4ffb4019169 Mon Sep 17 00:00:00 2001 From: "R. C. Howell" Date: Wed, 17 Apr 2024 06:35:01 -0700 Subject: [PATCH] Fix normalization --- .../ast/normalize/NormalizeFromSource.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt index fe2e2dc776..8c12807ef6 100644 --- a/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt +++ b/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/NormalizeFromSource.kt @@ -52,11 +52,19 @@ internal object NormalizeFromSource : AstPass { override fun visitFromValue(node: From.Value, ctx: Int): From { val expr = visitExpr(node.expr, ctx) as Expr - val asAlias = node.asAlias ?: expr.toBinder(ctx) - val atAlias = node.atAlias ?: expr.toBinder(ctx + 1) - val byAlias = node.byAlias ?: expr.toBinder(ctx + 2) - return if (expr !== node.expr || asAlias !== node.asAlias || atAlias !== node.atAlias || byAlias !== node.byAlias) { - node.copy(expr = expr, asAlias = asAlias, atAlias = atAlias, byAlias = byAlias) + var i = ctx + var asAlias = node.asAlias + var atAlias = node.atAlias + // derive AS alias + if (asAlias == null) { + asAlias = expr.toBinder(i++) + } + // derive AT binder + if (atAlias == null && node.type == From.Value.Type.UNPIVOT) { + atAlias = expr.toBinder(i++) + } + return if (expr !== node.expr || asAlias !== node.asAlias || atAlias !== node.atAlias) { + node.copy(expr = expr, asAlias = asAlias, atAlias = atAlias) } else { node }