nalgeon / sqlean

The ultimate set of SQLite extensions
MIT License
3.65k stars 115 forks source link

varargs for hashing? #88

Closed tantaman closed 1 year ago

tantaman commented 1 year ago

I noticed that the hash functions only accept 1 argument

https://github.com/nalgeon/sqlean/blob/ab0e7e2173328fadad492673fbdd4851a6d3f9e6/src/crypto/extension.c#L200-L204

but the underlying implementations do appear to be able to update an existing hash with more data. E.g., from sha1.c

https://github.com/nalgeon/sqlean/blob/ab0e7e2173328fadad492673fbdd4851a6d3f9e6/src/crypto/sha1.c#L202-L221

Could the extension be updated to accept varargs for hash code generation? E.g., for the use case of hashing an entire selection set?

cc @schickling

nalgeon commented 1 year ago

What do you mean by "use case of hashing an entire selection set"? Please provide an example.

tantaman commented 1 year ago
SELECT sha1(*) FROM foo;

where sha1 here would return the hash code of all the cells taken together for each row.

E.g.,

.mode column
CREATE TABLE foo (a, b, c);
INSERT INTO foo VALUES (1,2,3), (4,5,6), (7,8,9);
SELECT sha1(*) as hash FROM foo;
hash
----
row1_hash
row2_hash
row3_hash
nalgeon commented 1 year ago

Thanks!

That's not exactly how SQL works. To apply a function (other than count) to all columns in a row, you must list them explicitly:

select sha1(a, b, c) as hash from foo;

Could you please clarify why it would be helpful to compute a single hash over multiple columns? Do you have a real-world use case?

tantaman commented 1 year ago

Do you have a real-world use case?

@schickling does

He wants to compare whether or not new query results differs from his previous query results. Other ideas would be:

nalgeon commented 1 year ago

I'm not sure I follow. Let's say sha1 can compute the hash over multiple columns (sha1(a, b, c)). What do I do with that? What exactly does it mean "to compare whether or not new query results differs from his previous query results"? What are the "query results" and "previous query results" here? I'd appreciate a complete example.

nalgeon commented 1 year ago

All right, then 🤷‍♀️