Skip to content

Commit

Permalink
schema validator impl (#7544)
Browse files Browse the repository at this point in the history
* Database implements sql.SchemaValidator

* [ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/update.sh

* fix bats

* bump

---------

Co-authored-by: max-hoffman <[email protected]>
  • Loading branch information
max-hoffman and max-hoffman authored Feb 28, 2024
1 parent 41714c2 commit 1eab16f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,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.20240227214442-0f1cfb730e2d
github.com/dolthub/go-mysql-server v0.17.1-0.20240228012714-ac39104191e7
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
github.com/google/go-github/v57 v57.0.0
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ 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.20240227214442-0f1cfb730e2d h1:gVSwMnAc92+c73NBPYjOmK4iDHdn8/VEl5NBuCOOPGw=
github.com/dolthub/go-mysql-server v0.17.1-0.20240227214442-0f1cfb730e2d/go.mod h1:RjyLbJet/Pzosl5lB6sSx37vee7t66B7+PW05uGCRgM=
github.com/dolthub/go-mysql-server v0.17.1-0.20240228012714-ac39104191e7 h1:lYm5JTzTWfXMLpnFF8nmrOKueKF0PdE5O8pZM002viE=
github.com/dolthub/go-mysql-server v0.17.1-0.20240228012714-ac39104191e7/go.mod h1:RjyLbJet/Pzosl5lB6sSx37vee7t66B7+PW05uGCRgM=
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.20240227200619-19675ab05c71 h1:bMGS25NWAGTEtT5tOBsCuCrlYnLRKpbJVJkDbrTRhwQ=
Expand Down
13 changes: 3 additions & 10 deletions go/libraries/doltcore/schema/schema_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/analyzer/analyzererrors"
gmstypes "github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/vitess/go/vt/proto/query"

Expand Down Expand Up @@ -182,12 +181,6 @@ func ValidateForInsert(allCols *ColCollection) error {
c.TypeInfo.ToSqlType()
}

if rowLen := MaxRowStorageSize(allCols); rowLen > int64(val.MaxTupleDataSize) {
// |val.MaxTupleDataSize| is less than |types.MaxRowLength| to account for
// serial message metadata
return analyzererrors.ErrInvalidRowLength.New(val.MaxTupleDataSize, rowLen)
}

if !seenPkCol && !FeatureFlagKeylessSchema {
return ErrNoPrimaryKeyColumns
}
Expand Down Expand Up @@ -217,10 +210,10 @@ func ValidateForInsert(allCols *ColCollection) error {
}

// MaxRowStorageSize returns the storage length for Dolt types.
func MaxRowStorageSize(cols *ColCollection) int64 {
func MaxRowStorageSize(sch sql.Schema) int64 {
var numBytesPerRow int64 = 0
for _, col := range cols.cols {
switch n := col.TypeInfo.ToSqlType().(type) {
for _, col := range sch {
switch n := col.Type.(type) {
case sql.NumberType:
numBytesPerRow += 8
case sql.StringType:
Expand Down
12 changes: 12 additions & 0 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/analyzer"
"github.com/dolthub/go-mysql-server/sql/analyzer/analyzererrors"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/fulltext"
"github.com/dolthub/go-mysql-server/sql/plan"
Expand All @@ -50,6 +51,7 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/libraries/utils/concurrentmap"
"github.com/dolthub/dolt/go/store/hash"
"github.com/dolthub/dolt/go/store/val"
)

var ErrInvalidTableName = errors.NewKind("Invalid table name %s.")
Expand Down Expand Up @@ -88,6 +90,7 @@ var _ sql.EventDatabase = Database{}
var _ sql.AliasedDatabase = Database{}
var _ fulltext.Database = Database{}
var _ rebase.RebasePlanDatabase = Database{}
var _ sql.SchemaValidator = Database{}

type ReadOnlyDatabase struct {
Database
Expand Down Expand Up @@ -123,6 +126,15 @@ func (db Database) WithBranchRevision(requestedName string, branchSpec dsess.Ses
return db, nil
}

func (db Database) ValidateSchema(sch sql.Schema) error {
if rowLen := schema.MaxRowStorageSize(sch); rowLen > int64(val.MaxTupleDataSize) {
// |val.MaxTupleDataSize| is less than |types.MaxRowLength| to account for
// serial message metadata
return analyzererrors.ErrInvalidRowLength.New(val.MaxTupleDataSize, rowLen)
}
return nil
}

// Revision implements dsess.RevisionDatabase
func (db Database) Revision() string {
return db.revision
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bats/docs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ teardown() {
[[ "$output" =~ "Invalid table name dolt_docs" ]] || false

run dolt sql -q "CREATE TABLE dolt_docs (
doc_name varchar(16383) NOT NULL,
doc_name varchar(1023) NOT NULL,
doc_text longtext,
PRIMARY KEY (doc_name)
);"
Expand Down

0 comments on commit 1eab16f

Please sign in to comment.