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: Fix SUM(all zeroes) to return 0 instead of NULL #65796

Merged
merged 7 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@

palesz marked this conversation as resolved.
Show resolved Hide resolved
aggregatingAllZerosWithFirst
palesz marked this conversation as resolved.
Show resolved Hide resolved
schema::FIRST_AllZeros:i
SELECT FIRST(bytes_in) as "FIRST_AllZeros" FROM logs WHERE bytes_in = 0;
palesz marked this conversation as resolved.
Show resolved Hide resolved

FIRST_AllZeros
---------------
0
;


aggregatingAllNullsWithFirst
schema::FIRST_AllNulls:i
SELECT FIRST(bytes_out) as "FIRST_AllNulls" FROM logs WHERE bytes_out IS NULL;

FIRST_AllNulls
---------------
null
;


aggregatingAllZerosWithLast
schema::LAST_AllZeros:i
SELECT LAST(bytes_in) as "LAST_AllZeros" FROM logs WHERE bytes_in = 0;

LAST_AllZeros
---------------
0
;


aggregatingAllNullsWithLast
schema::LAST_AllNulls:i
SELECT LAST(bytes_out) as "LAST_AllNulls" FROM logs WHERE bytes_out IS NULL;

LAST_AllNulls
---------------
null
;


aggregatingAllZerosWithCount
schema::COUNT_AllZeros:l
SELECT COUNT(bytes_in) as "COUNT_AllZeros" FROM logs WHERE bytes_in = 0;

COUNT_AllZeros
---------------
2
;


aggregatingAllNullsWithCount
schema::COUNT_AllNulls:l
SELECT COUNT(bytes_out) as "COUNT_AllNulls" FROM logs WHERE bytes_out IS NULL;

COUNT_AllNulls
---------------
0
;


aggregatingAllZerosWithCountStar
schema::COUNT_AllZeros:l
SELECT COUNT(*) as "COUNT_AllZeros" FROM logs WHERE bytes_in = 0;

COUNT_AllZeros
---------------
2
;


aggregatingAllNullsWithCountStar
schema::COUNT_AllNulls:l
SELECT COUNT(*) as "COUNT_AllNulls" FROM logs WHERE bytes_out IS NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

We already have a test that deals with this scenario: SELECT COUNT(*) count FROM test_emp WHERE first_name IS NULL


COUNT_AllNulls
---------------
51
;


aggregatingAllZerosWithAvg
schema::AVG_AllZeros:d
SELECT AVG(bytes_in) as "AVG_AllZeros" FROM logs WHERE bytes_in = 0;

AVG_AllZeros
---------------
0.0
;


aggregatingAllNullsWithAvg
schema::AVG_AllNulls:d
SELECT AVG(bytes_out) as "AVG_AllNulls" FROM logs WHERE bytes_out IS NULL;

AVG_AllNulls
---------------
null
;


aggregatingAllZerosWithMin
schema::MIN_AllZeros:i
SELECT MIN(bytes_in) as "MIN_AllZeros" FROM logs WHERE bytes_in = 0;

MIN_AllZeros
---------------
0
;


aggregatingAllNullsWithMin
schema::MIN_AllNulls:i
SELECT MIN(bytes_out) as "MIN_AllNulls" FROM logs WHERE bytes_out IS NULL;

MIN_AllNulls
---------------
null
;


aggregatingAllZerosWithMax
schema::MAX_AllZeros:i
SELECT MAX(bytes_in) as "MAX_AllZeros" FROM logs WHERE bytes_in = 0;

MAX_AllZeros
---------------
0
;


aggregatingAllNullsWithMax
schema::MAX_AllNulls:i
SELECT MAX(bytes_out) as "MAX_AllNulls" FROM logs WHERE bytes_out IS NULL;

MAX_AllNulls
---------------
null
;


aggregatingAllZerosWithSum
schema::SUM_AllZeros:i
SELECT SUM(bytes_in) as "SUM_AllZeros" FROM logs WHERE bytes_in = 0;

SUM_AllZeros
---------------
0
;


aggregatingAllNullsWithSum
schema::SUM_AllNulls:i
SELECT SUM(bytes_out) as "SUM_AllNulls" FROM logs WHERE bytes_out IS NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would look in checking if adding or changing one of the entries in logs to have bytes_in as null would have a big impact on the existing tests. If not, then I would make the change (either adding an entry or changing an existent one) and then a more complex query like SELECT bytes_in, SUM(bytes_in) as SUM_AllNulls, MIN(bytes_in), MAX(bytes_in), AVG(bytes_in) FROM logs WHERE bytes_in = 0 OR bytes_in IS NULL GROUP BY bytes_in would be possible.


SUM_AllNulls
---------------
null
;


aggregatingAllZerosWithPercentile
schema::PERCENTILE_AllZeros:d
SELECT PERCENTILE(bytes_in, 0) as "PERCENTILE_AllZeros" FROM logs WHERE bytes_in = 0;

PERCENTILE_AllZeros
-------------------
0.0
;


aggregatingAllNullsWithPercentile
schema::PERCENTILE_AllNulls:d
SELECT PERCENTILE(bytes_out, 0) as "PERCENTILE_AllNulls" FROM logs WHERE bytes_out IS NULL;

PERCENTILE_AllNulls
-------------------
null
;


