Datasets:
File size: 76,410 Bytes
2668faf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | {"task_id": "flinksql_001_en", "id": "online-compute_FlinkSQL_flinksql_001", "name": "Order Stream and Payment Stream Interval Join", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses the built-in datagen connector to simulate an order stream and a payment stream, performing an Interval Join on `order_id`, and writing matched records directly to the console.\n\n**Business Background and Objective**: Simulate an order stream and a payment stream using two datagen built-in tables, each generating 100 million records. The `order_id` and `user_id` in the order stream range from 1 to 1,000,000, and the `order_id` in the payment stream also ranges from 1 to 1,000,000. Both tables use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay.\n\nPerform an Interval Join on the two tables using `order_id`, matching only records where the payment time falls within 5 minutes after the order time. Upon a successful match, output the original fields directly to the console table, including `order_id`, `user_id`, `pay_amount`, the order time, and the payment time.\n\n**Source Table Definitions**:\n- `orders_source` (order stream, datagen connector):\n - `order_id INT`: Order ID, randomly generated in the range 1–1,000,000\n - `user_id INT`: User ID, randomly generated in the range 1–1,000,000\n - `order_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 1000`\n\n- `payments_source` (payment stream, datagen connector):\n - `order_id INT`: Order ID, randomly generated in the range 1–1,000,000\n - `pay_amount DOUBLE`: Payment amount, randomly generated in the range 1–100,000\n - `pay_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 1000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `order_id INT`\n - `user_id INT`\n - `pay_amount DOUBLE`\n - `order_time TIMESTAMP(3)`\n - `pay_time TIMESTAMP(3)`\n\n**Join Logic**:\n- Join condition: `orders_source.order_id = payments_source.order_id`\n- Time window: `payments_source.pay_time BETWEEN orders_source.order_time AND orders_source.order_time + INTERVAL '5' MINUTE`\n- Note: When writing to the sink, you must handle the dual rowtime attribute conflict by applying `CAST(pay_time AS TIMESTAMP(3))` to downgrade `pay_time` to a plain timestamp.\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the matched results.\n- Output field order: `order_id`, `user_id`, `pay_amount`, `order_time`, `pay_time`", "ground_truth": "-- online_001: 订单流 + 支付流 Interval Join\n-- 每个源生成 1 亿条数据 @ 1000 rows/sec\n\n-- 1. 订单流 datagen 源表\nCREATE TABLE orders_source (\n order_id INT,\n user_id INT,\n order_time AS LOCALTIMESTAMP,\n WATERMARK FOR order_time AS order_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '1000000'\n);\n\n-- 2. 支付流 datagen 源表\nCREATE TABLE payments_source (\n order_id INT,\n pay_amount DOUBLE,\n pay_time AS LOCALTIMESTAMP,\n WATERMARK FOR pay_time AS pay_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.pay_amount.kind' = 'random',\n 'fields.pay_amount.min' = '1',\n 'fields.pay_amount.max' = '100000'\n);\n\n-- 3. Console 输出表\nCREATE TABLE console_output (\n order_id INT,\n user_id INT,\n pay_amount DOUBLE,\n order_time TIMESTAMP(3),\n pay_time TIMESTAMP(3)\n) WITH (\n 'connector' = 'print'\n);\n\n-- 4. Interval Join: 匹配支付时间在订单时间后 5 分钟内的记录\nINSERT INTO console_output\nSELECT\n o.order_id,\n o.user_id,\n p.pay_amount,\n o.order_time,\n CAST(p.pay_time AS TIMESTAMP(3)) AS pay_time\nFROM orders_source o\nJOIN payments_source p\n ON o.order_id = p.order_id\nWHERE\n p.pay_time BETWEEN o.order_time AND o.order_time + INTERVAL '5' MINUTE;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_001_en"}
{"task_id": "flinksql_002_en", "id": "online-compute_FlinkSQL_flinksql_002", "name": "Order-Payment Interval Join + Windowed Aggregation", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses two datagen built-in tables to simulate an order stream and a payment stream, performs an Interval Join on `order_id`, and then aggregates the order count and total payment amount over a 10-minute tumbling window, outputting the results to the console.\n\n**Business Background and Objective**: Simulate an order stream and a payment stream using two datagen built-in tables, each generating 100 million records. The `order_id` and `user_id` in the order stream range from 1 to 1,000,000, and the `order_id` in the payment stream also ranges from 1 to 1,000,000. Both tables use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay.\n\nPerform an Interval Join on the two tables using `order_id`, matching only records where the payment time falls within 10 minutes before or after the order time. Then, aggregate the successfully matched records over a 10-minute tumbling window to compute the order count and total payment amount per window, and finally output the results to the console table.\n\n**Source Table Definitions**:\n- `orders_source` (order stream, datagen connector):\n - `order_id INT`: Order ID, randomly generated in the range 1–1,000,000\n - `user_id INT`: User ID, randomly generated in the range 1–1,000,000\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 1000`\n\n- `payments_source` (payment stream, datagen connector):\n - `order_id INT`: Order ID, randomly generated in the range 1–1,000,000\n - `pay_amount DOUBLE`: Payment amount, randomly generated in the range 1–100,000\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 1000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `window_start VARCHAR`: Window start time\n - `window_end VARCHAR`: Window end time\n - `order_count BIGINT`: Order count within the window\n - `total_pay_amount DOUBLE`: Total payment amount within the window\n\n**Join + Aggregation Logic**:\n- Interval Join condition: `orders_source.order_id = payments_source.order_id`\n- Time window: `payments_source.event_time BETWEEN orders_source.event_time - INTERVAL '10' MINUTE AND orders_source.event_time + INTERVAL '10' MINUTE`\n- Tumbling window: `TUMBLE` 10 minutes\n- Aggregation metrics: `COUNT(*) AS order_count`, `SUM(pay_amount) AS total_pay_amount`\n- Convert window times to `VARCHAR` for output\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `window_start`, `window_end`, `order_count`, `total_pay_amount`", "ground_truth": "-- online_002: 订单支付 Interval Join + Tumble 窗口聚合\n-- 每个源生成 1 亿条数据 @ 1000 rows/sec\n\n-- 1. 订单流 datagen 源表\nCREATE TABLE orders_source (\n order_id INT,\n user_id INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '1000000'\n);\n\n-- 2. 支付流 datagen 源表\nCREATE TABLE payments_source (\n order_id INT,\n pay_amount DOUBLE,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.pay_amount.kind' = 'random',\n 'fields.pay_amount.min' = '1',\n 'fields.pay_amount.max' = '100000'\n);\n\n-- 3. Console 输出表\nCREATE TABLE console_output (\n window_start VARCHAR,\n window_end VARCHAR,\n order_count BIGINT,\n total_pay_amount DOUBLE\n) WITH (\n 'connector' = 'print'\n);\n\n-- 4. Interval Join + Tumble 10min 窗口聚合\nINSERT INTO console_output\nSELECT\n CAST(TUMBLE_START(o.event_time, INTERVAL '10' MINUTE) AS VARCHAR) AS window_start,\n CAST(TUMBLE_END(o.event_time, INTERVAL '10' MINUTE) AS VARCHAR) AS window_end,\n COUNT(*) AS order_count,\n SUM(p.pay_amount) AS total_pay_amount\nFROM orders_source o\nJOIN payments_source p\n ON o.order_id = p.order_id\n AND p.event_time BETWEEN o.event_time - INTERVAL '10' MINUTE\n AND o.event_time + INTERVAL '10' MINUTE\nGROUP BY TUMBLE(o.event_time, INTERVAL '10' MINUTE);", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_002_en"}
{"task_id": "flinksql_003_en", "id": "online-compute_FlinkSQL_flinksql_003", "name": "Word Frequency Tumbling Window Statistics", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses a datagen built-in table to simulate a word frequency data stream, groups by `word` and opens a 1-minute tumbling window to count the occurrences of each word, and outputs the results to the console.\n\n**Business Background and Objective**: Simulate a word frequency data stream using a datagen built-in table, generating 1 billion records in the `word` field. Use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay. Group by `word` and count the occurrences of each word over a 1-minute tumbling window, then output `word` and `cnt` to the console table.\n\n**Source Table Definition**:\n- `word_source` (word frequency stream, datagen connector):\n - `word VARCHAR`: Word, randomly generated (value range 1–1000 to simulate distinct words)\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 1000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `word VARCHAR`: Word\n - `cnt BIGINT`: Number of occurrences of the word within the current window\n\n**Aggregation Logic**:\n- Group by `word`, using a 1-minute tumbling window\n- Aggregation metric: `COUNT(*) AS cnt`\n- The window is defined by the `TUMBLE` function\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `word`, `cnt`", "ground_truth": "-- online_003: 词频滚动窗口统计\n-- 单源生成 10 亿条数据 @ 1000 rows/sec\n\n-- 1. 词频流 datagen 源表\nCREATE TABLE word_source (\n word VARCHAR,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.word.kind' = 'random',\n 'fields.word.length' = '5'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n word VARCHAR,\n cnt BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. Tumble 1min 窗口词频统计\nINSERT INTO console_output\nSELECT\n word,\n COUNT(*) AS cnt\nFROM word_source\nGROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), word;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_003_en"}
{"task_id": "flinksql_004_en", "id": "online-compute_FlinkSQL_flinksql_004", "name": "Dual Window Aggregation (Tumble + Hop)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses a datagen built-in table to generate order events, and computes the sales volume and order count per product using both a tumbling window and a sliding window, outputting the results to two separate console tables.\n\n**Business Background and Objective**: Generate order events (`order_id BIGINT`, `product_id INT`, `quantity INT`) using a datagen built-in table. Use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay.\n\nCompute statistics using two window types:\n1. **1-minute tumbling window**: Compute the sales volume `SUM(quantity) AS sales` and order count `COUNT(*) AS order_cnt` per product.\n2. **Sliding window** (slides every 30 seconds, covering the past 1 minute): Compute the sales volume `SUM(quantity) AS sales` and order count `COUNT(*) AS order_cnt` per product.\n\nPrint the results of both window types to two separate console tables, with each record carrying a window type identifier `window_type`.\n\n**Source Table Definition**:\n- `orders_source` (order event stream, datagen connector):\n - `order_id BIGINT`: Order ID, randomly generated in the range 1–1,000,000\n - `product_id INT`: Product ID, randomly generated in the range 1–10,000\n - `quantity INT`: Quantity, randomly generated in the range 1–10\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 50`\n\n**Output Table Definitions**:\n- `console_tumble` (print connector, tumbling window results):\n - `window_type STRING`: Window type identifier, fixed as `'TUMBLE'`\n - `window_start TIMESTAMP(3)`: Window start time\n - `window_end TIMESTAMP(3)`: Window end time\n - `product_id INT`: Product ID\n - `sales INT`: Sales volume of the product within the window\n - `order_cnt BIGINT`: Order count of the product within the window\n\n- `console_hop` (print connector, sliding window results):\n - `window_type STRING`: Window type identifier, fixed as `'HOP'`\n - `window_start TIMESTAMP(3)`: Window start time\n - `window_end TIMESTAMP(3)`: Window end time\n - `product_id INT`: Product ID\n - `sales INT`: Sales volume of the product within the window\n - `order_cnt BIGINT`: Order count of the product within the window\n\n**Aggregation Logic**:\n- Tumbling window: `GROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), product_id`\n- Sliding window: `GROUP BY HOP(event_time, INTERVAL '30' SECOND, INTERVAL '1' MINUTE), product_id`\n- Aggregation metrics: `SUM(quantity) AS sales`, `COUNT(*) AS order_cnt`\n- Both window times and the window type must be explicitly output\n\n**Output Requirements**:\n- Use two `INSERT INTO` statements to output to `console_tumble` and `console_hop` respectively.\n- Output field order: `window_type`, `window_start`, `window_end`, `product_id`, `sales`, `order_cnt`", "ground_truth": "-- online_004: 双窗口聚合 (Tumble + Hop)\n-- 单源生成 @ 50 rows/sec\n\n-- 1. 订单事件 datagen 源表\nCREATE TABLE orders_source (\n order_id BIGINT,\n product_id INT,\n quantity INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '50',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.product_id.kind' = 'random',\n 'fields.product_id.min' = '1',\n 'fields.product_id.max' = '10000',\n 'fields.quantity.kind' = 'random',\n 'fields.quantity.min' = '1',\n 'fields.quantity.max' = '10'\n);\n\n-- 2. Console 输出表 (Tumble 滚动窗口)\nCREATE TABLE console_tumble (\n window_type STRING,\n window_start TIMESTAMP(3),\n window_end TIMESTAMP(3),\n product_id INT,\n sales INT,\n order_cnt BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. Console 输出表 (Hop 滑动窗口)\nCREATE TABLE console_hop (\n window_type STRING,\n window_start TIMESTAMP(3),\n window_end TIMESTAMP(3),\n product_id INT,\n sales INT,\n order_cnt BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 4a. Tumble 1min 滚动窗口:SUM 销量 + COUNT 订单数\nINSERT INTO console_tumble\nSELECT\n 'TUMBLE' AS window_type,\n TUMBLE_START(event_time, INTERVAL '1' MINUTE) AS window_start,\n TUMBLE_END(event_time, INTERVAL '1' MINUTE) AS window_end,\n product_id,\n CAST(SUM(quantity) AS INT) AS sales,\n COUNT(*) AS order_cnt\nFROM orders_source\nGROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), product_id;\n\n-- 4b. Hop 滑动窗口 (30s slide, 1min size):SUM 销量 + COUNT 订单数\nINSERT INTO console_hop\nSELECT\n 'HOP' AS window_type,\n HOP_START(event_time, INTERVAL '30' SECOND, INTERVAL '1' MINUTE) AS window_start,\n HOP_END(event_time, INTERVAL '30' SECOND, INTERVAL '1' MINUTE) AS window_end,\n product_id,\n CAST(SUM(quantity) AS INT) AS sales,\n COUNT(*) AS order_cnt\nFROM orders_source\nGROUP BY HOP(event_time, INTERVAL '30' SECOND, INTERVAL '1' MINUTE), product_id;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_004_en"}
{"task_id": "flinksql_005", "id": "online-compute_FlinkSQL_flinksql_005", "name": "滚动窗口 TopN 商品排行", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置表模拟电商订单,按 10 秒滚动窗口做 Top-N 商品排行,输出销售额最高的前 3 个商品到 console。\n\n**业务背景与目标**:用 datagen 内置表生成电商订单(字段 order_id / product_id / amount),每秒生成约 10 条。以 LOCALTIMESTAMP 作为事件时间,设置 5 秒的 Watermark 延迟。\n\n按 10 秒滚动窗口做 Top-N 排行:每个窗口内统计每个商品的销售额 SUM(amount) 和订单数 COUNT(*),按销售额降序取前三名,打印到 console。\n\n**源表定义**:\n- `orders_source`(订单流,datagen 连接器):\n - order_id BIGINT:订单 ID,随机取值 1~1000000\n - product_id INT:商品 ID,随机取值 1~1000\n - amount DOUBLE:订单金额,随机取值 1~10000\n - event_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 5 秒\n - 生成速率:rows-per-second = 10\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start TIMESTAMP(3):窗口起始时间\n - product_id INT:商品 ID\n - sales DOUBLE:该窗口内销售额\n - order_cnt BIGINT:该窗口内订单数\n - rn BIGINT:排名序号\n\n**TopN 逻辑**:\n- 使用 10 秒滚动窗口聚合:SUM(amount) AS sales, COUNT(*) AS order_cnt\n- 在窗口内使用 ROW_NUMBER() 按销售额降序排名\n- 只输出排名前 3 的记录(rn <= 3)\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将 Top 3 结果输出\n- 输出字段顺序:window_start, product_id, sales, order_cnt, rn", "ground_truth": "-- online_005: 滚动窗口 TopN 商品排行\n-- 单源生成 @ 10 rows/sec, Tumble 10s + ROW_NUMBER Top3\n\n-- 1. 订单流 datagen 源表\nCREATE TABLE orders_source (\n order_id BIGINT,\n product_id INT,\n amount DOUBLE,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '10',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.product_id.kind' = 'random',\n 'fields.product_id.min' = '1',\n 'fields.product_id.max' = '1000',\n 'fields.amount.kind' = 'random',\n 'fields.amount.min' = '1',\n 'fields.amount.max' = '10000'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP(3),\n product_id INT,\n sales DOUBLE,\n order_cnt BIGINT,\n rn BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. Tumble 10s 窗口聚合 + ROW_NUMBER Top 3\nINSERT INTO console_output\nSELECT\n window_start,\n product_id,\n sales,\n order_cnt,\n rn\nFROM (\n SELECT\n TUMBLE_START(event_time, INTERVAL '10' SECOND) AS window_start,\n product_id,\n SUM(amount) AS sales,\n COUNT(*) AS order_cnt,\n ROW_NUMBER() OVER (\n PARTITION BY TUMBLE_START(event_time, INTERVAL '10' SECOND)\n ORDER BY SUM(amount) DESC\n ) AS rn\n FROM orders_source\n GROUP BY TUMBLE(event_time, INTERVAL '10' SECOND), product_id\n)\nWHERE rn <= 3;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_005"}
{"task_id": "flinksql_006", "id": "online-compute_FlinkSQL_flinksql_006", "name": "异常事件过滤 + 窗口 TopN", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置表造设备事件流,过滤出异常事件后按省份做窗口 TopN 排名,输出到 console。\n\n**业务背景与目标**:用内置 datagen 表生成设备事件数据,字段包括 device_id(int)、event_type(int)、province(string)、ts(timestamp),每秒 80 条,event_type 取值 1-10,province 长度 4 字符,用 ts 字段作为事件时间开 1 分钟滚动窗口(watermark 延迟 5 秒),先过滤 event_type=3 的异常事件,然后按省份分组统计每分钟的异常事件数,按异常数降序排名取 Top-5,输出字段包括窗口开始时间、省份、异常事件数、排名,打印到 console。\n\n**源表定义**:\n- `device_source`(设备事件流,datagen 连接器):\n - device_id INT:设备 ID,随机生成\n - event_type INT:事件类型,随机取值 1~10\n - province VARCHAR:省份,随机生成,长度 4 字符\n - event_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 5 秒\n - 生成速率:rows-per-second = 80\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start TIMESTAMP:窗口开始时间\n - province VARCHAR:省份\n - abnormal_cnt BIGINT:异常事件数\n - `rank` BIGINT:排名(注意 rank 是关键字,需要用反引号包裹)\n\n**查询逻辑**:\n- 过滤:WHERE event_type = 3(异常事件)\n- 窗口:TUMBLE(event_time, INTERVAL '1' MINUTE) 滚动窗口\n- 聚合:按 province 和窗口分组,COUNT(*) 统计异常事件数\n- 排名:ROW_NUMBER() OVER (PARTITION BY window_start ORDER BY abnormal_cnt DESC) AS `rank`\n- Top-N:WHERE `rank` <= 5\n\n**输出要求**:\n- 使用 INSERT INTO console_output 输出结果\n- 输出字段顺序:window_start, province, abnormal_cnt, `rank`", "ground_truth": "-- flinksql_006: 异常事件过滤 + 窗口 TopN\n-- datagen 生成设备事件流 @ 80 rows/sec, event_type=3 过滤后 Tumble 1min Top-5\n\n-- 1. 设备事件流 datagen 源表\nCREATE TABLE device_source (\n device_id INT,\n event_type INT,\n province VARCHAR,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '80',\n 'fields.event_type.kind' = 'random',\n 'fields.event_type.min' = '1',\n 'fields.event_type.max' = '10',\n 'fields.province.kind' = 'random',\n 'fields.province.length' = '4'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP,\n province VARCHAR,\n abnormal_cnt BIGINT,\n `rank` BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. 过滤异常事件(event_type=3) → Tumble 窗口 → Top-5\nINSERT INTO console_output\nSELECT\n window_start,\n province,\n abnormal_cnt,\n `rank`\nFROM (\n SELECT\n TUMBLE_START(event_time, INTERVAL '1' MINUTE) AS window_start,\n province,\n COUNT(*) AS abnormal_cnt,\n ROW_NUMBER() OVER (PARTITION BY TUMBLE_START(event_time, INTERVAL '1' MINUTE) ORDER BY COUNT(*) DESC) AS `rank`\n FROM device_source\n WHERE event_type = 3\n GROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), province\n)\nWHERE `rank` <= 5;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_006"}
{"task_id": "flinksql_007", "id": "online-compute_FlinkSQL_flinksql_007", "name": "用户行为滚动窗口统计", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置表造用户行为流,开滚动窗口统计每个用户的 action 数,结果写到 console。\n\n**业务背景与目标**:用 datagen 内置数据源表模拟用户行为数据流,每秒生成 50 条记录,包含三个字段:userId(用户ID,字符串,长度6)、action(动作类型,字符串,长度8)、ftime(事件时间,timestamp 类型)。请基于这个 datagen 表,按事件时间做 1 分钟滚动窗口聚合,统计每个用户在每个 1 分钟窗口内的 action 总次数,并把结果(用户ID、动作次数、窗口起始时间)写入到内置控制台表中输出。事件时间字段 ftime 使用 LOCALTIMESTAMP 作为 computed column 并设置 watermark 延迟 5 秒。\n\n**源表定义**:\n- `user_source`(用户行为流,datagen 连接器):\n - userId VARCHAR:用户 ID,随机生成,长度 6 字符\n - action VARCHAR:动作类型,随机生成,长度 8 字符\n - ftime:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 5 秒\n - 生成速率:rows-per-second = 50\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - user_id VARCHAR:用户 ID\n - action_count BIGINT:动作次数\n - window_start TIMESTAMP:窗口起始时间\n\n**查询逻辑**:\n- 窗口:TUMBLE(ftime, INTERVAL '1' MINUTE) 滚动窗口\n- 聚合:按 userId 分组,COUNT(*) 统计 action 总次数\n- 输出:窗口起始时间 + 用户 ID + 动作次数\n\n**输出要求**:\n- 使用 INSERT INTO console_output 输出结果\n- 输出字段顺序:user_id, action_count, window_start", "ground_truth": "-- flinksql_007: 用户行为滚动窗口统计\n-- datagen 生成用户行为流 @ 50 rows/sec, Tumble 1min 窗口聚合\n\n-- 1. 用户行为流 datagen 源表\nCREATE TABLE user_source (\n userId VARCHAR,\n action VARCHAR,\n ftime AS LOCALTIMESTAMP,\n WATERMARK FOR ftime AS ftime - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '50',\n 'fields.userId.length' = '6',\n 'fields.action.length' = '8'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n user_id VARCHAR,\n action_count BIGINT,\n window_start TIMESTAMP\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. Tumble 1min 窗口聚合: 统计每个用户每分钟的 action 次数\nINSERT INTO console_output\nSELECT\n userId AS user_id,\n COUNT(*) AS action_count,\n TUMBLE_START(ftime, INTERVAL '1' MINUTE) AS window_start\nFROM user_source\nGROUP BY TUMBLE(ftime, INTERVAL '1' MINUTE), userId;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_007"}
{"task_id": "flinksql_008_en", "id": "online-compute_FlinkSQL_flinksql_008", "name": "Session Window User Behavior Aggregation", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses a datagen built-in table to simulate a user behavior stream, performs session window aggregation to compute behavioral metrics per user within each session, and outputs the results to the console.\n\n**Business Background and Objective**: Simulate a user behavior data stream using a datagen built-in table that generates 50 records per second, containing four fields: `userId` (user ID, string of length 6), `action` (action type, string of length 8), `action_time` (action occurrence time, timestamp), and `ftime` (event time, timestamp). Based on this datagen table, using `ftime` as the event time (with a 5-second watermark delay), perform a 30-minute session window (`SESSION`) aggregation grouped by `userId`. Compute the following metrics for each user within each session window: session start time, session end time, total action count, number of distinct action types, and the last action time (the maximum value of `action_time`). Output the results to a built-in console table.\n\n**Source Table Definition**:\n- `user_source` (user behavior stream, datagen connector):\n - `userId VARCHAR`: User ID, randomly generated, 6 characters in length\n - `action VARCHAR`: Action type, randomly generated, 8 characters in length\n - `action_time TIMESTAMP(3)`: Action occurrence time\n - `ftime`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 50`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `user_id VARCHAR`: User ID\n - `session_start TIMESTAMP`: Session start time\n - `session_end TIMESTAMP`: Session end time\n - `action_count BIGINT`: Total action count\n - `unique_actions BIGINT`: Number of distinct action types\n - `last_action_time TIMESTAMP`: Last action time\n\n**Query Logic**:\n- Window: `SESSION(ftime, INTERVAL '30' MINUTE)` session window\n- Grouping: Group by `userId` and the session window\n- Aggregation: `COUNT(*)` for total action count, `COUNT(DISTINCT action)` for distinct action type count, `MAX(action_time)` for last action time\n- Window functions: Use `SESSION_START` / `SESSION_END` to obtain the window start and end times\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `user_id`, `session_start`, `session_end`, `action_count`, `unique_actions`, `last_action_time`", "ground_truth": "-- flinksql_008: 会话窗口用户行为聚合\n-- datagen 生成用户行为流 @ 50 rows/sec, SESSION 30min 窗口聚合\n\n-- 1. 用户行为流 datagen 源表\nCREATE TABLE user_source (\n userId VARCHAR,\n action VARCHAR,\n action_time TIMESTAMP(3),\n ftime AS LOCALTIMESTAMP,\n WATERMARK FOR ftime AS ftime - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '50',\n 'fields.userId.length' = '6',\n 'fields.action.length' = '8'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n user_id VARCHAR,\n session_start TIMESTAMP,\n session_end TIMESTAMP,\n action_count BIGINT,\n unique_actions BIGINT,\n last_action_time TIMESTAMP\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. SESSION 30min 窗口聚合: 统计每个用户每段会话的行为指标\nINSERT INTO console_output\nSELECT\n userId AS user_id,\n SESSION_START(ftime, INTERVAL '30' MINUTE) AS session_start,\n SESSION_END(ftime, INTERVAL '30' MINUTE) AS session_end,\n COUNT(*) AS action_count,\n COUNT(DISTINCT action) AS unique_actions,\n MAX(action_time) AS last_action_time\nFROM user_source\nGROUP BY SESSION(ftime, INTERVAL '30' MINUTE), userId;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_008_en"}
{"task_id": "flinksql_009_en", "id": "online-compute_FlinkSQL_flinksql_009", "name": "Tumbling Window PV/UV Statistics", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses a datagen built-in table to simulate a user click stream, computes PV and UV per page over a window, and writes the results to the console.\n\n**Business Background and Objective**: Simulate a user click stream using a datagen built-in table (`user_id` ranging from 1–1000, `page_id` ranging from 1–50, 10,000 records per second). Compute the PV (`COUNT(*)`) and UV (`COUNT(DISTINCT user_id)`) per page over a 1-minute tumbling window. Output the results (window start, window end, `page_id`, `pv`, `uv`) to the console.\n\n**Source Table Definition**:\n- `click_source` (user click stream, datagen connector):\n - `user_id INT`: User ID, randomly generated in the range 1–1000\n - `page_id INT`: Page ID, randomly generated in the range 1–50\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 10000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `window_start TIMESTAMP`: Window start time\n - `window_end TIMESTAMP`: Window end time\n - `page_id INT`: Page ID\n - `pv BIGINT`: Page views (PV)\n - `uv BIGINT`: Unique visitors (UV)\n\n**Query Logic**:\n- Window: `TUMBLE(event_time, INTERVAL '1' MINUTE)` tumbling window\n- Grouping: Group by `page_id` and the window\n- PV: `COUNT(*)` to compute page views\n- UV: `COUNT(DISTINCT user_id)` to compute unique visitors\n- Window functions: Use `TUMBLE_START` / `TUMBLE_END` to obtain the window start and end times\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `window_start`, `window_end`, `page_id`, `pv`, `uv`", "ground_truth": "-- flinksql_009: 滚动窗口 PV/UV 统计\n-- datagen 生成用户点击流 @ 10000 rows/sec, Tumble 1min PV/UV\n\n-- 1. 用户点击流 datagen 源表\nCREATE TABLE click_source (\n user_id INT,\n page_id INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '10000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '1000',\n 'fields.page_id.kind' = 'random',\n 'fields.page_id.min' = '1',\n 'fields.page_id.max' = '50'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP,\n window_end TIMESTAMP,\n page_id INT,\n pv BIGINT,\n uv BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. Tumble 1min 窗口聚合: 按页面统计 PV 和 UV\nINSERT INTO console_output\nSELECT\n TUMBLE_START(event_time, INTERVAL '1' MINUTE) AS window_start,\n TUMBLE_END(event_time, INTERVAL '1' MINUTE) AS window_end,\n page_id,\n COUNT(*) AS pv,\n COUNT(DISTINCT user_id) AS uv\nFROM click_source\nGROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), page_id;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_009_en"}
{"task_id": "flinksql_010", "id": "online-compute_FlinkSQL_flinksql_010", "name": "滑动窗口(HOP)店铺统计", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 模拟订单流,按店铺做滑动窗口统计订单金额和数量,写入 console。\n\n**业务背景与目标**:用 datagen 模拟订单流(order_id 取值 1~1000000、shop_id 取值 1~100、amount 取值 0~1000,每秒 5000 条),按 1 分钟步长、5 分钟窗口大小做 HOP 滑动窗口统计每个店铺的订单总额(SUM)、订单数(COUNT)和平均金额(AVG),写入 console。\n\n**源表定义**:\n- `order_source`(订单流,datagen 连接器):\n - order_id BIGINT:订单 ID,随机取值 1~1000000\n - shop_id INT:店铺 ID,随机取值 1~100\n - amount DOUBLE:订单金额,随机取值 0~1000\n - event_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 10 秒\n - 生成速率:rows-per-second = 5000\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start TIMESTAMP:窗口开始时间\n - window_end TIMESTAMP:窗口结束时间\n - shop_id INT:店铺 ID\n - total_amount DOUBLE:订单总额\n - order_cnt BIGINT:订单数量\n - avg_amount DOUBLE:平均订单金额\n\n**查询逻辑**:\n- 窗口:HOP(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE) 滑动窗口\n - 步长(slide):1 分钟\n - 窗口大小(size):5 分钟\n- 分组:按 shop_id 和窗口分组\n- 聚合:SUM(amount) 订单总额,COUNT(*) 订单数,AVG(amount) 平均金额\n- 窗口函数:HOP_START / HOP_END 获取窗口起止时间\n\n**输出要求**:\n- 使用 INSERT INTO console_output 输出结果\n- 输出字段顺序:window_start, window_end, shop_id, total_amount, order_cnt, avg_amount", "ground_truth": "-- flinksql_010: 滑动窗口(HOP)店铺统计\n-- datagen 生成订单流 @ 5000 rows/sec, HOP 1min slide 5min window\n\n-- 1. 订单流 datagen 源表\nCREATE TABLE order_source (\n order_id BIGINT,\n shop_id INT,\n amount DOUBLE,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '10' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '5000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.shop_id.kind' = 'random',\n 'fields.shop_id.min' = '1',\n 'fields.shop_id.max' = '100',\n 'fields.amount.kind' = 'random',\n 'fields.amount.min' = '0',\n 'fields.amount.max' = '1000'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP,\n window_end TIMESTAMP,\n shop_id INT,\n total_amount DOUBLE,\n order_cnt BIGINT,\n avg_amount DOUBLE\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. HOP 滑动窗口聚合: 按店铺统计订单总额、数量和平均金额\nINSERT INTO console_output\nSELECT\n HOP_START(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE) AS window_start,\n HOP_END(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE) AS window_end,\n shop_id,\n SUM(amount) AS total_amount,\n COUNT(*) AS order_cnt,\n AVG(amount) AS avg_amount\nFROM order_source\nGROUP BY HOP(event_time, INTERVAL '1' MINUTE, INTERVAL '5' MINUTE), shop_id;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_010"}
{"task_id": "flinksql_011_en", "id": "online-compute_FlinkSQL_flinksql_011", "name": "Tumbling Window Product TopN Ranking", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses the built-in datagen connector to simulate an order data source, aggregates product sales over a 10-minute tumbling window, and then uses the `ROW_NUMBER` window function to retrieve the top 5 products by sales within each window, outputting the results to the console.\n\n**Business Background and Objective**: Simulate an order stream using a datagen built-in table, generating massive order data. Group by a 10-minute tumbling window to compute the total sales amount and order count per product within each window, and finally use `ROW_NUMBER` ranking to retrieve the top 5 products by sales amount within each window.\n\n**Source Table Definition**:\n- `order_source` (order stream, datagen connector):\n - `order_id BIGINT`: Order ID, randomly generated in the range 1–10,000,000\n - `product_id INT`: Product ID, randomly generated in the range 1–200\n - `amount DOUBLE`: Order amount, randomly generated in the range 0–500\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 8000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `window_start TIMESTAMP(3)`: Tumbling window start time\n - `product_id INT`: Product ID\n - `sales DOUBLE`: Total sales amount of the product within the window\n - `order_cnt BIGINT`: Number of orders for the product within the window\n - `rn BIGINT`: Sales ranking within the window\n\n**SQL Logic**:\n- Apply `TUMBLE(event_time, INTERVAL '10' MINUTE)` tumbling window on `order_source`\n- Group by `window_start`, `window_end`, `product_id`\n- Aggregate `SUM(amount) AS sales` and `COUNT(*) AS order_cnt`\n- Use `ROW_NUMBER() OVER (PARTITION BY window_start, window_end ORDER BY sales DESC) AS rn`\n- Outer query filter: `WHERE rn <= 5`\n- Final SELECT: `window_start`, `product_id`, `sales`, `order_cnt`, `rn`\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `window_start`, `product_id`, `sales`, `order_cnt`, `rn`", "ground_truth": "-- flinksql_011: 滚动窗口商品 TopN 排行\n-- 10-min TUMBLE + SUM(amount) + COUNT(*) + ROW_NUMBER Top-5\n\n-- 1. 订单源 datagen 表\nCREATE TABLE order_source (\n order_id BIGINT,\n product_id INT,\n amount DOUBLE,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '8000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '10000000',\n 'fields.product_id.kind' = 'random',\n 'fields.product_id.min' = '1',\n 'fields.product_id.max' = '200',\n 'fields.amount.kind' = 'random',\n 'fields.amount.min' = '0',\n 'fields.amount.max' = '500'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP(3),\n product_id INT,\n sales DOUBLE,\n order_cnt BIGINT,\n rn BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. TUMBLE 窗口聚合 + ROW_NUMBER Top-5\nINSERT INTO console_output\nSELECT\n window_start,\n product_id,\n sales,\n order_cnt,\n rn\nFROM (\n SELECT\n window_start,\n window_end,\n product_id,\n sales,\n order_cnt,\n ROW_NUMBER() OVER (\n PARTITION BY window_start, window_end\n ORDER BY sales DESC\n ) AS rn\n FROM (\n SELECT\n TUMBLE_START(event_time, INTERVAL '10' MINUTE) AS window_start,\n TUMBLE_END(event_time, INTERVAL '10' MINUTE) AS window_end,\n product_id,\n SUM(amount) AS sales,\n COUNT(*) AS order_cnt\n FROM order_source\n GROUP BY\n TUMBLE(event_time, INTERVAL '10' MINUTE),\n product_id\n )\n)\nWHERE rn <= 5;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_011_en"}
{"task_id": "flinksql_012_en", "id": "online-compute_FlinkSQL_flinksql_012", "name": "Session Window User Behavior Statistics", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses the built-in datagen connector to simulate a user behavior data source, computes per-user behavioral statistics over a 30-minute session window (`SESSION`), and outputs the results to the console.\n\n**Business Background and Objective**: Simulate a user behavior stream using a datagen built-in table. Group by a 30-minute session window to compute the total action count (`COUNT`) and the number of distinct action types (`COUNT DISTINCT`) per user within each session window. Output the results to the console.\n\n**Source Table Definition**:\n- `user_actions` (user behavior stream, datagen connector):\n - `user_id INT`: User ID, randomly generated in the range 1–5000\n - `action_id INT`: Action type ID, randomly generated in the range 1–10\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 2000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `user_id INT`: User ID\n - `session_start TIMESTAMP(3)`: Session window start time\n - `session_end TIMESTAMP(3)`: Session window end time\n - `action_count BIGINT`: Total action count\n - `unique_actions BIGINT`: Number of distinct action types\n\n**SQL Logic**:\n- Apply `SESSION(event_time, INTERVAL '30' MINUTE)` session window on `user_actions`\n- Group by `SESSION(event_time, INTERVAL '30' MINUTE)` and `user_id`\n- Aggregate `COUNT(*) AS action_count` and `COUNT(DISTINCT action_id) AS unique_actions`\n- Use `SESSION_START` and `SESSION_END` functions to obtain the window start and end times\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `user_id`, `session_start`, `session_end`, `action_count`, `unique_actions`", "ground_truth": "-- flinksql_012: 会话窗口用户行为统计\n-- 30-min SESSION + COUNT + COUNT DISTINCT\n\n-- 1. 用户行为 datagen 源表\nCREATE TABLE user_actions (\n user_id INT,\n action_id INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '2000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '5000',\n 'fields.action_id.kind' = 'random',\n 'fields.action_id.min' = '1',\n 'fields.action_id.max' = '10'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n user_id INT,\n session_start TIMESTAMP(3),\n session_end TIMESTAMP(3),\n action_count BIGINT,\n unique_actions BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. SESSION 窗口聚合\nINSERT INTO console_output\nSELECT\n user_id,\n SESSION_START(event_time, INTERVAL '30' MINUTE) AS session_start,\n SESSION_END(event_time, INTERVAL '30' MINUTE) AS session_end,\n COUNT(*) AS action_count,\n COUNT(DISTINCT action_id) AS unique_actions\nFROM user_actions\nGROUP BY\n SESSION(event_time, INTERVAL '30' MINUTE),\n user_id;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_012_en"}
{"task_id": "flinksql_013", "id": "online-compute_FlinkSQL_flinksql_013", "name": "累积窗口日活统计 (Cumulate UV)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置连接器模拟用户登录数据源,通过累积窗口(CUMULATE)统计日活 UV 和登录次数,输出到 console。\n\n**业务背景与目标**:用 datagen 内置表模拟用户登录流水。使用 CUMULATE TVF 累积窗口,窗口步长 1 分钟、最大窗口 1 天,统计每个累积窗口内的独立用户数(UV)和登录总次数,输出到 console。\n\n**源表定义**:\n- `user_source`(用户登录流,datagen 连接器):\n - user_id INT:用户 ID,随机取值 1~100000\n - device_id INT:设备 ID,随机取值 1~5\n - event_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 5 秒\n - 生成速率:rows-per-second = 3000\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start TIMESTAMP(3):累积窗口起始时间\n - window_end TIMESTAMP(3):累积窗口结束时间\n - uv BIGINT:独立用户数(去重 user_id)\n - login_cnt BIGINT:登录总次数\n\n**SQL 逻辑**:\n- 使用 CUMULATE TVF:TABLE(CUMULATE(TABLE user_source, DESCRIPTOR(event_time), INTERVAL '1' MINUTE, INTERVAL '1' DAY))\n- 按 window_start 和 window_end 分组\n- 聚合 COUNT(DISTINCT user_id) AS uv 和 COUNT(*) AS login_cnt\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将结果输出\n- 输出字段顺序:window_start, window_end, uv, login_cnt", "ground_truth": "-- flinksql_013: 累积窗口日活统计 (Cumulate UV)\n-- CUMULATE(1min step, 1day max) + COUNT DISTINCT + COUNT\n\n-- 1. 用户登录 datagen 源表\nCREATE TABLE user_source (\n user_id INT,\n device_id INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '3000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '100000',\n 'fields.device_id.kind' = 'random',\n 'fields.device_id.min' = '1',\n 'fields.device_id.max' = '5'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP(3),\n window_end TIMESTAMP(3),\n uv BIGINT,\n login_cnt BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. CUMULATE TVF 窗口聚合\nINSERT INTO console_output\nSELECT\n window_start,\n window_end,\n COUNT(DISTINCT user_id) AS uv,\n COUNT(*) AS login_cnt\nFROM TABLE(\n CUMULATE(\n TABLE user_source,\n DESCRIPTOR(event_time),\n INTERVAL '1' MINUTE,\n INTERVAL '1' DAY\n )\n)\nGROUP BY window_start, window_end;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_013"}
{"task_id": "flinksql_014", "id": "online-compute_FlinkSQL_flinksql_014", "name": "Interval Join + 窗口聚合 (订单支付)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用两个 datagen 内置连接器模拟订单流和支付流,先按 order_id 做 Interval Join 匹配订单与支付,再对匹配结果做 10 分钟滚动窗口聚合统计,输出到 console。\n\n**业务背景与目标**:用两张 datagen 内置表分别模拟订单流和支付流。先按 order_id 做 Interval Join(支付时间在订单时间前后 10 分钟内),对匹配上的记录按 10 分钟滚动窗口聚合,统计每个窗口内的订单数和支付总额,输出到 console。\n\n**源表定义**:\n- `orders_source`(订单流,datagen 连接器):\n - order_id INT:订单 ID,随机取值 1~1000000\n - user_id INT:用户 ID,随机取值 1~100000\n - order_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 60 秒\n - 生成速率:rows-per-second = 10000\n\n- `payments_source`(支付流,datagen 连接器):\n - order_id INT:订单 ID,随机取值 1~1000000\n - pay_amount DOUBLE:支付金额,随机取值 0~5000\n - pay_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 60 秒\n - 生成速率:rows-per-second = 10000\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start VARCHAR:滚动窗口起始时间\n - window_end VARCHAR:滚动窗口结束时间\n - order_count BIGINT:窗口内匹配订单数\n - total_pay DOUBLE:窗口内支付总额\n\n**SQL 逻辑**:\n- 第一步:orders_source 与 payments_source 做 Interval Join\n - 关联条件:o.order_id = p.order_id\n - 时间窗口:p.pay_time BETWEEN o.order_time - INTERVAL '10' MINUTE AND o.order_time + INTERVAL '10' MINUTE\n - 注意:对 pay_time 做 CAST(pay_time AS TIMESTAMP(3)) 降为普通时间戳\n- 第二步:对 Join 结果做 10 分钟 TUMBLE 滚动窗口聚合\n - 使用 TUMBLE 按 o.order_time 作为事件时间\n - 聚合 COUNT(*) AS order_count 和 SUM(p.pay_amount) AS total_pay\n - 使用 TUMBLE_START 和 TUMBLE_END(CAST 为 VARCHAR)\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将结果输出\n- 输出字段顺序:window_start, window_end, order_count, total_pay", "ground_truth": "-- flinksql_014: Interval Join + 10-min TUMBLE 窗口聚合 (订单支付)\n-- 双源 Interval Join → TUMBLE 聚合 → COUNT + SUM\n\n-- 1. 订单流 datagen 源表\nCREATE TABLE orders_source (\n order_id INT,\n user_id INT,\n order_time AS LOCALTIMESTAMP,\n WATERMARK FOR order_time AS order_time - INTERVAL '60' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '10000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '100000'\n);\n\n-- 2. 支付流 datagen 源表\nCREATE TABLE payments_source (\n order_id INT,\n pay_amount DOUBLE,\n pay_time AS LOCALTIMESTAMP,\n WATERMARK FOR pay_time AS pay_time - INTERVAL '60' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '10000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000',\n 'fields.pay_amount.kind' = 'random',\n 'fields.pay_amount.min' = '0',\n 'fields.pay_amount.max' = '5000'\n);\n\n-- 3. Console 输出表\nCREATE TABLE console_output (\n window_start VARCHAR,\n window_end VARCHAR,\n order_count BIGINT,\n total_pay DOUBLE\n) WITH (\n 'connector' = 'print'\n);\n\n-- 4. Interval Join + TUMBLE 窗口聚合\nINSERT INTO console_output\nSELECT\n CAST(TUMBLE_START(o.order_time, INTERVAL '10' MINUTE) AS VARCHAR) AS window_start,\n CAST(TUMBLE_END(o.order_time, INTERVAL '10' MINUTE) AS VARCHAR) AS window_end,\n COUNT(*) AS order_count,\n SUM(p.pay_amount) AS total_pay\nFROM orders_source o\nJOIN payments_source p\n ON o.order_id = p.order_id\nWHERE\n p.pay_time BETWEEN o.order_time - INTERVAL '10' MINUTE\n AND o.order_time + INTERVAL '10' MINUTE\nGROUP BY\n TUMBLE(o.order_time, INTERVAL '10' MINUTE);", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_014"}
{"task_id": "flinksql_015", "id": "online-compute_FlinkSQL_flinksql_015", "name": "Regular Join 曝光点击关联", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用两个 datagen 内置连接器分别模拟广告曝光流和广告点击流,通过 Regular Join(常规内连接)关联曝光与点击,输出到 console。\n\n**业务背景与目标**:用两张 datagen 内置表分别模拟广告曝光和广告点击数据。Regular Join 不需要事件时间或 Watermark,直接按 user_id 和 ad_id 做内连接,将同一用户对同一广告的曝光和点击关联起来输出。\n\n**源表定义**:\n- `impression_source`(曝光流,datagen 连接器):\n - imp_id BIGINT:曝光 ID,随机取值 1~10000000\n - user_id INT:用户 ID,随机取值 1~100000\n - ad_id INT:广告 ID,随机取值 1~500\n - 生成速率:rows-per-second = 5000\n - 注意:Regular Join 不需要定义 event_time 或 WATERMARK\n\n- `click_source`(点击流,datagen 连接器):\n - click_id BIGINT:点击 ID,随机取值 1~10000000\n - user_id INT:用户 ID,随机取值 1~100000\n - ad_id INT:广告 ID,随机取值 1~500\n - 生成速率:rows-per-second = 1000\n - 注意:Regular Join 不需要定义 event_time 或 WATERMARK\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - user_id INT:用户 ID\n - ad_id INT:广告 ID\n - imp_id BIGINT:曝光 ID\n - click_id BIGINT:点击 ID\n\n**Join 逻辑**:\n- 使用 INNER JOIN 关联曝光流和点击流\n- 关联条件:i.user_id = c.user_id AND i.ad_id = c.ad_id\n- 这是一个 Regular Join,不需要 Interval Join 的时间窗口条件\n- 注意:Regular Join 的 state 会持续增长,在生产环境需注意 TTL 配置\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将匹配结果输出\n- 输出字段顺序:user_id, ad_id, imp_id, click_id", "ground_truth": "-- flinksql_015: Regular Join (广告曝光 + 点击)\n-- 双源无 WATERMARK + INNER JOIN on user_id AND ad_id\n\n-- 1. 曝光流 datagen 源表 (无 event_time, 无 WATERMARK)\nCREATE TABLE impression_source (\n imp_id BIGINT,\n user_id INT,\n ad_id INT\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '5000',\n 'fields.imp_id.kind' = 'random',\n 'fields.imp_id.min' = '1',\n 'fields.imp_id.max' = '10000000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '100000',\n 'fields.ad_id.kind' = 'random',\n 'fields.ad_id.min' = '1',\n 'fields.ad_id.max' = '500'\n);\n\n-- 2. 点击流 datagen 源表 (无 event_time, 无 WATERMARK)\nCREATE TABLE click_source (\n click_id BIGINT,\n user_id INT,\n ad_id INT\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.click_id.kind' = 'random',\n 'fields.click_id.min' = '1',\n 'fields.click_id.max' = '10000000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '100000',\n 'fields.ad_id.kind' = 'random',\n 'fields.ad_id.min' = '1',\n 'fields.ad_id.max' = '500'\n);\n\n-- 3. Console 输出表\nCREATE TABLE console_output (\n user_id INT,\n ad_id INT,\n imp_id BIGINT,\n click_id BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 4. Regular Join: 按 user_id + ad_id 关联曝光和点击\nINSERT INTO console_output\nSELECT\n i.user_id,\n i.ad_id,\n i.imp_id,\n c.click_id\nFROM impression_source i\nINNER JOIN click_source c\n ON i.user_id = c.user_id\n AND i.ad_id = c.ad_id;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_015"}
{"task_id": "flinksql_016", "id": "online-compute_FlinkSQL_flinksql_016", "name": "滚动窗口多维聚合 (GROUPING SETS)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置连接器模拟事件流,按 1 分钟滚动窗口做 GROUPING SETS 多维聚合,结果打到 console。\n\n**业务背景与目标**:用 datagen 内置表生成设备事件流,共 5000 行/秒。字段包括 os_id(操作系统 ID,1~3)、device_id(设备 ID,1~20)、city_id(城市 ID,1~100)。使用 LOCALTIMESTAMP 作为事件时间,设置 5 秒 Watermark 延迟。\n\n按 1 分钟 TUMBLE 滚动窗口,使用 GROUPING SETS 进行多维分组聚合,分组组合包括:(os_id, device_id)、(city_id)、以及全局汇总()。统计每个维度组合的事件数 COUNT(*),输出到 console 表。\n\n**源表定义**:\n- `event_source`(datagen 连接器):\n - os_id INT:操作系统 ID,随机取值 1~3\n - device_id INT:设备 ID,随机取值 1~20\n - city_id INT:城市 ID,随机取值 1~100\n - event_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 5 秒\n - 生成速率:rows-per-second = 5000\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start TIMESTAMP(3):窗口起始时间\n - os_id INT:操作系统 ID(聚合维度可为 NULL)\n - device_id INT:设备 ID(聚合维度可为 NULL)\n - city_id INT:城市 ID(聚合维度可为 NULL)\n - event_cnt BIGINT:事件计数\n\n**聚合逻辑**:\n- 滚动窗口:TUMBLE 1 分钟\n- 分组方式:GROUPING SETS ((os_id, device_id), (city_id), ())\n- 聚合指标:COUNT(*) AS event_cnt\n- 窗口起始时间用 TUMBLE_START 计算\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将结果输出\n- 输出字段顺序:window_start, os_id, device_id, city_id, event_cnt", "ground_truth": "-- flinksql_016: 滚动窗口多维聚合 (GROUPING SETS) — TVF syntax\n-- 生成 5000 行/秒的设备事件流\n\n-- 1. 设备事件流 datagen 源表\nCREATE TABLE event_source (\n os_id INT,\n device_id INT,\n city_id INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '5000',\n 'fields.os_id.kind' = 'random',\n 'fields.os_id.min' = '1',\n 'fields.os_id.max' = '3',\n 'fields.device_id.kind' = 'random',\n 'fields.device_id.min' = '1',\n 'fields.device_id.max' = '20',\n 'fields.city_id.kind' = 'random',\n 'fields.city_id.min' = '1',\n 'fields.city_id.max' = '100'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP(3),\n os_id INT,\n device_id INT,\n city_id INT,\n event_cnt BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. TUMBLE TVF 1min + GROUPING SETS 多维聚合\nINSERT INTO console_output\nSELECT\n window_start,\n os_id,\n device_id,\n city_id,\n COUNT(*) AS event_cnt\nFROM TABLE(\n TUMBLE(TABLE event_source, DESCRIPTOR(event_time), INTERVAL '1' MINUTE)\n)\nGROUP BY window_start, window_end,\n GROUPING SETS ((os_id, device_id), (city_id), ());", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_016"}
{"task_id": "flinksql_017_en", "id": "online-compute_FlinkSQL_flinksql_017", "name": "Row-Level TopN (Windowless)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses the built-in datagen connector to simulate a spending stream, retrieves the top 3 records by spending amount grouped by `country_id` without any window, and outputs the results directly to the console.\n\n**Business Background and Objective**: Generate user spending records using a datagen built-in table at a rate of 1000 rows per second. Fields include `country_id` (country ID, 1–30), `user_id` (user ID, 1–100,000), and `cost_money` (spending amount, 0–10,000). No event time or watermark is required, since no window operations are involved.\n\nUse the `ROW_NUMBER()` window function, partitioned by `country_id` and sorted by `cost_money` in descending order, keeping only the top 3 records per partition (`row_num <= 3`), and output the results directly to the console table. This is a continuous TopN query where each country's top 3 dynamically updates as new data flows in.\n\n**Source Table Definition**:\n- `spend_source` (datagen connector):\n - `country_id INT`: Country ID, randomly generated in the range 1–30\n - `user_id INT`: User ID, randomly generated in the range 1–100,000\n - `cost_money DOUBLE`: Spending amount, randomly generated in the range 0–10,000\n - Generation rate: `rows-per-second = 1000`\n\n**Output Table Definition**:\n- `console_output` (print connector):\n - `country_id INT`: Country ID\n - `user_id INT`: User ID\n - `cost_money DOUBLE`: Spending amount\n - `row_num BIGINT`: Row number\n\n**TopN Logic**:\n- Use `ROW_NUMBER() OVER (PARTITION BY country_id ORDER BY cost_money DESC)` to generate row numbers\n- Apply `WHERE row_num <= 3` to keep the top 3 per country\n- No window operation is required; no event time or watermark needs to be defined\n\n**Output Requirements**:\n- Use `INSERT INTO console_output` to output the results.\n- Output field order: `country_id`, `user_id`, `cost_money`, `row_num`", "ground_truth": "-- flinksql_017: 行级 TopN (无窗口)\n-- 生成 1000 行/秒的用户消费流\n\n-- 1. 用户消费流 datagen 源表(无事件时间/Watermark)\nCREATE TABLE spend_source (\n country_id INT,\n user_id INT,\n cost_money DOUBLE\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '1000',\n 'fields.country_id.kind' = 'random',\n 'fields.country_id.min' = '1',\n 'fields.country_id.max' = '30',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '100000',\n 'fields.cost_money.kind' = 'random',\n 'fields.cost_money.min' = '0',\n 'fields.cost_money.max' = '10000'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n country_id INT,\n user_id INT,\n cost_money DOUBLE,\n row_num BIGINT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. 行级 TopN: 每个 country_id 取 cost_money 最高的前 3 条\nINSERT INTO console_output\nSELECT\n country_id,\n user_id,\n cost_money,\n row_num\nFROM (\n SELECT\n country_id,\n user_id,\n cost_money,\n ROW_NUMBER() OVER (\n PARTITION BY country_id\n ORDER BY cost_money DESC\n ) AS row_num\n FROM spend_source\n) t\nWHERE row_num <= 3;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_017_en"}
{"task_id": "flinksql_018_en", "id": "online-compute_FlinkSQL_flinksql_018", "name": "Dual Output (Detail + Windowed Aggregation)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "en", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\nI need you to write a Flink SQL that uses the built-in datagen connector to simulate an order stream, simultaneously outputting detail records to `console1` and windowed aggregation results over a 1-minute tumbling window to `console2`.\n\n**Business Background and Objective**: Generate an order event stream using a datagen built-in table at a rate of 2000 rows per second. Fields include `order_id` (order ID), `user_id` (user ID, 1–10,000), and `amount` (order amount, 0–1000). Use `LOCALTIMESTAMP` as the event time and set a 5-second watermark delay.\n\nThe same source data must be simultaneously output to two sinks:\n- `console1`: Detail passthrough, directly outputting the original fields\n- `console2`: Aggregation over a 1-minute `TUMBLE` tumbling window, grouped by `user_id` to compute the order count and total amount\n\n**Source Table Definition**:\n- `order_source` (datagen connector):\n - `order_id BIGINT`: Order ID, randomly generated\n - `user_id INT`: User ID, randomly generated in the range 1–10,000\n - `amount DOUBLE`: Order amount, randomly generated in the range 0–1000\n - `event_time`: Uses `LOCALTIMESTAMP` to generate event time, with a WATERMARK delay of 5 seconds\n - Generation rate: `rows-per-second = 2000`\n\n**Output Table 1 Definition**:\n- `console1` (print connector, detail output):\n - `order_id BIGINT`\n - `user_id INT`\n - `amount DOUBLE`\n\n**Output Table 2 Definition**:\n- `console2` (print connector, aggregation output):\n - `window_start TIMESTAMP(3)`: Window start time\n - `user_id INT`: User ID\n - `order_cnt BIGINT`: Order count within the window\n - `total_amount DOUBLE`: Total order amount within the window\n\n**Aggregation Logic**:\n- Detail output: Direct passthrough from `order_source`\n- Aggregation output: `TUMBLE` 1-minute tumbling window, `GROUP BY user_id`\n- Aggregation metrics: `COUNT(*) AS order_cnt`, `SUM(amount) AS total_amount`\n\n**Output Requirements**:\n- Use two `INSERT INTO` statements to write to `console1` and `console2` respectively.\n- `console1` output field order: `order_id`, `user_id`, `amount`\n- `console2` output field order: `window_start`, `user_id`, `order_cnt`, `total_amount`", "ground_truth": "-- flinksql_018: 双输出 (明细 + 窗口聚合)\n-- 生成 2000 行/秒的订单事件流\n\n-- 1. 订单事件流 datagen 源表\nCREATE TABLE order_source (\n order_id BIGINT,\n user_id INT,\n amount DOUBLE,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '2000',\n 'fields.order_id.kind' = 'random',\n 'fields.order_id.min' = '1',\n 'fields.order_id.max' = '1000000000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '10000',\n 'fields.amount.kind' = 'random',\n 'fields.amount.min' = '0',\n 'fields.amount.max' = '1000'\n);\n\n-- 2. Console1: 明细透传输出表\nCREATE TABLE console1 (\n order_id BIGINT,\n user_id INT,\n amount DOUBLE\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. Console2: 窗口聚合输出表\nCREATE TABLE console2 (\n window_start TIMESTAMP(3),\n user_id INT,\n order_cnt BIGINT,\n total_amount DOUBLE\n) WITH (\n 'connector' = 'print'\n);\n\n-- 4. 明细透传:直接输出原始字段\nINSERT INTO console1\nSELECT\n order_id,\n user_id,\n amount\nFROM order_source;\n\n-- 5. 窗口聚合:TUMBLE 1min 按 user_id 分组统计\nINSERT INTO console2\nSELECT\n TUMBLE_START(event_time, INTERVAL '1' MINUTE) AS window_start,\n user_id,\n COUNT(*) AS order_cnt,\n SUM(amount) AS total_amount\nFROM order_source\nGROUP BY TUMBLE(event_time, INTERVAL '1' MINUTE), user_id;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_018_en"}
{"task_id": "flinksql_019", "id": "online-compute_FlinkSQL_flinksql_019", "name": "全局分组 + 过滤统计", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置连接器模拟交易流,按状态过滤后全局分组统计,结果打到 console。\n\n**业务背景与目标**:用 datagen 内置表生成交易事件流,共 3000 行/秒。字段包括 trade_id(交易 ID)、type(交易类型,1~5)、status(状态,0~2)、amount(交易金额,0~5000)。\n\n注意:`type` 和 `status` 是 SQL 保留字,需要使用反引号包裹(例如 `` `type` ``、`` `status` ``)。\n\n先按 status = 1 过滤,再按 type 分组统计交易笔数和交易金额总和,最后输出到 console 表。因为不涉及窗口操作,无需定义事件时间和 Watermark。\n\n**源表定义**:\n- `trade_source`(datagen 连接器):\n - trade_id BIGINT:交易 ID,随机取值\n - `` `type` `` INT:交易类型,随机取值 1~5\n - `` `status` `` INT:状态,随机取值 0~2\n - amount DOUBLE:交易金额,随机取值 0~5000\n - 生成速率:rows-per-second = 3000\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - `` `type` `` INT:交易类型\n - trade_cnt BIGINT:交易笔数\n - total_amount DOUBLE:交易金额总和\n\n**过滤与聚合逻辑**:\n- 过滤条件:`` `status` `` = 1(仅统计状态为 1 的交易)\n- 分组:按 `` `type` `` 分组\n- 聚合指标:COUNT(*) AS trade_cnt, SUM(amount) AS total_amount\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将结果输出\n- 输出字段顺序:`` `type` ``, trade_cnt, total_amount", "ground_truth": "-- flinksql_019: 全局分组 + 过滤统计\n-- 生成 3000 行/秒的交易事件流,使用反引号包裹保留字\n\n-- 1. 交易事件流 datagen 源表(无事件时间/Watermark)\nCREATE TABLE trade_source (\n trade_id BIGINT,\n `type` INT,\n `status` INT,\n amount DOUBLE\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '3000',\n 'fields.trade_id.kind' = 'random',\n 'fields.trade_id.min' = '1',\n 'fields.trade_id.max' = '1000000000',\n 'fields.type.kind' = 'random',\n 'fields.type.min' = '1',\n 'fields.type.max' = '5',\n 'fields.status.kind' = 'random',\n 'fields.status.min' = '0',\n 'fields.status.max' = '2',\n 'fields.amount.kind' = 'random',\n 'fields.amount.min' = '0',\n 'fields.amount.max' = '5000'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n `type` INT,\n trade_cnt BIGINT,\n total_amount DOUBLE\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. 过滤 status=1 + 按 type 分组统计\nINSERT INTO console_output\nSELECT\n `type`,\n COUNT(*) AS trade_cnt,\n SUM(amount) AS total_amount\nFROM trade_source\nWHERE `status` = 1\nGROUP BY `type`;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_019"}
{"task_id": "flinksql_020", "id": "online-compute_FlinkSQL_flinksql_020", "name": "窗口去重 (TVF Dedup)", "workload": "online-compute", "engine": "FlinkSQL", "category": "online-compute/FlinkSQL", "language": "zh", "modality": "pure-text", "timeout_seconds": 600, "prompt": "## Prompt\n我需要你写一段 Flink SQL,用 datagen 内置连接器模拟事件流,使用 TUMBLE TVF 窗口表值函数 + ROW_NUMBER 做窗口去重,每个窗口内每个用户只保留最早的一条事件。\n\n**业务背景与目标**:用 datagen 内置表生成用户事件流,共 5000 行/秒。字段包括 user_id(用户 ID,1~10000)、event_id(事件 ID)、event_type(事件类型,1~8)。使用 LOCALTIMESTAMP 作为事件时间,设置 10 秒 Watermark 延迟。\n\n使用 TUMBLE 表值函数(TABLE(TUMBLE(...)))将事件分配到 5 分钟滚动窗口,然后在每个(window_start, user_id)分区内,按事件时间升序使用 ROW_NUMBER() 编号,只保留每个窗口每个用户的第一条事件(rn = 1),最后输出到 console。\n\n**源表定义**:\n- `event_source`(datagen 连接器):\n - user_id INT:用户 ID,随机取值 1~10000\n - event_id BIGINT:事件 ID,随机取值\n - event_type INT:事件类型,随机取值 1~8\n - event_time:使用 LOCALTIMESTAMP 生成事件时间,设置 WATERMARK 延迟 10 秒\n - 生成速率:rows-per-second = 5000\n\n**输出表定义**:\n- `console_output`(print 连接器):\n - window_start TIMESTAMP(3):窗口起始时间\n - window_end TIMESTAMP(3):窗口结束时间\n - user_id INT:用户 ID\n - event_id BIGINT:事件 ID\n - event_type INT:事件类型\n\n**去重逻辑**:\n- 窗口:TUMBLE TVF 5 分钟滚动窗口\n- 排序:ROW_NUMBER() OVER (PARTITION BY window_start, user_id ORDER BY event_time ASC)\n- 过滤:WHERE rn = 1 保留每个窗口每个用户的第一条事件\n- 窗口时间字段可从 TVF 的 window_start 和 window_end 获得\n\n**输出要求**:\n- 使用 INSERT INTO console_output 将结果输出\n- 输出字段顺序:window_start, window_end, user_id, event_id, event_type", "ground_truth": "-- flinksql_020: 窗口去重 (TVF Dedup)\n-- TUMBLE TVF + ROW_NUMBER 按窗口去重,保留每个窗口每个用户的第一条\n\n-- 1. 用户事件流 datagen 源表\nCREATE TABLE event_source (\n user_id INT,\n event_id BIGINT,\n event_type INT,\n event_time AS LOCALTIMESTAMP,\n WATERMARK FOR event_time AS event_time - INTERVAL '10' SECOND\n) WITH (\n 'connector' = 'datagen',\n 'rows-per-second' = '5000',\n 'fields.user_id.kind' = 'random',\n 'fields.user_id.min' = '1',\n 'fields.user_id.max' = '10000',\n 'fields.event_id.kind' = 'random',\n 'fields.event_id.min' = '1',\n 'fields.event_id.max' = '1000000000',\n 'fields.event_type.kind' = 'random',\n 'fields.event_type.min' = '1',\n 'fields.event_type.max' = '8'\n);\n\n-- 2. Console 输出表\nCREATE TABLE console_output (\n window_start TIMESTAMP(3),\n window_end TIMESTAMP(3),\n user_id INT,\n event_id BIGINT,\n event_type INT\n) WITH (\n 'connector' = 'print'\n);\n\n-- 3. TUMBLE TVF + ROW_NUMBER 窗口去重\nINSERT INTO console_output\nSELECT\n window_start,\n window_end,\n user_id,\n event_id,\n event_type\nFROM (\n SELECT\n window_start,\n window_end,\n user_id,\n event_id,\n event_type,\n ROW_NUMBER() OVER (\n PARTITION BY window_start, user_id\n ORDER BY event_time ASC\n ) AS rn\n FROM TABLE(\n TUMBLE(TABLE event_source, DESCRIPTOR(event_time), INTERVAL '5' MINUTE)\n )\n) t\nWHERE rn = 1;", "expected_csv": "", "grade_spec_csv": "", "path": "tasks/online-compute/FlinkSQL/flinksql_020"}
|