starrocks icon indicating copy to clipboard operation
starrocks copied to clipboard

Query execution plan failure due to window functions

Open vitalyDE opened this issue 6 months ago • 0 comments

Description

The SQL query returns correct results on the first run. However, on subsequent runs, an error occurs, leading to unsuccessful query execution. The error is related to the execution plan of the query.

Steps to reproduce the behavior (Required)

  1. create the table:
CREATE TABLE sandbox.test_table (
       crm_id varchar(64),
       status_code varchar(64),
       created_on_dttm DATETIME
   );
  1. Insert data:
INSERT INTO sandbox.test_table (crm_id, status_code, created_on_dttm) VALUES
('1', '10', '2023-10-01 10:00:00'),
('1', '20', '2023-10-01 10:00:00'),
('1', '30', '2023-10-02 12:00:00'),
('2', '10', '2023-10-01 09:00:00'),
('2', '15', '2023-10-01 09:00:00'),
('3', '10', '2023-10-03 08:00:00'),
('3', '20', '2023-10-03 08:00:00'),
('3', '30', '2023-10-04 14:00:00');
  1. Execute the following query:
SELECT
       t.crm_id,
       t.status_code,
       t.min_date_create
   FROM
       (SELECT
               tt.crm_id,
               tt.status_code,
            MIN(tt.created_on_dttm) OVER (PARTITION BY tt.created_on_dttm, tt.crm_id) AS min_date_create,
               ROW_NUMBER () OVER (PARTITION BY tt.created_on_dttm, tt.crm_id ORDER BY tt.status_code) AS row_num
       FROM sandbox.test_table tt
       ) t
   WHERE t.row_num = 1;

Expected behavior (Required)

The query should return correct results on every execution without errors.

Real behavior (Required)

On the first run, the query returns the correct result. However, starting from the second run, the following error occurs:

SQL Error [1064] [42000]: Invalid plan:
PHYSICAL_WINDOW
->  PHYSICAL_DECODE
    ->  PhysicalTopNOperator {phase=FINAL, orderBy=[9: created_on_dttm ASC NULLS FIRST, 2: crm_id ASC NULLS FIRST, 19: status_code ASC NULLS FIRST], limit=-1, offset=0}
        ->  PhysicalDistributionOperator {distributionSpec=SHUFFLE_AGG[9(true), 2(true)] ,globalDict=[19:com.starrocks.sql.optimizer.statistics.ColumnDict@650fe224]}
            ->  PhysicalTopNOperator {phase=PARTIAL, orderBy=[9: created_on_dttm ASC NULLS FIRST, 2: crm_id ASC NULLS FIRST, 19: status_code ASC NULLS FIRST], limit=-1, offset=0}
                ->  PhysicalOlapScanOperator {table=10602272, selectedPartitionId=[10602271], selectedIndexId=10602273, outputColumns=[2: crm_id, 19: status_code, 9: created_on_dttm], projection=null, predicate=null, prunedPartitionPredicates=[], limit=-1}Input dependency cols check failed. The required cols {18} cannot obtain from input cols {2,3,9}.

StarRocks version (Required)

  • 3.3.4

vitalyDE avatar Jun 20 '25 07:06 vitalyDE