aggregatingAllZerosWithPercentileRank
schema::PERCENTILE_RANK_AllZeros:d
SELECT PERCENTILE_RANK(bytes_in, 0) as "PERCENTILE_RANK_AllZeros" FROM logs WHERE bytes_in = 0;

PERCENTILE_RANK_AllZeros
------------------------
100.0
;


aggregatingAllNullsWithPercentileRank
schema::PERCENTILE_RANK_AllNulls:d
SELECT PERCENTILE_RANK(bytes_out, 0) as "PERCENTILE_RANK_AllNulls" FROM logs WHERE bytes_out IS NULL;

PERCENTILE_RANK_AllNulls
------------------------
null
;


aggregatingAllZerosWithSumOfSquares
schema::SUM_OF_SQUARES_AllZeros:d
SELECT SUM_OF_SQUARES(bytes_in) as "SUM_OF_SQUARES_AllZeros" FROM logs WHERE bytes_in = 0;

SUM_OF_SQUARES_AllZeros
-----------------------
0.0
;


aggregatingAllNullsWithSumOfSquares
schema::SUM_OF_SQUARES_AllNulls:d
SELECT SUM_OF_SQUARES(bytes_out) as "SUM_OF_SQUARES_AllNulls" FROM logs WHERE bytes_out IS NULL;

SUM_OF_SQUARES_AllNulls
-----------------------
null
;


aggregatingAllZerosWithStddevPop
schema::STDDEV_POP_AllZeros:d
SELECT STDDEV_POP(bytes_in) as "STDDEV_POP_AllZeros" FROM logs WHERE bytes_in = 0;

STDDEV_POP_AllZeros
-------------------
0.0
;


aggregatingAllNullsWithStddevPop
schema::STDDEV_POP_AllNulls:d
SELECT STDDEV_POP(bytes_out) as "STDDEV_POP_AllNulls" FROM logs WHERE bytes_out IS NULL;

STDDEV_POP_AllNulls
-------------------
null
;


aggregatingAllZerosWithStddevSamp
schema::STDDEV_SAMP_AllZeros:d
SELECT STDDEV_SAMP(bytes_in) as "STDDEV_SAMP_AllZeros" FROM logs WHERE bytes_in = 0;

STDDEV_SAMP_AllZeros
--------------------
0.0
;


aggregatingAllNullsWithStddevSamp
schema::STDDEV_SAMP_AllNulls:d
SELECT STDDEV_SAMP(bytes_out) as "STDDEV_SAMP_AllNulls" FROM logs WHERE bytes_out IS NULL;

STDDEV_SAMP_AllNulls
--------------------
null
;


aggregatingAllZerosWithVarSamp
schema::VAR_SAMP_AllZeros:d
SELECT VAR_SAMP(bytes_in) as "VAR_SAMP_AllZeros" FROM logs WHERE bytes_in = 0;

VAR_SAMP_AllZeros
-----------------
0.0
;


aggregatingAllNullsWithVarSamp
schema::VAR_SAMP_AllNulls:d
SELECT VAR_SAMP(bytes_out) as "VAR_SAMP_AllNulls" FROM logs WHERE bytes_out IS NULL;

VAR_SAMP_AllNulls
-----------------
null
;


aggregatingAllZerosWithVarPop
schema::VAR_POP_AllZeros:d
SELECT VAR_POP(bytes_in) as "VAR_POP_AllZeros" FROM logs WHERE bytes_in = 0;

VAR_POP_AllZeros
----------------
0.0
;


aggregatingAllNullsWithVarPop
schema::VAR_POP_AllNulls:d
SELECT VAR_POP(bytes_out) as "VAR_POP_AllNulls" FROM logs WHERE bytes_out IS NULL;

VAR_POP_AllNulls
----------------
null
;


aggregatingAllZerosWithSkewness
schema::SKEWNESS_AllZeros:d
SELECT SKEWNESS(bytes_in) as "SKEWNESS_AllZeros" FROM logs WHERE bytes_in = 0;

SKEWNESS_AllZeros
-----------------
NaN
;


aggregatingAllNullsWithSkewness
schema::SKEWNESS_AllNulls:d
SELECT SKEWNESS(bytes_out) as "SKEWNESS_AllNulls" FROM logs WHERE bytes_out IS NULL;

SKEWNESS_AllNulls
-----------------
null
;


aggregatingAllZerosWithMad
schema::MAD_AllZeros:d
SELECT MAD(bytes_in) as "MAD_AllZeros" FROM logs WHERE bytes_in = 0;

MAD_AllZeros
---------------
0.0
;


aggregatingAllNullsWithMad
schema::MAD_AllNulls:d
SELECT MAD(bytes_out) as "MAD_AllNulls" FROM logs WHERE bytes_out IS NULL;

MAD_AllNulls
---------------
NaN
;


aggregatingAllZerosWithKurtosis
schema::KURTOSIS_AllZeros:d
SELECT KURTOSIS(bytes_in) as "KURTOSIS_AllZeros" FROM logs WHERE bytes_in = 0;

KURTOSIS_AllZeros
-----------------
NaN
;


aggregatingAllNullsWithKurtosis
schema::KURTOSIS_AllNulls:d
SELECT KURTOSIS(bytes_out) as "KURTOSIS_AllNulls" FROM logs WHERE bytes_out IS NULL;

KURTOSIS_AllNulls
-----------------
null
;
Loading