snowflake-cloud-data-platformsql-function

Memoizable functions - Snowflake


When querying INFORMATION_SCHEMA or SHOW FUNCTION we could find a column IS_MEMOIZABLE.

SELECT IS_MEMOIZABLE, *
FROM INFORMATION_SCHEMA.FUNCTIONS;

None of built-in function is memoizable:

SHOW BUILTIN FUNCTIONS;

SELECT "is_memoizable", *
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
WHERE "is_memoizable" <> 'N';
-- 0 rows

Memoization

In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

The question is how to create user defined function that has IS_MEMOIZABLE property equals to 'Y'(true)?

Is there any specific keyword required and/or does it apply to specific types of functions(external/Python/Java/immutable/...)?


Solution

  • Memoizable UDFs

    A scalar SQL UDF can be memoizable. A memoizable function caches the result of calling a scalar SQL UDF and then returns the cached result when the output is needed at a later time. The benefit of using a memoizable function is to improve performance for complex queries, such as multiple column lookups in mapping tables referenced within a row access policy or masking policy.

    You can define a scalar SQL UDF to be memoizable in the CREATE FUNCTION statement by specifying the MEMOIZABLE keyword. You can create a memoizable to function with or without arguments.

    Example:

    CREATE OR REPLACE FUNCTION test()
    RETURNS INT
    MEMOIZABLE
    AS
    $$SELECT 1$$;