From b73bf8802a32f0b731b21c752d7f96a57ee4770a Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 25 Mar 2024 18:34:40 +0800 Subject: [PATCH] [KYUUBI #6205] Backport SPARK-47300: quoteIfNeeded should quote identifier starts with digits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # :mag: Description The function is forked from Apache Spark, and we should align behavior with upstream timely. ## Types of changes :bookmark: - [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 2e44fe757 [Cheng Pan] Backport SPARK-47300: quoteIfNeeded should quote identifier starts with digits Authored-by: Cheng Pan Signed-off-by: Cheng Pan --- .../engine/spark/util/SparkCatalogUtils.scala | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/util/SparkCatalogUtils.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/util/SparkCatalogUtils.scala index b55319830b5..ff4564e5462 100644 --- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/util/SparkCatalogUtils.scala +++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/util/SparkCatalogUtils.scala @@ -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("`", "``") + "`" + } }