为什么 sys.fn_xe_file_target_read_file 需要对 datetime2 列进行显式转换?

Zik*_*ato 11 sql-server extended-events sql-server-2022

根据文档,返回的列timestamp_utc应为datetime2(7)类型

但是当我这样查询时

SELECT 
    *
FROM sys.fn_xe_file_target_read_file('system_health*.xel', null, null, null)
WHERE timestamp_utc > dateadd(hour, -1, GETUTCDATE())
Run Code Online (Sandbox Code Playgroud)

它不返回任何行。仅当我向datetime2添加显式转换时,它才返回行

SELECT 
    *
FROM sys.fn_xe_file_target_read_file('system_health*.xel', null, null, null)
WHERE cast(timestamp_utc as datetime2(7)) > dateadd(hour, -1, GETUTCDATE())
Run Code Online (Sandbox Code Playgroud)

这与文档中的最后一个示例匹配(即使没有引起注意)

在此输入图像描述

这是为什么?

Pau*_*ite 13

Erland Sommarskog 在 2022 年 5 月向我询问过这个问题。

这是当谓词被推入内部流表值函数时暴露的错误。返回类型确实是datetime2(7),但源是 Windows FILETIME

谓词被下推到内部转换下方,因此服务器最终会尝试将 adatetime2与 a进行比较FILETIME,但这不起作用。

谓词下推
谓词下推

当您添加显式转换时,谓词不能再被推送到 TVF 中,因此它显示为单独的 Filter:

独立过滤器
在单独的过滤器中评估谓词

我不知道为什么这个问题没有得到解决。

Erland 随后创建了一个反馈项:Filtering output from fn_xe_file_target_read_file on timestamp_utc returns no rows