Skip to content

Allow comparison between boolean and int values #16798

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

osipovartem
Copy link
Contributor

Which issue does this PR close?

Closes #16797

Rationale for this change

This PR enables comparison between boolean and interger types which already supported by arrow cast

What changes are included in this PR?

Added boolean_coercion

Are these changes tested?

Added tests

Are there any user-facing changes?

No

@github-actions github-actions bot added the logical-expr Logical plan and expressions label Jul 16, 2025
@github-actions github-actions bot added the optimizer Optimizer rules label Jul 16, 2025
@github-actions github-actions bot added the physical-expr Changes to the physical-expr crates label Jul 16, 2025
@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label Jul 16, 2025
@2010YOUY01
Copy link
Contributor

what about using explicit casting in applications? For example:

> select not(arrow_cast(1, 'Boolean'));
+------------------------------------------+
| NOT arrow_cast(Int64(1),Utf8("Boolean")) |
+------------------------------------------+
| false                                    |
+------------------------------------------+

IIRC, PostgreSQL doesn't support such comparisons, while DuckDB does.
I think this rule may feel natural primarily to C++ programmers (perhaps that’s why DuckDB allows such coercion), but SQL is more application-oriented. Many SQL users might not be familiar with this convention, so introducing this coercion rule might make the application more error-prone.

@comphead
Copy link
Contributor

what about using explicit casting in applications? For example:

> select not(arrow_cast(1, 'Boolean'));
+------------------------------------------+
| NOT arrow_cast(Int64(1),Utf8("Boolean")) |
+------------------------------------------+
| false                                    |
+------------------------------------------+

IIRC, PostgreSQL doesn't support such comparisons

Just tried PG

select not(cast(1 as boolean)) as a 


   a
----
false

Am I missing something? 🤔

@2010YOUY01
Copy link
Contributor

2010YOUY01 commented Jul 18, 2025

@comphead We're talking about implicit conversions

PG:
postgres=# SELECT not(1);
ERROR: argument of NOT must be type boolean, not type integer
LINE 1: SELECT not(1);

This PR:
query BB
SELECT not(1), not(0)

false true

@comphead
Copy link
Contributor

@comphead We're talking about implicit conversions

PG: postgres=# SELECT not(1); ERROR: argument of NOT must be type boolean, not type integer LINE 1: SELECT not(1);

This PR: query BB SELECT not(1), not(0)

false true

Thanks @2010YOUY01
I agree for implicit conversion it might be confusing, DuckDB works

D select not(2);
┌─────────┐
│ (NOT 2) │
│ boolean │
├─────────┤
│ false   │
└─────────┘
D select not(-1);
┌──────────┐
│ (NOT -1) │
│ boolean  │
├──────────┤
│ false    │
└──────────┘
D select not(0);
┌─────────┐
│ (NOT 0) │
│ boolean │
├─────────┤
│ true    │
└─────────┘

But IMHO we should stick to PG behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions optimizer Optimizer rules physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow comparison netween booleans and integers
3 participants