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

Incorrect result returned by UNION ALL (SQLancer-TLP) #11742

Closed
2010YOUY01 opened this issue Jul 31, 2024 · 1 comment · Fixed by #11961
Closed

Incorrect result returned by UNION ALL (SQLancer-TLP) #11742

2010YOUY01 opened this issue Jul 31, 2024 · 1 comment · Fixed by #11961
Assignees
Labels
bug Something isn't working

Comments

@2010YOUY01
Copy link
Contributor

Describe the bug

Here is Q1 select nvl(v1, 0.5) from tt;
then let Q1 union a empty set, two queries should have the same result, but it's returning a different result.
Looks like it's an incorrect casting (cast double to int), since double has higher precision, it should cast in the opposite direction

Reproducer in datafusion-cli

DataFusion CLI v40.0.0
> create table tt(v1 int);

0 row(s) fetched.
Elapsed 0.072 seconds.

> insert into tt values (1),(null);

+-------+
| count |
+-------+
| 2     |
+-------+
1 row(s) fetched.
Elapsed 0.051 seconds.

> select nvl(v1, 0.5) from tt;

+-------------------------+
| nvl(tt.v1,Float64(0.5)) |
+-------------------------+
| 1.0                     |
| 0.5                     |
+-------------------------+
2 row(s) fetched.
Elapsed 0.015 seconds.

> select nvl(v1, 0.5) from tt
union all
select null where false;

+-------------------------+
| nvl(tt.v1,Float64(0.5)) |
+-------------------------+
| 1                       |
| 0                       |
+-------------------------+
2 row(s) fetched.
Elapsed 0.007 seconds.

// Example: two number have different type, cast to the type with higher precision/range
> select 1 + 0.5;
+-------------------------+
| Int64(1) + Float64(0.5) |
+-------------------------+
| 1.5                     |
+-------------------------+
1 row(s) fetched.
Elapsed 0.008 seconds.

To Reproduce

No response

Expected behavior

No response

Additional context

Found by SQLancer #11030

@2010YOUY01 2010YOUY01 added the bug Something isn't working label Jul 31, 2024
@jonahgao jonahgao self-assigned this Aug 1, 2024
@2010YOUY01
Copy link
Contributor Author

2010YOUY01 commented Aug 2, 2024

Update: checked

Note to myself

Maybe duplicate:

TLP-Aggregate oracle violated:
    Q's result is not equalt to MIN(Q1, Q2, Q3): RS(Q) - MIN(RS(Q1), RS(Q2), RS(Q3)) is :0.1159144256643827
    Q: SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1
    Q1: SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1 WHERE t1.v3
    Q2: SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1 WHERE (NOT t1.v3)
    Q3: SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1 WHERE ((t1.v3) IS NULL)
    SELECT (SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1) - (    SELECT MIN(value)     FROM (        SELECT (SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1 WHERE t1.v3) AS value         UNION ALL         SELECT (SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1 WHERE (NOT t1.v3))         UNION ALL         SELECT (SELECT MIN((+ NVL2(LEVENSHTEIN(t1.v1, t1.v1), 1, ((t1.v0)&(t1.v0))))) FROM t1 WHERE ((t1.v3) IS NULL))     ) AS sub) AS result_difference;
    =======================================
    Reproducer:
    /*DML*/CREATE TABLE t0(v0 BIGINT, v1 BIGINT);
    /*DML*/CREATE TABLE t1(v0 DOUBLE, v1 STRING, v2 BIGINT, v3 BOOLEAN);
    /*DML*/CREATE TABLE t2(v0 BIGINT, v1 DOUBLE, v2 DOUBLE, v3 BIGINT);
    /*DML*/CREATE TABLE t3(v0 STRING);
    /*DML*/CREATE TABLE t4(v0 BOOLEAN, v1 STRING, v2 BIGINT, v3 DOUBLE);
    /*DML*/CREATE TABLE t5(v0 BOOLEAN, v1 BOOLEAN);
    /*DML*/INSERT INTO t0(v1, v0) VALUES (-10765769, 2);
    /*DML*/INSERT INTO t0(v0, v1) VALUES (-843822613, -1914693644);
    /*DML*/INSERT INTO t1(v3, v0, v2) VALUES (true, 0.1159144256643827, 1);
    /*DML*/INSERT INTO t1(v0, v1, v3) VALUES (0.2465316117209997, '-843822613', true);
    /*DML*/INSERT INTO t1(v2) VALUES (4), (-2), (-843822613);
    /*DML*/INSERT INTO t1(v3, v0, v1) VALUES (false, 0.9482603886185891, 'GGr'), (false, '-Inf'::Double, '-10765769');
    /*DML*/INSERT INTO t1(v1, v3, v2, v0) VALUES ('', false, -3, 1.381075930882357E308), ('៴vT#puV', true, 0, -0.0);
    /*DML*/INSERT INTO t1(v1, v3) VALUES ('', true);
    /*DML*/INSERT INTO t1(v2) VALUES (-843822613);
    /*DML*/INSERT INTO t1(v0, v1, v3, v2) VALUES ('-Inf'::Double, '<1', false, 956881535), (-4.0627665029805583E307, '', false, -326297564);
    /*DML*/INSERT INTO t1(v1, v2, v0, v3) VALUES ('0.1159144256643827', 1482720486, 0.470031442051111, true), ('', 0, 0.470031442051111, false);
    /*DML*/INSERT INTO t2(v1, v0, v2) VALUES (0.7697534125359654, 929737830, 0.6151720378782016);
    /*DML*/INSERT INTO t2(v3, v0, v2) VALUES (-4, 0, -1.4666596663114807E308), (3, -843822613, 0.5014898500679925);
    /*DML*/INSERT INTO t2(v3, v2, v0, v1) VALUES (-505463416, '+Inf'::Double, -419044616, '-Inf'::Double);
    /*DML*/INSERT INTO t2(v3) VALUES (-2);
    /*DML*/INSERT INTO t2(v3, v1, v0) VALUES (2049212250, -1.1814950289896128E308, -5);
    /*DML*/INSERT INTO t2(v1) VALUES (-1.563676650263876E308), (0.8904374929525254), ('NaN'::Double);
    /*DML*/INSERT INTO t3(v0) VALUES ('Ni|G8sTo9');
    /*DML*/INSERT INTO t4(v1) VALUES (']A');
    /*DML*/INSERT INTO t4(v0, v2) VALUES (true, 2049212250);
    /*DML*/INSERT INTO t4(v0, v1, v3) VALUES (false, '-419044616', 0.25850501617722565);
    /*DML*/INSERT INTO t4(v0, v3) VALUES (true, 0.0075723803913145815);
    /*DML*/INSERT INTO t5(v1, v0) VALUES (true, false), (true, true);
    /*DML*/INSERT INTO t5(v1) VALUES (false);
    /*DML*/INSERT INTO t5(v0) VALUES (false);
    /*DML*/INSERT INTO t5(v1, v0) VALUES (false, false);
    /*DML*/INSERT INTO t5(v0, v1) VALUES (false, true);
    /*DML*/INSERT INTO t5(v0, v1) VALUES (false, false), (false, true);

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

Successfully merging a pull request may close this issue.

2 participants