From 05f1dd5ed0b5f3c70c4c66a0d4ba753590204a8d Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 28 Sep 2023 00:54:19 +0800 Subject: [PATCH] hint: fix panic when query block not found in prepare statement (#46818) (#46843) close pingcap/tidb#46817 --- executor/prepared_test.go | 5 +++++ util/hint/hint_processor.go | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/executor/prepared_test.go b/executor/prepared_test.go index 9914827aa5c01..7622e6e99649a 100644 --- a/executor/prepared_test.go +++ b/executor/prepared_test.go @@ -96,6 +96,11 @@ func TestPreparedStmtWithHint(t *testing.T) { go dom.ExpensiveQueryHandle().SetSessionManager(sv).Run() tk.MustExec("prepare stmt from \"select /*+ max_execution_time(100) */ sleep(10)\"") tk.MustQuery("execute stmt").Check(testkit.Rows("1")) + + // see https://github.com/pingcap/tidb/issues/46817 + tk.MustExec("use test") + tk.MustExec("create table if not exists t (i int)") + tk.MustExec("prepare stmt from 'with a as (select /*+ qb_name(qb1) */ * from t) select /*+ leading(@qb1)*/ * from a;'") } func TestPreparedNullParam(t *testing.T) { diff --git a/util/hint/hint_processor.go b/util/hint/hint_processor.go index 0dc459ab9f67d..7164390cdad69 100644 --- a/util/hint/hint_processor.go +++ b/util/hint/hint_processor.go @@ -597,8 +597,10 @@ func (p *BlockHintProcessor) GetCurrentStmtHints(hints []*ast.TableOptimizerHint } offset := p.GetHintOffset(hint.QBName, currentOffset) if offset < 0 || !p.checkTableQBName(hint.Tables) { - hintStr := RestoreTableOptimizerHint(hint) - p.Ctx.GetSessionVars().StmtCtx.AppendWarning(fmt.Errorf("Hint %s is ignored due to unknown query block name", hintStr)) + if p.Ctx != nil { + hintStr := RestoreTableOptimizerHint(hint) + p.Ctx.GetSessionVars().StmtCtx.AppendWarning(fmt.Errorf("Hint %s is ignored due to unknown query block name", hintStr)) + } continue } p.QbHints[offset] = append(p.QbHints[offset], hint)