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-37013][SQL] Forbid %0$ usage explicitly to ensure format_string has same behavior when using Java 8 and Java 17 #34313

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,8 @@ 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")
require(checkArgumentIndexNotZero(children(0)), "Illegal format argument index = 0")
Copy link
Contributor

Choose a reason for hiding this comment

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

let's use the new error framework to throw error in newly added code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cloud-fan Sorry, is there any sample? I'll fix it later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

yea

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK ~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm very sorry I haven't started this week



override def foldable: Boolean = children.forall(_.foldable)
override def nullable: Boolean = children(0).nullable
Expand Down Expand Up @@ -1688,6 +1690,21 @@ case class FormatString(children: Expression*) extends Expression with ImplicitC

override protected def withNewChildrenInternal(
newChildren: IndexedSeq[Expression]): FormatString = FormatString(newChildren: _*)

/**
* SPARK-37013: The `formatSpecifier` defined in `j.u.Formatter` as follows:
* "%[argument_index$][flags][width][.precision][t]conversion"
* The optional `argument_index` is a decimal integer indicating the position of the argument
* in the argument list. The first argument is referenced by "1$", the second by "2$", etc.
* However, for the illegal definition of "%0$", Java 8 and Java 11 uses it as "%1$",
* and Java 17 throws IllegalFormatArgumentIndexException(Illegal format argument index = 0).
* 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 pattern: Literal if pattern.dataType == StringType => !pattern.toString.contains("%0$")
Copy link
Member

Choose a reason for hiding this comment

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

case StringLiteral(pattern) => !pattern.contains("%0$")?

case _ => true
}
}

/**
Expand Down