pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
36.91k stars 5.81k forks source link

Panic triggered at core.(*LogicalJoin).BuildKeyInfo (planner/core/rule_build_key_info.go:201) #42736

Open JZuming opened 1 year ago

JZuming commented 1 year ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Setup the database:

CREATE TABLE t0 (c1 double);
CREATE TABLE t2 (c11 int);

Testcase

select 1
from
  (select false as c1) as subq_0
where (case when subq_0.c1 >= (
            select
                1 as c0
              from
                t2 as ref_4
              where false
              order by c0 asc limit 1
        ) then 1 else 0 end
      ) = nullif(1 << 1, (
                select
                    0 as c0
                  from
                    t0 as ref_8
                  where exists (
                        select
                            1 as c11
                          from
                            t0 as ref_13
                          where ref_8.c1 < ref_13.c1
                        )
                  order by c0 asc limit 1
                 ));

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

No panic.

3. What did you see instead (Required)

ERROR 1105 (HY000): interface conversion: expression.Expression is *expression.ScalarFunction, not *expression.Column

TiDB log:

[ERROR] [conn.go:1039] ["connection running loop panic"] [conn=2199023258091] 
[lastSQL="select 1 
from 
  (select false as c1) as subq_0
where (case when subq_0.c1 >= ( 
            select  
                1 as c0
              from 
                t2 as ref_4
              where false
              order by c0 asc limit 1
        ) then 1 else 0 end
      ) = nullif(1 << 1, ( 
                select  
                    0 as c0
                  from 
                    t0 as ref_8
                  where exists (
                        select  
                            1 as c11
                          from 
                            t0 as ref_13
                          where ref_8.c1 < ref_13.c1
                )
                  order by c0 asc limit 1
             ))"]
[err="interface conversion: expression.Expression is *expression.ScalarFunction, not *expression.Column"] 
[stack="github.com/pingcap/tidb/server.(*clientConn).Run.func1
    /root/tidb/server/conn.go:1042
runtime.gopanic
    /usr/local/go/src/runtime/panic.go:884
github.com/pingcap/tidb/executor.(*Compiler).Compile.func1
    /root/tidb/executor/compiler.go:54
runtime.gopanic
    /usr/local/go/src/runtime/panic.go:884
runtime.panicdottypeE
    /usr/local/go/src/runtime/iface.go:262
runtime.panicdottypeI
    /usr/local/go/src/runtime/iface.go:272
github.com/pingcap/tidb/planner/core.(*LogicalJoin).BuildKeyInfo
    /root/tidb/planner/core/rule_build_key_info.go:201
github.com/pingcap/tidb/planner/core.buildKeyInfo
    /root/tidb/planner/core/rule_build_key_info.go:42
github.com/pingcap/tidb/planner/core.(*LogicalJoin).PredicatePushDown
    /root/tidb/planner/core/rule_predicate_push_down.go:243
github.com/pingcap/tidb/planner/core.(*LogicalSelection).PredicatePushDown
    /root/tidb/planner/core/rule_predicate_push_down.go:108
github.com/pingcap/tidb/planner/core.(*baseLogicalPlan).PredicatePushDown
    /root/tidb/planner/core/rule_predicate_push_down.go:81
github.com/pingcap/tidb/planner/core.(*LogicalProjection).PredicatePushDown
    /root/tidb/planner/core/rule_predicate_push_down.go:479
github.com/pingcap/tidb/planner/core.(*ppdSolver).optimize
    /root/tidb/planner/core/rule_predicate_push_down.go:46
github.com/pingcap/tidb/planner/core.logicalOptimize
    /root/tidb/planner/core/optimizer.go:1093
github.com/pingcap/tidb/planner/core.DoOptimize
    /root/tidb/planner/core/optimizer.go:294
github.com/pingcap/tidb/planner.optimize
    /root/tidb/planner/optimize.go:467
github.com/pingcap/tidb/planner.Optimize
    /root/tidb/planner/optimize.go:295
github.com/pingcap/tidb/executor.(*Compiler).Compile
    /root/tidb/executor/compiler.go:98
github.com/pingcap/tidb/session.(*session).ExecuteStmt
    /root/tidb/session/session.go:2139
github.com/pingcap/tidb/server.(*TiDBContext).ExecuteStmt
    /root/tidb/server/driver_tidb.go:252
github.com/pingcap/tidb/server.(*clientConn).handleStmt
    /root/tidb/server/conn.go:2054
github.com/pingcap/tidb/server.(*clientConn).handleQuery
    /root/tidb/server/conn.go:1851
github.com/pingcap/tidb/server.(*clientConn).dispatch
    /root/tidb/server/conn.go:1337
github.com/pingcap/tidb/server.(*clientConn).Run
    /root/tidb/server/conn.go:1120
github.com/pingcap/tidb/server.(*Server).onConn
    /root/tidb/server/server.go:675"]

4. What is your TiDB version? (Required)

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Release Version: v7.1.0-alpha-27-gf5ca27ef3 Edition: Community Git Commit Hash: f5ca27ef326e94fb29b4ba35a5d59aa575f66880 Git Branch: master UTC Build Time: 2023-03-23 13:57:53 GoVersion: go1.20.2 Race Enabled: false TiKV Min Version: 6.2.0-alpha Check Table Before Drop: false Store: unistore | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

fixdb commented 10 months ago

/assign @hawkingrei

hawkingrei commented 1 week ago

We can simple the SQL like this.

select 1
from
  (select false as c1) as subq_0
where (case when subq_0.c1 >= (
            select
                1 as c0
              from
                t2 as ref_4
        ) then 1 else 0 end
      ) = nullif(1 << 1, (
                select
                    0 as c0
                  from
                    t0 as ref_8
                  where exists (
                        select
                            1 as c11
                          from
                            t0 as ref_13
                          where ref_8.c1 < ref_13.c1
                        )

                 ));