apache / doris

Apache Doris is an easy-to-use, high performance and unified analytics database.
https://doris.apache.org
Apache License 2.0
11.84k stars 3.12k forks source link

[Bug] Wrong synchronized materialized view is selected #37207

Open liutang123 opened 3 days ago

liutang123 commented 3 days ago

Search before asking

Version

master

What's Wrong?

CREATE TABLE advertiser_view_record(
  time date not null,
  advertiser varchar(10),
  dt date not null,
  channel varchar(10),
  user_id int) 
DUPLICATE KEY(`time`, `advertiser`)
  PARTITION BY RANGE (dt)(FROM ("2024-07-02") TO ("2024-07-04") INTERVAL 1 DAY)
  -- AUTO PARTITION BY RANGE (date_trunc(`time`, 'day'))()
  distributed BY hash(time) 
  properties("replication_num" = "1");
CREATE materialized VIEW advertiser_uv AS
SELECT advertiser,
       channel,
       dt,
       bitmap_union(to_bitmap(user_id))
FROM advertiser_view_record
GROUP BY advertiser,
         channel,
         dt;
insert into advertiser_view_record values("2024-07-02",'a', "2024-07-02", 'a',1);
insert into advertiser_view_record values("2024-07-03",'b', "2024-07-03", 'b',1);
EXPLAIN
SELECT dt,advertiser,
       count(DISTINCT user_id)
FROM advertiser_view_record
GROUP BY dt,advertiser

The advertiser_uv can not be selected and the result is:

|   0:VOlapScanNode(421)                                                                              |
|      TABLE: test7.advertiser_view_record(advertiser_view_record), PREAGGREGATION: ON                |
|      partitions=2/2 (p_20240702,p_20240703)                                                         |
...
| MaterializedView                                                                                    |
| MaterializedViewRewriteSuccessAndChose:                                                             |
|                                                                                                     |
| MaterializedViewRewriteSuccessButNotChose:                                                          |
|   Names: internal#test_37207#advertiser_uv,                                                         |
| MaterializedViewRewriteFail:                                                                        |
|                                                                                                     |
| Statistics                                                                                          |
|  planed with unknown column statistics

If we set set enable_sync_mv_cost_based_rewrite = false; the right materialized view can be selected. This variable is introduced by #33699.

What You Expected?

In version 2.1.4, advertiser_uv will be selected.

How to Reproduce?

No response

Anything Else?

No response

Are you willing to submit PR?

Code of Conduct