risingwavelabs / risingwave

Best-in-class stream processing, analytics, and management. Perform continuous analytics, or build event-driven applications, real-time ETL pipelines, and feature stores in minutes. Unified streaming and batch. PostgreSQL compatible.
https://go.risingwave.com/slack
Apache License 2.0
7.06k stars 580 forks source link

feat: support arbitrary expression as time column of time window function #6641

Open xiangjinwu opened 1 year ago

xiangjinwu commented 1 year ago

Describe the bug

In a time window function like tumble or hop:

TUMBLE(table_or_source, start_time, window_size)
HOP(table_or_source, start_time, hop_size, window_size)

The current implementation requires start_time to be a column name rather than arbitrary expression. But the error message is misleading.

To Reproduce

CREATE TABLE t (
    tag VARCHAR,
    value INT,
    ms BIGINT
);

CREATE MATERIALIZED VIEW mv1 AS
SELECT
    window_start,
    tag,
    SUM(value) as total
FROM
    TUMBLE(
        t,
        to_timestamp(ms / 1e3),
        INTERVAL '10' MINUTE
    )
GROUP BY
    window_start,
    tag;

Current error message is:

ERROR:  QueryError: Bind error: the 2st arg of window table function should be a timestamp with time zone, timestamp or date column

Expected behavior

Pick one of the following:

https://github.com/risingwavelabs/risingwave/blob/a061017590a291d2adf1531507fdc4d27d2ad44f/src/frontend/src/binder/relation/window_table_function.rs#L82-L92

Additional context

Note that we do have strict requirement of the 1st and 3rd argument:

Current workaround is to use with-cte syntax:

CREATE MATERIALIZED VIEW mv1 AS
WITH t AS (SELECT
    tag, value,
    to_timestamp(ms / 1e3) AS ts
FROM t)
SELECT
    window_start,
    tag,
    SUM(value) as total
FROM
    TUMBLE(
        t,
        ts,
        INTERVAL '10' MINUTE
    )
GROUP BY
    window_start,
    tag;
hzxa21 commented 1 year ago

table_or_source is required to be a simple name (table, source, mv, cte, view) rather than an arbitrary relation (subquery, join, table function).

I encounter a similar issue recently and I just workaround it via CREATE VIEW

fuyufjh commented 1 year ago

Is it done?

xiangjinwu commented 1 year ago

Current status: The error message has been fixed. It guides the user to use CTE or view as workaround.

To improve user experience, we may support arbitrary expression at this place i.e. provide a syntax sugar rather than writing CTE. So keeping this issue open but at a later release.