Skip to content

[BREAKING] Use Python-style division operator (/ is always floating point division, // is integer division) #7082

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

Merged
merged 11 commits into from
Apr 15, 2023

Conversation

Mytherin
Copy link
Collaborator

This PR reworks the division operator in DuckDB to mimic that of Python, where / is always a floating point division (even if the arguments are integers), and the current division operator is moved to //. This results in generally more intuitive results. Particularly for data science workflows integer division is rarely needed - and can often produce subtly wrong and confusing results, especially for beginners who might not be familiar with what integer division is or why integer division works in this manner.

SELECT 1/2 as d;
┌────────┐
│   d    │
│ double │
├────────┤
│    0.5 │
└────────┘
SELECT 1//2 AS d;
┌───────┐
│   d   │
│ int32 │
├───────┤
│     0 │
└───────┘

Most analytical systems seem to deviate towards this standard, with mostly older systems preferring integer division.

Integer Division: SQLite, Postgres, SQL Server
Floating Point Division: MySQL, SparkSQL, BigQuery, Snowflake, ClickHouse

In addition, other popular systems in the analytics space take this approach, such as Python itself, Pandas, NumPy, Polars, R and dplyr.

In order to faciliate a more smooth transition, we also add the integer_division flag that allows users to revert back to the old behavior, e.g.:

SELECT 1/2 AS d;
┌────────┐
│   d    │
│ double │
├────────┤
│    0.5 │
└────────┘
SET integer_division=true;
SELECT 1/2 AS d;
┌───────┐
│   d   │
│ int32 │
├───────┤
│     0 │
└───────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant