Skip to content

Commit a7479e5

Browse files
authored
Allow both internal and public name in realm::Results::{sort, distinct} (#7877)
1 parent 5dc121b commit a7479e5

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* None.
66

77
### Fixed
8-
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
9-
* None.
8+
* 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)
109

1110
### Breaking changes
1211
* None.

src/realm/object-store/results.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ static std::vector<ExtendedColumnKey> parse_keypath(StringData keypath, Schema c
871871
begin = sep + (sep != end);
872872

873873
auto prop = object_schema->property_for_public_name(key);
874+
if (!prop) {
875+
prop = object_schema->property_for_name(key);
876+
}
874877
check(prop, "property '%1.%2' does not exist", object_schema->name, key);
875878
if (is_dictionary(prop->type)) {
876879
check(index.length(), "missing dictionary key");

test/object-store/results.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4984,7 +4984,7 @@ TEST_CASE("results: public name declared", "[results]") {
49844984
realm->commit_transaction();
49854985
Results r(realm, table);
49864986

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

4996-
SECTION("distinct") {
4996+
SECTION("sorted by name") {
4997+
auto sorted = r.sort({{"value", true}});
4998+
REQUIRE(sorted.limit(0).size() == 0);
4999+
REQUIRE_ORDER(sorted.limit(1), 2);
5000+
REQUIRE_ORDER(sorted.limit(2), 2, 6);
5001+
REQUIRE_ORDER(sorted.limit(8), 2, 6, 3, 7, 0, 4, 1, 5);
5002+
REQUIRE_ORDER(sorted.limit(100), 2, 6, 3, 7, 0, 4, 1, 5);
5003+
}
5004+
5005+
SECTION("distinct by public_name") {
49975006
auto sorted = r.distinct({"public_value"});
49985007
REQUIRE(sorted.limit(0).size() == 0);
49995008
REQUIRE_ORDER(sorted.limit(1), 0);
@@ -5006,6 +5015,20 @@ TEST_CASE("results: public name declared", "[results]") {
50065015
REQUIRE_ORDER(sorted.limit(2), 2, 3);
50075016
REQUIRE_ORDER(sorted.limit(8), 2, 3, 0, 1);
50085017
}
5018+
5019+
SECTION("distinct by name") {
5020+
auto sorted = r.distinct({"value"});
5021+
REQUIRE(sorted.limit(0).size() == 0);
5022+
REQUIRE_ORDER(sorted.limit(1), 0);
5023+
REQUIRE_ORDER(sorted.limit(2), 0, 1);
5024+
REQUIRE_ORDER(sorted.limit(8), 0, 1, 2, 3);
5025+
5026+
sorted = r.sort({{"value", true}}).distinct({"value"});
5027+
REQUIRE(sorted.limit(0).size() == 0);
5028+
REQUIRE_ORDER(sorted.limit(1), 2);
5029+
REQUIRE_ORDER(sorted.limit(2), 2, 3);
5030+
REQUIRE_ORDER(sorted.limit(8), 2, 3, 0, 1);
5031+
}
50095032
}
50105033

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

0 commit comments

Comments
 (0)