Github user gatorsmile commented on a diff in the pull request:
https://github.com/apache/spark/pull/13991#discussion_r69173355
--- Diff:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/xml/XPathExpressionSuite.scala
---
@@ -27,33 +27,127 @@ import org.apache.spark.sql.types.StringType
*/
class XPathExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
{
- private def testBoolean[T](xml: String, path: String, expected: T): Unit
= {
+ test("xpath_boolean") {
+ def testExpr[T](xml: String, path: String, expected:
java.lang.Boolean): Unit = {
+ checkEvaluation(
+ XPathBoolean(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a><b>b</b></a>", "a/b", true)
+ testExpr("<a><b>b</b></a>", "a/c", false)
+ testExpr("<a><b>b</b></a>", "a/b = \"b\"", true)
+ testExpr("<a><b>b</b></a>", "a/b = \"c\"", false)
+ testExpr("<a><b>10</b></a>", "a/b < 10", false)
+ testExpr("<a><b>10</b></a>", "a/b = 10", true)
+ }
+
+ test("xpath_short") {
+ def testExpr[T](xml: String, path: String, expected: java.lang.Short):
Unit = {
+ checkEvaluation(
+ XPathShort(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a>this is not a number</a>", "a", 0.toShort)
+ testExpr("<a>try a boolean</a>", "a = 10", 0.toShort)
+ testExpr("<a><b class=\"odd\">1</b><b class=\"even\">2</b><b
class=\"odd\">4</b><c>8</c></a>",
+ "sum(a/b[@class=\"odd\"])",
+ 5.toShort)
+ }
+
+ test("xpath_int") {
+ def testExpr[T](xml: String, path: String, expected:
java.lang.Integer): Unit = {
+ checkEvaluation(
+ XPathInt(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a>this is not a number</a>", "a", 0)
+ testExpr("<a>try a boolean</a>", "a = 10", 0)
+ testExpr("<a><b class=\"odd\">1</b><b class=\"even\">2</b><b
class=\"odd\">4</b><c>8</c></a>",
+ "sum(a/b[@class=\"odd\"])",
+ 5)
+ }
+
+ test("xpath_long") {
+ def testExpr[T](xml: String, path: String, expected: java.lang.Long):
Unit = {
+ checkEvaluation(
+ XPathLong(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a>this is not a number</a>", "a", 0L)
+ testExpr("<a>try a boolean</a>", "a = 10", 0L)
+ testExpr("<a><b class=\"odd\">1</b><b class=\"even\">2</b><b
class=\"odd\">4</b><c>8</c></a>",
+ "sum(a/b[@class=\"odd\"])",
+ 5L)
+ }
+
+ test("xpath_float") {
+ def testExpr[T](xml: String, path: String, expected: java.lang.Float):
Unit = {
+ checkEvaluation(
+ XPathFloat(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a>this is not a number</a>", "a", Float.NaN)
+ testExpr("<a>try a boolean</a>", "a = 10", 0.0F)
+ testExpr("<a><b class=\"odd\">1</b><b class=\"even\">2</b><b
class=\"odd\">4</b><c>8</c></a>",
+ "sum(a/b[@class=\"odd\"])",
+ 5.0F)
+ }
+
+ test("xpath_double") {
+ def testExpr[T](xml: String, path: String, expected:
java.lang.Double): Unit = {
+ checkEvaluation(
+ XPathDouble(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a>this is not a number</a>", "a", Double.NaN)
+ testExpr("<a>try a boolean</a>", "a = 10", 0.0)
+ testExpr("<a><b class=\"odd\">1</b><b class=\"even\">2</b><b
class=\"odd\">4</b><c>8</c></a>",
+ "sum(a/b[@class=\"odd\"])",
+ 5.0)
+ }
+
+ test("xpath_string") {
+ def testExpr[T](xml: String, path: String, expected: String): Unit = {
+ checkEvaluation(
+ XPathString(Literal.create(xml, StringType), Literal.create(path,
StringType)),
+ expected)
+ }
+
+ testExpr("<a><b>bb</b><c>cc</c></a>", "a", "bbcc")
+ testExpr("<a><b>bb</b><c>cc</c></a>", "a/b", "bb")
+ testExpr("<a><b>bb</b><c>cc</c></a>", "a/c", "cc")
+ testExpr("<a><b>bb</b><c>cc</c></a>", "a/d", "")
+ testExpr("<a><b>b1</b><b>b2</b></a>", "//b", "b1")
+ testExpr("<a><b>b1</b><b>b2</b></a>", "a/b[1]", "b1")
+ testExpr("<a><b>b1</b><b id='b_2'>b2</b></a>", "a/b[@id='b_2']", "b2")
+ }
+
+ test("null handling") {
+ // We only do this for one expression since they all share the same
common base implementation
+ checkEvaluation(
+ XPathLong(Literal.create(null, StringType), Literal.create(null,
StringType)), null)
checkEvaluation(
- XPathBoolean(Literal.create(xml, StringType), Literal.create(path,
StringType)),
- expected)
+ XPathLong(Literal.create("", StringType), Literal.create(null,
StringType)), null)
+ checkEvaluation(
+ XPathLong(Literal.create(null, StringType), Literal.create("",
StringType)), null)
}
- test("xpath_boolean") {
- testBoolean("<a><b>b</b></a>", "a/b", true)
- testBoolean("<a><b>b</b></a>", "a/c", false)
- testBoolean("<a><b>b</b></a>", "a/b = \"b\"", true)
- testBoolean("<a><b>b</b></a>", "a/b = \"c\"", false)
- testBoolean("<a><b>10</b></a>", "a/b < 10", false)
- testBoolean("<a><b>10</b></a>", "a/b = 10", true)
-
- // null input
- testBoolean(null, null, null)
- testBoolean(null, "a", null)
- testBoolean("<a><b>10</b></a>", null, null)
-
- // exception handling for invalid input
+ test("invalid xml handling") {
intercept[Exception] {
--- End diff --
Which Exception will we throw? What is the message we issue? When users see
the message, can they understand the cause?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]