Skip to content

Conversation

dwdwqfwe
Copy link
Contributor

@dwdwqfwe dwdwqfwe commented Aug 29, 2025

Versions

  • dev
  • 3.0
  • 2.1
  • 2.0

Languages

  • Chinese
  • English

Docs Checklist

  • Checked by AI
  • Test Cases Built

@dwdwqfwe dwdwqfwe force-pushed the date_function_sec_to_time branch from ce5f7e0 to 1608949 Compare September 11, 2025 13:17
函数将输入的 `TIME``DATETIME` 类型的值转换为以秒为单位的总时间。如果输入为 `DATETIME` 类型,函数会自动提取时间部分(`HH:MM:SS`)。
TIME_TO_SEC 函数用于将输入的时间值转换为以秒为单位的总秒数。该函数支持处理 TIME、DATETIME 类型:若输入为 DATETIME 类型,会自动提取其中的时间部分(HH:MM:SS)进行计算;若输入为纯时间值,则直接转换为总秒数。

该函数与 mysql 中的 [time_to_sec 函数](https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_time-to-sec) 使用 SECOND 为单位的行为一致
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“使用 SECOND 为单位的行为一致”多余的,看看还有没有别的复制粘贴出问题的

- 如果 `<end_datetime>``<start_datetime>``NULL`,函数返回 `NULL`
- 如果 `<end_datetime>``<start_datetime>` 为非法时间或日期时间值(如 `0000-00-00 00:00:00`),函数返回 `NULL`
- 当返回时间差不为整数秒时,返回时间带有 scale.
- 当计算机结果超出 time 范围[-838:59:59,828:59:59],返回错误
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

计算结果。另外范围上界也不对吧

## 描述

TIMESTAMP 函数有两种用法:
TIMESTAMP 将 datetime 字符串转换为 DATETIME 类型
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

符合datetime格式的字符串

| `string` | 日期时间字符串 |
| `date` | 日期值,可以是 DATE 类型或格式正确的日期字符串 |
| `time` | 时间值,可以是 TIME 类型或格式正确的时间字符串 |
| `string` | 日期时间字符串或者 datetime 类型 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

描述错误

- 返回一个 TIME 类型值,格式为 `HH:MM:SS`,表示从起点时间(00:00:00)开始计算的时间。
- 如果输入的 `<seconds>` 为 NULL,函数返回 NULL。
返回一个秒数转换为 TIME 类型的值
- 若输入秒数超出 TIME 类型的有效范围(-838:59:59 至 838:59:59,对应秒数范围 -3023999 至 3023999),返回错误;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sec_to_time超出范围后不报错。

日期时间匹配方式,用两根指针指向两字符串起始位置
1. 当遇格式字符串到 % 符号时,会根据 %下一个字母匹配时间字符对应的时间部分,若不匹配(如 %Y 匹配日期时间部分却为 10:10:10 或者 % 不支持解析的字符如 %*),则返回 NULL,匹配成功则移动到下一个字符解析。
2. 任意时刻两串中的任一个遇到空格字符,直接跳过解析下一个字符串
3. 当遇到普通字母的匹配,则查看两字符串现在指针所指向的字符是否相等,不相等则返回 NULL,相等则解析下一个字符
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

报错,等待行为变更

3. 当遇到普通字母的匹配,则查看两字符串现在指针所指向的字符是否相等,不相等则返回 NULL,相等则解析下一个字符
4. 当任日期指针指向字符串末尾时,若日期时间只包含日期部分,则格式字符串会检查是否包含匹配时间部分的字符(如 %H),若包含,则会设置时间部分为 00:00:00。
5. 当格式字符串指向末尾时,匹配结束。
6. 最后检查匹配时间部分是否合法(如月份必须在 [1,12] 区间内),如果不合法,则返回 NULL,合法则返回解析出的日期时间
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

6. 最后检查匹配时间部分是否合法(如月份必须在 [1,12] 区间内),如果不合法,则返回 NULL,合法则返回解析出的日期时间


- 若 <datetime_str> 与 <format> 格式不匹配(如字符串为 2023/13/01 而格式为 %Y-%m-%d),返回 NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这条在上面描述了吧

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等待行为变更

- 若 <datetime_str> 与 <format> 格式不匹配(如字符串为 2023/13/01 而格式为 %Y-%m-%d),返回 NULL;
- 若任一参数为 NULL,返回 NULL;
- 若 <format> 为空字符串,返回 NULL;
- 若 <datetime_str> 缺少时间部分(仅日期),解析后时间部分默认为 00:00:00;若缺少日期部分(仅时间),日期部分默认为 0000-00-00(无效日期,返回 NULL)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

报错,等待行为变更,不需要描述默认为多少了

+---------------------+

-- 仅时间字符串(日期无效,返回空字符串)
SELECT STR_TO_DATE('15:30:45', '%H:%i:%s') AS result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们现在是这个行为吗?这个应该有bug的

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

得记录个jira


特殊情况:
- 如果任何参数为 NULL,返回 NULL。
- 当输入的时区不合法的时候,返回NULL。 时区的设置参考 [时区管理](../../../../admin-manual/cluster-management/time-zone)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不合法的时候已经是报错了

Copy link
Contributor

@zclllyybb zclllyybb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整个看一下

@dwdwqfwe dwdwqfwe force-pushed the date_function_sec_to_time branch from bc7e8ba to 16b75ec Compare September 25, 2025 04:39
@dwdwqfwe dwdwqfwe force-pushed the date_function_sec_to_time branch from 16b75ec to ac6f98b Compare September 25, 2025 04:42
+--------+

--- 包含 scale 的时间,会把小数部分差距算入
SELECT SECONDS_DIFF('2023-07-13 12:00:00.123', '2023-07-13 11:59:59') AS result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个例子不恰当,本来不带小数也是1秒

@zclllyybb zclllyybb merged commit 2bc450a into apache:master Sep 25, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants