Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql (fix): Quote digit QName parts to generate valid SQL #3493

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,6 @@ object Expression {
QuotedIdentifier(x.stripPrefix("\"").stripSuffix("\""), None)
} else if (x.matches("[0-9]+")) {
DigitId(x, None)
} else if (!x.matches("[0-9a-zA-Z_]*")) {
// Quotations are needed with special characters to generate valid SQL
QuotedIdentifier(x, None)
Copy link
Member Author

@takezoe takezoe Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to QName.sqlExpr so that this method can focus on identifier generation without handling special cases.

} else {
UnquotedIdentifier(x, None)
}
Expand All @@ -479,7 +476,18 @@ object Expression {
case class QName(parts: List[String], nodeLocation: Option[NodeLocation]) extends LeafExpression {
def fullName: String = parts.mkString(".")
override def toString: String = fullName
override def sqlExpr: String = parts.map(Expression.newIdentifier).map(_.sqlExpr).mkString(".")
override def sqlExpr: String = parts
.map { part =>
if (part.matches("[0-9]+")) {
// Quotations are needed for digits to generate valid SQL
Expression.newIdentifier(s""""$part"""")
} else if (!part.matches("[0-9a-zA-Z_]*")) {
// Quotations are needed with special characters to generate valid SQL
Expression.newIdentifier(s""""$part"""")
} else {
Expression.newIdentifier(part)
}
}.map(_.sqlExpr).mkString(".")
}
object QName {
def apply(s: String, nodeLocation: Option[NodeLocation]): QName = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,5 @@
select user_agent || 'x', count(*) from impression group by 1
- sql: |
select * FROM "café"
- sql: |
select * FROM "café"."123"
Loading