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

SQL: Implement TOP as an alternative to LIMIT #57428

Merged
merged 5 commits into from
Jun 2, 2020
Merged

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Jun 1, 2020

Add basic support for TOP X as a synonym to LIMIT X which is used
by MS-SQL server,
e.g.:

SELECT TOP 5 a, b, c FROM test

TOP in SQL server also supports the PERCENTAGE amd WITH TIES
keywords which this implementation doesn't.

Don't allow usage of both TOP and LIMIT in the same query.

Refers to #41195

Add basic support for `TOP X` as a synonym to LIMIT X which is used
by [MS-SQL server](https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-ver15),
e.g.:

```
SELECT TOP 5 a, b, c FROM test
```

TOP in SQL server also supports the `PERCENTAGE` amd `WITH TIES`
keywords which this implementation doesn't.

Don't allow usage of both TOP and LIMIT in the same query.

Refers to elastic#41195
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (:Query Languages/SQL)

@elasticmachine elasticmachine added the Team:QL (Deprecated) Meta label for query languages team label Jun 1, 2020
Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -243,4 +258,8 @@ public LogicalPlan visitTableName(TableNameContext ctx) {
TableIdentifier tableIdentifier = visitTableIdentifier(ctx.tableIdentifier());
return new UnresolvedRelation(source(ctx), tableIdentifier, alias, ctx.FROZEN() != null);
}

private Limit wrapWithLimit(LogicalPlan plan, Source source, Token limit) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer calling the method simply limit - it's not really wrapping but instead adding a new node to the tree.

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

LGTM. Left few minor comments.


where

count:: is a positive integer or zero indicating the maximum *possible* number of results being returned (as there might be less matches
Copy link
Contributor

Choose a reason for hiding this comment

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

I think fewer goes better with matches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was a copy from LIMIT, so fixing it there as well.

----

[NOTE]
<<sql-syntax-top, `TOP`>> and <<sql-syntax-limit, `LIMIT`>> cannot be used in combination in the same query and error is returned.
Copy link
Contributor

Choose a reason for hiding this comment

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

...cannot be used together in the same query and an error is returned otherwise sounds better? If so, same sentence was used for LIMIT and the change is needed there as well.


// TOP
SqlBaseParser.TopClauseContext topClauseContext = ctx.topClause();
if (topClauseContext != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not combining all three conditions in the same if? More readable this way?

plan = new Limit(source(limitClause), new Literal(source(limitClause),
Integer.parseInt(limit.getText()), DataTypes.INTEGER), plan);
if (plan instanceof Limit) {
throw new ParsingException(source(limitClause), "Cannot use both TOP and LIMIT in the same query");
Copy link
Member

Choose a reason for hiding this comment

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

I would rephrase the error a bit - TOP and LIMIT are not allowed in the same query - use one or the other.

@matriv matriv merged commit 2f5ab81 into elastic:master Jun 2, 2020
@matriv matriv deleted the impl-top branch June 2, 2020 07:56
matriv added a commit that referenced this pull request Jun 2, 2020
Add basic support for `TOP X` as a synonym to LIMIT X which is used
by [MS-SQL server](https://docs.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-ver15),
e.g.:

```
SELECT TOP 5 a, b, c FROM test
```

TOP in SQL server also supports the `PERCENTAGE` and `WITH TIES`
keywords which this implementation doesn't.

Don't allow usage of both TOP and LIMIT in the same query.

Refers to #41195

(cherry picked from commit 2f5ab81)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants