Skip to content

Commit

Permalink
Allow both internal and public name in realm::Results::{sort, distinc…
Browse files Browse the repository at this point in the history
…t} (#7877)
  • Loading branch information
kneth authored Jul 11, 2024
1 parent 5dc121b commit a7479e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* None.

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* None.
* When a public name is defined on a property, calling `realm::Results::sort()` or `realm::Results::distinct()` with the internal name could throw an error like `Cannot sort on key path 'NAME': property 'PersonObject.NAME' does not exist`. ([realm/realm-js#6779](https://github.com/realm/realm-js/issues/6779), since v12.12.0)

### Breaking changes
* None.
Expand Down
3 changes: 3 additions & 0 deletions src/realm/object-store/results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ static std::vector<ExtendedColumnKey> parse_keypath(StringData keypath, Schema c
begin = sep + (sep != end);

auto prop = object_schema->property_for_public_name(key);
if (!prop) {
prop = object_schema->property_for_name(key);
}
check(prop, "property '%1.%2' does not exist", object_schema->name, key);
if (is_dictionary(prop->type)) {
check(index.length(), "missing dictionary key");
Expand Down
27 changes: 25 additions & 2 deletions test/object-store/results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4984,7 +4984,7 @@ TEST_CASE("results: public name declared", "[results]") {
realm->commit_transaction();
Results r(realm, table);

SECTION("sorted") {
SECTION("sorted by public_name") {
auto sorted = r.sort({{"public_value", true}});
REQUIRE(sorted.limit(0).size() == 0);
REQUIRE_ORDER(sorted.limit(1), 2);
Expand All @@ -4993,7 +4993,16 @@ TEST_CASE("results: public name declared", "[results]") {
REQUIRE_ORDER(sorted.limit(100), 2, 6, 3, 7, 0, 4, 1, 5);
}

SECTION("distinct") {
SECTION("sorted by name") {
auto sorted = r.sort({{"value", true}});
REQUIRE(sorted.limit(0).size() == 0);
REQUIRE_ORDER(sorted.limit(1), 2);
REQUIRE_ORDER(sorted.limit(2), 2, 6);
REQUIRE_ORDER(sorted.limit(8), 2, 6, 3, 7, 0, 4, 1, 5);
REQUIRE_ORDER(sorted.limit(100), 2, 6, 3, 7, 0, 4, 1, 5);
}

SECTION("distinct by public_name") {
auto sorted = r.distinct({"public_value"});
REQUIRE(sorted.limit(0).size() == 0);
REQUIRE_ORDER(sorted.limit(1), 0);
Expand All @@ -5006,6 +5015,20 @@ TEST_CASE("results: public name declared", "[results]") {
REQUIRE_ORDER(sorted.limit(2), 2, 3);
REQUIRE_ORDER(sorted.limit(8), 2, 3, 0, 1);
}

SECTION("distinct by name") {
auto sorted = r.distinct({"value"});
REQUIRE(sorted.limit(0).size() == 0);
REQUIRE_ORDER(sorted.limit(1), 0);
REQUIRE_ORDER(sorted.limit(2), 0, 1);
REQUIRE_ORDER(sorted.limit(8), 0, 1, 2, 3);

sorted = r.sort({{"value", true}}).distinct({"value"});
REQUIRE(sorted.limit(0).size() == 0);
REQUIRE_ORDER(sorted.limit(1), 2);
REQUIRE_ORDER(sorted.limit(2), 2, 3);
REQUIRE_ORDER(sorted.limit(8), 2, 3, 0, 1);
}
}

TEST_CASE("notifications: objects with PK recreated", "[results]") {
Expand Down

0 comments on commit a7479e5

Please sign in to comment.