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

Stack overflow caused by checking constant wrongly #10156

Closed
qw4990 opened this issue Apr 15, 2019 · 1 comment · Fixed by #10174
Closed

Stack overflow caused by checking constant wrongly #10156

qw4990 opened this issue Apr 15, 2019 · 1 comment · Fixed by #10174
Assignees
Labels
sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@qw4990
Copy link
Contributor

qw4990 commented Apr 15, 2019

Bug Report

Please answer these questions before submitting your issue. Thanks!

  1. What did you do?
    If possible, provide a recipe for reproducing the error.
CREATE TABLE `t1` (
  `period_name` varchar(24) DEFAULT NULL ,
  `period_id` bigint(20) DEFAULT NULL ,
  `starttime` bigint(20) DEFAULT NULL 
);

CREATE TABLE `t2` (
  `bussid` bigint(20) DEFAULT NULL,
  `ct` bigint(20) DEFAULT NULL
);

select
    a.period_name,
    b.date8
from
    (select * from t1) a
left join
    (select bussid,date(from_unixtime(ct)) date8 from t2) b
on 
    a.period_id = b.bussid
where 
    datediff(b.date8, date(from_unixtime(a.starttime))) >= 0;
  1. What did you expect to see?
mysql> select
    ->     a.period_name,
    ->     b.date8
    -> from
    ->     (select * from t1) a
    -> left join
    ->     (select bussid,date(from_unixtime(ct)) date8 from t2) b
    -> on 
    ->     a.period_id = b.bussid
    -> where 
    ->     datediff(b.date8, date(from_unixtime(a.starttime))) >= 0;
Empty set (0.01 sec)
  1. What did you see instead?
    No result and eventually panic caused by stack overflow.
mysql> select     a.period_name,     b.date8 from     (select * from t1) a left join     (select bussid,date(from_unixtime(ct)) date8 from t2) b on      a.period_id = b.bussid where      datediff(b.date8, date(from_unixtime(a.starttime))) >= 0;
ERROR 2013 (HY000): Lost connection to MySQL server during query
  1. What version of TiDB are you using (tidb-server -V or run select tidb_version(); on TiDB)?
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                                                                                         |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v3.0.0-beta.1-109-g7666f688b
Git Commit Hash: 7666f688b3d91ea84fa6b0a20c2e784fff3c8945
Git Branch: master
UTC Build Time: 2019-04-15 12:14:36
GoVersion: go version go1.11.3 darwin/amd64
Race Enabled: false
TiKV Min Version: 2.1.0-alpha.1-ff3dd160846b7d1aed9079c389fc188f7f5ea13e
Check Table Before Drop: false |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
@qw4990 qw4990 added type/bug The issue is confirmed as a bug. sig/planner SIG: Planner labels Apr 15, 2019
@qw4990 qw4990 self-assigned this Apr 15, 2019
@qw4990
Copy link
Contributor Author

qw4990 commented Apr 15, 2019

The root reason is that we check const inappropriately.
In foldConstant, we check if a expression is constant by simply converting it to *Constant, and if it succeed, we think it is constant.

In this case, we will fold datediff(b.date8, date(from_unixtime(a.starttime))).
Because date(from_unixtime(a.starttime)) is not constant, we will replace it with const One, and then call NewFunctionInternal.
NewFunctionInternal firstly call dateDiffFunctionClass.getFunction which will wrap the argument One to a scalar function CastAsDateTime.
And NewFunctionInternal then call foldConstant to fold the function expression created by dateDiffFunctionClass.
Since the constant argument One has been wrapped to CastAsDateTime, it is not constant in foldConstant and then NewFunctionInternal will be called again...

To fix this problem, we can change the way of checking const expression by using ConstItem() introduced by #10004.

What shown below is a part of stack caused by this problem.

 /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:37 +0x35 fp=0xc028002b18 sp=0xc028002ae0 pc=0x49989f5
