Skip to content

Commit

Permalink
hint: fix panic when query block not found in prepare statement (#46818
Browse files Browse the repository at this point in the history
…) (#46842)

close #46817
  • Loading branch information
ti-chi-bot authored Oct 18, 2023
1 parent 60b4dcc commit 288af37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions util/hint/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,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)
Expand Down

0 comments on commit 288af37

Please sign in to comment.