Skip to content

Commit

Permalink
[typo](docs) update/delete supports CTE (apache#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
yagagagaga authored Apr 2, 2024
1 parent 83cfd18 commit f8a4c95
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,31 @@ WHERE
column_name op { value | value_list } [ AND column_name op { value | value_list } ...];
```

<version since="dev">

Syntax 2:This syntax can only used on UNIQUE KEY model

```sql
[cte]
DELETE FROM table_name
[PARTITION partition_name | PARTITIONS (partition_name [, partition_name])]
[USING additional_tables]
WHERE condition
```

</version>

#### Required Parameters

+ table_name: Specifies the table from which rows are removed.
+ column_name: column belong to table_name
+ op: Logical comparison operator, The optional types of op include: =, >, <, >=, <=, !=, in, not in
+ value | value_list: value or value list used for logial comparison

<version since="dev">

+ WHERE condition: Specifies a condition to use to select rows for removal

</version>


#### Optional Parameters

+ cte: Common Table Expression, e.g. 'WITH a AS SELECT * FROM tbl'.
+ PARTITION partition_name | PARTITIONS (partition_name [, partition_name]): Specifies the partition or partitions to select rows for removal

<version since="dev">

+ table_alias: alias of table
+ USING additional_tables: If you need to refer to additional tables in the WHERE clause to help identify the rows to be removed, then specify those table names in the USING clause. You can also use the USING clause to specify subqueries that identify the rows to be removed.

</version>

#### Note

1. Only conditions on the key column can be specified when using AGGREGATE (UNIQUE) model.
Expand Down Expand Up @@ -115,8 +102,6 @@ DELETE FROM table_name
WHERE k1 >= 3 AND k2 = "abc";
````

<version since="dev">

4. use the result of `t2` join `t3` to romve rows from `t1`,delete table only support unique key model

```sql
Expand Down Expand Up @@ -172,7 +157,46 @@ DELETE FROM table_name
+----+----+----+--------+------------+
```
</version>
5. using cte
```sql
create table orders(
o_orderkey bigint,
o_totalprice decimal(15, 2)
) unique key(o_orderkey)
distributed by hash(o_orderkey) buckets 1
properties (
"replication_num" = "1"
);
insert into orders values
(1, 34.1),
(2, 432.8);
create table lineitem(
l_linenumber int,
o_orderkey bigint,
l_discount decimal(15, 2)
) unique key(l_linenumber)
distributed by hash(l_linenumber) buckets 1
properties (
"replication_num" = "1"
);
insert into lineitem values
(1, 1, 1.23),
(2, 1, 3.21),
(3, 2, 18.08),
(4, 2, 23.48);
with discount_orders as (
select * from orders
where o_totalprice > 100
)
delete from lineitem
using discount_orders
where lineitem.o_orderkey = discount_orders.o_orderkey;
```

### Keywords

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,13 @@ The UPDATE operation currently only supports updating the Value column. The upda
#### Syntax

```sql
UPDATE target_table [table_alias]
SET assignment_list
WHERE condition

assignment_list:
assignment [, assignment] ...

assignment:
col_name = value

value:
{expr | DEFAULT}
```

<version since="dev">

```sql
[cte]
UPDATE target_table [table_alias]
SET assignment_list
[ FROM additional_tables]
WHERE condition
```

</version>

#### Required Parameters

+ target_table: The target table of the data to be updated. Can be of the form 'db_name.table_name'
Expand All @@ -71,13 +53,10 @@ UPDATE target_table [table_alias]

#### Optional Parameters

<version since="dev">

+ cte: Common Table Expression, e.g. 'WITH a AS SELECT * FROM tbl'
+ table_alias: alias of table
+ FROM additional_tables: Specifies one or more tables to use for selecting rows to update or for setting new values. Note that if you want use target table here, you should give it a alias explicitly.

</version>

#### Note

The current UPDATE statement only supports row updates on the UNIQUE KEY model.
Expand All @@ -98,8 +77,6 @@ UPDATE test SET v1 = 1 WHERE k1=1 and k2=2;
UPDATE test SET v1 = v1+1 WHERE k1=1;
```

<version since="dev">

3. use the result of `t2` join `t3` to update `t1`

```sql
Expand Down Expand Up @@ -157,7 +134,46 @@ the expect result is only update the row where id = 1 in table t1
+----+----+----+--------+------------+
```

</version>
4. using cte

```sql
create table orders(
o_orderkey bigint,
o_totalprice decimal(15, 2)
) unique key(o_orderkey)
distributed by hash(o_orderkey) buckets 1
properties (
"replication_num" = "1"
);

insert into orders values
(1, 34.1),
(2, 432.8);

create table lineitem(
l_linenumber int,
o_orderkey bigint,
l_discount decimal(15, 2)
) unique key(l_linenumber)
distributed by hash(l_linenumber) buckets 1
properties (
"replication_num" = "1"
);

insert into lineitem values
(1, 1, 1.23),
(2, 1, 3.21),
(3, 2, 18.08),
(4, 2, 23.48);

with discount_orders as (
select * from orders
where o_totalprice > 100
)
update lineitem set l_discount = l_discount*0.9
from discount_orders
where lineitem.o_orderkey = discount_orders.o_orderkey;
```

### Keywords

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,31 @@ WHERE
column_name op { value | value_list } [ AND column_name op { value | value_list } ...];
```

<version since="dev">

语法二:该语法只能在UNIQUE KEY模型表上使用

```sql
[cte]
DELETE FROM table_name [table_alias]
[PARTITION partition_name | PARTITIONS (partition_name [, partition_name])]
[USING additional_tables]
WHERE condition
```

</version>

#### Required Parameters

+ table_name: 指定需要删除数据的表
+ column_name: 属于table_name的列
+ op: 逻辑比较操作符,可选类型包括:=, >, <, >=, <=, !=, in, not in
+ value | value_list: 做逻辑比较的值或值列表

<version since="dev">

+ WHERE condition: 指定一个用于选择删除行的条件

</version>


#### Optional Parameters

+ cte: 通用表达式。可以是 'WITH a AS SELECT * FROM tbl' 形式
+ PARTITION partition_name | PARTITIONS (partition_name [, partition_name]): 指定执行删除数据的分区名,如果表不存在此分区,则报错

<version since="dev">

+ table_alias: 表的别名
+ USING additional_tables: 如果需要在WHERE语句中使用其他的表来帮助识别需要删除的行,则可以在USING中指定这些表或者查询。

</version>

#### Note

1. 使用聚合类的表模型(AGGREGATE、UNIQUE)只能指定 key 列上的条件。
Expand Down Expand Up @@ -115,8 +102,6 @@ DELETE FROM table_name [table_alias]
WHERE k1 >= 3 AND k2 = "abc";
```

<version since="dev">

4. 使用`t2``t3`表连接的结果,删除`t1`中的数据,删除的表只支持unique模型

```sql
Expand Down Expand Up @@ -172,7 +157,46 @@ DELETE FROM table_name [table_alias]
+----+----+----+--------+------------+
```
</version>
5. 使用 cte 关联删除
```sql
create table orders(
o_orderkey bigint,
o_totalprice decimal(15, 2)
) unique key(o_orderkey)
distributed by hash(o_orderkey) buckets 1
properties (
"replication_num" = "1"
);
insert into orders values
(1, 34.1),
(2, 432.8);
create table lineitem(
l_linenumber int,
o_orderkey bigint,
l_discount decimal(15, 2)
) unique key(l_linenumber)
distributed by hash(l_linenumber) buckets 1
properties (
"replication_num" = "1"
);
insert into lineitem values
(1, 1, 1.23),
(2, 1, 3.21),
(3, 2, 18.08),
(4, 2, 23.48);
with discount_orders as (
select * from orders
where o_totalprice > 100
)
delete from lineitem
using discount_orders
where lineitem.o_orderkey = discount_orders.o_orderkey;
```

### Keywords

Expand Down
Loading

0 comments on commit f8a4c95

Please sign in to comment.