Redshift:错误:目标表必须是等值连词谓词

时间:2018-04-26 13:23:50

标签: sql amazon-redshift

首先,对于那些可能会说这是重复问题的人,我检查了Update statement: Error: Target table must be part of an equijoin predicate ,但这并没有帮助我,因为我的情景完全不同。

来到这个问题,

我正在尝试连接两个表并执行更新。这是我的疑问:

UPDATE table1 SET f1 = table2.f1,f2 = table2.f2,f3 = table2.f3 FROM table2 where (table1.f1 = table2.f1 OR (table1.f1 is null and table2.f1 is null)) AND (table1.f2 = table2.f2 OR (table1.f2 is null and table2.f2 is null))

对于此查询,我收到错误:错误:目标表必须是equijoin谓词的一部分。

但是当我看到目标表已经在equijoin中使用时,为什么我会收到此错误? 此外,我需要匹配源和目标空值,所以我使用is null检查。如果我跳过它并执行如下的普通连接查询,它不会给出任何错误。

UPDATE table1 SET f1 = table2.f1,f2 = table2.f2,f3 = table2.f3 FROM table2 where table1.f1 = table2.f1 AND table1.f2 = table2.f2

首先查询是什么问题?毕竟还有额外的括号和条件,根据我不应该有很大的不同。

只是好奇,我在redshift中尝试了几个查询。

1. UPDATE table1 SET f1 = table2.f1,f2 = table2.f2,f3 = table2.f3 FROM table2 where (table1.f1 = table2.f1 OR (table1.f1 is null and table2.f1 is null)) -- Failed with same error.

2. UPDATE table1 SET f1 = table2.f1,f2 = table2.f2,f3 = table2.f3 FROM table2 where (table1.f1 = table2.f1 OR (table1.f1 is null and table2.f1 is null)) AND (table1.f2 = table2.f2) -- Passed

3. UPDATE table1 SET f1 = table2.f1,f2 = table2.f2,f3 = table2.f3 FROM table2 where (table1.f1 = table2.f1 OR (table1.f1 is null and table2.f1 is null)) OR (table1.f2 = table2.f2) -- Failed with same error

看起来只有在最后一个条件为OR并且在AND时通过时才会失败,非常非常令人惊讶。

2 个答案:

答案 0 :(得分:0)

这有用吗?

UPDATE table1
     SET f1 = table2.f1,f2 = table2.f2,f3 = table2.f
     FROM table2 
     WHERE table1.f1 = table2.f1 AND
           (table1.f2 = table2.f2 OR (table1.f2 is null and table2.f2 is null));

如果是这样,您可以将其分解为多个更新。

答案 1 :(得分:0)

如果您只评估了=运算符,并且如果有多个条件,它只能是一个交集点(您只能使用AND,而不是OR),那么等值连接谓词就是严格的。我不确切地知道它为什么以这种方式实现,但我认为它背后有很好的理由与并行化有关。更复杂的逻辑需要通过子查询实现它。

第一个查询不符合这些要求。并且解决方案是相同的 - 任何时候你想做一些比equijoin更复杂的事情,你将更复杂的连接包装到子查询中,然后将它等同于更新的表。