Skip to content

Unexpected result when using BETWEEN and CASE WHEN #15602

Closed

Description

@suyZhong

What happens?

Consider the following test case. The second query returned false, showing that the expression (((CASE t0.c0 WHEN t0.c0 THEN t0.c0 END ) BETWEEN 1 AND t0.c0)AND(t0.c0 <= 0)) should be evaluated to false; however, the third query return -1, which is unexpected.

To Reproduce

CREATE TABLE  t0(c0 INT);
INSERT INTO t0( c0) VALUES ( -1);

SELECT * FROM t0; -- -1
SELECT (((CASE t0.c0 WHEN t0.c0 THEN t0.c0 END ) BETWEEN 1 AND t0.c0)AND(t0.c0 <= 0)) FROM t0; -- false
SELECT * FROM t0 WHERE (((CASE t0.c0 WHEN t0.c0 THEN t0.c0 END ) BETWEEN 1 AND t0.c0)AND(t0.c0 <= 0)) ;
-- Expected: empty table
-- Actual: -1

OS:

Ubuntu 22.04

DuckDB Version:

v1.1.4-dev4108 acdbf60

DuckDB Client:

CLI

Hardware:

No response

Full Name:

Suyang Zhong

Affiliation:

NUS

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a source build

Did you include all relevant data sets for reproducing the issue?

Not applicable - the reproduction does not require a data set

Did you include all code required to reproduce the issue?

  • Yes, I have

Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

  • Yes, I have

Activity

  1. szarnyasg commented on Jan 8, 2025

    @szarnyasg
    Collaborator

    Thanks! Indeed, Postgres returns an empty table:

    postgres=# CREATE TABLE  t0(c0 INT);
    CREATE TABLE
    postgres=# INSERT INTO t0( c0) VALUES ( -1);
    INSERT 0 1
    postgres=#
    postgres=# SELECT * FROM t0; -- -1
     c0
    ----
     -1
    (1 row)
    
    postgres=# SELECT (((CASE t0.c0 WHEN t0.c0 THEN t0.c0 END ) BETWEEN 1 AND t0.c0)AND(t0.c0 <= 0)) FROM t0; -- false
     ?column?
    ----------
     f
    (1 row)
    
    postgres=# SELECT * FROM t0 WHERE (((CASE t0.c0 WHEN t0.c0 THEN t0.c0 END ) BETWEEN 1 AND t0.c0)AND(t0.c0 <= 0)) ;
     c0
    ----
    (0 rows)
    
  2. changed the title [-]Unexpected result when using `BETWEEN ` and `CASE WHEN`[/-] [+]Unexpected result when using `BETWEEN` and `CASE WHEN`[/+] on Jan 8, 2025
  3. added 2 commits that reference this issue on Jan 28, 2025
    4c0a394
    ec07e87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions