Skip to content

Commit

Permalink
add VIEW and MATERIALIZED VIEW statements parsing (#138)
Browse files Browse the repository at this point in the history
* add post-parsing check for routine options

* CREATE VIEW

* CREATE and ALTER MATERIALIZED VIEW and ALTER VIEW
  • Loading branch information
jennifersp authored Feb 28, 2024
1 parent 8c6a8e3 commit e8eb9a9
Show file tree
Hide file tree
Showing 21 changed files with 4,800 additions and 4,257 deletions.
414 changes: 288 additions & 126 deletions postgres/parser/parser/sql.y

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions postgres/parser/sem/tree/alter_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var _ Statement = &AlterIndexAllInTablespace{}
type AlterIndexAllInTablespace struct {
Name Name
OwnedBy []string
Tablespace Name
Tablespace string
NoWait bool
}

Expand All @@ -64,7 +64,7 @@ func (node *AlterIndexAllInTablespace) Format(ctx *FmtCtx) {
ctx.WriteString(strings.Join(node.OwnedBy, ", "))
}
ctx.WriteString(" SET TABLESPACE ")
ctx.FormatNode(&node.Tablespace)
ctx.WriteString(node.Tablespace)
if node.NoWait {
ctx.WriteString(" NOWAIT")
}
Expand Down Expand Up @@ -152,11 +152,11 @@ func (node *AlterIndexSetStorage) Format(ctx *FmtCtx) {
// AlterIndexSetTablespace represents an ALTER INDEX ... SET TABLESPACE
// command.
type AlterIndexSetTablespace struct {
Tablespace Name
Tablespace string
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexSetTablespace) Format(ctx *FmtCtx) {
ctx.WriteString(" SET TABLESPACE ")
ctx.FormatNode(&node.Tablespace)
ctx.WriteString(node.Tablespace)
}
49 changes: 49 additions & 0 deletions postgres/parser/sem/tree/alter_materialized_view.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 tree

var _ Statement = &AlterMaterializedView{}

// AlterMaterializedView represents an ALTER MATERIALIZED VIEW statement.
type AlterMaterializedView struct {
Name *UnresolvedObjectName
IfExists bool
Cmds AlterTableCmds
No bool
Extension string
}

func (node *AlterMaterializedView) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER MATERIALIZED VIEW ")
if node.IfExists {
ctx.WriteString("IF EXISTS ")
}
ctx.FormatNode(node.Name)
if node.Extension != "" {
if node.No {
ctx.WriteString(" NO")
}
ctx.WriteString(" DEPENDS ON EXTENSION ")
ctx.WriteString(node.Extension)
} else {
ctx.WriteByte(' ')
ctx.FormatNode(&node.Cmds)
}
}

// The following statements are included as part of ALTER TABLE ... or RENAME TABLE statement:
// ALTER MATERIALIZED VIEW [ IF EXISTS ] name RENAME TO new_name
// ALTER MATERIALIZED VIEW [ IF EXISTS ] name SET SCHEMA new_schema
// ALTER MATERIALIZED VIEW ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ] SET TABLESPACE new_tablespace [ NOWAIT ]
55 changes: 30 additions & 25 deletions postgres/parser/sem/tree/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,6 @@ func (node *AlterTableOwner) Format(ctx *FmtCtx) {
ctx.FormatNameP(&node.Owner)
}

type DetachPartition int

const (
DetachPartitionNone DetachPartition = iota
DetachPartitionConcurrently
DetachPartitionFinalize
)

// AlterTableRenameColumn represents an ALTER TABLE RENAME [COLUMN] command.
type AlterTableRenameColumn struct {
Column Name
Expand Down Expand Up @@ -885,26 +877,23 @@ var _ Statement = &AlterTableSetSchema{}

// AlterTableSetSchema represents an ALTER TABLE SET SCHEMA statement.
type AlterTableSetSchema struct {
Name *UnresolvedObjectName
Schema string
IfExists bool
IsView bool
Name *UnresolvedObjectName
Schema string
IfExists bool

IsMaterialized bool
IsSequence bool
}

// Format implements the NodeFormatter interface.
func (node *AlterTableSetSchema) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER")
if node.IsView {
if node.IsMaterialized {
ctx.WriteString(" MATERIALIZED")
}
ctx.WriteString(" VIEW ")
ctx.WriteString("ALTER ")
if node.IsMaterialized {
ctx.WriteString("MATERIALIZED VIEW")
} else if node.IsSequence {
ctx.WriteString(" SEQUENCE ")
} else {
ctx.WriteString(" TABLE ")
ctx.WriteString("TABLE")
}
if node.IfExists {
ctx.WriteString("IF EXISTS ")
Expand All @@ -916,35 +905,51 @@ func (node *AlterTableSetSchema) Format(ctx *FmtCtx) {

var _ Statement = &AlterTableAllInTablespace{}

// AlterTableAllInTablespace represents an ALTER TABLE ALL IN TABLESPACE ... statement.
// AlterTableAllInTablespace represents an ALTER { TABLE | MATERIALIZED VIEW } ALL IN TABLESPACE ... statement.
type AlterTableAllInTablespace struct {
Name Name
OwnedBy []string
Tablespace Name
Tablespace string
NoWait bool

IsMaterialized bool
}

// Format implements the NodeFormatter interface.
func (node *AlterTableAllInTablespace) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER TABLE ALL IN TABLESPACE ")
ctx.WriteString("ALTER ")
if node.IsMaterialized {
ctx.WriteString("MATERIALIZED VIEW")
} else {
ctx.WriteString("TABLE")
}
ctx.WriteString(" ALL IN TABLESPACE ")
ctx.FormatNode(&node.Name)
if node.OwnedBy != nil {
ctx.WriteString(" OWNED BY ")
ctx.WriteString(strings.Join(node.OwnedBy, ", "))
}
ctx.WriteString(" SET TABLESPACE ")
ctx.FormatNode(&node.Tablespace)
ctx.WriteString(node.Tablespace)
if node.NoWait {
ctx.WriteString(" NOWAIT")
}
}

type DetachPartition int

const (
DetachPartitionNone DetachPartition = iota
DetachPartitionConcurrently
DetachPartitionFinalize
)

var _ Statement = &AlterTablePartition{}

// AlterTablePartition represents an ALTER TABLE { ATTACH | DETACH } PARTITION ...
// command.
type AlterTablePartition struct {
Table *UnresolvedObjectName
Name *UnresolvedObjectName
IfExists bool
Partition Name
Spec PartitionBoundSpec
Expand All @@ -958,7 +963,7 @@ func (node *AlterTablePartition) Format(ctx *FmtCtx) {
if node.IfExists {
ctx.WriteString("IF EXISTS ")
}
node.Table.Format(ctx)
node.Name.Format(ctx)
if node.IsDetach {
ctx.WriteString(" DETACH PARTITION ")
ctx.FormatNode(&node.Partition)
Expand Down
139 changes: 139 additions & 0 deletions postgres/parser/sem/tree/alter_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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 tree

var _ Statement = &AlterView{}

// AlterView represents an ALTER VIEW statement.
type AlterView struct {
Name *UnresolvedObjectName
IfExists bool
Cmd AlterViewCmd
}

func (node *AlterView) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER VIEW ")
if node.IfExists {
ctx.WriteString("IF EXISTS ")
}
ctx.FormatNode(node.Name)
ctx.FormatNode(node.Cmd)
}

// AlterViewCmd represents a view modification operation.
type AlterViewCmd interface {
NodeFormatter
// Placeholder function to ensure that only desired views
// (AlterView*) conform to the AlterViewCmd interface.
alterViewCmd()
}

func (*AlterViewSetDefault) alterViewCmd() {}
func (*AlterViewOwnerTo) alterViewCmd() {}
func (*AlterViewRenameColumn) alterViewCmd() {}
func (*AlterViewRenameTo) alterViewCmd() {}
func (*AlterViewSetSchema) alterViewCmd() {}
func (*AlterViewSetOption) alterViewCmd() {}

var _ AlterViewCmd = &AlterViewSetDefault{}
var _ AlterViewCmd = &AlterViewOwnerTo{}
var _ AlterViewCmd = &AlterViewRenameColumn{}
var _ AlterViewCmd = &AlterViewRenameTo{}
var _ AlterViewCmd = &AlterViewSetSchema{}
var _ AlterViewCmd = &AlterViewSetOption{}

// AlterViewSetDefault represents an ALTER VIEW ALTER COLUMN SET DEFAULT
// or DROP DEFAULT command.
type AlterViewSetDefault struct {
Column Name
Default Expr
}

// Format implements the NodeFormatter interface.
func (node *AlterViewSetDefault) Format(ctx *FmtCtx) {
ctx.WriteString(" ALTER COLUMN ")
ctx.FormatNode(&node.Column)
if node.Default == nil {
ctx.WriteString(" DROP DEFAULT")
} else {
ctx.WriteString(" SET DEFAULT ")
ctx.FormatNode(node.Default)
}
}

// AlterViewOwnerTo represents an ALTER VIEW OWNER TO command.
type AlterViewOwnerTo struct {
Owner string
}

// Format implements the NodeFormatter interface.
func (node *AlterViewOwnerTo) Format(ctx *FmtCtx) {
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
}

// AlterViewRenameColumn represents an ALTER VIEW RENAME [COLUMN] command.
type AlterViewRenameColumn struct {
Column Name
NewName Name
}

// Format implements the NodeFormatter interface.
func (node *AlterViewRenameColumn) Format(ctx *FmtCtx) {
ctx.WriteString(" RENAME COLUMN ")
ctx.FormatNode(&node.Column)
ctx.WriteString(" TO ")
ctx.FormatNode(&node.NewName)
}

// AlterViewRenameTo represents an ALTER VIEW ... RENAME TO
// command.
type AlterViewRenameTo struct {
Rename *UnresolvedObjectName
}

// Format implements the NodeFormatter interface.
func (node *AlterViewRenameTo) Format(ctx *FmtCtx) {
ctx.WriteString(" RENAME TO ")
ctx.FormatNode(node.Rename)
}

// AlterViewSetSchema represents an ALTER VIEW ... SET SCHEMA
// command.
type AlterViewSetSchema struct {
Schema string
}

// Format implements the NodeFormatter interface.
func (node *AlterViewSetSchema) Format(ctx *FmtCtx) {
ctx.WriteString(" SET SCHEMA ")
ctx.WriteString(node.Schema)
}

// AlterViewSetOption represents an ALTER VIEW SET | RESET ... command.
type AlterViewSetOption struct {
Reset bool
Params ViewOptions
}

// Format implements the NodeFormatter interface.
func (node *AlterViewSetOption) Format(ctx *FmtCtx) {
if node.Reset {
ctx.WriteString(" RESET ")
} else {
ctx.WriteString(" SET ")
}
ctx.FormatNode(&node.Params)
}
6 changes: 3 additions & 3 deletions postgres/parser/sem/tree/create_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func (node RoutineArgs) Format(ctx *FmtCtx) {
}
}

type functionOption int8
type FunctionOption int8

const (
OptionLanguage functionOption = 1 + iota
OptionLanguage FunctionOption = 1 + iota
OptionTransform
OptionWindow
OptionVolatility
Expand All @@ -119,7 +119,7 @@ const (
)

type RoutineOption struct {
OptionType functionOption
OptionType FunctionOption
// these members cannot be defined more than once
Language string
TransformTypes []ResolvableTypeReference
Expand Down
Loading

0 comments on commit e8eb9a9

Please sign in to comment.