Skip to content

Commit

Permalink
Move quote function to ExpressionUtils, and use StaticInvoke instead …
Browse files Browse the repository at this point in the history
…of Invoke
  • Loading branch information
sarutak committed Jan 23, 2025
1 parent 6d9ab0b commit 230a967
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2160,14 +2160,6 @@ public UTF8String soundex() {
return UTF8String.fromBytes(sx);
}

public UTF8String quote() {
final String qtChar = "'";
final String qtCharRep = "\\\\'";

String sp = toString().replaceAll(qtChar, qtCharRep);
return fromString(qtChar + sp + qtChar);
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
byte[] bytes = getBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,12 @@ public static UTF8String randStr(XORShiftRandom rng, int length) {
}
return UTF8String.fromBytes(bytes);
}

public static UTF8String quote(UTF8String str) {
final String qtChar = "'";
final String qtCharRep = "\\\\'";

String sp = str.toString().replaceAll(qtChar, qtCharRep);
return UTF8String.fromString(qtChar + sp + qtChar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3724,23 +3724,31 @@ case class Luhncheck(input: Expression) extends RuntimeReplaceable with Implicit
* A function that prepends a backslash to each instance of single quote
* in the given string and encloses the result by single quotes.
*/
// scalastyle:off line.size.limit
@ExpressionDescription(
usage = """
_FUNC_(str) - Returns `str` enclosed by single quotes and
each instance of single quote in it is preceded by a backslash.
""",
usage = "_FUNC_(str) - Returns `str` enclosed by single quotes and each instance of single quote in it is preceded by a backslash.",
examples = """
Examples:
> SELECT _FUNC_('Don\'t');
'Don\'t'
""",
since = "4.0.0",
group = "string_funcs")
case class Quote(input: Expression) extends RuntimeReplaceable with ImplicitCastInputTypes
with UnaryLike[Expression] {
// scalastyle:on line.size.limit
case class Quote(input: Expression)
extends UnaryExpression
with RuntimeReplaceable
with ImplicitCastInputTypes
with DefaultStringProducingExpression {

override def nullIntolerant: Boolean = true

override lazy val replacement: Expression = Invoke(input, "quote", input.dataType)
override lazy val replacement: Expression = StaticInvoke(
classOf[ExpressionImplUtils],
dataType,
"quote",
Seq(input),
inputTypes)

override def inputTypes: Seq[AbstractDataType] = {
Seq(StringTypeWithCollation(supportsTrimCollation = true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1452,21 +1452,4 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {
Seq(Row("abc", "def")))
}
}

test("SPARK-50582: string quote function") {
val df = Seq(("Don't")).toDF("value")

checkAnswer(
df.select(quote($"value")),
Row("'Don\\'t'"))

checkAnswer(
df.selectExpr("quote('Spark')"),
Row("'Spark'")
)

checkAnswer(
df.selectExpr("quote(NULL)"),
Row(null))
}
}

0 comments on commit 230a967

Please sign in to comment.