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): Digit identifier must be quoted #3482

Closed
wants to merge 1 commit into from

Conversation

takezoe
Copy link
Member

@takezoe takezoe commented Apr 11, 2024

No description provided.

@takezoe takezoe changed the title airframe-sql: Digit identifier must be quoted sql (fix): Digit identifier must be quoted Apr 11, 2024
override def sqlExpr: String = value
override def toString: String = s"Id(${value})"
override def sqlExpr: String = s""""${value}""""
override def toString: String = s"""Id("${value}")"""
Copy link
Member

Choose a reason for hiding this comment

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

No need to quote this string representation

@xerial
Copy link
Member

xerial commented Apr 19, 2024

@takezoe I think DigitIdentifiers are intended for representing GROUP BY 1, 2, 3 .... In which case do we need to quote them?

@takezoe
Copy link
Member Author

takezoe commented Apr 20, 2024

I think DigitIdentifiers are intended for representing GROUP BY 1, 2, 3 ...

Ah, I see.

In which case do we need to quote them?

Assuming the following query, "123" is parsed as a digit identifier

SELECT * FROM database."123"

and regenerated SQL from the AST will be invalid:

SELECT * FROM database.123

Now I found the root cause is that a digit identifier is generated in Expression.newIdentifier() for the table name when generate SQL. I will send another PR.

def newIdentifier(x: String): Identifier = {
if (x.startsWith("`") && x.endsWith("`")) {
BackQuotedIdentifier(x.stripPrefix("`").stripSuffix("`"), None)
} else if (x.startsWith("\"") && x.endsWith("\"")) {
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)
} else {
UnquotedIdentifier(x, None)
}
}

@takezoe takezoe closed this Apr 20, 2024
@takezoe takezoe deleted the sql-quote-digit-id branch April 20, 2024 02:35
@takezoe
Copy link
Member Author

takezoe commented Apr 20, 2024

Created #3493

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants