Skip to content

Commit

Permalink
Merge pull request #5262 from realm/js/between
Browse files Browse the repository at this point in the history
Query parser's BETWEEN is a closed interval
  • Loading branch information
jedelbo authored Feb 23, 2022
2 parents 5f652ba + 570d994 commit 1a6a69a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Fix an error when compiling a watchOS Simulator target not supporting Thread-local storage ([#7623](https://github.com/realm/realm-swift/issues/7623), since v11.7.0)
* Check, when opening a realm, that in-memory realms are not encrypted ([#5195](https://github.com/realm/realm-core/issues/5195))
* Using asynchronous writes from multiple threads had several race conditions and would often crash (since v11.8.0).
* Changed parsed queries using the `between` operator to be inclusive of the limits, a closed interval instead of an open interval. This is to conform to the published documentation and for parity with NSPredicate's definition. ([#5262](https://github.com/realm/realm-core/issues/5262), since the introduction of this operator in v11.3.0)

### Breaking changes
* Renamed SubscriptionSet::State::Superceded -> Superseded to correct typo.
Expand Down
4 changes: 2 additions & 2 deletions src/realm/parser/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ Query BetweenNode::visit(ParserDriver* drv)

ValueNode min(limits->elements.at(0));
ValueNode max(limits->elements.at(1));
RelationalNode cmp1(prop, CompareNode::GREATER, &min);
RelationalNode cmp2(prop, CompareNode::LESS, &max);
RelationalNode cmp1(prop, CompareNode::GREATER_EQUAL, &min);
RelationalNode cmp2(prop, CompareNode::LESS_EQUAL, &max);
AndNode and_node(&cmp1, &cmp2);

return and_node.visit(drv);
Expand Down
19 changes: 16 additions & 3 deletions test/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ TEST(Parser_basic_serialisation)
verify_query(test_context, t, "age = 1 || age == 3", 2);
verify_query(test_context, t, "fees = 1.2 || fees = 2.23", 1);
verify_query(test_context, t, "fees = 2 || fees = 3", 1);
verify_query(test_context, t, "fees BETWEEN {2, 3}", 3);
verify_query(test_context, t, "fees BETWEEN {2.20, 2.25}", 2);
verify_query(test_context, t, "fees BETWEEN {2, 3}", 4);
verify_query(test_context, t, "fees BETWEEN {2.20, 2.25}", 3);
verify_query(test_context, t, "fees = 2 || fees = 3 || fees = 4", 1);
verify_query(test_context, t, "fees = 0 || fees = 1", 0);

Expand Down Expand Up @@ -3871,7 +3871,20 @@ TEST(Parser_Between)
verify_query(test_context, table, "between > 0", 2);
verify_query(test_context, table, "between <= 3", 3);

verify_query(test_context, table, "age between {20, 25}", 1);
verify_query(test_context, table, "age between {24, 26}", 3);

verify_query(test_context, table, "age between {20, 23}", 0);
verify_query(test_context, table, "age between {20, 24}", 1);
verify_query(test_context, table, "age between {20, 25}", 2);
verify_query(test_context, table, "age between {20, 26}", 3);
verify_query(test_context, table, "age between {20, 27}", 3);

verify_query(test_context, table, "age between {23, 30}", 3);
verify_query(test_context, table, "age between {24, 30}", 3);
verify_query(test_context, table, "age between {25, 30}", 2);
verify_query(test_context, table, "age between {26, 30}", 1);
verify_query(test_context, table, "age between {27, 30}", 0);

CHECK_THROW_ANY(verify_query(test_context, table, "age between {20}", 1));
CHECK_THROW_ANY(verify_query(test_context, table, "age between {20, 25, 34}", 1));
}
Expand Down

0 comments on commit 1a6a69a

Please sign in to comment.