Skip to content

Commit 6c45ada

Browse files
author
James Cor
committed
helper function
1 parent 053f8e1 commit 6c45ada

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

sql/planbuilder/scalar.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,8 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
131131
}
132132
b.handleErr(err)
133133
}
134-
// Look past table aliases
135-
var origTbl string
136-
if inScope.node != nil {
137-
transform.Inspect(inScope.node, func(node sql.Node) bool {
138-
switch n := node.(type) {
139-
case *plan.TableAlias:
140-
if n.Name() == c.table {
141-
if child, ok := n.Child.(sql.Nameable); ok {
142-
origTbl = child.Name()
143-
}
144-
}
145-
return false
146-
}
147-
return true
148-
})
149-
}
150134

135+
origTbl := b.getOrigTblName(inScope.node, c.table)
151136
c = c.withOriginal(origTbl, v.Name.String())
152137
return c.scalarGf()
153138
case *ast.FuncExpr:
@@ -418,6 +403,28 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
418403
return nil
419404
}
420405

406+
func (b *Builder) getOrigTblName(node sql.Node, alias string) string {
407+
if node == nil {
408+
return ""
409+
}
410+
// Look past table aliases
411+
var origTbl string
412+
transform.Inspect(node, func(n sql.Node) bool {
413+
switch n := node.(type) {
414+
case *plan.TableAlias:
415+
if n.Name() == alias {
416+
if child, ok := n.Child.(sql.Nameable); ok {
417+
origTbl = child.Name()
418+
}
419+
}
420+
return false
421+
default:
422+
return true
423+
}
424+
})
425+
return origTbl
426+
}
427+
421428
// getJsonValueTypeLiteral converts a type coercion string into a literal
422429
// expression with the zero type of the coercion (see json_value function).
423430
func (b *Builder) getJsonValueTypeLiteral(e sql.Expression) sql.Expression {

0 commit comments

Comments
 (0)