Skip to content

Commit

Permalink
Merge pull request #181 from dolthub/zachmu/schemas
Browse files Browse the repository at this point in the history
Support for schemas
  • Loading branch information
zachmu authored Apr 29, 2024
2 parents 0d4c378 + 04a602d commit 1b37960
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 116 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ 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.20240411164336-498af25e3dfd
github.com/dolthub/dolt/go v0.40.5-0.20240429221409-bf82c4809507
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240228231039-f903736a64a8
github.com/dolthub/go-mysql-server v0.18.2-0.20240410074202-09d57d9841d3
github.com/dolthub/go-mysql-server v0.18.2-0.20240429214844-6feb67867355
github.com/dolthub/sqllogictest/go v0.0.0-20240118211725-a52e3f5697e3
github.com/dolthub/vitess v0.0.0-20240409192815-eb9306c6497f
github.com/dolthub/vitess v0.0.0-20240429213844-e8e1b4cd75c4
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
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,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.20240411164336-498af25e3dfd h1:Fglemyo9RBwNS9k/slc/pjmo8hBX8+vTLvQq0cCtC18=
github.com/dolthub/dolt/go v0.40.5-0.20240411164336-498af25e3dfd/go.mod h1:PutGBeYj3pNAA7oq1nrv3P5valiNNBeXB7LLgyOIVc8=
github.com/dolthub/dolt/go v0.40.5-0.20240429221409-bf82c4809507 h1:0IDW0cNJS1LvNeak4gEihqZzELQc+4E+bHDcZXgP6NY=
github.com/dolthub/dolt/go v0.40.5-0.20240429221409-bf82c4809507/go.mod h1:XKLiUZiTGlw3gXgN5eR5qIbqK/snH1PG1hbzHlYxa5o=
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240228231039-f903736a64a8 h1:7GAc45zje/43KeNMX4zBcdCShLdqJhyVQvLDWoxvA+s=
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240228231039-f903736a64a8/go.mod h1:gHeHIDGU7em40EhFTliq62pExFcc1hxDTIZ9g5UqXYM=
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww=
Expand All @@ -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-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.18.2-0.20240410074202-09d57d9841d3 h1:i48WY1tLEbvbN4PWiozfC7CzuuOw9or4tZedIWwxWE0=
github.com/dolthub/go-mysql-server v0.18.2-0.20240410074202-09d57d9841d3/go.mod h1:uTqIti1oPqKEUS9N1zcT43a/QQ8n0PuBdZUMZziBaGU=
github.com/dolthub/go-mysql-server v0.18.2-0.20240429214844-6feb67867355 h1:Dylx0T0J40z3momZ0pDlUm0PWEvPWrcOVkeZ9jFXtVQ=
github.com/dolthub/go-mysql-server v0.18.2-0.20240429214844-6feb67867355/go.mod h1:T6EEu2iQoasR13Ovtp44yDn+rXQOBgh3BACPZMxSF/8=
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 All @@ -236,8 +236,8 @@ github.com/dolthub/sqllogictest/go v0.0.0-20240118211725-a52e3f5697e3 h1:+eDpuEJ
github.com/dolthub/sqllogictest/go v0.0.0-20240118211725-a52e3f5697e3/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-20240409192815-eb9306c6497f h1:r4CEDRuMubHOJjFwl6J+OTxEDru32U5SbL4Aq+5Sh84=
github.com/dolthub/vitess v0.0.0-20240409192815-eb9306c6497f/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20240429213844-e8e1b4cd75c4 h1:OM5fvL/E58MUkxqqdiixNXBKMcomvnjBAWnPA2YIv7c=
github.com/dolthub/vitess v0.0.0-20240429213844-e8e1b4cd75c4/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
7 changes: 4 additions & 3 deletions server/ast/create_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ func nodeCreateDatabase(node *tree.CreateDatabase) (*vitess.DBDDL, error) {
}

return &vitess.DBDDL{
Action: vitess.CreateStr,
DBName: node.Name.String(),
IfNotExists: node.IfNotExists,
Action: vitess.CreateStr,
SchemaOrDatabase: "database",
DBName: node.Name.String(),
IfNotExists: node.IfNotExists,
}, nil
}
12 changes: 9 additions & 3 deletions server/ast/create_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package ast