github.com/pingcap/tidb/expression.newFunctionImpl(0x54f6140, 0xc006d6fc20, 0xc6e2a901, 0xc00033a1a5, 0x8, 0xc00f5af0e0, 0xc0127daa80, 0x2, 0x2, 0x0, ...)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:100 +0x340 fp=0xc028002be8 sp=0xc028002b18 pc=0x49b61b0
github.com/pingcap/tidb/expression.NewFunction(0x54f6140, 0xc006d6fc20, 0xc00033a1a5, 0x8, 0xc00f5aeae0, 0xc0127daa80, 0x2, 0x2, 0xc028002cd0, 0x4046057, ...)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:107 +0x8f fp=0xc028002c60 sp=0xc028002be8 pc=0x49b64df
github.com/pingcap/tidb/expression.NewFunctionInternal(0x54f6140, 0xc006d6fc20, 0xc00033a1a5, 0x8, 0xc00f5aeae0, 0xc0127daa80, 0x2, 0x2, 0x0, 0x0)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:117 +0x8d fp=0xc028002ce0 sp=0xc028002c60 pc=0x49b669d
github.com/pingcap/tidb/expression.foldConstant(0x54f6bc0, 0xc00f584ff0, 0x5196ee0, 0xc00033a101, 0xc00f584ff0)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:120 +0xa9c fp=0xc028003258 sp=0xc028002ce0 pc=0x4999dcc
github.com/pingcap/tidb/expression.FoldConstant(0x54f6bc0, 0xc00f584ff0, 0xc00033a1a5, 0x8)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:37 +0x35 fp=0xc028003290 sp=0xc028003258 pc=0x49989f5
github.com/pingcap/tidb/expression.newFunctionImpl(0x54f6140, 0xc006d6fc20, 0x1, 0xc00033a1a5, 0x8, 0xc00f5aeae0, 0xc0127da980, 0x2, 0x2, 0x0, ...)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:100 +0x340 fp=0xc028003360 sp=0xc028003290 pc=0x49b61b0
github.com/pingcap/tidb/expression.NewFunction(0x54f6140, 0xc006d6fc20, 0xc00033a1a5, 0x8, 0xc00f5ae4e0, 0xc0127da980, 0x2, 0x2, 0xc028003448, 0x4046057, ...)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:107 +0x8f fp=0xc0280033d8 sp=0xc028003360 pc=0x49b64df
github.com/pingcap/tidb/expression.NewFunctionInternal(0x54f6140, 0xc006d6fc20, 0xc00033a1a5, 0x8, 0xc00f5ae4e0, 0xc0127da980, 0x2, 0x2, 0x0, 0x0)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:117 +0x8d fp=0xc028003458 sp=0xc0280033d8 pc=0x49b669d
github.com/pingcap/tidb/expression.foldConstant(0x54f6bc0, 0xc00f584af0, 0x5196ee0, 0xc00033a101, 0xc00f584af0)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:120 +0xa9c fp=0xc0280039d0 sp=0xc028003458 pc=0x4999dcc
github.com/pingcap/tidb/expression.FoldConstant(0x54f6bc0, 0xc00f584af0, 0xc00033a1a5, 0x8)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:37 +0x35 fp=0xc028003a08 sp=0xc0280039d0 pc=0x49989f5
github.com/pingcap/tidb/expression.newFunctionImpl(0x54f6140, 0xc006d6fc20, 0xc6e20c01, 0xc00033a1a5, 0x8, 0xc00f5ae4e0, 0xc0127da860, 0x2, 0x2, 0x0, ...)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:100 +0x340 fp=0xc028003ad8 sp=0xc028003a08 pc=0x49b61b0
github.com/pingcap/tidb/expression.NewFunction(0x54f6140, 0xc006d6fc20, 0xc00033a1a5, 0x8, 0xc00f5f3f80, 0xc0127da860, 0x2, 0x2, 0xc028003bc0, 0x4046057, ...)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:107 +0x8f fp=0xc028003b50 sp=0xc028003ad8 pc=0x49b64df
github.com/pingcap/tidb/expression.NewFunctionInternal(0x54f6140, 0xc006d6fc20, 0xc00033a1a5, 0x8, 0xc00f5f3f80, 0xc0127da860, 0x2, 0x2, 0x0, 0x0)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/scalar_function.go:117 +0x8d fp=0xc028003bd0 sp=0xc028003b50 pc=0x49b669d
github.com/pingcap/tidb/expression.foldConstant(0x54f6bc0, 0xc00f5845f0, 0x5196ee0, 0xc00033a101, 0xc00f5845f0)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:120 +0xa9c fp=0xc028004148 sp=0xc028003bd0 pc=0x4999dcc
github.com/pingcap/tidb/expression.FoldConstant(0x54f6bc0, 0xc00f5845f0, 0xc00033a1a5, 0x8)
        /Users/zhangyuanjia/Workspace/go/src/github.com/pingcap/tidb/expression/constant_fold.go:37 +0x35 fp=0xc028004180 sp=0xc028004148 pc=0x49989f5
github.com/pingcap/tidb/expression.newFunctionImpl(0x54f6140, 0xc006d6fc20, 0xc6e1de01, 0xc00033a1a5, 0x8, 0xc00f5f3f80, 0xc0127da760, 0x2, 0x2, 0x0, ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

1 participant