时间谓语 - Amazon Kinesis Data Analytics

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

时间谓语

下表显示了标准 SQL 支持的时态谓词以及 Amazon Kinesis Data Analytics 支持的 SQL 标准扩展的图形表示形式。它显示了每个谓词所涵盖的关系。每个关系均表示为时间间隔上限和下限,并具有组合意义 upperInterval predicate lowerInterval evaluates to TRUE。前 7 个谓词是标准 SQL。最后 10 个谓词以粗体显示,是 Amazon Kinesis Data Analytics SQL 标准的扩展。

谓词 涵盖的关系

CONTAINS

Blue rectangular shapes arranged in horizontal rows, resembling a simplified layout or structure.

OVERLAPS

Blue rectangular boxes arranged in rows, representing a structured layout or diagram.

EQUALS

Two horizontal blue rectangles with orange borders, stacked vertically.

先于

Four blue rectangular shapes representing placeholder text or content blocks.

继任

Four blue rectangular shapes arranged horizontally with gaps between them.

立即先于

Blue rectangular shapes representing text or content placeholders.

立即继任

Two horizontal blue rectangular shapes against a white background.

LEADS

Four blue rectangular buttons with orange outlines, arranged horizontally.

LAGS

Four blue rectangular bars of varying lengths arranged horizontally.

STRICTLY CONTAINS

Two blue rectangular shapes with orange outlines, one larger above a smaller one.

STRICTLY OVERLAPS

Two blue rectangular shapes, one longer than the other, stacked vertically.

STRICTLY PRECEDES

Two horizontal blue bars representing placeholder elements in a user interface.

STRICTLY SUCCEEDS

Two blue rectangular shapes representing UI elements or buttons.

STRICTLY LEADS

Two blue rectangular shapes representing text or content blocks.

STRICTLY LAGS

Two horizontal blue rectangular shapes, one above the other, against a white background.

IMMEDIATELY LEADS

Two blue rectangular shapes, one longer than the other, stacked vertically.

IMMEDIATELY LAGS

Two blue rectangular shapes, one longer than the other, stacked vertically.

为了启用简洁的表达式,Amazon Kinesis Data Analytics 还支持以下扩展:

  • 可选的 PERIOD 关键字 — 可以省略 PERIOD 关键字。

  • 紧凑链接 — 如果其中两个谓词背靠背出现,用 AND 分隔,则可以省略 AND,前提是第一个谓词的右边间隔与第二个谓词的左间隔相同。

  • TSDIFF — 此函数采用两个 TIMESTAMP 参数并以毫秒为单位返回它们的差值。

例如,您可以输入以下表达式:

 PERIOD (s1,e1) PRECEDES PERIOD(s2,e2)  AND PERIOD(s2, e2) PRECEDES PERIOD(s3,e3)

更简洁地说,如下所示:

(s1,e1) PRECEDES (s2,e2) PRECEDES PERIOD(s3,e3)

以下简洁表达式:

TSDIFF(s,e)

意思如下:

CAST((e - s) SECOND(10, 3) * 1000 AS BIGINT)

最后,标准 SQL 允许 CONTAINS 谓词使用单个 TIMESTAMP 作为其右侧参数。例如,以下表达式:

PERIOD(s, e) CONTAINS t

等效于以下筛选条件:

s <= t AND t < e

语法

时间谓词已集成到一个新的布尔值表达式中:

<period-expression> :=  <left-period> <half-period-predicate> <right-period> <half-period-predicate> :=  <period-predicate> [ <left-period> <half-period-predicate> ] <period-predicate> :=   EQUALS | [ STRICTLY ] CONTAINS | [ STRICTLY ] OVERLAPS | [ STRICTLY | IMMEDIATELY ] PRECEDES | [ STRICTLY | IMMEDIATELY ] SUCCEEDS | [ STRICTLY | IMMEDIATELY ] LEADS | [ STRICTLY | IMMEDIATELY ] LAGS <left-period> := <bounded-period> <right-period> := <bounded-period> | <timestamp-expression> <bounded-period> := [ PERIOD ] ( <start-time>, <end-time> ) <start-time> := <timestamp-expression> <end-time> := <timestamp-expression> <timestamp-expression> :=  an expression which evaluates to a TIMESTAMP value where <right-period> may evaluate to a <timestamp-expression> only if the immediately preceding <period-predicate> is [ STRICTLY ] CONTAINS

以下内置函数支持此布尔表达式:

BIGINT tsdiff( startTime TIMESTAMP, endTime TIMESTAMP )

以毫秒为单位返回 (endTime-StartTime) 的值。

示例

以下示例代码记录了如果在空调开启时窗户处于打开状态,则会发出警报:

create or replace pump alarmPump stopped as  insert into alarmStream( houseID, roomID, alarmTime, alarmMessage ) select stream w.houseID, w.roomID, current_timestamp,                   'Window open while air conditioner is on.' from    windowIsOpenEvents over (range interval '1' minute preceding) w join    acIsOnEvents over (range interval '1' minute preceding) h on w.houseID = h.houseID where (h.startTime, h.endTime) overlaps (w.startTime, w.endTime);

使用案例示例

当两个人尝试在两个不同的位置同时使用同一张信用卡时,以下查询使用时间谓词发出欺诈警报:

create pump creditCardFraudPump stopped as insert into alarmStream  select stream    current_timestamp, creditCardNumber, registerID1, registerID2  from transactionsPerCreditCard  where registerID1 <> registerID2  and (startTime1, endTime1) overlaps (startTime2, endTime2) ;

前面的代码示例使用具有以下数据集的输入流:

(current_timestamp TIMESTAMP, creditCardNumber VARCHAR(16), registerID1 VARCHAR(16), registerID2 VARCHAR(16), startTime1 TIMESTAMP, endTime1 TIMESTAMP, startTime2 TIMESTAMP, endTime2 TIMESTAMP)