Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new Keyword FETCH FIRST N ROWS ONLY in Presto Query #18968

Merged
merged 5 commits into from Jan 30, 2023
Merged

Introduce new Keyword FETCH FIRST N ROWS ONLY in Presto Query #18968

merged 5 commits into from Jan 30, 2023

Conversation

skairali
Copy link
Member

Introduce new Keyword FETCH FIRST N ROWS ONLY in Presto Query
Issue details : #18935

We are introducing a new keyword FETCH FIRST N ROWS ONLY which can be used in presto queries. The keyword will work in sql queries just like LIMIT keyword to limit the number of rows returned from an sql select query.

For Example : select * from table FETCH FIRST 3 ROWS ONLY will return only 3 rows as a result of the sql query.

== RELEASE NOTES ==

General Changes

Introducing a new Keyword FETCH FIRST N ROWS ONLY to the Presto Query

bentonyjoe191 and others added 2 commits January 16, 2023 23:54
Issue details : #18935

We are introducing a new keyword FETCH FIRST N ROWS ONLY which can be used in presto queries. The keyword will work in sql queries just like LIMIT keyword to limit the number of rows returned from an sql select query.

For Example : select * from table FETCH FIRST 3 ROWS ONLY will return only 3 rows as a result of the sql query.

== RELEASE NOTES ==

General Changes

Introducing a new Keyword FETCH FIRST N ROWS ONLY to the Presto Query
@skairali skairali requested a review from a team as a code owner January 24, 2023 22:16
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 24, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Contributor

@rschlussel rschlussel left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! I didn't realize until now that "LIMIT" wasn't in the sql standard.

FYI @jainxrohit @elharo - this will need to be added to crux as well.

@@ -223,6 +223,7 @@ externalRoutineName
queryNoWith:
queryTerm
(ORDER BY sortItem (',' sortItem)*)?
(FETCH FIRST fetchFirstNRows=INTEGER_VALUE ROWS ONLY)?
(OFFSET offset=INTEGER_VALUE (ROW | ROWS)?)?
Copy link
Contributor

Choose a reason for hiding this comment

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

offset comes before "FETCH FIRST"

Copy link
Member Author

@skairali skairali Jan 27, 2023

Choose a reason for hiding this comment

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

This is addressed

@@ -223,6 +223,7 @@ externalRoutineName
queryNoWith:
queryTerm
(ORDER BY sortItem (',' sortItem)*)?
(FETCH FIRST fetchFirstNRows=INTEGER_VALUE ROWS ONLY)?
(OFFSET offset=INTEGER_VALUE (ROW | ROWS)?)?
(LIMIT limit=(INTEGER_VALUE | ALL))?
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't allow queries to have both LIMIT and FETCH FIRST

Copy link
Member Author

Choose a reason for hiding this comment

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

This is handled and necessary conditions are in place

@elharo
Copy link
Contributor

elharo commented Jan 25, 2023

Why do we want this and LIMIT?

Where does the syntax come from? This seems quite verbose compared to LIMIT and is more than one keyword, or perhaps not a keyword at all. I'd like a much more complete rationale for this change before accepting it.

@rschlussel
Copy link
Contributor

Syntax comes from the SQL Spec, which is why I think we should support it. It turns out LIMIT isn't actually in the SQL Spec even though i assume it's way more widely used.

From 2016 sql spec

<query expression> ::=
  [ <with clause> ] <query expression body>
      [ <order by clause> ] [ <result offset clause> ] [ <fetch first clause> ]
...
...
<fetch first clause> ::=
  FETCH { FIRST | NEXT } [ <fetch first quantity> ] { ROW | ROWS } { ONLY | WITH TIES }
<fetch first quantity> ::=
    <fetch first row count>
  | <fetch first percentage>
...
<fetch first row count> ::=
  <simple value specification>
<fetch first percentage> ::=
  <simple value specification> PERCENT

@rschlussel
Copy link
Contributor

You can read more about the all the various ways of limiting result rows across DBs here: https://en.wikipedia.org/wiki/Select_(SQL)#Limiting_result_rows.

@kaikalur
Copy link
Contributor

kaikalur commented Jan 25, 2023

Or you could just get the whole SQL 2016 spec using my awesome SQL 16 parser :)

https://github.com/prestodb/sql/

@elharo
Copy link
Contributor

elharo commented Jan 25, 2023

I think this needs a broader discussion amongst the interested parties before committing. Standard or otherwise, we already have LIMIT. Adding a new way of doing the same thing is worth a second look. I don't think Presto anyone else have ever promised to implement the full ISO spec.

@rschlussel
Copy link
Contributor

I think this needs a broader discussion amongst the interested parties before committing. Standard or otherwise, we already have LIMIT. Adding a new way of doing the same thing is worth a second look. I don't think Presto anyone else have ever promised to implement the full ISO spec.

We don't promise to implement the full ISO spec, but for features we have, we strive to implement them in a way that is ISO SQL compliant. As a general principle, I agree it's good to have just one way of doing things. But in this case, the cost of this change is very minimal (very limited in scope), and it brings us better SQL compliance. I don't see a big downside here.

@skairali
Copy link
Member Author

@rschlussel Exactly, While fetch and limit have the same functionality, the fetch clause is an SQL standard while limit is not. Fetch was included in the SQL standard in 2008 which makes it the go to option if you want your query to be portable across several RDBMS. So this is a MUST needed in terms of SQL compatibility standpoint for all presto consumers

@skairali skairali requested review from rschlussel and removed request for presto-oss January 25, 2023 19:55
@wanglinsong wanglinsong requested review from a team and presto-oss and removed request for a team January 25, 2023 20:42
@skairali
Copy link
Member Author

skairali commented Jan 27, 2023

@rschlussel Please see that your comments are addressed.

@v-jizhang
Copy link
Contributor

@prestobot kick off tests

@rschlussel
Copy link
Contributor

Had offline discussion with @elharo, and he no longer has concerns about this change.

@rschlussel rschlussel merged commit 60803f5 into prestodb:master Jan 30, 2023
@rschlussel
Copy link
Contributor

rschlussel commented Jan 30, 2023

@skairali sorry, i merged this without noticing, but in the future, commits should be self contained, so squash all the "address comments" type of commits in to the original, and also always rebase changes on latest rather than merging. We don't like to have merge commits

@skairali
Copy link
Member Author

@rschlussel Sure

@rohanpednekar
Copy link
Contributor

@skairali, can you pls add documentation PR about this new feature? I think this should be listed here as well. Thanks!

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.

None yet

7 participants