This is an automated email from the ASF dual-hosted git repository.

vjasani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/master by this push:
     new 9e3104ed01 PHOENIX-7545 BSON_VALUE() to support returning sub-document 
(#2210)
9e3104ed01 is described below

commit 9e3104ed0132b5a51a25f604ebd7c63bb805d3c8
Author: Rahul Kumar <[email protected]>
AuthorDate: Fri Jul 11 09:45:46 2025 +0530

    PHOENIX-7545 BSON_VALUE() to support returning sub-document (#2210)
---
 .../expression/function/BsonValueFunction.java     |  3 ++
 .../java/org/apache/phoenix/end2end/Bson4IT.java   | 60 ++++++++++++++++++++++
 phoenix-core/src/it/resources/json/result_03.json  | 36 +++++++++++++
 3 files changed, 99 insertions(+)

diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueFunction.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueFunction.java
index b56283379f..8273a0f60c 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueFunction.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueFunction.java
@@ -26,6 +26,7 @@ import org.apache.phoenix.util.DateUtil;
 import org.bson.BsonBinary;
 import org.bson.BsonBoolean;
 import org.bson.BsonDateTime;
+import org.bson.BsonDocument;
 import org.bson.BsonNumber;
 import org.bson.BsonString;
 import org.bson.BsonValue;
@@ -166,6 +167,8 @@ public class BsonValueFunction extends ScalarFunction {
             ptr.set(PVarbinaryEncoded.INSTANCE.toBytes(((BsonBinary) 
bsonValue).getData()));
         } else if (bsonValueDataType == PDate.INSTANCE && bsonValue instanceof 
BsonDateTime) {
             ptr.set(PDate.INSTANCE.toBytes(new Date(((BsonDateTime) 
bsonValue).getValue())));
+        } else if (bsonValueDataType == PBson.INSTANCE && bsonValue instanceof 
BsonDocument) {
+            ptr.set(PBson.INSTANCE.toBytes((BsonDocument) bsonValue));
         } else {
             throw new IllegalArgumentException(
                 "The function data type does not match with actual data type");
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson4IT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson4IT.java
index c20a7bbd23..7cbef1b4c6 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson4IT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson4IT.java
@@ -44,6 +44,7 @@ import org.bson.BsonDouble;
 import org.bson.BsonNull;
 import org.bson.BsonString;
 import org.bson.RawBsonDocument;
+import org.bson.Document;
 import org.junit.Assume;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -495,6 +496,65 @@ public class Bson4IT extends ParallelStatsDisabledIT {
     }
   }
 
+  @Test
+  public void testBsonValueFunctionWithBSONType() throws Exception {
+    Assume.assumeTrue(this.coveredIndex && this.columnEncoded); // Since 
indexing on BSON not supported PHOENIX-7654
+    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+    String tableName = generateUniqueName();
+    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+      String ddl = "CREATE TABLE " + tableName
+          + " (PK1 VARCHAR NOT NULL, C1 VARCHAR, COL BSON"
+          + " CONSTRAINT pk PRIMARY KEY(PK1)) "
+          + (this.columnEncoded ? "" : "COLUMN_ENCODED_BYTES=0");
+
+      conn.createStatement().execute(ddl);
+
+      String sample1 = getJsonString("json/sample_01.json");
+      String sample2 = getJsonString("json/sample_02.json");
+      String sample3 = getJsonString("json/sample_03.json");
+      String result = getJsonString("json/result_03.json");
+      BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+      BsonDocument bsonDocument2 = RawBsonDocument.parse(sample2);
+      BsonDocument bsonDocument3 = RawBsonDocument.parse(sample3);
+      BsonDocument bsonResultDocument = RawBsonDocument.parse(result);
+
+      upsertRows(conn, tableName, bsonDocument1, bsonDocument2, bsonDocument3);
+
+      conn.commit();
+
+      ResultSet rs = conn.createStatement().executeQuery("SELECT count(*) FROM 
" + tableName);
+      assertTrue(rs.next());
+      assertEquals(3, rs.getInt(1));
+
+
+      PreparedStatement ps = conn.prepareStatement("SELECT PK1, COL, "
+          + "BSON_VALUE(COL, 'result[1]', 'BSON') FROM " + tableName
+          + " WHERE BSON_VALUE(COL, 'result[1].location', 'BSON') = ?");
+
+      Document info = new Document()
+          .append("street", "4897 Gerhold Lodge")
+          .append("city", "Minneapolis")
+          .append("state", "Arkansas")
+          .append("country", "Democratic Republic of the Congo")
+          .append("zip", "79299")
+          .append("coordinates", new Document()
+              .append("latitude", 3.4015)
+              .append("longitude", 52.3736));
+      RawBsonDocument document = RawBsonDocument.parse(info.toJson());
+
+      ps.setObject(1, document);
+      rs = ps.executeQuery();
+
+      assertTrue(rs.next());
+      assertEquals("pk1011", rs.getString(1));
+      BsonDocument actualDoc = (BsonDocument) rs.getObject(3);
+      assertEquals(bsonResultDocument, actualDoc);
+      assertFalse(rs.next());
+
+      validateExplainPlan(ps, tableName, "FULL SCAN ");
+    }
+  }
+
   @Test
   public void testConditionalUpsertReturnRow() throws Exception {
     Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
diff --git a/phoenix-core/src/it/resources/json/result_03.json 
b/phoenix-core/src/it/resources/json/result_03.json
new file mode 100644
index 0000000000..256d05a95d
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/result_03.json
@@ -0,0 +1,36 @@
+{
+  "status": "disabled",
+  "name": {
+    "first": "Elliot",
+    "middle": "Jamie",
+    "last": "Watsica"
+  },
+  "username": "Elliot-Watsica",
+  "emails": [
+    "[email protected]",
+    "[email protected]"
+  ],
+  "phoneNumber": "(308) 971-1166 x5940",
+  "location": {
+    "street": "4897 Gerhold Lodge",
+    "city": "Minneapolis",
+    "state": "Arkansas",
+    "country": "Democratic Republic of the Congo",
+    "zip": "79299",
+    "coordinates": {
+      "latitude": 3.4015,
+      "longitude": 52.3736
+    }
+  },
+  "website": "https://cautious-fugato.info";,
+  "domain": "pricey-block.org",
+  "job": {
+    "title": "Forward Program Developer",
+    "descriptor": "Direct",
+    "area": "Quality",
+    "type": "Producer",
+    "company": "Davis, Turcotte and Padberg"
+  },
+  "uuid": "80874c0d-3014-4860-8a08-d0f59c709a82",
+  "objectId": "666bdf52dd6539db3454d49a"
+}
\ No newline at end of file

Reply via email to