github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/create_index.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plan
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	errors "gopkg.in/src-d/go-errors.v1"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  )
    25  
    26  var (
    27  	// ErrNotIndexable is returned when the table is not indexable.
    28  	ErrNotIndexable = errors.NewKind("the table is not indexable")
    29  
    30  	// ErrInvalidIndexDriver is returned when the index driver can't be found.
    31  	ErrInvalidIndexDriver = errors.NewKind("invalid driver index %q")
    32  
    33  	// ErrExprTypeNotIndexable is returned when the expression type cannot be
    34  	// indexed, such as BLOB or JSON.
    35  	ErrExprTypeNotIndexable = errors.NewKind("expression %q with type %s cannot be indexed")
    36  )
    37  
    38  // CreateIndex is a node to create an index.
    39  type CreateIndex struct {
    40  	Name            string
    41  	Table           sql.Node
    42  	Exprs           []sql.Expression
    43  	Driver          string
    44  	Config          map[string]string
    45  	Catalog         sql.Catalog
    46  	CurrentDatabase string
    47  }
    48  
    49  var _ sql.Node = (*CreateIndex)(nil)
    50  var _ sql.Databaseable = (*CreateIndex)(nil)
    51  var _ sql.CollationCoercible = (*CreateIndex)(nil)
    52  
    53  // NewCreateIndex creates a new CreateIndex node.
    54  func NewCreateIndex(
    55  	name string,
    56  	table sql.Node,
    57  	exprs []sql.Expression,
    58  	driver string,
    59  	config map[string]string,
    60  ) *CreateIndex {
    61  	return &CreateIndex{
    62  		Name:   name,
    63  		Table:  table,
    64  		Exprs:  exprs,
    65  		Driver: driver,
    66  		Config: config,
    67  	}
    68  }
    69  
    70  func (c *CreateIndex) Database() string { return c.CurrentDatabase }
    71  
    72  // Children implements the Node interface.
    73  func (c *CreateIndex) Children() []sql.Node { return []sql.Node{c.Table} }
    74  
    75  // Resolved implements the Node interface.
    76  func (c *CreateIndex) Resolved() bool {
    77  	if !c.Table.Resolved() {
    78  		return false
    79  	}
    80  
    81  	for _, e := range c.Exprs {
    82  		if !e.Resolved() {
    83  			return false
    84  		}
    85  	}
    86  
    87  	return true
    88  }
    89  
    90  func (c *CreateIndex) IsReadOnly() bool {
    91  	return false
    92  }
    93  
    94  // Schema implements the Node interface.
    95  func (c *CreateIndex) Schema() sql.Schema { return nil }
    96  
    97  func (c *CreateIndex) String() string {
    98  	var exprs = make([]string, len(c.Exprs))
    99  	for i, e := range c.Exprs {
   100  		exprs[i] = e.String()
   101  	}
   102  
   103  	pr := sql.NewTreePrinter()
   104  	_ = pr.WriteNode("CreateIndex(%s)", c.Name)
   105  	_ = pr.WriteChildren(
   106  		fmt.Sprintf("USING %s", c.Driver),
   107  		fmt.Sprintf("Expressions (%s)", strings.Join(exprs, ", ")),
   108  		c.Table.String(),
   109  	)
   110  	return pr.String()
   111  }
   112  
   113  // Expressions implements the Expressioner interface.
   114  func (c *CreateIndex) Expressions() []sql.Expression {
   115  	return c.Exprs
   116  }
   117  
   118  // WithExpressions implements the Expressioner interface.
   119  func (c *CreateIndex) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
   120  	if len(exprs) != len(c.Exprs) {
   121  		return nil, sql.ErrInvalidChildrenNumber.New(c, len(exprs), len(c.Exprs))
   122  	}
   123  
   124  	nc := *c
   125  	nc.Exprs = exprs
   126  	return &nc, nil
   127  }
   128  
   129  // WithChildren implements the Node interface.
   130  func (c *CreateIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
   131  	if len(children) != 1 {
   132  		return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 1)
   133  	}
   134  
   135  	nc := *c
   136  	nc.Table = children[0]
   137  	return &nc, nil
   138  }
   139  
   140  // CheckPrivileges implements the interface sql.Node.
   141  func (c *CreateIndex) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   142  	subject := sql.PrivilegeCheckSubject{
   143  		Database: CheckPrivilegeNameForDatabase(GetDatabase(c.Table)),
   144  		Table:    getTableName(c.Table),
   145  	}
   146  
   147  	return opChecker.UserHasPrivileges(ctx,
   148  		sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Index))
   149  }
   150  
   151  // CollationCoercibility implements the interface sql.CollationCoercible.
   152  func (*CreateIndex) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   153  	return sql.Collation_binary, 7
   154  }