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

Potential hack for integer-safe addition in SQLite #253

Open
AnyhowStep opened this issue Feb 18, 2020 · 3 comments
Open

Potential hack for integer-safe addition in SQLite #253

AnyhowStep opened this issue Feb 18, 2020 · 3 comments
Assignees
Labels

Comments

@AnyhowStep
Copy link
Owner

SELECT 
9223372036854775807,
9223372036854775807+9223372036854775807, -- Casts to real and gives lossy result
typeof(9223372036854775807),
typeof(9223372036854775807+9223372036854775807);

SELECT
(SELECT SUM(x) FROM (SELECT 9223372036854775807 AS x UNION ALL SELECT 9223372036854775807)); -- integer overflow error

According to the SQLite documentation,
https://www.sqlite.org/lang_aggfunc.html

Sum() will throw an "integer overflow" exception if all inputs are integers or NULL and an integer overflow occurs at any point during the computation.

@AnyhowStep AnyhowStep self-assigned this Feb 18, 2020
@AnyhowStep
Copy link
Owner Author

We are abusing an aggregate function, derived table, and compound query just to get integer overflow errors...

@AnyhowStep
Copy link
Owner Author

AnyhowStep commented Feb 18, 2020

I don't know what madness inspired me to come up with this workaround. I regret it now because I'm seriously considering this...

user-defined functions are more "elegant" but it means having to always create it, and the AST isn't technically portable unless the special bigint_add() function is added to every SQLite database you want to run the query on

@AnyhowStep
Copy link
Owner Author

We want a throw-on-overflow addition operator, and not a cast-on-overflow operator, because of this,

SELECT
(9223372036854775807+9223372036854775807)/9973

This should throw.
But on SQLite, we get 1849668512354311.8

It is decidedly not an integer.

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

No branches or pull requests

1 participant