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

     1  // Copyright 2023 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 rowexec
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/plan"
    23  )
    24  
    25  // schemaPositionDeleter contains a sql.RowDeleter and the start (inclusive) and end (exclusive) position
    26  // within a schema that indicate the portion of the schema that is associated with this specific deleter.
    27  type schemaPositionDeleter struct {
    28  	deleter     sql.RowDeleter
    29  	schemaStart int
    30  	schemaEnd   int
    31  }
    32  
    33  // findSourcePosition searches the specified |schema| for the first group of columns whose source is |name|,
    34  // and returns the start position of that source in the schema (inclusive) and the end position (exclusive).
    35  // If any problems were an encountered, such as not finding any columns from the specified source name,
    36  // an error is returned.
    37  func findSourcePosition(schema sql.Schema, name string) (uint, uint, error) {
    38  	foundStart := false
    39  	name = strings.ToLower(name)
    40  	var start uint
    41  	for i, col := range schema {
    42  		if strings.ToLower(col.Source) == name {
    43  			if !foundStart {
    44  				start = uint(i)
    45  				foundStart = true
    46  			}
    47  		} else {
    48  			if foundStart {
    49  				return start, uint(i), nil
    50  			}
    51  		}
    52  	}
    53  	if foundStart {
    54  		return start, uint(len(schema)), nil
    55  	}
    56  
    57  	return 0, 0, fmt.Errorf("unable to find any columns in schema from source %q", name)
    58  }
    59  
    60  // deleteIter executes the DELETE FROM logic to delete rows from tables as they flow through the iterator. For every
    61  // table the deleteIter needs to delete rows from, it needs a schemaPositionDeleter that provides the RowDeleter
    62  // interface as well as start and end position for that table's full row in the row this iterator consumes from its
    63  // child. For simple DELETE FROM statements deleting from a single table, this will likely be the full row contents,
    64  // but in more complex scenarios when there are columns contributed by outer scopes and for DELETE FROM JOIN statements
    65  // the child iterator will return a row that is composed of rows from multiple table sources.
    66  type deleteIter struct {
    67  	deleters  []schemaPositionDeleter
    68  	schema    sql.Schema
    69  	childIter sql.RowIter
    70  	closed    bool
    71  }
    72  
    73  func (d *deleteIter) Next(ctx *sql.Context) (sql.Row, error) {
    74  	row, err := d.childIter.Next(ctx)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	select {
    79  	case <-ctx.Done():
    80  		return nil, ctx.Err()
    81  	default:
    82  	}
    83  
    84  	// For each target table from which we are deleting rows, reduce the row from our child iterator to just
    85  	// the columns that are part of that target table. This means looking at the position in the schema for
    86  	// the target table and also removing any prepended columns contributed by outer scopes.
    87  	fullSchemaLength := len(d.schema)
    88  	rowLength := len(row)
    89  	for _, deleter := range d.deleters {
    90  		schemaLength := deleter.schemaEnd - deleter.schemaStart
    91  		subSlice := row
    92  		if schemaLength < rowLength {
    93  			subSlice = row[(rowLength - fullSchemaLength + deleter.schemaStart):(rowLength - fullSchemaLength + deleter.schemaEnd)]
    94  		}
    95  		err = deleter.deleter.Delete(ctx, subSlice)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  	}
   100  
   101  	return row, nil
   102  }
   103  
   104  func (d *deleteIter) Close(ctx *sql.Context) error {
   105  	if !d.closed {
   106  		d.closed = true
   107  		var firstErr error
   108  		// Make sure we close all the deleters and the childIter, and track the first
   109  		// error seen so we can return it after safely closing all resources.
   110  		for _, deleter := range d.deleters {
   111  			err := deleter.deleter.Close(ctx)
   112  			if err != nil && firstErr == nil {
   113  				firstErr = err
   114  			}
   115  		}
   116  		err := d.childIter.Close(ctx)
   117  
   118  		if firstErr != nil {
   119  			return firstErr
   120  		} else {
   121  			return err
   122  		}
   123  	}
   124  	return nil
   125  }
   126  
   127  func newDeleteIter(childIter sql.RowIter, schema sql.Schema, deleters ...schemaPositionDeleter) sql.RowIter {
   128  	openerClosers := make([]sql.EditOpenerCloser, len(deleters))
   129  	for i, ds := range deleters {
   130  		openerClosers[i] = ds.deleter
   131  	}
   132  	return plan.NewTableEditorIter(&deleteIter{
   133  		deleters:  deleters,
   134  		childIter: childIter,
   135  		schema:    schema,
   136  	}, openerClosers...)
   137  }