hex(NULL) Bug
(1) By Sunny Saini (SunnySaini_com) on 2022-09-14 01:23:24 [link] [source]
Select typeof (hex (Null)) Result;
Result
text
It should be Null.
Also, why despite of requests by large number of people around the world, the unhex() function is not available as core function?
(2) By Stephan Beal (stephan) on 2022-09-14 03:00:40 in reply to 1 [link] [source]
It should be Null.
In the parlance of Wikipedia: "citation needed"
The docs clearly say it returns a string:
The hex() function interprets its argument as a BLOB and returns a string which is the upper-case hexadecimal rendering of the content of that blob.
Also, why despite of requests by large number of people around the world,
Again: "citation needed". A search for "unhex" in this forum reveals exactly one relevant thread (not including this one).
... the unhex() function is not available as core function?
Keith succinctly explains why the hex transformation is not generically reversible in 25da10332dd9a1eb.
(3) By Michael A. Cleverly (cleverly) on 2022-09-14 03:01:11 in reply to 1 [link] [source]
The result of hex(null)
is not a null. It is an empty string.
SQLite version 3.39.3 2022-09-05 11:02:23
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> select count(*) where hex(null) is null;
0
sqlite> select count(*) where hex(null) = '';
1
(4) By Sunny Saini (SunnySaini_com) on 2022-09-14 04:09:18 in reply to 2 [source]
It's documented but when we cast null as text, real or integer we get null. Also typeof many functions on null is null.
For example
Select typeof (max(null));
is null.
Don't you think by analogy type of (hex(null) ) should also be null.
Thanks for the link to the forum post of unhex methods.
(5) By Sunny Saini (SunnySaini_com) on 2022-09-14 04:12:02 in reply to 3 [link] [source]
Interesting
(6) By ddevienne on 2022-09-14 08:05:13 in reply to 4 [link] [source]
Don't you think by analogy type of (hex(null) ) should also be null.
You're comparing apples and oranges though, IMO.
max()
is polymorphic, in a way, since will return different types based on arguments.
While hex()
by design always returns text
, whatever its argument's type.
So it could have returned a null
for a null
input, but it's just as correct,
given the semantic of that function, to return an empty text
value too.
(7) By Michael A. Cleverly (cleverly) on 2022-09-14 16:08:18 in reply to 5 [link] [source]
If you really need the result to be null then just wrap it in a nullif()
:
SQLite version 3.39.3 2022-09-05 11:02:23
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> select typeof(nullif(hex(null),''));
null
sqlite> select typeof(nullif(hex(42),''));
text
(8) By Sunny Saini (SunnySaini_com) on 2022-09-14 16:33:48 in reply to 6 [link] [source]
What about
typeof (upper(Null))
typeof (lower(Null))
typeof (length(Null))
These are not polymorphic functions. Typeof these is also NULL.
(9) By Sunny Saini (SunnySaini_com) on 2022-09-14 16:34:30 in reply to 7 [link] [source]
Thanks
(10) By David Raymond (dvdraymond) on 2022-09-14 16:56:22 in reply to 7 [link] [source]
Can't use that, because that will return NULL if the input is the empty string. You really need a CASE statement or iif() function. case when X is not null then hex(X) end; or iif(X is null, null, hex(X));
(11) By ddevienne on 2022-09-14 17:44:36 in reply to 8 [link] [source]
I'm not sure where you're getting at, honestly.
All I'm saying is that both returning null
and returning ''
for hex(null)
are reasonable choices.
I tend to agree that returning null
might have been a better choice, for consistency's sake, as you pointed out.
But that's water under the bridge, as they say. A long time ago, the behavior of hex(null)
was decided, and it's not going to change now.
That would be bad for backward-compatibility, obviously. So just learn to live with it? :)
(12) By Sunny Saini (SunnySaini_com) on 2022-09-14 21:52:34 in reply to 10 [link] [source]
These are better workouts, thanks.
(13) By Sunny Saini (SunnySaini_com) on 2022-09-14 21:56:40 in reply to 11 [link] [source]
You seem right, not changing the code will be better idea. I'll manage by queries given by David Raymond above.
(14) By Sunny Saini (SunnySaini_com) on 2022-09-19 07:55:37 in reply to 11 [link] [source]
Excuse me for raising the issue again but, this bug is irritating me and I think this must be fixed. Here are the logical points for reasoning.
Almost all functions and operations on Nulls yield Null.
There is no function in SQLite that can Cast Null as text but, this hex function is doing so.
Also I don't think that this is water under the bridge or water over the dam. It can be corrected by offering it as Pragma statements, for example:
Pragma legacy_hex = 0
For the reputation of SQLite, bugs must be fixed.
(15) By Stephan Beal (stephan) on 2022-09-19 08:01:30 in reply to 14 [link] [source]
Excuse me for raising the issue again but, this bug is irritating me
A "bug" is incorrect behavior. What's irritating you is that the designers explicitly chose and documented behavior which is "differently correct" than your notion of correct.
For the reputation of SQLite, bugs must be fixed.
Bugs, yes, but not behaviors which work exactly as they are documented to. That is, by definition, not a bug.
(16) By Sunny Saini (SunnySaini_com) on 2022-09-19 08:13:47 in reply to 15 [link] [source]
Probably, illogical behaviour is a bug and documented bug still remains a bug.
What do you say about my two reasoning points?
(17.1) By Chris Locke (chrisjlocke1) on 2022-09-19 08:44:31 edited from 17.0 in reply to 14 [link] [source]
It's not a bug. A bug is when you expect X but get Y. Eg, turn the steering wheel left to turn left, but the car turns right. If a function returns a textual value, this isn't a bug. If the function returned a numeric function or a date, then that would be a bug. You're getting text. No bug. If the hex function returns a textual value, the 'typeof' is always going to be text.
but, this hex function is doing so
It's not though. The function returns a textual value. So you're not getting the typeof a null as the function returns a textual value. The typeof a textual value is text. As its a textual value.
You can code around this, so unsure why it's still an issue?
(18) By Stephan Beal (stephan) on 2022-09-19 08:40:55 in reply to 16 [link] [source]
What do you say about my two reasoning points?
That they are irrelevant because, as it was previously put, hex()'s behavior is water under the bridge. The only person who is known to be losing sleep over its documented, correct, and non-bug behavior is you. That's not justification enough to add a new pragma which has to be supported and maintained for the life of the project (officially until at least 2050). Given sqlite's unwavering requirement for 100% branch test coverage, every new line of C code has a maintenance burden attached to it which most projects do not have, and the personal preferences of a single user, or a disproportionately loud minority of users, do not justify that burden.
(19.1) By Ryan Smith (cuz) on 2022-09-19 08:58:05 edited from 19.0 in reply to 14 [link] [source]
Almost all functions and operations on Nulls yield Null.
Yeah, and some don't.
There is no function in SQLite that can Cast Null as text but, this hex function is doing so.
The Hex function, like every function except "CAST" or "CONVERT" (in other SQL engines), doesn't cast values as anything.
When you use the function "HEX()" you specifically ask the engine to show you the bytes as a string of hexadecimal characters. If the bytes in the target value is 0x102F then you expect the text string '102f' back, and if there are no bytes in the target value, you expect the empty string '' back, but you never ever expect anything other than a text string back.
I use this function quite extensively to read bytes that I can transfer in text-based structures (like JSON), I would be quite irritated by it suddenly starting to return something other than a string showing the exact bytes (or lack thereof) for the target value (or non-value).
In Hex, a Zero byte will return '00', the only possible way to represent a null (or no-value) [Edit: as a hex string] is by showing no bytes, this is useful. Why are you intent to make a Text-function return anything other than text? The only reason some other functions HAVE to return null is that they have no other way to display "non-value" or "null-value" (whichever term you like best). HEX() does not have that problem, it has a very clear way to display those.
Your arguments amount to nothing more than "I don't understand why it's like this", but let's imagine you did have a good reason for wanting NULL returns, it still isn't how it works and is documented to work, and so it still isn't a bug. It also isn't a "documented bug" as you seem to suggest, it works as documented, meaning it is not a bug. Your contention seems to lean more towards "design flaw" - like you could argue cars would have better traction if they all had six wheels rather than four, and sure, you'd have a point, but there would be downsides to that too. Most importantly, you may call a 4-wheeled vehicle "a bad design" if you like, but you can't call it a bug - it is exactly how the designers intended it to be, documented it to be, and advertised it to be.
(20) By Ryan Smith (cuz) on 2022-09-19 09:10:06 in reply to 1 [link] [source]
Also, why despite of requests by large number of people around the world, the unhex() function is not available as core function?
Quick curiosity questions:
- Which engines support Unhex()?
- In these engines, what is the resulting value of Unhex('') - i.e. getting the Unhex() value of an empty string?
(21) By Sunny Saini (SunnySaini_com) on 2022-09-19 11:26:28 in reply to 18 [link] [source]
Given sqlite's unwavering requirement for 100% branch test coverage, every new line of C code has a maintenance burden attached to it
OK
(22) By Sunny Saini (SunnySaini_com) on 2022-09-19 11:31:04 in reply to 19.1 [link] [source]
I use this function quite extensively to read bytes that I can transfer in text-based structures (like JSON)
OK
(23) By Sunny Saini (SunnySaini_com) on 2022-09-19 11:32:58 in reply to 20 [link] [source]
The unhex() issue doubt was resolved above.
(24) By Sunny Saini (SunnySaini_com) on 2022-09-19 11:40:06 in reply to 18 [link] [source]
life of the project (officially until at least 2050)
Curious! will SQLite not be maintained after 28 years from now?!
(25.1) By Chris Locke (chrisjlocke1) on 2022-09-19 12:12:37 edited from 25.0 in reply to 24 [link] [source]
The official support is until 2050. You can sleep soundly that it'll be supported until 2050. That's the 'promise'. It may be maintained after that, but that's waaaaay off into the future - who knows what computers will be like then - may have to stick them up your nose or they'll need plums and apricots to work.
You might want to see this page. http://www3.sqlite.org/lts.html
(26) By Richard Damon (RichardDamon) on 2022-09-19 12:12:53 in reply to 24 [link] [source]
life of the project (officially until at least 2050)
Curious! will SQLite not be maintained after 28 years from now?! They just are not promising at this time to maintain it after then. They are not saying that it WILL have support dropped.
Perhaps at that point, they will drop support for some ancient compatibilities. Maybe in the near future (like the next decade or so), we will get an announcement of the extension of support for "modern" SQLite, and a listing of features that will be dropped from support.
Or, perhaps in the "near" future, an SQLite4 will be released that is compatible with "modern" SQLite3, and that will be the new long-term support path.
(27) By Sunny Saini (SunnySaini_com) on 2022-09-19 14:42:23 in reply to 25.1 [link] [source]
http://www3.sqlite.org/lts.html
Interesting, thanks
(28) By Sunny Saini (SunnySaini_com) on 2022-09-19 14:43:30 in reply to 26 [link] [source]
OK, thanks