Skip to content

Commit

Permalink
Merge pull request #727 from dolthub/zachmu/more-testing
Browse files Browse the repository at this point in the history
Enabled `use mydb/main` without quoting and implemented the IF function
  • Loading branch information
zachmu authored Sep 20, 2024
2 parents e54956e + 3f710c4 commit 6da1bf1
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 113 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ require (
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240827111219-e4bb9ca3442d
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
github.com/dolthub/go-mysql-server v0.18.2-0.20240919175427-5055c7114c8b
github.com/dolthub/go-mysql-server v0.18.2-0.20240920224603-6f1a8518b048
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216
github.com/dolthub/vitess v0.0.0-20240916204416-9d4d4a09b1d9
github.com/dolthub/vitess v0.0.0-20240919225659-2ad81685e772
github.com/fatih/color v1.13.0
github.com/goccy/go-json v0.10.2
github.com/gogo/protobuf v1.3.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,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-20240916130659-0118adc6b662 h1:aC17hZD6iwzBwwfO5M+3oBT5E5gGRiQPdn+vzpDXqIA=
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.18.2-0.20240919175427-5055c7114c8b h1:y5oKakoZEsTZCDu67VOD395uhKG00VpVGzSYTZoYiDY=
github.com/dolthub/go-mysql-server v0.18.2-0.20240919175427-5055c7114c8b/go.mod h1:m88EMm9OthVVa6qIhbpnRDpj/eYUXuNpvY/+0YWKVwc=
github.com/dolthub/go-mysql-server v0.18.2-0.20240920224603-6f1a8518b048 h1:cYdcm/bKRAupP51QfcWhelgDBNg1r36OKlAf7+meU4E=
github.com/dolthub/go-mysql-server v0.18.2-0.20240920224603-6f1a8518b048/go.mod h1:lGbU2bK+QNnlETdUjOOaE+UnlEUu31VaQOFKAFGyZN4=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=
Expand All @@ -238,8 +238,8 @@ github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 h1:JWkKRE4
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216/go.mod h1:e/FIZVvT2IR53HBCAo41NjqgtEnjMJGKca3Y/dAmZaA=
github.com/dolthub/swiss v0.1.0 h1:EaGQct3AqeP/MjASHLiH6i4TAmgbG/c4rA6a1bzCOPc=
github.com/dolthub/swiss v0.1.0/go.mod h1:BeucyB08Vb1G9tumVN3Vp/pyY4AMUnr9p7Rz7wJ7kAQ=
github.com/dolthub/vitess v0.0.0-20240916204416-9d4d4a09b1d9 h1:2My8cED5m5/sFay7U4bvLxpECJccKj0cEKCqEA+63yU=
github.com/dolthub/vitess v0.0.0-20240916204416-9d4d4a09b1d9/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20240919225659-2ad81685e772 h1:vDwBX7Lc8DnA8Zk0iRIu6slCw0GIUfYfFlYDYJQw8GQ=
github.com/dolthub/vitess v0.0.0-20240919225659-2ad81685e772/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down
21 changes: 18 additions & 3 deletions postgres/parser/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ func (u *sqlSymUnion) aggregatesToDrop() []tree.AggregateToDrop {
%type <tree.ReadWriteMode> transaction_read_mode
%type <tree.DeferrableMode> deferrable_mode opt_deferrable_mode

%type <str> name opt_name opt_name_parens
%type <str> name opt_name opt_name_parens use_db_name
%type <str> privilege savepoint_name
%type <tree.KVOption> role_option password_clause valid_until_clause
%type <tree.Operator> subquery_op
Expand Down Expand Up @@ -5752,12 +5752,27 @@ reset_stmt:
// "USE <dbname>" is an alias for "SET [SESSION] database = <dbname>".
// %SeeAlso: SET SESSION, WEBDOCS/set-vars.html
use_stmt:
USE var_value
USE use_db_name
{
$$.val = &tree.SetVar{Name: "database", Values: tree.Exprs{$2.expr()}}
$$.val = &tree.SetVar{Name: "database", Values: tree.Exprs{tree.NewStrVal($2)}}
}
| USE error // SHOW HELP: USE

// use_db_name is for the db name in USE <dbname>. We extend the simple syntax to allow for mydb/main without quoting.
use_db_name:
SCONST
{
$$ = $1
}
| db_object_name_component
{
$$ = $1
}
| db_object_name_component '/' db_object_name_component
{
$$ = fmt.Sprintf("%s/%s", $1, $3)
}

// SET statements including e.g. SET TRANSACTION
set_stmt:
set_transaction_stmt // EXTEND WITH HELP: SET TRANSACTION
Expand Down
30 changes: 28 additions & 2 deletions server/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,34 @@ func nodeExpr(node tree.Expr) (vitess.Expr, error) {
case *tree.IfErrExpr:
return nil, fmt.Errorf("IFERROR is not yet supported")
case *tree.IfExpr:
// TODO: probably should be the IF function
return nil, fmt.Errorf("IF is not yet supported")
cond, err := nodeExpr(node.Cond)
if err != nil {
return nil, err
}
trueVal, err := nodeExpr(node.True)
if err != nil {
return nil, err
}
falseVal, err := nodeExpr(node.Else)
if err != nil {
return nil, err
}

// TODO: this could be a postgres func, but postgres doesn't have an IF function, this is an extension from cockroach
return &vitess.FuncExpr{
Name: vitess.NewColIdent("IF"),
Exprs: vitess.SelectExprs{
&vitess.AliasedExpr{
Expr: cond,
},
&vitess.AliasedExpr{
Expr: trueVal,
},
&vitess.AliasedExpr{
Expr: falseVal,
},
},
}, nil
case *tree.IndexedVar:
// TODO: figure out if I can delete this
return nil, fmt.Errorf("this should probably be deleted (internal error, IndexedVar)")
Expand Down
4 changes: 2 additions & 2 deletions server/ast/index_table_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func nodeIndexTableDef(node *tree.IndexTableDef) (*vitess.IndexDefinition, error
default:
return nil, fmt.Errorf("unknown index sorting direction encountered")
}
if indexElem.Direction != tree.Ascending {
if indexElem.Direction == tree.Descending {
logrus.Warn("descending indexes are not yet supported, ignoring sort order")
}
switch indexElem.NullsOrder {
case tree.DefaultNullsOrder:
//TODO: the default NULL order is reversed compared to MySQL, so the default is technically always wrong.
// TODO: the default NULL order is reversed compared to MySQL, so the default is technically always wrong.
// To prevent choking on every index, we allow this to proceed (even with incorrect results) for now.
case tree.NullsFirst:
// The only form supported in GMS for now
Expand Down
49 changes: 49 additions & 0 deletions testing/go/create_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _go

import (
"testing"

"github.com/dolthub/go-mysql-server/sql"
)

func TestCreateTable(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "create table with primary key",
Assertions: []ScriptTestAssertion{
{
// TODO: we don't currently have a way to check for warnings in these tests, but this query was incorrectly
// producing a warning. Would be nice to assert no warnings on most queries.
Query: "create table employees (" +
" id int8," +
" last_name text," +
" first_name text," +
" primary key(id));",
},
{
Query: "insert into employees (id, last_name, first_name) values (1, 'Doe', 'John');",
},
{
Query: "select * from employees;",
Expected: []sql.Row{
{1, "Doe", "John"},
},
},
},
},
})
}
108 changes: 81 additions & 27 deletions testing/go/enginetest/doltgres_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package enginetest

import (
"os"
"regexp"
"runtime"
"testing"

Expand Down Expand Up @@ -108,48 +109,88 @@ func TestSchemaOverrides(t *testing.T) {
denginetest.RunSchemaOverridesTest(t, harness)
}

type doltCommitValidator struct{}

var _ enginetest.CustomValueValidator = &doltCommitValidator{}

// TODO: this custom validator is supposed to match only a commit hash, but we extend it to match the formatting
//
// characters present in the Doltgres response for some calls. We can remove this when we support the syntax
// `select * from dolt_commit(...)`
var hashRegex = regexp.MustCompile(`^\{?([0-9a-v]{32}).*$`)

// Validate returns true if the value is a valid commit hash.
func (dcv *doltCommitValidator) Validate(val interface{}) (bool, error) {
hash, ok := val.(string)
if !ok {
return false, nil
}
return hashRegex.MatchString(hash), nil
}

// CommitHash returns the commit hash from the value, if it is a valid commit hash.
func (dcv *doltCommitValidator) CommitHash(val interface{}) (bool, string) {
hash, ok := val.(string)
if !ok {
return false, ""
}

matches := hashRegex.FindStringSubmatch(hash)
if len(matches) == 0 {
return false, ""
}
return true, matches[1]
}

// Convenience test for debugging a single query. Unskip and set to the desired query.
func TestSingleScript(t *testing.T) {
t.Skip()

var scripts = []queries.ScriptTest{
{
Name: "dolt_reset('--soft') commits the active SQL transaction",
Name: "dolt-tag: SQL create tags",
SetUpScript: []string{
"create table t (pk int primary key);",
"insert into t values (1), (2);",
"call dolt_commit('-Am', 'creating table t');",
"CREATE TABLE test(pk int primary key);",
"CALL DOLT_ADD('.')",
"INSERT INTO test VALUES (0),(1),(2);",
"CALL DOLT_COMMIT('-am','created table test')",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "start transaction;",
Expected: []sql.Row{},
Query: "CALL DOLT_TAG('v1', 'HEAD')",
Expected: []sql.Row{{0}},
},
{
Query: "SELECT tag_name, IF(CHAR_LENGTH(tag_hash) < 0, NULL, 'not null'), tagger, email, IF(date IS NULL, NULL, 'not null'), message from dolt_tags",
Expected: []sql.Row{{"v1", "not null", "billy bob", "[email protected]", "not null", ""}},
},
{
Query: "call dolt_reset('--soft', 'HEAD~');",
Query: "CALL DOLT_TAG('v2', '-m', 'create tag v2')",
Expected: []sql.Row{{0}},
},
{
// dolt_status should only show the unstaged table t being added
Query: "select * from dolt_status",
Expected: []sql.Row{{"t", false, "new table"}},
Query: "SELECT tag_name, message from dolt_tags",
Expected: []sql.Row{{"v1", ""}, {"v2", "create tag v2"}},
},
},
},
}

for _, script := range scripts {
harness := newDoltgresServerHarness(t)
// harness.Setup(setup.MydbData, setup.MytableData)

engine, err := harness.NewEngine(t)
if err != nil {
panic(err)
}
// engine.EngineAnalyzer().Debug = true
// engine.EngineAnalyzer().Verbose = true

enginetest.TestScriptWithEngine(t, engine, harness, script)
func() {
harness := newDoltgresServerHarness(t)
defer harness.Close()
// harness.Setup(setup.MydbData, setup.MytableData)

engine, err := harness.NewEngine(t)
if err != nil {
panic(err)
}
// engine.EngineAnalyzer().Debug = true
// engine.EngineAnalyzer().Verbose = true

enginetest.TestScriptWithEngine(t, engine, harness, script)
}()
}
}

Expand Down Expand Up @@ -1095,8 +1136,13 @@ func TestDoltCheckout(t *testing.T) {
}

func TestDoltCheckoutPrepared(t *testing.T) {
t.Skip()
h := newDoltgresServerHarness(t)
t.Skip("need to implement prepared queries in harness")
h := newDoltgresServerHarness(t).WithSkippedQueries([]string{
"dolt_checkout and base name resolution", // needs db-qualified table names
"branch last checked out is deleted",
"Using non-existent refs",
"read-only databases", // read-only not yet implemented in harness
})
denginetest.RunDoltCheckoutPreparedTests(t, h)
}

Expand All @@ -1110,8 +1156,11 @@ func TestDoltBranch(t *testing.T) {
}

func TestDoltTag(t *testing.T) {
t.Skip()
h := newDoltgresServerHarness(t)
h := newDoltgresServerHarness(t).WithSkippedQueries([]string{
// dolt's initialization is different which results in a different user name for the tagger,
// should fix the harness to match
"SELECT tag_name, IF(CHAR_LENGTH(tag_hash) < 0, NULL, 'not null'), tagger, email, IF(date IS NULL, NULL, 'not null'), message from dolt_tags",
})
denginetest.RunDoltTagTests(t, h)
}

Expand Down Expand Up @@ -1394,8 +1443,13 @@ func TestDoltCherryPickPrepared(t *testing.T) {
}

func TestDoltCommit(t *testing.T) {
t.Skip()
harness := newDoltgresServerHarness(t)
harness := newDoltgresServerHarness(t).WithSkippedQueries([]string{
// These tests set @@autocommit, which we can't translate accurately yet
"CALL DOLT_COMMIT('-amend') works to update commit message",
"CALL DOLT_COMMIT('-amend') works to add changes to a commit",
"CALL DOLT_COMMIT('-amend') works to remove changes from a commit",
"CALL DOLT_COMMIT('-amend') works to update a merge commit",
})
denginetest.RunDoltCommitTests(t, harness)
}

Expand Down
Loading

0 comments on commit 6da1bf1

Please sign in to comment.