Snowflake - Lateral cannot be on the left side of join
Asked Answered
P

2

7

I have a variant data type that I am performing a lateral flatten on but I then need to left join one of the json elements to lookup the value for the corresponding ID from another relational table within Snowflake. When I do this it gives me the error "Lateral View cannot be on the left side of join" which doesn't make sense because if I don't include the outer join an create the view and then create an additional view on top of this view, it allows me to perform a left join.

Example:

create or replace view  my_view
copy grants
as
select 
    rowid as row_id,
    siteData.value:siteID::int as site_id,
    es.site_name AS site_name
from 
    "RAW_DATA" raw, 
    lateral flatten(datamap:data, outer => true) siteData
    LEFT join ext_site es on es.siteid = siteData.value:siteID
;
P answered 13/8, 2020 at 14:8 Comment(2)
Can you join it the way you need the join by using TABLE instead of LATERAL? It would look like table(flatten(datamap:data, outer => true)) siteData. You can left join on that, but I'm not sure it's what you need.Frankish
Add a outer select query before joining.Iinden
H
8

I cannot explain the limitation on not being able to LEFT JOIN after a LATERAL, but here are two potential workarounds to the issue...

Option 1 - Use a CTE (Common Table Expression)

create or replace view my_view
copy grants
as
with my_cte as (
  select 
      rowid as row_id,
      siteData.value:siteID::int as site_id
  from 
      "RAW_DATA" raw, 
      lateral flatten(datamap:data, outer => true) siteData
)
select 
    c.row_id,
    c.site_id,
    es.site_name
from 
    my_cte c
    LEFT join ext_site es on es.siteid = c.site_id
;

Option 2 - Use an inline (anonymous) view

create or replace view my_view
copy grants
as
select 
    c.row_id,
    c.site_id,
    es.site_name
from 
    (
  select 
      rowid as row_id,
      siteData.value:siteID::int as site_id
  from 
      "RAW_DATA" raw, 
      lateral flatten(datamap:data, outer => true) siteData
    ) c
    LEFT join ext_site es on es.siteid = c.site_id
;

Personally, I would use the CTE approach, as I find it much more readable.

Regarding your use of outer => true, that is required only if the VARIANT structure is somehow inconsistent and does not guarantee the existence of a data key within your datamap dictionary having a value (array or dictionary) that itself contains one or more elements or attributes. If it is not required, then I would remove it.

Helbonnas answered 13/8, 2020 at 17:33 Comment(1)
Even with encapsulating the left table with the lateral flatten in a CTE, the error message is the sameShank
F
5

Try this workaround:

create or replace view  my_view
copy grants
as
select 
    rowid as row_id,
    siteData.value:siteID::int as site_id,
    es.site_name AS site_name
from 
    "RAW_DATA" raw
    LEFT join table(flatten(input => raw.datamap:data, outer => TRUE)) siteData
    LEFT join ext_site es on es.siteid = siteData.value:siteID::int
;
Fulgurite answered 19/9, 2022 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.