bojana-db commented on code in PR #57120:
URL: https://github.com/apache/spark/pull/57120#discussion_r3550611121
##########
sql/core/src/test/scala/org/apache/spark/sql/VariantSuite.scala:
##########
@@ -384,6 +384,61 @@ class VariantSuite extends SharedSparkSession with
ExpressionEvalHelper {
}
}
+ test("try_variant_insert with literal arguments") {
+ def rows(results: Any*): Seq[Row] = results.map(Row(_))
+
+ checkAnswer(
+ sql("SELECT to_json(try_variant_insert(parse_json('{\"a\": 1}'), '$.b',
2))"),
+ rows("""{"a":1,"b":2}"""))
+
+ checkAnswer(
+ sql("SELECT to_json(try_variant_insert(parse_json('{\"a\": 1}'), '$.a',
2))"),
+ rows(null))
+ checkAnswer(
+ sql("SELECT to_json(try_variant_insert(parse_json('{\"a\": 1}'),
'$.a.b', 2))"),
+ rows(null))
+
+ checkAnswer(
+ sql("SELECT to_json(try_variant_insert(parse_json('{\"a\": 1}'), '$[0]',
2))"),
+ rows(null))
+ checkAnswer(
+ sql("SELECT to_json(try_variant_insert(parse_json('{\"a\": 5}'),
'$.a[0]', 2))"),
+ rows(null))
+
+ checkAnswer(
+ sql("SELECT to_json(try_variant_insert(parse_json('{\"a\": 1}'), '$.b',
NULL))"),
+ rows(null))
+
+ checkError(
+ exception = intercept[SparkRuntimeException] {
+ sql("SELECT try_variant_insert(parse_json('{}'), '$', 1)").collect()
+ },
+ condition = "INVALID_VARIANT_PATH",
+ parameters = Map("path" -> "$", "functionName" ->
toSQLId("try_variant_insert")))
+ }
+
+ test("try_variant_insert with dynamic arguments") {
+ def rows(results: Any*): Seq[Row] = results.map(Row(_))
+ Seq("CODEGEN_ONLY", "NO_CODEGEN").foreach { codegenMode =>
+ withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> codegenMode) {
+ val df = Seq(
+ ("""{"a": 1}""", "$.b"),
+ ("""{"a": 1}""", "$.a"),
+ ("""{"a": 1}""", "$.a.b"),
+ (null, "$.b")
+ ).toDF("json", "path")
+ val v = parse_json(col("json"))
+ val out = df.select(to_json(try_variant_insert(v, col("path"),
lit(2))).alias("r"))
+ checkAnswer(out, rows("""{"a":1,"b":2}""", null, null, null))
Review Comment:
Yes, we will have `variant_set` with `create_if_missing` flag ([PR
link](https://github.com/apache/spark/pull/57125)).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]