Skip to content

Commit c5725b1

Browse files
authored
Merge pull request #2710 from dolthub/fulghum/doltgres-fix
Changing `selectExprNeedsAlias` to consider string literal quotes
2 parents baa759c + 64ebafe commit c5725b1

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

enginetest/queries/query_plans.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/plan/str_expr.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plan
22

33
import (
44
"github.com/dolthub/go-mysql-server/sql"
5+
"github.com/dolthub/go-mysql-server/sql/expression"
56
"github.com/dolthub/go-mysql-server/sql/transform"
67
)
78

@@ -20,6 +21,14 @@ func AliasSubqueryString(e sql.Expression) string {
2021
if err != nil {
2122
panic(err)
2223
}
24+
25+
// String literal values are quoted when their String() method is called, so to avoid that, we
26+
// check if we're dealing with a string literal and use it's raw value if so.
27+
if literal, ok := e.(*expression.Literal); ok {
28+
if s, ok := literal.Value().(string); ok {
29+
return s
30+
}
31+
}
2332
return e.String()
2433
}
2534

sql/planbuilder/project.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ func selectExprNeedsAlias(e *ast.AliasedExpr, expr sql.Expression) bool {
242242
return true
243243
}
244244
})
245+
if complex {
246+
return true
247+
}
248+
249+
// If the expression's string representation is quoted, trim the quotes before comparing it to the input expression.
250+
// InputExpression is assigned in the Vitess layer, and it always trims quotes at that time, too.
251+
exprString := expr.String()
252+
if strings.HasPrefix(exprString, "'") && strings.HasSuffix(exprString, "'") {
253+
exprString = exprString[1 : len(exprString)-1]
254+
}
245255

246-
return complex || e.InputExpression != expr.String()
256+
// If the expression's input value does not match expr.String(), then we know that it is not
257+
// referenceable and will need an alias.
258+
return e.InputExpression != exprString
247259
}

0 commit comments

Comments
 (0)