Skip to content

PHOENIX-7757 : Support arithmetic with if_not_exists in bson SET update expression#2362

Merged
palashc merged 3 commits intoapache:masterfrom
palashc:PHOENIX-7757
Feb 3, 2026
Merged

PHOENIX-7757 : Support arithmetic with if_not_exists in bson SET update expression#2362
palashc merged 3 commits intoapache:masterfrom
palashc:PHOENIX-7757

Conversation

@palashc
Copy link
Contributor

@palashc palashc commented Feb 2, 2026

No description provided.

@palashc palashc requested a review from virajjasani February 2, 2026 23:32
Copy link
Contributor

@virajjasani virajjasani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}

private static Number resolveSetOperand(BsonValue operand, BsonDocument bsonDocument) {
if (operand.isNumber()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems in the latest BSON version, operand.isNumber() seems to be fixed. Otherwise, we follow this pattern everywhere: operand.isNumber() || operand.isDecimal128()

Comment on lines +687 to +688
BsonValue resolved = resolveIfNotExists((BsonDocument) operand, bsonDocument);
return getNumberFromBsonNumber((BsonNumber) resolved);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one provides non-number data type with $IF_NOT_EXISTS, it will fail. This is expected, looks good.

Comment on lines +647 to +656
} else if (doc.containsKey("$ADD")) {
BsonArray operands = doc.getArray("$ADD");
Number result = resolveSetOperand(operands.get(0), bsonDocument);
result = addNum(result, resolveSetOperand(operands.get(1), bsonDocument));
return getBsonNumberFromNumber(result);
} else if (doc.containsKey("$SUBTRACT")) {
BsonArray operands = doc.getArray("$SUBTRACT");
Number result = resolveSetOperand(operands.get(0), bsonDocument);
result = subtractNum(result, resolveSetOperand(operands.get(1), bsonDocument));
return getBsonNumberFromNumber(result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to extend $ADD and $SUBTRACT to include more operands, we can replace operands.get(0) and operands.get(1) within a loop. Not urgent now, can be done in future.

// 5c. document format with 2 simple operands: fieldA = fieldA + 1 = 10 + 1 = 11
+ " \"fieldA\": {\n" + " \"$ADD\": [\"fieldA\", 1]\n" + " },\n"
// 5d. document format with nested path: nested.value = nested.value + 1 = 5 + 1 = 6
+ " \"nested.value\": {\n" + " \"$ADD\": [\"nested.value\", 1]\n" + " }\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we also add one more case where something like nested.value is top level attribute? i.e. top level attribute with .?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@palashc palashc merged commit 5ed9667 into apache:master Feb 3, 2026
1 check passed
palashc added a commit to palashc/phoenix-1 that referenced this pull request Feb 3, 2026
…te expression (apache#2362)

Co-authored-by: Palash Chauhan <p.chauhan@pchauha-ltmgv47.internal.salesforce.com>
palashc added a commit that referenced this pull request Feb 4, 2026
…te expression (#2362) (#2364)

Co-authored-by: Palash Chauhan <p.chauhan@pchauha-ltmgv47.internal.salesforce.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants