From fd06783825ce408514ee34df21ef8af44eebdaa4 Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Wed, 11 Dec 2019 23:53:03 +0200 Subject: [PATCH] Have COUNT DISTINCT return 0 instead of NULL for no documents matching. (#50037) (cherry picked from commit cb94731e6f41bc51c23e4aab495b64eea731a061) --- .../sql/qa/src/main/resources/agg.csv-spec | 23 +++++++++++++++++++ .../search/extractor/MetricAggExtractor.java | 4 ---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec index 182b6c2c76f39..8c3bb32484116 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec @@ -635,6 +635,29 @@ SELECT COUNT(first_name)=COUNT(DISTINCT first_name) AS areEqual, COUNT(first_nam true |90 |90 ; +aggCountWithNull +schema::COUNT(*):l|COUNT(DISTINCT languages):l|languages:bt +SELECT COUNT(*), COUNT(DISTINCT languages), languages FROM test_emp GROUP BY languages ORDER BY languages DESC; + + COUNT(*) |COUNT(DISTINCT languages)| languages +---------------+-------------------------+--------------- +21 |1 |5 +18 |1 |4 +17 |1 |3 +19 |1 |2 +15 |1 |1 +10 |0 |null +; + +aggCountZeroDocuments +schema::COUNT(*):l|COUNT(DISTINCT languages):l +SELECT COUNT(*), COUNT(DISTINCT languages) FROM test_emp WHERE languages > 100; + + COUNT(*) |COUNT(DISTINCT languages) +---------------+------------------------- +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; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java index 07309c3582200..007c7a26c4966 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java @@ -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; @@ -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); }