Skip to content

Commit

Permalink
Avoid deserializing value on FK check (#6957)
Browse files Browse the repository at this point in the history
* Avoid deserializing key for FK check

* use ReferenceChecker interface
  • Loading branch information
max-hoffman authored Nov 7, 2023
1 parent 1d17128 commit 9fc5e2e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ require (
github.com/cespare/xxhash v1.1.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.17.1-0.20231105071709-422c3fee20f6
github.com/dolthub/go-mysql-server v0.17.1-0.20231107171708-b0d61a50e286
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
Expand Down
6 changes: 4 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e h1:kPsT4a47cw1+y/N5SSCkma7FhAPw7KeGmD6c9PBZW9Y=
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.17.1-0.20231105071709-422c3fee20f6 h1:gWdydyqAiA0U51NUfdRGYeUiHkRM/OWM0KmNjUF3Wvk=
github.com/dolthub/go-mysql-server v0.17.1-0.20231105071709-422c3fee20f6/go.mod h1:YKNZpEARxfNl5LUyIZ8oHEoMDM7DzjnPlhl9cj89QBg=
github.com/dolthub/go-mysql-server v0.17.1-0.20231102000456-6985ecd1f59f h1:klP1BLlTLwnFSRAPEa3oQRwuEjnpqedWa1KdGDjmT2E=
github.com/dolthub/go-mysql-server v0.17.1-0.20231102000456-6985ecd1f59f/go.mod h1:YKNZpEARxfNl5LUyIZ8oHEoMDM7DzjnPlhl9cj89QBg=
github.com/dolthub/go-mysql-server v0.17.1-0.20231107171708-b0d61a50e286 h1:p8sxDMAgUlzKPJdO6O4PlMPtTShAAcEAeNgRrCgFOaQ=
github.com/dolthub/go-mysql-server v0.17.1-0.20231107171708-b0d61a50e286/go.mod h1:YKNZpEARxfNl5LUyIZ8oHEoMDM7DzjnPlhl9cj89QBg=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488 h1:0HHu0GWJH0N6a6keStrHhUAK5/o9LVfkh44pvsV4514=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488/go.mod h1:ehexgi1mPxRTk0Mok/pADALuHbvATulTh6gzr7NzZto=
github.com/dolthub/jsonpath v0.0.2-0.20230525180605-8dc13778fd72 h1:NfWmngMi1CYUWU4Ix8wM+USEhjc+mhPlT9JUR/anvbQ=
Expand Down
33 changes: 21 additions & 12 deletions go/libraries/doltcore/sqle/writer/prolly_fk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import (
)

type prollyFkIndexer struct {
writer *prollyTableWriter
index index.DoltIndex
pRange prolly.Range
writer *prollyTableWriter
index index.DoltIndex
pRange prolly.Range
refCheck bool
}

var _ sql.Table = (*prollyFkIndexer)(nil)
var _ sql.IndexedTable = (*prollyFkIndexer)(nil)
var _ sql.ReferenceChecker = (*prollyFkIndexer)(nil)

// Name implements the interface sql.Table.
func (n *prollyFkIndexer) Name() string {
Expand All @@ -49,6 +51,11 @@ func (n *prollyFkIndexer) Schema() sql.Schema {
return n.writer.sqlSch
}

func (n *prollyFkIndexer) SetReferenceCheck() error {
n.refCheck = true
return nil
}

// Collation implements the interface sql.Table.
func (n *prollyFkIndexer) Collation() sql.CollationID {
return sql.CollationID(n.writer.sch.GetCollation())
Expand Down Expand Up @@ -84,23 +91,19 @@ func (n *prollyFkIndexer) PartitionRows(ctx *sql.Context, _ sql.Partition) (sql.
pkToIdxMap[i] = j
}
}

rangeIter, err := idxWriter.IterRange(ctx, n.pRange)
if err != nil {
return nil, err
}
if primary, ok := n.writer.primary.(prollyIndexWriter); ok {
rangeIter, err := idxWriter.IterRange(ctx, n.pRange)
if err != nil {
return nil, err
}
return &prollyFkPkRowIter{
rangeIter: rangeIter,
pkToIdxMap: pkToIdxMap,
primary: primary,
sqlSch: n.writer.sqlSch,
refCheck: n.refCheck,
}, nil
} else {
rangeIter, err := idxWriter.IterRange(ctx, n.pRange)
if err != nil {
return nil, err
}
return &prollyFkKeylessRowIter{
rangeIter: rangeIter,
primary: n.writer.primary.(prollyKeylessWriter),
Expand All @@ -115,6 +118,7 @@ type prollyFkPkRowIter struct {
pkToIdxMap val.OrdinalMapping
primary prollyIndexWriter
sqlSch sql.Schema
refCheck bool
}

var _ sql.RowIter = prollyFkPkRowIter{}
Expand Down Expand Up @@ -149,6 +153,11 @@ func (iter prollyFkPkRowIter) Next(ctx *sql.Context) (sql.Row, error) {
continue // referential integrity broken
}

if iter.refCheck {
// no need to deserialize
return nil, nil
}

nextRow := make(sql.Row, len(iter.primary.keyMap)+len(iter.primary.valMap))
for from := range iter.primary.keyMap {
to := iter.primary.keyMap.MapOrdinal(from)
Expand Down

0 comments on commit 9fc5e2e

Please sign in to comment.