Skip to content

Commit

Permalink
[KYUUBI #6205] Backport SPARK-47300: quoteIfNeeded should quote ident…
Browse files Browse the repository at this point in the history
…ifier starts with digits

# 🔍 Description

The function is forked from Apache Spark, and we should align behavior with upstream timely.

## Types of changes 🔖

- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Test Plan 🧪

Pass GA.

---

# Checklist 📝

- [x] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html)

**Be nice. Be informative.**

Closes #6205 from pan3793/quoteIfNeeded.

Closes #6205

2e44fe7 [Cheng Pan] Backport SPARK-47300: quoteIfNeeded should quote identifier starts with digits

Authored-by: Cheng Pan <[email protected]>
Signed-off-by: Cheng Pan <[email protected]>
  • Loading branch information
pan3793 committed Mar 25, 2024
1 parent 54687e0 commit b73bf88
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,22 @@ object SparkCatalogUtils extends Logging {
// format: on
}

/**
* Forked from Apache Spark's [[org.apache.spark.sql.catalyst.util.quoteIfNeeded]]
*/
// SPARK-47300 (4.0.0): quoteIfNeeded should quote identifier starts with digits
private val validIdentPattern = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]*")

// Forked from Apache Spark's [[org.apache.spark.sql.catalyst.util.QuotingUtils.quoteIfNeeded]]
def quoteIfNeeded(part: String): String = {
if (part.matches("[a-zA-Z0-9_]+") && !part.matches("\\d+")) {
if (validIdentPattern.matcher(part).matches()) {
part
} else {
s"`${part.replace("`", "``")}`"
quoteIdentifier(part)
}
}

// Forked from Apache Spark's [[org.apache.spark.sql.catalyst.util.QuotingUtils.quoteIdentifier]]
def quoteIdentifier(name: String): String = {
// Escapes back-ticks within the identifier name with double-back-ticks, and then quote the
// identifier with back-ticks.
"`" + name.replace("`", "``") + "`"
}
}

0 comments on commit b73bf88

Please sign in to comment.