Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented support for various expressions needed by sqllogictest #60

Merged
merged 15 commits into from
Dec 1, 2023
Merged
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/cockroachdb/apd/v2 v2.0.3-0.20200518165714-d020e156310a
github.com/cockroachdb/errors v1.7.5
github.com/dolthub/dolt/go v0.40.5-0.20231114163244-0cc42b844045
github.com/dolthub/go-mysql-server v0.17.1-0.20231113191556-2da061871eb8
github.com/dolthub/dolt/go v0.40.5-0.20231130221002-0d5eeb513092
github.com/dolthub/go-mysql-server v0.17.1-0.20231130181846-c0a0060c4fe7
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20231109003730-c0fa018b5ef6
github.com/dolthub/vitess v0.0.0-20231127171856-2466012fb61f
github.com/fatih/color v1.13.0
github.com/gogo/protobuf v1.3.2
github.com/golang/geo v0.0.0-20200730024412-e86565bf3f35
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dolthub/dolt/go v0.40.5-0.20231114163244-0cc42b844045 h1:3XcJEPd9hydNGegfDC95b+PJWvzJrqU0serxh5Bo9Qw=
github.com/dolthub/dolt/go v0.40.5-0.20231114163244-0cc42b844045/go.mod h1:CAA/kBFxZdT8s3aLtLUw3gEsuxSOVeChlQucDlZCTBU=
github.com/dolthub/dolt/go v0.40.5-0.20231130221002-0d5eeb513092 h1:DvCGM7uxHtNe8b6C2vpX71oFhhHf1KoXeb+/y8wBkt8=
github.com/dolthub/dolt/go v0.40.5-0.20231130221002-0d5eeb513092/go.mod h1:PVPL+qOhr6+DPUW0QyHTvoTd0AbYBIRd7Y4ITrhi97s=
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20231018220650-48f565111c6a h1:WYBHmrFuKPqPfLB8ab1WqL5MgR8PQICkfUs5RTjHZM8=
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20231018220650-48f565111c6a/go.mod h1:Fi7KchJVfwMuPJkX4vJeAlNZkxCiVyhvVYfCgaSDlTU=
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww=
Expand All @@ -226,8 +226,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.20231113191556-2da061871eb8 h1:hOOUs5dptLxn8hsNV/lm7GNgUcqmms3d9N86o2INALM=
github.com/dolthub/go-mysql-server v0.17.1-0.20231113191556-2da061871eb8/go.mod h1:Z3EbOzC1yoK9MoYBxl6LDksV8GRRyjjHDZTu2lWpT/E=
github.com/dolthub/go-mysql-server v0.17.1-0.20231130181846-c0a0060c4fe7 h1:aNrWLEmWLvD3vJR14XJ8PhRzRteOOg6/1coHqU5sOR8=
github.com/dolthub/go-mysql-server v0.17.1-0.20231130181846-c0a0060c4fe7/go.mod h1:vXlRKS39WHav9N51VsfYphKhmSA2t5FkhHmW3BtwH5I=
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 All @@ -238,8 +238,8 @@ github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9X
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
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-20231109003730-c0fa018b5ef6 h1:/GOBV8ceNCMuyS9/sdjqfmgti6OSibUKDTop9YbowzM=
github.com/dolthub/vitess v0.0.0-20231109003730-c0fa018b5ef6/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
github.com/dolthub/vitess v0.0.0-20231127171856-2466012fb61f h1:I480LKHhb4usnF3dYhp6J4ORKMrncNKaWYZvIZwlK+U=
github.com/dolthub/vitess v0.0.0-20231127171856-2466012fb61f/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down Expand Up @@ -982,8 +982,8 @@ golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnL
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
golang.org/x/image v0.10.0 h1:gXjUUtwtx5yOE0VKWq1CH4IJAClq4UGgUA3i+rpON9M=
golang.org/x/image v0.10.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
12 changes: 9 additions & 3 deletions postgres/messages/row_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ func VitessFieldToDataTypeObjectID(field *query.Field) (int32, error) {
return 1114, nil
case query.Type_DATE:
return 1082, nil
case query.Type_NULL_TYPE:
return 25, nil // NULL is treated as TEXT on the wire
default:
return 0, fmt.Errorf("unsupported type returned from engine")
return 0, fmt.Errorf("unsupported type returned from engine: %s", field.Type)
}
}

Expand Down Expand Up @@ -210,8 +212,10 @@ func VitessFieldToDataTypeSize(field *query.Field) (int16, error) {
return 8, nil
case query.Type_DATE:
return 4, nil
case query.Type_NULL_TYPE:
return -1, nil // NULL is treated as TEXT on the wire
default:
return 0, fmt.Errorf("unsupported type returned from engine")
return 0, fmt.Errorf("unsupported type returned from engine: %s", field.Type)
}
}

Expand Down Expand Up @@ -255,7 +259,9 @@ func VitessFieldToDataTypeModifier(field *query.Field) (int32, error) {
return -1, nil
case query.Type_DATE:
return -1, nil
case query.Type_NULL_TYPE:
return -1, nil // NULL is treated as TEXT on the wire
default:
return 0, fmt.Errorf("unsupported type returned from engine")
return 0, fmt.Errorf("unsupported type returned from engine: %s", field.Type)
}
}
26 changes: 23 additions & 3 deletions server/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ func nodeExpr(node tree.Expr) (vitess.Expr, error) {
Type: convertType,
}, nil
case *tree.CoalesceExpr:
return nil, fmt.Errorf("COALESCE is not yet supported")
exprs, err := nodeExprsToSelectExprs(node.Exprs)
if err != nil {
return nil, err
}

