diff --git a/executor/prepared_test.go b/executor/prepared_test.go index 588cc93d49d35..7a9772a52d231 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)