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

Non-deterministic SUM() aggregate function result (found by SQLancer-TLP) #11264

Closed
2010YOUY01 opened this issue Jul 4, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@2010YOUY01
Copy link
Contributor

2010YOUY01 commented Jul 4, 2024

Describe the bug

This aggregate query returns different result from run to run
Much appreciated if anyone could rerun & confirm, or minimize this bug!

To Reproduce

Executing following statements in datafusion-cli to setup tables

/*1*/ /*DML*/ CREATE TABLE t0(v0 BIGINT, v1 DOUBLE, v2 BOOLEAN);
/*2*/ /*DML*/ CREATE TABLE t64(v0 DOUBLE);
/*3*/ /*DML*/ CREATE TABLE t1(v0 DOUBLE);
/*4*/ /*DML*/ CREATE TABLE t97(v0 BOOLEAN);
/*5*/ /*DML*/ CREATE TABLE t73(v0 DOUBLE, v1 BIGINT, v2 BIGINT);
/*6*/ /*DML*/ INSERT INTO t64(v0) VALUES (0.9381581402445061), (+0.0), (-0.0);
/*7*/ /*DML*/ INSERT INTO t64(v0) VALUES (NULL), (1.5753477614031765E308), (0.006050796796817082);
/*8*/ /*DML*/ INSERT INTO t64(v0) VALUES (0.2409806315244235), (0.2210083496149292);
/*9*/ /*DML*/ INSERT INTO t64(v0) VALUES (0.7884859643116078);
/*10*/ /*DML*/ INSERT INTO t64(v0) VALUES (1.0520311921422949E308), (7.328703000726609E307);
/*11*/ /*DML*/ INSERT INTO t64(v0) VALUES (1.1395407049358574E308), (-1.3151865100063228E308);
/*12*/ /*DML*/ INSERT INTO t64(v0) VALUES (0.5051666339856035), (0.4220858619783221), (-1.2830593404326414E308);
/*13*/ /*DML*/ INSERT INTO t64(v0) VALUES (0.2668989395491763);
/*14*/ /*DML*/ INSERT INTO t64(v0) VALUES (0.38341817667602074);
/*15*/ /*DML*/ INSERT INTO t73(v0) VALUES (0.04629472695473025);
/*16*/ /*DML*/ INSERT INTO t73(v2, v0, v1) VALUES (389871702, '-Inf'::Double, 389871702);
/*17*/ /*DML*/ INSERT INTO t97(v0) VALUES (true);
/*18*/ /*DML*/ INSERT INTO t97(v0) VALUES (true);
/*19*/ /*DML*/ INSERT INTO t97(v0) VALUES (false);
/*20*/ /*DML*/ INSERT INTO t97(v0) VALUES (false), (false);
/*21*/ /*DML*/ INSERT INTO t97(v0) VALUES (false);
/*22*/ /*DML*/ INSERT INTO t97(v0) VALUES (false), (false);
/*23*/ /*DML*/ INSERT INTO t97(v0) VALUES (false);
/*24*/ /*DML*/ INSERT INTO t0(v0, v1) VALUES (611863454, -1.7759381831381458E308), (389871702, -0.0);
/*25*/ /*DML*/ INSERT INTO t0(v0, v2) VALUES (389871702, false);
/*26*/ /*DML*/ INSERT INTO t0(v0, v1, v2) VALUES (NULL, 0.2824144132029637, false);
/*27*/ /*DML*/ INSERT INTO t0(v1, v0, v2) VALUES (8.632837270764036E307, 389871702, true);
/*28*/ /*DML*/ INSERT INTO t0(v2) VALUES (true), (false);
/*29*/ /*DML*/ INSERT INTO t0(v2, v0) VALUES (true, -1448403337);
/*30*/ /*DML*/ INSERT INTO t1(v0) VALUES (0.2668989395491763);
/*31*/ /*DML*/ INSERT INTO t1(v0) VALUES (+0.0);
/*32*/ /*DML*/ INSERT INTO t1(v0) VALUES ('-Inf'::Double), ('NaN'::Double), (+0.0);
/*33*/ /*DML*/ INSERT INTO t1(v0) VALUES (0.38341817667602074);
/*34*/ /*DML*/ INSERT INTO t1(v0) VALUES (+0.0), (6.11863454E8);
/*35*/ /*DML*/ INSERT INTO t1(v0) VALUES (-7.024071633675249E307);
/*36*/ /*DML*/ INSERT INTO t1(v0) VALUES (-0.0), (0.6980448162044035), (-8.299172784637644E307);
/*37*/ /*DML*/ INSERT INTO t1(v0) VALUES (0.8169452791572056);
/*38*/ /*DML*/ INSERT INTO t1(v0) VALUES (+0.0), (0.3710336048023243);

Then execute the query. It seems around half of the result is Inf and another half is NaN 🤔

> SELECT SUM(t64.v0) FROM t97, t73, t64 WHERE t97.v0;
+-------------+
| sum(t64.v0) |
+-------------+
| NaN         |
+-------------+
1 row(s) fetched.
Elapsed 0.008 seconds.

> SELECT SUM(t64.v0) FROM t97, t73, t64 WHERE t97.v0;
+-------------+
| sum(t64.v0) |
+-------------+
| inf         |
+-------------+
1 row(s) fetched.
Elapsed 0.008 seconds.

> SELECT SUM(t64.v0) FROM t97, t73, t64 WHERE t97.v0;
+-------------+
| sum(t64.v0) |
+-------------+
| NaN         |
+-------------+
1 row(s) fetched.
Elapsed 0.008 seconds.

Expected behavior

returns Inf

Additional context

Found by SQLancer-TLP(Aggr) #11030

@2010YOUY01 2010YOUY01 added the bug Something isn't working label Jul 4, 2024
@2010YOUY01
Copy link
Contributor Author

If aggregation is executed in multiple partitions, and the order to merge partial aggregation result is not deterministic: then it's possible +Inf and -Inf will be added first in some runs, and generate NaN as the final result.
I think this behavior is reasonable, so close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant