Why is this happening ?
dwettlaufer's answer is spot-on, but I'll provide a visual representation to help with understanding just what's happening here:
> SELECT id,token(id),name FROM numbers ;
id | system.token(id) | name
----+----------------------+-------
5 | -7509452495886106294 | five
1 | -4069959284402364209 | one
2 | -3248873570005575792 | two
4 | -2729420104000364805 | four
3 | 9010454139840013625 | three
(5 rows)
Data in Astra DB is stored in order by the hashed token value of the partition key. Because of the hash, numeric values are quite likely to be arranged in a different order.
And is there a way to solve this ?
As was mentioned, sort order can only be enforced within a partition key. So if a different column is used as the partition key, id can be used to sort data within it.
Consider a table that looks like this:
CREATE TABLE dept_numbers (
dept int,
id int,
name TEXT,
PRIMARY KEY(dept,id));
Then something like this would work:
> SELECT * FROM dept_numbers WHERE dept=1;
dept | id | name
------+----+-------
1 | 1 | one
1 | 2 | two
1 | 3 | three
1 | 4 | four
1 | 5 | five
(5 rows)
Note that all queries with Astra DB should have a WHERE clause. Without it, an expensive table scan will result.