Skip to content

Allow passing a range to take - #471

Merged
aljazerzen merged 11 commits into
mainfrom
offset
May 16, 2022
Merged

aljazerzen merged 11 commits into
mainfrom
offset

Conversation

@max-sixty

Copy link
Copy Markdown
Member

This is a WIP. Ideally we can do

from employees
take 5..10

And it'll translate to a LIMIT & OFFSET

Passing take 10 would be the same as passing take 1..10.

Comment thread prql-compiler/src/sql/translator.rs Outdated
/// Aggregate several ordered ranges into one, computing the intersection.
///
/// Returns a tuple of `(start, end)`, where `end` is optional.
fn range_of_ranges(ranges: Vec<Range>) -> Result<(i64, Option<i64>)> {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very complicated implementation for what it's actually doing, but I'm not sure of an easier one...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Algorithmically, this is as concise as you can get (I think). But code-wise, this could be compressed, by:

  • first mapping all ranges to Rust's Range<i64>,
  • replacing cs > length with a check for negative length at the end,
  • replace a the two if let Some with unwrap_or and map

Comment thread prql-compiler/src/sql/translator.rs Outdated
Some(
10,
),
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems very verbose from rustfmt...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would assert_yaml_snapshot work? I believe tuple and Option have Serializable trait...

@max-sixty

Copy link
Copy Markdown
Member Author

Docs:
image

image

@max-sixty
max-sixty requested a review from aljazerzen May 15, 2022 04:30
@aljazerzen aljazerzen mentioned this pull request May 15, 2022
@aljazerzen

Copy link
Copy Markdown
Member

Sorry, couldn't help it: I did a refactor of range_of_ranges. I just like to rotate code around until it looks nice.

But I found a mistake, best seen in:

take 101..110
- LIMIT 110 OFFSET 100
+ LIMIT 10 OFFSET 100

Limit directly tells how much rows should there be in the result - its applied after offset.

Comment thread prql-compiler/src/sql/translator.rs Outdated
Some(
10,
),
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would assert_yaml_snapshot work? I believe tuple and Option have Serializable trait...

Comment thread prql-compiler/src/parser.rs Resolved
Comment thread prql-compiler/src/ast/item.rs Resolved
Comment thread prql-compiler/src/sql/translator.rs Resolved
Comment thread prql-compiler/src/sql/translator.rs Outdated
/// Aggregate several ordered ranges into one, computing the intersection.
///
/// Returns a tuple of `(start, end)`, where `end` is optional.
fn range_of_ranges(ranges: Vec<Range>) -> Result<(i64, Option<i64>)> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Algorithmically, this is as concise as you can get (I think). But code-wise, this could be compressed, by:

  • first mapping all ranges to Rust's Range<i64>,
  • replacing cs > length with a check for negative length at the end,
  • replace a the two if let Some with unwrap_or and map

@aljazerzen

Copy link
Copy Markdown
Member

Ahh. I've written these comments before and they are posted only after I finish the review... oops

@aljazerzen
aljazerzen enabled auto-merge (squash) May 16, 2022 19:42
@aljazerzen
aljazerzen merged commit d314af5 into main May 16, 2022
@aljazerzen
aljazerzen deleted the offset branch May 16, 2022 19:43
}

impl<T> OrMap<T> for Option<T> {
fn or_map<F>(self, b: Self, f: F) -> Self

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea

// There can be many takes, but they have to be consecutive
// For example `take 100 | sort a | take 10` can't be one CTE.
// But this is enforced by transform order anyway.
"Take" => false,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

date
LIMIT
110 OFFSET 100
10 OFFSET 100

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was wrong in my code...

@max-sixty

Copy link
Copy Markdown
Member Author

Ahh. I've written these comments before and they are posted only after I finish the review... oops

Ha, yeah I do this a lot...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants