Skip to content

Commit dfaa737

Browse files
committed
Fix problems with default value in order clause
When there's a default value specified on the order clause on a Keypath that is also part of the return value with a different default value, then the returned results were wrong. It was sorted by the return value default, not by the order clause default. This fixes #53.
1 parent e934912 commit dfaa737

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

repl-tests/order.noise

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# order expressions.
2+
3+
drop target/tests/querytestorder;
4+
create target/tests/querytestorder;
5+
6+
add {"_id":"1", "bar": false};
7+
"1"
8+
add {"_id":"2", "bar": true};
9+
"2"
10+
add {"_id":"3", "bar": "string"};
11+
"3"
12+
add {"_id":"4"};
13+
"4"
14+
15+
find {}
16+
order .bar asc
17+
return .bar;
18+
[
19+
null,
20+
false,
21+
true,
22+
"string"
23+
]
24+
25+
find {}
26+
order .bar default=1 asc
27+
return .bar default=1;
28+
[
29+
false,
30+
true,
31+
1,
32+
"string"
33+
]
34+
35+
find {}
36+
order .bar default=1 asc
37+
return .bar;
38+
[
39+
false,
40+
true,
41+
null,
42+
"string"
43+
]
44+
45+
46+
find {}
47+
order .bar asc
48+
return ._id;
49+
[
50+
"4",
51+
"1",
52+
"2",
53+
"3"
54+
]
55+
56+
find {}
57+
order .bar default=1 asc
58+
return ._id;
59+
[
60+
"1",
61+
"2",
62+
"4",
63+
"3"
64+
]
65+
66+
find {}
67+
order .bar asc
68+
return .bar default=1;
69+
[
70+
1,
71+
false,
72+
true,
73+
"string"
74+
]

src/returnable.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,19 @@ impl Returnable for RetValue {
312312
}
313313

314314
fn take_order_for_matching_fields(&mut self, map: &mut HashMap<String, OrderInfo>) {
315-
self.order_info = map.remove(&self.rp.to_key());
315+
// Compare the default values of the to be removed order info with the default value for
316+
// the return value. If they are equal, the order can be safely removed, as the ordering
317+
// of the result will be the same.
318+
// If they are not equal, then we need to keep the order else the final result would be
319+
// sorted by the values of the result and not of the orde clause.
320+
let keypath = &self.rp.to_key();
321+
let remove_order = match map.get(keypath) {
322+
Some(order_info) => order_info.default == self.default,
323+
None => false,
324+
};
325+
if remove_order {
326+
self.order_info = map.remove(keypath);
327+
}
316328
}
317329

318330
fn get_ordering(&mut self, orders: &mut Vec<Option<OrderInfo>>) {

0 commit comments

Comments
 (0)