Kudu

wztqucjr  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(407)

Kudu是否支持 UPDATE 部分 UPSERT INTO ?
我是否可以提供一个条件子句,仅基于insert值和目标表之间的比较来更新给定的值?实际的用例是用最新的更新timestamp列。
这是我想象中的行为。

CREATE TABLE my_first_table
(
  id INT,
  name STRING,
  status INT,
  PRIMARY KEY(id)
)
PARTITION BY HASH PARTITIONS 4
STORED AS KUDU;

INSERT INTO my_first_table VALUES (1, "lee", 101), (2 "shiv", 102), (3,"bob", 103);

--CONDITION FALSE, UPDATE NOT PERFORMED
UPSERT INTO my_first_table AS t 
VALUES (3, "bobby", 100) AS v 
WHERE v.status > t.status

+----+------+--------+
| id | name | status |
+----+------+--------+
| 1  | lee  | 101    |
| 2  | shiv | 102    |
| 3  | bob  | 103    |
+----+------+--------+

--CONDITION TRUE, UPDATE PERFORMED
UPSERT INTO my_first_table AS t 
VALUES (3, "bobby", 100) AS v 
WHERE v.status < t.status

+----+------+--------+
| id | name | status |
+----+------+--------+
| 1  | lee  | 101    |
| 2  | shiv | 102    |
| 3  | bobby| 100    |
+----+------+--------+

如果3不存在,则应插入。
如果没有,是否有一个优雅的解决方法?

xoshrz7s

xoshrz7s1#

我找到的一个解决办法是使用 LEFT JOIN 并在 SELECT 表情。假设我们有一张table to_upsert 与目标表相同,所有潜在的上升点。。。

INSERT INTO to_upsert VALUES (3, "bobby" 100), (5, "newgal", 600);

UPSERT INTO my_first_table
SELECT to_upsert.id, to_upsert.name, to_upsert.status
FROM to_upsert
LEFT JOIN my_first_table ON to_upsert.id = my_first_table.id
WHERE my_first_table.status > to_upsert.status OR my_first_table.id IS NULL;

SELECT * FROM my_first_table;
+----+--------+--------+
| id | name   | status |
+----+--------+--------+
| 3  | bobby  | 100    |
| 1  | lee    | 101    |
| 2  | shiv   | 102    |
| 5  | newgal | 600    |
+----+--------+--------+

感谢收看本期《看我学sql》。

相关问题