Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Spectrum nested query error" Redshift error

When I run this query in Redshift:

select sd.device_id
from devices.s_devices sd
left join devices.c_devices cd
on sd.device_id = cd.device_id

I get an error like this:

ERROR:  Spectrum nested query error
DETAIL:  
  -----------------------------------------------
  error:  Spectrum nested query error
  code:      8001
  context:   A subquery that refers to a nested table cannot refer to any other table.
  query:     0
  location:  nested_query_rewriter.cpp:726
  process:   padbmaster [pid=6361]
  -----------------------------------------------

I'm not too sure what this error means. I'm only joining to one table I'm not sure which "other table" it's referring to, and I can't find much info about this error on the web.

I've noticed if I change it from left join to join, the error goes away, but I do need to do a left join.

Any ideas what I'm doing wrong?

like image 810
del Avatar asked Oct 14 '25 15:10

del


2 Answers

Redshift reference mentions:

If a FROM clause in a subquery refers to a nested table, it can't refer to any other table.

In your example, you're trying to join two nested columns in one statement.

I would try to first unnest them separately and only then join:

with 
    s_dev as (select sd.device_id from devices.s_devices sd),
    c_dev as (select cd.device_id from devices.c_devices cd)
select 
    c_dev.device_id
from c_dev 
    left join s_dev 
        on s_dev.device_id = c_dev.device_id

 
like image 121
botchniaque Avatar answered Oct 17 '25 15:10

botchniaque


The solution that worked for me, was to create a temporary table with the nested table's data and then join the temp table with the rest of the tables I needed to.

For example, if the nested table is spectrum.customers, the solution will be:

 DROP TABLE IF EXISTS temp_spectrum_customers;

 CREATE TEMPORARY TABLE
     temp_spectrum_customers AS
 SELECT c.id, o.shipdate, c.customer_id
 FROM spectrum.customers c,
      c.orders o;

 SELECT tc.id, tc.shipdate, tc.customer_id, d.delivery_carrier
 FROM temp_spectrum_customers tc
          LEFT OUTER JOIN orders_delivery d on tc.id = d.order_id;
like image 24
user34977 Avatar answered Oct 17 '25 17:10

user34977



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!