greptimedb icon indicating copy to clipboard operation
greptimedb copied to clipboard

Proposal: within time range syntax

Open killme2008 opened this issue 1 year ago • 6 comments

What type of enhancement is this?

API improvement, User experience

What does the enhancement do?

If the user wants to query the rows in a given year, date, or minute currently, they must specify the query conditions such as:

select * from monitors where ts >= '2024-01-01 00:00:00' and ts < '2025-01-01 00:00:00';

select * from monitors where ts >= '2024-04-19 00:00:00' and ts < '2024-04-20 00:00:00';

select * from monitors where ts >= '2024-04-19 23:50:00' and ts < '2024-04-19 23:51:00';

It would be better to provide a syntax sugar that helps users simplify the query:

select * from monitors where ts within '2024';

select * from monitors where ts within '2024-04-19';

select * from monitors where ts within '2024-04-19 23:50';

The predicate [column] WITHIN [TIMESTAMP] that will be transformed into

[column] >= [TIMESTAMP start] and [column] < [TIMESTAMP + 1 interval]

The interval is chosen based on TIMESTAMP precision.

Implementation challenges

No response

killme2008 avatar Apr 19 '24 15:04 killme2008

@killme2008 I'm interested in this. I'd like to use it.

If GreptimeTeam accepts this feature, I'd like to work on it. Maybe the first PR is to add support for the WITHIN filter to GreptimeTeam/sqlparser-rs.

NiwakaDev avatar Jan 15 '25 13:01 NiwakaDev

@killme2008 I'm interested in this. I'd like to use it.

If GreptimeTeam accepts this feature, I'd like to work on it. Maybe the first PR is to add support for the WITHIN filter to GreptimeTeam/sqlparser-rs.

Cool! It's so kind of you. Please take it! It's very useful for users.

killme2008 avatar Jan 15 '25 15:01 killme2008

FYI: there is a similar logic in https://github.com/greptimeteam/greptimedb/blob/54e25d8cddc2d4d29a4bb99f7fd1e522fdee9d84/src/log-query/src/log_query.rs#L87-L142

waynexia avatar Jan 16 '25 09:01 waynexia

@waynexia Thank you for sharing! Your implementation looks like it's very helpful!!

I'm sorry for pointing a very minor issue, but as far as I understand, it seems like chrono doesn't support only %Y format(https://github.com/chronotope/chrono/blob/main/src/naive/date/tests.rs#L675).

For example:

        let mut tf = TimeFilter {
            start: Some("2023".to_string()),
            end: None,
            span: None,
        };
        tf.canonicalize().unwrap();
        assert!(tf.end.is_some());

I added something like this to the project, and the test failed.

NiwakaDev avatar Jan 18 '25 20:01 NiwakaDev

@killme2008 By the way, I think we could support Questdb's modifier syntax like:

select * from monitors where ts within '2018-01;-3d';

Do you think it's worthwhile to support this?

Reference

https://questdb.com/docs/reference/sql/where/#examples

NiwakaDev avatar Jan 22 '25 02:01 NiwakaDev

@killme2008 By the way, I think we could support Questdb's modifier syntax like:

select * from monitors where ts within '2018-01;-3d'; Do you think it's worthwhile to support this?

Reference

https://questdb.com/docs/reference/sql/where/#examples

That's cool. I don't notice that before, thanks for sharing.

killme2008 avatar Jan 22 '25 02:01 killme2008