Skip to content

Commit

Permalink
[parser] parser: support index advisor (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrNewt authored and ti-chi-bot committed Oct 9, 2021
1 parent de42910 commit 07751a6
Show file tree
Hide file tree
Showing 6 changed files with 8,318 additions and 7,960 deletions.
80 changes: 80 additions & 0 deletions parser/ast/advisor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ast

import (
. "github.com/pingcap/parser/format"
)

var _ StmtNode = &IndexAdviseStmt{}

// IndexAdviseStmt is used to advise indexes
type IndexAdviseStmt struct {
stmtNode

IsLocal bool
Path string
MaxMinutes uint64
MaxIndexNum *MaxIndexNumClause
LinesInfo *LinesClause
}

// Restore implements Node Accept interface.
func (n *IndexAdviseStmt) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("INDEX ADVISE ")
if n.IsLocal {
ctx.WriteKeyWord("LOCAL ")
}
ctx.WriteKeyWord("INFILE ")
ctx.WriteString(n.Path)
if n.MaxMinutes != UnspecifiedSize {
ctx.WriteKeyWord(" MAX_MINUTES ")
ctx.WritePlainf("%d", n.MaxMinutes)
}
if n.MaxIndexNum != nil {
n.MaxIndexNum.Restore(ctx)
}
n.LinesInfo.Restore(ctx)
return nil
}

// Accept implements Node Accept interface.
func (n *IndexAdviseStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*IndexAdviseStmt)
return v.Leave(n)
}

// MaxIndexNumClause represents 'maximum number of indexes' clause in index advise statement.
type MaxIndexNumClause struct {
PerTable uint64
PerDB uint64
}

// Restore for max index num clause
func (n *MaxIndexNumClause) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord(" MAX_IDXNUM")
if n.PerTable != UnspecifiedSize {
ctx.WriteKeyWord(" PER_TABLE ")
ctx.WritePlainf("%d", n.PerTable)
}
if n.PerDB != UnspecifiedSize {
ctx.WriteKeyWord(" PER_DB ")
ctx.WritePlainf("%d", n.PerDB)
}
return nil
}
7 changes: 7 additions & 0 deletions parser/ast/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

package ast

import "math"

// UnspecifiedSize is unspecified size.
const (
UnspecifiedSize = math.MaxUint64
)

// IsReadOnly checks whether the input ast is readOnly.
func IsReadOnly(node Node) bool {
switch st := node.(type) {
Expand Down
5 changes: 5 additions & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var tokenMap = map[string]int{
"ACTION": action,
"ADD": add,
"ADDDATE": addDate,
"ADVISE": advise,
"ADMIN": admin,
"AFTER": after,
"AGAINST": against,
Expand Down Expand Up @@ -393,6 +394,8 @@ var tokenMap = map[string]int{
"MAX": max,
"MAX_CONNECTIONS_PER_HOUR": maxConnectionsPerHour,
"MAX_EXECUTION_TIME": maxExecutionTime,
"MAX_IDXNUM": max_idxnum,
"MAX_MINUTES": max_minutes,
"MAX_QUERIES_PER_HOUR": maxQueriesPerHour,
"MAX_ROWS": maxRows,
"MAX_UPDATES_PER_HOUR": maxUpdatesPerHour,
Expand Down Expand Up @@ -461,6 +464,8 @@ var tokenMap = map[string]int{
"PARTITIONS": partitions,
"PASSWORD": password,
"PESSIMISTIC": pessimistic,
"PER_TABLE": per_table,
"PER_DB": per_db,
"PLUGINS": plugins,
"POSITION": position,
"PRECEDING": preceding,
Expand Down
Loading

0 comments on commit 07751a6

Please sign in to comment.