-
Notifications
You must be signed in to change notification settings - Fork 387
[Feature](function) Support function DEFAULT #3029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
linrrzqqq
wants to merge
3
commits into
apache:master
Choose a base branch
from
linrrzqqq:default
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
docs/sql-manual/sql-functions/scalar-functions/other-functions/default.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| --- | ||
| { | ||
| "title": "DEFAULT", | ||
| "language": "en" | ||
| } | ||
| --- | ||
|
|
||
| ## Description | ||
|
|
||
| Returns the default value of a specific column in the table. If the column has no default value, an error is thrown. | ||
|
|
||
| This function behaves consistently with the [DEFAULT function](https://dev.mysql.com/doc/refman/8.4/en/miscellaneous-functions.html#function_default) in MySQL. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```sql | ||
| DEFAULT(<column>) | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Description | | ||
| | ---------- | ---------------------------------------------------------------------------------------------------------- | | ||
| | `<column>` | The column for which to query the default value. Only columns are accepted; otherwise, an error is thrown. | | ||
|
|
||
| ## Return Value | ||
|
|
||
| Constant default values(include `PI`, `E`): | ||
| - If a default value is set, returns the corresponding default value. | ||
| - If no default value is set and the column is not NOT NULL, returns NULL. | ||
| - If no default value is set and the column is NOT NULL, throws an error. | ||
|
|
||
| Non-constant default values (`CURRENT_TIMESTAMP`, `CURRENT_DATE`): | ||
| - If the column is not NOT NULL, returns NULL. | ||
| - If the column is NOT NULL, returns the minimum value of the corresponding type `0000-01-01 00:00:00` or `0000-01-01`. | ||
|
|
||
| Special Cases: | ||
| - If the input column is of an aggregate type (`HLL`, `BITMAP`, `QUANTILE_STATE`), throws an error. | ||
| - Input parameters are only allowed for columns. If the input is a constant (including NULL) or an expression, throws an error. | ||
|
|
||
| [Doris supported default value related parameters](https://doris.apache.org/docs/dev/sql-manual/sql-statements/table-and-view/table/CREATE-TABLE#column-default-value-related-parameters) | ||
|
|
||
| ## Examples | ||
|
|
||
| 1. Non-aggregate type constant default value example | ||
| ```sql | ||
| -- setup | ||
| -- Columns without explicitly specified DEFAULT values accept only NULL as default | ||
| -- ARRAY type default values accept only `[]` and NULL | ||
| CREATE TABLE test_default_scalar ( | ||
| c_bool BOOLEAN NULL DEFAULT 1, | ||
| c_tinyint TINYINT NULL DEFAULT 7, | ||
| c_smallint SMALLINT NULL DEFAULT 32000, | ||
| c_int INT NULL DEFAULT 2147483647, | ||
| c_bigint BIGINT NULL DEFAULT 9223372036854775807, | ||
| c_largeint LARGEINT NULL DEFAULT '170141183460469231731687303715884105727', | ||
| c_float FLOAT NULL DEFAULT 3.125, | ||
| c_double DOUBLE NULL DEFAULT 2.718281828, | ||
| c_decimal DECIMAL(27, 9) NULL DEFAULT '123456789.123456789', | ||
| c_decimal_compact DECIMAL(18, 4) NULL DEFAULT '99999.1234', | ||
| c_pi DOUBLE NULL DEFAULT PI, | ||
| c_e DOUBLE NULL DEFAULT E, | ||
| c_char CHAR(8) NULL DEFAULT 'charDemo', | ||
| c_varchar VARCHAR(32) NULL DEFAULT '', | ||
| c_string STRING NULL DEFAULT 'plain string', | ||
| c_datetime DATETIME NULL DEFAULT '2025-10-25 11:22:33', | ||
| c_date DATE NULL DEFAULT '2025-10-31', | ||
| c_json JSON NULL, | ||
| c_ipv4 IPV4 NULL DEFAULT '192.168.1.1', | ||
| c_ipv6 IPV6 NULL DEFAULT '2001:db8::1', | ||
| c_array_int ARRAY<INT> NULL DEFAULT '[]', | ||
| c_array_string ARRAY<STRING> NULL, | ||
| c_map_str_int MAP<STRING, INT> NULL, | ||
| c_struct STRUCT<f1:INT,f2:STRING,f3:BOOLEAN> NULL, | ||
| c_variant VARIANT NULL | ||
| ) PROPERTIES ( 'replication_num' = '1'); | ||
|
|
||
| -- INSERT statements omitted in the example | ||
| SELECT | ||
| DEFAULT(c_bool), | ||
| DEFAULT(c_tinyint), | ||
| DEFAULT(c_smallint), | ||
| DEFAULT(c_int), | ||
| DEFAULT(c_bigint), | ||
| DEFAULT(c_largeint), | ||
| DEFAULT(c_float), | ||
| DEFAULT(c_double), | ||
| DEFAULT(c_decimal), | ||
| DEFAULT(c_decimal_compact), | ||
| DEFAULT(c_pi), | ||
| DEFAULT(c_e), | ||
| DEFAULT(c_char), | ||
| DEFAULT(c_varchar), | ||
| DEFAULT(c_string), | ||
| DEFAULT(c_datetime), | ||
| DEFAULT(c_date), | ||
| DEFAULT(c_json), | ||
| DEFAULT(c_ipv4), | ||
| DEFAULT(c_ipv6), | ||
| DEFAULT(c_array_int), | ||
| DEFAULT(c_array_string), | ||
| DEFAULT(c_map_str_int), | ||
| DEFAULT(c_struct), | ||
| DEFAULT(c_variant) | ||
| FROM test_default_scalar | ||
| LIMIT 1; | ||
| ``` | ||
| ```text | ||
| +-----------------+--------------------+---------------------+----------------+---------------------+-----------------------------------------+------------------+-------------------+---------------------+----------------------------+-------------------+-------------------+-----------------+--------------------+-------------------+---------------------+-----------------+-----------------+-----------------+-----------------+----------------------+-------------------------+------------------------+-------------------+--------------------+ | ||
| | DEFAULT(c_bool) | DEFAULT(c_tinyint) | DEFAULT(c_smallint) | DEFAULT(c_int) | DEFAULT(c_bigint) | DEFAULT(c_largeint) | DEFAULT(c_float) | DEFAULT(c_double) | DEFAULT(c_decimal) | DEFAULT(c_decimal_compact) | DEFAULT(c_pi) | DEFAULT(c_e) | DEFAULT(c_char) | DEFAULT(c_varchar) | DEFAULT(c_string) | DEFAULT(c_datetime) | DEFAULT(c_date) | DEFAULT(c_json) | DEFAULT(c_ipv4) | DEFAULT(c_ipv6) | DEFAULT(c_array_int) | DEFAULT(c_array_string) | DEFAULT(c_map_str_int) | DEFAULT(c_struct) | DEFAULT(c_variant) | | ||
| +-----------------+--------------------+---------------------+----------------+---------------------+-----------------------------------------+------------------+-------------------+---------------------+----------------------------+-------------------+-------------------+-----------------+--------------------+-------------------+---------------------+-----------------+-----------------+-----------------+-----------------+----------------------+-------------------------+------------------------+-------------------+--------------------+ | ||
| | 1 | 7 | 32000 | 2147483647 | 9223372036854775807 | 170141183460469231731687303715884105727 | 3.125 | 2.718281828 | 123456789.123456789 | 99999.1234 | 3.141592653589793 | 2.718281828459045 | charDemo | | plain string | 2025-10-25 11:22:33 | 2025-10-31 | NULL | 192.168.1.1 | 2001:db8::1 | [] | NULL | NULL | NULL | NULL | | ||
| +-----------------+--------------------+---------------------+----------------+---------------------+-----------------------------------------+------------------+-------------------+---------------------+----------------------------+-------------------+-------------------+-----------------+--------------------+-------------------+---------------------+-----------------+-----------------+-----------------+-----------------+----------------------+-------------------------+------------------------+-------------------+--------------------+ | ||
| ``` | ||
|
|
||
| 2. Aggregate type example | ||
| ```sql | ||
| CREATE TABLE test_default_agg ( | ||
| k_id INT NOT NULL COMMENT '聚合键', | ||
| bitmap_col BITMAP BITMAP_UNION, | ||
| hll_col HLL HLL_UNION, | ||
| quantile_col QUANTILE_STATE QUANTILE_UNION | ||
| )AGGREGATE KEY(k_id) | ||
| PROPERTIES ( 'replication_num' = '1' ); | ||
|
|
||
| SELECT | ||
| default(bitmap_col),default(hll_col) | ||
| FROM test_default_agg | ||
| LIMIT 1; | ||
| -- ERROR 1105 (HY000): errCode = 2, detailMessage = (127.0.0.1)[INVALID_ARGUMENT]Agg type(HLL, BITMAP, QUANTILE_STATE) cannot be used for the DEFAULT function | ||
| ``` | ||
|
|
||
| 3. Non-constant default value (`CURRENT_TIMESTAMP`, `CURRENT_DATE`) example: | ||
| ```sql | ||
| CREATE TABLE test_non_const_default_value_t ( | ||
| null_dt DATETIME NULL DEFAULT CURRENT_TIMESTAMP, | ||
| not_null_dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| null_d DATE NULL DEFAULT CURRENT_DATE, | ||
| not_null_d DATE NOT NULL DEFAULT CURRENT_DATE | ||
| ) PROPERTIES ( 'replication_num' = '1' ); | ||
|
|
||
|
|
||
| SELECT | ||
| DEFAULT(null_dt), DEFAULT(not_null_dt), | ||
| DEFAULT(null_d), DEFAULT(not_null_d) | ||
| FROM test_non_const_default_value_t | ||
| LIMIT 1; | ||
| ``` | ||
| ```text | ||
| +------------------+----------------------+-----------------+---------------------+ | ||
| | DEFAULT(null_dt) | DEFAULT(not_null_dt) | DEFAULT(null_d) | DEFAULT(not_null_d) | | ||
| +------------------+----------------------+-----------------+---------------------+ | ||
| | NULL | 0000-01-01 00:00:00 | NULL | 0000-01-01 | | ||
| +------------------+----------------------+-----------------+---------------------+ | ||
| ``` | ||
|
|
||
| 4. constant value | ||
| ```sql | ||
| SELECT DEFAULT('hello'); | ||
| -- ERROR 1105 (HY000): errCode = 2, detailMessage = DEFAULT function requires a column reference, not a constant or expression | ||
|
|
||
| SELECT DEFAULT(NULL); | ||
| -- ERROR 1105 (HY000): errCode = 2, detailMessage = DEFAULT function requires a column reference, not a constant or expression | ||
| ``` |
164 changes: 164 additions & 0 deletions
164
...cs/current/sql-manual/sql-functions/scalar-functions/other-functions/default.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| --- | ||
| { | ||
| "title": "DEFAULT", | ||
| "language": "zh-CN" | ||
| } | ||
| --- | ||
|
|
||
| ## 描述 | ||
|
|
||
| 返回表中特定列的默认值。如果该列没有默认值,则会抛出错误。 | ||
|
|
||
| 该函数与 MySQL 中的 [DEFAULT 函数](https://dev.mysql.com/doc/refman/8.4/en/miscellaneous-functions.html#function_default) 行为一致 | ||
|
|
||
| ## 语法 | ||
|
|
||
| ```sql | ||
| DEFAULT(<column>) | ||
| ``` | ||
|
|
||
| ## 参数 | ||
|
|
||
| | 参数 | 说明 | | ||
| | ---------- | ---------------------------------------------- | | ||
| | `<column>` | 需要查询默认值的列。仅接受列,否则会抛出错误。 | | ||
|
|
||
| ## 返回值 | ||
|
|
||
| 常量默认值(包括`PI`, `E`): | ||
| - 若有设置默认值,则返回对应的默认值 | ||
| - 若没有设置默认值,且列不为 NOT NULL, 则返回 NULL | ||
| - 若没有设置默认值,且列为 NOT NULL, 则抛出错误 | ||
|
|
||
| 非常量默认值(`CURRENT_TIMESTAMP`, `CURRENT_DATE`): | ||
zclllyybb marked this conversation as resolved.
Show resolved
Hide resolved
linrrzqqq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - 若该列不为 NOT NULL,则返回 NULL | ||
| - 若该列为 NOT NULL,则返回对应类型的最小值 `0000-01-01 00:00:00` 或 `0000-01-01` | ||
|
|
||
| 特殊情况: | ||
| - 当输入列为聚合类型时(`HLL`, `BITMAP`, `QUANTILE_STATE`), 抛出错误 | ||
| - 输入参数仅允许列,当输入为常量(包括 NULL)或表达式时,抛出错误 | ||
|
|
||
| [Doris 支持的默认值相关参数](https://doris.apache.org/zh-CN/docs/dev/sql-manual/sql-statements/table-and-view/table/CREATE-TABLE#%E5%88%97%E7%9A%84%E9%BB%98%E8%AE%A4%E5%80%BC%E7%9B%B8%E5%85%B3%E5%8F%82%E6%95%B0) | ||
|
|
||
| ## 举例 | ||
|
|
||
| 1. 非聚合类型常量默认值举例 | ||
| ```sql | ||
| -- setup | ||
| -- 表中没有显示表明DEFAULT值的代表其默认值仅接受 NULL 值 | ||
| -- ARRAY 类型的默认值仅接受 `[]` 和 NULL | ||
| CREATE TABLE test_default_scalar ( | ||
| c_bool BOOLEAN NULL DEFAULT 1, | ||
| c_tinyint TINYINT NULL DEFAULT 7, | ||
| c_smallint SMALLINT NULL DEFAULT 32000, | ||
| c_int INT NULL DEFAULT 2147483647, | ||
| c_bigint BIGINT NULL DEFAULT 9223372036854775807, | ||
| c_largeint LARGEINT NULL DEFAULT '170141183460469231731687303715884105727', | ||
| c_float FLOAT NULL DEFAULT 3.125, | ||
| c_double DOUBLE NULL DEFAULT 2.718281828, | ||
| c_decimal DECIMAL(27, 9) NULL DEFAULT '123456789.123456789', | ||
| c_decimal_compact DECIMAL(18, 4) NULL DEFAULT '99999.1234', | ||
| c_pi DOUBLE NULL DEFAULT PI, | ||
| c_e DOUBLE NULL DEFAULT E, | ||
| c_char CHAR(8) NULL DEFAULT 'charDemo', | ||
| c_varchar VARCHAR(32) NULL DEFAULT '', | ||
| c_string STRING NULL DEFAULT 'plain string', | ||
| c_datetime DATETIME NULL DEFAULT '2025-10-25 11:22:33', | ||
| c_date DATE NULL DEFAULT '2025-10-31', | ||
| c_json JSON NULL, | ||
| c_ipv4 IPV4 NULL DEFAULT '192.168.1.1', | ||
| c_ipv6 IPV6 NULL DEFAULT '2001:db8::1', | ||
| c_array_int ARRAY<INT> NULL DEFAULT '[]', | ||
| c_array_string ARRAY<STRING> NULL, | ||
| c_map_str_int MAP<STRING, INT> NULL, | ||
| c_struct STRUCT<f1:INT,f2:STRING,f3:BOOLEAN> NULL, | ||
| c_variant VARIANT NULL | ||
| ) PROPERTIES ( 'replication_num' = '1'); | ||
|
|
||
| -- 例中省略 INSERT 语句 | ||
| SELECT | ||
| DEFAULT(c_bool), | ||
| DEFAULT(c_tinyint), | ||
| DEFAULT(c_smallint), | ||
| DEFAULT(c_int), | ||
| DEFAULT(c_bigint), | ||
| DEFAULT(c_largeint), | ||
| DEFAULT(c_float), | ||
| DEFAULT(c_double), | ||
| DEFAULT(c_decimal), | ||
| DEFAULT(c_decimal_compact), | ||
| DEFAULT(c_pi), | ||
| DEFAULT(c_e), | ||
| DEFAULT(c_char), | ||
| DEFAULT(c_varchar), | ||
| DEFAULT(c_string), | ||
| DEFAULT(c_datetime), | ||
| DEFAULT(c_date), | ||
| DEFAULT(c_json), | ||
| DEFAULT(c_ipv4), | ||
| DEFAULT(c_ipv6), | ||
| DEFAULT(c_array_int), | ||
| DEFAULT(c_array_string), | ||
| DEFAULT(c_map_str_int), | ||
| DEFAULT(c_struct), | ||
| DEFAULT(c_variant) | ||
| FROM test_default_scalar | ||
| LIMIT 1; | ||
| ``` | ||
| ```text | ||
| +-----------------+--------------------+---------------------+----------------+---------------------+-----------------------------------------+------------------+-------------------+---------------------+----------------------------+-------------------+-------------------+-----------------+--------------------+-------------------+---------------------+-----------------+-----------------+-----------------+-----------------+----------------------+-------------------------+------------------------+-------------------+--------------------+ | ||
| | DEFAULT(c_bool) | DEFAULT(c_tinyint) | DEFAULT(c_smallint) | DEFAULT(c_int) | DEFAULT(c_bigint) | DEFAULT(c_largeint) | DEFAULT(c_float) | DEFAULT(c_double) | DEFAULT(c_decimal) | DEFAULT(c_decimal_compact) | DEFAULT(c_pi) | DEFAULT(c_e) | DEFAULT(c_char) | DEFAULT(c_varchar) | DEFAULT(c_string) | DEFAULT(c_datetime) | DEFAULT(c_date) | DEFAULT(c_json) | DEFAULT(c_ipv4) | DEFAULT(c_ipv6) | DEFAULT(c_array_int) | DEFAULT(c_array_string) | DEFAULT(c_map_str_int) | DEFAULT(c_struct) | DEFAULT(c_variant) | | ||
| +-----------------+--------------------+---------------------+----------------+---------------------+-----------------------------------------+------------------+-------------------+---------------------+----------------------------+-------------------+-------------------+-----------------+--------------------+-------------------+---------------------+-----------------+-----------------+-----------------+-----------------+----------------------+-------------------------+------------------------+-------------------+--------------------+ | ||
| | 1 | 7 | 32000 | 2147483647 | 9223372036854775807 | 170141183460469231731687303715884105727 | 3.125 | 2.718281828 | 123456789.123456789 | 99999.1234 | 3.141592653589793 | 2.718281828459045 | charDemo | | plain string | 2025-10-25 11:22:33 | 2025-10-31 | NULL | 192.168.1.1 | 2001:db8::1 | [] | NULL | NULL | NULL | NULL | | ||
| +-----------------+--------------------+---------------------+----------------+---------------------+-----------------------------------------+------------------+-------------------+---------------------+----------------------------+-------------------+-------------------+-----------------+--------------------+-------------------+---------------------+-----------------+-----------------+-----------------+-----------------+----------------------+-------------------------+------------------------+-------------------+--------------------+ | ||
| ``` | ||
|
|
||
| 2. 聚合类型举例 | ||
| ```sql | ||
| CREATE TABLE test_default_agg ( | ||
| k_id INT NOT NULL COMMENT '聚合键', | ||
| bitmap_col BITMAP BITMAP_UNION, | ||
| hll_col HLL HLL_UNION, | ||
| quantile_col QUANTILE_STATE QUANTILE_UNION | ||
| )AGGREGATE KEY(k_id) | ||
| PROPERTIES ( 'replication_num' = '1' ); | ||
|
|
||
| SELECT | ||
| default(bitmap_col),default(hll_col) | ||
| FROM test_default_agg | ||
| LIMIT 1; | ||
| -- ERROR 1105 (HY000): errCode = 2, detailMessage = (127.0.0.1)[INVALID_ARGUMENT]Agg type(HLL, BITMAP, QUANTILE_STATE) cannot be used for the DEFAULT function | ||
| ``` | ||
|
|
||
| 3. 非常量默认值(`CURRENT_TIMESTAMP`, `CURRENT_DATE`)举例: | ||
| ```sql | ||
| CREATE TABLE test_non_const_default_value_t ( | ||
| null_dt DATETIME NULL DEFAULT CURRENT_TIMESTAMP, | ||
| not_null_dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| null_d DATE NULL DEFAULT CURRENT_DATE, | ||
| not_null_d DATE NOT NULL DEFAULT CURRENT_DATE | ||
| ) PROPERTIES ( 'replication_num' = '1' ); | ||
|
|
||
|
|
||
| SELECT | ||
| DEFAULT(null_dt), DEFAULT(not_null_dt), | ||
| DEFAULT(null_d), DEFAULT(not_null_d) | ||
| FROM test_non_const_default_value_t | ||
| LIMIT 1; | ||
| ``` | ||
| ```text | ||
| +------------------+----------------------+-----------------+---------------------+ | ||
| | DEFAULT(null_dt) | DEFAULT(not_null_dt) | DEFAULT(null_d) | DEFAULT(not_null_d) | | ||
| +------------------+----------------------+-----------------+---------------------+ | ||
| | NULL | 0000-01-01 00:00:00 | NULL | 0000-01-01 | | ||
| +------------------+----------------------+-----------------+---------------------+ | ||
| ``` | ||
|
|
||
| 4. 常量值 | ||
| ```sql | ||
| SELECT DEFAULT('hello'); | ||
| -- ERROR 1105 (HY000): errCode = 2, detailMessage = DEFAULT function requires a column reference, not a constant or expression | ||
|
|
||
| SELECT DEFAULT(NULL); | ||
| -- ERROR 1105 (HY000): errCode = 2, detailMessage = DEFAULT function requires a column reference, not a constant or expression | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.