From be290f161e139934dc2b06f1c7998d594cd4d7e3 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 4 Jun 2024 20:14:38 +0800 Subject: [PATCH 1/6] [docs](ddl) add zh-CN docs for generated column --- .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 203 ++++++++++++++++++ .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 203 ++++++++++++++++++ versioned_sidebars/version-2.1-sidebars.json | 1 + 3 files changed, 407 insertions(+) create mode 100644 i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md create mode 100644 versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md new file mode 100644 index 0000000000000..561a0aedac40a --- /dev/null +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -0,0 +1,203 @@ +--- +{ + "title": "CREATE-TABLE-AND-GENERATED-COLUMN", + "language": "zh-CN" +} +--- + + +# 建表和生成列 +CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 +下面是一个使用生成列的例子: +```sql +CREATE TABLE products ( +product_id INT, +price DECIMAL(10,2), +quantity INT, +total_value DECIMAL(10,2) GENERATED ALWAYS AS (price * quantity) +) DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); + +insert into products values(1, 10.00, 10, default); +insert into products(product_id, price, quantity) values(1, 20.00, 10); +``` +从表中查询数据: +```sql +mysql> select * from products; ++------------+-------+----------+-------------+ +| product_id | price | quantity | total_value | ++------------+-------+----------+-------------+ +| 1 | 10.00 | 10 | 100.00 | +| 1 | 20.00 | 10 | 200.00 | ++------------+-------+----------+-------------+ +``` +在这个示例中, total_value 列是一个生成列,其值由 price 和 quantity 列的值相乘计算而来。 +在导入或更新时计算并存储在表中。 +## 语法 +```sql +col_name data_type [GENERATED ALWAYS] AS (expr) +[NOT NULL | NULL] [COMMENT 'string'] +``` +## 生成列的限制 +1. 使用的函数只能是内置的标量函数和运算符,不允许使用udf,聚合函数等其它。 +2. 不允许使用变量,子查询,Lambda表达式。 +3. AUTO_INCREMENT列不能用作生成的列定义中的基列。 +4. 生成的列定义可以引用其他生成的列,但只能引用表定义中较早出现的列。 生成的列定义可以引用表中的任何基本(非生成)列,无论其定义发生得早还是晚。 + +## 导入数据 +导入数据时,如果违反了生成列的NOT NULL限制,例如导入数据时,没有指定生成列引用的列,并且此列没有默认值,将导致导入失败。 +### INSERT +指定列时,指定的列不能包含生成列,否则将报错。 +```sql +insert into products(product_id, price, quantity) values(1, 20.00, 10); +``` +没有指定列时,生成列需要使用default关键字进行占位。 +```sql +insert into products values(1, 10.00, 10, default); +``` + +### LOAD +使用load方式进行数据导入时,需要显式指定导入列。不应当指定生成列为导入列,当指定导入生成列并在数据文件中有对应的数据时,生成列不会使用数据文件中的值,生成列的值仍然是根据表达式计算得到的结果。 +#### STREAM LOAD +创建表: +```sql +mysql> create table gen_col_stream_load(a int,b int,c double generated always as (abs(a+b)) not null) +DISTRIBUTED BY HASH(a) +PROPERTIES("replication_num" = "1"); +``` +准备数据,并进行stream load: +```shell +cat gen_col_data.csv +1,2 +3,5 +2,9 + +curl --location-trusted -u root: \ +-H "Expect:100-continue" \ +-H "column_separator:," \ +-H "columns:a,b" \ +-T gen_col_data.csv \ +-XPUT http://127.0.0.1:8030/api/testdb/gen_col_stream_load/_stream_load +{ + "TxnId": 223227, + "Label": "d4a615c9-6e73-4d95-a8a4-e4c30d3b2262", + "Comment": "", + "TwoPhaseCommit": "false", + "Status": "Success", + "Message": "OK", + "NumberTotalRows": 3, + "NumberLoadedRows": 3, + "NumberFilteredRows": 0, + "NumberUnselectedRows": 0, + "LoadBytes": 12, + "LoadTimeMs": 152, + "BeginTxnTimeMs": 5, + "StreamLoadPutTimeMs": 39, + "ReadDataTimeMs": 0, + "WriteDataTimeMs": 66, + "CommitAndPublishTimeMs": 37 +} +``` +查看数据导入结果: +```sql +mysql> select * from gen_col_stream_load; ++------+------+------+ +| a | b | c | ++------+------+------+ +| 1 | 2 | 3 | +| 2 | 9 | 11 | +| 3 | 5 | 8 | ++------+------+------+ +3 rows in set (0.07 sec) +``` +#### HTTP STREAM LOAD +创建表: +```sql +mysql> create table gencol_refer_gencol_http_load(a int,c double generated always as (abs(a+b)) not null,b int, d int generated always as(c+1)) +DISTRIBUTED BY HASH(a) +PROPERTIES("replication_num" = "1"); +``` +准备数据,并进行http stream load。 +```shell +curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ +-H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ +http://127.0.0.1:8030/api/_http_stream +{ + "TxnId": 223244, + "Label": "label_824464cba2a1eabc_bee78e427ea55e81", + "Comment": "", + "TwoPhaseCommit": "false", + "Status": "Success", + "Message": "OK", + "NumberTotalRows": 3, + "NumberLoadedRows": 3, + "NumberFilteredRows": 0, + "NumberUnselectedRows": 0, + "LoadBytes": 12, + "LoadTimeMs": 142, + "BeginTxnTimeMs": 0, + "StreamLoadPutTimeMs": 45, + "ReadDataTimeMs": 46, + "WriteDataTimeMs": 59, + "CommitAndPublishTimeMs": 36 +} +``` +查看数据导入结果: +```sql +mysql> select * from gencol_refer_gencol_http_load; +------+------+------+------+ +| a | c | b | d | ++------+------+------+------+ +| 2 | 11 | 9 | 12 | +| 1 | 3 | 2 | 4 | +| 3 | 8 | 5 | 9 | ++------+------+------+------+ +3 rows in set (0.04 sec) +``` +#### MYSQL LOAD +```sql +mysql> create table gen_col_mysql_load(a int,b int,c double generated always as (abs(a+b)) not null) +DISTRIBUTED BY HASH(a) +PROPERTIES("replication_num" = "1"); + +mysql> LOAD DATA LOCAL +INFILE '/path_to_data/gen_col_data.csv' +INTO TABLE gen_col_mysql_load +COLUMNS TERMINATED BY ',' +(a,b); +Query OK, 3 rows affected (0.14 sec) +Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 + +mysql> select * from gen_col_mysql_load; ++------+------+------+ +| a | b | c | ++------+------+------+ +| 2 | 9 | 11 | +| 3 | 5 | 8 | +| 1 | 2 | 3 | ++------+------+------+ +3 rows in set (0.06 sec) +``` +#### 其它LOAD +BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 + +## 删除生成列 +```sql +alter table products drop column total_value; +``` +注意事项: +如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 + diff --git a/versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md new file mode 100644 index 0000000000000..561a0aedac40a --- /dev/null +++ b/versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -0,0 +1,203 @@ +--- +{ + "title": "CREATE-TABLE-AND-GENERATED-COLUMN", + "language": "zh-CN" +} +--- + + +# 建表和生成列 +CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 +下面是一个使用生成列的例子: +```sql +CREATE TABLE products ( +product_id INT, +price DECIMAL(10,2), +quantity INT, +total_value DECIMAL(10,2) GENERATED ALWAYS AS (price * quantity) +) DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); + +insert into products values(1, 10.00, 10, default); +insert into products(product_id, price, quantity) values(1, 20.00, 10); +``` +从表中查询数据: +```sql +mysql> select * from products; ++------------+-------+----------+-------------+ +| product_id | price | quantity | total_value | ++------------+-------+----------+-------------+ +| 1 | 10.00 | 10 | 100.00 | +| 1 | 20.00 | 10 | 200.00 | ++------------+-------+----------+-------------+ +``` +在这个示例中, total_value 列是一个生成列,其值由 price 和 quantity 列的值相乘计算而来。 +在导入或更新时计算并存储在表中。 +## 语法 +```sql +col_name data_type [GENERATED ALWAYS] AS (expr) +[NOT NULL | NULL] [COMMENT 'string'] +``` +## 生成列的限制 +1. 使用的函数只能是内置的标量函数和运算符,不允许使用udf,聚合函数等其它。 +2. 不允许使用变量,子查询,Lambda表达式。 +3. AUTO_INCREMENT列不能用作生成的列定义中的基列。 +4. 生成的列定义可以引用其他生成的列,但只能引用表定义中较早出现的列。 生成的列定义可以引用表中的任何基本(非生成)列,无论其定义发生得早还是晚。 + +## 导入数据 +导入数据时,如果违反了生成列的NOT NULL限制,例如导入数据时,没有指定生成列引用的列,并且此列没有默认值,将导致导入失败。 +### INSERT +指定列时,指定的列不能包含生成列,否则将报错。 +```sql +insert into products(product_id, price, quantity) values(1, 20.00, 10); +``` +没有指定列时,生成列需要使用default关键字进行占位。 +```sql +insert into products values(1, 10.00, 10, default); +``` + +### LOAD +使用load方式进行数据导入时,需要显式指定导入列。不应当指定生成列为导入列,当指定导入生成列并在数据文件中有对应的数据时,生成列不会使用数据文件中的值,生成列的值仍然是根据表达式计算得到的结果。 +#### STREAM LOAD +创建表: +```sql +mysql> create table gen_col_stream_load(a int,b int,c double generated always as (abs(a+b)) not null) +DISTRIBUTED BY HASH(a) +PROPERTIES("replication_num" = "1"); +``` +准备数据,并进行stream load: +```shell +cat gen_col_data.csv +1,2 +3,5 +2,9 + +curl --location-trusted -u root: \ +-H "Expect:100-continue" \ +-H "column_separator:," \ +-H "columns:a,b" \ +-T gen_col_data.csv \ +-XPUT http://127.0.0.1:8030/api/testdb/gen_col_stream_load/_stream_load +{ + "TxnId": 223227, + "Label": "d4a615c9-6e73-4d95-a8a4-e4c30d3b2262", + "Comment": "", + "TwoPhaseCommit": "false", + "Status": "Success", + "Message": "OK", + "NumberTotalRows": 3, + "NumberLoadedRows": 3, + "NumberFilteredRows": 0, + "NumberUnselectedRows": 0, + "LoadBytes": 12, + "LoadTimeMs": 152, + "BeginTxnTimeMs": 5, + "StreamLoadPutTimeMs": 39, + "ReadDataTimeMs": 0, + "WriteDataTimeMs": 66, + "CommitAndPublishTimeMs": 37 +} +``` +查看数据导入结果: +```sql +mysql> select * from gen_col_stream_load; ++------+------+------+ +| a | b | c | ++------+------+------+ +| 1 | 2 | 3 | +| 2 | 9 | 11 | +| 3 | 5 | 8 | ++------+------+------+ +3 rows in set (0.07 sec) +``` +#### HTTP STREAM LOAD +创建表: +```sql +mysql> create table gencol_refer_gencol_http_load(a int,c double generated always as (abs(a+b)) not null,b int, d int generated always as(c+1)) +DISTRIBUTED BY HASH(a) +PROPERTIES("replication_num" = "1"); +``` +准备数据,并进行http stream load。 +```shell +curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ +-H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ +http://127.0.0.1:8030/api/_http_stream +{ + "TxnId": 223244, + "Label": "label_824464cba2a1eabc_bee78e427ea55e81", + "Comment": "", + "TwoPhaseCommit": "false", + "Status": "Success", + "Message": "OK", + "NumberTotalRows": 3, + "NumberLoadedRows": 3, + "NumberFilteredRows": 0, + "NumberUnselectedRows": 0, + "LoadBytes": 12, + "LoadTimeMs": 142, + "BeginTxnTimeMs": 0, + "StreamLoadPutTimeMs": 45, + "ReadDataTimeMs": 46, + "WriteDataTimeMs": 59, + "CommitAndPublishTimeMs": 36 +} +``` +查看数据导入结果: +```sql +mysql> select * from gencol_refer_gencol_http_load; +------+------+------+------+ +| a | c | b | d | ++------+------+------+------+ +| 2 | 11 | 9 | 12 | +| 1 | 3 | 2 | 4 | +| 3 | 8 | 5 | 9 | ++------+------+------+------+ +3 rows in set (0.04 sec) +``` +#### MYSQL LOAD +```sql +mysql> create table gen_col_mysql_load(a int,b int,c double generated always as (abs(a+b)) not null) +DISTRIBUTED BY HASH(a) +PROPERTIES("replication_num" = "1"); + +mysql> LOAD DATA LOCAL +INFILE '/path_to_data/gen_col_data.csv' +INTO TABLE gen_col_mysql_load +COLUMNS TERMINATED BY ',' +(a,b); +Query OK, 3 rows affected (0.14 sec) +Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 + +mysql> select * from gen_col_mysql_load; ++------+------+------+ +| a | b | c | ++------+------+------+ +| 2 | 9 | 11 | +| 3 | 5 | 8 | +| 1 | 2 | 3 | ++------+------+------+ +3 rows in set (0.06 sec) +``` +#### 其它LOAD +BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 + +## 删除生成列 +```sql +alter table products drop column total_value; +``` +注意事项: +如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 + diff --git a/versioned_sidebars/version-2.1-sidebars.json b/versioned_sidebars/version-2.1-sidebars.json index cfaf2cdb6773e..df4ba586cd623 100644 --- a/versioned_sidebars/version-2.1-sidebars.json +++ b/versioned_sidebars/version-2.1-sidebars.json @@ -1216,6 +1216,7 @@ "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-CATALOG", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-DATABASE", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE", + "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-LIKE", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AS-SELECT", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-INDEX", From 0060700652bff12c1b9dc2a079c4d4847543b275 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Wed, 5 Jun 2024 17:04:52 +0800 Subject: [PATCH 2/6] [docs](ddl) add zh-CN docs for generated column --- .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 25 +++++++++---------- .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 25 +++++++++---------- sidebars.json | 1 + versioned_sidebars/version-2.1-sidebars.json | 1 - 4 files changed, 25 insertions(+), 27 deletions(-) rename {i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1 => docs}/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md (88%) rename {versioned_docs/version-2.1 => i18n/zh-CN/docusaurus-plugin-content-docs/current}/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md (88%) diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md similarity index 88% rename from i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md rename to docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 561a0aedac40a..6d80c7c1ee560 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -21,7 +21,6 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -# 建表和生成列 CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 下面是一个使用生成列的例子: ```sql @@ -32,12 +31,12 @@ quantity INT, total_value DECIMAL(10,2) GENERATED ALWAYS AS (price * quantity) ) DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); -insert into products values(1, 10.00, 10, default); -insert into products(product_id, price, quantity) values(1, 20.00, 10); +INSERT INTO products VALUES(1, 10.00, 10, default); +INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` 从表中查询数据: ```sql -mysql> select * from products; +mysql> SELECT * FROM products; +------------+-------+----------+-------------+ | product_id | price | quantity | total_value | +------------+-------+----------+-------------+ @@ -63,11 +62,11 @@ col_name data_type [GENERATED ALWAYS] AS (expr) ### INSERT 指定列时,指定的列不能包含生成列,否则将报错。 ```sql -insert into products(product_id, price, quantity) values(1, 20.00, 10); +INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` 没有指定列时,生成列需要使用default关键字进行占位。 ```sql -insert into products values(1, 10.00, 10, default); +INSERT INTO products VALUES(1, 10.00, 10, default); ``` ### LOAD @@ -75,7 +74,7 @@ insert into products values(1, 10.00, 10, default); #### STREAM LOAD 创建表: ```sql -mysql> create table gen_col_stream_load(a int,b int,c double generated always as (abs(a+b)) not null) +mysql> CREATE TABLE gen_col_stream_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) not null) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` @@ -114,7 +113,7 @@ curl --location-trusted -u root: \ ``` 查看数据导入结果: ```sql -mysql> select * from gen_col_stream_load; +mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ | a | b | c | +------+------+------+ @@ -127,7 +126,7 @@ mysql> select * from gen_col_stream_load; #### HTTP STREAM LOAD 创建表: ```sql -mysql> create table gencol_refer_gencol_http_load(a int,c double generated always as (abs(a+b)) not null,b int, d int generated always as(c+1)) +mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL,b INT, d INT GENERATED ALWAYS AS(c+1)) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` @@ -158,7 +157,7 @@ http://127.0.0.1:8030/api/_http_stream ``` 查看数据导入结果: ```sql -mysql> select * from gencol_refer_gencol_http_load; +------+------+------+------+ +mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ | a | c | b | d | +------+------+------+------+ | 2 | 11 | 9 | 12 | @@ -169,7 +168,7 @@ mysql> select * from gencol_refer_gencol_http_load; ``` #### MYSQL LOAD ```sql -mysql> create table gen_col_mysql_load(a int,b int,c double generated always as (abs(a+b)) not null) +mysql> CREATE TABLE gen_col_mysql_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); @@ -181,7 +180,7 @@ COLUMNS TERMINATED BY ',' Query OK, 3 rows affected (0.14 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 -mysql> select * from gen_col_mysql_load; +mysql> SELECT * FROM gen_col_mysql_load; +------+------+------+ | a | b | c | +------+------+------+ @@ -196,7 +195,7 @@ BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表, ## 删除生成列 ```sql -alter table products drop column total_value; +ALTER TABLE products DROP COLUMN total_value; ``` 注意事项: 如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 diff --git a/versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md similarity index 88% rename from versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md rename to i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 561a0aedac40a..6d80c7c1ee560 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -21,7 +21,6 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -# 建表和生成列 CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 下面是一个使用生成列的例子: ```sql @@ -32,12 +31,12 @@ quantity INT, total_value DECIMAL(10,2) GENERATED ALWAYS AS (price * quantity) ) DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); -insert into products values(1, 10.00, 10, default); -insert into products(product_id, price, quantity) values(1, 20.00, 10); +INSERT INTO products VALUES(1, 10.00, 10, default); +INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` 从表中查询数据: ```sql -mysql> select * from products; +mysql> SELECT * FROM products; +------------+-------+----------+-------------+ | product_id | price | quantity | total_value | +------------+-------+----------+-------------+ @@ -63,11 +62,11 @@ col_name data_type [GENERATED ALWAYS] AS (expr) ### INSERT 指定列时,指定的列不能包含生成列,否则将报错。 ```sql -insert into products(product_id, price, quantity) values(1, 20.00, 10); +INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` 没有指定列时,生成列需要使用default关键字进行占位。 ```sql -insert into products values(1, 10.00, 10, default); +INSERT INTO products VALUES(1, 10.00, 10, default); ``` ### LOAD @@ -75,7 +74,7 @@ insert into products values(1, 10.00, 10, default); #### STREAM LOAD 创建表: ```sql -mysql> create table gen_col_stream_load(a int,b int,c double generated always as (abs(a+b)) not null) +mysql> CREATE TABLE gen_col_stream_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) not null) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` @@ -114,7 +113,7 @@ curl --location-trusted -u root: \ ``` 查看数据导入结果: ```sql -mysql> select * from gen_col_stream_load; +mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ | a | b | c | +------+------+------+ @@ -127,7 +126,7 @@ mysql> select * from gen_col_stream_load; #### HTTP STREAM LOAD 创建表: ```sql -mysql> create table gencol_refer_gencol_http_load(a int,c double generated always as (abs(a+b)) not null,b int, d int generated always as(c+1)) +mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL,b INT, d INT GENERATED ALWAYS AS(c+1)) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` @@ -158,7 +157,7 @@ http://127.0.0.1:8030/api/_http_stream ``` 查看数据导入结果: ```sql -mysql> select * from gencol_refer_gencol_http_load; +------+------+------+------+ +mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ | a | c | b | d | +------+------+------+------+ | 2 | 11 | 9 | 12 | @@ -169,7 +168,7 @@ mysql> select * from gencol_refer_gencol_http_load; ``` #### MYSQL LOAD ```sql -mysql> create table gen_col_mysql_load(a int,b int,c double generated always as (abs(a+b)) not null) +mysql> CREATE TABLE gen_col_mysql_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); @@ -181,7 +180,7 @@ COLUMNS TERMINATED BY ',' Query OK, 3 rows affected (0.14 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 -mysql> select * from gen_col_mysql_load; +mysql> SELECT * FROM gen_col_mysql_load; +------+------+------+ | a | b | c | +------+------+------+ @@ -196,7 +195,7 @@ BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表, ## 删除生成列 ```sql -alter table products drop column total_value; +ALTER TABLE products DROP COLUMN total_value; ``` 注意事项: 如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 diff --git a/sidebars.json b/sidebars.json index 936f697249237..8c85621f7d89f 100644 --- a/sidebars.json +++ b/sidebars.json @@ -1221,6 +1221,7 @@ "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-CATALOG", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-DATABASE", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE", + "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-LIKE", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AS-SELECT", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-INDEX", diff --git a/versioned_sidebars/version-2.1-sidebars.json b/versioned_sidebars/version-2.1-sidebars.json index df4ba586cd623..cfaf2cdb6773e 100644 --- a/versioned_sidebars/version-2.1-sidebars.json +++ b/versioned_sidebars/version-2.1-sidebars.json @@ -1216,7 +1216,6 @@ "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-CATALOG", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-DATABASE", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE", - "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-LIKE", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AS-SELECT", "sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-INDEX", From bf907ce85e92079da04854911bba3743474528c3 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Thu, 27 Jun 2024 17:17:04 +0800 Subject: [PATCH 3/6] [docs](ddl) add zh-CN docs for generated column --- .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 51 ++++++++++++++++-- .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 52 +++++++++++++++++-- 2 files changed, 96 insertions(+), 7 deletions(-) diff --git a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 6d80c7c1ee560..3788a7954c54b 100644 --- a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -29,7 +29,8 @@ product_id INT, price DECIMAL(10,2), quantity INT, total_value DECIMAL(10,2) GENERATED ALWAYS AS (price * quantity) -) DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); +) UNIQUE KEY(product_id) +DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); INSERT INTO products VALUES(1, 10.00, 10, default); INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); @@ -56,6 +57,7 @@ col_name data_type [GENERATED ALWAYS] AS (expr) 2. 不允许使用变量,子查询,Lambda表达式。 3. AUTO_INCREMENT列不能用作生成的列定义中的基列。 4. 生成的列定义可以引用其他生成的列,但只能引用表定义中较早出现的列。 生成的列定义可以引用表中的任何基本(非生成)列,无论其定义发生得早还是晚。 +5. 聚合模型中,生成列是VALUE列时,仅允许使用REPLACE和REPLACE_IF_NOT_NULL聚合类型。 ## 导入数据 导入数据时,如果违反了生成列的NOT NULL限制,例如导入数据时,没有指定生成列引用的列,并且此列没有默认值,将导致导入失败。 @@ -130,7 +132,7 @@ mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAY DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` -准备数据,并进行http stream load。 +准备数据,并进行http stream load: ```shell curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ -H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ @@ -193,7 +195,50 @@ mysql> SELECT * FROM gen_col_mysql_load; #### 其它LOAD BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 -## 删除生成列 +## 生成列与部分列更新 +在进行部分列更新时,必须在columns中指定生成列引用的所有普通列,否则会报错。 + +下面是一个示例, 建表和插入一行数据,并设置session变量: +```sql +CREATE TABLE test_partial_column_unique_gen_col (a INT, b INT, c INT AS (a+b), d INT AS (c+1), e INT) +UNIQUE KEY(a) DISTRIBUTED BY HASH(a) PROPERTIES( + "enable_unique_key_merge_on_write" = "true", + "replication_num"="1" +); +SET enable_unique_key_partial_update=true; +SET enable_insert_strict=false; +SET enable_fallback_to_original_planner=false; +INSERT INTO test_partial_column_unique_gen_col(a,b,e) VALUES(1,2,7); +``` +如果没有指定所有的被引用的普通列会报错: +```sql +mysql> INSERT INTO test_partial_column_unique_gen_col(a) VALUES(3); +ERROR 1105 (HY000): errCode = 2, detailMessage = Partial update should include all ordinary columns referenced by generated columns, missing: b +``` +LOAD也是这样,-H "columns: a, b"中需要指定所有被引用的普通列,下面是使用stream load的示例: +```shell +curl --location-trusted -u root: -H "Expect:100-continue" -H "column_separator:," \ +-H "columns: a, b" -H "partial_columns:true" \ +-T /Users/moailing/Documents/tmp/gen_col_data.csv \ +http://127.0.0.1:8030/api/testdb/partial_column_unique_gen_col/_stream_load +``` + + +## ALTER TABLE和生成列 +生成列暂时不支持ADD COLUMN, MODIFY COLUMN。 +### REORDER COLUMN +```sql +ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); +``` +注意事项: +修改后的列顺序仍然需要满足生成列建表时的顺序限制。 +### RENAME COLUMN +```sql +ALTER TABLE products RENAME COLUMN total_value new_name; +``` +注意事项: +如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能修改此生成列的名称。 +### DROP COLUMN ```sql ALTER TABLE products DROP COLUMN total_value; ``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 6d80c7c1ee560..3586032460e10 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -29,7 +29,8 @@ product_id INT, price DECIMAL(10,2), quantity INT, total_value DECIMAL(10,2) GENERATED ALWAYS AS (price * quantity) -) DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); +) UNIQUE KEY(product_id) +DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); INSERT INTO products VALUES(1, 10.00, 10, default); INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); @@ -56,6 +57,7 @@ col_name data_type [GENERATED ALWAYS] AS (expr) 2. 不允许使用变量,子查询,Lambda表达式。 3. AUTO_INCREMENT列不能用作生成的列定义中的基列。 4. 生成的列定义可以引用其他生成的列,但只能引用表定义中较早出现的列。 生成的列定义可以引用表中的任何基本(非生成)列,无论其定义发生得早还是晚。 +5. 聚合模型中,生成列是VALUE列时,仅允许使用REPLACE和REPLACE_IF_NOT_NULL聚合类型。 ## 导入数据 导入数据时,如果违反了生成列的NOT NULL限制,例如导入数据时,没有指定生成列引用的列,并且此列没有默认值,将导致导入失败。 @@ -130,7 +132,7 @@ mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAY DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` -准备数据,并进行http stream load。 +准备数据,并进行http stream load: ```shell curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ -H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ @@ -193,10 +195,52 @@ mysql> SELECT * FROM gen_col_mysql_load; #### 其它LOAD BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 -## 删除生成列 +## 生成列与部分列更新 +在进行部分列更新时,必须在columns中指定生成列引用的所有普通列,否则会报错。 + +下面是一个示例, 建表和插入一行数据,并设置session变量: +```sql +CREATE TABLE test_partial_column_unique_gen_col (a INT, b INT, c INT AS (a+b), d INT AS (c+1), e INT) +UNIQUE KEY(a) DISTRIBUTED BY HASH(a) PROPERTIES( + "enable_unique_key_merge_on_write" = "true", + "replication_num"="1" +); +SET enable_unique_key_partial_update=true; +SET enable_insert_strict=false; +SET enable_fallback_to_original_planner=false; +INSERT INTO test_partial_column_unique_gen_col(a,b,e) VALUES(1,2,7); +``` +如果没有指定所有的被引用的普通列会报错: +```sql +mysql> INSERT INTO test_partial_column_unique_gen_col(a) VALUES(3); +ERROR 1105 (HY000): errCode = 2, detailMessage = Partial update should include all ordinary columns referenced by generated columns, missing: b +``` +LOAD也是这样,-H "columns: a, b"中需要指定所有被引用的普通列,下面是使用stream load的示例: +```shell +curl --location-trusted -u root: -H "Expect:100-continue" -H "column_separator:," \ +-H "columns: a, b" -H "partial_columns:true" \ +-T /Users/moailing/Documents/tmp/gen_col_data.csv \ +http://127.0.0.1:8030/api/testdb/partial_column_unique_gen_col/_stream_load +``` + + +## ALTER TABLE和生成列 +生成列暂时不支持ADD COLUMN, MODIFY COLUMN。 +### REORDER COLUMN +```sql +ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); +``` +注意事项: +修改后的列顺序仍然需要满足生成列建表时的顺序限制。 +### RENAME COLUMN +```sql +ALTER TABLE products RENAME COLUMN total_value new_name; +``` +注意事项: +如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能修改此生成列的名称。 +### DROP COLUMN ```sql ALTER TABLE products DROP COLUMN total_value; ``` 注意事项: 如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 - From a2a8dfb581c2fb9d30931ba68837bd6e7237b207 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 9 Jul 2024 14:57:59 +0800 Subject: [PATCH 4/6] [docs](ddl) add en docs for generated column --- .../Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md | 56 +++++++++++ .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 95 +++++++------------ .../Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md | 55 +++++++++++ .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 38 ++------ sidebars.json | 1 + 5 files changed, 155 insertions(+), 90 deletions(-) create mode 100644 docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md create mode 100644 i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md diff --git a/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md new file mode 100644 index 0000000000000..4f46a20accd51 --- /dev/null +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md @@ -0,0 +1,56 @@ +--- +{ + "title": "ALTER-TABLE-AND-GENERATED-COLUMN", + "language": "en" +} +--- + + + +## ALTER TABLE and generated columns +The ALTER TABLE ADD COLUMN is not supported for adding a generated column, and the ALTER TABLE MODIFY COLUMN is not supported for modifying generated column information. +The ALTER TABLE syntax is supported for modifying the order of generated columns, modifying the names of generated columns, and deleting generated columns. + +The following error is reported for unsupported scenarios: +```sql +mysql> CREATE TABLE test_alter_add_column(a int, b int) properties("replication_num"="1"); +Query OK, 0 rows affected (0.14 sec) +mysql> ALTER TABLE test_alter_add_column ADD COLUMN c int AS (a+b); +ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table add generated columns. +mysql> ALTER TABLE test_alter MODIFY COLUMN c int KEY AS (a+b+1); +ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table modify generated columns. +``` + +### REORDER COLUMN +```sql +ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); +``` +Note: +The modified column order still needs to meet the order restrictions when generating columns and creating tables. +### RENAME COLUMN +```sql +ALTER TABLE products RENAME COLUMN total_value new_name; +``` +Note: +If a column in a table (generated column or common column) is referenced by other generated columns, you need to delete the other generated columns before you can modify the name of this generated column. +### DROP COLUMN +```sql +ALTER TABLE products DROP COLUMN total_value; +``` +Note: +If a column in a table (generated column or ordinary column) is referenced by other generated columns, you need to delete the other generated columns first before deleting the referenced generated column or ordinary column. diff --git a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 3788a7954c54b..151912a33514a 100644 --- a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -1,7 +1,7 @@ --- { "title": "CREATE-TABLE-AND-GENERATED-COLUMN", - "language": "zh-CN" + "language": "en" } --- @@ -21,8 +21,8 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 -下面是一个使用生成列的例子: +CREATE TABLE supports specifying generated columns, where the value of a generated column is calculated from the expression specified in the column definition. +Here is an example using a generated column: ```sql CREATE TABLE products ( product_id INT, @@ -35,7 +35,7 @@ DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); INSERT INTO products VALUES(1, 10.00, 10, default); INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` -从表中查询数据: +Query data from the table: ```sql mysql> SELECT * FROM products; +------------+-------+----------+-------------+ @@ -45,42 +45,41 @@ mysql> SELECT * FROM products; | 1 | 20.00 | 10 | 200.00 | +------------+-------+----------+-------------+ ``` -在这个示例中, total_value 列是一个生成列,其值由 price 和 quantity 列的值相乘计算而来。 -在导入或更新时计算并存储在表中。 -## 语法 +In this example, the total_value column is a generated column whose value is calculated by multiplying the values ​​of the price and quantity columns. +The values of generated columns are calculated and stored in the table when importing or updating. +## Grammar ```sql col_name data_type [GENERATED ALWAYS] AS (expr) [NOT NULL | NULL] [COMMENT 'string'] ``` -## 生成列的限制 -1. 使用的函数只能是内置的标量函数和运算符,不允许使用udf,聚合函数等其它。 -2. 不允许使用变量,子查询,Lambda表达式。 -3. AUTO_INCREMENT列不能用作生成的列定义中的基列。 -4. 生成的列定义可以引用其他生成的列,但只能引用表定义中较早出现的列。 生成的列定义可以引用表中的任何基本(非生成)列,无论其定义发生得早还是晚。 -5. 聚合模型中,生成列是VALUE列时,仅允许使用REPLACE和REPLACE_IF_NOT_NULL聚合类型。 - -## 导入数据 -导入数据时,如果违反了生成列的NOT NULL限制,例如导入数据时,没有指定生成列引用的列,并且此列没有默认值,将导致导入失败。 +## Restrictions on generated columns +1. The functions used can only be built-in scalar functions and operators. UDF, aggregate functions, etc. are not allowed. +2. Variables, subqueries, and Lambda expressions are not allowed. +3. AUTO_INCREMENT columns cannot be used as base columns in generated column definitions. +4. Generated column definitions can reference other generated columns, but only columns that appear earlier in the table definition. Generated column definitions can reference any base (non-generated) column in the table, regardless of whether its definition occurs earlier or later. +5. In the aggregate model, when the generated column is a VALUE column, only REPLACE and REPLACE_IF_NOT_NULL aggregate types are allowed. +## Import data +When importing data, if the NOT NULL restriction of the generated column is violated, for example, when importing data, the column referenced by the generated column is not specified, and this column has no default value, the import will fail. ### INSERT -指定列时,指定的列不能包含生成列,否则将报错。 +When specifying columns, the specified columns cannot contain generated columns, otherwise an error will be reported. ```sql INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` -没有指定列时,生成列需要使用default关键字进行占位。 +When no columns are specified, the DEFAULT keyword must be used as a placeholder for the generated columns.。 ```sql INSERT INTO products VALUES(1, 10.00, 10, default); ``` -### LOAD -使用load方式进行数据导入时,需要显式指定导入列。不应当指定生成列为导入列,当指定导入生成列并在数据文件中有对应的数据时,生成列不会使用数据文件中的值,生成列的值仍然是根据表达式计算得到的结果。 -#### STREAM LOAD -创建表: +### Load +When using the load method to import data, you need to explicitly specify the import column. You should not specify a generated column as an import column. When you specify an import generated column and there is corresponding data in the data file, the generated column will not use the value in the data file, and the value of the generated column is still the result of the expression calculation. +#### Stream Load +Create table: ```sql mysql> CREATE TABLE gen_col_stream_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) not null) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` -准备数据,并进行stream load: +Prepare data and perform stream loading: ```shell cat gen_col_data.csv 1,2 @@ -113,7 +112,7 @@ curl --location-trusted -u root: \ "CommitAndPublishTimeMs": 37 } ``` -查看数据导入结果: +View the data import results: ```sql mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ @@ -125,14 +124,14 @@ mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ 3 rows in set (0.07 sec) ``` -#### HTTP STREAM LOAD -创建表: +#### HTTP Stream Load +Create table: ```sql mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL,b INT, d INT GENERATED ALWAYS AS(c+1)) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` -准备数据,并进行http stream load: +Prepare data and perform HTTP stream loading: ```shell curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ -H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ @@ -157,7 +156,7 @@ http://127.0.0.1:8030/api/_http_stream "CommitAndPublishTimeMs": 36 } ``` -查看数据导入结果: +View the data import results: ```sql mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ | a | c | b | d | @@ -168,7 +167,7 @@ mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ 3 rows in set (0.04 sec) ``` -#### MYSQL LOAD +#### MySQL Load ```sql mysql> CREATE TABLE gen_col_mysql_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL) DISTRIBUTED BY HASH(a) @@ -192,13 +191,12 @@ mysql> SELECT * FROM gen_col_mysql_load; +------+------+------+ 3 rows in set (0.06 sec) ``` -#### 其它LOAD -BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 - -## 生成列与部分列更新 -在进行部分列更新时,必须在columns中指定生成列引用的所有普通列,否则会报错。 +#### Other Load +BROKER LOAD, ROUTINE LOAD and other methods can import data into a table with generated columns, which will not be listed here. +## Generated columns and partial update +When updating some columns, you must specify all the common columns referenced by the generated columns in columns, otherwise an error will be reported. -下面是一个示例, 建表和插入一行数据,并设置session变量: +The following is an example to create a table, insert a row of data, and set the session variable: ```sql CREATE TABLE test_partial_column_unique_gen_col (a INT, b INT, c INT AS (a+b), d INT AS (c+1), e INT) UNIQUE KEY(a) DISTRIBUTED BY HASH(a) PROPERTIES( @@ -210,38 +208,15 @@ SET enable_insert_strict=false; SET enable_fallback_to_original_planner=false; INSERT INTO test_partial_column_unique_gen_col(a,b,e) VALUES(1,2,7); ``` -如果没有指定所有的被引用的普通列会报错: +If all referenced normal columns are not specified, an error will be reported: ```sql mysql> INSERT INTO test_partial_column_unique_gen_col(a) VALUES(3); ERROR 1105 (HY000): errCode = 2, detailMessage = Partial update should include all ordinary columns referenced by generated columns, missing: b ``` -LOAD也是这样,-H "columns: a, b"中需要指定所有被引用的普通列,下面是使用stream load的示例: +The same is true for LOAD. All referenced normal columns need to be specified in -H "columns: a, b". The following is an example of using stream load: ```shell curl --location-trusted -u root: -H "Expect:100-continue" -H "column_separator:," \ -H "columns: a, b" -H "partial_columns:true" \ -T /Users/moailing/Documents/tmp/gen_col_data.csv \ http://127.0.0.1:8030/api/testdb/partial_column_unique_gen_col/_stream_load ``` - - -## ALTER TABLE和生成列 -生成列暂时不支持ADD COLUMN, MODIFY COLUMN。 -### REORDER COLUMN -```sql -ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); -``` -注意事项: -修改后的列顺序仍然需要满足生成列建表时的顺序限制。 -### RENAME COLUMN -```sql -ALTER TABLE products RENAME COLUMN total_value new_name; -``` -注意事项: -如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能修改此生成列的名称。 -### DROP COLUMN -```sql -ALTER TABLE products DROP COLUMN total_value; -``` -注意事项: -如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md new file mode 100644 index 0000000000000..189a77249118d --- /dev/null +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md @@ -0,0 +1,55 @@ +--- +{ + "title": "ALTER-TABLE-AND-GENERATED-COLUMN", + "language": "zh-CN" +} +--- + + + + +## ALTER TABLE和生成列 +不支持使用ALTER TABLE ADD COLUMN增加一个生成列,不支持使用ALTER TABLE MODIFY COLUMN修改生成列信息。支持使用ALTER TABLE对生成列顺序进行修改,修改生成列名称和删除生成列。 + +不支持的场景报错如下: +```sql +mysql> CREATE TABLE test_alter_add_column(a int, b int) properties("replication_num"="1"); +Query OK, 0 rows affected (0.14 sec) +mysql> ALTER TABLE test_alter_add_column ADD COLUMN c int AS (a+b); +ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table add generated columns. +mysql> ALTER TABLE test_alter MODIFY COLUMN c int KEY AS (a+b+1); +ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table modify generated columns. +``` +### REORDER COLUMN +```sql +ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); +``` +注意事项: +修改后的列顺序仍然需要满足生成列建表时的顺序限制。 +### RENAME COLUMN +```sql +ALTER TABLE products RENAME COLUMN total_value new_name; +``` +注意事项: +如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能修改此生成列的名称。 +### DROP COLUMN +```sql +ALTER TABLE products DROP COLUMN total_value; +``` +注意事项: +如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 3586032460e10..26a9705309da2 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -46,7 +46,7 @@ mysql> SELECT * FROM products; +------------+-------+----------+-------------+ ``` 在这个示例中, total_value 列是一个生成列,其值由 price 和 quantity 列的值相乘计算而来。 -在导入或更新时计算并存储在表中。 +生成列的值在导入或更新时计算并存储在表中。 ## 语法 ```sql col_name data_type [GENERATED ALWAYS] AS (expr) @@ -66,14 +66,14 @@ col_name data_type [GENERATED ALWAYS] AS (expr) ```sql INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` -没有指定列时,生成列需要使用default关键字进行占位。 +没有指定列时,生成列需要使用DEFAULT关键字进行占位。 ```sql INSERT INTO products VALUES(1, 10.00, 10, default); ``` -### LOAD +### Load 使用load方式进行数据导入时,需要显式指定导入列。不应当指定生成列为导入列,当指定导入生成列并在数据文件中有对应的数据时,生成列不会使用数据文件中的值,生成列的值仍然是根据表达式计算得到的结果。 -#### STREAM LOAD +#### Stream Load 创建表: ```sql mysql> CREATE TABLE gen_col_stream_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) not null) @@ -125,7 +125,7 @@ mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ 3 rows in set (0.07 sec) ``` -#### HTTP STREAM LOAD +#### HTTP Stream Load 创建表: ```sql mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL,b INT, d INT GENERATED ALWAYS AS(c+1)) @@ -168,7 +168,7 @@ mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ 3 rows in set (0.04 sec) ``` -#### MYSQL LOAD +#### MySQL Load ```sql mysql> CREATE TABLE gen_col_mysql_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL) DISTRIBUTED BY HASH(a) @@ -192,7 +192,7 @@ mysql> SELECT * FROM gen_col_mysql_load; +------+------+------+ 3 rows in set (0.06 sec) ``` -#### 其它LOAD +#### 其它Load BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 ## 生成列与部分列更新 @@ -210,7 +210,7 @@ SET enable_insert_strict=false; SET enable_fallback_to_original_planner=false; INSERT INTO test_partial_column_unique_gen_col(a,b,e) VALUES(1,2,7); ``` -如果没有指定所有的被引用的普通列会报错: +如果没有指定所有被引用的普通列会报错: ```sql mysql> INSERT INTO test_partial_column_unique_gen_col(a) VALUES(3); ERROR 1105 (HY000): errCode = 2, detailMessage = Partial update should include all ordinary columns referenced by generated columns, missing: b @@ -222,25 +222,3 @@ curl --location-trusted -u root: -H "Expect:100-continue" -H "column_separator:, -T /Users/moailing/Documents/tmp/gen_col_data.csv \ http://127.0.0.1:8030/api/testdb/partial_column_unique_gen_col/_stream_load ``` - - -## ALTER TABLE和生成列 -生成列暂时不支持ADD COLUMN, MODIFY COLUMN。 -### REORDER COLUMN -```sql -ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); -``` -注意事项: -修改后的列顺序仍然需要满足生成列建表时的顺序限制。 -### RENAME COLUMN -```sql -ALTER TABLE products RENAME COLUMN total_value new_name; -``` -注意事项: -如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能修改此生成列的名称。 -### DROP COLUMN -```sql -ALTER TABLE products DROP COLUMN total_value; -``` -注意事项: -如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 diff --git a/sidebars.json b/sidebars.json index 8c85621f7d89f..cbfe92a00459d 100644 --- a/sidebars.json +++ b/sidebars.json @@ -1251,6 +1251,7 @@ "sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-REPLACE", "sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-PROPERTY", "sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-COMMENT", + "sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN", "sql-manual/sql-statements/Data-Definition-Statements/Alter/CANCEL-ALTER-TABLE", "sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-VIEW", "sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-STORAGE-POLICY", From 73c5aa6d30385efd88e213d46e804c02bc1a22a8 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 9 Jul 2024 16:51:49 +0800 Subject: [PATCH 5/6] [docs](ddl) Add blank lines before and after code blocks --- .../Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md | 7 +++++ .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 29 +++++++++++++++++++ .../Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md | 14 +++++++-- .../CREATE-TABLE-AND-GENERATED-COLUMN.md | 29 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md index 4f46a20accd51..92793613bdfa3 100644 --- a/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md @@ -27,6 +27,7 @@ The ALTER TABLE ADD COLUMN is not supported for adding a generated column, and t The ALTER TABLE syntax is supported for modifying the order of generated columns, modifying the names of generated columns, and deleting generated columns. The following error is reported for unsupported scenarios: + ```sql mysql> CREATE TABLE test_alter_add_column(a int, b int) properties("replication_num"="1"); Query OK, 0 rows affected (0.14 sec) @@ -37,20 +38,26 @@ ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table modi ``` ### REORDER COLUMN + ```sql ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); ``` + Note: The modified column order still needs to meet the order restrictions when generating columns and creating tables. ### RENAME COLUMN + ```sql ALTER TABLE products RENAME COLUMN total_value new_name; ``` + Note: If a column in a table (generated column or common column) is referenced by other generated columns, you need to delete the other generated columns before you can modify the name of this generated column. ### DROP COLUMN + ```sql ALTER TABLE products DROP COLUMN total_value; ``` + Note: If a column in a table (generated column or ordinary column) is referenced by other generated columns, you need to delete the other generated columns first before deleting the referenced generated column or ordinary column. diff --git a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 151912a33514a..1dba9cd6d5b0c 100644 --- a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -23,6 +23,7 @@ under the License. --> CREATE TABLE supports specifying generated columns, where the value of a generated column is calculated from the expression specified in the column definition. Here is an example using a generated column: + ```sql CREATE TABLE products ( product_id INT, @@ -35,7 +36,9 @@ DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); INSERT INTO products VALUES(1, 10.00, 10, default); INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` + Query data from the table: + ```sql mysql> SELECT * FROM products; +------------+-------+----------+-------------+ @@ -45,13 +48,16 @@ mysql> SELECT * FROM products; | 1 | 20.00 | 10 | 200.00 | +------------+-------+----------+-------------+ ``` + In this example, the total_value column is a generated column whose value is calculated by multiplying the values ​​of the price and quantity columns. The values of generated columns are calculated and stored in the table when importing or updating. ## Grammar + ```sql col_name data_type [GENERATED ALWAYS] AS (expr) [NOT NULL | NULL] [COMMENT 'string'] ``` + ## Restrictions on generated columns 1. The functions used can only be built-in scalar functions and operators. UDF, aggregate functions, etc. are not allowed. 2. Variables, subqueries, and Lambda expressions are not allowed. @@ -62,10 +68,13 @@ col_name data_type [GENERATED ALWAYS] AS (expr) When importing data, if the NOT NULL restriction of the generated column is violated, for example, when importing data, the column referenced by the generated column is not specified, and this column has no default value, the import will fail. ### INSERT When specifying columns, the specified columns cannot contain generated columns, otherwise an error will be reported. + ```sql INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` + When no columns are specified, the DEFAULT keyword must be used as a placeholder for the generated columns.。 + ```sql INSERT INTO products VALUES(1, 10.00, 10, default); ``` @@ -74,12 +83,15 @@ INSERT INTO products VALUES(1, 10.00, 10, default); When using the load method to import data, you need to explicitly specify the import column. You should not specify a generated column as an import column. When you specify an import generated column and there is corresponding data in the data file, the generated column will not use the value in the data file, and the value of the generated column is still the result of the expression calculation. #### Stream Load Create table: + ```sql mysql> CREATE TABLE gen_col_stream_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) not null) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` + Prepare data and perform stream loading: + ```shell cat gen_col_data.csv 1,2 @@ -112,7 +124,9 @@ curl --location-trusted -u root: \ "CommitAndPublishTimeMs": 37 } ``` + View the data import results: + ```sql mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ @@ -124,14 +138,18 @@ mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ 3 rows in set (0.07 sec) ``` + #### HTTP Stream Load Create table: + ```sql mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL,b INT, d INT GENERATED ALWAYS AS(c+1)) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` + Prepare data and perform HTTP stream loading: + ```shell curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ -H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ @@ -156,7 +174,9 @@ http://127.0.0.1:8030/api/_http_stream "CommitAndPublishTimeMs": 36 } ``` + View the data import results: + ```sql mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ | a | c | b | d | @@ -167,7 +187,10 @@ mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ 3 rows in set (0.04 sec) ``` + #### MySQL Load +The process of creating a table, loading data and querying is as follows: + ```sql mysql> CREATE TABLE gen_col_mysql_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL) DISTRIBUTED BY HASH(a) @@ -191,12 +214,14 @@ mysql> SELECT * FROM gen_col_mysql_load; +------+------+------+ 3 rows in set (0.06 sec) ``` + #### Other Load BROKER LOAD, ROUTINE LOAD and other methods can import data into a table with generated columns, which will not be listed here. ## Generated columns and partial update When updating some columns, you must specify all the common columns referenced by the generated columns in columns, otherwise an error will be reported. The following is an example to create a table, insert a row of data, and set the session variable: + ```sql CREATE TABLE test_partial_column_unique_gen_col (a INT, b INT, c INT AS (a+b), d INT AS (c+1), e INT) UNIQUE KEY(a) DISTRIBUTED BY HASH(a) PROPERTIES( @@ -208,12 +233,16 @@ SET enable_insert_strict=false; SET enable_fallback_to_original_planner=false; INSERT INTO test_partial_column_unique_gen_col(a,b,e) VALUES(1,2,7); ``` + If all referenced normal columns are not specified, an error will be reported: + ```sql mysql> INSERT INTO test_partial_column_unique_gen_col(a) VALUES(3); ERROR 1105 (HY000): errCode = 2, detailMessage = Partial update should include all ordinary columns referenced by generated columns, missing: b ``` + The same is true for LOAD. All referenced normal columns need to be specified in -H "columns: a, b". The following is an example of using stream load: + ```shell curl --location-trusted -u root: -H "Expect:100-continue" -H "column_separator:," \ -H "columns: a, b" -H "partial_columns:true" \ diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md index 189a77249118d..fc8d84012d07b 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Alter/ALTER-TABLE-AND-GENERATED-COLUMN.md @@ -27,6 +27,7 @@ under the License. 不支持使用ALTER TABLE ADD COLUMN增加一个生成列,不支持使用ALTER TABLE MODIFY COLUMN修改生成列信息。支持使用ALTER TABLE对生成列顺序进行修改,修改生成列名称和删除生成列。 不支持的场景报错如下: + ```sql mysql> CREATE TABLE test_alter_add_column(a int, b int) properties("replication_num"="1"); Query OK, 0 rows affected (0.14 sec) @@ -35,21 +36,28 @@ ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table add mysql> ALTER TABLE test_alter MODIFY COLUMN c int KEY AS (a+b+1); ERROR 1105 (HY000): errCode = 2, detailMessage = Not supporting alter table modify generated columns. ``` + ### REORDER COLUMN + ```sql ALTER TABLE products ORDER BY (product_id, total_value, price, quantity); ``` -注意事项: + +注意: 修改后的列顺序仍然需要满足生成列建表时的顺序限制。 ### RENAME COLUMN + ```sql ALTER TABLE products RENAME COLUMN total_value new_name; ``` -注意事项: + +注意: 如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能修改此生成列的名称。 ### DROP COLUMN + ```sql ALTER TABLE products DROP COLUMN total_value; ``` -注意事项: + +注意: 如果表中某列(生成列或者普通列)被其它生成列引用,需要先删除其它生成列后,才能删除此被引用的生成列或者普通列。 diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 26a9705309da2..32fe233e191dc 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -23,6 +23,7 @@ under the License. --> CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 下面是一个使用生成列的例子: + ```sql CREATE TABLE products ( product_id INT, @@ -35,7 +36,9 @@ DISTRIBUTED BY HASH(product_id) PROPERTIES ("replication_num" = "1"); INSERT INTO products VALUES(1, 10.00, 10, default); INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` + 从表中查询数据: + ```sql mysql> SELECT * FROM products; +------------+-------+----------+-------------+ @@ -45,13 +48,16 @@ mysql> SELECT * FROM products; | 1 | 20.00 | 10 | 200.00 | +------------+-------+----------+-------------+ ``` + 在这个示例中, total_value 列是一个生成列,其值由 price 和 quantity 列的值相乘计算而来。 生成列的值在导入或更新时计算并存储在表中。 ## 语法 + ```sql col_name data_type [GENERATED ALWAYS] AS (expr) [NOT NULL | NULL] [COMMENT 'string'] ``` + ## 生成列的限制 1. 使用的函数只能是内置的标量函数和运算符,不允许使用udf,聚合函数等其它。 2. 不允许使用变量,子查询,Lambda表达式。 @@ -63,10 +69,13 @@ col_name data_type [GENERATED ALWAYS] AS (expr) 导入数据时,如果违反了生成列的NOT NULL限制,例如导入数据时,没有指定生成列引用的列,并且此列没有默认值,将导致导入失败。 ### INSERT 指定列时,指定的列不能包含生成列,否则将报错。 + ```sql INSERT INTO products(product_id, price, quantity) VALUES(1, 20.00, 10); ``` + 没有指定列时,生成列需要使用DEFAULT关键字进行占位。 + ```sql INSERT INTO products VALUES(1, 10.00, 10, default); ``` @@ -75,12 +84,15 @@ INSERT INTO products VALUES(1, 10.00, 10, default); 使用load方式进行数据导入时,需要显式指定导入列。不应当指定生成列为导入列,当指定导入生成列并在数据文件中有对应的数据时,生成列不会使用数据文件中的值,生成列的值仍然是根据表达式计算得到的结果。 #### Stream Load 创建表: + ```sql mysql> CREATE TABLE gen_col_stream_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) not null) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` + 准备数据,并进行stream load: + ```shell cat gen_col_data.csv 1,2 @@ -113,7 +125,9 @@ curl --location-trusted -u root: \ "CommitAndPublishTimeMs": 37 } ``` + 查看数据导入结果: + ```sql mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ @@ -125,14 +139,18 @@ mysql> SELECT * FROM gen_col_stream_load; +------+------+------+ 3 rows in set (0.07 sec) ``` + #### HTTP Stream Load 创建表: + ```sql mysql> CREATE TABLE gencol_refer_gencol_http_load(a INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL,b INT, d INT GENERATED ALWAYS AS(c+1)) DISTRIBUTED BY HASH(a) PROPERTIES("replication_num" = "1"); ``` + 准备数据,并进行http stream load: + ```shell curl --location-trusted -u root: -T gen_col_data.csv -H "Expect: 100-Continue" \ -H "sql:insert into testdb.gencol_refer_gencol_http_load(a, b) select * from http_stream(\"format\" = \"CSV\", \"column_separator\" = \",\" )" \ @@ -157,7 +175,9 @@ http://127.0.0.1:8030/api/_http_stream "CommitAndPublishTimeMs": 36 } ``` + 查看数据导入结果: + ```sql mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ | a | c | b | d | @@ -168,7 +188,10 @@ mysql> SELECT * FROM gencol_refer_gencol_http_load; +------+------+------+------+ 3 rows in set (0.04 sec) ``` + #### MySQL Load +建表,加载数据和查询的过程如下: + ```sql mysql> CREATE TABLE gen_col_mysql_load(a INT,b INT,c DOUBLE GENERATED ALWAYS AS (abs(a+b)) NOT NULL) DISTRIBUTED BY HASH(a) @@ -192,6 +215,7 @@ mysql> SELECT * FROM gen_col_mysql_load; +------+------+------+ 3 rows in set (0.06 sec) ``` + #### 其它Load BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表,不再一一列举。 @@ -199,6 +223,7 @@ BROKER LOAD, ROUTINE LOAD等方式都可以将数据导入有生成列的表, 在进行部分列更新时,必须在columns中指定生成列引用的所有普通列,否则会报错。 下面是一个示例, 建表和插入一行数据,并设置session变量: + ```sql CREATE TABLE test_partial_column_unique_gen_col (a INT, b INT, c INT AS (a+b), d INT AS (c+1), e INT) UNIQUE KEY(a) DISTRIBUTED BY HASH(a) PROPERTIES( @@ -210,12 +235,16 @@ SET enable_insert_strict=false; SET enable_fallback_to_original_planner=false; INSERT INTO test_partial_column_unique_gen_col(a,b,e) VALUES(1,2,7); ``` + 如果没有指定所有被引用的普通列会报错: + ```sql mysql> INSERT INTO test_partial_column_unique_gen_col(a) VALUES(3); ERROR 1105 (HY000): errCode = 2, detailMessage = Partial update should include all ordinary columns referenced by generated columns, missing: b ``` + LOAD也是这样,-H "columns: a, b"中需要指定所有被引用的普通列,下面是使用stream load的示例: + ```shell curl --location-trusted -u root: -H "Expect:100-continue" -H "column_separator:," \ -H "columns: a, b" -H "partial_columns:true" \ From 6de6000465ee2f730580cca6bf856fbc5cf0fa1f Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 9 Jul 2024 18:05:38 +0800 Subject: [PATCH 6/6] [docs](ddl) Add usage scenarios for generated columns --- .../Create/CREATE-TABLE-AND-GENERATED-COLUMN.md | 5 ++++- .../Create/CREATE-TABLE-AND-GENERATED-COLUMN.md | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 1dba9cd6d5b0c..5c9c758122ca5 100644 --- a/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -21,7 +21,10 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -CREATE TABLE supports specifying generated columns, where the value of a generated column is calculated from the expression specified in the column definition. +A generated column is a special database table column whose value is calculated from the values of other columns rather than directly inserted or updated by the user. This feature supports pre-computing the results of expressions and storing them in the database, which is suitable for scenarios that require frequent queries or complex calculations. + +Generated columns can automatically calculate results based on predefined expressions when data is imported or updated, and store these results persistently. In this way, during subsequent queries, you can directly access these calculated results without performing complex calculations during queries, thereby significantly reducing the computational burden during queries and improving query performance. + Here is an example using a generated column: ```sql diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md index 32fe233e191dc..feb2e28968579 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE-AND-GENERATED-COLUMN.md @@ -21,7 +21,11 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> -CREATE TABLE 支持指定生成列,生成列的值是从列定义中指定的表达式中计算得到的。 + +生成列是一种特殊的数据库表列,其值由其他列的值计算而来,而不是直接由用户插入或更新。该功能支持预先计算表达式的结果,并存储在数据库中,适用于需要频繁查询或进行复杂计算的场景。 + +生成列可以在数据导入或更新时自动根据预定义的表达式计算结果,并将这些结果持久化存储。这样,在后续的查询过程中,可以直接访问这些已经计算好的结果,而无需在查询时再进行复杂的计算,从而显著减少查询时的计算负担,提升查询性能。 + 下面是一个使用生成列的例子: ```sql