From 6b110607ec7600b5753c803d833e2c9cf10f7d7b Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Thu, 22 Aug 2024 14:14:12 -0500 Subject: [PATCH 01/11] add JsonNode function for variant type --- .../com/snowflake/snowpark_java/types/Variant.java | 11 +++++++++++ .../snowflake/snowpark_test/JavaVariantSuite.java | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index 469ec8e6..c53a764d 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -376,6 +376,17 @@ public String asJsonString() { } } + /** + * Converts the variant as valid JsonNode. + * + * @return A valid json Node + * @since 0.8.0 + */ + public JsonNode asJsonNode() { + verify(VariantTypes.String); + return objectToJsonNode(value); + } + /** * Converts the variant as binary value. * diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index aa9fb117..8f31a004 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -1,5 +1,6 @@ package com.snowflake.snowpark_test; +import com.fasterxml.jackson.databind.JsonNode; import com.snowflake.snowpark_java.types.Geography; import com.snowflake.snowpark_java.types.InternalUtils; import com.snowflake.snowpark_java.types.Variant; @@ -365,4 +366,17 @@ public void equalsAndToString() { assert v1.toString().equals("123"); } + + @Test + public void javaJsonNodeVariantConverter() throws IllegalArgumentException { + // Scala variant to Java Variant + com.snowflake.snowpark.types.Variant sv = + new com.snowflake.snowpark.types.Variant( + "{\"name\": \"abc\",\"grade\": 2, \"Interest\": \"cricket\"}"); + Variant jResult = InternalUtils.createVariant(sv); + JsonNode jsonNode = jResult.asJsonNode(); + + assert jsonNode.get("name").asText().equals("abc"); + assert jsonNode.get("grade").asInt() == 2; + } } From ad882b0d444213e811cc98db5694ad0d5189a737 Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Thu, 22 Aug 2024 14:28:28 -0500 Subject: [PATCH 02/11] updadte version --- src/main/java/com/snowflake/snowpark_java/types/Variant.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index c53a764d..c82affe7 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -380,7 +380,7 @@ public String asJsonString() { * Converts the variant as valid JsonNode. * * @return A valid json Node - * @since 0.8.0 + * @since 1.14.0 */ public JsonNode asJsonNode() { verify(VariantTypes.String); From ff073b24c3f6276b8f7b7a380fdc6ab91107df1b Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Thu, 22 Aug 2024 14:31:53 -0500 Subject: [PATCH 03/11] update variant type as object --- src/main/java/com/snowflake/snowpark_java/types/Variant.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index c82affe7..9b8630e8 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -383,7 +383,7 @@ public String asJsonString() { * @since 1.14.0 */ public JsonNode asJsonNode() { - verify(VariantTypes.String); + verify(VariantTypes.Object); return objectToJsonNode(value); } From 6036ed33b1f6dd851b144ed9bc8f88ac758e5bdd Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Tue, 27 Aug 2024 15:49:58 -0500 Subject: [PATCH 04/11] add JsonNode function for java and scala with test case --- .../com/snowflake/snowpark_java/types/Variant.java | 1 - .../scala/com/snowflake/snowpark/types/Variant.scala | 11 +++++++++++ .../com/snowflake/snowpark_test/JavaVariantSuite.java | 2 +- .../snowflake/snowpark_test/ScalaVariantSuite.scala | 8 ++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index 9b8630e8..ac14e0eb 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -383,7 +383,6 @@ public String asJsonString() { * @since 1.14.0 */ public JsonNode asJsonNode() { - verify(VariantTypes.Object); return objectToJsonNode(value); } diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala index 5ff86f9c..b74a6ebb 100644 --- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala +++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala @@ -44,6 +44,8 @@ private[snowpark] object Variant { object Object extends VariantType + object Jsonnode extends VariantType + // internal used when converting from Java def getType(name: String): VariantType = name match { case "RealNumber" => RealNumber @@ -381,6 +383,15 @@ class Variant private[snowpark] ( } } + /** + * Converts the variant as valid JsonNode + * + * @since 1.14.0 + */ + def asJsonNode(): JsonNode = { + objectToJsonNode(value) + } + /** * Converts the variant as binary value * @since 0.2.0 diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index 8f31a004..482ae8b5 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -308,7 +308,7 @@ public void javaVariantConversion() { assert (result.asMap().get("a").get().asString().equals("[1,2]")); assert (result.asMap().get("a").get().asJsonString().equals("[1,2]")); - // Scala variant to Java Variant +// Scala variant to Java Variant com.snowflake.snowpark.types.Variant sv = new com.snowflake.snowpark.types.Variant("{\"a\": [1, 2], \"b\": \"c\"}"); assert InternalUtils.createVariant(null) == null; diff --git a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala index 35e8c572..da9aad43 100644 --- a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala @@ -1,6 +1,7 @@ package com.snowflake.snowpark_test import com.snowflake.snowpark.types.{Geography, Variant} +import com.snowflake.snowpark_java.types.InternalUtils import org.scalatest.FunSuite import java.io.UncheckedIOException @@ -311,4 +312,11 @@ class ScalaVariantSuite extends FunSuite { assert(v1.toString() == "123") } + + test("JsonNode") { + val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}") + assert(sv.asMap.get("a").get.asJsonNode().isArray) + assert(sv.asMap.get("b").get.asJsonNode().asInt().equals(3)) + assert(sv.asMap.get("c").get.asJsonNode().asText().equals("xyz")) + } } From b4a068976e878dcaba827af73e41aa4da2fe331f Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Tue, 27 Aug 2024 15:54:01 -0500 Subject: [PATCH 05/11] fix imports --- src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java | 2 +- .../scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index 482ae8b5..8f31a004 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -308,7 +308,7 @@ public void javaVariantConversion() { assert (result.asMap().get("a").get().asString().equals("[1,2]")); assert (result.asMap().get("a").get().asJsonString().equals("[1,2]")); -// Scala variant to Java Variant + // Scala variant to Java Variant com.snowflake.snowpark.types.Variant sv = new com.snowflake.snowpark.types.Variant("{\"a\": [1, 2], \"b\": \"c\"}"); assert InternalUtils.createVariant(null) == null; diff --git a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala index da9aad43..5392750c 100644 --- a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala @@ -1,7 +1,6 @@ package com.snowflake.snowpark_test import com.snowflake.snowpark.types.{Geography, Variant} -import com.snowflake.snowpark_java.types.InternalUtils import org.scalatest.FunSuite import java.io.UncheckedIOException From 6f35e5f73cb5429e4b3f85fa4ffb60e7cd51f5fe Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Tue, 27 Aug 2024 16:00:28 -0500 Subject: [PATCH 06/11] update test case --- .../snowflake/snowpark_test/JavaVariantSuite.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index 8f31a004..f20e9dd2 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -369,14 +369,10 @@ public void equalsAndToString() { @Test public void javaJsonNodeVariantConverter() throws IllegalArgumentException { - // Scala variant to Java Variant - com.snowflake.snowpark.types.Variant sv = - new com.snowflake.snowpark.types.Variant( - "{\"name\": \"abc\",\"grade\": 2, \"Interest\": \"cricket\"}"); - Variant jResult = InternalUtils.createVariant(sv); - JsonNode jsonNode = jResult.asJsonNode(); + Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}"); + JsonNode jNode = jv.asJsonNode(); - assert jsonNode.get("name").asText().equals("abc"); - assert jsonNode.get("grade").asInt() == 2; + assert (jNode.get("a").isArray()); + assert (jNode.get("b").asText().equals("c")); } } From dbde5ddc73b4da8afca31c70d2632f5917ca857e Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Tue, 27 Aug 2024 21:09:17 -0500 Subject: [PATCH 07/11] update docs --- .../snowflake/snowpark_java/types/Variant.java | 15 +++++++++++++-- .../com/snowflake/snowpark/types/Variant.scala | 10 ++++++++++ .../snowflake/snowpark_test/JavaVariantSuite.java | 1 - 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index ac14e0eb..75e993ad 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -377,9 +377,20 @@ public String asJsonString() { } /** - * Converts the variant as valid JsonNode. + * Converts the variant as valid JsonNode. This function allows to read the JSON object directly + * rather parsing it as String and then extract the desired value * - * @return A valid json Node + *
{@code - to get the first value from array for key "a"
+   *
+   * Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
+   * JsonNode jNode = jv.asJsonNode();
+   * System.out.println(jNode.get("a").get(0));
+   *
+   * output
+   * 1
+   * }
+ * + * @return A valid JsonNode * @since 1.14.0 */ public JsonNode asJsonNode() { diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala index b74a6ebb..c3981402 100644 --- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala +++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala @@ -385,7 +385,17 @@ class Variant private[snowpark] ( /** * Converts the variant as valid JsonNode + * This function allows to read the JSON object directly rather parsing it as String + * and then extract the desired value + * Example - to get the first value from array for key "a" + * {{{ + * val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}") + * println(sv.asMap.get("a").get.asJsonNode().get(0)) * + * output + * 1 + * + * }}} * @since 1.14.0 */ def asJsonNode(): JsonNode = { diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index f20e9dd2..93adb47b 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -371,7 +371,6 @@ public void equalsAndToString() { public void javaJsonNodeVariantConverter() throws IllegalArgumentException { Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}"); JsonNode jNode = jv.asJsonNode(); - assert (jNode.get("a").isArray()); assert (jNode.get("b").asText().equals("c")); } From b0a739e64fecf3a4ca61745dcb67ceefcf8dc02a Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Tue, 27 Aug 2024 21:10:35 -0500 Subject: [PATCH 08/11] update docs --- src/main/scala/com/snowflake/snowpark/types/Variant.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala index c3981402..636873da 100644 --- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala +++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala @@ -43,9 +43,7 @@ private[snowpark] object Variant { object Array extends VariantType object Object extends VariantType - - object Jsonnode extends VariantType - + // internal used when converting from Java def getType(name: String): VariantType = name match { case "RealNumber" => RealNumber From a569e41f022d987564e3fdcc5237a3db1ec65bf6 Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Tue, 27 Aug 2024 22:59:56 -0500 Subject: [PATCH 09/11] rebuild --- src/main/scala/com/snowflake/snowpark/types/Variant.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala index 636873da..c682dca1 100644 --- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala +++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala @@ -43,7 +43,7 @@ private[snowpark] object Variant { object Array extends VariantType object Object extends VariantType - + // internal used when converting from Java def getType(name: String): VariantType = name match { case "RealNumber" => RealNumber @@ -389,10 +389,8 @@ class Variant private[snowpark] ( * {{{ * val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}") * println(sv.asMap.get("a").get.asJsonNode().get(0)) - * * output * 1 - * * }}} * @since 1.14.0 */ From a946cee079a1d0168b0e53b8f0809a9b3827e08d Mon Sep 17 00:00:00 2001 From: Ganesh Mahadevan Date: Wed, 28 Aug 2024 16:25:02 -0500 Subject: [PATCH 10/11] update asJsonNode impl for scala and java --- .../com/snowflake/snowpark_java/types/Variant.java | 9 ++++----- .../scala/com/snowflake/snowpark/types/Variant.scala | 10 +++++----- .../com/snowflake/snowpark_test/JavaVariantSuite.java | 5 ++--- .../snowflake/snowpark_test/ScalaVariantSuite.scala | 6 +++--- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index 75e993ad..140699d2 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -377,14 +377,13 @@ public String asJsonString() { } /** - * Converts the variant as valid JsonNode. This function allows to read the JSON object directly - * rather parsing it as String and then extract the desired value + * Return the variant value as a JsonNode. This function allows to read the JSON object directly + * as JsonNode from variant column rather parsing it as String * *
{@code - to get the first value from array for key "a"
    *
    * Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
-   * JsonNode jNode = jv.asJsonNode();
-   * System.out.println(jNode.get("a").get(0));
+   * System.out.println(jv.asJsonNode().get("a").get(0));
    *
    * output
    * 1
@@ -394,7 +393,7 @@ public String asJsonString() {
    * @since 1.14.0
    */
   public JsonNode asJsonNode() {
-    return objectToJsonNode(value);
+    return value;
   }
 
   /**
diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala
index c682dca1..a146b899 100644
--- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala
+++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala
@@ -382,20 +382,20 @@ class Variant private[snowpark] (
   }
 
   /**
-   * Converts the variant as valid JsonNode
-   * This function allows to read the JSON object directly rather parsing it as String
-   * and then extract the desired value
+   * Return the variant value as a JsonNode. This function allows to read the JSON object directly
+   * as JsonNode from variant column rather parsing it as String
    * Example - to get the first value from array for key "a"
    * {{{
    *   val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}")
-   *   println(sv.asMap.get("a").get.asJsonNode().get(0))
+   *   println(sv.asJsonNode().get("a").get(0))
    * output
    * 1
    * }}}
+ *
    * @since 1.14.0
    */
   def asJsonNode(): JsonNode = {
-    objectToJsonNode(value)
+    value
   }
 
   /**
diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
index 93adb47b..de2e953e 100644
--- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
+++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
@@ -370,8 +370,7 @@ public void equalsAndToString() {
   @Test
   public void javaJsonNodeVariantConverter() throws IllegalArgumentException {
     Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
-    JsonNode jNode = jv.asJsonNode();
-    assert (jNode.get("a").isArray());
-    assert (jNode.get("b").asText().equals("c"));
+    assert (jv.asJsonNode().get("a").isArray());
+    assert (jv.asJsonNode().get("b").asText().equals("c"));
   }
 }
diff --git a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala
index 5392750c..1f03c3f6 100644
--- a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala
+++ b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala
@@ -314,8 +314,8 @@ class ScalaVariantSuite extends FunSuite {
 
   test("JsonNode") {
     val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}")
-    assert(sv.asMap.get("a").get.asJsonNode().isArray)
-    assert(sv.asMap.get("b").get.asJsonNode().asInt().equals(3))
-    assert(sv.asMap.get("c").get.asJsonNode().asText().equals("xyz"))
+    assert(sv.asJsonNode().get("a").isArray)
+    assert(sv.asJsonNode().get("b").asInt().equals(3))
+    assert(sv.asJsonNode().get("c").asText().equals("xyz"))
   }
 }

From 58ea5b1831cfd850f6d26555f78df3a7f43f3867 Mon Sep 17 00:00:00 2001
From: Ganesh Mahadevan 
Date: Wed, 28 Aug 2024 16:37:20 -0500
Subject: [PATCH 11/11] format

---
 src/main/scala/com/snowflake/snowpark/types/Variant.scala       | 2 +-
 src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala
index a146b899..be0424b4 100644
--- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala
+++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala
@@ -391,7 +391,7 @@ class Variant private[snowpark] (
    * output
    * 1
    * }}}
- *
+   *
    * @since 1.14.0
    */
   def asJsonNode(): JsonNode = {
diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
index de2e953e..83ce4ea8 100644
--- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
+++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java
@@ -1,6 +1,5 @@
 package com.snowflake.snowpark_test;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import com.snowflake.snowpark_java.types.Geography;
 import com.snowflake.snowpark_java.types.InternalUtils;
 import com.snowflake.snowpark_java.types.Variant;