Skip to content
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
3 changes: 3 additions & 0 deletions core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"IF_PARTITION_NOT_EXISTS_UNSUPPORTED" : {
"message" : [ "Cannot write, IF NOT EXISTS is not supported for table: %s" ]
},
"ILLEGAL_SUBSTRING" : {
"message" : [ "%s cannot contain %s." ]
},
"INCOMPARABLE_PIVOT_COLUMN" : {
"message" : [ "Invalid pivot column '%s'. Pivot columns must be comparable." ],
"sqlState" : "42000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ case class ParseUrl(children: Seq[Expression], failOnError: Boolean = SQLConf.ge
case class FormatString(children: Expression*) extends Expression with ImplicitCastInputTypes {

require(children.nonEmpty, s"$prettyName() should take at least 1 argument")
Copy link
Contributor Author

@LuciferYang LuciferYang Nov 1, 2021

Choose a reason for hiding this comment

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

@cloud-fan this pr only change the newly added code, If also need to change line 1687, please let me know.

require(checkArgumentIndexNotZero(children(0)), "Illegal format argument index = 0")
checkArgumentIndexNotZero(children(0))


override def foldable: Boolean = children.forall(_.foldable)
Expand Down Expand Up @@ -1782,9 +1782,11 @@ case class FormatString(children: Expression*) extends Expression with ImplicitC
* Therefore, manually check that the pattern string not contains "%0$" to ensure consistent
* behavior of Java 8, Java 11 and Java 17.
*/
private def checkArgumentIndexNotZero(expression: Expression): Boolean = expression match {
case StringLiteral(pattern) => !pattern.contains("%0$")
case _ => true
private def checkArgumentIndexNotZero(expression: Expression): Unit = expression match {
case StringLiteral(pattern) if pattern.contains("%0$") =>
throw QueryCompilationErrors.illegalSubstringError(
"The argument_index of string format", "position 0$")
case _ => // do nothing
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ object QueryCompilationErrors {
messageParameters = Array(sizeLimit.toString))
}

def illegalSubstringError(subject: String, illegalContent: String): Throwable = {
new AnalysisException(
errorClass = "ILLEGAL_SUBSTRING",
messageParameters = Array(subject, illegalContent))
}

def unorderablePivotColError(pivotCol: Expression): Throwable = {
new AnalysisException(
errorClass = "INCOMPARABLE_PIVOT_COLUMN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ select format_string('%0$s', 'Hello')
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
requirement failed: Illegal format argument index = 0; line 1 pos 7
The argument_index of string format cannot contain position 0$.; line 1 pos 7


-- !query
Expand Down