Skip to content

Commit

Permalink
SNOW-1320314: fix Null handling in Variants (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-yuwang authored Jun 4, 2024
1 parent 9dddb98 commit 1fa8c6f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repos:
- repo: [email protected]:snowflakedb/casec_precommit.git
rev: HEAD
rev: v1.35.4
hooks:
- id: secret-scanner
10 changes: 9 additions & 1 deletion src/main/scala/com/snowflake/snowpark/types/Variant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,15 @@ class Variant private[snowpark] (
def this(str: String) =
this({
try {
MAPPER.readTree(str)
// `ObjectMapper` only reads the first token from
// the input string but not the whole string.
// For example, It can successfully
// convert "null dummy" to `null` value without reporting error.
if (str.toLowerCase().startsWith("null") && str != "null") {
JsonNodeFactory.instance.textNode(str)
} else {
MAPPER.readTree(str)
}
} catch {
case _: Exception => JsonNodeFactory.instance.textNode(str)
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/scala/com/snowflake/snowpark_test/DataTypeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.snowflake.snowpark_test
import com.snowflake.snowpark.{Row, SNTestBase, TestUtils}
import com.snowflake.snowpark.types._
import com.snowflake.snowpark.functions._
import com.snowflake.snowpark.internal.Utils

import java.sql.{Date, Time, Timestamp}
import java.util.TimeZone
Expand Down Expand Up @@ -588,4 +589,28 @@ class DataTypeSuite extends SNTestBase {
|""".stripMargin)
// scalastyle:on
}

test("Variant containing word null in the text") {
import session.implicits._
var variant = new Variant("null string starts with null")
var df = Seq(variant).toDF("a")
checkAnswer(
df,
Row("\"null string starts with null\"")
)

variant = new Variant("string with null in the middle")
df = Seq(variant).toDF("a")
checkAnswer(
df,
Row("\"string with null in the middle\"")
)

variant = new Variant("string with null in the end null")
df = Seq(variant).toDF("a")
checkAnswer(
df,
Row("\"string with null in the end null\"")
)
}
}

0 comments on commit 1fa8c6f

Please sign in to comment.