Skip to content
Advertisement

Snowflake unsupported subquery when using function

This is the function I created:

I’m using the ROW_NUMBER function because the RATE_DATE field is actually datetime and so there are multiple records per date.

When I call the function by itself, it works fine. However, when I try to use it in a view, I get the unsupported subquery type error. The view works fine without it. Can anyone think of what I can do to either fix the error or work around it by rewriting the query?

EDIT 1: View code and exact error message

Error message: SQL Error [2031] [42601]: SQL compilation error: Unsupported subquery type cannot be evaluated

Advertisement

Answer

The error is a correlated subquery and the are not supported (beyond some tiny toy examples)

But the basic form is

in effect for each row, a sub-query is run on another table to get a value. There are many tricks done in other DB’s to make this “work” but in effect there is work done on each row. Snowflake does not types of for each row operations.

The good news is there are other patterns that are effectively the same, that snowflake does support, the two patterns are really the same use a CTE or join to a sub-select which is the same thing.

so the above becomes:

So we first process/shape “all records” from the other/sub table, and we only keep the rows that have the count/shape we want, and then join to that result. The is very parallelizable, so performs well. The reason Snowflake does not auto translate a sub-query for you, is it rather easy to get it wrong, and they presently are spending there development efforts working on features that do not exist at all, etc etc, and it can be rewritten by you, given you understand your model.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement