Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpp: wrong planner behaviors when tidb_opt_agg_push_down and tidb_enforce_mpp enabled #34465

Closed
solotzg opened this issue May 9, 2022 · 3 comments · Fixed by #34468
Closed
Assignees
Labels
affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@solotzg
Copy link
Contributor

solotzg commented May 9, 2022

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Run with tiflash

1. table schema.

CREATE TABLE `customer` (
  `C_CUSTKEY` bigint(20) NOT NULL,
  `C_NAME` varchar(25) NOT NULL,
  `C_ADDRESS` varchar(40) NOT NULL,
  `C_NATIONKEY` bigint(20) NOT NULL,
  `C_PHONE` char(15) NOT NULL,
  `C_ACCTBAL` decimal(15,2) NOT NULL,
  `C_MKTSEGMENT` char(10) NOT NULL,
  `C_COMMENT` varchar(117) NOT NULL,
  PRIMARY KEY (`C_CUSTKEY`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;


CREATE TABLE `orders` (
  `O_ORDERKEY` bigint(20) NOT NULL,
  `O_CUSTKEY` bigint(20) NOT NULL,
  `O_ORDERSTATUS` char(1) NOT NULL,
  `O_TOTALPRICE` decimal(15,2) NOT NULL,
  `O_ORDERDATE` date NOT NULL,
  `O_ORDERPRIORITY` char(15) NOT NULL,
  `O_CLERK` char(15) NOT NULL,
  `O_SHIPPRIORITY` bigint(20) NOT NULL,
  `O_COMMENT` varchar(79) NOT NULL,
  PRIMARY KEY (`O_ORDERKEY`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

2. set tiflash replica for table customer and orders
3. insert into customer values (6, "a", "a", 2, "12345", 3, "Z", "haha");
4. set @@tidb_enforce_mpp=1; set @@tidb_opt_agg_push_down=1;
5.
                     select
                             c_custkey,
                             count(o_orderkey) as c_count
                     from
                             customer left outer join orders on
                                     c_custkey = o_custkey
                     group by
                             c_custkey

Run without tiflash

1. 
CREATE TABLE `customer` (
  `C_CUSTKEY` bigint(20) NOT NULL,
  `C_NAME` varchar(25) NOT NULL,
  `C_ADDRESS` varchar(40) NOT NULL,
  `C_NATIONKEY` bigint(20) NOT NULL,
  `C_PHONE` char(15) NOT NULL,
  `C_ACCTBAL` decimal(15,2) NOT NULL,
  `C_MKTSEGMENT` char(10) NOT NULL,
  `C_COMMENT` varchar(117) NOT NULL,
  PRIMARY KEY (`C_CUSTKEY`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;


CREATE TABLE `orders` (
  `O_ORDERKEY` bigint(20) NOT NULL,
  `O_CUSTKEY` bigint(20) NOT NULL,
  `O_ORDERSTATUS` char(1) NOT NULL,
  `O_TOTALPRICE` decimal(15,2) NOT NULL,
  `O_ORDERDATE` date NOT NULL,
  `O_ORDERPRIORITY` char(15) NOT NULL,
  `O_CLERK` char(15) NOT NULL,
  `O_SHIPPRIORITY` bigint(20) NOT NULL,
  `O_COMMENT` varchar(79) NOT NULL,
  PRIMARY KEY (`O_ORDERKEY`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

2. insert into customer values (6, "a", "a", 2, "12345", 3, "Z", "haha");
3.
select c_custkey,count(o_orderkey) as c_count from customer left outer join orders on c_custkey = o_custkey group by c_custkey;
4.
+-----------+---------+
| c_custkey | c_count |
+-----------+---------+
|         6 |       0 |
+-----------+---------+

4. set @@tidb_enforce_mpp=1; set @@tidb_opt_agg_push_down=1;
5. 
explain select c_custkey,count(o_orderkey) as c_count from customer left outer join orders on c_custkey = o_custkey group by c_custkey;
+----------------------------------+----------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------+
| id                               | estRows  | task      | access object  | operator info                                                                                                                   |
+----------------------------------+----------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------+
| Projection_8                     | 1.00     | root      |                | test.customer.c_custkey, Column#18                                                                                              |
| └─Projection_9                   | 1.00     | root      |                | Column#19, test.customer.c_custkey                                                                                              |
|   └─HashJoin_11                  | 1.00     | root      |                | left outer join, equal:[eq(test.customer.c_custkey, test.orders.o_custkey)]                                                     |
|     ├─TableReader_13(Build)      | 1.00     | root      |                | data:TableFullScan_12                                                                                                           |
|     │ └─TableFullScan_12         | 1.00     | cop[tikv] | table:customer | keep order:false, stats:pseudo                                                                                                  |
|     └─HashAgg_18(Probe)          | 8000.00  | root      |                | group by:test.orders.o_custkey, funcs:count(Column#20)->Column#19, funcs:firstrow(test.orders.o_custkey)->test.orders.o_custkey |
|       └─TableReader_19           | 8000.00  | root      |                | data:HashAgg_14                                                                                                                 |
|         └─HashAgg_14             | 8000.00  | cop[tikv] |                | group by:test.orders.o_custkey, funcs:count(test.orders.o_orderkey)->Column#20                                                  |
|           └─TableFullScan_17     | 10000.00 | cop[tikv] | table:orders   | keep order:false, stats:pseudo                                                                                                  |
+----------------------------------+----------+-----------+----------------+---------------------------------------------------------------------------------------------------------------------------------+
6.
select c_custkey,count(o_orderkey) as c_count from customer left outer join orders on c_custkey = o_custkey group by c_custkey;

+-----------+---------+
| c_custkey | c_count |
+-----------+---------+
|         6 |    NULL |
+-----------+---------+

2. What did you expect to see? (Required)

+-----------+---------+
| c_custkey | c_count |
+-----------+---------+
|         6 |       0 |
+-----------+---------+

3. What did you see instead (Required)

with tiflash

ERROR 1105 (HY000): other error for mpp stream: DB::Exception: Cannot convert NULL value to non-Nullable type

without tiflash

+-----------+---------+
| c_custkey | c_count |
+-----------+---------+
|         6 |    NULL |
+-----------+---------+

4. What is your TiFlash/TiDB/TiKV version? (Required)

master、release-5.4

@solotzg solotzg added the type/bug The issue is confirmed as a bug. label May 9, 2022
@solotzg
Copy link
Contributor Author

solotzg commented May 9, 2022

/assign @fixdb

@ti-chi-bot
Copy link
Member

@solotzg: GitHub didn't allow me to assign the following users: fixdb.

Note that only pingcap members, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

/assign @fixdb

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@solotzg
Copy link
Contributor Author

solotzg commented May 10, 2022

Affect version >= release-5.0

@seiya-annie seiya-annie added affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 sig/planner SIG: Planner severity/major labels May 10, 2022
@ti-chi-bot ti-chi-bot added the may-affects-4.0 This bug maybe affects 4.0.x versions. label May 10, 2022
fixdb added a commit to fixdb/tidb that referenced this issue May 13, 2022
… outer joins

When we push down aggregate past outer join, the top count aggregate assumed
that the partial count aggregate always return non-null value. But in case of
outer joins when the aggregate is pushed down to the null generating side of
the outer join, the input for the final aggregate may be nullable.

Before this patch, the final count aggregate always assume its input is not
nullable, hence during aggregate elimimation, the optimizer rewrote aggregate
into a plain column of null-generating side of outer join, which led to the
wrong result.

Close pingcap#34465
@Reminiscent Reminiscent removed the may-affects-4.0 This bug maybe affects 4.0.x versions. label May 13, 2022
fixdb added a commit to fixdb/tidb that referenced this issue May 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants