cozodb / cozo

A transactional, relational-graph-vector database that uses Datalog for query. The hippocampus for AI!
https://cozodb.org
Mozilla Public License 2.0
3.44k stars 108 forks source link

bug: `ReorderSort` does not work as expected #274

Closed creatorrr closed 3 months ago

creatorrr commented 4 months ago

ReorderSort does not return anything. Am I doing something wrong? @zh217

Query:

test[a] <- [[1], [2], [3]]

?[i, x] <~ ReorderSort(test[a], out: [a], sort_by: [-a])
Expected: i x
1 3
2 2
3 1
Actual: i x
wti commented 3 months ago

Working for me with take: n != 0 e.g.,

test[a] <- [[1], [2], [3]]

?[i, x] <~ ReorderSort(test[a], out: [a], sort_by: [-a], take: 10)

So it looks like the default value for take of 0 does not have the documented magic significance of unbounded.

wti commented 3 months ago

See https://github.com/cozodb/cozo/blob/6ab063c627a57242ff42d563dfa1ce41264bcc72/cozo-core/src/fixed_rule/utilities/reorder_sort.rs#L112

perhaps:

if 0 != count && count > take_plus_skip {
    break;
}
wti commented 3 months ago

Proposed fix in PR https://github.com/cozodb/cozo/pull/277

creatorrr commented 3 months ago

Ah that makes sense! Thanks for the catch and the fix 😄 Closing