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

[SPARK-47300][SQL] quoteIfNeeded should quote identifier starts with digits #45401

Closed
wants to merge 2 commits into from
Closed
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 @@ -16,6 +16,8 @@
*/
package org.apache.spark.sql.catalyst.util

import java.util.regex.Pattern

import org.apache.spark.sql.connector.catalog.Identifier

object QuotingUtils {
Expand All @@ -41,11 +43,13 @@ object QuotingUtils {
name.map(part => quoteIdentifier(part)).mkString(".")
}

private val validIdentPattern = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9_]*")

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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.util

import org.apache.spark.SparkFunSuite

class QuotingUtilsSuite extends SparkFunSuite {

test("do not quote for legal identifier strings") {
assert(quoteIfNeeded("a") == "a")
assert(quoteIfNeeded("a0") == "a0")
assert(quoteIfNeeded("a0a") == "a0a")
assert(quoteIfNeeded("_") == "_")
assert(quoteIfNeeded("_a") == "_a")
assert(quoteIfNeeded("_0") == "_0")
assert(quoteIfNeeded("_ab_") === "_ab_")
}

test("quote for illegal identifier strings") {
assert(quoteIfNeeded("&") == "`&`")
assert(quoteIfNeeded("a b") === "`a b`")
assert(quoteIfNeeded("a*b") === "`a*b`")
assert(quoteIfNeeded("0") == "`0`")
assert(quoteIfNeeded("01") == "`01`")
assert(quoteIfNeeded("0d") == "`0d`")
assert(quoteIfNeeded("") === "``")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,6 @@ class StringUtilsSuite extends SparkFunSuite with SQLHelper {
}
}

test("SPARK-34872: quoteIfNeeded should quote a string which contains non-word characters") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's covered by the new test

assert(quoteIfNeeded("a b") === "`a b`")
assert(quoteIfNeeded("a*b") === "`a*b`")
assert(quoteIfNeeded("123") === "`123`")
assert(quoteIfNeeded("1a") === "1a")
assert(quoteIfNeeded("_ab_") === "_ab_")
assert(quoteIfNeeded("_") === "_")
assert(quoteIfNeeded("") === "``")
}

test("SPARK-43841: mix of multipart and single-part identifiers") {
val baseString = "b"
// mix of multipart and single-part
Expand Down