Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
+------------------------------+
```

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);
Copy link
Contributor

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?

+-----------------------------------------+
| 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 |
+------------------------------+
```

Loading