Skip to content

Commit 6c4cb77

Browse files
palashcPalash Chauhan
andauthored
PHOENIX-7691 : Handle empty bson doc in bson update expression function (#2277)
Co-authored-by: Palash Chauhan <p.chauhan@pchauha-ltmgv47.internal.salesforce.com>
1 parent 9380d8e commit 6c4cb77

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

phoenix-core-client/src/main/java/org/apache/phoenix/expression/function/BsonUpdateExpressionFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
9595
updateExpressionBsonDoc = RawBsonDocument.parse(updateExpression);
9696
} else {
9797
updateExpressionBsonDoc = (RawBsonDocument) PBson.INSTANCE.toObject(ptr);
98-
if (updateExpressionBsonDoc == null || updateExpressionBsonDoc.isEmpty()) {
98+
if (updateExpressionBsonDoc == null) {
9999
return true;
100100
}
101101
}

phoenix-core/src/it/java/org/apache/phoenix/end2end/Bson4IT.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.bson.BsonString;
5757
import org.bson.Document;
5858
import org.bson.RawBsonDocument;
59+
import org.junit.Assert;
5960
import org.junit.Assume;
6061
import org.junit.Test;
6162
import org.junit.experimental.categories.Category;
@@ -505,6 +506,45 @@ public void testBsonValueFunctionWithBSONType() throws Exception {
505506
}
506507
}
507508

509+
@Test
510+
public void testBsonReturnValueWithEmptyUpdateExpression() throws Exception {
511+
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
512+
String tableName = generateUniqueName();
513+
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
514+
conn.setAutoCommit(true);
515+
conn.createStatement().execute("CREATE TABLE " + tableName + " (" +
516+
" hk VARCHAR NOT NULL, " +
517+
" sk VARCHAR NOT NULL, " +
518+
" col BSON, " +
519+
" CONSTRAINT pk PRIMARY KEY (hk, sk))");
520+
521+
RawBsonDocument bsonDoc = RawBsonDocument.parse("{\"a\":1,\"b\":2}");
522+
523+
PreparedStatement p = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?,?)");
524+
p.setString(1, "h1");
525+
p.setString(2, "s1");
526+
p.setObject(3, bsonDoc);
527+
p.execute();
528+
529+
p = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?) ON DUPLICATE KEY UPDATE\n" +
530+
" COL = BSON_UPDATE_EXPRESSION(COL,'{}')");
531+
p.setString(1, "h1");
532+
p.setString(2, "s1");
533+
Pair<Integer, ResultSet> resultPair = p.unwrap(PhoenixPreparedStatement.class).executeAtomicUpdateReturnRow();
534+
Assert.assertEquals(1, resultPair.getFirst().intValue());
535+
Assert.assertEquals(bsonDoc, resultPair.getSecond().getObject(3));
536+
537+
p = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?,?) ON DUPLICATE KEY UPDATE\n" +
538+
" COL = BSON_UPDATE_EXPRESSION(COL,?)");
539+
p.setString(1, "h1");
540+
p.setString(2, "s1");
541+
p.setObject(3, new BsonDocument());
542+
resultPair = p.unwrap(PhoenixPreparedStatement.class).executeAtomicUpdateReturnRow();
543+
Assert.assertEquals(1, resultPair.getFirst().intValue());
544+
Assert.assertEquals(bsonDoc, resultPair.getSecond().getObject(3));
545+
}
546+
}
547+
508548
@Test
509549
public void testConditionalUpsertReturnRow() throws Exception {
510550
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);

0 commit comments

Comments
 (0)