return &vitess.FuncExpr{
Name: vitess.NewColIdent("COALESCE"),
Exprs: exprs,
}, nil
case *tree.CollateExpr:
return nil, fmt.Errorf("collations are not yet supported")
case *tree.ColumnAccessExpr:
Expand Down Expand Up @@ -400,8 +408,20 @@ func nodeExpr(node tree.Expr) (vitess.Expr, error) {
Expr: expr,
}, nil
case *tree.NullIfExpr:
//TODO: probably should be the IF function: IF(Expr1 == Expr2, NULL, Expr1)
return nil, fmt.Errorf("NULLIF is not yet supported")
expr1, err := nodeExprToSelectExpr(node.Expr1)
if err != nil {
return nil, err
}

expr2, err := nodeExprToSelectExpr(node.Expr2)
if err != nil {
return nil, err
}

return &vitess.FuncExpr{
Name: vitess.NewColIdent("NULLIF"),
Exprs: vitess.SelectExprs{expr1, expr2},
}, nil
case tree.NullLiteral:
return &vitess.NullVal{}, nil
case *tree.NumVal:
Expand Down
6 changes: 3 additions & 3 deletions server/ast/func_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func nodeFuncExpr(node *tree.FuncExpr) (*vitess.FuncExpr, error) {
}
var distinct bool
switch node.Type {
case 0:
case 0, tree.AllFuncType:
distinct = false
case tree.DistinctFuncType:
distinct = true
case tree.AllFuncType:
return nil, fmt.Errorf("function spec is not yet supported")
default:
return nil, fmt.Errorf("unknown function spec type %d", node.Type)
zachmu marked this conversation as resolved.
Show resolved Hide resolved
}
windowDef, err := nodeWindowDef(node.WindowDef)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions server/ast/table_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func nodeTableExpr(node tree.TableExpr) (vitess.TableExpr, error) {
for i := range treeCondition.Cols {
condition.Using[i] = vitess.NewColIdent(string(treeCondition.Cols[i]))
}
case nil:
// cross join (no join condition)
default:
return nil, fmt.Errorf("unknown JOIN condition: `%T`", treeCondition)
}
Expand All @@ -70,10 +72,7 @@ func nodeTableExpr(node tree.TableExpr) (vitess.TableExpr, error) {
} else {
joinType = vitess.RightJoinStr
}
case tree.AstCross:
// GMS doesn't have any support for CROSS joins, as MySQL doesn't actually implement them
return nil, fmt.Errorf("CROSS joins are not yet supported")
case tree.AstInner:
case tree.AstCross, tree.AstInner:
zachmu marked this conversation as resolved.
Show resolved Hide resolved
joinType = vitess.JoinStr
case "":
if condition.On == nil && len(condition.Using) == 0 {
Expand Down
124 changes: 124 additions & 0 deletions testing/go/regression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2023 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 TestRegressions(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "nullif",
SetUpScript: []string{},
Assertions: []ScriptTestAssertion{
{
Query: "select nullif(1, 1);",
Expected: []sql.Row{{nil}},
},
{
Query: "select nullif('', null);",
Expected: []sql.Row{{""}},
},
{
Query: "select nullif(10, 'a');",
Expected: []sql.Row{{10}},
},
},
},
{
Name: "coalesce",
SetUpScript: []string{},
Assertions: []ScriptTestAssertion{
{
Query: "select coalesce(null + 5, 100);",
Expected: []sql.Row{{100.0}}, // TODO: this should be an integer
},
{
Query: "select coalesce(null, null, 'abc');",
Expected: []sql.Row{{"abc"}},
},
{
Query: "select coalesce(null, null);",
Expected: []sql.Row{{nil}},
},
},
},
{
Name: "case / when",
SetUpScript: []string{},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT\n" +
" CASE\n" +
" WHEN 1 = 1 THEN 'One is equal to One'\n" +
" ELSE 'One is not equal to One'\n" +
" END AS result;",
Expected: []sql.Row{{"One is equal to One"}},
},
{
Query: "SELECT\n" +
" CASE\n" +
" WHEN NULL IS NULL THEN 'NULL is NULL'\n" +
" ELSE 'NULL is not NULL'\n" +
" END AS result;",
Expected: []sql.Row{{"NULL is NULL"}},
},
},
},
{
Name: "ALL / DISTINCT in functions",
SetUpScript: []string{
"create table t1 (pk int);",
"insert into t1 values (1), (2), (3), (1);",
},
Assertions: []ScriptTestAssertion{
{
Query: "select all count(*) from t1;",
Expected: []sql.Row{{4}},
},
{
Query: "select all count(distinct pk) from t1;",
Expected: []sql.Row{{3}},
},
{
Query: "select all count(all pk) from t1;",
Expected: []sql.Row{{4}},
},
},
},
{
Name: "cross joins",
SetUpScript: []string{
"create table t1 (pk int);",
"create table t2 (pk int);",
"insert into t1 values (1), (2);",
"insert into t2 values (3), (4);",
},
Assertions: []ScriptTestAssertion{
{
Query: "select count(*) from t1 cross join t2;",
Expected: []sql.Row{{4}},
zachmu marked this conversation as resolved.
Show resolved Hide resolved
},
{
Query: "select count(*) from t1, t2;",
Expected: []sql.Row{{4}},
},
},
},
})
}
4 changes: 4 additions & 0 deletions testing/logictest/harness/doltgres_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ func columns(rows *sql.Rows) (string, []interface{}, error) {
colVal := sql.NullInt64{}
columns = append(columns, &colVal)
sb.WriteString("I")
case "UNKNOWN": // used for NULL values
colVal := sql.NullString{}
columns = append(columns, &colVal)
sb.WriteString("I") // is this right?
default:
return "", nil, fmt.Errorf("Unhandled type %s", columnType.DatabaseTypeName())
}
Expand Down
Loading