sqlite-digest icon indicating copy to clipboard operation
sqlite-digest copied to clipboard

Can't use in expressions for generated stored columns

Open natskvi opened this issue 3 years ago • 0 comments

Thanks for making this library available, I found it useful.

Minor problem: Because you specify the SQLITE_ANY type in all five sqlite3_create_function() statements in digest.c, the digest functions cannot be used in expressions for generated stored columns. For example, the following table definition will fail:

$ sqlite3
SQLite version 3.37.2 2022-01-06 13:25:41
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TABLE messages (
  id         INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
  size       INTEGER NOT NULL GENERATED ALWAYS AS (iif(data IS NULL, 0, length(data))) STORED,
  sha256     VARCHAR NOT NULL GENERATED ALWAYS AS (iif(data IS NULL, NULL, hex(digest(data, 'sha256')))) STORED, 
  data       BLOB
);
Error: in prepare, non-deterministic functions prohibited in generated columns (1)

This is in agreement with the SQLite documentation for generated columns:

The expression of a generated column may only reference constant literals and columns within the same row, and may only use scalar deterministic functions. The expression may not use subqueries, aggregate functions, window functions, or table-valued functions.

This issue should be resolved by changing SQLITE_ANY to SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC for all five sqlite3_create_function() statements in digest.c.

natskvi avatar Mar 04 '22 17:03 natskvi