-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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: Disallow non-collapsable subselects with ORDER BY #72991
Changes from 1 commit
f7a89ed
d46d543
676cb9b
5c96b30
af808e6
0b52c6f
bb99b44
e8d84f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Ordering an already pre-ordered and limited subselect is not allowed, as such queries cannot be collapsed and translated into query DSL, but the require an extra ordering step on top of the results returned internally by the search/agg query. Fixes: #71158
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.planner; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.ql.index.EsIndex; | ||
import org.elasticsearch.xpack.ql.index.IndexResolution; | ||
import org.elasticsearch.xpack.sql.analysis.analyzer.Analyzer; | ||
import org.elasticsearch.xpack.sql.analysis.analyzer.VerificationException; | ||
import org.elasticsearch.xpack.sql.expression.function.SqlFunctionRegistry; | ||
import org.elasticsearch.xpack.sql.parser.SqlParser; | ||
import org.elasticsearch.xpack.sql.stats.Metrics; | ||
|
||
import static org.elasticsearch.xpack.sql.SqlTestUtils.TEST_CFG; | ||
import static org.elasticsearch.xpack.sql.types.SqlTypesTests.loadMapping; | ||
|
||
public class VerifierErrorTests extends ESTestCase { | ||
|
||
private final SqlParser parser = new SqlParser(); | ||
private final IndexResolution indexResolution = IndexResolution.valid( | ||
new EsIndex("test", loadMapping("mapping-multi-field-with-nested.json")) | ||
); | ||
private final Analyzer analyzer = new Analyzer( | ||
TEST_CFG, | ||
new SqlFunctionRegistry(), | ||
indexResolution, | ||
new org.elasticsearch.xpack.sql.analysis.analyzer.Verifier(new Metrics()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's with the fqn? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was automatic from IDE, since we are in the |
||
); | ||
private final Planner planner = new Planner(); | ||
|
||
private String error(String sql) { | ||
PlanningException e = expectThrows( | ||
PlanningException.class, | ||
() -> planner.plan(analyzer.analyze(parser.createStatement(sql), true), true) | ||
); | ||
String message = e.getMessage(); | ||
assertTrue(message.startsWith("Found ")); | ||
String pattern = "\nline "; | ||
int index = message.indexOf(pattern); | ||
return message.substring(index + pattern.length()); | ||
} | ||
|
||
public void testSubselectWithOrderByOnTopOfOrderByAndLimit() { | ||
assertEquals( | ||
"1:60: Cannot use ORDER BY on top of a subquery with ORDER BY and LIMIT", | ||
error("SELECT * FROM (SELECT * FROM test ORDER BY 1 ASC LIMIT 10) ORDER BY 2") | ||
); | ||
assertEquals( | ||
"1:72: Cannot use ORDER BY on top of a subquery with ORDER BY and LIMIT", | ||
error("SELECT * FROM (SELECT * FROM (SELECT * FROM test LIMIT 10) ORDER BY 1) ORDER BY 2") | ||
); | ||
assertEquals( | ||
"1:75: Cannot use ORDER BY on top of a subquery with ORDER BY and LIMIT", | ||
error("SELECT * FROM (SELECT * FROM (SELECT * FROM test ORDER BY 1 ASC) LIMIT 5) ORDER BY 1 DESC") | ||
); | ||
} | ||
|
||
public void testSubselectWithOrderByOnTopOfGroupByOrderByAndLimit() { | ||
assertEquals( | ||
"1:96: Cannot use ORDER BY on top of a subquery with ORDER BY and LIMIT", | ||
error( | ||
"SELECT * FROM (" + "SELECT max(int) AS max, bool FROM test GROUP BY bool ORDER BY max ASC LIMIT 10) " + "ORDER BY max DESC" | ||
) | ||
); | ||
assertEquals( | ||
"1:112: Cannot use ORDER BY on top of a subquery with ORDER BY and LIMIT", | ||
error( | ||
"SELECT * FROM (" | ||
+ "SELECT * FROM (" | ||
+ "SELECT max(int) AS max, bool FROM test GROUP BY bool ORDER BY max ASC) " | ||
+ "LIMIT 10) " | ||
+ "ORDER BY max DESC" | ||
) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently errors are handled also inside
VerifierTests
through theerror
method (iirc); the reason being the test checks both things that pass and those that do not and it's easier to see both in one place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VerifierTests is in
eql
, should I just rename itVerifierTests
also for the sql class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say so yes, easier to find it since the difference is just in the packages.