Skip to content

Commit

Permalink
Merge pull request #2710 from dolthub/fulghum/doltgres-fix
Browse files Browse the repository at this point in the history
Changing `selectExprNeedsAlias` to consider string literal quotes
  • Loading branch information
fulghum authored Oct 22, 2024
2 parents baa759c + 64ebafe commit c5725b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
20 changes: 10 additions & 10 deletions enginetest/queries/query_plans.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions sql/plan/str_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plan

import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/transform"
)

Expand All @@ -20,6 +21,14 @@ func AliasSubqueryString(e sql.Expression) string {
if err != nil {
panic(err)
}

// String literal values are quoted when their String() method is called, so to avoid that, we
// check if we're dealing with a string literal and use it's raw value if so.
if literal, ok := e.(*expression.Literal); ok {
if s, ok := literal.Value().(string); ok {
return s
}
}
return e.String()
}

Expand Down
14 changes: 13 additions & 1 deletion sql/planbuilder/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ func selectExprNeedsAlias(e *ast.AliasedExpr, expr sql.Expression) bool {
return true
}
})
if complex {
return true
}

// 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]
}

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

0 comments on commit c5725b1

Please sign in to comment.