问题
I am writing a parameterized CQL statement that is not behaving as I would expect. I am trying to return a list of records for a range of dates that in this instance are using the same date for the beginning and end points.
The table is defined as follows:
CREATE TABLE sometable (
partition_key varchar,
cluster_start_date timestamp,
other_cluster_key varchar,
data varchar,
PRIMARY KEY((partition_key), cluster_start_date, other_cluster_key)
) WITH CLUSTERING ORDER BY(cluster_start_date ASC, other_cluster_key ASC);
The prepared statement is as follows:
SELECT * FROM sometable WHERE partition_key = 'xxx' AND cluster_start_date >= ? AND cluster_start_date <= ?;
When the parameterized dates are different, the statement returns the correct data. But when the parameterized dates are the same, then no values are returned.
Could someone tell me why this statement does not work with identical parameters, and if so, what can be done to get the expected behavior?
Thanks for all who can help....
回答1:
You didn't show how you are specifying the timestamp value in your query, but I'm guessing you are specifying it in seconds. Internally the resoultion of timestamp is higher than seconds.
If you specify a timestamp in seconds, then your lower bound would be rounded down to 000 for the milliseconds, and your upper bound would also be rounded down to 000 milliseconds. It's likely the row you expect to see does not have 000 for the milliseconds, and so it is outside the tiny 1 millisecond wide range you specified. For example:
SELECT * from sometable ;
partition_key | cluster_start_date | other_cluster_key | data
---------------+--------------------------+-------------------+------
a | 2015-08-22 06:16:38-0400 | a | row1
a | 2015-08-22 06:17:09-0400 | b | row2
a | 2015-08-22 06:17:31-0400 | c | row3
SELECT * FROM sometable WHERE partition_key = 'a'
and cluster_start_date >= '2015-08-22 06:17:09-0400'
and cluster_start_date <= '2015-08-22 06:17:09-0400';
partition_key | cluster_start_date | other_cluster_key | data
---------------+--------------------+-------------------+------
(0 rows)
But if you specify the timestamp to millisecond precision, and use 999 for the milliseconds on the upper bound, it finds the row:
SELECT * FROM sometable WHERE partition_key = 'a'
and cluster_start_date >= '2015-08-22 06:17:09.000-0400'
and cluster_start_date <= '2015-08-22 06:17:09.999-0400';
partition_key | cluster_start_date | other_cluster_key | data
---------------+--------------------------+-------------------+------
a | 2015-08-22 06:17:09-0400 | b | row2
Or you could drop the equals on the upper bound and say less than the next second like this:
SELECT * FROM sometable WHERE partition_key = 'a'
and cluster_start_date >= '2015-08-22 06:17:09-0400'
and cluster_start_date < '2015-08-22 06:17:10-0400';
partition_key | cluster_start_date | other_cluster_key | data
---------------+--------------------------+-------------------+------
a | 2015-08-22 06:17:09-0400 | b | row2
来源:https://stackoverflow.com/questions/32151887/why-does-this-cql-return-empty-results-with-beginning-and-ending-date-range-are