Scalar functions name resolution special behavior
This topic is about the behavior of name resolution in Snowflake inside CREATE VIEW.
When you execute queries, Snowflake looks for the objects specified in the query in the schemas specified in SEARCH_PATH. You can view them like this:
But it seems like a good idea to make the creation of VIEWs independent of SEARCH_PATH. Otherwise, we will get different results when we work with VIEWs at different SEARCH_PATH.
The documentation says the following:
The SEARCH_PATH is not used inside views or UDFs. All unqualifed objects in a view or UDF definition will be resolved in the view’s or UDF’s schema only.
That's great! And it works!
will return
And not only for tables. For any objects, except … scalar functions!
will return
Strange behavior, don't you agree?
In PostgreSQL, for example, it works like this: when creating a VIEW, all objects without schema specification are searched in the public schema. If you want a different schema, specify it by hand.
But maybe I'm being picky? Let's add one more thing…
will return
Oops… i.e. if there is a scalar function in the scheme where VIEW is created, it will be used. If not, the function from PUBLIC will be used.
I.e. if you didn't specify a schema for a scalar function from the PUBLIC schema while creating a VIEW, in a schema other than PUBLIC, then to corrupt the data in your database it is enough to create a function with the same name in the corresponding schema…
At Dwh.dev, we don't display downstream lineage for scalar functions right now, but if you click on that function in the source code (which we display in a very cool way), you will jump to the exact function used in that context.
Last updated