1

This query works in one account but throws an error (below) in another. Is there a parameter that affects this behaviour? Both accounts have the same Snowflake version (6.41.2).

Error Message

with cte_one as (


select 'aaa' a
,'bbb' b  
  

)

select t1.a || t1.b as c
from cte_one t1
join cte_one t2  on c = t2.a || t2.b
;
  • Nice catch! Having said that, it's not a good practice to use column aliases from `select` in the `join` even when supported. I suggest adding another CTE/Subquery or repeating the expression with original column names – Radagast Dec 16 '22 at 17:45

3 Answers3

0

This will work.

with cte_one as (

select 'aaa' a
,'bbb' b  

), t3 as
(
    select t1.a || t1.b as c
    from cte_one t1
)
select * from t3
join cte_one t2  on c = t2.a || t2.b
;
C A B
aaabbb aaa bbb

Virtual column C doesn't exist yet in the original syntax when the compiler goes into the join. As humans we can read what the original SQL is trying to do and it makes sense. The compiler sees it differently.

Out of curiosity I went to SQL Fiddle I tried the original SQL on Oracle, Postgres, and MS SQL Server. They all reported errors saying C does not exist.

Greg Pavlik
  • 10,089
  • 2
  • 12
  • 29
  • My original query does work on our production snowflake account but not in our development snowflake account. If it didn't work in either then that would be fine, we would have to develop code differently. By the way, the query is a simple example of the problem we are having with more complex SQL statements when moving code between snowflake accounts. – jh1111111111 Dec 16 '22 at 14:48
  • Are your dev and prod accounts in the same region? Which region(s)? I'll check on this. – Greg Pavlik Dec 16 '22 at 14:55
  • Based on your comment and Lukasz's answer, I tested this in different Snowflake deployments. It worked on some and did not work on others. Since you're seeing different behavior between your prod and dev accounts, this probably warrants a support case. – Greg Pavlik Dec 16 '22 at 15:38
  • I've reached out to Snowflake support. Thanks for the assistance. – jh1111111111 Dec 16 '22 at 15:56
  • Both accounts are in the same region, by the way. – jh1111111111 Dec 16 '22 at 15:58
  • That's even weirder. Probably something with parameter settings or a new rollout. – Greg Pavlik Dec 16 '22 at 15:59
0

The provided code sample works as-is:

enter image description here


Environment:

SELECT CURRENT_REGION(), CURRENT_VERSION();
-- AZURE_WESTEUROPE 6.41.2

Query profile:

explain using tabular 
with cte_one as (
select 'aaa' a ,'bbb' b union all select 'x', 'y' 
)
select t1.a || t1.b as c, 1
from cte_one t1
join cte_one t2  
 on c = t2.a || t2.b;

Output:

enter image description here

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
0

There is a feature for resolving aliases from the select list in the join predicate. The rollout process was not completed yet for all accounts. You should engage Snowflake Support and ask them to fix the configuration discrepancies on your accounts.

I would suggest disabling it on both accounts and using Greg's workaround.

Gokhan Atil
  • 9,278
  • 2
  • 11
  • 24