github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/merge/violations_prolly.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 merge
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  
    23  	"github.com/dolthub/dolt/go/store/prolly"
    24  	"github.com/dolthub/dolt/go/store/prolly/tree"
    25  	"github.com/dolthub/dolt/go/store/val"
    26  )
    27  
    28  func NextConstraintViolation(ctx context.Context, itr prolly.ArtifactIter, kd, vd val.TupleDesc, ns tree.NodeStore) (violationType uint64, key sql.Row, value sql.Row, err error) {
    29  	art, err := itr.Next(ctx)
    30  	if err != nil {
    31  		return
    32  	}
    33  
    34  	key = make(sql.Row, kd.Count())
    35  	for i := 0; i < kd.Count(); i++ {
    36  		key[i], err = tree.GetField(ctx, kd, i, art.SourceKey, ns)
    37  		if err != nil {
    38  			return
    39  		}
    40  	}
    41  
    42  	var meta prolly.ConstraintViolationMeta
    43  	err = json.Unmarshal(art.Metadata, &meta)
    44  	if err != nil {
    45  		return
    46  	}
    47  
    48  	value = make(sql.Row, vd.Count())
    49  	for i := 0; i < vd.Count(); i++ {
    50  		value[i], err = tree.GetField(ctx, vd, i, meta.Value, ns)
    51  		if err != nil {
    52  			return
    53  		}
    54  	}
    55  
    56  	return MapCVType(art.ArtType), key, value, nil
    57  }
    58  
    59  func MapCVType(artifactType prolly.ArtifactType) (outType uint64) {
    60  	switch artifactType {
    61  	case prolly.ArtifactTypeForeignKeyViol:
    62  		outType = uint64(CvType_ForeignKey)
    63  	case prolly.ArtifactTypeUniqueKeyViol:
    64  		outType = uint64(CvType_UniqueIndex)
    65  	case prolly.ArtifactTypeChkConsViol:
    66  		outType = uint64(CvType_CheckConstraint)
    67  	case prolly.ArtifactTypeNullViol:
    68  		outType = uint64(CvType_NotNull)
    69  	default:
    70  		panic("unhandled cv type")
    71  	}
    72  	return
    73  }
    74  
    75  func UnmapCVType(in CvType) (out prolly.ArtifactType) {
    76  	switch in {
    77  	case CvType_ForeignKey:
    78  		out = prolly.ArtifactTypeForeignKeyViol
    79  	case CvType_UniqueIndex:
    80  		out = prolly.ArtifactTypeUniqueKeyViol
    81  	case CvType_CheckConstraint:
    82  		out = prolly.ArtifactTypeChkConsViol
    83  	case CvType_NotNull:
    84  		out = prolly.ArtifactTypeNullViol
    85  	default:
    86  		panic("unhandled cv type")
    87  	}
    88  	return
    89  }