From 5f71687e5ab9d56198fec24286c47b981d18d3ca Mon Sep 17 00:00:00 2001 From: Jason Fulghum Date: Fri, 18 Oct 2024 15:56:41 -0700 Subject: [PATCH] Changing selectExprNeedsAlias to consider string literal quotes --- sql/planbuilder/project.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/sql/planbuilder/project.go b/sql/planbuilder/project.go index 35a42c14a3..512a40906b 100644 --- a/sql/planbuilder/project.go +++ b/sql/planbuilder/project.go @@ -243,5 +243,14 @@ func selectExprNeedsAlias(e *ast.AliasedExpr, expr sql.Expression) bool { } }) - return complex || e.InputExpression != expr.String() + // If the expression's string representation is quoted, trim the quotes before comparing it to the input expression. + // InputExpression is assigned in the Vitess layer, and it always trims quotes at that time, too. + exprString := expr.String() + if strings.HasPrefix(exprString, "'") && strings.HasSuffix(exprString, "'") { + exprString = exprString[1 : len(exprString)-1] + } + + // If the expression's input value matches expr.String(), then we know that it is referenceable and + // does not need an alias. + return complex || e.InputExpression != exprString }