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 c2579ab947 PHOENIX-7551 BSON_VALUE_TYPE function to return the data 
type of BSON field value (#2228)
c2579ab947 is described below

commit c2579ab947850e479a5093ec23219e01ba74fab6
Author: Rahul Kumar <[email protected]>
AuthorDate: Sat Jul 26 01:24:47 2025 +0530

    PHOENIX-7551 BSON_VALUE_TYPE function to return the data type of BSON field 
value (#2228)
---
 .../apache/phoenix/expression/ExpressionType.java  |   1 +
 .../expression/function/BsonValueTypeFunction.java | 149 +++++++++++++++++++++
 .../phoenix/parse/BsonValueTypeParseNode.java      |  45 +++++++
 .../java/org/apache/phoenix/end2end/Bson3IT.java   | 133 +++++++++++++++++-
 4 files changed, 327 insertions(+), 1 deletion(-)

diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/ExpressionType.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/ExpressionType.java
index 7e4844047e..f3d8993f85 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/ExpressionType.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/ExpressionType.java
@@ -196,6 +196,7 @@ public enum ExpressionType {
   BsonConditionExpressionFunction(BsonConditionExpressionFunction.class),
   BsonUpdateExpressionFunction(BsonUpdateExpressionFunction.class),
   BsonValueFunction(BsonValueFunction.class),
+  BsonValueTypeFunction(BsonValueTypeFunction.class),
   PartitionIdFunction(PartitionIdFunction.class),
   DecodeBinaryFunction(DecodeBinaryFunction.class),
   EncodeBinaryFunction(EncodeBinaryFunction.class),
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueTypeFunction.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueTypeFunction.java
new file mode 100644
index 0000000000..86cb9b39bb
--- /dev/null
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonValueTypeFunction.java
@@ -0,0 +1,149 @@
+/*
+ * 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.phoenix.expression.function;
+
+import java.util.List;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.util.bson.CommonComparisonExpressionUtils;
+import org.apache.phoenix.parse.BsonValueTypeParseNode;
+import org.apache.phoenix.parse.FunctionParseNode;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PBson;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDate;
+import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PJson;
+import org.apache.phoenix.schema.types.PLong;
+import org.apache.phoenix.schema.types.PVarbinary;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.bson.BsonArray;
+import org.bson.BsonBinary;
+import org.bson.BsonBoolean;
+import org.bson.BsonDateTime;
+import org.bson.BsonDecimal128;
+import org.bson.BsonDocument;
+import org.bson.BsonDouble;
+import org.bson.BsonInt32;
+import org.bson.BsonInt64;
+import org.bson.BsonNull;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.bson.RawBsonDocument;
+
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+
+/**
+ * BSON_VALUE_TYPE function to retrieve the SQL data type name of any field in 
BSON. This can be
+ * used for any top-level or nested Bson fields. 1. The first argument 
represents BSON Object on
+ * which the function performs scan. 2. The second argument represents the 
field key. The field key
+ * can represent any top level or nested fields within the document. The 
caller should use "."
+ * notation for accessing nested document elements and "[n]" notation for 
accessing nested array
+ * elements. Top level fields do not require any additional character.
+ */
[email protected](name = BsonValueTypeFunction.NAME,
+    nodeClass = BsonValueTypeParseNode.class,
+    args = {
+      @FunctionParseNode.Argument(allowedTypes = { PJson.class, PBson.class, 
PVarbinary.class }),
+      @FunctionParseNode.Argument(allowedTypes = { PVarchar.class }, 
isConstant = true), })
+public class BsonValueTypeFunction extends ScalarFunction {
+
+  public static final String NAME = "BSON_VALUE_TYPE";
+
+  public BsonValueTypeFunction() {
+    // no-op
+  }
+
+  public BsonValueTypeFunction(List<Expression> children) {
+    super(children);
+    Preconditions.checkNotNull(getChildren().get(1));
+  }
+
+  @Override
+  public String getName() {
+    return NAME;
+  }
+
+  @Override
+  public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+    if (!getChildren().get(0).evaluate(tuple, ptr)) {
+      return false;
+    }
+    if (ptr == null || ptr.getLength() == 0) {
+      return false;
+    }
+
+    Object object = PBson.INSTANCE.toObject(ptr, 
getChildren().get(0).getSortOrder());
+    RawBsonDocument rawBsonDocument = (RawBsonDocument) object;
+
+    if (!getChildren().get(1).evaluate(tuple, ptr)) {
+      return false;
+    }
+    if (ptr.getLength() == 0) {
+      return false;
+    }
+
+    String documentFieldKey =
+      (String) PVarchar.INSTANCE.toObject(ptr, 
getChildren().get(1).getSortOrder());
+    if (documentFieldKey == null) {
+      return false;
+    }
+
+    BsonValue bsonValue =
+      CommonComparisonExpressionUtils.getFieldFromDocument(documentFieldKey, 
rawBsonDocument);
+    if (bsonValue == null) {
+      ptr.set(PVarchar.INSTANCE.toBytes("NULL"));
+      return true;
+    }
+
+    String sqlTypeName = getValueType(bsonValue);
+    ptr.set(PVarchar.INSTANCE.toBytes(sqlTypeName));
+    return true;
+  }
+
+  private String getValueType(BsonValue bsonValue) {
+    if (bsonValue instanceof BsonString) {
+      return PVarchar.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonInt32) {
+      return PInteger.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonInt64) {
+      return PLong.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonDouble || bsonValue instanceof 
BsonDecimal128) {
+      return PDouble.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonBoolean) {
+      return PBoolean.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonBinary) {
+      return PVarbinary.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonDateTime) {
+      return PDate.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonDocument || bsonValue instanceof 
BsonArray) {
+      return PBson.INSTANCE.getSqlTypeName();
+    } else if (bsonValue instanceof BsonNull) {
+      return "NULL";
+    } else {
+      return PVarchar.INSTANCE.getSqlTypeName();
+    }
+  }
+
+  @Override
+  public PDataType<?> getDataType() {
+    return PVarchar.INSTANCE;
+  }
+}
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonValueTypeParseNode.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonValueTypeParseNode.java
new file mode 100644
index 0000000000..6040861d25
--- /dev/null
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonValueTypeParseNode.java
@@ -0,0 +1,45 @@
+/*
+ * 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.phoenix.parse;
+
+import java.sql.SQLException;
+import java.util.List;
+import org.apache.phoenix.compile.StatementContext;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.function.BsonValueTypeFunction;
+import org.apache.phoenix.expression.function.FunctionExpression;
+import org.apache.phoenix.schema.types.PBson;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PJson;
+
+public class BsonValueTypeParseNode extends FunctionParseNode {
+
+  public BsonValueTypeParseNode(String name, List<ParseNode> children, 
BuiltInFunctionInfo info) {
+    super(name, children, info);
+  }
+
+  @Override
+  public FunctionExpression create(List<Expression> children, StatementContext 
context)
+    throws SQLException {
+    PDataType<?> dataType = children.get(0).getDataType();
+    if (!dataType.isCoercibleTo(PJson.INSTANCE) && 
!dataType.isCoercibleTo(PBson.INSTANCE)) {
+      throw new SQLException(dataType + " type is unsupported for 
BSON_VALUE_TYPE().");
+    }
+    return new BsonValueTypeFunction(children);
+  }
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson3IT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson3IT.java
index c9bf6505f3..a0ccdb3765 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson3IT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson3IT.java
@@ -51,7 +51,7 @@ import org.apache.hadoop.hbase.TableName;
 import org.apache.hadoop.hbase.client.Admin;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.query.QueryConstants;
-import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.schema.types.*;
 import org.apache.phoenix.util.CDCUtil;
 import org.apache.phoenix.util.EnvironmentEdgeManager;
 import org.apache.phoenix.util.ManualEnvironmentEdge;
@@ -2161,6 +2161,137 @@ public class Bson3IT extends ParallelStatsDisabledIT {
     }
   }
 
+  @Test
+  public void testBsonValueWithBsonValueType() throws Exception {
+    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");
+      BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+      PreparedStatement stmt =
+        conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+      stmt.setString(1, "pk0001");
+      stmt.setString(2, "0002");
+      stmt.setObject(3, bsonDocument1);
+      stmt.executeUpdate();
+      conn.commit();
+
+      Object[][] testCases = { { "cold", PVarchar.INSTANCE.getSqlTypeName(), 
"method" },
+        { "hurry", PDouble.INSTANCE.getSqlTypeName(), -593264871.4436941 },
+        { "press", PVarchar.INSTANCE.getSqlTypeName(), "beat" },
+        { "browserling", PDouble.INSTANCE.getSqlTypeName(), 
-505169340.54880095 },
+        { "attention", PInteger.INSTANCE.getSqlTypeName(), 166583691 },
+        { "track[0].speed", PVarchar.INSTANCE.getSqlTypeName(), "sun" },
+        { "track[0].shot[2][0].character", PInteger.INSTANCE.getSqlTypeName(), 
413968576 },
+        { "track[0].shot[2][0].earth", PVarchar.INSTANCE.getSqlTypeName(), 
"helpful" },
+        { "track[0].shot[2][0].money", PBoolean.INSTANCE.getSqlTypeName(), 
false },
+        { "track[0].shot[2][0].city.standard[5]", 
PVarchar.INSTANCE.getSqlTypeName(), "softly" },
+        { "track[0].shot[2][0].city.problem[2]", 
PDouble.INSTANCE.getSqlTypeName(),
+          1527061470.2690287 },
+        { "track[0].shot[2][0].city.problem[3].condition", 
PVarchar.INSTANCE.getSqlTypeName(),
+          "else" },
+        { "track[0].shot[2][0].city.problem[3].higher", 
PDouble.INSTANCE.getSqlTypeName(),
+          1462910924.088698 },
+        { "track[0].shot[2][0].city.problem[3].scene", 
PBoolean.INSTANCE.getSqlTypeName(), false },
+        { "track[0].shot[2][0].city.flame", 
PInteger.INSTANCE.getSqlTypeName(), 1066643931 },
+        { "track[0].shot[2][0].city.brought", 
PVarchar.INSTANCE.getSqlTypeName(), "rock" },
+        { "track[0].shot[2][0].height", PBoolean.INSTANCE.getSqlTypeName(), 
true },
+        { "track[0].disease", PVarchar.INSTANCE.getSqlTypeName(), "disease" },
+        { "track[1]", PVarchar.INSTANCE.getSqlTypeName(), "printed" },
+        { "symbol", PBoolean.INSTANCE.getSqlTypeName(), false } };
+
+      for (Object[] testCase : testCases) {
+        String path = (String) testCase[0];
+        String expectedType = (String) testCase[1];
+        Object expectedValue = testCase[2];
+
+        String typeQuery =
+          "SELECT BSON_VALUE_TYPE(COL, '" + path + "') FROM " + tableName + " 
WHERE PK1 = 'pk0001'";
+
+        ResultSet rs = conn.createStatement().executeQuery(typeQuery);
+        assertTrue("No result for path " + path, rs.next());
+        String actualType = rs.getString(1);
+        assertEquals("Wrong type for path " + path, expectedType, actualType);
+
+        String valueQuery = "SELECT BSON_VALUE(COL, '" + path + "', '" + 
actualType + "') FROM "
+          + tableName + " WHERE PK1 = 'pk0001'";
+
+        rs = conn.createStatement().executeQuery(valueQuery);
+        assertTrue("No result for path " + path, rs.next());
+
+        if (expectedType.equals(PVarchar.INSTANCE.getSqlTypeName())) {
+          assertEquals("Wrong value for path " + path, expectedValue, 
rs.getString(1));
+        } else if (expectedType.equals(PInteger.INSTANCE.getSqlTypeName())) {
+          assertEquals("Wrong value for path " + path, ((Number) 
expectedValue).intValue(),
+            rs.getInt(1));
+        } else if (expectedType.equals(PLong.INSTANCE.getSqlTypeName())) {
+          assertEquals("Wrong value for path " + path, ((Number) 
expectedValue).longValue(),
+            rs.getLong(1));
+        } else if (expectedType.equals(PDouble.INSTANCE.getSqlTypeName())) {
+          assertEquals("Wrong value for path " + path, ((Number) 
expectedValue).doubleValue(),
+            rs.getDouble(1), 0.000001);
+        } else if (expectedType.equals(PBoolean.INSTANCE.getSqlTypeName())) {
+          assertEquals("Wrong value for path " + path, expectedValue, 
rs.getBoolean(1));
+        } else if (expectedType.equals(PBson.INSTANCE.getSqlTypeName())) {
+          assertNotNull("Null value for path " + path, rs.getObject(1));
+        }
+
+        assertFalse("Too many results for path " + path, rs.next());
+      }
+    }
+  }
+
+  @Test
+  public void testBsonValueTypeNegativeScenarios() throws Exception {
+    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");
+      BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+
+      PreparedStatement stmt =
+        conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+      stmt.setString(1, "pk0001");
+      stmt.setString(2, "0002");
+      stmt.setObject(3, bsonDocument1);
+      stmt.executeUpdate();
+      conn.commit();
+
+      ResultSet rs = conn.createStatement()
+        .executeQuery("SELECT BSON_VALUE_TYPE(COL, 'non_existent_field') FROM 
" + tableName
+          + " WHERE PK1 = 'pk0001'");
+      assertTrue(rs.next());
+      assertEquals("NULL", rs.getString(1));
+
+      rs = conn.createStatement().executeQuery(
+        "SELECT BSON_VALUE_TYPE(COL, 'track[999]') FROM " + tableName + " 
WHERE PK1 = 'pk0001'");
+      assertTrue(rs.next());
+      assertEquals("NULL", rs.getString(1));
+
+      rs = conn.createStatement().executeQuery(
+        "SELECT BSON_VALUE_TYPE(COL, '') FROM " + tableName + " WHERE PK1 = 
'pk0001'");
+      assertTrue(rs.next());
+      assertNull(rs.getString(1));
+
+      rs = conn.createStatement().executeQuery(
+        "SELECT BSON_VALUE_TYPE(COL, NULL) FROM " + tableName + " WHERE PK1 = 
'pk0001'");
+      assertTrue(rs.next());
+      assertNull(rs.getString(1));
+    }
+  }
+
   private static void verifyFinalPreImagesWithLastPostImages(
     Map<String, Map<String, Object>> ttlDeleteEvents,
     Map<String, Map<String, Object>> lastPostImages) {

Reply via email to