Skip to content

SQL: COUNT DISTINCT returns 0 instead of NULL for no matching docs #50037

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

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,29 @@ SELECT COUNT(first_name)=COUNT(DISTINCT first_name) AS areEqual, COUNT(first_nam
true |90 |90
;

aggCountWithNull
schema::COUNT(NULL):l|COUNT(*):l|COUNT(DISTINCT languages):l|languages:bt
SELECT COUNT(NULL), COUNT(*), COUNT(DISTINCT languages), languages FROM test_emp GROUP BY languages ORDER BY languages DESC;

COUNT(NULL) | COUNT(*) |COUNT(DISTINCT languages)| languages
---------------+---------------+-------------------------+---------------
null |21 |1 |5
null |18 |1 |4
null |17 |1 |3
null |19 |1 |2
null |15 |1 |1
null |10 |0 |null
;

aggCountZeroDocuments
schema::COUNT(NULL):l|COUNT(*):l|COUNT(DISTINCT languages):l
SELECT COUNT(NULL), COUNT(*), COUNT(DISTINCT languages) FROM test_emp WHERE languages > 100;

COUNT(NULL) | COUNT(*) |COUNT(DISTINCT languages)
---------------+---------------+-------------------------
null |0 |0
;

aggCountAllEquality
schema::areEqual:b|afn:l
SELECT COUNT(first_name)=COUNT(ALL first_name) AS areEqual, COUNT(ALL first_name) afn FROM test_emp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.aggregations.matrix.stats.InternalMatrixStats;
import org.elasticsearch.search.aggregations.metrics.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.InternalCardinality;
import org.elasticsearch.search.aggregations.metrics.InternalMax;
import org.elasticsearch.search.aggregations.metrics.InternalMin;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
Expand Down Expand Up @@ -149,9 +148,6 @@ private static boolean containsValues(InternalAggregation agg) {
if (agg instanceof InternalAvg) {
return hasValue((InternalAvg) agg);
}
if (agg instanceof InternalCardinality) {
return hasValue((InternalCardinality) agg);
}
if (agg instanceof InternalSum) {
return hasValue((InternalSum) agg);
}
Expand Down