-
Notifications
You must be signed in to change notification settings - Fork 388
[doc](function) Support interval function from mysql #3032
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
dwdwqfwe
wants to merge
2
commits into
apache:master
Choose a base branch
from
dwdwqfwe:support_interval
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.
Open
Changes from 1 commit
Commits
Show all changes
2 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
99 changes: 99 additions & 0 deletions
99
docs/sql-manual/sql-functions/scalar-functions/numeric-functions/interval.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,99 @@ | ||
| --- | ||
| { | ||
| "title": "INTERVAL", | ||
| "language": "en" | ||
| } | ||
| --- | ||
|
|
||
| ## Description | ||
|
|
||
| The INTERVAL function uses binary search to return the index position of the first threshold that is strictly greater than N. The function requires threshold parameters to be arranged in ascending order (N1 ≤ N2 ≤ N3 ≤ ... ≤ Nn) to correctly use the binary search algorithm for optimal performance. | ||
|
|
||
| - All arguments are treated as integer types (TINYINT, SMALLINT, INT, BIGINT, LARGEINT). | ||
| - Uses binary search algorithm with O(log n) time complexity for excellent performance. | ||
| - Returns the count of thresholds if all thresholds are less than or equal to N. | ||
|
|
||
| This function behaves consistently with MySQL's [interval function](https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_interval). | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```sql | ||
| INTERVAL(N, N1, N2, N3, ...) | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Description | | ||
| | -- | -- | | ||
| | `N` | The value to search for, integer type (TINYINT, SMALLINT, INT, BIGINT, LARGEINT) | | ||
| | `N1, N2, N3, ...` | List of thresholds, must be the same type as N, and should be arranged in ascending order (N1 ≤ N2 ≤ N3 ≤ ... ≤ Nn). At least one threshold parameter is required | | ||
|
|
||
| ## Return Value | ||
|
|
||
| Returns an INT32 result: | ||
| - Returns the index position (0-based) of the first threshold that is strictly greater than N. | ||
| - Returns the count of thresholds if all thresholds are less than or equal to N. | ||
| - Returns NULL if N is NULL. | ||
|
|
||
| **Return Rules:** | ||
| - If N < N1, returns 0 | ||
| - If N < N2, returns 1 | ||
| - If N < N3, returns 2 | ||
| - And so on... | ||
| - If all thresholds ≤ N, returns the count of thresholds | ||
|
|
||
| Special cases: | ||
| - Returns NULL if any argument is NULL. | ||
| - If threshold parameters are not arranged in ascending order, the function will still perform binary search but may return incorrect results. | ||
|
|
||
| ## Example | ||
|
|
||
| ```sql | ||
| -- Basic usage | ||
| SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200); | ||
| +-----------------------------------------+ | ||
| | INTERVAL(23, 1, 15, 17, 30, 44, 200) | | ||
| +-----------------------------------------+ | ||
| | 3 | | ||
| +-----------------------------------------+ | ||
|
|
||
| SELECT INTERVAL(10, 1, 10, 100, 1000); | ||
| +-----------------------------------+ | ||
| | INTERVAL(10, 1, 10, 100, 1000) | | ||
| +-----------------------------------+ | ||
| | 2 | | ||
| +-----------------------------------+ | ||
|
|
||
| -- Boundary case: less than first threshold | ||
| SELECT INTERVAL(0, 1, 10, 100); | ||
| +--------------------------+ | ||
| | INTERVAL(0, 1, 10, 100) | | ||
| +--------------------------+ | ||
| | 0 | | ||
| +--------------------------+ | ||
|
|
||
| -- Boundary case: greater than all thresholds | ||
| SELECT INTERVAL(200, 1, 10, 100); | ||
| +----------------------------+ | ||
| | INTERVAL(200, 1, 10, 100) | | ||
| +----------------------------+ | ||
| | 3 | | ||
| +----------------------------+ | ||
|
|
||
| -- Boundary case: equal to a threshold | ||
| SELECT INTERVAL(33, 1, 10, 32, 33, 102, 200); | ||
| +-------------------------------------------+ | ||
| | INTERVAL(33, 1, 10, 32, 33, 102, 200) | | ||
| +-------------------------------------------+ | ||
| | 4 | | ||
| +-------------------------------------------+ | ||
|
|
||
| -- NULL value handling | ||
| SELECT INTERVAL(NULL, 1, 10, 100); | ||
| +------------------------------+ | ||
| | INTERVAL(NULL, 1, 10, 100) | | ||
| +------------------------------+ | ||
| | NULL | | ||
| +------------------------------+ | ||
| ``` | ||
|
|
99 changes: 99 additions & 0 deletions
99
...current/sql-manual/sql-functions/scalar-functions/numeric-functions/interval.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,99 @@ | ||
| --- | ||
| { | ||
| "title": "INTERVAL", | ||
| "language": "zh-CN" | ||
| } | ||
| --- | ||
|
|
||
| ## 描述 | ||
|
|
||
| INTERVAL 函数使用二分查找返回第一个严格大于 N 的阈值位置索引。该函数要求阈值参数按升序排列(N1 ≤ N2 ≤ N3 ≤ ... ≤ Nn)才能正确使用二分查找算法,从而获得最佳性能。 | ||
|
|
||
| - 所有参数都被视为整数类型(TINYINT、SMALLINT、INT、BIGINT、LARGEINT)。 | ||
| - 使用二分查找算法,时间复杂度为 O(log n),性能优异。 | ||
| - 如果所有阈值都小于等于 N,则返回阈值数量。 | ||
|
|
||
| 该函数与 MySQL 中的 [interval 函数](https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_interval) 行为一致。 | ||
|
|
||
| ## 语法 | ||
|
|
||
| ```sql | ||
| INTERVAL(N, N1, N2, N3, ...) | ||
| ``` | ||
|
|
||
| ## 参数 | ||
|
|
||
| | 参数 | 说明 | | ||
| | -- | -- | | ||
| | `N` | 待查找的值,整数类型(TINYINT、SMALLINT、INT、BIGINT、LARGEINT) | | ||
| | `N1, N2, N3, ...` | 阈值列表,必须与 N 的类型相同,且要求按升序排列(N1 ≤ N2 ≤ N3 ≤ ... ≤ Nn)。至少需要一个阈值参数 | | ||
|
|
||
| ## 返回值 | ||
|
|
||
| 返回 INT32 类型的结果: | ||
| - 返回第一个严格大于 N 的阈值位置索引(从 0 开始计数)。 | ||
| - 如果所有阈值都小于等于 N,返回阈值数量。 | ||
| - 如果 N 为 NULL,返回 NULL。 | ||
|
|
||
| **返回规则:** | ||
| - 如果 N < N1,返回 0 | ||
| - 如果 N < N2,返回 1 | ||
| - 如果 N < N3,返回 2 | ||
| - 以此类推... | ||
| - 如果所有阈值都 ≤ N,返回阈值数量 | ||
|
|
||
| 特殊情况: | ||
| - 任何参数为 NULL 时,返回 NULL。 | ||
| - 如果阈值参数未按升序排列,函数仍会执行二分查找,但可能返回不正确的结果。 | ||
|
|
||
| ## 举例 | ||
|
|
||
| ```sql | ||
| -- 基本用法 | ||
| SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200); | ||
| +-----------------------------------------+ | ||
| | INTERVAL(23, 1, 15, 17, 30, 44, 200) | | ||
| +-----------------------------------------+ | ||
| | 3 | | ||
| +-----------------------------------------+ | ||
|
|
||
| SELECT INTERVAL(10, 1, 10, 100, 1000); | ||
| +-----------------------------------+ | ||
| | INTERVAL(10, 1, 10, 100, 1000) | | ||
| +-----------------------------------+ | ||
| | 2 | | ||
| +-----------------------------------+ | ||
|
|
||
| -- 边界情况:小于第一个阈值 | ||
| SELECT INTERVAL(0, 1, 10, 100); | ||
| +--------------------------+ | ||
| | INTERVAL(0, 1, 10, 100) | | ||
| +--------------------------+ | ||
| | 0 | | ||
| +--------------------------+ | ||
|
|
||
| -- 边界情况:大于所有阈值 | ||
| SELECT INTERVAL(200, 1, 10, 100); | ||
| +----------------------------+ | ||
| | INTERVAL(200, 1, 10, 100) | | ||
| +----------------------------+ | ||
| | 3 | | ||
| +----------------------------+ | ||
|
|
||
| -- 边界情况:等于某个阈值 | ||
| SELECT INTERVAL(33, 1, 10, 32, 33, 102, 200); | ||
| +-------------------------------------------+ | ||
| | INTERVAL(33, 1, 10, 32, 33, 102, 200) | | ||
| +-------------------------------------------+ | ||
| | 4 | | ||
| +-------------------------------------------+ | ||
|
|
||
| -- NULL 值处理 | ||
| SELECT INTERVAL(NULL, 1, 10, 100); | ||
| +------------------------------+ | ||
| | INTERVAL(NULL, 1, 10, 100) | | ||
| +------------------------------+ | ||
| | NULL | | ||
| +------------------------------+ | ||
| ``` | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if only one input?