github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/writer/noms_fk_indexer.go (about)

     1  // Copyright 2022 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 writer
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/table/typed/noms"
    25  )
    26  
    27  type nomsFkIndexer struct {
    28  	writer  *nomsTableWriter
    29  	idxName string
    30  	idxSch  schema.Schema
    31  	nrr     *noms.ReadRange
    32  }
    33  
    34  var _ sql.Table = (*nomsFkIndexer)(nil)
    35  var _ sql.IndexedTable = (*nomsFkIndexer)(nil)
    36  
    37  func (n *nomsFkIndexer) Name() string {
    38  	return n.writer.tableName
    39  }
    40  
    41  func (n *nomsFkIndexer) String() string {
    42  	return n.writer.tableName
    43  }
    44  
    45  func (n *nomsFkIndexer) Schema() sql.Schema {
    46  	return n.writer.sqlSch
    47  }
    48  
    49  func (n *nomsFkIndexer) Collation() sql.CollationID {
    50  	return sql.CollationID(n.writer.sch.GetCollation())
    51  }
    52  
    53  func (n *nomsFkIndexer) LookupPartitions(ctx *sql.Context, lookup sql.IndexLookup) (sql.PartitionIter, error) {
    54  	nrr, err := index.NomsRangesFromIndexLookup(ctx, lookup)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	n.nrr = nrr[0]
    59  	return sql.PartitionsToPartitionIter(fkDummyPartition{}), nil
    60  }
    61  
    62  func (n *nomsFkIndexer) Partitions(ctx *sql.Context) (sql.PartitionIter, error) {
    63  	return sql.PartitionsToPartitionIter(fkDummyPartition{}), nil
    64  }
    65  
    66  func (n *nomsFkIndexer) PartitionRows(ctx *sql.Context, partition sql.Partition) (sql.RowIter, error) {
    67  	dRows, err := n.writer.tableEditor.GetIndexedRows(ctx, n.nrr.Start, n.idxName, n.idxSch)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	rows := make([]sql.Row, len(dRows))
    72  	for i, dRow := range dRows {
    73  		rows[i], err = sqlutil.DoltRowToSqlRow(dRow, n.writer.sch)
    74  		if err != nil {
    75  			return nil, err
    76  		}
    77  	}
    78  	return sql.RowsToRowIter(rows...), err
    79  }
    80  
    81  // fkDummyPartition is used to return a partition that will be ignored, as a foreign key indexer does not handle
    82  // partitioning, however a partition must be used in order to retrieve rows.
    83  type fkDummyPartition struct{}
    84  
    85  var _ sql.Partition = fkDummyPartition{}
    86  
    87  // Key implements the interface sql.Partition.
    88  func (n fkDummyPartition) Key() []byte {
    89  	return nil
    90  }