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: selecting a literal from grouped by query generates error #41964

Merged
merged 29 commits into from
Feb 15, 2020

Conversation

codebird
Copy link
Contributor

@codebird codebird commented May 8, 2019

related to #41951 and #41413

@astefan
Copy link
Contributor

astefan commented May 9, 2019

@codebird thank you for raising the PR. We'll be looking at it, but there may be some time until then. There are several issues in flight related to literals in different scenarios and definitely this one is similar.

@jakelandis jakelandis added the :Analytics/SQL SQL querying label May 10, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

@codebird
Copy link
Contributor Author

codebird commented May 11, 2019

updates, now it resolves both #41413 #41951, but this #34583, still has issues.

@codebird
Copy link
Contributor Author

@astefan now all 3 issues are solved. 35483 is a bit hacky solution. I'll keep on looking into it... But well if you guys check it before I find anything...

@codebird
Copy link
Contributor Author

@astefan is this pull request dead? Shall I close it?

# Conflicts:
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java
# Conflicts:
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java
# Conflicts:
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java
#	x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java
@matriv
Copy link
Contributor

matriv commented Feb 5, 2020

Could you please add some unit tests (QueryTranslatorTests) and some integration tests (agg.sql-spec) ?

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

Left a couple of comments

@codebird
Copy link
Contributor Author

codebird commented Feb 9, 2020

Don't know what I was thinking to write integrity at the place of integration in my commit message

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.

This LGTM. Thank you for doing all of this and being so patient.
I hate to be the "party" breaker here, but I would like to see one more test, that @matriv also suggested in one of his reviews, but from what I can see, it's missing: #41964 (comment)

I tested the following query and it works, but I would also like to have it in the tests: SELECT 144 / 12 AS division, 1, 2 AS x, 1 + 2 FROM test_emp GROUP BY gender, salary.

Thanks again.

@codebird
Copy link
Contributor Author

codebird commented Feb 14, 2020

@astefan the test fails because of the column name of 1 + 2 H2 names it 3, ES names it 1 + 2 that's why I added an alias to the addition. Shall we open another issue concerning this, or this is acceptable?

@astefan
Copy link
Contributor

astefan commented Feb 14, 2020

@codebird an alias is fine, thank you. No need for another issue, as I believe we are doing the right thing by keeping the column name as 1 + 2.

@matriv
Copy link
Contributor

matriv commented Feb 14, 2020

@elasticmachine run elasticsearch-ci/2

@matriv
Copy link
Contributor

matriv commented Feb 14, 2020

@codebird I'd like to ask you for one more change please.
After some further investigation we came up with a bit different approach on the fix regarding
the resolution of attributes. The logic is the same though as in your fix: try to resolve groupings and/or aggregates based on the child plan (for example when there is a WHERE clause and the child plan is a filter). So instead of this code: https://github.com/elastic/elasticsearch/pull/41964/files#diff-bb55908282831d2f432f4a4650d55521R345
could you please add the following:

@@ -733,6 +720,19 @@ public class Analyzer extends RuleExecutor<LogicalPlan> {
                 }
             }

+            // Try to resolve aggregates and groupings based on the child plan
+            if (plan instanceof Aggregate) {
+                Aggregate a = (Aggregate) plan;
+                LogicalPlan child = a.child();
+                List<Expression> newGroupings = new ArrayList<>(a.groupings().size());
+                a.groupings().forEach(e -> newGroupings.add(tryResolveExpression(e, child)));
+                List<NamedExpression> newAggregates = new ArrayList<>(a.aggregates().size());
+                a.aggregates().forEach(e -> newAggregates.add(tryResolveExpression(e, child)));
+                if (newAggregates.equals(a.aggregates()) == false || newGroupings.equals(a.groupings()) == false) {
+                    return new Aggregate(a.source(), child, newGroupings, newAggregates);
+                }
+            }
+
             return plan;
         }

@codebird
Copy link
Contributor Author

much more elegant indeed.

@matriv matriv merged commit 45b8580 into elastic:master Feb 15, 2020
@matriv
Copy link
Contributor

matriv commented Feb 15, 2020

Thanks a lot once again @codebird for your patience and your overall effort on this contribution!

@codebird
Copy link
Contributor Author

Np.thanks for the good mentoring @matriv

matriv pushed a commit that referenced this pull request Feb 15, 2020
Translate to an agg query even if only literals are selected,
so that the correct number of rows is returned (number of buckets).

Fix issue with key only in GROUP BY (not in select) and WHERE clause:
Resolve aggregates and groupings based on the child plan which holds
the info info for all the fields of the underlying table.

Fixes: #41951
Fixes: #41413
(cherry picked from commit 45b8580)
matriv pushed a commit that referenced this pull request Feb 15, 2020
Translate to an agg query even if only literals are selected,
so that the correct number of rows is returned (number of buckets).

Fix issue with key only in GROUP BY (not in select) and WHERE clause:
Resolve aggregates and groupings based on the child plan which holds
the info info for all the fields of the underlying table.

Fixes: #41951
Fixes: #41413
(cherry picked from commit 45b8580)
@codebird codebird deleted the issue#41413 branch February 15, 2020 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants