github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/row/fk_spans.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package row 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/roachpb" 15 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 16 "github.com/cockroachdb/cockroach/pkg/sql/span" 17 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 18 ) 19 20 // spanForValues produce access spans for a single FK constraint and a 21 // tuple of columns. 22 func (f fkExistenceCheckBaseHelper) spanForValues(values tree.Datums) (roachpb.Span, error) { 23 if values == nil { 24 key := roachpb.Key(f.spanBuilder.KeyPrefix) 25 return roachpb.Span{Key: key, EndKey: key.PrefixEnd()}, nil 26 } 27 return FKCheckSpan(f.spanBuilder, values, f.ids, f.prefixLen) 28 } 29 30 // FKCheckSpan returns a span that can be scanned to ascertain existence of a 31 // specific row in a given index. 32 func FKCheckSpan( 33 s *span.Builder, values []tree.Datum, colMap map[sqlbase.ColumnID]int, numCols int, 34 ) (roachpb.Span, error) { 35 span, containsNull, err := s.SpanFromDatumRow(values, numCols, colMap) 36 if err != nil { 37 return roachpb.Span{}, err 38 } 39 // If it is safe to split this lookup into multiple families, generate a point lookup for 40 // family 0. Because we are just checking for existence, we only need family 0. 41 if s.CanSplitSpanIntoSeparateFamilies(1 /* numNeededFamilies */, numCols, containsNull) { 42 return s.SpanToPointSpan(span, 0), nil 43 } 44 return span, nil 45 }