You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by vj...@apache.org on 2024/08/12 02:29:54 UTC
(phoenix) branch master updated: PHOENIX-7330 Introducing Binary JSON (BSON) with Complex Document structures in Phoenix (#1906)
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 f1b0102301 PHOENIX-7330 Introducing Binary JSON (BSON) with Complex Document structures in Phoenix (#1906)
f1b0102301 is described below
commit f1b0102301c06390c51716bebffc6ebd2eda7b19
Author: Viraj Jasani <vj...@apache.org>
AuthorDate: Sun Aug 11 19:29:49 2024 -0700
PHOENIX-7330 Introducing Binary JSON (BSON) with Complex Document structures in Phoenix (#1906)
---
phoenix-core-client/pom.xml | 4 +
.../phoenix/compile/CreateIndexCompiler.java | 5 +-
.../apache/phoenix/compile/ProjectionCompiler.java | 6 +-
.../apache/phoenix/expression/ExpressionType.java | 5 +-
.../function/BsonConditionExpressionFunction.java | 129 ++
.../function/BsonUpdateExpressionFunction.java | 123 ++
.../expression/function/JsonValueFunction.java | 4 +-
.../util/bson/CommonComparisonExpressionUtils.java | 252 +++
.../bson/DocumentComparisonExpressionUtils.java | 351 ++++
.../util/bson/SQLComparisonExpressionUtils.java | 335 ++++
.../util/bson/UpdateExpressionUtils.java | 800 +++++++++
....java => BsonConditionExpressionParseNode.java} | 24 +-
...ode.java => BsonUpdateExpressionParseNode.java} | 24 +-
.../apache/phoenix/parse/JsonValueParseNode.java | 3 +-
.../org/apache/phoenix/schema/types/PBson.java | 136 ++
.../org/apache/phoenix/schema/types/PDataType.java | 7 +-
.../phoenix/schema/types/PDataTypeFactory.java | 1 +
.../main/java/org/apache/phoenix/util/CDCUtil.java | 7 +-
.../java/org/apache/phoenix/end2end/Bson1IT.java | 304 ++++
.../java/org/apache/phoenix/end2end/Bson2IT.java | 743 ++++++++
.../java/org/apache/phoenix/end2end/Bson3IT.java | 1859 ++++++++++++++++++++
.../phoenix/util/bson/SerializableBytesPtr.java | 66 +
.../apache/phoenix/util/bson/TestFieldValue.java | 961 ++++++++++
.../apache/phoenix/util/bson/TestFieldsMap.java | 83 +
.../org/apache/phoenix/util/bson/TestUtil.java | 353 ++++
phoenix-core/src/it/resources/json/sample_01.json | 80 +
phoenix-core/src/it/resources/json/sample_02.json | 92 +
phoenix-core/src/it/resources/json/sample_03.json | 328 ++++
.../src/it/resources/json/sample_updated_01.json | 84 +
.../src/it/resources/json/sample_updated_02.json | 167 ++
.../src/it/resources/json/sample_updated_03.json | 327 ++++
.../phoenix/expression/CoerceExpressionTest.java | 9 +-
.../apache/phoenix/schema/types/PDataTypeTest.java | 1 +
.../util/bson/ComparisonExpressionUtilsTest.java | 1020 +++++++++++
.../util/bson/UpdateExpressionUtilsTest.java | 895 ++++++++++
.../pherf/configuration/DataTypeMapping.java | 3 +-
.../apache/phoenix/pherf/rules/RulesApplier.java | 2 +
.../pherf/rules/SequentialListDataGenerator.java | 1 +
.../rules/SequentialVarcharDataGenerator.java | 1 +
.../org/apache/phoenix/pherf/util/PhoenixUtil.java | 3 +-
pom.xml | 13 +
41 files changed, 9572 insertions(+), 39 deletions(-)
diff --git a/phoenix-core-client/pom.xml b/phoenix-core-client/pom.xml
index 5cdedb4bdf..ad65b74f39 100644
--- a/phoenix-core-client/pom.xml
+++ b/phoenix-core-client/pom.xml
@@ -438,5 +438,9 @@
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.mvel</groupId>
+ <artifactId>mvel2</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/compile/CreateIndexCompiler.java b/phoenix-core-client/src/main/java/org/apache/phoenix/compile/CreateIndexCompiler.java
index 5e8c1e835a..c08bfabcda 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/compile/CreateIndexCompiler.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/compile/CreateIndexCompiler.java
@@ -43,6 +43,7 @@ import org.apache.phoenix.schema.PTable.IndexType;
import org.apache.phoenix.schema.tuple.MultiKeyValueTuple;
import org.apache.phoenix.schema.types.PArrayDataType;
import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PBson;
import org.apache.phoenix.schema.types.PChar;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PDate;
@@ -111,8 +112,8 @@ public class CreateIndexCompiler {
return "ARRAY[" + getValue(PDate.INSTANCE) + "]";
} else if (type instanceof PArrayDataType) {
return "ARRAY" + type.getSampleValue().toString();
- } else if (type instanceof PJson) {
- return "'{a:1}'";
+ } else if (type instanceof PJson || type instanceof PBson) {
+ return "'{\"a\":\"b\"}'";
} else {
return "0123";
}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java b/phoenix-core-client/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
index dc1349ef64..6300304c94 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
@@ -88,6 +88,7 @@ import org.apache.phoenix.schema.ValueBitSet;
import org.apache.phoenix.schema.tuple.Tuple;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PJson;
+import org.apache.phoenix.schema.types.PBson;
import org.apache.phoenix.util.ByteUtil;
import org.apache.phoenix.util.IndexUtil;
import org.apache.phoenix.util.SchemaUtil;
@@ -782,7 +783,7 @@ public class ProjectionCompiler {
private static boolean parseOnServer(Expression expression) {
return expression.getDataType().isArrayType() || expression.getDataType()
- .equals(PJson.INSTANCE);
+ .equals(PJson.INSTANCE) || expression.getDataType().equals(PBson.INSTANCE);
}
@Override
@@ -817,7 +818,8 @@ public class ProjectionCompiler {
@Override
public Void visit(ProjectedColumnExpression expression) {
if (expression.getDataType().isArrayType() || expression.getDataType()
- .equals(PJson.INSTANCE)) {
+ .equals(PJson.INSTANCE) || expression.getDataType()
+ .equals(PBson.INSTANCE)) {
indexProjectedColumns.add(expression);
PColumn col = expression.getColumn();
// hack'ish... For covered columns with local indexes we defer to the server.
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 e0abce8900..2b53f4c034 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,8 +196,9 @@ public enum ExpressionType {
JsonValueFunction(JsonValueFunction.class),
JsonQueryFunction(JsonQueryFunction.class),
JsonExistsFunction(JsonExistsFunction.class),
- JsonModifyFunction(JsonModifyFunction.class)
- ;
+ JsonModifyFunction(JsonModifyFunction.class),
+ BsonConditionExpressionFunction(BsonConditionExpressionFunction.class),
+ BsonUpdateExpressionFunction(BsonUpdateExpressionFunction.class);
ExpressionType(Class<? extends Expression> clazz) {
this.clazz = clazz;
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonConditionExpressionFunction.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonConditionExpressionFunction.java
new file mode 100644
index 0000000000..f9cd26c84e
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonConditionExpressionFunction.java
@@ -0,0 +1,129 @@
+/*
+ * 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.phoenix.expression.util.bson.DocumentComparisonExpressionUtils;
+import org.bson.BsonDocument;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.bson.RawBsonDocument;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.util.bson.SQLComparisonExpressionUtils;
+import org.apache.phoenix.parse.FunctionParseNode;
+import org.apache.phoenix.parse.BsonConditionExpressionParseNode;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PBson;
+import org.apache.phoenix.schema.types.PVarbinary;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+
+@FunctionParseNode.BuiltInFunction(name = BsonConditionExpressionFunction.NAME,
+ nodeClass = BsonConditionExpressionParseNode.class,
+ args =
+ {
+ @FunctionParseNode.Argument(allowedTypes = {PBson.class, PVarbinary.class}),
+ @FunctionParseNode.Argument(allowedTypes = {PBson.class, PVarbinary.class},
+ isConstant = true)
+ })
+public class BsonConditionExpressionFunction extends ScalarFunction {
+
+ public static final String NAME = "BSON_CONDITION_EXPRESSION";
+
+ public BsonConditionExpressionFunction() {
+ }
+
+ public BsonConditionExpressionFunction(List<Expression> children) {
+ super(children);
+ Preconditions.checkNotNull(getChildren().get(1));
+ }
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Override
+ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+ // Evaluate the BSON cell value
+ if (!getChildren().get(0).evaluate(tuple, ptr)) {
+ return false;
+ }
+ if (ptr == null || ptr.getLength() == 0) {
+ return false;
+ }
+
+ RawBsonDocument rawBsonDocument = (RawBsonDocument) PBson.INSTANCE.toObject(ptr);
+
+ // Evaluate condition expression
+ if (!getChildren().get(1).evaluate(tuple, ptr)) {
+ return false;
+ }
+ if (ptr.getLength() == 0) {
+ return false;
+ }
+
+ final RawBsonDocument conditionExpressionBsonDoc;
+ if (getChildren().get(1).getDataType() == PVarchar.INSTANCE) {
+ String conditionExpression =
+ (String) PVarchar.INSTANCE.toObject(ptr, getChildren().get(1).getSortOrder());
+ if (conditionExpression == null || conditionExpression.isEmpty()) {
+ ptr.set(PBoolean.INSTANCE.toBytes(true));
+ return true;
+ }
+ conditionExpressionBsonDoc = RawBsonDocument.parse(conditionExpression);
+ } else {
+ conditionExpressionBsonDoc = (RawBsonDocument) PBson.INSTANCE.toObject(ptr);
+ if (conditionExpressionBsonDoc == null || conditionExpressionBsonDoc.isEmpty()) {
+ ptr.set(PBoolean.INSTANCE.toBytes(true));
+ return true;
+ }
+ }
+
+ BsonValue conditionExp = conditionExpressionBsonDoc.get("$EXPR");
+ BsonValue exprValues = conditionExpressionBsonDoc.get("$VAL");
+ if (conditionExp != null && exprValues != null) {
+ if (conditionExp.isString() && exprValues.isDocument()) {
+ SQLComparisonExpressionUtils sqlComparisonExpressionUtils =
+ new SQLComparisonExpressionUtils(rawBsonDocument,
+ (BsonDocument) exprValues);
+ boolean result = sqlComparisonExpressionUtils.evaluateConditionExpression(
+ ((BsonString) conditionExp).getValue());
+ ptr.set(PBoolean.INSTANCE.toBytes(result));
+ return true;
+ }
+ throw new IllegalArgumentException(
+ "Condition Expression should contain valid expression and values");
+ } else {
+ boolean result = DocumentComparisonExpressionUtils.evaluateConditionExpression(
+ rawBsonDocument, conditionExpressionBsonDoc);
+ ptr.set(PBoolean.INSTANCE.toBytes(result));
+ return true;
+ }
+ }
+
+ @Override
+ public PDataType getDataType() {
+ return PBoolean.INSTANCE;
+ }
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonUpdateExpressionFunction.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonUpdateExpressionFunction.java
new file mode 100644
index 0000000000..a12b308110
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonUpdateExpressionFunction.java
@@ -0,0 +1,123 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.List;
+
+import org.bson.BsonBinaryReader;
+import org.bson.BsonDocument;
+import org.bson.RawBsonDocument;
+import org.bson.codecs.BsonDocumentCodec;
+import org.bson.codecs.DecoderContext;
+import org.bson.io.ByteBufferBsonInput;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.util.bson.UpdateExpressionUtils;
+import org.apache.phoenix.parse.BsonUpdateExpressionParseNode;
+import org.apache.phoenix.parse.FunctionParseNode;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PBson;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PVarbinary;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+
+@FunctionParseNode.BuiltInFunction(name = BsonUpdateExpressionFunction.NAME,
+ nodeClass = BsonUpdateExpressionParseNode.class,
+ args =
+ {
+ @FunctionParseNode.Argument(allowedTypes = {PBson.class, PVarbinary.class}),
+ @FunctionParseNode.Argument(allowedTypes = {PBson.class, PVarbinary.class},
+ isConstant = true)
+ })
+public class BsonUpdateExpressionFunction extends ScalarFunction {
+
+ public static final String NAME = "BSON_UPDATE_EXPRESSION";
+
+ public BsonUpdateExpressionFunction() {
+ }
+
+ public BsonUpdateExpressionFunction(List<Expression> children) {
+ super(children);
+ Preconditions.checkNotNull(getChildren().get(1));
+ }
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Override
+ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+ // Evaluate the BSON cell value
+ if (!getChildren().get(0).evaluate(tuple, ptr)) {
+ return false;
+ }
+ if (ptr == null || ptr.getLength() == 0) {
+ return false;
+ }
+
+ RawBsonDocument rawBsonDocument = (RawBsonDocument) PBson.INSTANCE.toObject(ptr);
+ BsonDocument bsonDocument;
+ try (BsonBinaryReader bsonReader = new BsonBinaryReader(
+ new ByteBufferBsonInput(rawBsonDocument.getByteBuffer()))) {
+ bsonDocument =
+ new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build());
+ }
+
+ // Evaluate update expression
+ if (!getChildren().get(1).evaluate(tuple, ptr)) {
+ return false;
+ }
+ if (ptr.getLength() == 0) {
+ return false;
+ }
+
+ final RawBsonDocument updateExpressionBsonDoc;
+ if (getChildren().get(1).getDataType() == PVarchar.INSTANCE) {
+ String updateExpression =
+ (String) PVarchar.INSTANCE.toObject(ptr, getChildren().get(1).getSortOrder());
+ if (updateExpression == null || updateExpression.isEmpty()) {
+ return true;
+ }
+ updateExpressionBsonDoc = RawBsonDocument.parse(updateExpression);
+ } else {
+ updateExpressionBsonDoc = (RawBsonDocument) PBson.INSTANCE.toObject(ptr);
+ if (updateExpressionBsonDoc == null || updateExpressionBsonDoc.isEmpty()) {
+ return true;
+ }
+ }
+
+ UpdateExpressionUtils.updateExpression(updateExpressionBsonDoc,
+ bsonDocument);
+
+ RawBsonDocument updatedDocument =
+ new RawBsonDocument(bsonDocument, new BsonDocumentCodec());
+ ByteBuffer buffer = updatedDocument.getByteBuffer().asNIO();
+
+ ptr.set(buffer.array(), buffer.arrayOffset(), buffer.limit());
+ return true;
+ }
+
+ @Override
+ public PDataType getDataType() {
+ return PBson.INSTANCE;
+ }
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/JsonValueFunction.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/JsonValueFunction.java
index a42b0cfc95..941666841e 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/JsonValueFunction.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/JsonValueFunction.java
@@ -22,6 +22,7 @@ import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode;
import org.apache.phoenix.parse.JsonValueParseNode;
import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PBson;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PJson;
import org.apache.phoenix.schema.types.PVarbinary;
@@ -41,7 +42,8 @@ import java.util.List;
*/
@FunctionParseNode.BuiltInFunction(name = JsonValueFunction.NAME,
nodeClass = JsonValueParseNode.class,
- args = { @FunctionParseNode.Argument(allowedTypes = { PJson.class, PVarbinary.class }),
+ args = { @FunctionParseNode.Argument(allowedTypes = { PJson.class, PBson.class,
+ PVarbinary.class }),
@FunctionParseNode.Argument(allowedTypes = { PVarchar.class }) })
public class JsonValueFunction extends ScalarFunction {
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/CommonComparisonExpressionUtils.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/CommonComparisonExpressionUtils.java
new file mode 100644
index 0000000000..af739b720d
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/CommonComparisonExpressionUtils.java
@@ -0,0 +1,252 @@
+/*
+ * 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.util.bson;
+
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+import org.bson.BsonArray;
+import org.bson.BsonBinary;
+import org.bson.BsonDateTime;
+import org.bson.BsonDocument;
+import org.bson.BsonNumber;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.hbase.util.Bytes;
+
+/**
+ * Common Util functions to help retrieve BSON Document values based on the given field expressions.
+ */
+public class CommonComparisonExpressionUtils {
+
+ private static final Logger LOGGER =
+ LoggerFactory.getLogger(CommonComparisonExpressionUtils.class);
+
+ /**
+ * Comparison operators supported for the Document value comparisons.
+ */
+ public enum CompareOp {
+ LESS,
+ LESS_OR_EQUAL,
+ GREATER_OR_EQUAL,
+ GREATER,
+ EQUALS,
+ NOT_EQUALS
+ }
+
+ /**
+ * Retrieve the value associated with the document 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.
+ *
+ * @param documentFieldKey The document field key for which the value is returned.
+ * @param rawBsonDocument The document from which to find the value.
+ * @return If the field key exists in the document, return the corresponding value. Else
+ * return null.
+ */
+ public static BsonValue getFieldFromDocument(final String documentFieldKey,
+ final BsonDocument rawBsonDocument) {
+ if (documentFieldKey.contains(".") || documentFieldKey.contains("[")) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < documentFieldKey.length(); i++) {
+ if (documentFieldKey.charAt(i) == '.') {
+ BsonValue value = rawBsonDocument.get(sb.toString());
+ if (value == null) {
+ return null;
+ }
+ return getNestedFieldVal(value, i, documentFieldKey);
+ } else if (documentFieldKey.charAt(i) == '[') {
+ BsonValue value = rawBsonDocument.get(sb.toString());
+ if (value == null) {
+ return null;
+ }
+ return getNestedFieldVal(value, i, documentFieldKey);
+ } else {
+ sb.append(documentFieldKey.charAt(i));
+ }
+ }
+ } else {
+ return rawBsonDocument.get(documentFieldKey);
+ }
+ return null;
+ }
+
+ /**
+ * Retrieve the value associated with the nested field key within the document.
+ *
+ * @param value Value of the parent data structure (document or array) which is used to search
+ * nested elements from.
+ * @param idx Index used to track which part of the field key has been covered so far.
+ * @param documentFieldKey The document field key for which the value is returned.
+ * @return If the field key exists in the document, return the corresponding value. Else
+ * return null.
+ */
+ public static BsonValue getNestedFieldVal(BsonValue value, int idx,
+ final String documentFieldKey) {
+ if (idx == documentFieldKey.length()) {
+ return value;
+ }
+ int curIdx = idx;
+ if (documentFieldKey.charAt(curIdx) == '.') {
+ BsonDocument nestedDocument =
+ value != null && value.isDocument() ? (BsonDocument) value : null;
+ if (nestedDocument == null) {
+ LOGGER.warn("Incorrect access. Should have found nested map for value: {}", value);
+ return null;
+ }
+ curIdx++;
+ StringBuilder sb = new StringBuilder();
+ for (; curIdx < documentFieldKey.length(); curIdx++) {
+ if (documentFieldKey.charAt(curIdx) == '.' || documentFieldKey.charAt(curIdx) == '[') {
+ BsonValue nestedValue = nestedDocument.get(sb.toString());
+ if (nestedValue == null) {
+ return null;
+ }
+ return getNestedFieldVal(nestedValue, curIdx, documentFieldKey);
+ } else {
+ sb.append(documentFieldKey.charAt(curIdx));
+ }
+ }
+ return nestedDocument.get(sb.toString());
+ } else if (documentFieldKey.charAt(curIdx) == '[') {
+ curIdx++;
+ StringBuilder arrayIdxStr = new StringBuilder();
+ while (documentFieldKey.charAt(curIdx) != ']') {
+ arrayIdxStr.append(documentFieldKey.charAt(curIdx));
+ curIdx++;
+ }
+ curIdx++;
+ int arrayIdx = Integer.parseInt(arrayIdxStr.toString());
+ BsonArray nestedArray = value != null && value.isArray() ? (BsonArray) value : null;
+ if (nestedArray == null) {
+ LOGGER.warn("Incorrect access. Should have found nested list for value: {}", value);
+ return null;
+ }
+ if (arrayIdx >= nestedArray.size()) {
+ LOGGER.warn(
+ "Incorrect access. Nested list size {} is less than attempted index access at {}",
+ nestedArray.size(), arrayIdx);
+ return null;
+ }
+ BsonValue valueAtIdx = nestedArray.get(arrayIdx);
+ if (curIdx == documentFieldKey.length()) {
+ return valueAtIdx;
+ }
+ return getNestedFieldVal(valueAtIdx, curIdx, documentFieldKey);
+ }
+ LOGGER.warn("This is erroneous case. getNestedFieldVal should not be used for "
+ + "top level document fields");
+ return null;
+ }
+
+ /**
+ * Compare the given Bson values. All values of the CompareOp enum are supported as
+ * comparison operators. For the comparison to be successful, both the value and the
+ * data type of the LHS and RHS operands must be considered.
+ *
+ * @param lhsOperand LHS operand to be compared with RHS operand.
+ * @param rhsOperand RHS operand.
+ * @param operator Comparison operator used to compare LHS and RHS operands.
+ * @return True if the comparison of LHS with RHS is successful.
+ */
+ public static boolean compareValues(final BsonValue lhsOperand, final BsonValue rhsOperand,
+ final CompareOp operator) {
+ Preconditions.checkNotNull(operator, "Comparison operator should not be null");
+ Preconditions.checkNotNull(lhsOperand,
+ "LHS operand for the Comparison operation should not be null");
+
+ if (operator == CompareOp.EQUALS) {
+ return lhsOperand.equals(rhsOperand);
+ } else if (operator == CompareOp.NOT_EQUALS) {
+ return !lhsOperand.equals(rhsOperand);
+ }
+
+ Preconditions.checkNotNull(rhsOperand,
+ "RHS operand for the Comparison operation should not be null");
+
+ if (lhsOperand.isString() && rhsOperand.isString()) {
+ int compare =
+ ((BsonString) lhsOperand).getValue().compareTo(((BsonString) rhsOperand).getValue());
+ switch (operator) {
+ case LESS:
+ return compare < 0;
+ case LESS_OR_EQUAL:
+ return compare <= 0;
+ case GREATER:
+ return compare > 0;
+ case GREATER_OR_EQUAL:
+ return compare >= 0;
+ }
+ }
+ if ((lhsOperand.isNumber() || lhsOperand.isDecimal128()) && (rhsOperand.isNumber()
+ || rhsOperand.isDecimal128())) {
+ switch (operator) {
+ case LESS:
+ return ((BsonNumber) lhsOperand).doubleValue() < ((BsonNumber) rhsOperand)
+ .doubleValue();
+ case LESS_OR_EQUAL:
+ return ((BsonNumber) lhsOperand).doubleValue() <= ((BsonNumber) rhsOperand)
+ .doubleValue();
+ case GREATER:
+ return ((BsonNumber) lhsOperand).doubleValue() > ((BsonNumber) rhsOperand)
+ .doubleValue();
+ case GREATER_OR_EQUAL:
+ return ((BsonNumber) lhsOperand).doubleValue() >= ((BsonNumber) rhsOperand)
+ .doubleValue();
+ }
+ }
+ if (lhsOperand.isBinary() && rhsOperand.isBinary()
+ && ((BsonBinary) lhsOperand).getType() == ((BsonBinary) rhsOperand).getType()) {
+ byte[] b1 = ((BsonBinary) lhsOperand).getData();
+ byte[] b2 = ((BsonBinary) rhsOperand).getData();
+ switch (operator) {
+ case LESS:
+ return Bytes.compareTo(b1, b2) < 0;
+ case LESS_OR_EQUAL:
+ return Bytes.compareTo(b1, b2) <= 0;
+ case GREATER:
+ return Bytes.compareTo(b1, b2) > 0;
+ case GREATER_OR_EQUAL:
+ return Bytes.compareTo(b1, b2) >= 0;
+ }
+ }
+ if (lhsOperand.isDateTime() && rhsOperand.isDateTime()) {
+ switch (operator) {
+ case LESS:
+ return ((BsonDateTime) lhsOperand).getValue() < ((BsonDateTime) rhsOperand)
+ .getValue();
+ case LESS_OR_EQUAL:
+ return ((BsonDateTime) lhsOperand).getValue() <= ((BsonDateTime) rhsOperand)
+ .getValue();
+ case GREATER:
+ return ((BsonDateTime) lhsOperand).getValue() > ((BsonDateTime) rhsOperand)
+ .getValue();
+ case GREATER_OR_EQUAL:
+ return ((BsonDateTime) lhsOperand).getValue() >= ((BsonDateTime) rhsOperand)
+ .getValue();
+ }
+ }
+ LOGGER.error("Expected comparison for {} is not of type String, Number, Binary"
+ + " or DateTime. LhsOperand: {} , RhsOperand: {}", operator, lhsOperand,
+ rhsOperand);
+ return false;
+ }
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/DocumentComparisonExpressionUtils.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/DocumentComparisonExpressionUtils.java
new file mode 100644
index 0000000000..08c5e9d66a
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/DocumentComparisonExpressionUtils.java
@@ -0,0 +1,351 @@
+/*
+ * 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.util.bson;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.bson.BsonArray;
+import org.bson.BsonBoolean;
+import org.bson.BsonDocument;
+import org.bson.BsonValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+
+/**
+ * Document style condition expression evaluation support.
+ */
+public class DocumentComparisonExpressionUtils {
+
+ private static final Logger LOGGER =
+ LoggerFactory.getLogger(DocumentComparisonExpressionUtils.class);
+
+ private static final String EXISTS_OP = "$exists";
+ private static final String EQUALS_OP = "$eq";
+ private static final String NOT_EQUALS_OP = "$ne";
+ private static final String LESS_THAN_OP = "$lt";
+ private static final String LESS_THAN_OR_EQUALS_OP = "$lte";
+ private static final String GREATER_THAN_OP = "$gt";
+ private static final String GREATER_THAN_OR_EQUALS_OP = "$gte";
+
+ public static boolean evaluateConditionExpression(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ if (document == null || conditionExpression == null) {
+ LOGGER.warn(
+ "Document and/or Condition Expression document are empty. Document: {}, "
+ + "conditionExpression: {}", document, conditionExpression);
+ return false;
+ }
+ return evaluateExpression(document, conditionExpression);
+ }
+
+ private static boolean evaluateExpression(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ final String firstFieldKey = conditionExpression.getFirstKey();
+ Preconditions.checkArgument(conditionExpression.size() == 1,
+ "Expected num of document entries is 1");
+ if (!firstFieldKey.startsWith("$")) {
+ BsonValue bsonValue = conditionExpression.get(firstFieldKey);
+ Preconditions.checkArgument(bsonValue instanceof BsonDocument,
+ "Expected type for Bson value is Document for field based condition operation");
+ BsonDocument bsonDocument = (BsonDocument) bsonValue;
+ if (bsonDocument.containsKey(EXISTS_OP)) {
+ return isExists(document, conditionExpression);
+ } else if (bsonDocument.containsKey(EQUALS_OP)) {
+ return equals(document, conditionExpression);
+ } else if (bsonDocument.containsKey(NOT_EQUALS_OP)) {
+ return notEquals(document, conditionExpression);
+ } else if (bsonDocument.containsKey(LESS_THAN_OP)) {
+ return lessThan(document, conditionExpression);
+ } else if (bsonDocument.containsKey(LESS_THAN_OR_EQUALS_OP)) {
+ return lessThanOrEquals(document, conditionExpression);
+ } else if (bsonDocument.containsKey(GREATER_THAN_OP)) {
+ return greaterThan(document, conditionExpression);
+ } else if (bsonDocument.containsKey(GREATER_THAN_OR_EQUALS_OP)) {
+ return greaterThanOrEquals(document, conditionExpression);
+ } else {
+ throw new IllegalArgumentException("Operator " + firstFieldKey + " is not supported");
+ }
+ } else {
+ switch (firstFieldKey) {
+ case "$or": {
+ BsonValue bsonValue = conditionExpression.get(firstFieldKey);
+ Preconditions.checkArgument(bsonValue instanceof BsonArray,
+ "Expected type for Bson value is Array for $or operator");
+ BsonArray bsonArray = (BsonArray) bsonValue;
+ List<BsonValue> bsonValues = bsonArray.getValues();
+ for (BsonValue value : bsonValues) {
+ if (evaluateExpression(document, (BsonDocument) value)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ case "$and": {
+ BsonValue bsonValue = conditionExpression.get(firstFieldKey);
+ Preconditions.checkArgument(bsonValue instanceof BsonArray,
+ "Expected type for Bson value is Array for $and operator");
+ BsonArray bsonArray = (BsonArray) bsonValue;
+ List<BsonValue> bsonValues = bsonArray.getValues();
+ for (BsonValue value : bsonValues) {
+ if (!evaluateExpression(document, (BsonDocument) value)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ default: {
+ throw new IllegalArgumentException(firstFieldKey + " is not a known operator");
+ }
+ }
+ }
+ }
+
+ private static boolean isExists(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ Set<Map.Entry<String, BsonValue>> entrySet = conditionExpression.entrySet();
+ Preconditions.checkArgument(entrySet.size() == 1, "Expected entry for the exists operation"
+ + " is 1");
+ for (Map.Entry<String, BsonValue> bsonValueEntry : entrySet) {
+ String fieldKey = bsonValueEntry.getKey();
+ BsonValue bsonValue = bsonValueEntry.getValue();
+ Preconditions.checkArgument(bsonValue instanceof BsonDocument,
+ "Expected type for Bson value is Document for exists operation");
+ BsonDocument bsonDocument = (BsonDocument) bsonValue;
+ BsonValue existsValue = bsonDocument.get(EXISTS_OP);
+ Preconditions.checkArgument(existsValue instanceof BsonBoolean,
+ "Expected type for $exists value is boolean");
+ BsonBoolean existsValBoolean = (BsonBoolean) existsValue;
+ if (existsValBoolean.getValue()) {
+ return exists(fieldKey, document);
+ } else {
+ return !exists(fieldKey, document);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Get the document style comparison operator based on the Comparison operator enum value
+ * used to differentiate all comparisons.
+ *
+ * @param compareOp The comparison operator enum.
+ * @return The document style comparison operator constant value.
+ */
+ private static String getCompareOperator(
+ final CommonComparisonExpressionUtils.CompareOp compareOp) {
+ if (compareOp == null) {
+ return null;
+ }
+ switch (compareOp) {
+ case LESS: {
+ return LESS_THAN_OP;
+ }
+ case LESS_OR_EQUAL: {
+ return LESS_THAN_OR_EQUALS_OP;
+ }
+ case GREATER: {
+ return GREATER_THAN_OP;
+ }
+ case GREATER_OR_EQUAL: {
+ return GREATER_THAN_OR_EQUALS_OP;
+ }
+ case EQUALS: {
+ return EQUALS_OP;
+ }
+ case NOT_EQUALS: {
+ return NOT_EQUALS_OP;
+ }
+ default: {
+ return null;
+ }
+ }
+ }
+
+ /**
+ * Common utility for performing comparison with document style comparison expression.
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @param compareOp The comparison operator.
+ * @return True if the field provided in the condition expression has value comparable to
+ * the value provided in the condition expression as per the given comparison operator.
+ */
+ private static boolean compare(final BsonDocument document,
+ final BsonDocument conditionExpression,
+ final CommonComparisonExpressionUtils.CompareOp compareOp) {
+ Set<Map.Entry<String, BsonValue>> entrySet = conditionExpression.entrySet();
+ Preconditions.checkArgument(entrySet.size() == 1,
+ "Expected entry for the " + compareOp + " operation is 1");
+ for (Map.Entry<String, BsonValue> bsonValueEntry : entrySet) {
+ String fieldKey = bsonValueEntry.getKey();
+ BsonValue bsonValue = bsonValueEntry.getValue();
+ Preconditions.checkArgument(bsonValue instanceof BsonDocument,
+ "Expected type for Bson value is Document for " + compareOp + " operation");
+ BsonDocument bsonDocument = (BsonDocument) bsonValue;
+ BsonValue compareValue = bsonDocument.get(getCompareOperator(compareOp));
+ BsonValue topLevelValue = document.get(fieldKey);
+ BsonValue actualValue = topLevelValue != null ?
+ topLevelValue :
+ CommonComparisonExpressionUtils.getFieldFromDocument(fieldKey, document);
+ return actualValue != null && CommonComparisonExpressionUtils.compareValues(actualValue,
+ compareValue, compareOp);
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if the field provided in the condition expression has value greater than
+ * or equals to the value provided in the condition expression.
+ * Condition Expression format:
+ * {
+ * <field>: {
+ * "$gte": <value>
+ * }
+ * }
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @return True if the field provided in the condition expression has value greater than
+ * or equals to the value provided in the condition expression.
+ */
+ private static boolean greaterThanOrEquals(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ return compare(document, conditionExpression,
+ CommonComparisonExpressionUtils.CompareOp.GREATER_OR_EQUAL);
+ }
+
+ /**
+ * Returns true if the field provided in the condition expression has value greater than
+ * the value provided in the condition expression.
+ * Condition Expression format:
+ * {
+ * <field>: {
+ * "$gt": <value>
+ * }
+ * }
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @return True if the field provided in the condition expression has value greater than
+ * the value provided in the condition expression.
+ */
+ private static boolean greaterThan(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ return compare(document, conditionExpression,
+ CommonComparisonExpressionUtils.CompareOp.GREATER);
+ }
+
+ /**
+ * Returns true if the field provided in the condition expression has value less than
+ * or equals to the value provided in the condition expression.
+ * Condition Expression format:
+ * {
+ * <field>: {
+ * "$lte": <value>
+ * }
+ * }
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @return True if the field provided in the condition expression has value less than
+ * or equals to the value provided in the condition expression.
+ */
+ private static boolean lessThanOrEquals(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ return compare(document, conditionExpression,
+ CommonComparisonExpressionUtils.CompareOp.LESS_OR_EQUAL);
+ }
+
+ /**
+ * Returns true if the field provided in the condition expression has value less than
+ * the value provided in the condition expression.
+ * Condition Expression format:
+ * {
+ * <field>: {
+ * "$lt": <value>
+ * }
+ * }
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @return True if the field provided in the condition expression has value less than
+ * the value provided in the condition expression.
+ */
+ private static boolean lessThan(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ return compare(document, conditionExpression,
+ CommonComparisonExpressionUtils.CompareOp.LESS);
+ }
+
+ /**
+ * Returns true if the field provided in the condition expression has value equal to the value
+ * provided in the condition expression.
+ * Condition Expression format:
+ * {
+ * <field>: {
+ * "$eq": <value>
+ * }
+ * }
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @return if the field provided in the condition expression has value equal to the value
+ * provided in the condition expression.
+ */
+ private static boolean equals(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ return compare(document, conditionExpression,
+ CommonComparisonExpressionUtils.CompareOp.EQUALS);
+ }
+
+ /**
+ * Returns true if the field provided in the condition expression has value not equal to the
+ * value provided in the condition expression.
+ * Condition Expression format:
+ * {
+ * <field>: {
+ * "$ne": <value>
+ * }
+ * }
+ *
+ * @param document The document used for comparison.
+ * @param conditionExpression Condition Expression Document.
+ * @return if the field provided in the condition expression has value not equal to the value
+ * provided in the condition expression.
+ */
+ private static boolean notEquals(final BsonDocument document,
+ final BsonDocument conditionExpression) {
+ return compare(document, conditionExpression,
+ CommonComparisonExpressionUtils.CompareOp.NOT_EQUALS);
+ }
+
+ private static boolean exists(final String documentField, final BsonDocument bsonDocument) {
+ BsonValue topLevelValue = bsonDocument.get(documentField);
+ if (topLevelValue != null) {
+ return true;
+ }
+ return CommonComparisonExpressionUtils.getFieldFromDocument(documentField, bsonDocument)
+ != null;
+ }
+
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/SQLComparisonExpressionUtils.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/SQLComparisonExpressionUtils.java
new file mode 100644
index 0000000000..96d2bb534c
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/SQLComparisonExpressionUtils.java
@@ -0,0 +1,335 @@
+/*
+ * 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.util.bson;
+
+import org.bson.BsonDocument;
+import org.bson.BsonValue;
+import org.bson.RawBsonDocument;
+import org.mvel2.MVEL;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * SQL style condition expression evaluation support.
+ */
+public class SQLComparisonExpressionUtils {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SQLComparisonExpressionUtils.class);
+
+ /**
+ * All supported operators. Used to parse the input string and identify how many of the
+ * operators are in use and accordingly performs string conversions using individual
+ * pattern-matcher-replace.
+ */
+ private static final String ALL_SUPPORTED_OPS =
+ "\\b(field_not_exists|field_exists|BETWEEN|IN|AND|OR|NOT)\\b|<=|>=|!=|==|=|<>|<|>";
+ private static final Pattern ALL_SUPPORTED_OPS_PATTERN = Pattern.compile(ALL_SUPPORTED_OPS);
+
+ private static final String FIELD_NOT_EXISTS = "field_not_exists\\(([^)]+)\\)";
+ private static final String FIELD_EXISTS = "field_exists\\(([^)]+)\\)";
+ private static final String EQUALS1 = "\\b([\\w.\\[\\]]+)\\s*=\\s*([#:$]*\\w+)";
+ private static final String EQUALS2 = "\\b([\\w.\\[\\]]+)\\s*==\\s*([#:$]*\\w+)";
+ private static final String NOT_EQUALS1 = "\\b([\\w.\\[\\]]+)\\s*!=\\s*([#:$]*\\w+)";
+ private static final String NOT_EQUALS2 = "\\b([\\w.\\[\\]]+)\\s*<>\\s*([#:$]*\\w+)";
+ private static final String LESS_THAN = "\\b([\\w.\\[\\]]+)\\s*<\\s*([#:$]*\\w+)";
+ private static final String LESS_THAN_OR_EQUALS = "\\b([\\w.\\[\\]]+)\\s*<=\\s*([#:$]*\\w+)";
+ private static final String GREATER_THAN = "\\b([\\w.\\[\\]]+)\\s*>\\s*([#:$]*\\w+)";
+ private static final String GREATER_THAN_OR_EQUALS = "\\b([\\w.\\[\\]]+)\\s*>=\\s*([#:$]*\\w+)";
+ private static final String BETWEEN =
+ "\\b([\\w.\\[\\]]+)\\s+BETWEEN\\s+([#:$]*\\w+)\\s+AND\\s+([#:$]*\\w+)";
+ private static final String IN = "\\b([\\w.\\[\\]]+)\\s+IN\\s+\\(([^)]+)\\)";
+ private static final String AND = "\\bAND\\b";
+ private static final String OR = "\\bOR\\b";
+ private static final String NOT = "\\bNOT\\b\\s*";
+
+ private static final Pattern FIELD_NOT_EXISTS_PATTERN = Pattern.compile(FIELD_NOT_EXISTS);
+ private static final Pattern FIELD_EXISTS_PATTERN = Pattern.compile(FIELD_EXISTS);
+ private static final Pattern EQUALS1_PATTERN = Pattern.compile(EQUALS1);
+ private static final Pattern EQUALS2_PATTERN = Pattern.compile(EQUALS2);
+ private static final Pattern NOT_EQUALS1_PATTERN = Pattern.compile(NOT_EQUALS1);
+ private static final Pattern NOT_EQUALS2_PATTERN = Pattern.compile(NOT_EQUALS2);
+ private static final Pattern LESS_THAN_PATTERN = Pattern.compile(LESS_THAN);
+ private static final Pattern LESS_THAN_OR_EQUALS_PATTERN = Pattern.compile(LESS_THAN_OR_EQUALS);
+ private static final Pattern GREATER_THAN_PATTERN = Pattern.compile(GREATER_THAN);
+ private static final Pattern GREATER_THAN_OR_EQUALS_PATTERN =
+ Pattern.compile(GREATER_THAN_OR_EQUALS);
+ private static final Pattern BETWEEN_PATTERN = Pattern.compile(BETWEEN);
+ private static final Pattern IN_PATTERN = Pattern.compile(IN);
+ private static final Pattern AND_PATTERN = Pattern.compile(AND);
+ private static final Pattern OR_PATTERN = Pattern.compile(OR);
+ private static final Pattern NOT_PATTERN = Pattern.compile(NOT);
+
+ private static final String FUNC_FIELD_NOT_EXISTS = "!exists('$1')";
+ private static final String FUNC_FIELD_EXISTS = "exists('$1')";
+ private static final String FUNC_EQUALS = "isEquals('$1', '$2')";
+ private static final String FUNC_NOT_EQUALS = "!isEquals('$1', '$2')";
+ private static final String FUNC_LESS_THAN = "lessThan('$1', '$2')";
+ private static final String FUNC_LESS_THAN_OR_EQUALS = "lessThanOrEquals('$1', '$2')";
+ private static final String FUNC_GREATER_THAN = "greaterThan('$1', '$2')";
+ private static final String FUNC_GREATER_THAN_OR_EQUALS = "greaterThanOrEquals('$1', '$2')";
+ private static final String FUNC_BETWEEN = "between('$1', '$2', '$3')";
+ private static final String FUNC_IN = "in('$1', '$2')";
+ private static final String OP_AND = "&&";
+ private static final String OP_OR = "||";
+ private static final String OP_NOT = "!";
+
+ private final RawBsonDocument rawBsonDocument;
+ private final BsonDocument comparisonValuesDocument;
+
+ public SQLComparisonExpressionUtils(RawBsonDocument rawBsonDocument,
+ BsonDocument comparisonValuesDocument) {
+ this.rawBsonDocument = rawBsonDocument;
+ this.comparisonValuesDocument = comparisonValuesDocument;
+ }
+
+ public boolean evaluateConditionExpression(final String conditionExpression) {
+ String expression = convertExpression(conditionExpression);
+ LOGGER.trace("Evaluating Expression: {}", expression);
+ try {
+ Object result = MVEL.eval(expression, this);
+ return result instanceof Boolean && (Boolean) result;
+ } catch (Exception e) {
+ LOGGER.error("Error while evaluating expression: {}", expression, e);
+ throw new RuntimeException("Expression could not be evaluated: " + expression);
+ }
+ }
+
+ /**
+ * Converts the input string expression into Java executable statement.
+ *
+ * @param expression Input string expression.
+ * @return Executable string conversion statement.
+ */
+ public String convertExpression(String expression) {
+ Set<String> patternsMatched = new HashSet<>();
+ Matcher matcher = ALL_SUPPORTED_OPS_PATTERN.matcher(expression);
+ while (matcher.find()) {
+ patternsMatched.add(matcher.group());
+ }
+
+ if (patternsMatched.contains("field_not_exists")) {
+ expression = FIELD_NOT_EXISTS_PATTERN.matcher(expression).replaceAll(FUNC_FIELD_NOT_EXISTS);
+ }
+ if (patternsMatched.contains("field_exists")) {
+ expression = FIELD_EXISTS_PATTERN.matcher(expression).replaceAll(FUNC_FIELD_EXISTS);
+ }
+ if (patternsMatched.contains("=")) {
+ expression = EQUALS1_PATTERN.matcher(expression).replaceAll(FUNC_EQUALS);
+ }
+ if (patternsMatched.contains("==")) {
+ expression = EQUALS2_PATTERN.matcher(expression).replaceAll(FUNC_EQUALS);
+ }
+ if (patternsMatched.contains("!=")) {
+ expression = NOT_EQUALS1_PATTERN.matcher(expression).replaceAll(FUNC_NOT_EQUALS);
+ }
+ if (patternsMatched.contains("<>")) {
+ expression = NOT_EQUALS2_PATTERN.matcher(expression).replaceAll(FUNC_NOT_EQUALS);
+ }
+ if (patternsMatched.contains("<")) {
+ expression = LESS_THAN_PATTERN.matcher(expression).replaceAll(FUNC_LESS_THAN);
+ }
+ if (patternsMatched.contains("<=")) {
+ expression =
+ LESS_THAN_OR_EQUALS_PATTERN.matcher(expression).replaceAll(FUNC_LESS_THAN_OR_EQUALS);
+ }
+ if (patternsMatched.contains(">")) {
+ expression = GREATER_THAN_PATTERN.matcher(expression).replaceAll(FUNC_GREATER_THAN);
+ }
+ if (patternsMatched.contains(">=")) {
+ expression = GREATER_THAN_OR_EQUALS_PATTERN.matcher(expression)
+ .replaceAll(FUNC_GREATER_THAN_OR_EQUALS);
+ }
+ if (patternsMatched.contains("BETWEEN")) {
+ expression = BETWEEN_PATTERN.matcher(expression).replaceAll(FUNC_BETWEEN);
+ }
+ if (patternsMatched.contains("IN")) {
+ expression = IN_PATTERN.matcher(expression).replaceAll(FUNC_IN);
+ }
+ if (patternsMatched.contains("AND")) {
+ expression = AND_PATTERN.matcher(expression).replaceAll(OP_AND);
+ }
+ if (patternsMatched.contains("OR")) {
+ expression = OR_PATTERN.matcher(expression).replaceAll(OP_OR);
+ }
+ if (patternsMatched.contains("NOT")) {
+ expression = NOT_PATTERN.matcher(expression).replaceAll(OP_NOT);
+ }
+ return expression;
+ }
+
+ /**
+ * Returns true if the value of the field is comparable to the value represented by
+ * {@code expectedFieldValue} as per the comparison operator represented by {@code compareOp}.
+ * The comparison can happen only if the data type of both values match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedFieldValue.
+ * @param expectedFieldValue The literal value to compare against the field value.
+ * @param compareOp The comparison operator.
+ * @return True if the comparison is successful, False otherwise.
+ */
+ private boolean compare(final String fieldKey,
+ final String expectedFieldValue,
+ final CommonComparisonExpressionUtils.CompareOp compareOp) {
+ BsonValue topLevelValue = rawBsonDocument.get(fieldKey);
+ BsonValue value = topLevelValue != null ?
+ topLevelValue :
+ CommonComparisonExpressionUtils.getFieldFromDocument(fieldKey, rawBsonDocument);
+ if (value != null) {
+ BsonValue compareValue = comparisonValuesDocument.get(expectedFieldValue);
+ return CommonComparisonExpressionUtils.compareValues(
+ value, compareValue, compareOp);
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if the given field exists in the document.
+ *
+ * @param documentField The document field.
+ * @return True if the given field exists in the document.
+ */
+ public boolean exists(final String documentField) {
+ BsonValue topLevelValue = rawBsonDocument.get(documentField);
+ if (topLevelValue != null) {
+ return true;
+ }
+ return CommonComparisonExpressionUtils.getFieldFromDocument(documentField, rawBsonDocument)
+ != null;
+ }
+
+ /**
+ * Returns true if the value of the field is less than the value represented by {@code
+ * expectedFieldValue}. The comparison can happen only if the data type of both values match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedFieldValue.
+ * @param expectedFieldValue The literal value to compare against the field value.
+ * @return True if the value of the field is less than expectedFieldValue.
+ */
+ public boolean lessThan(final String fieldKey, final String expectedFieldValue) {
+ return compare(fieldKey, expectedFieldValue,
+ CommonComparisonExpressionUtils.CompareOp.LESS);
+ }
+
+ /**
+ * Returns true if the value of the field is less than or equal to the value represented by
+ * {@code expectedFieldValue}. The comparison can happen only if the data type of both values
+ * match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedFieldValue.
+ * @param expectedFieldValue The literal value to compare against the field value.
+ * @return True if the value of the field is less than or equal to expectedFieldValue.
+ */
+ public boolean lessThanOrEquals(final String fieldKey, final String expectedFieldValue) {
+ return compare(fieldKey, expectedFieldValue,
+ CommonComparisonExpressionUtils.CompareOp.LESS_OR_EQUAL);
+ }
+
+ /**
+ * Returns true if the value of the field is greater than the value represented by {@code
+ * expectedFieldValue}. The comparison can happen only if the data type of both values match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedFieldValue.
+ * @param expectedFieldValue The literal value to compare against the field value.
+ * @return True if the value of the field is greater than expectedFieldValue.
+ */
+ public boolean greaterThan(final String fieldKey, final String expectedFieldValue) {
+ return compare(fieldKey, expectedFieldValue,
+ CommonComparisonExpressionUtils.CompareOp.GREATER);
+ }
+
+ /**
+ * Returns true if the value of the field is greater than or equal to the value represented by
+ * {@code expectedFieldValue}. The comparison can happen only if the data type of both values
+ * match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedFieldValue.
+ * @param expectedFieldValue The literal value to compare against the field value.
+ * @return True if the value of the field is greater than or equal to expectedFieldValue.
+ */
+ public boolean greaterThanOrEquals(final String fieldKey,
+ final String expectedFieldValue) {
+ return compare(fieldKey, expectedFieldValue,
+ CommonComparisonExpressionUtils.CompareOp.GREATER_OR_EQUAL);
+ }
+
+ /**
+ * Returns true if the value of the field is greater than or equal to the value represented by
+ * {@code expectedFieldValue1} and less than or equal to the value represented by
+ * {@code expectedFieldValue2}. The comparison can happen only if the data type of both values
+ * match.
+ *
+ * @param fieldKey The field key for which value is compared against two values.
+ * @param expectedFieldValue1 The first literal value to compare against the field value.
+ * @param expectedFieldValue2 The second literal value to compare against the field value.
+ * @return True if the value of the field is greater than or equal to the value represented by
+ * expectedFieldValue1 and less than or equal to the value represented by expectedFieldValue2.
+ */
+ public boolean between(final String fieldKey, final String expectedFieldValue1,
+ final String expectedFieldValue2) {
+ return greaterThanOrEquals(fieldKey, expectedFieldValue1) && lessThanOrEquals(
+ fieldKey, expectedFieldValue2);
+ }
+
+ /**
+ * Returns true if the value of the field equals to any of the comma separated values
+ * represented by {@code expectedInValues}. The equality check is successful only if the value
+ * and the data type both match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedInValues.
+ * @param expectedInValues The array of values for comparison, separated by comma.
+ * @return True if the value of the field equals to any of the comma separated values
+ * represented by expectedInValues. The equality check is successful only if the value
+ * and the data type both match.
+ */
+ public boolean in(final String fieldKey, final String expectedInValues) {
+ String[] expectedInVals = expectedInValues.split("\\s*,\\s*");
+ BsonValue topLevelValue = rawBsonDocument.get(fieldKey);
+ BsonValue value = topLevelValue != null ?
+ topLevelValue :
+ CommonComparisonExpressionUtils.getFieldFromDocument(fieldKey, rawBsonDocument);
+ if (value != null) {
+ for (String expectedInVal : expectedInVals) {
+ if (isEquals(fieldKey, expectedInVal)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if the value of the field is equal to the value represented by {@code
+ * expectedFieldValue}. The equality check is successful only if the value
+ * and the data type both match.
+ *
+ * @param fieldKey The field key for which value is compared against expectedFieldValue.
+ * @param expectedFieldValue The literal value to compare against the field value.
+ * @return True if the value of the field is equal to expectedFieldValue.
+ */
+ public boolean isEquals(final String fieldKey, final String expectedFieldValue) {
+ return compare(fieldKey, expectedFieldValue,
+ CommonComparisonExpressionUtils.CompareOp.EQUALS);
+ }
+
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/UpdateExpressionUtils.java b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/UpdateExpressionUtils.java
new file mode 100644
index 0000000000..46fb9bd268
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/expression/util/bson/UpdateExpressionUtils.java
@@ -0,0 +1,800 @@
+/*
+ * 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.util.bson;
+
+import java.math.BigDecimal;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.bson.BsonArray;
+import org.bson.BsonDecimal128;
+import org.bson.BsonDocument;
+import org.bson.BsonDouble;
+import org.bson.BsonInt32;
+import org.bson.BsonInt64;
+import org.bson.BsonNumber;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.bson.types.Decimal128;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * BSON Update Expression Utility to perform the Document updates. All update expressions
+ * provided by this utility supports operations on nested document fields. 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.
+ */
+public class UpdateExpressionUtils {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(UpdateExpressionUtils.class);
+
+ /**
+ * Update operator enum values. Any new update operator that requires update to deeply nested
+ * structures (nested documents or nested arrays), need to have its own type added here.
+ */
+ private enum UpdateOp {
+ SET,
+ UNSET,
+ ADD,
+ DELETE_FROM_SET
+ }
+
+ /**
+ * Updates the given document based on the update expression.
+ * <p/>
+ * {
+ * "$SET": { <field1>: <value1>, <field2>: <value2>, .... },
+ * "$UNSET": { <field1>: null, <field2>: null, ... },
+ * "$ADD": { <field1>: <value1>, <field2>: <value2>, .... },
+ * "$DELETE_FROM_SET": { <field1>: <value1>, <field2>: <value2>, .... }
+ * }
+ * <p/>
+ * "$SET": Use the SET action in an update expression to add one or more fields to a BSON
+ * Document. If any of these fields already exists, they are overwritten by the new values.
+ * To perform multiple SET actions, provide multiple fields key-value entries within the nested
+ * document under $SET field key.
+ * "$UNSET": Use the UNSET action in an update expression to unset or remove one or more fields
+ * from a BSON Document. To perform multiple UNSET actions, provide multiple field key-value
+ * entries within the nested document under $UNSET field key.
+ * "$ADD": Use the ADD action in an update expression to add a new field and its values to a
+ * BSON document. If the field already exists, the behavior of ADD depends on the field's
+ * data type:
+ * <p/>
+ * 1. If the field is a number, and the value you are adding is also a number, the value is
+ * mathematically added to the existing field.
+ * <p/>
+ * 2. If the field is a set, and the value you are adding is also a set, the value is appended
+ * to the existing set.
+ * <p/>
+ * "$DELETE_FROM_SET": Use the DELETE action in an update expression to remove one or more
+ * elements from a set. To perform multiple DELETE actions, provide multiple field key-value
+ * entries within the nested document under $DELETE_FROM_SET field key.
+ * Definition of path and subset in the context of the expression:
+ * <p/>
+ * 1. The path element is the document path to a field. The field must be a set data type.
+ * 2. The subset is one or more elements that you want to delete from the given path. Subset
+ * must be of set type.
+ * <p/>
+ *
+ * @param updateExpression Update Expression as a document.
+ * @param bsonDocument Document contents to be updated.
+ */
+ public static void updateExpression(final BsonDocument updateExpression,
+ final BsonDocument bsonDocument) {
+
+ LOGGER.info("Update Expression: {} , current bsonDocument: {}", updateExpression, bsonDocument);
+
+ if (updateExpression.containsKey("$SET")) {
+ executeSetExpression((BsonDocument) updateExpression.get("$SET"), bsonDocument);
+ }
+
+ if (updateExpression.containsKey("$UNSET")) {
+ executeRemoveExpression((BsonDocument) updateExpression.get("$UNSET"), bsonDocument);
+ }
+
+ if (updateExpression.containsKey("$ADD")) {
+ executeAddExpression((BsonDocument) updateExpression.get("$ADD"), bsonDocument);
+ }
+
+ if (updateExpression.containsKey("$DELETE_FROM_SET")) {
+ executeDeleteExpression((BsonDocument) updateExpression.get("$DELETE_FROM_SET"),
+ bsonDocument);
+ }
+ }
+
+ /**
+ * Update the given document by performing DELETE operation. This operation is applicable
+ * only on Set data structure. The document is updated by removing the given set of elements from
+ * the given set of elements.
+ * Let's say if the document field is of string set data type, and the elements are:
+ * {"yellow", "green", "red", "blue"}. The elements to be removed from the set are provided
+ * as {"blue", "yellow"} with the delete expression. The operation is expected to update the
+ * existing field by removing "blue" and "yellow" from the given set and the resultant set is
+ * expected to contain: {"green", "red"}.
+ *
+ * @param deleteExpr Delete Expression Document with key-value pairs. Key represents field in the
+ * given document, on which operation is to be performed. Value represents set of elements to be
+ * removed from the existing set.
+ * @param bsonDocument Document contents to be updated.
+ */
+ private static void executeDeleteExpression(final BsonDocument deleteExpr,
+ final BsonDocument bsonDocument) {
+ for (Map.Entry<String, BsonValue> deleteEntry : deleteExpr.entrySet()) {
+ String fieldKey = deleteEntry.getKey();
+ BsonValue newVal = deleteEntry.getValue();
+ BsonValue topLevelValue = bsonDocument.get(fieldKey);
+ if (!isBsonSet(newVal)) {
+ throw new RuntimeException("Type of new value to be removed should be sets only");
+ }
+ // If the top level field exists, perform the operation here and return.
+ if (topLevelValue != null) {
+ BsonValue value = modifyFieldValueByDeleteFromSet(topLevelValue, newVal);
+ if (value == null) {
+ bsonDocument.remove(fieldKey);
+ } else {
+ bsonDocument.put(fieldKey, value);
+ }
+ } else if (!fieldKey.contains(".") && !fieldKey.contains("[")) {
+ LOGGER.info("Nothing to be removed as field with key {} does not exist", fieldKey);
+ } else {
+ // If the top level field does not exist and the field key contains "." or "[]" notations
+ // for nested document or nested array, use the self-recursive function to go through
+ // the document tree, one level at a time.
+ updateNestedField(bsonDocument, 0, fieldKey, newVal, UpdateOp.DELETE_FROM_SET);
+ }
+ }
+ }
+
+ /**
+ * Update the existing set by removing the set values that are present in
+ * {@code setValuesToDelete}. For this operation to be successful, both {@code currentValue} and
+ * {@code setValuesToDelete} must be of same set data type. For instance, both must be either
+ * set of string, set of numbers or set of binary values.
+ *
+ * @param currentValue The value that needs to be updated by performing deletion operation.
+ * @param setValuesToDelete The set values that need to be deleted from the currentValue set.
+ * @return Updated set after performing the set difference operation.
+ */
+ private static BsonValue modifyFieldValueByDeleteFromSet(final BsonValue currentValue,
+ final BsonValue setValuesToDelete) {
+ if (areBsonSetOfSameType(currentValue, setValuesToDelete)) {
+ Set<BsonValue> set1 =
+ new HashSet<>(((BsonArray) ((BsonDocument) currentValue).get("$set")).getValues());
+ Set<BsonValue> set2 =
+ new HashSet<>(((BsonArray) ((BsonDocument) setValuesToDelete).get("$set")).getValues());
+ set1.removeAll(set2);
+ if (set1.isEmpty()) {
+ return null;
+ }
+ BsonDocument bsonDocument = new BsonDocument();
+ bsonDocument.put("$set", new BsonArray(new ArrayList<>(set1)));
+ return bsonDocument;
+ }
+ throw new RuntimeException(
+ "Data type for current value " + currentValue + " is not matching with new value "
+ + setValuesToDelete);
+ }
+
+ /**
+ * Update the given document by performing ADD operation. This operation is applicable
+ * only on either Set data structure or Numerical value represented by Int32, Int64, Double or
+ * Decimal. If the field is of type set, the document is updated by adding the given set of
+ * elements to the given set of elements. If the field is of type number, the document is updated
+ * by adding the numerical value to the given number field value.
+ * </p>
+ * Let's say if the document field is of numeric data type with value "234.5" and the value
+ * to be added is "10", the resultant value is expected to be "244.5". Adding negative value
+ * would result in subtract operation. For example, adding "-10" would result in "224.5".
+ * </p>
+ * On the other hand, if the document field is of string set data type, and the elements are:
+ * {"yellow", "green", "red"}. The elements to be added to the set are provided
+ * as {"blue", "yellow"} with the add expression. The operation is expected to update the
+ * existing field by removing adding unique value "blue" and the resultant set is
+ * expected to contain: {"yellow", "green", "red", "blue"}.
+ *
+ * @param addExpr Add Expression Document
+ * @param bsonDocument Document contents to be updated.
+ */
+ private static void executeAddExpression(final BsonDocument addExpr,
+ final BsonDocument bsonDocument) {
+ for (Map.Entry<String, BsonValue> addEntry : addExpr.entrySet()) {
+ String fieldKey = addEntry.getKey();
+ BsonValue newVal = addEntry.getValue();
+ BsonValue topLevelValue = bsonDocument.get(fieldKey);
+ if (!newVal.isNumber() && !newVal.isDecimal128() && !isBsonSet(newVal)) {
+ throw new RuntimeException(
+ "Type of new value to be updated should be either number or sets only");
+ }
+ // If the top level field exists, perform the operation here and return.
+ if (topLevelValue != null) {
+ bsonDocument.put(fieldKey, modifyFieldValueByAdd(topLevelValue, newVal));
+ } else if (!fieldKey.contains(".") && !fieldKey.contains("[")) {
+ bsonDocument.put(fieldKey, newVal);
+ } else {
+ // If the top level field does not exist and the field key contains "." or "[]" notations
+ // for nested document or nested array, use the self-recursive function to go through
+ // the document tree, one level at a time.
+ updateNestedField(bsonDocument, 0, fieldKey, newVal, UpdateOp.ADD);
+ }
+ }
+ }
+
+ /**
+ * Update the existing value {@code currentValue} depending on its data type. If the data type of
+ * {@code currentValue} is numeric, add numeric value represented by {@code newVal}
+ * to it. If the data type of {@code currentValue} is set, add set values represented by
+ * {@code newVal} to it. For this operation to be successful, both {@code currentValue} and
+ * {@code newVal} must be of same set data type. For instance, both must be either
+ * set of string, set of numbers or set of binary values, or both must be of number data type.
+ *
+ * @param currentValue The value that needs to be updated by performing add operation.
+ * @param newVal The numeric or set values that need to be added to the currentValue.
+ * @return Updated value after performing the add operation.
+ */
+ private static BsonValue modifyFieldValueByAdd(final BsonValue currentValue,
+ final BsonValue newVal) {
+ if ((currentValue.isNumber() || currentValue.isDecimal128()) && (newVal.isNumber()
+ || newVal.isDecimal128())) {
+ Number num1 = getNumberFromBsonNumber((BsonNumber) currentValue);
+ Number num2 = getNumberFromBsonNumber((BsonNumber) newVal);
+ Number newNum = addNum(num1, num2);
+ return getBsonNumberFromNumber(newNum);
+ } else if (areBsonSetOfSameType(currentValue, newVal)) {
+ Set<BsonValue> set1 =
+ new HashSet<>(((BsonArray) ((BsonDocument) currentValue).get("$set")).getValues());
+ Set<BsonValue> set2 =
+ new HashSet<>(((BsonArray) ((BsonDocument) newVal).get("$set")).getValues());
+ set1.addAll(set2);
+ BsonDocument bsonDocument = new BsonDocument();
+ bsonDocument.put("$set", new BsonArray(new ArrayList<>(set1)));
+ return bsonDocument;
+ }
+ throw new RuntimeException(
+ "Data type for current value " + currentValue + " is not matching with new value "
+ + newVal);
+ }
+
+ /**
+ * Update the given document by performing UNSET operation on one or more fields. This operation
+ * is applicable to any field of the document at any level of the hierarchy. If the field exists,
+ * it will be deleted.
+ *
+ * @param unsetExpr Unset Expression Document.
+ * @param bsonDocument Document contents to be updated.
+ */
+ private static void executeRemoveExpression(final BsonDocument unsetExpr,
+ final BsonDocument bsonDocument) {
+ for (Map.Entry<String, BsonValue> removeField : unsetExpr.entrySet()) {
+ String fieldKey = removeField.getKey();
+ BsonValue topLevelValue = bsonDocument.get(fieldKey);
+ // If the top level field exists, perform the operation here and return.
+ if (topLevelValue != null || (!fieldKey.contains(".") && !fieldKey.contains("["))) {
+ bsonDocument.remove(fieldKey);
+ } else {
+ // If the top level field does not exist and the field key contains "." or "[]" notations
+ // for nested document or nested array, use the self-recursive function to go through
+ // the document tree, one level at a time.
+ updateNestedField(bsonDocument, 0, fieldKey, null, UpdateOp.UNSET);
+ }
+ }
+ }
+
+ /**
+ * Update the given document by performing SET operation on a given field. This operation is
+ * applicable to any field of the document. The SET operation represents either adding a new
+ * field with the given value of updating the existing field with new value provided by the
+ * SET Expression Document.
+ *
+ * @param setExpression SET Expression Document.
+ * @param bsonDocument Document contents to be updated.
+ */
+ private static void executeSetExpression(final BsonDocument setExpression,
+ final BsonDocument bsonDocument) {
+ for (Map.Entry<String, BsonValue> setEntry : setExpression.entrySet()) {
+ String fieldKey = setEntry.getKey();
+ BsonValue fieldVal = setEntry.getValue();
+ BsonValue topLevelValue = bsonDocument.get(fieldKey);
+ BsonValue newVal = getNewFieldValue(fieldVal, bsonDocument);
+ // If the top level field exists, perform the operation here and return.
+ if (topLevelValue != null || (!fieldKey.contains(".") && !fieldKey.contains("["))) {
+ bsonDocument.put(fieldKey, newVal);
+ } else {
+ // If the top level field does not exist and the field key contains "." or "[]" notations
+ // for nested document or nested array, use the self-recursive function to go through
+ // the document tree, one level at a time.
+ updateNestedField(bsonDocument, 0, fieldKey, newVal, UpdateOp.SET);
+ }
+ }
+ }
+
+ /**
+ * Update the nested field with the given update operation. The update operation is determined
+ * by the enum value {@code updateOp}.
+ * The field key is expected to contain "." and/or "[]" notations for nested documents and/or
+ * nested array elements. This function keeps recursively calling itself until it reaches the
+ * leaf node in the given tree.
+ * For instance, for field key "category.subcategories.brands[5]", first the function
+ * evaluates and retrieves the value for top-level field "category". The value of "category"
+ * is expected to be nested document. First function call has value as full document, it retries
+ * nested document under "category" field and calls the function recursively with index value
+ * same as index value of first "." (dot) in the field key. For field key
+ * "category.subcategories.brands[5]", the index value would be 8. The second function call
+ * retrieves value of field key "subcategories", which is expected to be nested document.
+ * The third function call gets this nested document as BsonValue and index value as 22 as the
+ * field key has second "." (dot) notation at index 22. The third function call searches for
+ * field key "brands" and expects its value as nested array. The forth function call gets
+ * this nested array as BsonValue and index 29 as the field key has "[]" array notation starting
+ * at index 29. The forth function call retrieves value of nested array element at index 5.
+ * As the function is at leaf node in the tree, now it performs the update operation as per the
+ * given update operator ($SET / $UNSET / $ADD / $DELETE_FROM_SET) semantics.
+ *
+ * @param value Bson value at the given level of the document hierarchy.
+ * @param idx The index value for the given field key. The function is expected to retrieve
+ * the value of the nested document or array at the given level of the tree.
+ * @param fieldKey The full field key.
+ * @param newVal The new Bson value to be added to the given nested document or a particular
+ * index of the given nested array.
+ * @param updateOp The enum value representing the update operation to perform.
+ */
+ private static void updateNestedField(final BsonValue value,
+ final int idx,
+ final String fieldKey,
+ final BsonValue newVal,
+ final UpdateOp updateOp) {
+ int curIdx = idx;
+ if (fieldKey.charAt(curIdx) == '.') {
+ if (value == null || !value.isDocument()) {
+ LOGGER.error(
+ "Value is null or not document. Value: {}, Idx: {}, fieldKey: {}, New val: {},"
+ + " Update op: {}",
+ value, idx, fieldKey, newVal, updateOp);
+ throw new RuntimeException("Value is null or it is not of type document.");
+ }
+ BsonDocument nestedDocument = (BsonDocument) value;
+ curIdx++;
+ StringBuilder sb = new StringBuilder();
+ for (; curIdx < fieldKey.length(); curIdx++) {
+ if (fieldKey.charAt(curIdx) == '.' || fieldKey.charAt(curIdx) == '[') {
+ BsonValue nestedValue = nestedDocument.get(sb.toString());
+ if (nestedValue == null) {
+ LOGGER.error("Should have found nested map for {}", sb);
+ return;
+ }
+ updateNestedField(nestedValue, curIdx, fieldKey, newVal, updateOp);
+ return;
+ } else {
+ sb.append(fieldKey.charAt(curIdx));
+ }
+ }
+ // reached leaf node of the document tree, update the value as per the update operator
+ // semantics
+ updateDocumentAtLeafNode(newVal, updateOp, nestedDocument, sb);
+ } else if (fieldKey.charAt(curIdx) == '[') {
+ curIdx++;
+ StringBuilder arrayIdxStr = new StringBuilder();
+ while (fieldKey.charAt(curIdx) != ']') {
+ arrayIdxStr.append(fieldKey.charAt(curIdx));
+ curIdx++;
+ }
+ curIdx++;
+ int arrayIdx = Integer.parseInt(arrayIdxStr.toString());
+ if (value == null || !value.isArray()) {
+ LOGGER.error("Value is null or not document. Value: {}, Idx: {}, fieldKey: {}, New val: {}",
+ value, idx, fieldKey, newVal);
+ throw new RuntimeException("Value is null or not array.");
+ }
+ BsonArray nestedArray = (BsonArray) value;
+ if (curIdx == fieldKey.length()) {
+ // reached leaf node of the document tree, update the value as per the update operator
+ // semantics
+ updateArrayAtLeafNode(fieldKey, newVal, updateOp, arrayIdx, nestedArray);
+ return;
+ }
+ BsonValue nestedValue = nestedArray.get(arrayIdx);
+ if (nestedValue == null) {
+ LOGGER.error("Should have found nested list for index {}", arrayIdx);
+ return;
+ }
+ updateNestedField(nestedValue, curIdx, fieldKey, newVal, updateOp);
+ } else {
+ StringBuilder sb = new StringBuilder();
+ for (int i = idx; i < fieldKey.length(); i++) {
+ if (fieldKey.charAt(i) == '.') {
+ BsonValue topFieldValue = ((BsonDocument) value).get(sb.toString());
+ if (topFieldValue == null) {
+ LOGGER.error("Incorrect access. Should have found nested bsonDocument for {}", sb);
+ throw new RuntimeException("Document does not contain key: " + sb);
+ }
+ updateNestedField(topFieldValue, i, fieldKey, newVal, updateOp);
+ return;
+ } else if (fieldKey.charAt(i) == '[') {
+ BsonValue topFieldValue = ((BsonDocument) value).get(sb.toString());
+ if (topFieldValue == null) {
+ LOGGER.error("Incorrect access. Should have found nested list for {}", sb);
+ throw new RuntimeException("Document does not contain key: " + sb);
+ }
+ updateNestedField(topFieldValue, i, fieldKey, newVal, updateOp);
+ return;
+ } else {
+ sb.append(fieldKey.charAt(i));
+ }
+ }
+ }
+ }
+
+ /**
+ * Perform update operation at the leaf node. This method is called only when the leaf
+ * node or the target node for the given field key is encountered while iterating through
+ * the tree structure. This is specifically called when the closest ancestor of the
+ * given node is a nested array. The leaf node is for the field key. For example, for
+ * "category.subcategories", the leaf node is "subcategories" whereas for
+ * "category.subcategories.brands[2]", the leaf node is "brands".
+ *
+ * @param fieldKey The full field key.
+ * @param newVal New value to be used while performing update operation on the existing node.
+ * @param updateOp Type of the update operation to be performed.
+ * @param arrayIdx The index of the array at which the target node value is to be updated.
+ * @param nestedArray The parent or ancestor node, expected to be nested array only.
+ * For example, for "category.subcategories.brands[5]" field key, the nestedArray is expected to
+ * be "brands" array and arrayIdx is expected to be 5.
+ */
+ private static void updateArrayAtLeafNode(final String fieldKey,
+ final BsonValue newVal,
+ final UpdateOp updateOp,
+ final int arrayIdx,
+ final BsonArray nestedArray) {
+ switch (updateOp) {
+ case SET: {
+ if (arrayIdx < nestedArray.size()) {
+ nestedArray.set(arrayIdx, newVal);
+ } else {
+ nestedArray.add(newVal);
+ }
+ break;
+ }
+ case UNSET: {
+ if (arrayIdx < nestedArray.size()) {
+ nestedArray.remove(arrayIdx);
+ }
+ break;
+ }
+ case ADD: {
+ if (arrayIdx < nestedArray.size()) {
+ BsonValue currentValue = nestedArray.get(arrayIdx);
+ if (currentValue != null) {
+ nestedArray.set(arrayIdx, modifyFieldValueByAdd(currentValue, newVal));
+ } else {
+ nestedArray.set(arrayIdx, newVal);
+ }
+ } else {
+ nestedArray.add(newVal);
+ }
+ break;
+ }
+ case DELETE_FROM_SET: {
+ if (arrayIdx < nestedArray.size()) {
+ BsonValue currentValue = nestedArray.get(arrayIdx);
+ if (currentValue != null) {
+ BsonValue modifiedVal = modifyFieldValueByDeleteFromSet(currentValue, newVal);
+ if (modifiedVal == null) {
+ nestedArray.remove(arrayIdx);
+ } else {
+ nestedArray.set(arrayIdx, modifiedVal);
+ }
+ } else {
+ LOGGER.info(
+ "Nothing to be removed as nested list does not have value for field {}. "
+ + "Update operator: {}",
+ fieldKey, updateOp);
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ /**
+ * Perform update operation at the leaf node. This method is called only when the leaf
+ * node or the target node for the given field key is encountered while iterating through
+ * the tree structure. This is specifically called when the closest ancestor of the
+ * given node is a nested document. The leaf node is for the field key. For example, for
+ * "category.subcategories", the leaf node is "subcategories" whereas for
+ * "category.subcategories.brands[2]", the leaf node is "brands".
+ *
+ * @param newVal New value to be used while performing update operation on the existing node.
+ * @param updateOp Type of the update operation to be performed.
+ * @param nestedDocument The parent or ancestor node, expected to be nested document only.
+ * @param targetNodeFieldKey The target fieldKey value. For example, for "category.subcategories"
+ * field key, the target node field key is expected to be "subcategories" and the nestedDocument
+ * is expected to be "category" document.
+ */
+ private static void updateDocumentAtLeafNode(BsonValue newVal, UpdateOp updateOp,
+ BsonDocument nestedDocument, StringBuilder targetNodeFieldKey) {
+ switch (updateOp) {
+ case SET: {
+ nestedDocument.put(targetNodeFieldKey.toString(), newVal);
+ break;
+ }
+ case UNSET: {
+ nestedDocument.remove(targetNodeFieldKey.toString());
+ break;
+ }
+ case ADD: {
+ BsonValue currentValue = nestedDocument.get(targetNodeFieldKey.toString());
+ if (currentValue != null) {
+ nestedDocument.put(targetNodeFieldKey.toString(), modifyFieldValueByAdd(currentValue, newVal));
+ } else {
+ nestedDocument.put(targetNodeFieldKey.toString(), newVal);
+ }
+ break;
+ }
+ case DELETE_FROM_SET: {
+ BsonValue currentValue = nestedDocument.get(targetNodeFieldKey.toString());
+ if (currentValue != null) {
+ BsonValue modifiedVal = modifyFieldValueByDeleteFromSet(currentValue, newVal);
+ if (modifiedVal == null) {
+ nestedDocument.remove(targetNodeFieldKey.toString());
+ } else {
+ nestedDocument.put(targetNodeFieldKey.toString(), modifiedVal);
+ }
+ } else {
+ LOGGER.info(
+ "Nothing to be removed as field with key {} does not exist. Update operator: {}",
+ targetNodeFieldKey, updateOp);
+ }
+ break;
+ }
+ }
+ }
+
+ /**
+ * Retrieve the value to be updated for the given current value. If the current value does not
+ * contain any arithmetic operators, the current value is returned without any modifications.
+ * If the current value contains arithmetic expressions like "a + b" or "a - b", the values of
+ * operands are retrieved from the given document and if the values are numeric, the given
+ * arithmetic operation is performed.
+ *
+ * @param curValue The current value.
+ * @param bsonDocument The document with all field key-value pairs.
+ * @return Updated values to be used by SET operation.
+ */
+ private static BsonValue getNewFieldValue(final BsonValue curValue,
+ final BsonDocument bsonDocument) {
+ if (curValue != null && curValue.isString() && (
+ ((BsonString) curValue).getValue().contains(" + ") || ((BsonString) curValue).getValue()
+ .contains(" - "))) {
+ String[] tokens = ((BsonString) curValue).getValue().split("\\s+");
+ boolean addNum = true;
+ // Pattern pattern = Pattern.compile(":?[a-zA-Z0-9]+");
+ Pattern pattern = Pattern.compile("[#:$]?[^\\s\\n]+");
+ Number newNum = null;
+ for (String token : tokens) {
+ if (token.equals("+")) {
+ addNum = true;
+ continue;
+ } else if (token.equals("-")) {
+ addNum = false;
+ continue;
+ }
+ Matcher matcher = pattern.matcher(token);
+ if (matcher.find()) {
+ String operand = matcher.group();
+ Number literalNum;
+ BsonValue topLevelValue = bsonDocument.get(operand);
+ BsonValue bsonValue = topLevelValue != null ?
+ topLevelValue :
+ CommonComparisonExpressionUtils.getFieldFromDocument(operand, bsonDocument);
+
+ if (bsonValue == null && (literalNum = stringToNumber(operand)) != null) {
+ Number val = literalNum;
+ newNum =
+ newNum == null ? val : (addNum ? addNum(newNum, val) : subtractNum(newNum, val));
+ } else {
+ if (bsonValue == null) {
+ throw new IllegalArgumentException("Operand " + operand + " does not exist");
+ }
+ if (!bsonValue.isNumber() && !bsonValue.isDecimal128()) {
+ throw new IllegalArgumentException(
+ "Operand " + operand + " is not provided as number type");
+ }
+ Number val = getNumberFromBsonNumber((BsonNumber) bsonValue);
+ newNum =
+ newNum == null ? val : (addNum ? addNum(newNum, val) : subtractNum(newNum, val));
+ }
+ }
+ }
+ return getBsonNumberFromNumber(newNum);
+ }
+ return curValue;
+ }
+
+ /**
+ * Performs arithmetic addition operation on the given numeric operands.
+ *
+ * @param num1 First number.
+ * @param num2 Second number.
+ * @return The value as addition of both numbers.
+ */
+ private static Number addNum(final Number num1, final Number num2) {
+ if (num1 instanceof Double || num2 instanceof Double) {
+ return num1.doubleValue() + num2.doubleValue();
+ } else if (num1 instanceof Float || num2 instanceof Float) {
+ return num1.floatValue() + num2.floatValue();
+ } else if (num1 instanceof Long || num2 instanceof Long) {
+ return num1.longValue() + num2.longValue();
+ } else {
+ return num1.intValue() + num2.intValue();
+ }
+ }
+
+ /**
+ * Performs arithmetic subtraction operation on the given numeric operands.
+ *
+ * @param num1 First number.
+ * @param num2 Second number.
+ * @return The subtracted value as first number minus second number.
+ */
+ private static Number subtractNum(final Number num1, final Number num2) {
+ if (num1 instanceof Double || num2 instanceof Double) {
+ return num1.doubleValue() - num2.doubleValue();
+ } else if (num1 instanceof Float || num2 instanceof Float) {
+ return num1.floatValue() - num2.floatValue();
+ } else if (num1 instanceof Long || num2 instanceof Long) {
+ return num1.longValue() - num2.longValue();
+ } else {
+ return num1.intValue() - num2.intValue();
+ }
+ }
+
+ /**
+ * Convert the given Number to String.
+ *
+ * @param number The Number object.
+ * @return String represented number value.
+ */
+ private static String numberToString(Number number) {
+ if (number instanceof Integer || number instanceof Short || number instanceof Byte) {
+ return Integer.toString(number.intValue());
+ } else if (number instanceof Long) {
+ return Long.toString(number.longValue());
+ } else if (number instanceof Double) {
+ return Double.toString(number.doubleValue());
+ } else if (number instanceof Float) {
+ return Float.toString(number.floatValue());
+ }
+ throw new RuntimeException("Number type is not known for number: " + number);
+ }
+
+ /**
+ * Convert the given String to Number.
+ *
+ * @param number The String represented numeric value.
+ * @return The Number object.
+ */
+ private static Number stringToNumber(String number) {
+ try {
+ return Integer.parseInt(number);
+ } catch (NumberFormatException e) {
+ // no-op
+ }
+ try {
+ return Long.parseLong(number);
+ } catch (NumberFormatException e) {
+ // no-op
+ }
+ try {
+ return Double.parseDouble(number);
+ } catch (NumberFormatException e) {
+ // no-op
+ }
+ try {
+ return NumberFormat.getInstance().parse(number);
+ } catch (ParseException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Convert Number to BsonNumber.
+ *
+ * @param number The Number object.
+ * @return The BsonNumber object.
+ */
+ private static BsonNumber getBsonNumberFromNumber(Number number) {
+ BsonNumber bsonNumber;
+ if (number instanceof Integer || number instanceof Short || number instanceof Byte) {
+ bsonNumber = new BsonInt32(number.intValue());
+ } else if (number instanceof Long) {
+ bsonNumber = new BsonInt64(number.longValue());
+ } else if (number instanceof Double || number instanceof Float) {
+ bsonNumber = new BsonDouble(number.doubleValue());
+ } else if (number instanceof BigDecimal) {
+ bsonNumber = new BsonDecimal128(new Decimal128((BigDecimal) number));
+ } else {
+ throw new IllegalArgumentException("Unsupported Number type: " + number.getClass());
+ }
+ return bsonNumber;
+ }
+
+ /**
+ * Convert BsonNumber to Number.
+ *
+ * @param bsonNumber The BsonNumber object.
+ * @return The Number object.
+ */
+ public static Number getNumberFromBsonNumber(BsonNumber bsonNumber) {
+ if (bsonNumber instanceof BsonInt32) {
+ return ((BsonInt32) bsonNumber).getValue();
+ } else if (bsonNumber instanceof BsonInt64) {
+ return ((BsonInt64) bsonNumber).getValue();
+ } else if (bsonNumber instanceof BsonDouble) {
+ return ((BsonDouble) bsonNumber).getValue();
+ } else if (bsonNumber instanceof BsonDecimal128) {
+ return ((BsonDecimal128) bsonNumber).getValue().bigDecimalValue();
+ } else {
+ throw new IllegalArgumentException("Unsupported BsonNumber type: " + bsonNumber.getClass());
+ }
+ }
+
+ /**
+ * Returns true if the given BsonValue represents Set data structure.
+ *
+ * @param bsonValue The value.
+ * @return True if the given BsonValue represents Set data structure.
+ */
+ private static boolean isBsonSet(final BsonValue bsonValue) {
+ if (!bsonValue.isDocument()) {
+ return false;
+ }
+ BsonDocument bsonDocument = (BsonDocument) bsonValue;
+ if (bsonDocument.size() == 1 && bsonDocument.containsKey("$set")) {
+ BsonValue value = bsonDocument.get("$set");
+ return value != null && value.isArray();
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if both values represent Set data structure and the contents of the Set are
+ * of same type.
+ *
+ * @param bsonValue1 First value.
+ * @param bsonValue2 Second value.
+ * @return True if both values represent Set data structure and the contents of the Set are
+ * of same type.
+ */
+ private static boolean areBsonSetOfSameType(final BsonValue bsonValue1,
+ final BsonValue bsonValue2) {
+ if (!isBsonSet(bsonValue1) || !isBsonSet(bsonValue2)) {
+ return false;
+ }
+ BsonArray bsonArray1 = (BsonArray) ((BsonDocument) bsonValue1).get("$set");
+ BsonArray bsonArray2 = (BsonArray) ((BsonDocument) bsonValue2).get("$set");
+ return bsonArray1.get(0).getBsonType().equals(bsonArray2.get(0).getBsonType());
+ }
+
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonConditionExpressionParseNode.java
similarity index 67%
copy from phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
copy to phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonConditionExpressionParseNode.java
index 33f75fcc38..7fd55b8077 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonConditionExpressionParseNode.java
@@ -17,29 +17,31 @@
*/
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.FunctionExpression;
-import org.apache.phoenix.expression.function.JsonValueFunction;
+import org.apache.phoenix.expression.function.BsonConditionExpressionFunction;
import org.apache.phoenix.schema.types.PDataType;
-import org.apache.phoenix.schema.types.PJson;
-
-import java.sql.SQLException;
-import java.util.List;
+import org.apache.phoenix.schema.types.PBson;
-public class JsonValueParseNode extends FunctionParseNode {
+public class BsonConditionExpressionParseNode extends FunctionParseNode {
- public JsonValueParseNode(String name, List<ParseNode> children, BuiltInFunctionInfo info) {
+ public BsonConditionExpressionParseNode(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)) {
- throw new SQLException(dataType + " type is unsupported for JSON_VALUE().");
+ PDataType<?> dataType = children.get(0).getDataType();
+ if (!dataType.isCoercibleTo(PBson.INSTANCE)) {
+ throw new SQLException(
+ dataType + " type is unsupported for BSON_CONDITION_EXPRESSION().");
}
- return new JsonValueFunction(children);
+ return new BsonConditionExpressionFunction(children);
}
}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonUpdateExpressionParseNode.java
similarity index 68%
copy from phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
copy to phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonUpdateExpressionParseNode.java
index 33f75fcc38..7bb06cd4b7 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/BsonUpdateExpressionParseNode.java
@@ -17,29 +17,31 @@
*/
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.BsonUpdateExpressionFunction;
import org.apache.phoenix.expression.function.FunctionExpression;
-import org.apache.phoenix.expression.function.JsonValueFunction;
+import org.apache.phoenix.schema.types.PBson;
import org.apache.phoenix.schema.types.PDataType;
-import org.apache.phoenix.schema.types.PJson;
-
-import java.sql.SQLException;
-import java.util.List;
-public class JsonValueParseNode extends FunctionParseNode {
+public class BsonUpdateExpressionParseNode extends FunctionParseNode {
- public JsonValueParseNode(String name, List<ParseNode> children, BuiltInFunctionInfo info) {
+ public BsonUpdateExpressionParseNode(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)) {
- throw new SQLException(dataType + " type is unsupported for JSON_VALUE().");
+ PDataType<?> dataType = children.get(0).getDataType();
+ if (!dataType.isCoercibleTo(PBson.INSTANCE)) {
+ throw new SQLException(
+ dataType + " type is unsupported for BSON_CONDITION_EXPRESSION().");
}
- return new JsonValueFunction(children);
+ return new BsonUpdateExpressionFunction(children);
}
}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
index 33f75fcc38..eb1e413b3f 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/parse/JsonValueParseNode.java
@@ -21,6 +21,7 @@ import org.apache.phoenix.compile.StatementContext;
import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.expression.function.FunctionExpression;
import org.apache.phoenix.expression.function.JsonValueFunction;
+import org.apache.phoenix.schema.types.PBson;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PJson;
@@ -37,7 +38,7 @@ public class JsonValueParseNode extends FunctionParseNode {
public FunctionExpression create(List<Expression> children, StatementContext context)
throws SQLException {
PDataType dataType = children.get(0).getDataType();
- if (!dataType.isCoercibleTo(PJson.INSTANCE)) {
+ if (!dataType.isCoercibleTo(PJson.INSTANCE) && !dataType.isCoercibleTo(PBson.INSTANCE)) {
throw new SQLException(dataType + " type is unsupported for JSON_VALUE().");
}
return new JsonValueFunction(children);
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java
new file mode 100644
index 0000000000..6944f2cbcd
--- /dev/null
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PBson.java
@@ -0,0 +1,136 @@
+/*
+ * 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.schema.types;
+
+import org.bson.BsonDocument;
+import org.bson.RawBsonDocument;
+import org.bson.codecs.BsonDocumentCodec;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.schema.SortOrder;
+import org.apache.phoenix.util.ByteUtil;
+
+/**
+ * <p>
+ * A Phoenix data type to represent Bson. The Bson can represent Scalar types as well as
+ * Complex nested types in Binary Encoded JSON.
+ * </p>
+ */
+public class PBson extends PVarbinary {
+
+ public static final PBson INSTANCE = new PBson();
+
+ private PBson() {
+ super("BSON", PDataType.BSON_TYPE, byte[].class, null, 49);
+ }
+
+ @Override
+ public boolean canBePrimaryKey() {
+ return false;
+ }
+
+ @Override
+ public boolean isComparisonSupported() {
+ return false;
+ }
+
+ @Override
+ public int toBytes(Object object, byte[] bytes, int offset) {
+ if (object == null) {
+ return 0;
+ }
+ byte[] b = toBytes(object);
+ System.arraycopy(b, 0, bytes, offset, b.length);
+ return b.length;
+ }
+
+ @Override
+ public byte[] toBytes(Object object) {
+ if (object == null) {
+ return ByteUtil.EMPTY_BYTE_ARRAY;
+ }
+ if (!(object instanceof BsonDocument)) {
+ throw new IllegalArgumentException("The object should be of type BsonDocument");
+ }
+ if (object instanceof RawBsonDocument) {
+ return Bytes.toBytes(((RawBsonDocument) object).getByteBuffer().asNIO());
+ } else {
+ RawBsonDocument rawBsonDocument =
+ new RawBsonDocument((BsonDocument) object, new BsonDocumentCodec());
+ return Bytes.toBytes((rawBsonDocument).getByteBuffer().asNIO());
+ }
+ }
+
+ @Override
+ public Object toObject(byte[] bytes, int offset, int length,
+ @SuppressWarnings("rawtypes") PDataType actualType, SortOrder sortOrder,
+ Integer maxLength, Integer scale) {
+ if (length == 0) {
+ return null;
+ }
+ return new RawBsonDocument(bytes, offset, length);
+ }
+
+ @Override
+ public Object toObject(Object object, @SuppressWarnings("rawtypes") PDataType actualType) {
+ if (object == null) {
+ return null;
+ }
+ if (equalsAny(actualType, PVarchar.INSTANCE)) {
+ return toObject((String) object);
+ }
+ return object;
+ }
+
+ @Override
+ public Object toObject(String value) {
+ if (value == null || value.isEmpty()) {
+ return null;
+ }
+ return RawBsonDocument.parse(value);
+ }
+
+ @Override
+ public boolean isCoercibleTo(@SuppressWarnings("rawtypes") PDataType targetType) {
+ return equalsAny(targetType, this, PBinary.INSTANCE, PVarbinary.INSTANCE);
+ }
+
+ @Override
+ public int estimateByteSize(Object o) {
+ byte[] value = toBytes(o);
+ return value == null ? 1 : value.length;
+ }
+
+ @Override
+ public Integer getByteSize() {
+ return null;
+ }
+
+ @Override
+ public boolean isBytesComparableWith(@SuppressWarnings("rawtypes") PDataType otherType) {
+ return otherType == PVarbinary.INSTANCE;
+ }
+
+ @Override
+ public Object getSampleValue(Integer maxLength, Integer arrayLength) {
+ String mapStr = "{\"map\":{\"attr_0\":{\"s\":\"val_0\"}}}";
+ return this.toObject(mapStr);
+ }
+
+}
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataType.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataType.java
index 4b33891284..a9721c3e42 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataType.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataType.java
@@ -44,6 +44,8 @@ import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
import org.apache.phoenix.thirdparty.com.google.common.math.LongMath;
import org.apache.phoenix.thirdparty.com.google.common.primitives.Doubles;
import org.apache.phoenix.thirdparty.com.google.common.primitives.Longs;
+
+import org.bson.BsonDocument;
import org.bson.RawBsonDocument;
/**
@@ -532,6 +534,7 @@ public abstract class PDataType<T> implements DataType<T>, Comparable<PDataType<
public static final int ARRAY_TYPE_BASE = 3000;
public static final int JSON_TYPE = 5000;
+ public static final int BSON_TYPE = 7000;
public static final String ARRAY_TYPE_SUFFIX = "ARRAY";
protected static final ThreadLocal<Random> RANDOM = new ThreadLocal<Random>() {
@@ -1219,8 +1222,8 @@ public abstract class PDataType<T> implements DataType<T>, Comparable<PDataType<
}
} catch (SQLException e) { /* Passthrough to fail */ }
}
- } else if (value instanceof RawBsonDocument) {
- if (type == PJson.INSTANCE) {
+ } else if (value instanceof BsonDocument) {
+ if (type == PBson.INSTANCE) {
return type;
}
} else {
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataTypeFactory.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataTypeFactory.java
index b7d30e37a2..f8a6b722e1 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataTypeFactory.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDataTypeFactory.java
@@ -124,6 +124,7 @@ public class PDataTypeFactory {
types.add(PVarchar.INSTANCE);
types.add(PVarcharArray.INSTANCE);
types.add(PJson.INSTANCE);
+ types.add(PBson.INSTANCE);
classToInstance = new HashMap<>(types.size());
for (PDataType t : types) {
diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/util/CDCUtil.java b/phoenix-core-client/src/main/java/org/apache/phoenix/util/CDCUtil.java
index 78dc368003..2a587d8cf8 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/util/CDCUtil.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/util/CDCUtil.java
@@ -28,6 +28,7 @@ import java.util.Set;
import java.util.StringTokenizer;
import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.util.StringUtils;
import org.apache.phoenix.exception.SQLExceptionCode;
import org.apache.phoenix.exception.SQLExceptionInfo;
@@ -35,6 +36,7 @@ import org.apache.phoenix.execute.DescVarLengthFastByteComparisons;
import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
import org.apache.phoenix.schema.PTable;
import org.apache.phoenix.schema.types.PDataType;
+import org.bson.RawBsonDocument;
public class CDCUtil {
public static final String CDC_INDEX_PREFIX = "__CDC__";
@@ -121,7 +123,10 @@ public class CDCUtil {
public static Object getColumnEncodedValue(Object value, PDataType dataType) {
if (value != null) {
// TODO: Need suport for DECIMAL, NUMERIC and array types.
- if (dataType.getSqlType() == Types.BINARY || dataType.getSqlType() == Types.VARBINARY
+ if (dataType.getSqlType() == PDataType.BSON_TYPE) {
+ value = Bytes.toBytes(((RawBsonDocument) value).getByteBuffer().asNIO());
+ } else if (dataType.getSqlType() == Types.BINARY ||
+ dataType.getSqlType() == Types.VARBINARY
|| dataType.getSqlType() == Types.LONGVARBINARY) {
// Unfortunately, Base64.Encoder has no option to specify offset and length so can't
// avoid copying bytes.
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson1IT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson1IT.java
new file mode 100644
index 0000000000..7604289e7c
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson1IT.java
@@ -0,0 +1,304 @@
+/*
+ * 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.end2end;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.bson.BsonDocument;
+import org.bson.BsonString;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.util.bson.SerializableBytesPtr;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.apache.phoenix.util.bson.TestFieldsMap;
+import org.apache.phoenix.util.bson.TestUtil;
+import org.apache.phoenix.util.bson.TestFieldValue;
+
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for BSON.
+ */
+@Category(ParallelStatsDisabledTest.class)
+public class Bson1IT extends ParallelStatsDisabledIT {
+
+ @Test
+ public void testSimpleMap() 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, PK2 DOUBLE NOT NULL, COL BSON CONSTRAINT pk PRIMARY KEY(PK1, PK2))";
+ conn.createStatement().execute(ddl);
+ TestFieldsMap map = getMap1();
+ BsonDocument bsonDocument = TestUtil.getRawBsonDocument(map);
+ PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setDouble(2, 123985);
+ stmt.setObject(3, bsonDocument);
+ stmt.execute();
+ stmt.setString(1, "pk0002");
+ stmt.setDouble(2, 4596.354);
+ stmt.setString(3, bsonDocument.toJson());
+ stmt.execute();
+ conn.commit();
+
+ String query = "SELECT * FROM " + tableName;
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals(123985.0, rs.getDouble(2), 0.0);
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument, document1);
+
+ String documentAsString = rs.getString(3);
+ assertEquals(bsonDocument.toJson(), documentAsString);
+
+ assertTrue(rs.next());
+ assertEquals("pk0002", rs.getString(1));
+ assertEquals(4596.354, rs.getDouble(2), 0.0);
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument, document2);
+
+ documentAsString = rs.getString(3);
+ assertEquals(bsonDocument.toJson(), documentAsString);
+
+ assertFalse(rs.next());
+ }
+ }
+
+ @Test
+ public void testMapWithConditionExpressions() 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, PK2 DOUBLE NOT NULL, COL BSON CONSTRAINT pk PRIMARY KEY(PK1, PK2))";
+ conn.createStatement().execute(ddl);
+ TestFieldsMap map1 = getMap1();
+ BsonDocument bsonDocument1 = TestUtil.getRawBsonDocument(map1);
+ TestFieldsMap map2 = getMap2();
+ BsonDocument bsonDocument2 = TestUtil.getRawBsonDocument(map2);
+ TestFieldsMap testFieldsMap = getCompareValueMap();
+ BsonDocument compareValuesDocument =
+ TestUtil.getRawBsonDocument(testFieldsMap);
+ PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setDouble(2, 123985);
+ stmt.setObject(3, bsonDocument1);
+ stmt.execute();
+ stmt.setString(1, "pk0002");
+ stmt.setDouble(2, 4596.354);
+ stmt.setObject(3, bsonDocument2);
+ stmt.execute();
+ conn.commit();
+
+ String conditionExpression =
+ "(field_exists(Id) OR field_exists(attr_3)) AND field_exists(attr_6) AND field_exists(attr_6.n_attr_1)";
+
+ BsonDocument conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", compareValuesDocument);
+
+ String query = "SELECT * FROM " + tableName + " WHERE BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals(123985.0, rs.getDouble(2), 0.0);
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertTrue(rs.next());
+ assertEquals("pk0002", rs.getString(1));
+ assertEquals(4596.354, rs.getDouble(2), 0.0);
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument2, document2);
+
+ assertFalse(rs.next());
+
+ conditionExpression =
+ "(field_exists(Id) OR field_exists(attr_3)) AND field_exists(attr_6) AND field_exists(attr_6.n_attr_10)";
+
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", compareValuesDocument);
+
+ query = "SELECT * FROM " + tableName + " WHERE BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ rs = conn.createStatement().executeQuery(query);
+
+ assertFalse(rs.next());
+
+ conditionExpression = "(field_exists(ISBN))";
+
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", compareValuesDocument);
+
+ query = "SELECT * FROM " + tableName + " WHERE BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+
+ assertEquals("pk0002", rs.getString(1));
+ assertEquals(4596.354, rs.getDouble(2), 0.0);
+ document2 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument2, document2);
+
+ assertFalse(rs.next());
+
+ conditionExpression =
+ "NestedList1[0] <= :NestedList1_485 AND NestedList1[2][0] >= :NestedList1_xyz0123 "
+ + "AND NestedList1[2][1].Id < :Id1 AND IdS < :Ids1 AND Id2 > :Id2";
+
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", compareValuesDocument);
+
+ query = "SELECT * FROM " + tableName + " WHERE BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0002", rs.getString(1));
+ assertEquals(4596.354, rs.getDouble(2), 0.0);
+ document2 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument2, document2);
+
+ assertFalse(rs.next());
+ }
+ }
+
+ private static TestFieldsMap getCompareValueMap() {
+ TestFieldsMap compareValMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map2 = new HashMap<>();
+ map2.put(":Id", new TestFieldValue().withN(101.01));
+ map2.put(":Id1", new TestFieldValue().withN(120));
+ map2.put(":Ids1", new TestFieldValue().withS("12"));
+ map2.put(":Id2", new TestFieldValue().withN(12));
+ map2.put(":Title", new TestFieldValue().withS("Book 101 Title"));
+ map2.put(":ISBN", new TestFieldValue().withS("111-1111111111"));
+ map2.put(":InPublication", new TestFieldValue().withBOOL(false));
+ map2.put(":NMap1_NList1", new TestFieldValue().withS("NListVal01"));
+ map2.put(":NestedList1_485", new TestFieldValue().withN(-485.33));
+ map2.put(":NestedList1_xyz0123", new TestFieldValue().withS("xyz0123"));
+ compareValMap.setMap(map2);
+ return compareValMap;
+ }
+
+ private static TestFieldsMap getMap2() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withN(101.01));
+ map.put("IdS", new TestFieldValue().withS("101.01"));
+ map.put("Id2", new TestFieldValue().withN(101.01));
+ map.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1",
+ new TestFieldValue().withL(new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-23.4)));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ private static TestFieldsMap getMap1() {
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal = "randome93h5onefhu1nkr_1930`kjv-,!$%^ieifhbj034";
+ byte[] bytesAttrVal = bytesAttributeVal.getBytes();
+ map.put("attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal)));
+ map.put("attr_3", new TestFieldValue().withBOOL(true));
+ map.put("attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nestedMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nestedMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nestedMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nestedMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nestedMap1));
+ map.put("attr_7", new TestFieldValue().withSS("strset001", "str_set002", "strset003"));
+ map.put("attr_8", new TestFieldValue().withNS(7593, 3802.34, -4839, -40.667));
+ map.put("attr_9", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("abcd")),
+ new SerializableBytesPtr(Bytes.toBytes("string_0203"))));
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson2IT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson2IT.java
new file mode 100644
index 0000000000..3e02945014
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson2IT.java
@@ -0,0 +1,743 @@
+/*
+ * 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.end2end;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.bson.BsonDocument;
+import org.bson.BsonString;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.apache.phoenix.util.bson.TestFieldsMap;
+import org.apache.phoenix.util.bson.TestUtil;
+import org.apache.phoenix.util.bson.TestFieldValue;
+import org.apache.phoenix.util.bson.SerializableBytesPtr;
+
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for BSON.
+ */
+@Category(ParallelStatsDisabledTest.class)
+public class Bson2IT extends ParallelStatsDisabledIT {
+
+ @Test
+ public void testUpdateExpressions() 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, PK2 VARCHAR NOT NULL, COL BSON"
+ + " CONSTRAINT pk PRIMARY KEY(PK1, PK2))";
+ conn.createStatement().execute(ddl);
+ TestFieldsMap testFieldsMap1 = getTestFieldsMap1();
+ BsonDocument bsonDocument1 = TestUtil.getRawBsonDocument(testFieldsMap1);
+ TestFieldsMap compareTestFieldsMap = getComparisonValuesMap();
+ BsonDocument compareValuesDocument =
+ TestUtil.getRawBsonDocument(compareTestFieldsMap);
+ PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "pk0002");
+ stmt.setObject(3, bsonDocument1);
+ stmt.executeUpdate();
+ conn.commit();
+
+ String query = "SELECT * FROM " + tableName;
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("pk0002", rs.getString(2));
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+/*
+ String updateExp = "SET Title = :newTitle, Id = :newId , NestedMap1.ColorList = :ColorList ,"
+ + "Id1 = :Id1 , NestedMap1.NList1[0] = :NList1_0 , "
+ + "NestedList1[2][1].ISBN = :NestedList1_ISBN , "
+ + "NestedMap1.NestedMap2.NewID = :newId , "
+ + "NestedMap1.NestedMap2.NList[2] = :NList003 , "
+ + "NestedMap1.NestedMap2.NList[0] = :NList001 ADD AddedId :attr5_0 , "
+ + "NestedMap1.AddedId :attr5_0, NestedMap1.NestedMap2.Id :newIdNeg , "
+ + "NestedList12[2][0] :NestedList12_00 , NestedList12[2][1] :NestedList12_01 , "
+ + "Pictures :AddedPics "
+ + "REMOVE IdS, Id2, NestedMap1.Title , "
+ + "NestedMap1.NestedMap2.InPublication , NestedList1[2][1].TitleSet1 "
+ + "DELETE PictureBinarySet :PictureBinarySet01 , NestedMap1.NSet1 :NSet01 ,"
+ + " NestedList1[2][1].TitleSet2 :NestedList1TitleSet01";
+*/
+
+
+ //{
+ // "$SET": {
+ // "Title": "Cycle_1234_new",
+ // "Id": "12345",
+ // "NestedMap1.ColorList": [
+ // "Black",
+ // {
+ // "$binary": {
+ // "base64": "V2hpdGU=",
+ // "subType": "00"
+ // }
+ // },
+ // "Silver"
+ // ],
+ // "Id1": {
+ // "$binary": {
+ // "base64": "SURfMTAx",
+ // "subType": "00"
+ // }
+ // },
+ // "NestedMap1.NList1[0]": {
+ // "$set": [
+ // "Updated_set_01",
+ // "Updated_set_02"
+ // ]
+ // },
+ // "NestedList1[2][1].ISBN": "111-1111111122",
+ // "NestedMap1.NestedMap2.NewID": "12345",
+ // "NestedMap1.NestedMap2.NList[2]": null,
+ // "NestedMap1.NestedMap2.NList[0]": 12.22
+ // },
+ // "$UNSET": {
+ // "IdS": null,
+ // "Id2": null,
+ // "NestedMap1.Title": null,
+ // "NestedMap1.NestedMap2.InPublication": null,
+ // "NestedList1[2][1].TitleSet1": null
+ // },
+ // "$ADD": {
+ // "AddedId": 10,
+ // "NestedMap1.AddedId": 10,
+ // "NestedMap1.NestedMap2.Id": -12345,
+ // "NestedList12[2][0]": {
+ // "$set": [
+ // "xyz01234",
+ // "abc01234"
+ // ]
+ // },
+ // "NestedList12[2][1]": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "dmFsMDM=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "dmFsMDQ=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // },
+ // "Pictures": {
+ // "$set": [
+ // "xyz5@_rear.jpg",
+ // "1235@_rear.jpg"
+ // ]
+ // }
+ // },
+ // "$DELETE_FROM_SET": {
+ // "PictureBinarySet": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "MTIzX3JlYXIuanBn",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "eHl6X2Zyb250LmpwZ19ubw==",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "eHl6X2Zyb250LmpwZw==",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // },
+ // "NestedMap1.NSet1": {
+ // "$set": [
+ // -6830.5555,
+ // -48695
+ // ]
+ // },
+ // "NestedList1[2][1].TitleSet2": {
+ // "$set": [
+ // "Book 1011 Title",
+ // "Book 1010 Title"
+ // ]
+ // }
+ // }
+ //}
+
+ String updateExp = "{\n" +
+ " \"$SET\": {\n" +
+ " \"Title\": \"Cycle_1234_new\",\n" +
+ " \"Id\": \"12345\",\n" +
+ " \"NestedMap1.ColorList\": [\n" +
+ " \"Black\",\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"V2hpdGU=\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"Silver\"\n" +
+ " ],\n" +
+ " \"Id1\": {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"SURfMTAx\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"NestedMap1.NList1[0]\": {\n" +
+ " \"$set\": [\n" +
+ " \"Updated_set_01\",\n" +
+ " \"Updated_set_02\"\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList1[2][1].ISBN\": \"111-1111111122\",\n" +
+ " \"NestedMap1.NestedMap2.NewID\": \"12345\",\n" +
+ " \"NestedMap1.NestedMap2.NList[2]\": null,\n" +
+ " \"NestedMap1.NestedMap2.NList[0]\": 12.22\n" +
+ " },\n" +
+ " \"$UNSET\": {\n" +
+ " \"IdS\": null,\n" +
+ " \"Id2\": null,\n" +
+ " \"NestedMap1.Title\": null,\n" +
+ " \"NestedMap1.NestedMap2.InPublication\": null,\n" +
+ " \"NestedList1[2][1].TitleSet1\": null\n" +
+ " },\n" +
+ " \"$ADD\": {\n" +
+ " \"AddedId\": 10,\n" +
+ " \"NestedMap1.AddedId\": 10,\n" +
+ " \"NestedMap1.NestedMap2.Id\": -12345,\n" +
+ " \"NestedList12[2][0]\": {\n" +
+ " \"$set\": [\n" +
+ " \"xyz01234\",\n" +
+ " \"abc01234\"\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList12[2][1]\": {\n" +
+ " \"$set\": [\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"dmFsMDM=\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"dmFsMDQ=\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " }\n" +
+ " ]\n" +
+ " },\n" +
+ " \"Pictures\": {\n" +
+ " \"$set\": [\n" +
+ " \"xyz5@_rear.jpg\",\n" +
+ " \"1235@_rear.jpg\"\n" +
+ " ]\n" +
+ " }\n" +
+ " },\n" +
+ " \"$DELETE_FROM_SET\": {\n" +
+ " \"PictureBinarySet\": {\n" +
+ " \"$set\": [\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"MTIzX3JlYXIuanBn\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"eHl6X2Zyb250LmpwZ19ubw==\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"eHl6X2Zyb250LmpwZw==\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " }\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedMap1.NSet1\": {\n" +
+ " \"$set\": [\n" +
+ " -6830.5555,\n" +
+ " -48695\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList1[2][1].TitleSet2\": {\n" +
+ " \"$set\": [\n" +
+ " \"Book 1011 Title\",\n" +
+ " \"Book 1010 Title\"\n" +
+ " ]\n" +
+ " }\n" +
+ " }\n" +
+ "}";
+
+ String conditionExpression =
+ "PictureBinarySet = :PictureBinarySet AND NestedMap1.NestedMap2.NList[1] < :NList001";
+
+ BsonDocument conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", compareValuesDocument);
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?,?) ON DUPLICATE KEY UPDATE COL = CASE WHEN BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "') THEN BSON_UPDATE_EXPRESSION(COL, '"
+ + updateExp + "') ELSE COL END");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "pk0002");
+ stmt.executeUpdate();
+
+/*
+ updateExp = "SET Id1 = :newId , NestedList1[0] = NestedList1[0] + :NList001 , "
+ + "NestedList1[3] = :NList003 , NestedList1[4] = :NList004, "
+ + "attr_5[0] = attr_5[0] - :attr5_0";
+*/
+
+ updateExp = "{\n" +
+ " \"$SET\": {\n" +
+ " \"Id1\": \"12345\",\n" +
+ " \"NestedList1[0]\": \"NestedList1[0] + 12.22\",\n" +
+ " \"NestedList1[3]\": null,\n" +
+ " \"NestedList1[4]\": true,\n" +
+ " \"attr_5[0]\": \"attr_5[0] - 10\"\n" +
+ " }\n" +
+ "}";
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?,?) ON DUPLICATE KEY UPDATE COL = BSON_UPDATE_EXPRESSION(COL, '"
+ + updateExp + "')");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "pk0002");
+ stmt.executeUpdate();
+ conn.commit();
+
+ query = "SELECT * FROM " + tableName;
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("pk0002", rs.getString(2));
+
+ TestFieldsMap testFieldsMap3 = getTestFieldsMap3();
+ BsonDocument bsonDocument2 = TestUtil.getRawBsonDocument(testFieldsMap3);
+
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument2, document2);
+
+ assertFalse(rs.next());
+ }
+ }
+
+ private static TestFieldsMap getComparisonValuesMap() {
+ TestFieldsMap comparisonMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map2 = new HashMap<>();
+ map2.put(":newTitle", new TestFieldValue().withS("Cycle_1234_new"));
+ map2.put(":newId", new TestFieldValue().withS("12345"));
+ map2.put(":newIdNeg", new TestFieldValue().withN(-12345));
+ map2.put(":ColorList", new TestFieldValue().withL(
+ new TestFieldValue().withS("Black"),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White"))),
+ new TestFieldValue().withS("Silver")
+ ));
+ map2.put(":Id1",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("ID_101"))));
+ map2.put(":NList001", new TestFieldValue().withN(12.22));
+ map2.put(":NList003", new TestFieldValue().withNULL(true));
+ map2.put(":NList004", new TestFieldValue().withBOOL(true));
+ map2.put(":attr5_0", new TestFieldValue().withN(10));
+ map2.put(":NList1_0", new TestFieldValue().withSS("Updated_set_01", "Updated_set_02"));
+ map2.put(":NestedList1_ISBN", new TestFieldValue().withS("111-1111111122"));
+ map2.put(":NestedList12_00", new TestFieldValue().withSS("xyz01234", "abc01234"));
+ map2.put(":NestedList12_01", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val03")),
+ new SerializableBytesPtr(Bytes.toBytes("val04"))
+ ));
+ map2.put(":AddedPics", new TestFieldValue().withSS(
+ "1235@_rear.jpg",
+ "xyz5@_rear.jpg"));
+ map2.put(":PictureBinarySet01", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("123_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg_no"))
+ ));
+ map2.put(":NSet01", new TestFieldValue().withNS(-6830.5555, -48695));
+ map2.put(":NestedList1TitleSet01", new TestFieldValue().withSS("Book 1010 Title",
+ "Book 1011 Title"));
+ map2.put(":PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("123_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ comparisonMap.setMap(map2);
+ return comparisonMap;
+ }
+
+ private static TestFieldsMap getTestFieldsMap1() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withN(101.01));
+ map.put("IdS", new TestFieldValue().withS("101.01"));
+ map.put("Id2", new TestFieldValue().withN(101.01));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "123_rear.jpg",
+ "xyz_rear.jpg",
+ "123_front.jpg",
+ "xyz_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("123_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1",
+ new TestFieldValue().withL(new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-0.00234)));
+ Map<String, TestFieldValue> nestedMap2 = new HashMap<>();
+ nestedMap2.put("Id", new TestFieldValue().withN(101.22));
+ nestedMap2.put("Title", new TestFieldValue().withS("Book 10122 Title"));
+ nestedMap2.put("ISBN", new TestFieldValue().withS("111-1111111111999"));
+ nestedMap2.put("InPublication", new TestFieldValue().withBOOL(true));
+ nestedMap2.put("NList",
+ new TestFieldValue().withL(new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-0.00234)));
+ nestedMap1.put("NestedMap2",
+ new TestFieldValue().withM(nestedMap2));
+ nestedMap1.put("NSet1",
+ new TestFieldValue().withNS(123.45, 9586.7778, -124, -6830.5555, 10238,
+ -48695));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684d));
+ nestedList1Map1.put("TitleSet1",
+ new TestFieldValue().withSS("Book 1010 Title", "Book 1011 Title",
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("TitleSet2",
+ new TestFieldValue().withSS("Book 1010 Title", "Book 1011 Title",
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("BinaryTitleSet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("Book 1010 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1011 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1111 Title Binary"))
+ ));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ map.put("NestedList12",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withSS("xyz0123"),
+ new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val01")),
+ new SerializableBytesPtr(Bytes.toBytes("val02")),
+ new SerializableBytesPtr(Bytes.toBytes("val03"))))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ private static TestFieldsMap getTestFieldsMap2() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("AddedId", new TestFieldValue().withN(10));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("Id1",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("ID_101"))));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withS("12345"));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "123_rear.jpg",
+ "1235@_rear.jpg",
+ "xyz5@_rear.jpg",
+ "xyz_rear.jpg",
+ "123_front.jpg",
+ "xyz_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Cycle_1234_new"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("AddedId", new TestFieldValue().withN(10));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1", new TestFieldValue().withL(
+ new TestFieldValue().withSS("Updated_set_01", "Updated_set_02"),
+ new TestFieldValue().withN(-0.00234)));
+ nestedMap1.put("ColorList", new TestFieldValue().withL(
+ new TestFieldValue().withS("Black"),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White"))),
+ new TestFieldValue().withS("Silver")
+ ));
+ Map<String, TestFieldValue> nestedMap2 = new HashMap<>();
+ nestedMap2.put("Id", new TestFieldValue().withN(-12243.78));
+ nestedMap2.put("NewID", new TestFieldValue().withS("12345"));
+ nestedMap2.put("Title", new TestFieldValue().withS("Book 10122 Title"));
+ nestedMap2.put("ISBN", new TestFieldValue().withS("111-1111111111999"));
+ nestedMap2.put("NList",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(12.22),
+ new TestFieldValue().withN(-0.00234),
+ new TestFieldValue().withNULL(true)));
+ nestedMap1.put("NestedMap2",
+ new TestFieldValue().withM(nestedMap2));
+ nestedMap1.put("NSet1",
+ new TestFieldValue().withNS(123.45, 9586.7778, -124, 10238));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111122"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684D));
+ nestedList1Map1.put("TitleSet2",
+ new TestFieldValue().withSS(
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("BinaryTitleSet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("Book 1010 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1011 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1111 Title Binary"))
+ ));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ map.put("NestedList12",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(
+ new TestFieldValue().withSS("xyz0123", "xyz01234", "abc01234"),
+ new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val01")),
+ new SerializableBytesPtr(Bytes.toBytes("val02")),
+ new SerializableBytesPtr(Bytes.toBytes("val03")),
+ new SerializableBytesPtr(Bytes.toBytes("val04"))))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ private static TestFieldsMap getTestFieldsMap3() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("AddedId", new TestFieldValue().withN(10));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("Id1", new TestFieldValue().withS("12345"));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1224),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withS("12345"));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "123_rear.jpg",
+ "1235@_rear.jpg",
+ "xyz5@_rear.jpg",
+ "xyz_rear.jpg",
+ "123_front.jpg",
+ "xyz_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Cycle_1234_new"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("AddedId", new TestFieldValue().withN(10));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1", new TestFieldValue().withL(
+ new TestFieldValue().withSS("Updated_set_01", "Updated_set_02"),
+ new TestFieldValue().withN(-0.00234)));
+ nestedMap1.put("ColorList", new TestFieldValue().withL(
+ new TestFieldValue().withS("Black"),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White"))),
+ new TestFieldValue().withS("Silver")
+ ));
+ Map<String, TestFieldValue> nestedMap2 = new HashMap<>();
+ nestedMap2.put("Id", new TestFieldValue().withN(-12243.78));
+ nestedMap2.put("NewID", new TestFieldValue().withS("12345"));
+ nestedMap2.put("Title", new TestFieldValue().withS("Book 10122 Title"));
+ nestedMap2.put("ISBN", new TestFieldValue().withS("111-1111111111999"));
+ nestedMap2.put("NList",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(12.22),
+ new TestFieldValue().withN(-0.00234),
+ new TestFieldValue().withNULL(true)));
+ nestedMap1.put("NestedMap2",
+ new TestFieldValue().withM(nestedMap2));
+ nestedMap1.put("NSet1",
+ new TestFieldValue().withNS(123.45, 9586.7778, -124, 10238));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111122"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684d));
+ nestedList1Map1.put("TitleSet2",
+ new TestFieldValue().withSS(
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("BinaryTitleSet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("Book 1010 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1011 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1111 Title Binary"))
+ ));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-473.11999999999995),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1)),
+ new TestFieldValue().withNULL(true),
+ new TestFieldValue().withBOOL(true)));
+ map.put("NestedList12",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(
+ new TestFieldValue().withSS("xyz0123", "xyz01234", "abc01234"),
+ new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val01")),
+ new SerializableBytesPtr(Bytes.toBytes("val02")),
+ new SerializableBytesPtr(Bytes.toBytes("val03")),
+ new SerializableBytesPtr(Bytes.toBytes("val04"))))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+}
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
new file mode 100644
index 0000000000..67817c313d
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson3IT.java
@@ -0,0 +1,1859 @@
+/*
+ * 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.end2end;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.query.QueryConstants;
+import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+import org.apache.phoenix.util.CDCUtil;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.bson.BsonArray;
+import org.bson.BsonBinary;
+import org.bson.BsonBoolean;
+import org.bson.BsonDocument;
+import org.bson.BsonDouble;
+import org.bson.BsonNull;
+import org.bson.BsonString;
+import org.bson.RawBsonDocument;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for BSON.
+ */
+@Category(ParallelStatsDisabledTest.class)
+public class Bson3IT extends ParallelStatsDisabledIT {
+
+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+ private static String getJsonString(String jsonFilePath) throws IOException {
+ URL fileUrl = Bson3IT.class.getClassLoader().getResource(jsonFilePath);
+ Preconditions.checkArgument(fileUrl != null, "File path " + jsonFilePath + " seems invalid");
+ return FileUtils.readFileToString(new File(fileUrl.getFile()), Charset.defaultCharset());
+ }
+
+ /**
+ * Conditional Upserts for BSON pass where the Condition Expression is of SQL style.
+ */
+ @Test
+ public void testBsonOpsWithSqlConditionsUpdateSuccess() throws Exception {
+ Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+ String tableName = generateUniqueName();
+ String cdcName = 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))";
+ String cdcDdl = "CREATE CDC " + cdcName + " ON " + tableName;
+ conn.createStatement().execute(ddl);
+ conn.createStatement().execute(cdcDdl);
+ IndexToolIT.runIndexTool(false, "", tableName,
+ "\"" + CDCUtil.getCDCIndexName(cdcName) + "\"");
+ Timestamp ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ String sample1 = getJsonString("json/sample_01.json");
+ String sample2 = getJsonString("json/sample_02.json");
+ String sample3 = getJsonString("json/sample_03.json");
+ BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+ BsonDocument bsonDocument2 = RawBsonDocument.parse(sample2);
+ BsonDocument bsonDocument3 = RawBsonDocument.parse(sample3);
+
+ PreparedStatement stmt =
+ conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0002");
+ stmt.setObject(3, bsonDocument1);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1010");
+ stmt.setString(2, "1010");
+ stmt.setObject(3, bsonDocument2);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1011");
+ stmt.setString(2, "1011");
+ stmt.setObject(3, bsonDocument3);
+ stmt.executeUpdate();
+
+ conn.commit();
+ Thread.sleep(100);
+ Timestamp ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCAfterFirstUpsert(conn, cdcName, ts1, ts2, bsonDocument1, bsonDocument2, bsonDocument3);
+
+ ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ String conditionExpression =
+ "press = $press AND track[0].shot[2][0].city.standard[50] = $softly";
+
+ //{
+ // "$EXPR": "press = $press AND track[0].shot[2][0].city.standard[50] = $softly",
+ // "$VAL": {
+ // "$press": "beat",
+ // "$softly": "softly"
+ // }
+ //}
+ BsonDocument conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument()
+ .append("$press", new BsonString("beat"))
+ .append("$softly", new BsonString("softly")));
+
+ String query = "SELECT * FROM " + tableName +
+ " WHERE PK1 = 'pk0001' AND C1 = '0002' AND NOT BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0002", rs.getString(2));
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+ conditionExpression =
+ "press = $press AND track[0].shot[2][0].city.standard[5] = $softly";
+
+ //{
+ // "$EXPR": "press = $press AND track[0].shot[2][0].city.standard[5] = $softly",
+ // "$VAL": {
+ // "$press": "beat",
+ // "$softly": "softly"
+ // }
+ //}
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument()
+ .append("$press", new BsonString("beat"))
+ .append("$softly", new BsonString("softly")));
+
+ query = "SELECT * FROM " + tableName +
+ " WHERE PK1 = 'pk0001' AND C1 = '0002' AND BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0002", rs.getString(2));
+ document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+ //{
+ // "$SET": {
+ // "browserling": {
+ // "$binary": {
+ // "base64": "PkHjukNzgcg=",
+ // "subType": "00"
+ // }
+ // },
+ // "track[0].shot[2][0].city.standard[5]": "soft",
+ // "track[0].shot[2][0].city.problem[2]": "track[0].shot[2][0].city.problem[2] + 529.435"
+ // },
+ // "$UNSET": {
+ // "track[0].shot[2][0].city.flame": null
+ // }
+ //}
+ BsonDocument updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("browserling",
+ new BsonBinary(PDouble.INSTANCE.toBytes(-505169340.54880095)))
+ .append("track[0].shot[2][0].city.standard[5]", new BsonString("soft"))
+ .append("track[0].shot[2][0].city.problem[2]",
+ new BsonString("track[0].shot[2][0].city.problem[2] + 529.435")))
+ .append("$UNSET", new BsonDocument()
+ .append("track[0].shot[2][0].city.flame", new BsonNull()));
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END,"
+ + " C1 = ?");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0003");
+ stmt.executeUpdate();
+
+ //{
+ // "$ADD": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTA=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTM=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTQ=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$DELETE_FROM_SET": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDM=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$SET": {
+ // "newrecord": {
+ // "speed": "sun",
+ // "shot": [
+ // "fun",
+ // true,
+ // [
+ // {
+ // "character": 413968576,
+ // "earth": "helpful",
+ // "money": false,
+ // "city": {
+ // "softly": "service",
+ // "standard": [
+ // "pour",
+ // false,
+ // true,
+ // true,
+ // true,
+ // "softly",
+ // "happened"
+ // ],
+ // "problem": [
+ // -687102682.7731872,
+ // "tightly",
+ // 1527061470.2690287,
+ // {
+ // "condition": "else",
+ // "higher": 1462910924.088698,
+ // "scene": false,
+ // "safety": 240784722.66658115,
+ // "catch": false,
+ // "behavior": true,
+ // "protection": true
+ // },
+ // "torn",
+ // false,
+ // "eat"
+ // ],
+ // "flame": 1066643931,
+ // "rest": -1053428849,
+ // "additional": -442539394.7937908,
+ // "brought": "rock"
+ // },
+ // "upward": -1306729583.8727202,
+ // "sky": "act",
+ // "height": true
+ // },
+ // -2042805074.4290242,
+ // "settlers",
+ // 1455555511.4875226,
+ // -1448763321,
+ // false,
+ // 379701747
+ // ],
+ // false,
+ // 1241794365,
+ // "capital",
+ // false
+ // ],
+ // "hidden": false,
+ // "truth": "south",
+ // "other": true,
+ // "disease": "disease"
+ // }
+ // },
+ // "$UNSET": {
+ // "rather[3].outline.halfway.so[2][2]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$ADD", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample10")),
+ new BsonBinary(Bytes.toBytes("Sample12")),
+ new BsonBinary(Bytes.toBytes("Sample13")),
+ new BsonBinary(Bytes.toBytes("Sample14"))
+ )))))
+ .append("$DELETE_FROM_SET", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample02")),
+ new BsonBinary(Bytes.toBytes("Sample03"))
+ )))))
+ .append("$SET", new BsonDocument()
+ .append("newrecord", ((BsonArray) (document1.get("track"))).get(0)))
+ .append("$UNSET", new BsonDocument()
+ .append("rather[3].outline.halfway.so[2][2]", new BsonNull()));
+
+ conditionExpression =
+ "field_not_exists(newrecord) AND field_exists(rather[3].outline.halfway.so[2][2])";
+
+ //{
+ // "$EXPR": "field_not_exists(newrecord) AND field_exists(rather[3].outline.halfway.so[2][2])",
+ // "$VAL": {}
+ //}
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument());
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1010");
+ stmt.executeUpdate();
+
+ //{
+ // "$SET": {
+ // "result[1].location.state": "AK"
+ // },
+ // "$UNSET": {
+ // "result[4].emails[1]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("result[1].location.state", new BsonString("AK")))
+ .append("$UNSET", new BsonDocument()
+ .append("result[4].emails[1]", new BsonNull()));
+
+ conditionExpression =
+ "result[2].location.coordinates.latitude > $latitude OR "
+ + "(field_exists(result[1].location) AND result[1].location.state != $state" +
+ " AND field_exists(result[4].emails[1]))";
+
+ //{
+ // "$EXPR": "result[2].location.coordinates.latitude > $latitude OR (field_exists(result[1].location) AND result[1].location.state != $state AND field_exists(result[4].emails[1]))",
+ // "$VAL": {
+ // "$latitude": 0,
+ // "$state": "AK"
+ // }
+ //}
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument()
+ .append("$latitude", new BsonDouble(0))
+ .append("$state", new BsonString("AK")));
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1011");
+ stmt.executeUpdate();
+
+ conn.commit();
+ Thread.sleep(100);
+ ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCPostUpdate(conn, cdcName, ts1, ts2, bsonDocument1, bsonDocument2, bsonDocument3);
+
+ query = "SELECT * FROM " + tableName;
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0003", rs.getString(2));
+ document1 = (BsonDocument) rs.getObject(3);
+
+ String updatedJson = getJsonString("json/sample_updated_01.json");
+ assertEquals(RawBsonDocument.parse(updatedJson), document1);
+
+ assertTrue(rs.next());
+ assertEquals("pk1010", rs.getString(1));
+ assertEquals("1010", rs.getString(2));
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+
+ updatedJson = getJsonString("json/sample_updated_02.json");
+ assertEquals(RawBsonDocument.parse(updatedJson), document2);
+
+ assertTrue(rs.next());
+ assertEquals("pk1011", rs.getString(1));
+ assertEquals("1011", rs.getString(2));
+ BsonDocument document3 = (BsonDocument) rs.getObject(3);
+
+ updatedJson = getJsonString("json/sample_updated_03.json");
+ assertEquals(RawBsonDocument.parse(updatedJson), document3);
+
+ assertFalse(rs.next());
+ }
+ }
+
+ private static void testCDCAfterFirstUpsert(Connection conn, String cdcName, Timestamp ts1,
+ Timestamp ts2,
+ BsonDocument bsonDocument1,
+ BsonDocument bsonDocument2,
+ BsonDocument bsonDocument3)
+ throws SQLException, JsonProcessingException {
+ try (PreparedStatement pst = conn.prepareStatement(
+ "SELECT /*+ CDC_INCLUDE(PRE, POST) */ * FROM " + cdcName +
+ " WHERE PHOENIX_ROW_TIMESTAMP() >= ? AND PHOENIX_ROW_TIMESTAMP() <= ?")) {
+ pst.setTimestamp(1, ts1);
+ pst.setTimestamp(2, ts2);
+
+ ResultSet rs = pst.executeQuery();
+ Assert.assertTrue(rs.next());
+
+ String cdcVal = rs.getString(3);
+ Map<String, Object> map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ Map<String, Object> preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ Assert.assertNull(preImage.get("COL"));
+ Map<String, Object> postImage =
+ (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ String encodedBytes = (String) postImage.get("COL");
+ byte[] bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument r1 = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument1, r1);
+
+ Assert.assertTrue(rs.next());
+
+ cdcVal = rs.getString(3);
+ map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ Assert.assertNull(preImage.get("COL"));
+ postImage = (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ encodedBytes = (String) postImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument r2 = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument2, r2);
+
+ Assert.assertTrue(rs.next());
+
+ cdcVal = rs.getString(3);
+ map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ Assert.assertNull(preImage.get("COL"));
+ postImage = (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ encodedBytes = (String) postImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument r3 = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument3, r3);
+
+ Assert.assertFalse(rs.next());
+ }
+ }
+
+ private static void testCDCPostUpdate(Connection conn, String cdcName, Timestamp ts1,
+ Timestamp ts2, BsonDocument bsonDocument1,
+ BsonDocument bsonDocument2,
+ BsonDocument bsonDocument3)
+ throws SQLException, IOException {
+ ResultSet rs;
+ try (PreparedStatement pst = conn.prepareStatement(
+ "SELECT /*+ CDC_INCLUDE(PRE, POST) */ * FROM " + cdcName +
+ " WHERE PHOENIX_ROW_TIMESTAMP() >= ? AND PHOENIX_ROW_TIMESTAMP() <= ?")) {
+ pst.setTimestamp(1, ts1);
+ pst.setTimestamp(2, ts2);
+
+ rs = pst.executeQuery();
+ Assert.assertTrue(rs.next());
+
+ String cdcVal = rs.getString(3);
+ Map<String, Object> map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ Map<String, Object> preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ String encodedBytes = (String) preImage.get("COL");
+ byte[] bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument preDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument1, preDoc);
+
+ Map<String, Object> postImage =
+ (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ encodedBytes = (String) postImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument postDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(RawBsonDocument.parse(getJsonString("json/sample_updated_01.json")),
+ postDoc);
+
+ Assert.assertTrue(rs.next());
+
+ cdcVal = rs.getString(3);
+ map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ encodedBytes = (String) preImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ preDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument2, preDoc);
+
+ postImage = (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ encodedBytes = (String) postImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ postDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(RawBsonDocument.parse(getJsonString("json/sample_updated_02.json")),
+ postDoc);
+
+ Assert.assertTrue(rs.next());
+
+ cdcVal = rs.getString(3);
+ map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ encodedBytes = (String) preImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ preDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument3, preDoc);
+
+ postImage = (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ encodedBytes = (String) postImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ postDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(RawBsonDocument.parse(getJsonString("json/sample_updated_03.json")),
+ postDoc);
+
+ Assert.assertFalse(rs.next());
+ }
+ }
+
+ private static void testCDCUpdateOneRowChange(Connection conn, String cdcName, Timestamp ts1,
+ Timestamp ts2, BsonDocument bsonDocument1)
+ throws SQLException, IOException {
+ ResultSet rs;
+ try (PreparedStatement pst = conn.prepareStatement(
+ "SELECT /*+ CDC_INCLUDE(PRE, POST) */ * FROM " + cdcName +
+ " WHERE PHOENIX_ROW_TIMESTAMP() >= ? AND PHOENIX_ROW_TIMESTAMP() <= ?")) {
+ pst.setTimestamp(1, ts1);
+ pst.setTimestamp(2, ts2);
+
+ rs = pst.executeQuery();
+ Assert.assertTrue(rs.next());
+
+ String cdcVal = rs.getString(3);
+ Map<String, Object> map = OBJECT_MAPPER.readValue(cdcVal, Map.class);
+ Map<String, Object> preImage = (Map<String, Object>) map.get(QueryConstants.CDC_PRE_IMAGE);
+ String encodedBytes = (String) preImage.get("COL");
+ byte[] bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument preDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument1, preDoc);
+
+ Map<String, Object> postImage =
+ (Map<String, Object>) map.get(QueryConstants.CDC_POST_IMAGE);
+ encodedBytes = (String) postImage.get("COL");
+ bytes = Base64.getDecoder().decode(encodedBytes);
+ RawBsonDocument postDoc = new RawBsonDocument(bytes, 0, bytes.length);
+ Assert.assertEquals(bsonDocument1, postDoc);
+
+ Assert.assertFalse(rs.next());
+ }
+ }
+
+ /**
+ * Conditional Upserts for BSON pass where the Condition Expression is of Document style.
+ */
+ @Test
+ public void testBsonOpsWithDocumentConditionsUpdateSuccess() throws Exception {
+ Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+ String tableName = generateUniqueName();
+ String cdcName = 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))";
+ String cdcDdl = "CREATE CDC " + cdcName + " ON " + tableName;
+ conn.createStatement().execute(ddl);
+ conn.createStatement().execute(cdcDdl);
+ IndexToolIT.runIndexTool(false, "", tableName,
+ "\"" + CDCUtil.getCDCIndexName(cdcName) + "\"");
+ Timestamp ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ String sample1 = getJsonString("json/sample_01.json");
+ String sample2 = getJsonString("json/sample_02.json");
+ String sample3 = getJsonString("json/sample_03.json");
+ BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+ BsonDocument bsonDocument2 = RawBsonDocument.parse(sample2);
+ BsonDocument bsonDocument3 = RawBsonDocument.parse(sample3);
+
+ PreparedStatement stmt =
+ conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0002");
+ stmt.setObject(3, bsonDocument1);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1010");
+ stmt.setString(2, "1010");
+ stmt.setObject(3, bsonDocument2);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1011");
+ stmt.setString(2, "1011");
+ stmt.setObject(3, bsonDocument3);
+ stmt.executeUpdate();
+
+ conn.commit();
+
+ Thread.sleep(100);
+ Timestamp ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCAfterFirstUpsert(conn, cdcName, ts1, ts2, bsonDocument1, bsonDocument2, bsonDocument3);
+
+ ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ //{
+ // "$and": [
+ // {
+ // "press": {
+ // "$eq": "beat"
+ // }
+ // },
+ // {
+ // "track[0].shot[2][0].city.standard[50]": {
+ // "$eq": "softly"
+ // }
+ // }
+ // ]
+ //}
+ BsonDocument conditionDoc = new BsonDocument();
+ BsonArray andList1 = new BsonArray();
+ andList1.add(new BsonDocument()
+ .append("press", new BsonDocument()
+ .append("$eq", new BsonString("beat"))));
+ andList1.add(new BsonDocument()
+ .append("track[0].shot[2][0].city.standard[50]", new BsonDocument()
+ .append("$eq", new BsonString("softly"))));
+ conditionDoc.put("$and", andList1);
+
+ String query = "SELECT * FROM " + tableName +
+ " WHERE PK1 = 'pk0001' AND C1 = '0002' AND NOT BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0002", rs.getString(2));
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+ //{
+ // "$and": [
+ // {
+ // "press": {
+ // "$eq": "beat"
+ // }
+ // },
+ // {
+ // "track[0].shot[2][0].city.standard[5]": {
+ // "$eq": "softly"
+ // }
+ // }
+ // ]
+ //}
+ conditionDoc = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument()
+ .append("press", new BsonDocument()
+ .append("$eq", new BsonString("beat"))));
+ andList1.add(new BsonDocument()
+ .append("track[0].shot[2][0].city.standard[5]", new BsonDocument()
+ .append("$eq", new BsonString("softly"))));
+ conditionDoc.put("$and", andList1);
+
+ query = "SELECT * FROM " + tableName +
+ " WHERE PK1 = 'pk0001' AND C1 = '0002' AND BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0002", rs.getString(2));
+ document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+ //{
+ // "$SET": {
+ // "browserling": {
+ // "$binary": {
+ // "base64": "PkHjukNzgcg=",
+ // "subType": "00"
+ // }
+ // },
+ // "track[0].shot[2][0].city.standard[5]": "soft",
+ // "track[0].shot[2][0].city.problem[2]": "track[0].shot[2][0].city.problem[2] + 529.435"
+ // },
+ // "$UNSET": {
+ // "track[0].shot[2][0].city.flame": null
+ // }
+ //}
+ BsonDocument updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("browserling",
+ new BsonBinary(PDouble.INSTANCE.toBytes(-505169340.54880095)))
+ .append("track[0].shot[2][0].city.standard[5]", new BsonString("soft"))
+ .append("track[0].shot[2][0].city.problem[2]",
+ new BsonString("track[0].shot[2][0].city.problem[2] + 529.435")))
+ .append("$UNSET", new BsonDocument()
+ .append("track[0].shot[2][0].city.flame", new BsonNull()));
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END,"
+ + " C1 = ?");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0003");
+ stmt.executeUpdate();
+
+ //{
+ // "$ADD": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTA=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTM=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTQ=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$DELETE_FROM_SET": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDM=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$SET": {
+ // "newrecord": {
+ // "speed": "sun",
+ // "shot": [
+ // "fun",
+ // true,
+ // [
+ // {
+ // "character": 413968576,
+ // "earth": "helpful",
+ // "money": false,
+ // "city": {
+ // "softly": "service",
+ // "standard": [
+ // "pour",
+ // false,
+ // true,
+ // true,
+ // true,
+ // "softly",
+ // "happened"
+ // ],
+ // "problem": [
+ // -687102682.7731872,
+ // "tightly",
+ // 1527061470.2690287,
+ // {
+ // "condition": "else",
+ // "higher": 1462910924.088698,
+ // "scene": false,
+ // "safety": 240784722.66658115,
+ // "catch": false,
+ // "behavior": true,
+ // "protection": true
+ // },
+ // "torn",
+ // false,
+ // "eat"
+ // ],
+ // "flame": 1066643931,
+ // "rest": -1053428849,
+ // "additional": -442539394.7937908,
+ // "brought": "rock"
+ // },
+ // "upward": -1306729583.8727202,
+ // "sky": "act",
+ // "height": true
+ // },
+ // -2042805074.4290242,
+ // "settlers",
+ // 1455555511.4875226,
+ // -1448763321,
+ // false,
+ // 379701747
+ // ],
+ // false,
+ // 1241794365,
+ // "capital",
+ // false
+ // ],
+ // "hidden": false,
+ // "truth": "south",
+ // "other": true,
+ // "disease": "disease"
+ // }
+ // },
+ // "$UNSET": {
+ // "rather[3].outline.halfway.so[2][2]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$ADD", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample10")),
+ new BsonBinary(Bytes.toBytes("Sample12")),
+ new BsonBinary(Bytes.toBytes("Sample13")),
+ new BsonBinary(Bytes.toBytes("Sample14"))
+ )))))
+ .append("$DELETE_FROM_SET", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample02")),
+ new BsonBinary(Bytes.toBytes("Sample03"))
+ )))))
+ .append("$SET", new BsonDocument()
+ .append("newrecord", ((BsonArray) (document1.get("track"))).get(0)))
+ .append("$UNSET", new BsonDocument()
+ .append("rather[3].outline.halfway.so[2][2]", new BsonNull()));
+
+ //{
+ // "$and": [
+ // {
+ // "press": {
+ // "$exists": false
+ // }
+ // },
+ // {
+ // "rather[3].outline.halfway.so[2][2]": {
+ // "$exists": true
+ // }
+ // }
+ // ]
+ //}
+ conditionDoc = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument()
+ .append("press", new BsonDocument()
+ .append("$exists", new BsonBoolean(false))));
+ andList1.add(new BsonDocument()
+ .append("rather[3].outline.halfway.so[2][2]", new BsonDocument()
+ .append("$exists", new BsonBoolean(true))));
+ conditionDoc.put("$and", andList1);
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1010");
+ stmt.executeUpdate();
+
+ //{
+ // "$SET": {
+ // "result[1].location.state": "AK"
+ // },
+ // "$UNSET": {
+ // "result[4].emails[1]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("result[1].location.state", new BsonString("AK")))
+ .append("$UNSET", new BsonDocument()
+ .append("result[4].emails[1]", new BsonNull()));
+
+ //{
+ // "$or": [
+ // {
+ // "result[2].location.coordinates.latitude": {
+ // "$gt": 0
+ // }
+ // },
+ // {
+ // "$and": [
+ // {
+ // "result[1].location": {
+ // "$exists": true
+ // }
+ // },
+ // {
+ // "result[1].location.state": {
+ // "$ne": "AK"
+ // }
+ // },
+ // {
+ // "result[4].emails[1]": {
+ // "$exists": true
+ // }
+ // }
+ // ]
+ // }
+ // ]
+ //}
+ conditionDoc = new BsonDocument();
+ BsonArray orList1 = new BsonArray();
+ andList1 = new BsonArray();
+ BsonDocument andDoc1 = new BsonDocument();
+ andList1.add(new BsonDocument()
+ .append("result[1].location", new BsonDocument()
+ .append("$exists", new BsonBoolean(true))));
+ andList1.add(new BsonDocument()
+ .append("result[1].location.state", new BsonDocument()
+ .append("$ne", new BsonString("AK"))));
+ andList1.add(new BsonDocument()
+ .append("result[4].emails[1]", new BsonDocument()
+ .append("$exists", new BsonBoolean(true))));
+ andDoc1.put("$and", andList1);
+
+ orList1.add(new BsonDocument()
+ .append("result[2].location.coordinates.latitude",
+ new BsonDocument("$gt", new BsonDouble(0))));
+ orList1.add(andDoc1);
+
+ conditionDoc.put("$or", orList1);
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1011");
+ stmt.executeUpdate();
+
+ conn.commit();
+
+ Thread.sleep(100);
+ ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCPostUpdate(conn, cdcName, ts1, ts2, bsonDocument1, bsonDocument2, bsonDocument3);
+
+ query = "SELECT * FROM " + tableName;
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0003", rs.getString(2));
+ document1 = (BsonDocument) rs.getObject(3);
+
+ String updatedJson = getJsonString("json/sample_updated_01.json");
+ assertEquals(RawBsonDocument.parse(updatedJson), document1);
+
+ assertTrue(rs.next());
+ assertEquals("pk1010", rs.getString(1));
+ assertEquals("1010", rs.getString(2));
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+
+ updatedJson = getJsonString("json/sample_updated_02.json");
+ assertEquals(RawBsonDocument.parse(updatedJson), document2);
+
+ assertTrue(rs.next());
+ assertEquals("pk1011", rs.getString(1));
+ assertEquals("1011", rs.getString(2));
+ BsonDocument document3 = (BsonDocument) rs.getObject(3);
+
+ updatedJson = getJsonString("json/sample_updated_03.json");
+ assertEquals(RawBsonDocument.parse(updatedJson), document3);
+
+ assertFalse(rs.next());
+ }
+ }
+
+ /**
+ * Conditional Upserts for BSON fail i.e. BSON value is not updated, where the Condition
+ * Expression is of SQL style.
+ */
+ @Test
+ public void testBsonOpsWithSqlConditionsUpdateFailure() throws Exception {
+ Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+ String tableName = generateUniqueName();
+ String cdcName = 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))";
+ String cdcDdl = "CREATE CDC " + cdcName + " ON " + tableName;
+ conn.createStatement().execute(ddl);
+ conn.createStatement().execute(cdcDdl);
+ IndexToolIT.runIndexTool(false, "", tableName,
+ "\"" + CDCUtil.getCDCIndexName(cdcName) + "\"");
+ Timestamp ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ String sample1 = getJsonString("json/sample_01.json");
+ String sample2 = getJsonString("json/sample_02.json");
+ String sample3 = getJsonString("json/sample_03.json");
+ BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+ BsonDocument bsonDocument2 = RawBsonDocument.parse(sample2);
+ BsonDocument bsonDocument3 = RawBsonDocument.parse(sample3);
+
+ PreparedStatement stmt =
+ conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0002");
+ stmt.setObject(3, bsonDocument1);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1010");
+ stmt.setString(2, "1010");
+ stmt.setObject(3, bsonDocument2);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1011");
+ stmt.setString(2, "1011");
+ stmt.setObject(3, bsonDocument3);
+ stmt.executeUpdate();
+
+ conn.commit();
+
+ Thread.sleep(100);
+ Timestamp ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCAfterFirstUpsert(conn, cdcName, ts1, ts2, bsonDocument1, bsonDocument2, bsonDocument3);
+
+ Thread.sleep(100);
+ ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ String conditionExpression =
+ "press = $press AND track[0].shot[2][0].city.standard[50] = $softly";
+
+ //{
+ // "$EXPR": "press = $press AND track[0].shot[2][0].city.standard[50] = $softly",
+ // "$VAL": {
+ // "$press": "beat",
+ // "$softly": "softly"
+ // }
+ //}
+ BsonDocument conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument()
+ .append("$press", new BsonString("beat"))
+ .append("$softly", new BsonString("softly")));
+
+ String query = "SELECT * FROM " + tableName +
+ " WHERE PK1 = 'pk0001' AND C1 = '0002' AND NOT BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0002", rs.getString(2));
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+ conditionExpression =
+ "press = $press AND track[0].shot[2][0].city.standard[5] <> $softly";
+
+ //{
+ // "$EXPR": "press = $press AND track[0].shot[2][0].city.standard[5] <> $softly",
+ // "$VAL": {
+ // "$press": "beat",
+ // "$softly": "softly"
+ // }
+ //}
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument()
+ .append("$press", new BsonString("beat"))
+ .append("$softly", new BsonString("softly")));
+
+ //{
+ // "$SET": {
+ // "browserling": {
+ // "$binary": {
+ // "base64": "PkHjukNzgcg=",
+ // "subType": "00"
+ // }
+ // },
+ // "track[0].shot[2][0].city.standard[5]": "soft",
+ // "track[0].shot[2][0].city.problem[2]": "track[0].shot[2][0].city.problem[2] + 529.435"
+ // },
+ // "$UNSET": {
+ // "track[0].shot[2][0].city.flame": null
+ // }
+ //}
+ BsonDocument updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("browserling",
+ new BsonBinary(PDouble.INSTANCE.toBytes(-505169340.54880095)))
+ .append("track[0].shot[2][0].city.standard[5]", new BsonString("soft"))
+ .append("track[0].shot[2][0].city.problem[2]",
+ new BsonString("track[0].shot[2][0].city.problem[2] + 529.435")))
+ .append("$UNSET", new BsonDocument()
+ .append("track[0].shot[2][0].city.flame", new BsonNull()));
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END,"
+ + " C1 = ?");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0003");
+ stmt.executeUpdate();
+
+ //{
+ // "$ADD": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTA=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTM=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTQ=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$DELETE_FROM_SET": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDM=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$SET": {
+ // "newrecord": {
+ // "speed": "sun",
+ // "shot": [
+ // "fun",
+ // true,
+ // [
+ // {
+ // "character": 413968576,
+ // "earth": "helpful",
+ // "money": false,
+ // "city": {
+ // "softly": "service",
+ // "standard": [
+ // "pour",
+ // false,
+ // true,
+ // true,
+ // true,
+ // "softly",
+ // "happened"
+ // ],
+ // "problem": [
+ // -687102682.7731872,
+ // "tightly",
+ // 1527061470.2690287,
+ // {
+ // "condition": "else",
+ // "higher": 1462910924.088698,
+ // "scene": false,
+ // "safety": 240784722.66658115,
+ // "catch": false,
+ // "behavior": true,
+ // "protection": true
+ // },
+ // "torn",
+ // false,
+ // "eat"
+ // ],
+ // "flame": 1066643931,
+ // "rest": -1053428849,
+ // "additional": -442539394.7937908,
+ // "brought": "rock"
+ // },
+ // "upward": -1306729583.8727202,
+ // "sky": "act",
+ // "height": true
+ // },
+ // -2042805074.4290242,
+ // "settlers",
+ // 1455555511.4875226,
+ // -1448763321,
+ // false,
+ // 379701747
+ // ],
+ // false,
+ // 1241794365,
+ // "capital",
+ // false
+ // ],
+ // "hidden": false,
+ // "truth": "south",
+ // "other": true,
+ // "disease": "disease"
+ // }
+ // },
+ // "$UNSET": {
+ // "rather[3].outline.halfway.so[2][2]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$ADD", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample10")),
+ new BsonBinary(Bytes.toBytes("Sample12")),
+ new BsonBinary(Bytes.toBytes("Sample13")),
+ new BsonBinary(Bytes.toBytes("Sample14"))
+ )))))
+ .append("$DELETE_FROM_SET", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample02")),
+ new BsonBinary(Bytes.toBytes("Sample03"))
+ )))))
+ .append("$SET", new BsonDocument()
+ .append("newrecord", ((BsonArray) (document1.get("track"))).get(0)))
+ .append("$UNSET", new BsonDocument()
+ .append("rather[3].outline.halfway.so[2][2]", new BsonNull()));
+
+ conditionExpression =
+ "field_not_exists(newrecord) AND field_exists(rather[3].outline.halfway.so[2][20])";
+
+ //{
+ // "$EXPR": "field_not_exists(newrecord) AND field_exists(rather[3].outline.halfway.so[2][20])",
+ // "$VAL": {}
+ //}
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument());
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1010");
+ stmt.executeUpdate();
+
+ //{
+ // "$SET": {
+ // "result[1].location.state": "AK"
+ // },
+ // "$UNSET": {
+ // "result[4].emails[1]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("result[1].location.state", new BsonString("AK")))
+ .append("$UNSET", new BsonDocument()
+ .append("result[4].emails[1]", new BsonNull()));
+
+ conditionExpression =
+ "result[2].location.coordinates.latitude > $latitude OR "
+ + "(field_exists(result[1].location) AND result[1].location.state != $state" +
+ " AND field_not_exists(result[4].emails[1]))";
+
+ //{
+ // "$EXPR": "result[2].location.coordinates.latitude > $latitude OR (field_exists(result[1].location) AND result[1].location.state != $state AND field_exists(result[4].emails[1]))",
+ // "$VAL": {
+ // "$latitude": 0,
+ // "$state": "AK"
+ // }
+ //}
+ conditionDoc = new BsonDocument();
+ conditionDoc.put("$EXPR", new BsonString(conditionExpression));
+ conditionDoc.put("$VAL", new BsonDocument()
+ .append("$latitude", new BsonDouble(0))
+ .append("$state", new BsonString("AK")));
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1011");
+ stmt.executeUpdate();
+
+ conn.commit();
+
+ Thread.sleep(100);
+ ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCUpdateOneRowChange(conn, cdcName, ts1, ts2, bsonDocument1);
+
+ query = "SELECT * FROM " + tableName;
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0003", rs.getString(2));
+ document1 = (BsonDocument) rs.getObject(3);
+
+ assertEquals(bsonDocument1, document1);
+
+ assertTrue(rs.next());
+ assertEquals("pk1010", rs.getString(1));
+ assertEquals("1010", rs.getString(2));
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+
+ assertEquals(bsonDocument2, document2);
+
+ assertTrue(rs.next());
+ assertEquals("pk1011", rs.getString(1));
+ assertEquals("1011", rs.getString(2));
+ BsonDocument document3 = (BsonDocument) rs.getObject(3);
+
+ assertEquals(bsonDocument3, document3);
+
+ assertFalse(rs.next());
+ }
+ }
+
+ /**
+ * Conditional Upserts for BSON fail i.e. BSON value is not updated, where the Condition
+ * Expression is of Document style.
+ */
+ @Test
+ public void testBsonOpsWithDocumentConditionsUpdateFailure() throws Exception {
+ Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+ String tableName = generateUniqueName();
+ String cdcName = 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))";
+ String cdcDdl = "CREATE CDC " + cdcName + " ON " + tableName;
+ conn.createStatement().execute(ddl);
+ conn.createStatement().execute(cdcDdl);
+ IndexToolIT.runIndexTool(false, "", tableName,
+ "\"" + CDCUtil.getCDCIndexName(cdcName) + "\"");
+ Timestamp ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ String sample1 = getJsonString("json/sample_01.json");
+ String sample2 = getJsonString("json/sample_02.json");
+ String sample3 = getJsonString("json/sample_03.json");
+ BsonDocument bsonDocument1 = RawBsonDocument.parse(sample1);
+ BsonDocument bsonDocument2 = RawBsonDocument.parse(sample2);
+ BsonDocument bsonDocument3 = RawBsonDocument.parse(sample3);
+
+ PreparedStatement stmt =
+ conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0002");
+ stmt.setObject(3, bsonDocument1);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1010");
+ stmt.setString(2, "1010");
+ stmt.setObject(3, bsonDocument2);
+ stmt.executeUpdate();
+
+ stmt.setString(1, "pk1011");
+ stmt.setString(2, "1011");
+ stmt.setObject(3, bsonDocument3);
+ stmt.executeUpdate();
+
+ conn.commit();
+
+ Thread.sleep(100);
+ Timestamp ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCAfterFirstUpsert(conn, cdcName, ts1, ts2, bsonDocument1, bsonDocument2, bsonDocument3);
+
+ Thread.sleep(100);
+ ts1 = new Timestamp(System.currentTimeMillis());
+ Thread.sleep(100);
+
+ //{
+ // "$and": [
+ // {
+ // "press": {
+ // "$eq": "beat"
+ // }
+ // },
+ // {
+ // "track[0].shot[2][0].city.standard[50]": {
+ // "$eq": "softly"
+ // }
+ // }
+ // ]
+ //}
+ BsonDocument conditionDoc = new BsonDocument();
+ BsonArray andList1 = new BsonArray();
+ andList1.add(new BsonDocument()
+ .append("press", new BsonDocument()
+ .append("$eq", new BsonString("beat"))));
+ andList1.add(new BsonDocument()
+ .append("track[0].shot[2][0].city.standard[50]", new BsonDocument()
+ .append("$eq", new BsonString("softly"))));
+ conditionDoc.put("$and", andList1);
+
+ String query = "SELECT * FROM " + tableName +
+ " WHERE PK1 = 'pk0001' AND C1 = '0002' AND NOT BSON_CONDITION_EXPRESSION(COL, '"
+ + conditionDoc.toJson() + "')";
+ ResultSet rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0002", rs.getString(2));
+ BsonDocument document1 = (BsonDocument) rs.getObject(3);
+ assertEquals(bsonDocument1, document1);
+
+ assertFalse(rs.next());
+
+ //{
+ // "$and": [
+ // {
+ // "press": {
+ // "$eq": "beat"
+ // }
+ // },
+ // {
+ // "track[0].shot[2][0].city.standard[5]": {
+ // "$ne": "softly"
+ // }
+ // }
+ // ]
+ //}
+ conditionDoc = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument()
+ .append("press", new BsonDocument()
+ .append("$eq", new BsonString("beat"))));
+ andList1.add(new BsonDocument()
+ .append("track[0].shot[2][0].city.standard[5]", new BsonDocument()
+ .append("$ne", new BsonString("softly"))));
+ conditionDoc.put("$and", andList1);
+
+ //{
+ // "$SET": {
+ // "browserling": {
+ // "$binary": {
+ // "base64": "PkHjukNzgcg=",
+ // "subType": "00"
+ // }
+ // },
+ // "track[0].shot[2][0].city.standard[5]": "soft",
+ // "track[0].shot[2][0].city.problem[2]": "track[0].shot[2][0].city.problem[2] + 529.435"
+ // },
+ // "$UNSET": {
+ // "track[0].shot[2][0].city.flame": null
+ // }
+ //}
+ BsonDocument updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("browserling",
+ new BsonBinary(PDouble.INSTANCE.toBytes(-505169340.54880095)))
+ .append("track[0].shot[2][0].city.standard[5]", new BsonString("soft"))
+ .append("track[0].shot[2][0].city.problem[2]",
+ new BsonString("track[0].shot[2][0].city.problem[2] + 529.435")))
+ .append("$UNSET", new BsonDocument()
+ .append("track[0].shot[2][0].city.flame", new BsonNull()));
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END,"
+ + " C1 = ?");
+ stmt.setString(1, "pk0001");
+ stmt.setString(2, "0003");
+ stmt.executeUpdate();
+
+ //{
+ // "$ADD": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTA=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTM=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMTQ=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$DELETE_FROM_SET": {
+ // "new_samples": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDI=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "U2FtcGxlMDM=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // }
+ // },
+ // "$SET": {
+ // "newrecord": {
+ // "speed": "sun",
+ // "shot": [
+ // "fun",
+ // true,
+ // [
+ // {
+ // "character": 413968576,
+ // "earth": "helpful",
+ // "money": false,
+ // "city": {
+ // "softly": "service",
+ // "standard": [
+ // "pour",
+ // false,
+ // true,
+ // true,
+ // true,
+ // "softly",
+ // "happened"
+ // ],
+ // "problem": [
+ // -687102682.7731872,
+ // "tightly",
+ // 1527061470.2690287,
+ // {
+ // "condition": "else",
+ // "higher": 1462910924.088698,
+ // "scene": false,
+ // "safety": 240784722.66658115,
+ // "catch": false,
+ // "behavior": true,
+ // "protection": true
+ // },
+ // "torn",
+ // false,
+ // "eat"
+ // ],
+ // "flame": 1066643931,
+ // "rest": -1053428849,
+ // "additional": -442539394.7937908,
+ // "brought": "rock"
+ // },
+ // "upward": -1306729583.8727202,
+ // "sky": "act",
+ // "height": true
+ // },
+ // -2042805074.4290242,
+ // "settlers",
+ // 1455555511.4875226,
+ // -1448763321,
+ // false,
+ // 379701747
+ // ],
+ // false,
+ // 1241794365,
+ // "capital",
+ // false
+ // ],
+ // "hidden": false,
+ // "truth": "south",
+ // "other": true,
+ // "disease": "disease"
+ // }
+ // },
+ // "$UNSET": {
+ // "rather[3].outline.halfway.so[2][2]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$ADD", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample10")),
+ new BsonBinary(Bytes.toBytes("Sample12")),
+ new BsonBinary(Bytes.toBytes("Sample13")),
+ new BsonBinary(Bytes.toBytes("Sample14"))
+ )))))
+ .append("$DELETE_FROM_SET", new BsonDocument()
+ .append("new_samples",
+ new BsonDocument().append("$set",
+ new BsonArray(Arrays.asList(
+ new BsonBinary(Bytes.toBytes("Sample02")),
+ new BsonBinary(Bytes.toBytes("Sample03"))
+ )))))
+ .append("$SET", new BsonDocument()
+ .append("newrecord", ((BsonArray) (document1.get("track"))).get(0)))
+ .append("$UNSET", new BsonDocument()
+ .append("rather[3].outline.halfway.so[2][2]", new BsonNull()));
+
+ //{
+ // "$and": [
+ // {
+ // "press": {
+ // "$exists": false
+ // }
+ // },
+ // {
+ // "rather[3].outline.halfway.so[2][20]": {
+ // "$exists": true
+ // }
+ // }
+ // ]
+ //}
+ conditionDoc = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument()
+ .append("press", new BsonDocument()
+ .append("$exists", new BsonBoolean(false))));
+ andList1.add(new BsonDocument()
+ .append("rather[3].outline.halfway.so[2][20]", new BsonDocument()
+ .append("$exists", new BsonBoolean(true))));
+ conditionDoc.put("$and", andList1);
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1010");
+ stmt.executeUpdate();
+
+ //{
+ // "$SET": {
+ // "result[1].location.state": "AK"
+ // },
+ // "$UNSET": {
+ // "result[4].emails[1]": null
+ // }
+ //}
+ updateExp = new BsonDocument()
+ .append("$SET", new BsonDocument()
+ .append("result[1].location.state", new BsonString("AK")))
+ .append("$UNSET", new BsonDocument()
+ .append("result[4].emails[1]", new BsonNull()));
+
+ //{
+ // "$or": [
+ // {
+ // "result[2].location.coordinates.latitude": {
+ // "$gt": 0
+ // }
+ // },
+ // {
+ // "$and": [
+ // {
+ // "result[1].location": {
+ // "$exists": true
+ // }
+ // },
+ // {
+ // "result[1].location.state": {
+ // "$ne": "AK"
+ // }
+ // },
+ // {
+ // "result[4].emails[1]": {
+ // "$exists": false
+ // }
+ // }
+ // ]
+ // }
+ // ]
+ //}
+ conditionDoc = new BsonDocument();
+ BsonArray orList1 = new BsonArray();
+ andList1 = new BsonArray();
+ BsonDocument andDoc1 = new BsonDocument();
+ andList1.add(new BsonDocument()
+ .append("result[1].location", new BsonDocument()
+ .append("$exists", new BsonBoolean(true))));
+ andList1.add(new BsonDocument()
+ .append("result[1].location.state", new BsonDocument()
+ .append("$ne", new BsonString("AK"))));
+ andList1.add(new BsonDocument()
+ .append("result[4].emails[1]", new BsonDocument()
+ .append("$exists", new BsonBoolean(false))));
+ andDoc1.put("$and", andList1);
+
+ orList1.add(new BsonDocument()
+ .append("result[2].location.coordinates.latitude",
+ new BsonDocument("$gt", new BsonDouble(0))));
+ orList1.add(andDoc1);
+
+ conditionDoc.put("$or", orList1);
+
+ stmt = conn.prepareStatement("UPSERT INTO " + tableName
+ + " VALUES (?) ON DUPLICATE KEY UPDATE COL = CASE WHEN"
+ + " BSON_CONDITION_EXPRESSION(COL, '" + conditionDoc.toJson() + "')"
+ + " THEN BSON_UPDATE_EXPRESSION(COL, '" + updateExp + "') ELSE COL END");
+
+ stmt.setString(1, "pk1011");
+ stmt.executeUpdate();
+
+ conn.commit();
+
+ Thread.sleep(100);
+ ts2 = new Timestamp(System.currentTimeMillis());
+
+ testCDCUpdateOneRowChange(conn, cdcName, ts1, ts2, bsonDocument1);
+
+ query = "SELECT * FROM " + tableName;
+ rs = conn.createStatement().executeQuery(query);
+
+ assertTrue(rs.next());
+ assertEquals("pk0001", rs.getString(1));
+ assertEquals("0003", rs.getString(2));
+ document1 = (BsonDocument) rs.getObject(3);
+
+ assertEquals(bsonDocument1, document1);
+
+ assertTrue(rs.next());
+ assertEquals("pk1010", rs.getString(1));
+ assertEquals("1010", rs.getString(2));
+ BsonDocument document2 = (BsonDocument) rs.getObject(3);
+
+ assertEquals(bsonDocument2, document2);
+
+ assertTrue(rs.next());
+ assertEquals("pk1011", rs.getString(1));
+ assertEquals("1011", rs.getString(2));
+ BsonDocument document3 = (BsonDocument) rs.getObject(3);
+
+ assertEquals(bsonDocument3, document3);
+
+ assertFalse(rs.next());
+ }
+ }
+
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/util/bson/SerializableBytesPtr.java b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/SerializableBytesPtr.java
new file mode 100644
index 0000000000..83988c8284
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/SerializableBytesPtr.java
@@ -0,0 +1,66 @@
+/*
+ * 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.util.bson;
+
+import java.io.Serializable;
+
+import org.apache.hadoop.hbase.util.Bytes;
+
+public class SerializableBytesPtr
+ implements Serializable, Comparable<SerializableBytesPtr> {
+
+ private byte[] b;
+
+ public SerializableBytesPtr() {
+ }
+
+ public SerializableBytesPtr(byte[] b) {
+ this.b = b;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ SerializableBytesPtr that = (SerializableBytesPtr) o;
+ return Bytes.equals(b, that.b);
+ }
+
+ @Override
+ public int hashCode() {
+ return Bytes.hashCode(b);
+ }
+
+ @Override
+ public int compareTo(SerializableBytesPtr o) {
+ return Bytes.compareTo(this.b, o.b);
+ }
+
+ public void setB(byte[] b) {
+ this.b = b;
+ }
+
+ public byte[] getB() {
+ return b;
+ }
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestFieldValue.java b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestFieldValue.java
new file mode 100644
index 0000000000..85bbfadc92
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestFieldValue.java
@@ -0,0 +1,961 @@
+/*
+ * 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.util.bson;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+
+/**
+ * The value of specific data type (scalar or recursive structure).
+ */
+public class TestFieldValue implements Serializable, Cloneable {
+
+ /**
+ * An attribute of type String.
+ */
+ private String varchar;
+ /**
+ * An attribute of type Number.
+ */
+ private Number number;
+ /**
+ * An attribute of type Binary.
+ */
+ private SerializableBytesPtr binary;
+ /**
+ * An attribute of type String Set.
+ */
+ private Set<String> varcharset;
+ /**
+ * An attribute of type Number Set.
+ */
+ private Set<Number> numberset;
+ /**
+ * An attribute of type Binary Set.
+ */
+ private Set<SerializableBytesPtr> binaryset;
+ /**
+ * An attribute of type Map.
+ */
+ private Map<String, TestFieldValue> map;
+ /**
+ * An attribute of type List.
+ */
+ private List<TestFieldValue> list;
+ /**
+ * An attribute of type Null.
+ */
+ private Boolean nULLValue;
+ /**
+ * An attribute of type Boolean.
+ */
+ private Boolean bool;
+
+ /**
+ * Default constructor for TestFieldValue object. Callers should use the setter or fluent setter (with...) methods
+ * to initialize the object after creating it.
+ */
+ public TestFieldValue() {
+ }
+
+ /**
+ * Constructs a new TestFieldValue object. Callers should use the setter or fluent setter (with...) methods to
+ * initialize any additional object members.
+ *
+ * @param varchar An attribute of type String. For example:</p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ */
+ public TestFieldValue(String varchar) {
+ setVarchar(varchar);
+ }
+
+ /**
+ * Constructs a new TestFieldValue object. Callers should use the setter or fluent setter (with...) methods to
+ * initialize any additional object members.
+ *
+ * @param varcharset An attribute of type String Set. For example:</p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ */
+ public TestFieldValue(List<String> varcharset) {
+ setVarcharset(varcharset);
+ }
+
+ /**
+ * <p>
+ * An attribute of type String. For example:
+ * </p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ * </p>
+ *
+ * @param s An attribute of type String. For example:</p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ */
+
+ public void setVarchar(String s) {
+ this.varchar = s;
+ }
+
+ /**
+ * <p>
+ * An attribute of type String. For example:
+ * </p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ * </p>
+ *
+ * @return An attribute of type String. For example:</p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ */
+
+ public String getVarchar() {
+ return this.varchar;
+ }
+
+ /**
+ * <p>
+ * An attribute of type String. For example:
+ * </p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ * </p>
+ *
+ * @param s An attribute of type String. For example:</p>
+ * <p>
+ * <code>"S": "Hello"</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withS(String s) {
+ setVarchar(s);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number. For example:
+ * </p>
+ * <p>
+ * <code>"N": "123.45"</code>
+ * </p>
+ *
+ * @param number An attribute of type Number. For example:</p>
+ * <p>
+ * <code>"N": "123.45"</code>
+ * </p>
+ */
+
+ public void setNumber(Number number) {
+ this.number = number;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number. For example:
+ * </p>
+ * <p>
+ * <code>"N": "123.45"</code>
+ * </p>
+ *
+ * @return An attribute of type Number. For example:</p>
+ * <p>
+ * <code>"N": "123.45"</code>
+ * </p>
+ */
+
+ public Number getNumber() {
+ return this.number;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number. For example:
+ * </p>
+ * <p>
+ * <code>"N": "123.45"</code>
+ * </p>
+ *
+ * @param n An attribute of type Number. For example:</p>
+ * <p>
+ * <code>"N": "123.45"</code>
+ * </p>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withN(Number n) {
+ setNumber(n);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary. For example:
+ * </p>
+ * <p>
+ * <code>"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"</code>
+ * </p>
+ * <p>
+ * This String represents Base64 encoded bytes.
+ * </p>
+ *
+ * @param binary An attribute of type Binary. For example:</p>
+ * <p>
+ * <code>"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"</code>
+ */
+ public void setBinary(SerializableBytesPtr binary) {
+ this.binary = binary;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary. For example:
+ * </p>
+ * <p>
+ * <code>"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"</code>
+ * </p>
+ * <p>
+ * This String represents Base64 encoded bytes.
+ * </p>
+ *
+ * @return An attribute of type Binary. For example:</p>
+ * <p>
+ * <code>"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"</code>
+ */
+ public SerializableBytesPtr getBinary() {
+ return this.binary;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary. For example:
+ * </p>
+ * <p>
+ * <code>"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"</code>
+ * </p>
+ * <p>
+ * This String represents Base64 encoded bytes.
+ * </p>
+ *
+ * @param b An attribute of type Binary. For example:</p>
+ * <p>
+ * <code>"B": "dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk"</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withB(SerializableBytesPtr b) {
+ setBinary(b);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type String Set. For example:
+ * </p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ * </p>
+ *
+ * @return An attribute of type String Set. For example:</p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ */
+ public Set<String> getVarcharset() {
+ return varcharset;
+ }
+
+ /**
+ * <p>
+ * An attribute of type String Set. For example:
+ * </p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ * </p>
+ *
+ * @param sS An attribute of type String Set. For example:</p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ */
+ public void setVarcharset(Collection<String> sS) {
+ if (sS == null) {
+ this.varcharset = null;
+ return;
+ }
+
+ this.varcharset = new HashSet<>(sS);
+ }
+
+ /**
+ * <p>
+ * An attribute of type String Set. For example:
+ * </p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ * </p>
+ * <p>
+ * <b>NOTE:</b> This method appends the values to the existing list (if any). Use
+ * {@link #setVarcharset(Collection)} or {@link #withSS(Collection)} if you want to override the
+ * existing values.
+ * </p>
+ *
+ * @param sS An attribute of type String Set. For example:</p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withSS(String... sS) {
+ if (this.varcharset == null) {
+ setVarcharset(new ArrayList<String>(sS.length));
+ }
+ for (String ele : sS) {
+ this.varcharset.add(ele);
+ }
+ Preconditions.checkArgument(new HashSet<>(this.varcharset).size() == this.varcharset.size(),
+ "Set should not contain duplicate elements");
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type String Set. For example:
+ * </p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ * </p>
+ *
+ * @param sS An attribute of type String Set. For example:</p>
+ * <p>
+ * <code>"SS": ["Giraffe", "Hippo" ,"Zebra"]</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withSS(Collection<String> sS) {
+ setVarcharset(sS);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number Set. For example:
+ * </p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ *
+ * @return An attribute of type Number Set. For example:</p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ */
+ public Set<Number> getNumberset() {
+ return numberset;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number Set. For example:
+ * </p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ *
+ * @param nS An attribute of type Number Set. For example:</p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ */
+ public void setNumberset(Collection<Number> nS) {
+ if (nS == null) {
+ this.numberset = null;
+ return;
+ }
+
+ this.numberset = new HashSet<>(nS);
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number Set. For example:
+ * </p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ * <p>
+ * <b>NOTE:</b> This method appends the values to the existing list (if any). Use
+ * {@link #setNumberset(Collection)} or {@link #withNS(Collection)} if you want to override the
+ * existing values.
+ * </p>
+ *
+ * @param nS An attribute of type Number Set. For example:</p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withNS(Number... nS) {
+ if (this.numberset == null) {
+ setNumberset(new ArrayList<>(nS.length));
+ }
+ for (Number ele : nS) {
+ this.numberset.add(ele);
+ }
+ Preconditions.checkArgument(new HashSet<>(this.numberset).size() == this.numberset.size(),
+ "Set should not contain duplicate elements");
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Number Set. For example:
+ * </p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ *
+ * @param nS An attribute of type Number Set. For example:</p>
+ * <p>
+ * <code>"NS": ["42.2", "-19", "7.5", "3.14"]</code>
+ * </p>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withNS(Collection<Number> nS) {
+ setNumberset(nS);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary Set. For example:
+ * </p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ * </p>
+ *
+ * @return An attribute of type Binary Set. For example:</p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ */
+ public Set<SerializableBytesPtr> getBinaryset() {
+ return binaryset;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary Set. For example:
+ * </p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ * </p>
+ * <p>
+ * The String represents Base64 encoded bytes.
+ * </p>
+ *
+ * @param bS An attribute of type Binary Set. For example:</p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ */
+ public void setBinaryset(Collection<SerializableBytesPtr> bS) {
+ if (bS == null) {
+ this.binaryset = null;
+ return;
+ }
+
+ this.binaryset = new HashSet<>(bS);
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary Set. For example:
+ * </p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ * </p>
+ * <p>
+ * <b>NOTE:</b> This method appends the values to the existing list (if any). Use
+ * {@link #setBinaryset(Collection)} or {@link #withBS(Collection)} if you want to override the
+ * existing values.
+ * </p>
+ *
+ * @param bS An attribute of type Binary Set. For example:</p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withBS(SerializableBytesPtr... bS) {
+ if (this.binaryset == null) {
+ setBinaryset(new ArrayList<>(bS.length));
+ }
+ for (SerializableBytesPtr ele : bS) {
+ this.binaryset.add(ele);
+ }
+ Preconditions.checkArgument(new HashSet<>(this.binaryset).size() == this.binaryset.size(),
+ "Set should not contain duplicate elements");
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Binary Set. For example:
+ * </p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ * </p>
+ *
+ * @param bS An attribute of type Binary Set. For example:</p>
+ * <p>
+ * <code>"BS": ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withBS(Collection<SerializableBytesPtr> bS) {
+ setBinaryset(bS);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Map. For example:
+ * </p>
+ * <p>
+ * <code>"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}</code>
+ * </p>
+ *
+ * @return An attribute of type Map. For example:</p>
+ * <p>
+ * <code>"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}</code>
+ */
+ public Map<String, TestFieldValue> getMap() {
+ return map;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Map. For example:
+ * </p>
+ * <p>
+ * <code>"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}</code>
+ * </p>
+ *
+ * @param map An attribute of type Map. For example:</p>
+ * <p>
+ * <code>"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}</code>
+ */
+ public void setMap(Map<String, TestFieldValue> map) {
+ this.map = map;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Map. For example:
+ * </p>
+ * <p>
+ * <code>"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}</code>
+ * </p>
+ *
+ * @param m An attribute of type Map. For example:</p>
+ * <p>
+ * <code>"M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}}</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withM(Map<String, TestFieldValue> m) {
+ setMap(m);
+ return this;
+ }
+
+ /**
+ * Add a single M entry
+ *
+ * @returns a reference to this object so that method calls can be chained together.
+ * @see TestFieldValue#withM
+ */
+ public TestFieldValue addMEntry(String key, TestFieldValue value) {
+ if (null == this.map) {
+ this.map = new HashMap<>();
+ }
+ if (this.map.containsKey(key)) {
+ throw new IllegalArgumentException("Duplicated keys (" + key.toString() + ") are provided.");
+ }
+ this.map.put(key, value);
+ return this;
+ }
+
+ /**
+ * Removes all the entries added into M.
+ *
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue clearMEntries() {
+ this.map = null;
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type List. For example:
+ * </p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ * </p>
+ *
+ * @return An attribute of type List. For example:</p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ */
+
+ public List<TestFieldValue> getList() {
+ return list;
+ }
+
+ /**
+ * <p>
+ * An attribute of type List. For example:
+ * </p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ * </p>
+ *
+ * @param list An attribute of type List. For example:</p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ */
+
+ public void setList(Collection<TestFieldValue> list) {
+ if (list == null) {
+ this.list = null;
+ return;
+ }
+
+ this.list = new ArrayList<TestFieldValue>(list);
+ }
+
+ /**
+ * <p>
+ * An attribute of type List. For example:
+ * </p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ * </p>
+ * <p>
+ * <b>NOTE:</b> This method appends the values to the existing list (if any). Use
+ * {@link #setList(Collection)} or {@link #withL(Collection)} if you want to override the existing
+ * values.
+ * </p>
+ *
+ * @param l An attribute of type List. For example:</p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withL(TestFieldValue... l) {
+ if (this.list == null) {
+ setList(new ArrayList<TestFieldValue>(l.length));
+ }
+ for (TestFieldValue ele : l) {
+ this.list.add(ele);
+ }
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type List. For example:
+ * </p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ * </p>
+ *
+ * @param l An attribute of type List. For example:</p>
+ * <p>
+ * <code>"L": [ {"S": "Cookies"} , {"S": "Coffee"}, {"N": "3.14159"}]</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withL(Collection<TestFieldValue> l) {
+ setList(l);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Null. For example:
+ * </p>
+ * <p>
+ * <code>"NULL": true</code>
+ * </p>
+ *
+ * @param nULLValue An attribute of type Null. For example:</p>
+ * <p>
+ * <code>"NULL": true</code>
+ */
+
+ public void setNull(Boolean nULLValue) {
+ this.nULLValue = nULLValue;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Null. For example:
+ * </p>
+ * <p>
+ * <code>"NULL": true</code>
+ * </p>
+ *
+ * @return An attribute of type Null. For example:</p>
+ * <p>
+ * <code>"NULL": true</code>
+ */
+ public Boolean getNull() {
+ return this.nULLValue;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Null. For example:
+ * </p>
+ * <p>
+ * <code>"NULL": true</code>
+ * </p>
+ *
+ * @param nULLValue An attribute of type Null. For example:</p>
+ * <p>
+ * <code>"NULL": true</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+ public TestFieldValue withNULL(Boolean nULLValue) {
+ setNull(nULLValue);
+ return this;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Null. For example:
+ * </p>
+ * <p>
+ * <code>"NULL": true</code>
+ * </p>
+ *
+ * @return An attribute of type Null. For example:</p>
+ * <p>
+ * <code>"NULL": true</code>
+ */
+ public Boolean isNULL() {
+ return this.nULLValue;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Boolean. For example:
+ * </p>
+ * <p>
+ * <code>"BOOL": true</code>
+ * </p>
+ *
+ * @param bOOL An attribute of type Boolean. For example:</p>
+ * <p>
+ * <code>"BOOL": true</code>
+ */
+ public void setBoolean(Boolean bOOL) {
+ this.bool = bOOL;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Boolean. For example:
+ * </p>
+ * <p>
+ * <code>"BOOL": true</code>
+ * </p>
+ *
+ * @return An attribute of type Boolean. For example:</p>
+ * <p>
+ * <code>"BOOL": true</code>
+ */
+ public Boolean getBoolean() {
+ return this.bool;
+ }
+
+ /**
+ * <p>
+ * An attribute of type Boolean. For example:
+ * </p>
+ * <p>
+ * <code>"BOOL": true</code>
+ * </p>
+ *
+ * @param bOOL An attribute of type Boolean. For example:</p>
+ * <p>
+ * <code>"BOOL": true</code>
+ * @return Returns a reference to this object so that method calls can be chained together.
+ */
+
+ public TestFieldValue withBOOL(Boolean bOOL) {
+ setBoolean(bOOL);
+ return this;
+ }
+
+
+ /**
+ * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be
+ * redacted from this string using a placeholder value.
+ *
+ * @return A string representation of this object.
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("{");
+ if (getVarchar() != null)
+ sb.append("S: ").append(getVarchar());
+ if (getNumber() != null)
+ sb.append("N: ").append(getNumber());
+ if (getBinary() != null)
+ sb.append("B: ").append(getBinary());
+ if (getVarcharset() != null)
+ sb.append("SS: ").append(getVarcharset());
+ if (getNumberset() != null)
+ sb.append("NS: ").append(getNumberset());
+ if (getBinaryset() != null)
+ sb.append("BS: ").append(getBinaryset());
+ if (getMap() != null)
+ sb.append("M: ").append(getMap());
+ if (getList() != null)
+ sb.append("L: ").append(getList());
+ if (getNull() != null)
+ sb.append("NULL: ").append(getNull());
+ if (getBoolean() != null)
+ sb.append("BOOL: ").append(getBoolean());
+ sb.append("}");
+ return sb.toString();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+
+ if (!(obj instanceof TestFieldValue)) {
+ return false;
+ }
+ TestFieldValue other = (TestFieldValue) obj;
+ if (other.getVarchar() == null ^ this.getVarchar() == null) {
+ return false;
+ }
+ if (other.getVarchar() != null && !other.getVarchar().equals(this.getVarchar())) {
+ return false;
+ }
+ if (other.getNumber() == null ^ this.getNumber() == null) {
+ return false;
+ }
+ if (other.getNumber() != null && !other.getNumber().equals(this.getNumber())) {
+ return false;
+ }
+ if (other.getBinary() == null ^ this.getBinary() == null) {
+ return false;
+ }
+ if (other.getBinary() != null && !other.getBinary().equals(this.getBinary())) {
+ return false;
+ }
+ if (other.getVarcharset() == null ^ this.getVarcharset() == null) {
+ return false;
+ }
+ if (other.getVarcharset() != null && !other.getVarcharset().equals(this.getVarcharset())) {
+ return false;
+ }
+ if (other.getNumberset() == null ^ this.getNumberset() == null) {
+ return false;
+ }
+ if (other.getNumberset() != null && !other.getNumberset().equals(this.getNumberset())) {
+ return false;
+ }
+ if (other.getBinaryset() == null ^ this.getBinaryset() == null) {
+ return false;
+ }
+ if (other.getBinaryset() != null && !other.getBinaryset().equals(this.getBinaryset())) {
+ return false;
+ }
+ if (other.getMap() == null ^ this.getMap() == null) {
+ return false;
+ }
+ if (other.getMap() != null && !other.getMap().equals(this.getMap())) {
+ return false;
+ }
+ if (other.getList() == null ^ this.getList() == null) {
+ return false;
+ }
+ if (other.getList() != null && !other.getList().equals(this.getList())) {
+ return false;
+ }
+ if (other.getNull() == null ^ this.getNull() == null) {
+ return false;
+ }
+ if (other.getNull() != null && !other.getNull().equals(this.getNull())) {
+ return false;
+ }
+ if (other.getBoolean() == null ^ this.getBoolean() == null) {
+ return false;
+ }
+ if (other.getBoolean() != null && !other.getBoolean().equals(this.getBoolean())) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int hashCode = 1;
+
+ hashCode = prime * hashCode + ((getVarchar() == null) ? 0 : getVarchar().hashCode());
+ hashCode = prime * hashCode + ((getNumber() == null) ? 0 : getNumber().hashCode());
+ hashCode = prime * hashCode + ((getBinary() == null) ? 0 : getBinary().hashCode());
+ hashCode = prime * hashCode + ((getVarcharset() == null) ? 0 : getVarcharset().hashCode());
+ hashCode = prime * hashCode + ((getNumberset() == null) ? 0 : getNumberset().hashCode());
+ hashCode = prime * hashCode + ((getBinaryset() == null) ? 0 : getBinaryset().hashCode());
+ hashCode = prime * hashCode + ((getMap() == null) ? 0 : getMap().hashCode());
+ hashCode = prime * hashCode + ((getList() == null) ? 0 : getList().hashCode());
+ hashCode = prime * hashCode + ((getNull() == null) ? 0 : getNull().hashCode());
+ hashCode = prime * hashCode + ((getBoolean() == null) ? 0 : getBoolean().hashCode());
+ return hashCode;
+ }
+
+ @Override
+ public TestFieldValue clone() {
+ try {
+ return (TestFieldValue) super.clone();
+ } catch (CloneNotSupportedException e) {
+ throw new IllegalStateException(
+ "Got a CloneNotSupportedException from Object.clone() even though we're Cloneable!",
+ e);
+ }
+ }
+
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestFieldsMap.java b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestFieldsMap.java
new file mode 100644
index 0000000000..3e365c49f9
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestFieldsMap.java
@@ -0,0 +1,83 @@
+/*
+ * 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.util.bson;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Objects;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * The class representing key value pairs. Value can be any of the subtypes defined in
+ * TestFieldValue.
+ */
+public class TestFieldsMap implements Serializable {
+
+ private Map<String, TestFieldValue> map;
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ public Map<String, TestFieldValue> getMap() {
+ return map;
+ }
+
+ public void setMap(Map<String, TestFieldValue> map) {
+ this.map = map;
+ }
+
+ public String writeValueAsString() throws JsonProcessingException {
+ return MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL)
+ .writeValueAsString(this);
+ }
+
+ public String writeValueAsPrettyPrinter() throws JsonProcessingException {
+ return MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL)
+ .writerWithDefaultPrettyPrinter().writeValueAsString(this);
+ }
+
+ public byte[] writeValueAsBytes() throws JsonProcessingException {
+ return MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL)
+ .writeValueAsBytes(this);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+ TestFieldsMap that = (TestFieldsMap) o;
+ return Objects.equals(map, that.map);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(map);
+ }
+
+ @Override
+ public String toString() {
+ try {
+ return writeValueAsPrettyPrinter();
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestUtil.java b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestUtil.java
new file mode 100644
index 0000000000..64b5ef3032
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/util/bson/TestUtil.java
@@ -0,0 +1,353 @@
+/*
+ * 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.util.bson;
+
+import java.io.UncheckedIOException;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.bson.BsonArray;
+import org.bson.BsonBinary;
+import org.bson.BsonBinaryReader;
+import org.bson.BsonBoolean;
+import org.bson.BsonDecimal128;
+import org.bson.BsonDocument;
+import org.bson.BsonDocumentReader;
+import org.bson.BsonDouble;
+import org.bson.BsonInt32;
+import org.bson.BsonInt64;
+import org.bson.BsonNull;
+import org.bson.BsonNumber;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.bson.RawBsonDocument;
+import org.bson.codecs.BsonDocumentCodec;
+import org.bson.codecs.DecoderContext;
+import org.bson.codecs.RawBsonDocumentCodec;
+import org.bson.io.ByteBufferBsonInput;
+import org.bson.types.Decimal128;
+
+import org.apache.hadoop.hbase.util.Bytes;
+
+/**
+ * Test Utility class for BSON.
+ */
+public class TestUtil {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ public static byte[] getRawBsonBytes(final TestFieldsMap map) {
+ return Bytes.toBytes(getRawBsonDocument(map).getByteBuffer().asNIO());
+ }
+
+ private static BsonDocument fromRawToBsonDocument(RawBsonDocument rawDocument) {
+ try (BsonBinaryReader bsonReader = new BsonBinaryReader(
+ new ByteBufferBsonInput(rawDocument.getByteBuffer()))) {
+ return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build());
+ }
+ }
+
+ public static RawBsonDocument getRawBsonDocument(final TestFieldsMap map) {
+ BsonDocument bsonDocument = new BsonDocument();
+ for (Map.Entry<String, TestFieldValue> entry : map.getMap().entrySet()) {
+ updateBsonDocEntries(entry, bsonDocument);
+ }
+ return new RawBsonDocumentCodec().decode(new BsonDocumentReader(bsonDocument),
+ DecoderContext.builder().build());
+ }
+
+ public static BsonDocument getBsonDocument(final TestFieldsMap map) {
+ return fromRawToBsonDocument(getRawBsonDocument(map));
+ }
+
+ public static TestFieldsMap getPhoenixFieldMap(final BsonDocument bsonDocument) {
+ Map<String, TestFieldValue> map = new HashMap<>();
+ for (Map.Entry<String, BsonValue> entry : bsonDocument.entrySet()) {
+ updateMapEntries(entry, map);
+ }
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ public static TestFieldsMap getPhoenixFieldMap(byte[] bytes) {
+ return getPhoenixFieldMap(bytes, 0, bytes.length);
+ }
+
+ public static TestFieldsMap getPhoenixFieldMap(byte[] bytes, int offset, int length) {
+ RawBsonDocument rawBsonDocument = new RawBsonDocument(bytes, offset, length);
+ return getPhoenixFieldMap(rawBsonDocument);
+ }
+
+ private static void updateBsonDocEntries(Map.Entry<String, TestFieldValue> entry,
+ BsonDocument bsonDocument) {
+ TestFieldValue testFieldValue = entry.getValue();
+ bsonDocument.append(entry.getKey(), getBsonValue(testFieldValue));
+ }
+
+ private static void updateMapEntries(Map.Entry<String, BsonValue> entry,
+ Map<String, TestFieldValue> map) {
+ BsonValue bsonValue = entry.getValue();
+ map.put(entry.getKey(), getPhoenixFieldValue(bsonValue));
+ }
+
+ private static TestFieldValue getPhoenixFieldValue(BsonValue bsonValue) {
+ if (bsonValue.isString()) {
+ return new TestFieldValue().withS(((BsonString) bsonValue).getValue());
+ } else if (bsonValue.isNumber() || bsonValue.isDecimal128()) {
+ return getNumber((BsonNumber) bsonValue);
+ } else if (bsonValue.isBinary()) {
+ BsonBinary bsonBinary = (BsonBinary) bsonValue;
+ return new TestFieldValue().withB(new SerializableBytesPtr(bsonBinary.getData()));
+ } else if (bsonValue.isBoolean()) {
+ return new TestFieldValue().withBOOL(((BsonBoolean) bsonValue).getValue());
+ } else if (bsonValue.isNull()) {
+ return new TestFieldValue().withNULL(true);
+ } else if (bsonValue.isDocument()) {
+ BsonDocument bsonDocument = (BsonDocument) bsonValue;
+ if (bsonDocument.size() == 1 && bsonDocument.containsKey("$set")) {
+ BsonValue value = bsonDocument.get("$set");
+ if (!value.isArray()) {
+ throw new IllegalArgumentException("$set is reserved for Set datatype");
+ }
+ BsonArray bsonArray = (BsonArray) value;
+ if (bsonArray.isEmpty()) {
+ throw new IllegalArgumentException("Set cannot be empty");
+ }
+ BsonValue firstElement = bsonArray.get(0);
+ if (firstElement.isString()) {
+ TestFieldValue testFieldValue = new TestFieldValue().withSS();
+ bsonArray.getValues()
+ .forEach(val -> testFieldValue.withSS(((BsonString) val).getValue()));
+ return testFieldValue;
+ } else if (firstElement.isNumber() || firstElement.isDecimal128()) {
+ TestFieldValue testFieldValue = new TestFieldValue().withNS();
+ bsonArray.getValues().forEach(
+ val -> testFieldValue.withNS(getNumberFromBsonNumber((BsonNumber) val)));
+ return testFieldValue;
+ } else if (firstElement.isBinary()) {
+ TestFieldValue testFieldValue = new TestFieldValue().withBS();
+ bsonArray.getValues().forEach(val -> testFieldValue.withBS(
+ new SerializableBytesPtr(((BsonBinary) val).getData())));
+ return testFieldValue;
+ }
+ throw new IllegalArgumentException("Invalid set type");
+ } else {
+ Map<String, TestFieldValue> map = new HashMap<>();
+ for (Map.Entry<String, BsonValue> entry : bsonDocument.entrySet()) {
+ updateMapEntries(entry, map);
+ }
+ return new TestFieldValue().withM(map);
+ }
+ } else if (bsonValue.isArray()) {
+ BsonArray bsonArray = (BsonArray) bsonValue;
+ List<TestFieldValue> phoenixFieldList = new ArrayList<>();
+ for (BsonValue bsonArrayValue : bsonArray.getValues()) {
+ phoenixFieldList.add(getPhoenixFieldValue(bsonArrayValue));
+ }
+ return new TestFieldValue().withL(phoenixFieldList);
+ }
+ throw new RuntimeException("Invalid data type of BsonValue");
+ }
+
+ private static BsonValue getBsonValue(TestFieldValue testFieldValue) {
+ if (testFieldValue.getVarchar() != null) {
+ return new BsonString(testFieldValue.getVarchar());
+ } else if (testFieldValue.getNumber() != null) {
+ return getBsonNumber(testFieldValue);
+ } else if (testFieldValue.getBinary() != null) {
+ return new BsonBinary(testFieldValue.getBinary().getB());
+ } else if (testFieldValue.getBoolean() != null) {
+ return new BsonBoolean(testFieldValue.getBoolean());
+ } else if (testFieldValue.getNull() != null) {
+ return new BsonNull();
+ } else if (testFieldValue.getMap() != null) {
+ BsonDocument bsonDocument = new BsonDocument();
+ for (Map.Entry<String, TestFieldValue> entry : testFieldValue.getMap()
+ .entrySet()) {
+ updateBsonDocEntries(entry, bsonDocument);
+ }
+ return bsonDocument;
+ } else if (testFieldValue.getList() != null) {
+ BsonArray bsonArray = new BsonArray();
+ for(TestFieldValue listValue : testFieldValue.getList()) {
+ bsonArray.add(getBsonValue(listValue));
+ }
+ return bsonArray;
+ } else if (testFieldValue.getVarcharset() != null) {
+ BsonDocument bsonDocument = new BsonDocument();
+ List<BsonString> list = new ArrayList<>();
+ testFieldValue.getVarcharset().forEach(val -> list.add(new BsonString(val)));
+ bsonDocument.put("$set", new BsonArray(list));
+ return bsonDocument;
+ } else if (testFieldValue.getNumberset() != null) {
+ BsonDocument bsonDocument = new BsonDocument();
+ List<BsonNumber> list = new ArrayList<>();
+ testFieldValue.getNumberset().forEach(val -> list.add(getBsonNumberFromNumber(val)));
+ bsonDocument.put("$set", new BsonArray(list));
+ return bsonDocument;
+ } else if (testFieldValue.getBinaryset() != null) {
+ BsonDocument bsonDocument = new BsonDocument();
+ List<BsonBinary> list = new ArrayList<>();
+ testFieldValue.getBinaryset().forEach(val -> list.add(new BsonBinary(val.getB())));
+ bsonDocument.put("$set", new BsonArray(list));
+ return bsonDocument;
+ }
+ throw new RuntimeException("Invalid data type of PhoenixFieldValue");
+ }
+
+ private static BsonNumber getBsonNumberFromNumber(Number number) {
+ BsonNumber bsonNumber;
+ if (number instanceof Integer || number instanceof Short || number instanceof Byte) {
+ bsonNumber = new BsonInt32(number.intValue());
+ } else if (number instanceof Long) {
+ bsonNumber = new BsonInt64(number.longValue());
+ } else if (number instanceof Double || number instanceof Float) {
+ bsonNumber = new BsonDouble(number.doubleValue());
+ } else if (number instanceof BigDecimal) {
+ bsonNumber = new BsonDecimal128(new Decimal128((BigDecimal) number));
+ } else {
+ throw new IllegalArgumentException("Unsupported Number type: " + number.getClass());
+ }
+ return bsonNumber;
+ }
+
+ private static BsonNumber getBsonNumber(TestFieldValue testFieldValue) {
+ Number number = testFieldValue.getNumber();
+ return getBsonNumberFromNumber(number);
+ }
+
+ private static Number getNumberFromBsonNumber(BsonNumber bsonNumber) {
+ if (bsonNumber instanceof BsonInt32) {
+ return ((BsonInt32) bsonNumber).getValue();
+ } else if (bsonNumber instanceof BsonInt64) {
+ return ((BsonInt64) bsonNumber).getValue();
+ } else if (bsonNumber instanceof BsonDouble) {
+ return ((BsonDouble) bsonNumber).getValue();
+ } else if (bsonNumber instanceof BsonDecimal128) {
+ return ((BsonDecimal128) bsonNumber).getValue().bigDecimalValue();
+ } else {
+ throw new IllegalArgumentException("Unsupported BsonNumber type: " + bsonNumber.getClass());
+ }
+ }
+
+ private static TestFieldValue getNumber(BsonNumber bsonNumber) {
+ TestFieldValue testFieldValue = new TestFieldValue();
+ testFieldValue.withN(getNumberFromBsonNumber(bsonNumber));
+ return testFieldValue;
+ }
+
+ public static void main(String[] args) throws Exception {
+ TestFieldsMap map = getFieldMap1();
+ byte[] bytes = getRawBsonBytes(map);
+ TestFieldsMap map2 = getPhoenixFieldMap(bytes);
+ System.out.println(map.equals(map2));
+ }
+
+ private static TestFieldsMap getFieldMap1() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesFieldVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesFieldVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withN(101.01));
+ map.put("IdS", new TestFieldValue().withS("101.01"));
+ map.put("Id2", new TestFieldValue().withN(101.01));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "http://example.com/products/123_rear.jpg",
+ "http://example.com/products/xyz_rear.jpg",
+ "http://example.com/products/123_front.jpg",
+ "http://example.com/products/123_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("http://example.com/products/123_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("http://example.com/products/xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("http://example.com/products/123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("http://example.com/products/123_front.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1",
+ new TestFieldValue().withL(new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-0.00234)));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684D));
+ nestedList1Map1.put("TitleSet",
+ new TestFieldValue().withSS("Book 1010 Title", "Book 1011 Title",
+ "Book 1111 Title"));
+ nestedList1Map1.put("BinaryTitleSet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("Book 1010 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1011 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1111 Title Binary"))
+ ));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ public static TestFieldsMap getPhoenixFieldMap(String value) {
+ TestFieldsMap map;
+ try {
+ map = MAPPER.readerFor(TestFieldsMap.class).readValue(value);
+ } catch (JsonProcessingException e) {
+ throw new UncheckedIOException(e);
+ }
+ return map;
+ }
+
+}
diff --git a/phoenix-core/src/it/resources/json/sample_01.json b/phoenix-core/src/it/resources/json/sample_01.json
new file mode 100644
index 0000000000..d3d8312342
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/sample_01.json
@@ -0,0 +1,80 @@
+{
+ "cold": "method",
+ "hurry": -593264871.4436941,
+ "press": "beat",
+ "browserling": -505169340.54880095,
+ "attention": 166583691,
+ "track": [
+ {
+ "speed": "sun",
+ "shot": [
+ "fun",
+ true,
+ [
+ {
+ "character": 413968576,
+ "earth": "helpful",
+ "money": false,
+ "city": {
+ "softly": "service",
+ "standard": [
+ "pour",
+ false,
+ true,
+ true,
+ true,
+ "softly",
+ "happened"
+ ],
+ "problem": [
+ -687102682.7731872,
+ "tightly",
+ 1527061470.2690287,
+ {
+ "condition": "else",
+ "higher": 1462910924.088698,
+ "scene": false,
+ "safety": 240784722.66658115,
+ "catch": false,
+ "behavior": true,
+ "protection": true
+ },
+ "torn",
+ false,
+ "eat"
+ ],
+ "flame": 1066643931,
+ "rest": -1053428849,
+ "additional": -442539394.7937908,
+ "brought": "rock"
+ },
+ "upward": -1306729583.8727202,
+ "sky": "act",
+ "height": true
+ },
+ -2042805074.4290242,
+ "settlers",
+ 1455555511.4875226,
+ -1448763321,
+ false,
+ 379701747
+ ],
+ false,
+ 1241794365,
+ "capital",
+ false
+ ],
+ "hidden": false,
+ "truth": "south",
+ "other": true,
+ "disease": "disease"
+ },
+ "printed",
+ "church",
+ "talk",
+ "ants",
+ "salmon",
+ -689893450
+ ],
+ "symbol": false
+}
\ No newline at end of file
diff --git a/phoenix-core/src/it/resources/json/sample_02.json b/phoenix-core/src/it/resources/json/sample_02.json
new file mode 100644
index 0000000000..333d830c0a
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/sample_02.json
@@ -0,0 +1,92 @@
+{
+ "gpu_name": "understanding",
+ "answer": -1207198585.4193487,
+ "natural": -299491449,
+ "rather": [
+ false,
+ -1057842580.2713003,
+ "sets",
+ {
+ "chapter": 1714682536.0572476,
+ "outline": {
+ "clock": "personal",
+ "halfway": {
+ "camp": false,
+ "combination": false,
+ "win": false,
+ "so": [
+ "spell",
+ false,
+ [
+ -1387905155,
+ "solar",
+ "hand",
+ 887113883,
+ 387828706,
+ -1067580636.7789006,
+ "sky",
+ false
+ ],
+ true,
+ false,
+ true,
+ false,
+ 1860501299
+ ],
+ "flow": "till",
+ "cat": false,
+ "damage": 384456425,
+ "clock": true
+ },
+ "thrown": false,
+ "saved": "completely",
+ "son": false,
+ "arrow": "studying",
+ "done": false,
+ "large": "together"
+ },
+ "themselves": 1155861188.1535501,
+ "personal": false,
+ "careful": "pool",
+ "manufacturing": "stuck",
+ "force": -8668311.262439728,
+ "plenty": false
+ },
+ false,
+ -2054306404.6564512,
+ "morning",
+ -1619008410
+ ],
+ "read": -802322222,
+ "top": false,
+ "living": "yourself",
+ "below": "who",
+ "new_samples": {
+ "$set": [
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMDE=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMDI=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMDM=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMDQ=",
+ "subType": "00"
+ }
+ }
+ ]
+ }
+}
\ No newline at end of file
diff --git a/phoenix-core/src/it/resources/json/sample_03.json b/phoenix-core/src/it/resources/json/sample_03.json
new file mode 100644
index 0000000000..dc6dd6c708
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/sample_03.json
@@ -0,0 +1,328 @@
+{
+ "result": [
+ {
+ "status": "active",
+ "name": {
+ "first": "Nat",
+ "middle": "Ellis",
+ "last": "Muller"
+ },
+ "username": "Nat-Muller",
+ "emails": [
+ "Delia3@example.com",
+ "Minnie.Mohr@gmail.com"
+ ],
+ "phoneNumber": "548-392-1895",
+ "location": {
+ "street": "41886 Mittie Forest",
+ "city": "Blockshire",
+ "state": "Wyoming",
+ "country": "Syrian Arab Republic",
+ "zip": "13127-2399",
+ "coordinates": {
+ "latitude": -58.0598,
+ "longitude": -44.9835
+ }
+ },
+ "website": "https://arctic-bolero.name",
+ "domain": "medium-daddy.info",
+ "job": {
+ "title": "Senior Usability Director",
+ "descriptor": "International",
+ "area": "Web",
+ "type": "Coordinator",
+ "company": "Berge Group"
+ },
+ "uuid": "e2af22f7-00fa-435a-b277-7e1cb771af36",
+ "objectId": "666bdf52dd6539db3454d499"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Elliot",
+ "middle": "Jamie",
+ "last": "Watsica"
+ },
+ "username": "Elliot-Watsica",
+ "emails": [
+ "Petra.Upton-Kozey@gmail.com",
+ "Regan78@example.com"
+ ],
+ "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"
+ },
+ {
+ "status": "active",
+ "name": {
+ "first": "Esteban",
+ "middle": "Jaden",
+ "last": "Beatty"
+ },
+ "username": "Esteban-Beatty",
+ "emails": [
+ "Bert.Adams53@example.com",
+ "Chanel_Renner-Carter@example.com"
+ ],
+ "phoneNumber": "240-999-1841 x356",
+ "location": {
+ "street": "4964 Hagenes Fork",
+ "city": "South Mohamed",
+ "state": "Ohio",
+ "country": "Turks and Caicos Islands",
+ "zip": "30649",
+ "coordinates": {
+ "latitude": -45.0224,
+ "longitude": -131.4055
+ }
+ },
+ "website": "https://bite-sized-bellows.name",
+ "domain": "windy-driving.info",
+ "job": {
+ "title": "Corporate Tactics Consultant",
+ "descriptor": "Corporate",
+ "area": "Accountability",
+ "type": "Planner",
+ "company": "Padberg LLC"
+ },
+ "uuid": "6ac5946e-6acf-405f-b47a-80f92c8681cc",
+ "objectId": "666bdf52dd6539db3454d49b"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Clint",
+ "middle": "Corey",
+ "last": "Gleichner"
+ },
+ "username": "Clint-Gleichner",
+ "emails": [
+ "Kadin3@example.com",
+ "Isabelle.Hermann44@gmail.com"
+ ],
+ "phoneNumber": "387.712.5381 x6598",
+ "location": {
+ "street": "47004 Wyman Trail",
+ "city": "Seattle",
+ "state": "Nebraska",
+ "country": "Svalbard & Jan Mayen Islands",
+ "zip": "73927",
+ "coordinates": {
+ "latitude": -75.2248,
+ "longitude": 107.0872
+ }
+ },
+ "website": "https://skinny-skyline.com/",
+ "domain": "possible-sill.info",
+ "job": {
+ "title": "Global Interactions Producer",
+ "descriptor": "Global",
+ "area": "Solutions",
+ "type": "Planner",
+ "company": "Schoen - Corkery"
+ },
+ "uuid": "a194136c-62b2-4e69-b256-3fd3c2285816",
+ "objectId": "666bdf52dd6539db3454d49c"
+ },
+ {
+ "status": "active",
+ "name": {
+ "first": "Marisa",
+ "middle": "Reese",
+ "last": "West"
+ },
+ "username": "Marisa-West",
+ "emails": [
+ "Elvis82@example.com",
+ "Rupert.Hodkiewicz48@example.com"
+ ],
+ "phoneNumber": "(757) 887-5160 x181",
+ "location": {
+ "street": "70273 Pollich Squares",
+ "city": "Cummeratafield",
+ "state": "Idaho",
+ "country": "Qatar",
+ "zip": "66684-4034",
+ "coordinates": {
+ "latitude": 67.8857,
+ "longitude": -59.1559
+ }
+ },
+ "website": "https://dangerous-fanny-pack.com",
+ "domain": "strident-aluminium.com",
+ "job": {
+ "title": "Principal Markets Director",
+ "descriptor": "Human",
+ "area": "Identity",
+ "type": "Specialist",
+ "company": "Nolan - Kozey"
+ },
+ "uuid": "cf2dacac-0b6a-4bc7-a375-6f3b7906c40c",
+ "objectId": "666bdf52dd6539db3454d49d"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Leonie",
+ "middle": "Reese",
+ "last": "Kessler"
+ },
+ "username": "Leonie-Kessler",
+ "emails": [
+ "Bradford.Abernathy@example.com",
+ "Ludie.Schmidt62@example.com"
+ ],
+ "phoneNumber": "411.584.7976",
+ "location": {
+ "street": "790 Wiza Shoals",
+ "city": "Hagenestown",
+ "state": "Alabama",
+ "country": "Bouvet Island",
+ "zip": "63799",
+ "coordinates": {
+ "latitude": 33.2153,
+ "longitude": -98.5389
+ }
+ },
+ "website": "https://sniveling-manner.biz",
+ "domain": "dizzy-molding.net",
+ "job": {
+ "title": "Corporate Response Specialist",
+ "descriptor": "Future",
+ "area": "Interactions",
+ "type": "Assistant",
+ "company": "Block LLC"
+ },
+ "uuid": "9c992028-f776-4d50-ab66-2ababdfc62fb",
+ "objectId": "666bdf52dd6539db3454d49e"
+ },
+ {
+ "status": "active",
+ "name": {
+ "first": "Andrew",
+ "middle": "Bowie",
+ "last": "Reilly-Hahn"
+ },
+ "username": "Andrew-Reilly-Hahn",
+ "emails": [
+ "Jacinto94@example.com",
+ "Nicolette51@example.com"
+ ],
+ "phoneNumber": "1-891-508-4746 x2730",
+ "location": {
+ "street": "169 Faye Island",
+ "city": "Fort Ashlyshire",
+ "state": "Arkansas",
+ "country": "Bahamas",
+ "zip": "13604",
+ "coordinates": {
+ "latitude": -15.1819,
+ "longitude": -91.9214
+ }
+ },
+ "website": "https://lopsided-cassock.info/",
+ "domain": "immense-courtroom.info",
+ "job": {
+ "title": "Direct Solutions Supervisor",
+ "descriptor": "Internal",
+ "area": "Markets",
+ "type": "Architect",
+ "company": "Grimes, Krajcik and Medhurst"
+ },
+ "uuid": "711bbf38-3c15-44dc-bf72-796ea2d7dfbf",
+ "objectId": "666bdf52dd6539db3454d49f"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Jaron",
+ "middle": "Corey",
+ "last": "Wolff"
+ },
+ "username": "Jaron-Wolff",
+ "emails": [
+ "Amaya19@gmail.com",
+ "Lera_Buckridge@example.com"
+ ],
+ "phoneNumber": "863-981-9104 x64213",
+ "location": {
+ "street": "50828 Chance Junction",
+ "city": "East Gerda",
+ "state": "Kentucky",
+ "country": "Aland Islands",
+ "zip": "68664-7869",
+ "coordinates": {
+ "latitude": 2.7211,
+ "longitude": 94.1885
+ }
+ },
+ "website": "https://unsightly-shortage.com/",
+ "domain": "acceptable-bankruptcy.com",
+ "job": {
+ "title": "National Security Manager",
+ "descriptor": "Chief",
+ "area": "Implementation",
+ "type": "Strategist",
+ "company": "Fahey and Sons"
+ },
+ "uuid": "cdca9da3-7b95-419d-b979-2e299b4beefa",
+ "objectId": "666bdf52dd6539db3454d4a0"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Wendell",
+ "middle": "Finley",
+ "last": "Cartwright"
+ },
+ "username": "Wendell-Cartwright",
+ "emails": [
+ "Virgil77@example.com",
+ "Jeromy42@example.com"
+ ],
+ "phoneNumber": "757.581.1946 x822",
+ "location": {
+ "street": "23083 Barton Viaduct",
+ "city": "New Genevieve",
+ "state": "Louisiana",
+ "country": "Turkmenistan",
+ "zip": "95556",
+ "coordinates": {
+ "latitude": -31.709,
+ "longitude": 52.4862
+ }
+ },
+ "website": "https://exotic-science.name",
+ "domain": "careful-liberty.net",
+ "job": {
+ "title": "Chief Tactics Consultant",
+ "descriptor": "Legacy",
+ "area": "Accountability",
+ "type": "Manager",
+ "company": "Hermann - Wisozk"
+ },
+ "uuid": "0cf0691a-a8a3-42a6-8b8c-9d5ded4c81bc",
+ "objectId": "666bdf52dd6539db3454d4a1"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/phoenix-core/src/it/resources/json/sample_updated_01.json b/phoenix-core/src/it/resources/json/sample_updated_01.json
new file mode 100644
index 0000000000..30127a799f
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/sample_updated_01.json
@@ -0,0 +1,84 @@
+{
+ "cold": "method",
+ "hurry": -593264871.4436941,
+ "press": "beat",
+ "browserling": {
+ "$binary": {
+ "base64": "PkHjukNzgcg=",
+ "subType": "00"
+ }
+ },
+ "attention": 166583691,
+ "track": [
+ {
+ "speed": "sun",
+ "shot": [
+ "fun",
+ true,
+ [
+ {
+ "character": 413968576,
+ "earth": "helpful",
+ "money": false,
+ "city": {
+ "softly": "service",
+ "standard": [
+ "pour",
+ false,
+ true,
+ true,
+ true,
+ "soft",
+ "happened"
+ ],
+ "problem": [
+ -687102682.7731872,
+ "tightly",
+ 1527061999.7040287,
+ {
+ "condition": "else",
+ "higher": 1462910924.088698,
+ "scene": false,
+ "safety": 240784722.66658115,
+ "catch": false,
+ "behavior": true,
+ "protection": true
+ },
+ "torn",
+ false,
+ "eat"
+ ],
+ "rest": -1053428849,
+ "additional": -442539394.7937908,
+ "brought": "rock"
+ },
+ "upward": -1306729583.8727202,
+ "sky": "act",
+ "height": true
+ },
+ -2042805074.4290242,
+ "settlers",
+ 1455555511.4875226,
+ -1448763321,
+ false,
+ 379701747
+ ],
+ false,
+ 1241794365,
+ "capital",
+ false
+ ],
+ "hidden": false,
+ "truth": "south",
+ "other": true,
+ "disease": "disease"
+ },
+ "printed",
+ "church",
+ "talk",
+ "ants",
+ "salmon",
+ -689893450
+ ],
+ "symbol": false
+}
\ No newline at end of file
diff --git a/phoenix-core/src/it/resources/json/sample_updated_02.json b/phoenix-core/src/it/resources/json/sample_updated_02.json
new file mode 100644
index 0000000000..7f4a34fe91
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/sample_updated_02.json
@@ -0,0 +1,167 @@
+{
+ "gpu_name": "understanding",
+ "answer": -1207198585.4193487,
+ "natural": -299491449,
+ "rather": [
+ false,
+ -1057842580.2713003,
+ "sets",
+ {
+ "chapter": 1714682536.0572476,
+ "outline": {
+ "clock": "personal",
+ "halfway": {
+ "camp": false,
+ "combination": false,
+ "win": false,
+ "so": [
+ "spell",
+ false,
+ [
+ -1387905155,
+ "solar",
+ 887113883,
+ 387828706,
+ -1067580636.7789006,
+ "sky",
+ false
+ ],
+ true,
+ false,
+ true,
+ false,
+ 1860501299
+ ],
+ "flow": "till",
+ "cat": false,
+ "damage": 384456425,
+ "clock": true
+ },
+ "thrown": false,
+ "saved": "completely",
+ "son": false,
+ "arrow": "studying",
+ "done": false,
+ "large": "together"
+ },
+ "themselves": 1155861188.1535501,
+ "personal": false,
+ "careful": "pool",
+ "manufacturing": "stuck",
+ "force": -8668311.262439728,
+ "plenty": false
+ },
+ false,
+ -2054306404.6564512,
+ "morning",
+ -1619008410
+ ],
+ "read": -802322222,
+ "top": false,
+ "living": "yourself",
+ "below": "who",
+ "new_samples": {
+ "$set": [
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMDQ=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMTQ=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMTM=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMDE=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMTI=",
+ "subType": "00"
+ }
+ },
+ {
+ "$binary": {
+ "base64": "U2FtcGxlMTA=",
+ "subType": "00"
+ }
+ }
+ ]
+ },
+ "newrecord": {
+ "speed": "sun",
+ "shot": [
+ "fun",
+ true,
+ [
+ {
+ "character": 413968576,
+ "earth": "helpful",
+ "money": false,
+ "city": {
+ "softly": "service",
+ "standard": [
+ "pour",
+ false,
+ true,
+ true,
+ true,
+ "softly",
+ "happened"
+ ],
+ "problem": [
+ -687102682.7731872,
+ "tightly",
+ 1527061470.2690287,
+ {
+ "condition": "else",
+ "higher": 1462910924.088698,
+ "scene": false,
+ "safety": 240784722.66658115,
+ "catch": false,
+ "behavior": true,
+ "protection": true
+ },
+ "torn",
+ false,
+ "eat"
+ ],
+ "flame": 1066643931,
+ "rest": -1053428849,
+ "additional": -442539394.7937908,
+ "brought": "rock"
+ },
+ "upward": -1306729583.8727202,
+ "sky": "act",
+ "height": true
+ },
+ -2042805074.4290242,
+ "settlers",
+ 1455555511.4875226,
+ -1448763321,
+ false,
+ 379701747
+ ],
+ false,
+ 1241794365,
+ "capital",
+ false
+ ],
+ "hidden": false,
+ "truth": "south",
+ "other": true,
+ "disease": "disease"
+ }
+}
\ No newline at end of file
diff --git a/phoenix-core/src/it/resources/json/sample_updated_03.json b/phoenix-core/src/it/resources/json/sample_updated_03.json
new file mode 100644
index 0000000000..98a71baa56
--- /dev/null
+++ b/phoenix-core/src/it/resources/json/sample_updated_03.json
@@ -0,0 +1,327 @@
+{
+ "result": [
+ {
+ "status": "active",
+ "name": {
+ "first": "Nat",
+ "middle": "Ellis",
+ "last": "Muller"
+ },
+ "username": "Nat-Muller",
+ "emails": [
+ "Delia3@example.com",
+ "Minnie.Mohr@gmail.com"
+ ],
+ "phoneNumber": "548-392-1895",
+ "location": {
+ "street": "41886 Mittie Forest",
+ "city": "Blockshire",
+ "state": "Wyoming",
+ "country": "Syrian Arab Republic",
+ "zip": "13127-2399",
+ "coordinates": {
+ "latitude": -58.0598,
+ "longitude": -44.9835
+ }
+ },
+ "website": "https://arctic-bolero.name",
+ "domain": "medium-daddy.info",
+ "job": {
+ "title": "Senior Usability Director",
+ "descriptor": "International",
+ "area": "Web",
+ "type": "Coordinator",
+ "company": "Berge Group"
+ },
+ "uuid": "e2af22f7-00fa-435a-b277-7e1cb771af36",
+ "objectId": "666bdf52dd6539db3454d499"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Elliot",
+ "middle": "Jamie",
+ "last": "Watsica"
+ },
+ "username": "Elliot-Watsica",
+ "emails": [
+ "Petra.Upton-Kozey@gmail.com",
+ "Regan78@example.com"
+ ],
+ "phoneNumber": "(308) 971-1166 x5940",
+ "location": {
+ "street": "4897 Gerhold Lodge",
+ "city": "Minneapolis",
+ "state": "AK",
+ "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"
+ },
+ {
+ "status": "active",
+ "name": {
+ "first": "Esteban",
+ "middle": "Jaden",
+ "last": "Beatty"
+ },
+ "username": "Esteban-Beatty",
+ "emails": [
+ "Bert.Adams53@example.com",
+ "Chanel_Renner-Carter@example.com"
+ ],
+ "phoneNumber": "240-999-1841 x356",
+ "location": {
+ "street": "4964 Hagenes Fork",
+ "city": "South Mohamed",
+ "state": "Ohio",
+ "country": "Turks and Caicos Islands",
+ "zip": "30649",
+ "coordinates": {
+ "latitude": -45.0224,
+ "longitude": -131.4055
+ }
+ },
+ "website": "https://bite-sized-bellows.name",
+ "domain": "windy-driving.info",
+ "job": {
+ "title": "Corporate Tactics Consultant",
+ "descriptor": "Corporate",
+ "area": "Accountability",
+ "type": "Planner",
+ "company": "Padberg LLC"
+ },
+ "uuid": "6ac5946e-6acf-405f-b47a-80f92c8681cc",
+ "objectId": "666bdf52dd6539db3454d49b"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Clint",
+ "middle": "Corey",
+ "last": "Gleichner"
+ },
+ "username": "Clint-Gleichner",
+ "emails": [
+ "Kadin3@example.com",
+ "Isabelle.Hermann44@gmail.com"
+ ],
+ "phoneNumber": "387.712.5381 x6598",
+ "location": {
+ "street": "47004 Wyman Trail",
+ "city": "Seattle",
+ "state": "Nebraska",
+ "country": "Svalbard & Jan Mayen Islands",
+ "zip": "73927",
+ "coordinates": {
+ "latitude": -75.2248,
+ "longitude": 107.0872
+ }
+ },
+ "website": "https://skinny-skyline.com/",
+ "domain": "possible-sill.info",
+ "job": {
+ "title": "Global Interactions Producer",
+ "descriptor": "Global",
+ "area": "Solutions",
+ "type": "Planner",
+ "company": "Schoen - Corkery"
+ },
+ "uuid": "a194136c-62b2-4e69-b256-3fd3c2285816",
+ "objectId": "666bdf52dd6539db3454d49c"
+ },
+ {
+ "status": "active",
+ "name": {
+ "first": "Marisa",
+ "middle": "Reese",
+ "last": "West"
+ },
+ "username": "Marisa-West",
+ "emails": [
+ "Elvis82@example.com"
+ ],
+ "phoneNumber": "(757) 887-5160 x181",
+ "location": {
+ "street": "70273 Pollich Squares",
+ "city": "Cummeratafield",
+ "state": "Idaho",
+ "country": "Qatar",
+ "zip": "66684-4034",
+ "coordinates": {
+ "latitude": 67.8857,
+ "longitude": -59.1559
+ }
+ },
+ "website": "https://dangerous-fanny-pack.com",
+ "domain": "strident-aluminium.com",
+ "job": {
+ "title": "Principal Markets Director",
+ "descriptor": "Human",
+ "area": "Identity",
+ "type": "Specialist",
+ "company": "Nolan - Kozey"
+ },
+ "uuid": "cf2dacac-0b6a-4bc7-a375-6f3b7906c40c",
+ "objectId": "666bdf52dd6539db3454d49d"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Leonie",
+ "middle": "Reese",
+ "last": "Kessler"
+ },
+ "username": "Leonie-Kessler",
+ "emails": [
+ "Bradford.Abernathy@example.com",
+ "Ludie.Schmidt62@example.com"
+ ],
+ "phoneNumber": "411.584.7976",
+ "location": {
+ "street": "790 Wiza Shoals",
+ "city": "Hagenestown",
+ "state": "Alabama",
+ "country": "Bouvet Island",
+ "zip": "63799",
+ "coordinates": {
+ "latitude": 33.2153,
+ "longitude": -98.5389
+ }
+ },
+ "website": "https://sniveling-manner.biz",
+ "domain": "dizzy-molding.net",
+ "job": {
+ "title": "Corporate Response Specialist",
+ "descriptor": "Future",
+ "area": "Interactions",
+ "type": "Assistant",
+ "company": "Block LLC"
+ },
+ "uuid": "9c992028-f776-4d50-ab66-2ababdfc62fb",
+ "objectId": "666bdf52dd6539db3454d49e"
+ },
+ {
+ "status": "active",
+ "name": {
+ "first": "Andrew",
+ "middle": "Bowie",
+ "last": "Reilly-Hahn"
+ },
+ "username": "Andrew-Reilly-Hahn",
+ "emails": [
+ "Jacinto94@example.com",
+ "Nicolette51@example.com"
+ ],
+ "phoneNumber": "1-891-508-4746 x2730",
+ "location": {
+ "street": "169 Faye Island",
+ "city": "Fort Ashlyshire",
+ "state": "Arkansas",
+ "country": "Bahamas",
+ "zip": "13604",
+ "coordinates": {
+ "latitude": -15.1819,
+ "longitude": -91.9214
+ }
+ },
+ "website": "https://lopsided-cassock.info/",
+ "domain": "immense-courtroom.info",
+ "job": {
+ "title": "Direct Solutions Supervisor",
+ "descriptor": "Internal",
+ "area": "Markets",
+ "type": "Architect",
+ "company": "Grimes, Krajcik and Medhurst"
+ },
+ "uuid": "711bbf38-3c15-44dc-bf72-796ea2d7dfbf",
+ "objectId": "666bdf52dd6539db3454d49f"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Jaron",
+ "middle": "Corey",
+ "last": "Wolff"
+ },
+ "username": "Jaron-Wolff",
+ "emails": [
+ "Amaya19@gmail.com",
+ "Lera_Buckridge@example.com"
+ ],
+ "phoneNumber": "863-981-9104 x64213",
+ "location": {
+ "street": "50828 Chance Junction",
+ "city": "East Gerda",
+ "state": "Kentucky",
+ "country": "Aland Islands",
+ "zip": "68664-7869",
+ "coordinates": {
+ "latitude": 2.7211,
+ "longitude": 94.1885
+ }
+ },
+ "website": "https://unsightly-shortage.com/",
+ "domain": "acceptable-bankruptcy.com",
+ "job": {
+ "title": "National Security Manager",
+ "descriptor": "Chief",
+ "area": "Implementation",
+ "type": "Strategist",
+ "company": "Fahey and Sons"
+ },
+ "uuid": "cdca9da3-7b95-419d-b979-2e299b4beefa",
+ "objectId": "666bdf52dd6539db3454d4a0"
+ },
+ {
+ "status": "disabled",
+ "name": {
+ "first": "Wendell",
+ "middle": "Finley",
+ "last": "Cartwright"
+ },
+ "username": "Wendell-Cartwright",
+ "emails": [
+ "Virgil77@example.com",
+ "Jeromy42@example.com"
+ ],
+ "phoneNumber": "757.581.1946 x822",
+ "location": {
+ "street": "23083 Barton Viaduct",
+ "city": "New Genevieve",
+ "state": "Louisiana",
+ "country": "Turkmenistan",
+ "zip": "95556",
+ "coordinates": {
+ "latitude": -31.709,
+ "longitude": 52.4862
+ }
+ },
+ "website": "https://exotic-science.name",
+ "domain": "careful-liberty.net",
+ "job": {
+ "title": "Chief Tactics Consultant",
+ "descriptor": "Legacy",
+ "area": "Accountability",
+ "type": "Manager",
+ "company": "Hermann - Wisozk"
+ },
+ "uuid": "0cf0691a-a8a3-42a6-8b8c-9d5ded4c81bc",
+ "objectId": "666bdf52dd6539db3454d4a1"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/CoerceExpressionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/CoerceExpressionTest.java
index c625972964..8f0eda2a0a 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/CoerceExpressionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/CoerceExpressionTest.java
@@ -27,6 +27,7 @@ import java.util.HashMap;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.phoenix.schema.types.PBinary;
+import org.apache.phoenix.schema.types.PBson;
import org.apache.phoenix.schema.types.PChar;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PDecimal;
@@ -102,8 +103,8 @@ public class CoerceExpressionTest {
@Test
public void testCoerceExpressionSupportsCoercingAllPDataTypesToVarBinary() throws Exception {
- for (PDataType p : PDataType.values()) {
- if (!p.isArrayType() && !p.equals(PJson.INSTANCE)) {
+ for (PDataType<?> p : PDataType.values()) {
+ if (!p.isArrayType() && !p.equals(PJson.INSTANCE) && !p.equals(PBson.INSTANCE)) {
LiteralExpression v = LiteralExpression.newConstant(map.get(p.getJavaClass()), p);
CoerceExpression e = new CoerceExpression(v, PVarbinary.INSTANCE);
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
@@ -117,8 +118,8 @@ public class CoerceExpressionTest {
@Test
public void testCoerceExpressionSupportsCoercingAllPDataTypesToBinary() throws Exception {
- for (PDataType p : PDataType.values()) {
- if (!p.isArrayType() && !p.equals(PJson.INSTANCE)) {
+ for (PDataType<?> p : PDataType.values()) {
+ if (!p.isArrayType() && !p.equals(PJson.INSTANCE) && !p.equals(PBson.INSTANCE)) {
LiteralExpression v = LiteralExpression.newConstant(map.get(p.getJavaClass()), p);
CoerceExpression e = new CoerceExpression(v, PBinary.INSTANCE);
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDataTypeTest.java b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDataTypeTest.java
index 74c5a99758..dffaee6747 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDataTypeTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDataTypeTest.java
@@ -1747,6 +1747,7 @@ public class PDataTypeTest {
+ "BINARY ARRAY=[BINARY ARRAY, VARBINARY ARRAY], "
+ "BOOLEAN=[BINARY, BOOLEAN, VARBINARY], "
+ "BOOLEAN ARRAY=[BINARY ARRAY, BOOLEAN ARRAY, VARBINARY ARRAY], "
+ + "BSON=[BINARY, BSON, VARBINARY], "
+ "CHAR=[BINARY, CHAR, VARBINARY, VARCHAR], "
+ "CHAR ARRAY=[BINARY ARRAY, CHAR ARRAY, VARBINARY ARRAY, VARCHAR ARRAY], "
+ "DATE=[BINARY, DATE, TIME, TIMESTAMP, VARBINARY], "
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/bson/ComparisonExpressionUtilsTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/bson/ComparisonExpressionUtilsTest.java
new file mode 100644
index 0000000000..50879dceb4
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/util/bson/ComparisonExpressionUtilsTest.java
@@ -0,0 +1,1020 @@
+/*
+ * 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.util.bson;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.bson.BsonArray;
+import org.bson.BsonBinary;
+import org.bson.BsonBoolean;
+import org.bson.BsonDocument;
+import org.bson.BsonDouble;
+import org.bson.BsonInt32;
+import org.bson.BsonInt64;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.bson.RawBsonDocument;
+import org.junit.Test;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.expression.util.bson.DocumentComparisonExpressionUtils;
+import org.apache.phoenix.expression.util.bson.SQLComparisonExpressionUtils;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for BSON Condition Expression Utility.
+ */
+public class ComparisonExpressionUtilsTest {
+
+ @Test
+ public void testSQLComparisonExpression1() {
+ TestFieldsMap testFieldsMap1 = getPhoenixFieldMap1();
+ TestFieldsMap compareValMap1 = getCompareValMap1();
+
+ RawBsonDocument rawBsonDocument = TestUtil.getRawBsonDocument(testFieldsMap1);
+ //{
+ // "$Id20": 101.011,
+ // "$Id2": 12,
+ // "#NestedList1_10": "1234abce",
+ // "$Id1": 120,
+ // "$Id10": 101,
+ // "$Ids1": "12",
+ // ":ISBN": "111-1111111111",
+ // "#NestedList1_xyz0123": "xyz0123",
+ // "$NestedList1_485": -485.33,
+ // "$NestedMap1_NList1_30": {
+ // "$binary": {
+ // "base64": "V2hpdGVl",
+ // "subType": "00"
+ // }
+ // },
+ // "InPublication": false,
+ // "$Ids10": "100",
+ // "#NestedMap1_NList1_3": {
+ // "$binary": {
+ // "base64": "V2hpdA==",
+ // "subType": "00"
+ // }
+ // },
+ // "#NestedList1_1": "1234abcc",
+ // "#NMap1_NList1": "NListVal01",
+ // "$NestedList1_4850": -485.35,
+ // "$Id": 101.01,
+ // "#Title": "Book 101 Title"
+ //}
+ RawBsonDocument compareValues = TestUtil.getRawBsonDocument(compareValMap1);
+
+ SQLComparisonExpressionUtils SQLComparisonExpressionUtils =
+ new SQLComparisonExpressionUtils(rawBsonDocument, compareValues);
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "(field_exists(Id) OR field_not_exists(Title))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((field_not_exists(Id) AND field_not_exists(Title1)) OR field_exists(ISBN2))"
+ + " OR ((Id <> #Title) AND ((InPublication = InPublication) OR ((ISBN = :ISBN)"
+ + " AND (Title = #Title))))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((field_exists(NestedMap1.ISBN) AND field_not_exists(NestedMap1.NList1[3])))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedMap1.Id = $Id AND (NestedMap1.InPublication = InPublication)"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((NestedMap1.Id = $Id) AND ((NestedMap1.InPublication[0] = InPublication) OR "
+ + "((ISBN[0] = :ISBN) AND (Title = #Title))) OR "
+ + "(NestedMap1.NList1[0] = #NMap1_NList1))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((field_not_exists(Id) AND field_not_exists(Title1)) OR field_exists(ISBN2))"
+ + " OR ((NestedMap1.Id = $Id) AND ((NestedMap1.InPublication = InPublication) OR "
+ + "((ISBN = :ISBN) AND (Title = #Title))))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] <= $NestedList1_485 AND NestedList1[1] > #NestedList1_1 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedList1[2][1].Id < $Id1 AND IdS < $Ids1 AND Id2 > $Id2 AND NestedMap1.NList1[2] > #NestedMap1_NList1_3"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] <= $NestedList1_485 AND NestedList1[1] >= #NestedList1_1 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedList1[2][1].Id <= $Id1 AND IdS <= $Ids1 AND Id2 >= $Id2 AND NestedMap1.NList1[2] >= #NestedMap1_NList1_3"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] >= $NestedList1_4850 AND NestedList1[1] < #NestedList1_10 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedList1[2][1].Id > $Id10 AND IdS > $Ids10 AND Id2 < $Id20 AND NestedMap1.NList1[2] < $NestedMap1_NList1_30"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] >= $NestedList1_4850 AND NestedList1[1] <= #NestedList1_10 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedList1[2][1].Id >= $Id10 AND IdS >= $Ids10 AND Id2 <= $Id20 AND NestedMap1.NList1[2] <= $NestedMap1_NList1_30 AND "
+ + "NestedMap1.NList1[2] <> $NestedMap1_NList1_30"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] >= $NestedList1_4850 AND NestedList1[1] <= #NestedList1_10 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedList1[2][1].Id >= $Id10 AND IdS >= $Ids10 AND Id2 <= $Id20 AND NestedMap1.NList1[2] <= $NestedMap1_NList1_30 AND "
+ + "(NestedMap1.NList1[2] = $NestedMap1_NList1_30 OR NestedList1[0] BETWEEN $NestedList1_4850 AND $Id2)"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] >= $NestedList1_4850 AND NestedList1[1] <= #NestedList1_10 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedMap1.NList1[0] IN ($Id, $Id1, $Id20, #NMap1_NList1) AND NestedMap1.NList1[2] <= $NestedMap1_NList1_30 AND "
+ + "(NestedMap1.NList1[2] = $NestedMap1_NList1_30 OR NestedList1[0] BETWEEN $NestedList1_4850 AND $Id2)"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "NestedList1[0] >= $NestedList1_4850 AND NestedList1[1] <= #NestedList1_10 AND NestedList1[2][0] >= #NestedList1_xyz0123 AND "
+ + "NestedMap1.NList1[0] IN ($Id, $Id1, $Id20, #NMap1_NList1) AND NestedMap1.NList1[2] <= $NestedMap1_NList1_30 AND "
+ + "(NestedMap1.NList1[2] = $NestedMap1_NList1_30 OR NestedList1[0] BETWEEN $NestedList1_4850 AND $Id2)"
+ + " AND NOT NestedMap1.InPublication IN ($Id, $Id1, $Id20, $Id21)"));
+
+ }
+
+ /**
+ * Test that directly uses executable expression to reduce the dependency on pattern-matcher.
+ */
+ @Test
+ public void testSQLComparisonExpression2() {
+ TestFieldsMap testFieldsMap1 = getPhoenixFieldMap1();
+ TestFieldsMap compareValMap1 = getCompareValMap1();
+
+ RawBsonDocument rawBsonDocument = TestUtil.getRawBsonDocument(testFieldsMap1);
+ //{
+ // "$Id20": 101.011,
+ // "$Id2": 12,
+ // "#NestedList1_10": "1234abce",
+ // "$Id1": 120,
+ // "$Id10": 101,
+ // "$Ids1": "12",
+ // ":ISBN": "111-1111111111",
+ // "#NestedList1_xyz0123": "xyz0123",
+ // "$NestedList1_485": -485.33,
+ // "$NestedMap1_NList1_30": {
+ // "$binary": {
+ // "base64": "V2hpdGVl",
+ // "subType": "00"
+ // }
+ // },
+ // "InPublication": false,
+ // "$Ids10": "100",
+ // "#NestedMap1_NList1_3": {
+ // "$binary": {
+ // "base64": "V2hpdA==",
+ // "subType": "00"
+ // }
+ // },
+ // "#NestedList1_1": "1234abcc",
+ // "#NMap1_NList1": "NListVal01",
+ // "$NestedList1_4850": -485.35,
+ // "$Id": 101.01,
+ // "#Title": "Book 101 Title"
+ //}
+ RawBsonDocument compareValues = TestUtil.getRawBsonDocument(compareValMap1);
+
+ SQLComparisonExpressionUtils SQLComparisonExpressionUtils =
+ new SQLComparisonExpressionUtils(rawBsonDocument, compareValues);
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "(exists('Id') || !exists('Title'))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((!exists('Id') && !exists('Title1')) || exists('ISBN2')) || " +
+ "((!isEquals('Id', '#Title'))" +
+ " && ((isEquals('InPublication', 'InPublication'))" +
+ " || ((isEquals('ISBN', ':ISBN')) && (isEquals('Title', '#Title')))))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((exists('NestedMap1.ISBN') && !exists('NestedMap1.NList1[3]')))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "isEquals('NestedMap1.Id', '$Id')" +
+ " && (isEquals('NestedMap1.InPublication', 'InPublication'))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((isEquals('NestedMap1.Id', '$Id'))" +
+ " && ((isEquals('NestedMap1.InPublication[0]', 'InPublication'))" +
+ " || ((isEquals('ISBN[0]', ':ISBN')) && (isEquals('Title', '#Title'))))" +
+ " || (isEquals('NestedMap1.NList1[0]', '#NMap1_NList1')))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "((!exists('Id') && !exists('Title1')) || exists('ISBN2')) ||" +
+ " ((isEquals('NestedMap1.Id', '$Id'))" +
+ " && ((isEquals('NestedMap1.InPublication', 'InPublication'))" +
+ " || ((isEquals('ISBN', ':ISBN')) && (isEquals('Title', '#Title')))))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "lessThanOrEquals('NestedList1[0]', '$NestedList1_485')" +
+ " && greaterThan('NestedList1[1]', '#NestedList1_1')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && lessThan('NestedList1[2][1].Id', '$Id1') && lessThan('IdS', '$Ids1')" +
+ " && greaterThan('Id2', '$Id2')" +
+ " && greaterThan('NestedMap1.NList1[2]', '#NestedMap1_NList1_3')"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "lessThanOrEquals('NestedList1[0]', '$NestedList1_485')" +
+ " && greaterThanOrEquals('NestedList1[1]', '#NestedList1_1')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && lessThanOrEquals('NestedList1[2][1].Id', '$Id1')" +
+ " && lessThanOrEquals('IdS', '$Ids1')" +
+ " && greaterThanOrEquals('Id2', '$Id2')" +
+ " && greaterThanOrEquals('NestedMap1.NList1[2]', '#NestedMap1_NList1_3')"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "greaterThanOrEquals('NestedList1[0]', '$NestedList1_4850')" +
+ " && lessThan('NestedList1[1]', '#NestedList1_10')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && greaterThan('NestedList1[2][1].Id', '$Id10')" +
+ " && greaterThan('IdS', '$Ids10') && lessThan('Id2', '$Id20')" +
+ " && lessThan('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "greaterThanOrEquals('NestedList1[0]', '$NestedList1_4850')" +
+ " && lessThanOrEquals('NestedList1[1]', '#NestedList1_10')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && greaterThanOrEquals('NestedList1[2][1].Id', '$Id10')" +
+ " && greaterThanOrEquals('IdS', '$Ids10') && lessThanOrEquals('Id2', '$Id20')" +
+ " && lessThanOrEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " && !isEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "greaterThanOrEquals('NestedList1[0]', '$NestedList1_4850')" +
+ " && lessThanOrEquals('NestedList1[1]', '#NestedList1_10')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && greaterThanOrEquals('NestedList1[2][1].Id', '$Id10')" +
+ " && greaterThanOrEquals('IdS', '$Ids10')" +
+ " && lessThanOrEquals('Id2', '$Id20')" +
+ " && lessThanOrEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " && (isEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " || between('NestedList1[0]', '$NestedList1_4850', '$Id2'))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "greaterThanOrEquals('NestedList1[0]', '$NestedList1_4850')" +
+ " && lessThanOrEquals('NestedList1[1]', '#NestedList1_10')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && in('NestedMap1.NList1[0]', '$Id, $Id1, $Id20, #NMap1_NList1')" +
+ " && lessThanOrEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " && (isEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " || between('NestedList1[0]', '$NestedList1_4850', '$Id2'))"));
+
+ assertTrue(SQLComparisonExpressionUtils.evaluateConditionExpression(
+ "greaterThanOrEquals('NestedList1[0]', '$NestedList1_4850')" +
+ " && lessThanOrEquals('NestedList1[1]', '#NestedList1_10')" +
+ " && greaterThanOrEquals('NestedList1[2][0]', '#NestedList1_xyz0123')" +
+ " && in('NestedMap1.NList1[0]', '$Id, $Id1, $Id20, #NMap1_NList1')" +
+ " && lessThanOrEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " && (isEquals('NestedMap1.NList1[2]', '$NestedMap1_NList1_30')" +
+ " || between('NestedList1[0]', '$NestedList1_4850', '$Id2'))" +
+ " && !in('NestedMap1.InPublication', '$Id, $Id1, $Id20, $Id21')"));
+
+ }
+
+ @Test
+ public void testDocumentComparisonExpression() {
+ TestFieldsMap testFieldsMap1 = getPhoenixFieldMap1();
+
+ RawBsonDocument rawBsonDocument = TestUtil.getRawBsonDocument(testFieldsMap1);
+ BsonDocument expressionDocument = new BsonDocument();
+ List<BsonValue> orList = new ArrayList<>();
+ orList.add(new BsonDocument().append("Id",
+ new BsonDocument().append("$exists", new BsonBoolean(true))));
+ orList.add(new BsonDocument().append("Title",
+ new BsonDocument().append("$exists", new BsonBoolean(false))));
+ expressionDocument.append("$or", new BsonArray(orList));
+
+ // Condition Expression:
+ //{
+ // "$or": [
+ // {
+ // "Id": {
+ // "$exists": true
+ // }
+ // },
+ // {
+ // "Title": {
+ // "$exists": false
+ // }
+ // }
+ // ]
+ //}
+
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+ BsonArray orListArray = new BsonArray();
+ BsonArray orList1 = new BsonArray();
+ BsonDocument orDoc1 = new BsonDocument();
+ BsonDocument andDoc1 = new BsonDocument();
+ BsonArray andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("Id",
+ new BsonDocument().append("$exists", new BsonBoolean(false))));
+ andList1.add(new BsonDocument().append("Title1",
+ new BsonDocument().append("$exists", new BsonBoolean(false))));
+ andDoc1.append("$and", andList1);
+ orList1.add(andDoc1);
+ orList1.add(new BsonDocument().append("ISBN2",
+ new BsonDocument().append("$exists", new BsonBoolean(true))));
+ orDoc1.append("$or", orList1);
+ orListArray.add(orDoc1);
+
+ BsonArray andList2 = new BsonArray();
+ BsonDocument andDoc2 = new BsonDocument();
+ andList2.add(new BsonDocument().append("Id",
+ new BsonDocument().append("$ne", new BsonString("Book 101 Title"))));
+
+ BsonArray orList2 = new BsonArray();
+ BsonDocument orDoc2 = new BsonDocument();
+ orList2.add(new BsonDocument().append("InPublication",
+ new BsonDocument().append("$eq", new BsonBoolean(false))));
+
+ BsonArray andList3 = new BsonArray();
+ BsonDocument andDoc3 = new BsonDocument();
+ andList3.add(new BsonDocument().append("ISBN",
+ new BsonDocument().append("$eq", new BsonString("111-1111111111"))));
+ andList3.add(new BsonDocument().append("Title",
+ new BsonDocument().append("$eq", new BsonString("Book 101 Title"))));
+ andDoc3.append("$and", andList3);
+
+ orList2.add(andDoc3);
+ orDoc2.append("$or", orList2);
+
+ andList2.add(orDoc2);
+ andDoc2.append("$and", andList2);
+ orListArray.add(andDoc2);
+
+ expressionDocument.append("$or", orListArray);
+
+ // Condition Expression:
+ //{
+ // "$or": [
+ // {
+ // "$or": [
+ // {
+ // "$and": [
+ // {
+ // "Id": {
+ // "$exists": false
+ // }
+ // },
+ // {
+ // "Title1": {
+ // "$exists": false
+ // }
+ // }
+ // ]
+ // },
+ // {
+ // "ISBN2": {
+ // "$exists": true
+ // }
+ // }
+ // ]
+ // },
+ // {
+ // "$and": [
+ // {
+ // "Id": {
+ // "$ne": "Book 101 Title"
+ // }
+ // },
+ // {
+ // "$or": [
+ // {
+ // "InPublication": {
+ // "$eq": false
+ // }
+ // },
+ // {
+ // "$and": [
+ // {
+ // "ISBN": {
+ // "$eq": "111-1111111111"
+ // }
+ // },
+ // {
+ // "Title": {
+ // "$eq": "Book 101 Title"
+ // }
+ // }
+ // ]
+ // }
+ // ]
+ // }
+ // ]
+ // }
+ // ]
+ //}
+
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("NestedMap1.ISBN",
+ new BsonDocument().append("$exists", new BsonBoolean(true))));
+ andList1.add(new BsonDocument().append("NestedMap1.NList1[3]",
+ new BsonDocument().append("$exists", new BsonBoolean(false))));
+ expressionDocument.append("$and", andList1);
+
+ // Condition Expression:
+ //{
+ // "$and": [
+ // {
+ // "NestedMap1.ISBN": {
+ // "$exists": true
+ // }
+ // },
+ // {
+ // "NestedMap1.NList1[3]": {
+ // "$exists": false
+ // }
+ // }
+ // ]
+ //}
+
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("NestedMap1.Id",
+ new BsonDocument().append("$eq", new BsonDouble(101.01))));
+ andList1.add(new BsonDocument().append("NestedMap1.InPublication",
+ new BsonDocument().append("$eq", new BsonBoolean(false))));
+ expressionDocument.append("$and", andList1);
+
+ // Condition Expression:
+ //{
+ // "$and": [
+ // {
+ // "NestedMap1.Id": {
+ // "$eq": 101.01
+ // }
+ // },
+ // {
+ // "NestedMap1.InPublication": {
+ // "$eq": false
+ // }
+ // }
+ // ]
+ //}
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+
+ andList1 = new BsonArray();
+ andDoc1 = new BsonDocument();
+ andList1.add(new BsonDocument().append("NestedMap1.Id",
+ new BsonDocument().append("$eq", new BsonDouble(101.01))));
+
+ orList1 = new BsonArray();
+ orDoc1 = new BsonDocument();
+ orList1.add(new BsonDocument().append("NestedMap1.InPublication[0]",
+ new BsonDocument().append("$eq", new BsonBoolean(false))));
+
+ andList2 = new BsonArray();
+ andDoc2 = new BsonDocument();
+ andList2.add(new BsonDocument().append("ISBN[0]",
+ new BsonDocument().append("$eq", new BsonString("111-1111111111"))));
+ andList2.add(new BsonDocument().append("Title",
+ new BsonDocument().append("$eq", new BsonString("Book 101 Title"))));
+ andDoc2.append("$and", andList2);
+
+ orList1.add(andDoc2);
+ orDoc1.append("$or", orList1);
+
+ andList1.add(orDoc1);
+ andDoc1.append("$and", andList1);
+
+ orList2 = new BsonArray();
+ orDoc2 = new BsonDocument();
+
+ orList2.add(andDoc1);
+ orList2.add(new BsonDocument().append("NestedMap1.NList1[0]",
+ new BsonDocument().append("$eq", new BsonString("NListVal01"))));
+
+ expressionDocument.append("$or", orList2);
+
+ // Condition Expression:
+ //{
+ // "$or": [
+ // {
+ // "$and": [
+ // {
+ // "NestedMap1.Id": {
+ // "$eq": 101.01
+ // }
+ // },
+ // {
+ // "$or": [
+ // {
+ // "NestedMap1.InPublication[0]": {
+ // "$eq": false
+ // }
+ // },
+ // {
+ // "$and": [
+ // {
+ // "ISBN[0]": {
+ // "$eq": "111-1111111111"
+ // }
+ // },
+ // {
+ // "Title": {
+ // "$eq": "Book 101 Title"
+ // }
+ // }
+ // ]
+ // }
+ // ]
+ // }
+ // ]
+ // },
+ // {
+ // "NestedMap1.NList1[0]": {
+ // "$eq": "NListVal01"
+ // }
+ // }
+ // ]
+ //}
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+ orListArray = new BsonArray();
+
+ andList1 = new BsonArray();
+ andDoc1 = new BsonDocument();
+ andList1.add(new BsonDocument().append("Id",
+ new BsonDocument().append("$exists", new BsonBoolean(false))));
+ andList1.add(new BsonDocument().append("Title1",
+ new BsonDocument().append("$exists", new BsonBoolean(false))));
+ andDoc1.append("$and", andList1);
+
+ orList1 = new BsonArray();
+ orDoc1 = new BsonDocument();
+
+ orList1.add(andDoc1);
+ orList1.add(new BsonDocument().append("ISBN2",
+ new BsonDocument().append("$exists", new BsonBoolean(true))));
+ orDoc1.append("$or", orList1);
+
+ orListArray.add(orDoc1);
+
+ andList2 = new BsonArray();
+ andDoc2 = new BsonDocument();
+ andList2.add(new BsonDocument().append("NestedMap1.Id",
+ new BsonDocument().append("$eq", new BsonDouble(101.01))));
+
+ orList2 = new BsonArray();
+ orDoc2 = new BsonDocument();
+ orList2.add(new BsonDocument().append("NestedMap1.InPublication",
+ new BsonDocument().append("$eq", new BsonBoolean(false))));
+
+ andList3 = new BsonArray();
+ andDoc3 = new BsonDocument();
+
+ andList3.add(new BsonDocument().append("ISBN",
+ new BsonDocument().append("$eq", new BsonString("111-1111111111"))));
+ andList3.add(new BsonDocument().append("Title",
+ new BsonDocument().append("$eq", new BsonString("Book 101 Title"))));
+ andDoc3.append("$and", andList3);
+
+ orList2.add(andDoc3);
+ orDoc2.append("$or", orList2);
+
+ andList2.add(orDoc2);
+ andDoc2.append("$and", andList2);
+
+ orListArray.add(andDoc2);
+ expressionDocument.append("$or", orListArray);
+
+ // Condition Expression:
+ //{
+ // "$or": [
+ // {
+ // "$or": [
+ // {
+ // "$and": [
+ // {
+ // "Id": {
+ // "$exists": false
+ // }
+ // },
+ // {
+ // "Title1": {
+ // "$exists": false
+ // }
+ // }
+ // ]
+ // },
+ // {
+ // "ISBN2": {
+ // "$exists": true
+ // }
+ // }
+ // ]
+ // },
+ // {
+ // "$and": [
+ // {
+ // "NestedMap1.Id": {
+ // "$eq": 101.01
+ // }
+ // },
+ // {
+ // "$or": [
+ // {
+ // "NestedMap1.InPublication": {
+ // "$eq": false
+ // }
+ // },
+ // {
+ // "$and": [
+ // {
+ // "ISBN": {
+ // "$eq": "111-1111111111"
+ // }
+ // },
+ // {
+ // "Title": {
+ // "$eq": "Book 101 Title"
+ // }
+ // }
+ // ]
+ // }
+ // ]
+ // }
+ // ]
+ // }
+ // ]
+ //}
+
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("NestedList1[0]",
+ new BsonDocument().append("$lte", new BsonDouble(-485.33))));
+ andList1.add(new BsonDocument().append("NestedList1[1]",
+ new BsonDocument().append("$gt", new BsonString("1234abcc"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][0]",
+ new BsonDocument().append("$gte", new BsonString("xyz0123"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][1].Id",
+ new BsonDocument().append("$lt", new BsonInt32(120))));
+ andList1.add(new BsonDocument().append("IdS",
+ new BsonDocument().append("$lt", new BsonString("12"))));
+ andList1.add(new BsonDocument().append("Id2",
+ new BsonDocument().append("$gt", new BsonInt32(12))));
+ andList1.add(new BsonDocument().append("NestedMap1.NList1[2]",
+ new BsonDocument().append("$gt", new BsonBinary(Bytes.toBytes("Whit")))));
+
+ expressionDocument.append("$and", andList1);
+
+ // Condition Expression:
+ //{
+ // "$and": [
+ // {
+ // "NestedList1[0]": {
+ // "$lte": -485.33
+ // }
+ // },
+ // {
+ // "NestedList1[1]": {
+ // "$gt": "1234abcc"
+ // }
+ // },
+ // {
+ // "NestedList1[2][0]": {
+ // "$gte": "xyz0123"
+ // }
+ // },
+ // {
+ // "NestedList1[2][1].Id": {
+ // "$lt": 120
+ // }
+ // },
+ // {
+ // "IdS": {
+ // "$lt": "12"
+ // }
+ // },
+ // {
+ // "Id2": {
+ // "$gt": 12
+ // }
+ // },
+ // {
+ // "NestedMap1.NList1[2]": {
+ // "$gt": {
+ // "$binary": {
+ // "base64": "V2hpdA==",
+ // "subType": "00"
+ // }
+ // }
+ // }
+ // }
+ // ]
+ //}
+
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("NestedList1[0]",
+ new BsonDocument().append("$lte", new BsonDouble(-485.33))));
+ andList1.add(new BsonDocument().append("NestedList1[1]",
+ new BsonDocument().append("$gte", new BsonString("1234abcc"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][0]",
+ new BsonDocument().append("$gte", new BsonString("xyz0123"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][1].Id",
+ new BsonDocument().append("$lte", new BsonInt32(120))));
+ andList1.add(new BsonDocument().append("IdS",
+ new BsonDocument().append("$lte", new BsonString("12"))));
+ andList1.add(new BsonDocument().append("Id2",
+ new BsonDocument().append("$gte", new BsonInt32(12))));
+ andList1.add(new BsonDocument().append("NestedMap1.NList1[2]",
+ new BsonDocument().append("$gte", new BsonBinary(Bytes.toBytes("Whit")))));
+
+ expressionDocument.append("$and", andList1);
+
+ // Condition Expression:
+ //{
+ // "$and": [
+ // {
+ // "NestedList1[0]": {
+ // "$lte": -485.33
+ // }
+ // },
+ // {
+ // "NestedList1[1]": {
+ // "$gte": "1234abcc"
+ // }
+ // },
+ // {
+ // "NestedList1[2][0]": {
+ // "$gte": "xyz0123"
+ // }
+ // },
+ // {
+ // "NestedList1[2][1].Id": {
+ // "$lte": 120
+ // }
+ // },
+ // {
+ // "IdS": {
+ // "$lte": "12"
+ // }
+ // },
+ // {
+ // "Id2": {
+ // "$gte": 12
+ // }
+ // },
+ // {
+ // "NestedMap1.NList1[2]": {
+ // "$gte": {
+ // "$binary": {
+ // "base64": "V2hpdA==",
+ // "subType": "00"
+ // }
+ // }
+ // }
+ // }
+ // ]
+ //}
+
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("NestedList1[0]",
+ new BsonDocument().append("$gte", new BsonDouble(-485.35))));
+ andList1.add(new BsonDocument().append("NestedList1[1]",
+ new BsonDocument().append("$lt", new BsonString("1234abce"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][0]",
+ new BsonDocument().append("$gte", new BsonString("xyz0123"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][1].Id",
+ new BsonDocument().append("$gt", new BsonInt64(101))));
+ andList1.add(
+ new BsonDocument().append("IdS", new BsonDocument().append("$gt", new BsonString("100"))));
+ andList1.add(new BsonDocument().append("Id2",
+ new BsonDocument().append("$lt", new BsonDouble(101.011))));
+ andList1.add(new BsonDocument().append("NestedMap1.NList1[2]",
+ new BsonDocument().append("$lt", new BsonBinary(Bytes.toBytes("Whitee")))));
+
+ expressionDocument.append("$and", andList1);
+
+ // Condition Expression:
+ //{
+ // "$and": [
+ // {
+ // "NestedList1[0]": {
+ // "$gte": -485.35
+ // }
+ // },
+ // {
+ // "NestedList1[1]": {
+ // "$lt": "1234abce"
+ // }
+ // },
+ // {
+ // "NestedList1[2][0]": {
+ // "$gte": "xyz0123"
+ // }
+ // },
+ // {
+ // "NestedList1[2][1].Id": {
+ // "$gt": 101
+ // }
+ // },
+ // {
+ // "IdS": {
+ // "$gt": "100"
+ // }
+ // },
+ // {
+ // "Id2": {
+ // "$lt": 101.011
+ // }
+ // },
+ // {
+ // "NestedMap1.NList1[2]": {
+ // "$lt": {
+ // "$binary": {
+ // "base64": "V2hpdGVl",
+ // "subType": "00"
+ // }
+ // }
+ // }
+ // }
+ // ]
+ //}
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ expressionDocument = new BsonDocument();
+
+ andList1 = new BsonArray();
+ andList1.add(new BsonDocument().append("NestedList1[0]",
+ new BsonDocument().append("$gte", new BsonDouble(-485.35))));
+ andList1.add(new BsonDocument().append("NestedList1[1]",
+ new BsonDocument().append("$lte", new BsonString("1234abce"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][0]",
+ new BsonDocument().append("$gte", new BsonString("xyz0123"))));
+ andList1.add(new BsonDocument().append("NestedList1[2][1].Id",
+ new BsonDocument().append("$gte", new BsonInt64(101))));
+ andList1.add(new BsonDocument().append("IdS",
+ new BsonDocument().append("$gte", new BsonString("100"))));
+ andList1.add(new BsonDocument().append("Id2",
+ new BsonDocument().append("$lte", new BsonDouble(101.011))));
+ andList1.add(new BsonDocument().append("NestedMap1.NList1[2]",
+ new BsonDocument().append("$lte", new BsonBinary(Bytes.toBytes("Whitee")))));
+ andList1.add(new BsonDocument().append("NestedMap1.NList1[2]",
+ new BsonDocument().append("$ne", new BsonBinary(Bytes.toBytes("Whitee")))));
+
+ expressionDocument.append("$and", andList1);
+
+ // Condition Expression:
+ //{
+ // "$and": [
+ // {
+ // "NestedList1[0]": {
+ // "$gte": -485.35
+ // }
+ // },
+ // {
+ // "NestedList1[1]": {
+ // "$lte": "1234abce"
+ // }
+ // },
+ // {
+ // "NestedList1[2][0]": {
+ // "$gte": "xyz0123"
+ // }
+ // },
+ // {
+ // "NestedList1[2][1].Id": {
+ // "$gte": 101
+ // }
+ // },
+ // {
+ // "IdS": {
+ // "$gte": "100"
+ // }
+ // },
+ // {
+ // "Id2": {
+ // "$lte": 101.011
+ // }
+ // },
+ // {
+ // "NestedMap1.NList1[2]": {
+ // "$lte": {
+ // "$binary": {
+ // "base64": "V2hpdGVl",
+ // "subType": "00"
+ // }
+ // }
+ // }
+ // },
+ // {
+ // "NestedMap1.NList1[2]": {
+ // "$ne": {
+ // "$binary": {
+ // "base64": "V2hpdGVl",
+ // "subType": "00"
+ // }
+ // }
+ // }
+ // }
+ // ]
+ //}
+ assertTrue(DocumentComparisonExpressionUtils.evaluateConditionExpression(rawBsonDocument,
+ expressionDocument));
+
+ }
+
+ private static TestFieldsMap getCompareValMap1() {
+ TestFieldsMap compareValMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map2 = new HashMap<>();
+ map2.put("$Id", new TestFieldValue().withN(101.01));
+ map2.put("$Id1", new TestFieldValue().withN(120));
+ map2.put("$Id10", new TestFieldValue().withN(101));
+ map2.put("$Ids1", new TestFieldValue().withS("12"));
+ map2.put("$Ids10", new TestFieldValue().withS("100"));
+ map2.put("$Id2", new TestFieldValue().withN(12));
+ map2.put("$Id20", new TestFieldValue().withN(101.011));
+ map2.put("#Title", new TestFieldValue().withS("Book 101 Title"));
+ map2.put(":ISBN", new TestFieldValue().withS("111-1111111111"));
+ map2.put("InPublication", new TestFieldValue().withBOOL(false));
+ map2.put("#NMap1_NList1", new TestFieldValue().withS("NListVal01"));
+ map2.put("$NestedList1_485", new TestFieldValue().withN(-485.33));
+ map2.put("$NestedList1_4850", new TestFieldValue().withN(-485.35));
+ map2.put("#NestedList1_xyz0123", new TestFieldValue().withS("xyz0123"));
+ map2.put("#NestedList1_1", new TestFieldValue().withS("1234abcc"));
+ map2.put("#NestedList1_10", new TestFieldValue().withS("1234abce"));
+ map2.put("#NestedMap1_NList1_3",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Whit"))));
+ map2.put("$NestedMap1_NList1_30",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Whitee"))));
+ compareValMap.setMap(map2);
+ return compareValMap;
+ }
+
+ private static TestFieldsMap getPhoenixFieldMap1() {
+ TestFieldsMap testFieldsMap1 = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("Id", new TestFieldValue().withN(101.01));
+ map.put("IdS", new TestFieldValue().withS("101.01"));
+ map.put("Id2", new TestFieldValue().withN(101.01));
+ map.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1",
+ new TestFieldValue().withL(
+ new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-0023.4),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White")))));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ testFieldsMap1.setMap(map);
+ return testFieldsMap1;
+ }
+
+}
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/bson/UpdateExpressionUtilsTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/bson/UpdateExpressionUtilsTest.java
new file mode 100644
index 0000000000..4f09946b40
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/util/bson/UpdateExpressionUtilsTest.java
@@ -0,0 +1,895 @@
+/*
+ * 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.util.bson;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.bson.BsonBinaryReader;
+import org.bson.BsonDocument;
+import org.bson.BsonNull;
+import org.bson.BsonNumber;
+import org.bson.BsonString;
+import org.bson.BsonValue;
+import org.bson.RawBsonDocument;
+import org.bson.codecs.BsonDocumentCodec;
+import org.bson.codecs.DecoderContext;
+import org.bson.io.ByteBufferBsonInput;
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.expression.util.bson.UpdateExpressionUtils;
+
+/**
+ * Tests for BSON Update Expression Utility.
+ */
+public class UpdateExpressionUtilsTest {
+
+ @Test
+ public void testUpdateExpression() {
+ TestFieldsMap map = getTestFieldsMap1();
+ TestFieldsMap comparisonMap = getComparisonValuesMap();
+
+ Assert.assertEquals(TestUtil.getPhoenixFieldMap(
+ TestUtil.getRawBsonBytes(map)), map);
+ Assert.assertEquals(TestUtil.getPhoenixFieldMap(
+ map.toString()), map);
+
+ BsonDocument bsonDocument = TestUtil.getBsonDocument(map);
+ assertDeserializedBsonDoc(bsonDocument);
+
+/* BsonDocument expressionDoc = getBsonDocument("SET Title = :newTitle, Id = :newId , " +
+ "NestedMap1.ColorList = :ColorList , "
+ + "Id1 = :Id1 , NestedMap1.NList1[0] = :NList1_0 , "
+ + "NestedList1[2][1].ISBN = :NestedList1_ISBN , "
+ + "NestedMap1.NestedMap2.NewID = :newId , "
+ + "NestedMap1.NestedMap2.NList[2] = :NList003 , "
+ + "NestedMap1.NestedMap2.NList[0] = :NList001 ADD AddedId :attr5_0 , "
+ + "NestedMap1.AddedId :attr5_0, NestedMap1.NestedMap2.Id :newIdNeg , "
+ + "NestedList12[2][0] :NestedList12_00 , NestedList12[2][1] :NestedList12_01 ,"
+ + " Pictures :AddedPics "
+ + "REMOVE IdS, Id2, NestedMap1.Title , "
+ + "NestedMap1.NestedMap2.InPublication , NestedList1[2][1].TitleSet1 "
+ + "DELETE PictureBinarySet :PictureBinarySet01 , NestedMap1.NSet1 :NSet01 ,"
+ + " NestedList1[2][1].TitleSet2 :NestedList1TitleSet01", TestUtil.getRawBsonDocument(comparisonMap));
+ */
+
+ String updateExpression = "{\n" +
+ " \"$SET\": {\n" +
+ " \"Title\": \"Cycle_1234_new\",\n" +
+ " \"Id\": \"12345\",\n" +
+ " \"NestedMap1.ColorList\": [\n" +
+ " \"Black\",\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"V2hpdGU=\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"Silver\"\n" +
+ " ],\n" +
+ " \"Id1\": {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"SURfMTAx\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"NestedMap1.NList1[0]\": {\n" +
+ " \"$set\": [\n" +
+ " \"Updated_set_01\",\n" +
+ " \"Updated_set_02\"\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList1[2][1].ISBN\": \"111-1111111122\",\n" +
+ " \"NestedMap1.NestedMap2.NewID\": \"12345\",\n" +
+ " \"NestedMap1.NestedMap2.NList[2]\": null,\n" +
+ " \"NestedMap1.NestedMap2.NList[0]\": 12.22\n" +
+ " },\n" +
+ " \"$UNSET\": {\n" +
+ " \"IdS\": null,\n" +
+ " \"Id2\": null,\n" +
+ " \"NestedMap1.Title\": null,\n" +
+ " \"NestedMap1.NestedMap2.InPublication\": null,\n" +
+ " \"NestedList1[2][1].TitleSet1\": null,\n" +
+ " \"NestedList1[2][10]\": null,\n" +
+ " \"NestedMap1.NList1[2]\": null\n" +
+ " },\n" +
+ " \"$ADD\": {\n" +
+ " \"AddedId\": 10,\n" +
+ " \"NestedMap1.AddedId\": 10,\n" +
+ " \"NestedMap1.NestedMap2.Id\": -12345,\n" +
+ " \"NestedList12[2][0]\": {\n" +
+ " \"$set\": [\n" +
+ " \"xyz01234\",\n" +
+ " \"abc01234\"\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList12[2][1]\": {\n" +
+ " \"$set\": [\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"dmFsMDM=\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"dmFsMDQ=\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " }\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList12[2][2]\": {\n" +
+ " \"$set\": [\n" +
+ " -234.56,\n" +
+ " 123,\n" +
+ " 93756.93475960549,\n" +
+ " 293755723028458.6\n" +
+ " ]\n" +
+ " },\n" +
+ " \"Pictures\": {\n" +
+ " \"$set\": [\n" +
+ " \"xyz5@_rear.jpg\",\n" +
+ " \"1235@_rear.jpg\"\n" +
+ " ]\n" +
+ " }\n" +
+ " },\n" +
+ " \"$DELETE_FROM_SET\": {\n" +
+ " \"PictureBinarySet\": {\n" +
+ " \"$set\": [\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"MTIzX3JlYXIuanBn\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"eHl6X2Zyb250LmpwZ19ubw==\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"$binary\": {\n" +
+ " \"base64\": \"eHl6X2Zyb250LmpwZw==\",\n" +
+ " \"subType\": \"00\"\n" +
+ " }\n" +
+ " }\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedMap1.NSet1\": {\n" +
+ " \"$set\": [\n" +
+ " -6830.5555,\n" +
+ " -48695\n" +
+ " ]\n" +
+ " },\n" +
+ " \"NestedList1[2][1].TitleSet2\": {\n" +
+ " \"$set\": [\n" +
+ " \"Book 1011 Title\",\n" +
+ " \"Book 1010 Title\"\n" +
+ " ]\n" +
+ " }\n" +
+ " }\n" +
+ "}";
+
+ //{
+ // "$SET": {
+ // "Title": "Cycle_1234_new",
+ // "Id": "12345",
+ // "NestedMap1.ColorList": [
+ // "Black",
+ // {
+ // "$binary": {
+ // "base64": "V2hpdGU=",
+ // "subType": "00"
+ // }
+ // },
+ // "Silver"
+ // ],
+ // "Id1": {
+ // "$binary": {
+ // "base64": "SURfMTAx",
+ // "subType": "00"
+ // }
+ // },
+ // "NestedMap1.NList1[0]": {
+ // "$set": [
+ // "Updated_set_01",
+ // "Updated_set_02"
+ // ]
+ // },
+ // "NestedList1[2][1].ISBN": "111-1111111122",
+ // "NestedMap1.NestedMap2.NewID": "12345",
+ // "NestedMap1.NestedMap2.NList[2]": null,
+ // "NestedMap1.NestedMap2.NList[0]": 12.22
+ // },
+ // "$UNSET": {
+ // "IdS": null,
+ // "Id2": null,
+ // "NestedMap1.Title": null,
+ // "NestedMap1.NestedMap2.InPublication": null,
+ // "NestedList1[2][1].TitleSet1": null,
+ // "NestedList1[2][10]": null,
+ // "NestedMap1.NList1[2]": null
+ // },
+ // "$ADD": {
+ // "AddedId": 10,
+ // "NestedMap1.AddedId": 10,
+ // "NestedMap1.NestedMap2.Id": -12345,
+ // "NestedList12[2][0]": {
+ // "$set": [
+ // "xyz01234",
+ // "abc01234"
+ // ]
+ // },
+ // "NestedList12[2][1]": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "dmFsMDM=",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "dmFsMDQ=",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // },
+ // "NestedList12[2][2]": {
+ // "$set": [
+ // -234.56,
+ // 123,
+ // 93756.93475960549,
+ // 293755723028458.6
+ // ]
+ // },
+ // "Pictures": {
+ // "$set": [
+ // "xyz5@_rear.jpg",
+ // "1235@_rear.jpg"
+ // ]
+ // }
+ // },
+ // "$DELETE_FROM_SET": {
+ // "PictureBinarySet": {
+ // "$set": [
+ // {
+ // "$binary": {
+ // "base64": "MTIzX3JlYXIuanBn",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "eHl6X2Zyb250LmpwZ19ubw==",
+ // "subType": "00"
+ // }
+ // },
+ // {
+ // "$binary": {
+ // "base64": "eHl6X2Zyb250LmpwZw==",
+ // "subType": "00"
+ // }
+ // }
+ // ]
+ // },
+ // "NestedMap1.NSet1": {
+ // "$set": [
+ // -6830.5555,
+ // -48695
+ // ]
+ // },
+ // "NestedList1[2][1].TitleSet2": {
+ // "$set": [
+ // "Book 1011 Title",
+ // "Book 1010 Title"
+ // ]
+ // }
+ // }
+ //}
+ RawBsonDocument expressionDoc = RawBsonDocument.parse(updateExpression);
+
+ UpdateExpressionUtils.updateExpression(expressionDoc, bsonDocument);
+ Assert.assertEquals("Update expression could not update the map",
+ getTestFieldsMap2(),
+ TestUtil.getPhoenixFieldMap(bsonDocument));
+
+ //{
+ // "$SET": {
+ // "NestedList1[0]": "NestedList1[0] + 12.22",
+ // "NestedList1[3]": null,
+ // "NestedList1[4]": true,
+ // "attr_5[0]": "attr_5[0] - 10",
+ // "Id1": "12345"
+ // },
+ // "$DELETE_FROM_SET": {
+ // "NestedList12[2][2]": {
+ // "$set": [
+ // -234.56,
+ // 123,
+ // 93756.93475960549,
+ // 293755723028458.6
+ // ]
+ // }
+ // }
+ //}
+
+ updateExpression = "{\n" +
+ " \"$SET\": {\n" +
+ " \"NestedList1[0]\": \"NestedList1[0] + 12.22\",\n" +
+ " \"NestedList1[3]\": null,\n" +
+ " \"NestedList1[4]\": true,\n" +
+ " \"attr_5[0]\": \"attr_5[0] - 10\",\n" +
+ " \"Id1\": \"12345\"\n" +
+ " },\n" +
+ " \"$DELETE_FROM_SET\": {\n" +
+ " \"NestedList12[2][2]\": {\n" +
+ " \"$set\": [\n" +
+ " -234.56,\n" +
+ " 123,\n" +
+ " 93756.93475960549,\n" +
+ " 293755723028458.6\n" +
+ " ]\n" +
+ " }\n" +
+ " }\n" +
+ "}";
+
+/* UpdateExpressionUtils.updateExpression(
+ getBsonDocument("SET NestedList1[0] = NestedList1[0] + :NList001 , "
+ + "NestedList1[3] = :NList003 , NestedList1[4] = :NList004, "
+ + "attr_5[0] = attr_5[0] - :attr5_0, Id1 = :newId",
+ TestUtil.getRawBsonDocument(comparisonMap)), bsonDocument);
+ */
+ expressionDoc = RawBsonDocument.parse(updateExpression);
+ UpdateExpressionUtils.updateExpression(expressionDoc, bsonDocument);
+
+ Assert.assertEquals("Update expression could not update the map after second update",
+ getTestFieldsMap3(),
+ TestUtil.getPhoenixFieldMap(bsonDocument));
+
+ Assert.assertEquals(TestUtil.getPhoenixFieldMap(
+ TestUtil.getRawBsonBytes(map)), map);
+ }
+
+ private static void assertDeserializedBsonDoc(BsonDocument bsonDocument) {
+ RawBsonDocument rawBsonDocument = new RawBsonDocument(bsonDocument, new BsonDocumentCodec());
+ byte[] serializedBytes = Bytes.toBytes((rawBsonDocument).getByteBuffer().asNIO());
+ RawBsonDocument desRawBsonDocument = new RawBsonDocument(serializedBytes);
+ BsonDocument deserializedBsonDocument;
+ try (BsonBinaryReader bsonReader = new BsonBinaryReader(
+ new ByteBufferBsonInput(desRawBsonDocument.getByteBuffer()))) {
+ deserializedBsonDocument =
+ new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build());
+ }
+ Assert.assertEquals(bsonDocument, deserializedBsonDocument);
+ }
+
+ public static BsonDocument getBsonDocument(String updateExpression,
+ BsonDocument comparisonValue) {
+
+ String setRegExPattern = "SET\\s+(.+?)(?=\\s+(REMOVE|ADD|DELETE)\\b|$)";
+ String removeRegExPattern = "REMOVE\\s+(.+?)(?=\\s+(SET|ADD|DELETE)\\b|$)";
+ String addRegExPattern = "ADD\\s+(.+?)(?=\\s+(SET|REMOVE|DELETE)\\b|$)";
+ String deleteRegExPattern = "DELETE\\s+(.+?)(?=\\s+(SET|REMOVE|ADD)\\b|$)";
+
+ String setString = "";
+ String removeString = "";
+ String addString = "";
+ String deleteString = "";
+
+ Pattern pattern = Pattern.compile(setRegExPattern);
+ Matcher matcher = pattern.matcher(updateExpression);
+ if (matcher.find()) {
+ setString = matcher.group(1).trim();
+ }
+
+ pattern = Pattern.compile(removeRegExPattern);
+ matcher = pattern.matcher(updateExpression);
+ if (matcher.find()) {
+ removeString = matcher.group(1).trim();
+ }
+
+ pattern = Pattern.compile(addRegExPattern);
+ matcher = pattern.matcher(updateExpression);
+ if (matcher.find()) {
+ addString = matcher.group(1).trim();
+ }
+
+ pattern = Pattern.compile(deleteRegExPattern);
+ matcher = pattern.matcher(updateExpression);
+ if (matcher.find()) {
+ deleteString = matcher.group(1).trim();
+ }
+
+ BsonDocument bsonDocument = new BsonDocument();
+ if (!setString.isEmpty()) {
+ BsonDocument setBsonDoc = new BsonDocument();
+ String[] setExpressions = setString.split(",");
+ for (String setExpression : setExpressions) {
+ String[] keyVal = setExpression.split("\\s*=\\s*");
+ if (keyVal.length == 2) {
+ String attributeKey = keyVal[0].trim();
+ String attributeVal = keyVal[1].trim();
+ if (!attributeVal.contains("+") && !attributeVal.contains("-")) {
+ setBsonDoc.put(attributeKey, comparisonValue.get(attributeVal));
+ } else {
+ setBsonDoc.put(attributeKey, getArithmeticExpVal(attributeVal, comparisonValue));
+ }
+ } else {
+ throw new RuntimeException(
+ "SET Expression " + setString + " does not include key value pairs separated by =");
+ }
+ }
+ bsonDocument.put("$SET", setBsonDoc);
+ }
+ if (!removeString.isEmpty()) {
+ String[] removeExpressions = removeString.split(",");
+ BsonDocument unsetBsonDoc = new BsonDocument();
+ for (String removeAttribute : removeExpressions) {
+ String attributeKey = removeAttribute.trim();
+ unsetBsonDoc.put(attributeKey, new BsonNull());
+ }
+ bsonDocument.put("$UNSET", unsetBsonDoc);
+ }
+ if (!addString.isEmpty()) {
+ String[] addExpressions = addString.split(",");
+ BsonDocument addBsonDoc = new BsonDocument();
+ for (String addExpression : addExpressions) {
+ addExpression = addExpression.trim();
+ String[] keyVal = addExpression.split("\\s+");
+ if (keyVal.length == 2) {
+ String attributeKey = keyVal[0].trim();
+ String attributeVal = keyVal[1].trim();
+ addBsonDoc.put(attributeKey, comparisonValue.get(attributeVal));
+ } else {
+ throw new RuntimeException("ADD Expression " + addString
+ + " does not include key value pairs separated by space");
+ }
+ }
+ bsonDocument.put("$ADD", addBsonDoc);
+ }
+ if (!deleteString.isEmpty()) {
+ BsonDocument delBsonDoc = new BsonDocument();
+ String[] deleteExpressions = deleteString.split(",");
+ for (String deleteExpression : deleteExpressions) {
+ deleteExpression = deleteExpression.trim();
+ String[] keyVal = deleteExpression.split("\\s+");
+ if (keyVal.length == 2) {
+ String attributeKey = keyVal[0].trim();
+ String attributeVal = keyVal[1].trim();
+ delBsonDoc.put(attributeKey, comparisonValue.get(attributeVal));
+ } else {
+ throw new RuntimeException("DELETE Expression " + deleteString
+ + " does not include key value pairs separated by space");
+ }
+ }
+ bsonDocument.put("$DELETE_FROM_SET", delBsonDoc);
+ }
+ return bsonDocument;
+ }
+
+ private static BsonString getArithmeticExpVal(String attributeVal,
+ BsonDocument comparisonValuesDocument) {
+ String[] tokens = attributeVal.split("\\s+");
+ // Pattern pattern = Pattern.compile(":?[a-zA-Z0-9]+");
+ Pattern pattern = Pattern.compile("[#:$]?[^\\s\\n]+");
+ Number newNum = null;
+ StringBuilder val = new StringBuilder();
+ for (String token : tokens) {
+ if (token.equals("+")) {
+ val.append(" + ");
+ continue;
+ } else if (token.equals("-")) {
+ val.append(" - ");
+ continue;
+ }
+ Matcher matcher = pattern.matcher(token);
+ if (matcher.find()) {
+ String operand = matcher.group();
+ if (operand.startsWith(":") || operand.startsWith("$") || operand.startsWith("#")) {
+ BsonValue bsonValue = comparisonValuesDocument.get(operand);
+ if (!bsonValue.isNumber() && !bsonValue.isDecimal128()) {
+ throw new IllegalArgumentException(
+ "Operand " + operand + " is not provided as number type");
+ }
+ Number numVal = UpdateExpressionUtils.getNumberFromBsonNumber((BsonNumber) bsonValue);
+ val.append(numVal);
+ } else {
+ val.append(operand);
+ }
+ }
+ }
+ return new BsonString(val.toString());
+ }
+
+ private static TestFieldsMap getComparisonValuesMap() {
+ TestFieldsMap comparisonMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map2 = new HashMap<>();
+ map2.put(":newTitle", new TestFieldValue().withS("Cycle_1234_new"));
+ map2.put(":newId", new TestFieldValue().withS("12345"));
+ map2.put(":newIdNeg", new TestFieldValue().withN(-12345));
+ map2.put(":ColorList", new TestFieldValue().withL(
+ new TestFieldValue().withS("Black"),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White"))),
+ new TestFieldValue().withS("Silver")
+ ));
+ map2.put(":Id1",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("ID_101"))));
+ map2.put(":NList001", new TestFieldValue().withN(12.22));
+ map2.put(":NList003", new TestFieldValue().withNULL(true));
+ map2.put(":NList004", new TestFieldValue().withBOOL(true));
+ map2.put(":attr5_0", new TestFieldValue().withN(10));
+ map2.put(":NList1_0", new TestFieldValue().withSS("Updated_set_01", "Updated_set_02"));
+ map2.put(":NestedList1_ISBN", new TestFieldValue().withS("111-1111111122"));
+ map2.put(":NestedList12_00", new TestFieldValue().withSS("xyz01234", "abc01234"));
+ map2.put(":NestedList12_01", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val03")),
+ new SerializableBytesPtr(Bytes.toBytes("val04"))
+ ));
+ map2.put(":AddedPics", new TestFieldValue().withSS(
+ "1235@_rear.jpg",
+ "xyz5@_rear.jpg"));
+ map2.put(":PictureBinarySet01", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("123_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg_no"))
+ ));
+ map2.put(":NSet01", new TestFieldValue().withNS(-6830.5555, -48695));
+ map2.put(":NestedList1TitleSet01", new TestFieldValue().withSS("Book 1010 Title",
+ "Book 1011 Title"));
+ comparisonMap.setMap(map2);
+ return comparisonMap;
+ }
+
+ private static TestFieldsMap getTestFieldsMap1() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withN(101.01));
+ map.put("IdS", new TestFieldValue().withS("101.01"));
+ map.put("Id2", new TestFieldValue().withN(101.01));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "123_rear.jpg",
+ "xyz_rear.jpg",
+ "123_front.jpg",
+ "xyz_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("123_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyz_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1",
+ new TestFieldValue().withL(new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-0.00234),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("to_be_removed")))));
+ Map<String, TestFieldValue> nestedMap2 = new HashMap<>();
+ nestedMap2.put("Id", new TestFieldValue().withN(101.22));
+ nestedMap2.put("Title", new TestFieldValue().withS("Book 10122 Title"));
+ nestedMap2.put("ISBN", new TestFieldValue().withS("111-1111111111999"));
+ nestedMap2.put("InPublication", new TestFieldValue().withBOOL(true));
+ nestedMap2.put("NList",
+ new TestFieldValue().withL(new TestFieldValue().withS("NListVal01"),
+ new TestFieldValue().withN(-0.00234)));
+ nestedMap1.put("NestedMap2",
+ new TestFieldValue().withM(nestedMap2));
+ nestedMap1.put("NSet1",
+ new TestFieldValue().withNS(123.45, 9586.7778, -124, -6830.5555, 10238,
+ -48695));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684d));
+ nestedList1Map1.put("TitleSet1",
+ new TestFieldValue().withSS("Book 1010 Title", "Book 1011 Title",
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("TitleSet2",
+ new TestFieldValue().withSS("Book 1010 Title", "Book 1011 Title",
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("BinaryTitleSet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("Book 1010 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1011 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1111 Title Binary"))
+ ));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ map.put("NestedList12",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(
+ new TestFieldValue().withSS("xyz0123"),
+ new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val01")),
+ new SerializableBytesPtr(Bytes.toBytes("val02")),
+ new SerializableBytesPtr(Bytes.toBytes("val03"))))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ private static TestFieldsMap getTestFieldsMap2() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("AddedId", new TestFieldValue().withN(10));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("Id1",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("ID_101"))));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1234),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withS("12345"));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "123_rear.jpg",
+ "1235@_rear.jpg",
+ "xyz5@_rear.jpg",
+ "xyz_rear.jpg",
+ "123_front.jpg",
+ "xyz_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Cycle_1234_new"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("AddedId", new TestFieldValue().withN(10));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1", new TestFieldValue().withL(
+ new TestFieldValue().withSS("Updated_set_01", "Updated_set_02"),
+ new TestFieldValue().withN(-0.00234)));
+ nestedMap1.put("ColorList", new TestFieldValue().withL(
+ new TestFieldValue().withS("Black"),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White"))),
+ new TestFieldValue().withS("Silver")
+ ));
+ Map<String, TestFieldValue> nestedMap2 = new HashMap<>();
+ nestedMap2.put("Id", new TestFieldValue().withN(-12243.78));
+ nestedMap2.put("NewID", new TestFieldValue().withS("12345"));
+ nestedMap2.put("Title", new TestFieldValue().withS("Book 10122 Title"));
+ nestedMap2.put("ISBN", new TestFieldValue().withS("111-1111111111999"));
+ nestedMap2.put("NList",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(12.22),
+ new TestFieldValue().withN(-0.00234),
+ new TestFieldValue().withNULL(true)));
+ nestedMap1.put("NestedMap2",
+ new TestFieldValue().withM(nestedMap2));
+ nestedMap1.put("NSet1",
+ new TestFieldValue().withNS(123.45, 9586.7778, -124, 10238));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111122"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684D));
+ nestedList1Map1.put("TitleSet2",
+ new TestFieldValue().withSS(
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
+ nestedList1Map1.put("BinaryTitleSet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("Book 1010 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1011 Title Binary")),
+ new SerializableBytesPtr(Bytes.toBytes("Book 1111 Title Binary"))
+ ));
+ map.put("NestedList1",
+ new TestFieldValue().withL(new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(new TestFieldValue().withS("xyz0123"),
+ new TestFieldValue().withM(nestedList1Map1))));
+ map.put("NestedList12",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(-485.34),
+ new TestFieldValue().withS("1234abcd"),
+ new TestFieldValue().withL(
+ new TestFieldValue().withSS("xyz0123", "xyz01234", "abc01234"),
+ new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("val01")),
+ new SerializableBytesPtr(Bytes.toBytes("val02")),
+ new SerializableBytesPtr(Bytes.toBytes("val03")),
+ new SerializableBytesPtr(Bytes.toBytes("val04"))),
+ new TestFieldValue().withNS(
+ -234.56,
+ 123,
+ 93756.93475960549,
+ 293755723028458.6))));
+ testFieldsMap.setMap(map);
+ return testFieldsMap;
+ }
+
+ private static TestFieldsMap getTestFieldsMap3() {
+ TestFieldsMap testFieldsMap = new TestFieldsMap();
+ Map<String, TestFieldValue> map = new HashMap<>();
+ map.put("attr_0", new TestFieldValue().withS("str_val_0"));
+ map.put("AddedId", new TestFieldValue().withN(10));
+ map.put("attr_1", new TestFieldValue().withN(1295.03));
+ map.put("Id1", new TestFieldValue().withS("12345"));
+ map.put("attr_5", new TestFieldValue().withL(
+ new TestFieldValue().withN(1224),
+ new TestFieldValue().withS("str001"),
+ new TestFieldValue().withB(new SerializableBytesPtr(
+ new byte[] {0, 1, 2, 3, 4, 5}))));
+ Map<String, TestFieldValue> nMap1 = new HashMap<>();
+ nMap1.put("n_attr_0", new TestFieldValue().withS("str_val_0"));
+ nMap1.put("n_attr_1", new TestFieldValue().withN(1295.03));
+ String bytesAttributeVal1 = "2048u5nblwjeiWFGTH(4bf930";
+ byte[] bytesAttrVal1 = bytesAttributeVal1.getBytes();
+ nMap1.put("n_attr_2", new TestFieldValue().withB(new SerializableBytesPtr(
+ bytesAttrVal1)));
+ nMap1.put("n_attr_3", new TestFieldValue().withBOOL(true));
+ nMap1.put("n_attr_4", new TestFieldValue().withNULL(true));
+ map.put("attr_6", new TestFieldValue().withM(nMap1));
+ map.put("Id", new TestFieldValue().withS("12345"));
+ map.put("ColorBytes",
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("Black"))));
+ map.put("RelatedItems",
+ new TestFieldValue().withNS(1234, -485.45582904, 123.0948, 0.111));
+ map.put("Pictures", new TestFieldValue().withSS(
+ "123_rear.jpg",
+ "1235@_rear.jpg",
+ "xyz5@_rear.jpg",
+ "xyz_rear.jpg",
+ "123_front.jpg",
+ "xyz_front.jpg"
+ ));
+ map.put("PictureBinarySet", new TestFieldValue().withBS(
+ new SerializableBytesPtr(Bytes.toBytes("xyz_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123_front.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("123abc_rear.jpg")),
+ new SerializableBytesPtr(Bytes.toBytes("xyzabc_rear.jpg"))
+ ));
+ map.put("Title", new TestFieldValue().withS("Cycle_1234_new"));
+ map.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ map.put("InPublication", new TestFieldValue().withBOOL(false));
+ Map<String, TestFieldValue> nestedMap1 = new HashMap<>();
+ nestedMap1.put("Id", new TestFieldValue().withN(101.01));
+ nestedMap1.put("AddedId", new TestFieldValue().withN(10));
+ nestedMap1.put("ISBN", new TestFieldValue().withS("111-1111111111"));
+ nestedMap1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedMap1.put("NList1", new TestFieldValue().withL(
+ new TestFieldValue().withSS("Updated_set_01", "Updated_set_02"),
+ new TestFieldValue().withN(-0.00234)));
+ nestedMap1.put("ColorList", new TestFieldValue().withL(
+ new TestFieldValue().withS("Black"),
+ new TestFieldValue().withB(new SerializableBytesPtr(Bytes.toBytes("White"))),
+ new TestFieldValue().withS("Silver")
+ ));
+ Map<String, TestFieldValue> nestedMap2 = new HashMap<>();
+ nestedMap2.put("Id", new TestFieldValue().withN(-12243.78));
+ nestedMap2.put("NewID", new TestFieldValue().withS("12345"));
+ nestedMap2.put("Title", new TestFieldValue().withS("Book 10122 Title"));
+ nestedMap2.put("ISBN", new TestFieldValue().withS("111-1111111111999"));
+ nestedMap2.put("NList",
+ new TestFieldValue().withL(
+ new TestFieldValue().withN(12.22),
+ new TestFieldValue().withN(-0.00234),
+ new TestFieldValue().withNULL(true)));
+ nestedMap1.put("NestedMap2",
+ new TestFieldValue().withM(nestedMap2));
+ nestedMap1.put("NSet1",
+ new TestFieldValue().withNS(123.45, 9586.7778, -124, 10238));
+ map.put("NestedMap1", new TestFieldValue().withM(nestedMap1));
+ Map<String, TestFieldValue> nestedList1Map1 = new HashMap<>();
+ nestedList1Map1.put("Id", new TestFieldValue().withN(101.01));
+ nestedList1Map1.put("Title", new TestFieldValue().withS("Book 101 Title"));
+ nestedList1Map1.put("ISBN", new TestFieldValue().withS("111-1111111122"));
+ nestedList1Map1.put("InPublication", new TestFieldValue().withBOOL(false));
+ nestedList1Map1.put("IdSet",
+ new TestFieldValue().withNS(204850.69703847596, -39457860.486939476, 20576024,
+ 19306873, 4869067048362749590684d));
+ nestedList1Map1.put("TitleSet2",
+ new TestFieldValue().withSS(
+ "Book 1111 Title", "Book 1200 Title", "Book 1201 Title"));
... 131 lines suppressed ...