import (
"fmt"

vitess "github.com/dolthub/vitess/go/vt/sqlparser"

"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
Expand All @@ -27,5 +25,13 @@ func nodeCreateSchema(node *tree.CreateSchema) (vitess.Statement, error) {
if node == nil {
return nil, nil
}
return nil, fmt.Errorf("CREATE SCHEMA is not yet supported")

return &vitess.DBDDL{
Action: "CREATE",
SchemaOrDatabase: "schema",
DBName: node.Schema,
IfNotExists: node.IfNotExists,
CharsetCollate: nil, // TODO
// TODO: AuthRole
}, nil
}
7 changes: 5 additions & 2 deletions server/ast/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ func nodeInsert(node *tree.Insert) (*vitess.Insert, error) {
if err != nil {
return nil, err
}
// GMS doesn't like having a ValuesStatement within a Select for INSERT, so we extract it

// GMS For a ValuesStatement with simple rows, GMS expects AliasedValues
if vSelect, ok := rows.(*vitess.Select); ok && len(vSelect.From) == 1 {
if valsStmt, ok := vSelect.From[0].(*vitess.ValuesStatement); ok {
rows = valsStmt.Rows
rows = &vitess.AliasedValues{
Values: valsStmt.Rows,
}
}
}
return &vitess.Insert{
Expand Down
30 changes: 11 additions & 19 deletions server/ast/table_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package ast

import (
"fmt"
"strings"

vitess "github.com/dolthub/vitess/go/vt/sqlparser"

"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
Expand All @@ -29,24 +26,19 @@ func nodeTableName(node *tree.TableName) (vitess.TableName, error) {
return vitess.TableName{}, nil
}

if node.ExplicitCatalog || node.ExplicitSchema {
if strings.ToLower(string(node.SchemaName)) == "information_schema" {
return vitess.TableName{
Name: vitess.NewTableIdent(string(node.ObjectName)),
Qualifier: vitess.NewTableIdent(string(node.SchemaName)),
}, nil
} else if !node.ExplicitCatalog && node.ExplicitSchema && strings.ToLower(string(node.SchemaName)) == "public" {
// the "public" schema is the default schema in PostgreSQL, so treat it as if it were not explicitly specified
return vitess.TableName{
Name: vitess.NewTableIdent(string(node.ObjectName)),
Qualifier: vitess.NewTableIdent(""),
}, nil
}
return vitess.TableName{}, fmt.Errorf("referencing items outside the schema or database is not yet supported")
var dbName, schemaName vitess.TableIdent

if node.ExplicitCatalog {
dbName = vitess.NewTableIdent(string(node.CatalogName))
}

if node.ExplicitSchema {
schemaName = vitess.NewTableIdent(string(node.SchemaName))
}

return vitess.TableName{
Name: vitess.NewTableIdent(string(node.ObjectName)),
Qualifier: vitess.NewTableIdent(string(node.SchemaName)),
Name: vitess.NewTableIdent(string(node.ObjectName)),
DbQualifier: dbName,
SchemaQualifier: schemaName,
}, nil
}
4 changes: 2 additions & 2 deletions server/ast/unresolved_object_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func nodeUnresolvedObjectName(node *tree.UnresolvedObjectName) (vitess.TableName
return vitess.TableName{}, fmt.Errorf("referencing items outside the schema or database is not yet supported")
}
return vitess.TableName{
Name: vitess.NewTableIdent(node.Parts[0]),
Qualifier: vitess.TableIdent{},
Name: vitess.NewTableIdent(node.Parts[0]),
DbQualifier: vitess.TableIdent{},
}, nil
}
2 changes: 1 addition & 1 deletion server/types/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (b DateType) Convert(val any) (any, sql.ConvertInRange, error) {
} else if t, err = time.Parse("2006-Jan-02", val); err == nil {
return t.UTC(), sql.InRange, nil
}
return nil, sql.OutOfRange, fmt.Errorf("invalid format for time")
return nil, sql.OutOfRange, fmt.Errorf("invalid format for date")
case time.Time:
return val.UTC(), sql.InRange, nil
default:
Expand Down
6 changes: 3 additions & 3 deletions testing/bats/replication.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ teardown() {
start_sql_server_with_args "--config=replication-config.yaml"

# Create the table that already exists on the primary before doing any inserts on the primary
query_server doltgres -c "create table t1 (a int primary key, b int)"
query_server doltgres -c "create table public.t1 (a int primary key, b int)"

# this insert on the primary should now replicate to the replica
postgres_primary_query "insert into t1 values (1, 2)"
sleep 1

query_server doltgres -c "select * from t1" -t
run query_server doltgres -c "select * from t1" -t
query_server doltgres -c "select * from public.t1" -t
run query_server doltgres -c "select * from public.t1" -t
[ "$status" -eq 0 ]
[[ "$output" =~ "1 | 2" ]] || false

Expand Down
72 changes: 36 additions & 36 deletions testing/generation/command_docs/output/create_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@ import "testing"

func TestCreateSchema(t *testing.T) {
tests := []QueryParses{
Parses("CREATE SCHEMA schema_name"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION user_name"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER"),
Parses("CREATE SCHEMA schema_name CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION user_name"),
Parses("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE"),
Parses("CREATE SCHEMA AUTHORIZATION CURRENT_USER"),
Parses("CREATE SCHEMA AUTHORIZATION SESSION_USER"),
Parses("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Parses("CREATE SCHEMA IF NOT EXISTS schema_name"),
Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION user_name"),
Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_ROLE"),
Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_USER"),
Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION SESSION_USER"),
Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION user_name"),
Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_ROLE"),
Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_USER"),
Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION SESSION_USER"),
Converts("CREATE SCHEMA schema_name"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION user_name"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER"),
Converts("CREATE SCHEMA schema_name CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION user_name"),
Converts("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE"),
Converts("CREATE SCHEMA AUTHORIZATION CURRENT_USER"),
Converts("CREATE SCHEMA AUTHORIZATION SESSION_USER"),
Converts("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"),
Converts("CREATE SCHEMA IF NOT EXISTS schema_name"),
Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION user_name"),
Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_ROLE"),
Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_USER"),
Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION SESSION_USER"),
Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION user_name"),
Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_ROLE"),
Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_USER"),
Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION SESSION_USER"),
}
RunTests(t, tests)
}
Loading

0 comments on commit 1b37960

Please sign in to comment.