FOUND could have a different behavior in Snowflake in some scenarios.
This is a deprecated version of the SnowConvert documentation, please visit the official site HERE.
Severity
Low
Description
The FOUND property in PostgreSQL is a property based on the last executed query, it can be affected by some statements such as INSERT, UPDATE, DELETE, MERGE, SELECT INTO, PERFORM, FETCH and FOR loops. To read more details about this property, this is PostgreSQL documentation.
In Snowflake there is not a direct translation for this property, for the following scenarios:
INSERT
UPDATE
DELETE
MERGE
The converted code will be SQLFOUND Snowflake property (Here is the documentation) since it behaves like the PostgreSQL FOUND property.
For the other cases such as:
SELECT INTO
PERFORM
FETCH
The converted code will be a custom UDF (IS_FOUND_UDF) that behaves like the PostgreSQL FOUND property.
This happens because SQLFOUND changes its value only when at least one row is affected by the last executed query, if the last query does not change any row, it does not change.
While the IS_FOUND_UDF only works for statements that returns rows, if no row is returned it, it will return FALSE.
SQLFOUND Example
INSERT INTO SampleTable (SampleColumn1)VALUES ('SampleValue0.1');
The last query affects a table, so the SQLFOUND is the closest to the PostgreSQL functionality.
IS_FOUND_UDF Example
SELECT SampleColumn FROM SampleTable;
The last query will return a row but does not change anything, so the IS_FOUND_UDF() is the closest to the PostgreSQL functionality.
IS_FOUND_UDF Source Code
CREATE OR REPLACEFUNCTIONIS_FOUND_UDF() RETURNSBOOLEANLANGUAGESQLIMMUTABLEAS$$SELECT (COUNT(*) !=0) FROMTABLE(RESULT_SCAN(LAST_QUERY_ID()));$$;
Code Example
Insert Statement:
-- Found property used with INSERT statement.CREATE OR REPLACEFUNCTIONFoundUsingInsertFunction()RETURNSBOOLEANAS$$BEGININSERT INTO SampleTable (SampleColumn1)VALUES ('SampleValue0.1');RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with INSERT statement.CREATEORREPLACEPROCEDURE FoundUsingInsertFunction ()RETURNSBOOLEANLANGUAGESQLAS$$BEGININSERT INTO SampleTable (SampleColumn1)VALUES ('SampleValue0.1'); RETURN SQLFOUND /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;
Update Statement:
-- Found property used with UPDATE statement.CREATE OR REPLACEFUNCTIONFoundUsingUpdateFunction()RETURNSBOOLEANAS$$BEGINUPDATE SampleTable SET SampleColumn1 ='SampleValue0.1'WHERE SampleColumn1 ='SampleValue0.1';RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with UPDATE statement.CREATEORREPLACEPROCEDURE FoundUsingUpdateFunction ()RETURNSBOOLEANLANGUAGESQLAS$$BEGINUPDATE SampleTableSET SampleColumn1 ='SampleValue0.1'WHERE SampleColumn1 ='SampleValue0.1'; RETURN SQLFOUND /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;
Delete Statement:
-- Found property used with DELETE statement.CREATE OR REPLACEFUNCTIONFoundUsingDeleteFunction()RETURNSBOOLEANAS$$BEGINDELETEFROM SampleTable WHERE SampleColumn1 ='SampleValue0.1';RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with DELETE statement.CREATEORREPLACEPROCEDURE FoundUsingDeleteFunction ()RETURNSBOOLEANLANGUAGESQLAS$$BEGINDELETEFROM SampleTableWHERE SampleColumn1 ='SampleValue0.1'; RETURN SQLFOUND /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;
Merge Statement:
-- Found property used with MERGE statement.CREATE OR REPLACEFUNCTIONFoundUsingMergeFunction()RETURNSBOOLEANAS$$BEGINMERGEINTO SampleTableB BUSING (SELECT*FROM SampleTableA) AON B.SampleColumn1 = A.SampleColumn2WHENMATCHEDTHENDELETE;RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with MERGE statement.CREATEORREPLACEPROCEDURE FoundUsingMergeFunction ()RETURNSBOOLEANLANGUAGESQLAS$$BEGINMERGEINTO SampleTableB BUSING (SELECT*FROM SampleTableA) AON B.SampleColum1 = A.SampleColum2WHENMATCHEDTHENDELETE; RETURN SQLFOUND /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;
Select Into Statement
-- Found property used with SELECT INTO statement.CREATE OR REPLACEFUNCTIONFoundUsingSelectIntoFunction()RETURNSBOOLEANAS$$DECLARE SampleNumber INTEGER;BEGINSELECT1INTO SampleNumber;RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with SELECT INTO statement.CREATEORREPLACEPROCEDURE FoundUsingSelectIntoFunction ()RETURNSBOOLEANLANGUAGESQLAS$$DECLARE SampleNumber INTEGER;BEGINSELECT1INTO :SampleNumber; RETURN IS_FOUND_UDF() /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;
Perform Statement:
-- Found property used with PERFORM statement.CREATE OR REPLACEFUNCTIONFoundUsingPerformFunction()RETURNSBOOLEANAS$$BEGIN PERFORM 1;RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with SELECT statement.CREATEORREPLACEPROCEDURE FoundUsingSelectFunction ()RETURNSBOOLEANLANGUAGESQLAS$$BEGINSELECT1; RETURN IS_FOUND_UDF() /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;
Fetch Statement:
-- Found property used with FETCH statement.CREATE OR REPLACEFUNCTIONFoundUsingFetchFunction ()RETURNSBOOLEANAS$$DECLARE SampleRow VARCHAR; SampleCursor CURSORFORSELECT EmptyColumn FROM EmptyTable;BEGINOPEN SampleCursor;FETCH SampleCursor;CLOSE SampleCursor;RETURN FOUND;END;$$LANGUAGE plpgsql;
-- Found property used with FETCH statement.CREATEORREPLACEPROCEDURE FoundUsingFetchFunction ()RETURNSBOOLEANLANGUAGESQLAS$$DECLARE SampleRow VARCHAR; SampleCursor CURSORFORSELECT EmptyColumn FROM EmptyTable;BEGINOPEN SampleCursor;FETCH SampleCursor;CLOSE SampleCursor; RETURN IS_FOUND_UDF() /*** MSC-WARNING - MSC-PG0027 - FOUND COULD HAVE A DIFFERENT BEHAVIOR IN SNOWFLAKE IN SOME SCENARIOS ***/;
END;$$;