ganeshashree commented on code in PR #57888: URL: https://github.com/apache/spark/pull/57888#discussion_r3751205947
########## sql/core/src/test/scala/org/apache/spark/sql/JsonValueSuite.scala: ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql + +import org.apache.spark.SparkRuntimeException +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StringType} + +/** + * End-to-end tests for the SQL:2016 `JSON_VALUE` scalar function. + */ +class JsonValueSuite extends QueryTest with SharedSparkSession { + import testImplicits._ + + private val doc = + """{"id":7,"name":"Ada","tags":["x","y"],"addr":{"city":"NYC"},"score":null,"f":"3.14"}""" + + test("extract a scalar value as STRING by default") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.name')"), Row("Ada")) + // Numbers and booleans come back as their JSON text under the default STRING RETURNING. + checkAnswer(sql(s"SELECT json_value('$doc', '$$.id')"), Row("7")) + } + + test("RETURNING casts the scalar to the requested type") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.id' RETURNING INT)"), Row(7)) + assert(sql(s"SELECT json_value('$doc', '$$.id' RETURNING INT)").schema.head.dataType + === IntegerType) + checkAnswer(sql(s"SELECT json_value('$doc', '$$.f' RETURNING DOUBLE)"), Row(3.14d)) + checkAnswer(sql("SELECT json_value('{\"v\":\"true\"}', '$.v' RETURNING BOOLEAN)"), Row(true)) + checkAnswer( + sql("SELECT json_value('{\"v\":\"2020-01-02\"}', '$.v' RETURNING DATE)"), + Row(java.sql.Date.valueOf("2020-01-02"))) + } + + test("default RETURNING type is STRING") { + assert(sql(s"SELECT json_value('$doc', '$$.name')").schema.head.dataType === StringType) + } + + test("a present JSON null yields SQL NULL") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.score')"), Row(null)) + } + + test("a non-scalar (object/array) match is an ON ERROR case, NULL by default") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.addr')"), Row(null)) + checkAnswer(sql(s"SELECT json_value('$doc', '$$.tags')"), Row(null)) + } + + test("a missing path is an ON EMPTY case, NULL by default") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.missing')"), Row(null)) + checkAnswer(sql(s"SELECT json_value('$doc', '$$.addr.zip')"), Row(null)) + } + + test("NULL JSON input propagates to NULL (not ON EMPTY / ON ERROR)") { + checkAnswer(sql("SELECT json_value(CAST(NULL AS STRING), '$.a' ERROR ON EMPTY ERROR ON ERROR)"), + Row(null)) + } + + test("DEFAULT ON EMPTY") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.missing' DEFAULT '?' ON EMPTY)"), Row("?")) + checkAnswer( + sql(s"SELECT json_value('$doc', '$$.missing' RETURNING INT DEFAULT 42 ON EMPTY)"), Row(42)) + } + + test("ERROR ON EMPTY raises for a missing path") { + val e = intercept[SparkRuntimeException] { + sql(s"SELECT json_value('$doc', '$$.missing' ERROR ON EMPTY)").collect() + } + assert(e.getCondition == "JSON_VALUE_ON_ERROR.EMPTY") + } + + test("DEFAULT ON ERROR for a non-scalar match and for malformed input") { + checkAnswer(sql(s"SELECT json_value('$doc', '$$.addr' DEFAULT 'n/a' ON ERROR)"), Row("n/a")) + checkAnswer(sql("SELECT json_value('not json', '$.a' DEFAULT 'bad' ON ERROR)"), Row("bad")) + } + + test("ERROR ON ERROR raises for malformed input") { + val e = intercept[SparkRuntimeException] { + sql("SELECT json_value('not json', '$.a' ERROR ON ERROR)").collect() + } + assert(e.getCondition == "JSON_VALUE_ON_ERROR.ERROR") + } + + test("ERROR ON ERROR raises for a non-scalar match") { + val e = intercept[SparkRuntimeException] { + sql(s"SELECT json_value('$doc', '$$.addr' ERROR ON ERROR)").collect() + } + assert(e.getCondition == "JSON_VALUE_ON_ERROR.ERROR") + } + + test("a failed cast is an ON ERROR case") { + // NULL ON ERROR default. + checkAnswer(sql(s"SELECT json_value('$doc', '$$.name' RETURNING INT)"), Row(null)) + // DEFAULT ON ERROR. + checkAnswer( + sql(s"SELECT json_value('$doc', '$$.name' RETURNING INT DEFAULT -1 ON ERROR)"), Row(-1)) + // ERROR ON ERROR. + val e = intercept[SparkRuntimeException] { + sql(s"SELECT json_value('$doc', '$$.name' RETURNING INT ERROR ON ERROR)").collect() + } + assert(e.getCondition == "JSON_VALUE_ON_ERROR.ERROR") + } + + test("independent ON EMPTY and ON ERROR behaviors") { + // Missing path -> ON EMPTY branch; a malformed / non-scalar -> ON ERROR branch. Review Comment: Applied. -- 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]
