Skip to content

Commit

Permalink
postgres: Column merging for table inheritence (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Jun 8, 2023
1 parent 0c340f2 commit d37c9d4
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ CREATE TABLE person (
CREATE TABLE organisation (
legal_name text
) INHERITS (party);

CREATE TABLE llc (
incorporation_date timestamp,
legal_name text NOT NULL
) INHERITS (organisation);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ CREATE TABLE person (
CREATE TABLE organisation (
legal_name text
) INHERITS (party);

CREATE TABLE llc (
incorporation_date timestamp,
legal_name text NOT NULL
) INHERITS (organisation);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ CREATE TABLE person (
CREATE TABLE organisation (
legal_name text
) INHERITS (party);

CREATE TABLE llc (
incorporation_date timestamp,
legal_name text NOT NULL
) INHERITS (organisation);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- name: GetAllParties :many
SELECT * FROM party;

-- name: GetAllOrganisations :many
SELECT * FROM organisation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE party (
name text NOT NULL
);

CREATE TABLE organisation (
name integer NOT NULL
) INHERITS (party);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# package querytest
schema.sql:1:1: column "name" has a type conflict
36 changes: 31 additions & 5 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,28 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
}

tbl := Table{Rel: stmt.Name, Comment: stmt.Comment}
m := make(map[string]struct{}) // used to check for duplicate column names
coltype := make(map[string]ast.TypeName) // used to check for duplicate column names
seen := make(map[string]bool) // used to check for duplicate column names
for _, inheritTable := range stmt.Inherits {
t, _, err := schema.getTable(inheritTable)
if err != nil {
return err
}
// check and ignore duplicate columns
for _, col := range t.Columns {
if _, ok := m[col.Name]; ok {
if notNull, ok := seen[col.Name]; ok {
seen[col.Name] = notNull || col.IsNotNull
if a, ok := coltype[col.Name]; ok {
if !sameType(&a, &col.Type) {
return fmt.Errorf("column \"%s\" has a type conflict", col.Name)
}
}
continue
} else {
m[col.Name] = struct{}{}
tbl.Columns = append(tbl.Columns, col)
}

seen[col.Name] = col.IsNotNull
coltype[col.Name] = col.Type
tbl.Columns = append(tbl.Columns, col)
}
}

Expand All @@ -272,6 +280,16 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
}
} else {
for _, col := range stmt.Cols {
if notNull, ok := seen[col.Colname]; ok {
seen[col.Colname] = notNull || col.IsNotNull
if a, ok := coltype[col.Colname]; ok {
if !sameType(&a, col.TypeName) {
return fmt.Errorf("column \"%s\" has a type conflict", col.Colname)
}
}
continue
}

tc := &Column{
Name: col.Colname,
Type: *col.TypeName,
Expand All @@ -293,6 +311,14 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
tbl.Columns = append(tbl.Columns, tc)
}
}

// If one of the merged columns was not null, mark the column as not null
for i := range tbl.Columns {
if notNull, ok := seen[tbl.Columns[i].Name]; ok {
tbl.Columns[i].IsNotNull = notNull
}
}

schema.Tables = append(schema.Tables, &tbl)
return nil
}
Expand Down

0 comments on commit d37c9d4

Please sign in to comment.