Skip to content

Commit

Permalink
support some postgres syntaxes - 1 (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp authored Jan 8, 2024
1 parent 9b74d0e commit e90f5d4
Show file tree
Hide file tree
Showing 13 changed files with 1,477 additions and 1,110 deletions.
264 changes: 195 additions & 69 deletions postgres/parser/parser/sql.y

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions postgres/parser/sem/tree/alter_aggregate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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.

// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package tree

import "strings"

var _ Statement = &AlterAggregate{}

// AlterAggregate represents a ALTER AGGREGATE statement.
type AlterAggregate struct {
Name Name
AggSig *AggregateSignature
Rename Name
Owner string
Schema string
}

// Format implements the NodeFormatter interface.
func (node *AlterAggregate) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER AGGREGATE ")
ctx.FormatNode(&node.Name)
ctx.WriteString(" (")
node.AggSig.Format(ctx)
ctx.WriteString(" )")

if node.Rename != "" {
ctx.WriteString(" RENAME ")
ctx.FormatNode(&node.Rename)
} else if node.Owner != "" {
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
} else if node.Schema != "" {
ctx.WriteString(" SET SCHEMA ")
ctx.FormatNameP(&node.Schema)
}
}

// AggregateSignature represents an aggregate_signature clause.
type AggregateSignature struct {
All bool
Arg *AggregateArg
OrderBy *AggregateArg
}

// Format implements the NodeFormatter interface.
func (node *AggregateSignature) Format(ctx *FmtCtx) {
if node.All {
ctx.WriteString(" * ")
} else {
if node.Arg != nil {
ctx.WriteByte(' ')
node.Arg.Format(ctx)
}
if node.OrderBy != nil {
ctx.WriteString(" ORDER BY ")
node.Arg.Format(ctx)
}
}
}

// AggregateArg represents an aggregate argument(s).
type AggregateArg struct {
Mode string
Name Name
Types []ResolvableTypeReference
}

// Format implements the NodeFormatter interface.
func (node *AggregateArg) Format(ctx *FmtCtx) {
ctx.WriteString(node.Mode)
if node.Name != "" {
ctx.WriteByte(' ')
ctx.FormatNode(&node.Name)
}
types := make([]string, len(node.Types))
for i, t := range node.Types {
types[i] = t.SQLString()
}

if len(types) > 0 {
ctx.WriteByte(' ')
ctx.WriteString(strings.Join(types, ", "))
}
}
54 changes: 54 additions & 0 deletions postgres/parser/sem/tree/alter_collation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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.

// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package tree

var _ Statement = &AlterCollation{}

// AlterCollation represents a ALTER COLLATION statement.
type AlterCollation struct {
Name Name
RefreshVersion bool
Rename Name
Owner string
Schema string
}

// Format implements the NodeFormatter interface.
func (node *AlterCollation) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER COLLATION ")
ctx.FormatNode(&node.Name)
if node.RefreshVersion {
ctx.WriteString(" REFRESH VERSION")
} else if node.Rename != "" {
ctx.WriteString(" RENAME ")
ctx.FormatNode(&node.Rename)
} else if node.Owner != "" {
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
} else if node.Schema != "" {
ctx.WriteString(" SET SCHEMA ")
ctx.FormatNameP(&node.Schema)
}
}
54 changes: 54 additions & 0 deletions postgres/parser/sem/tree/alter_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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.

// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package tree

var _ Statement = &AlterConversion{}

// AlterConversion represents a ALTER COLLATION statement.
type AlterConversion struct {
Name Name
RefreshVersion bool
Rename Name
Owner string
Schema string
}

// Format implements the NodeFormatter interface.
func (node *AlterConversion) Format(ctx *FmtCtx) {
ctx.WriteString("ALTER COLLATION ")
ctx.FormatNode(&node.Name)
if node.RefreshVersion {
ctx.WriteString(" REFRESH VERSION")
} else if node.Rename != "" {
ctx.WriteString(" RENAME ")
ctx.FormatNode(&node.Rename)
} else if node.Owner != "" {
ctx.WriteString(" OWNER TO ")
ctx.FormatNameP(&node.Owner)
} else if node.Schema != "" {
ctx.WriteString(" SET SCHEMA ")
ctx.FormatNameP(&node.Schema)
}
}
29 changes: 28 additions & 1 deletion postgres/parser/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,35 @@ var _ CCLOnlyStatement = &Import{}
var _ CCLOnlyStatement = &Export{}
var _ CCLOnlyStatement = &ScheduledBackup{}

// StatementType implements the Statement interface.
func (*AlterAggregate) StatementType() StatementType { return DDL }

// StatementTag returns a short string identifying the type of statement.
func (*AlterAggregate) StatementTag() string { return "ALTER AGGREGATE" }

func (*AlterAggregate) hiddenFromShowQueries() {}

// StatementType implements the Statement interface.
func (*AlterCollation) StatementType() StatementType { return DDL }

// StatementTag returns a short string identifying the type of statement.
func (*AlterCollation) StatementTag() string { return "ALTER COLLATION" }

func (*AlterCollation) hiddenFromShowQueries() {}

// StatementType implements the Statement interface.
func (*AlterConversion) StatementType() StatementType { return DDL }

// StatementTag returns a short string identifying the type of statement.
func (*AlterConversion) StatementTag() string { return "ALTER CONVERSION" }

func (*AlterConversion) hiddenFromShowQueries() {}

// StatementType implements the Statement interface.
func (*AlterDatabaseOwner) StatementType() StatementType { return DDL }

// StatementTag returns a short string identifying the type of statement.
func (*AlterDatabaseOwner) StatementTag() string { return "ALTER DATABSE OWNER" }
func (*AlterDatabaseOwner) StatementTag() string { return "ALTER DATABASE OWNER" }

func (*AlterDatabaseOwner) hiddenFromShowQueries() {}

Expand Down Expand Up @@ -1016,6 +1040,9 @@ func (*ValuesClause) StatementType() StatementType { return Rows }
// StatementTag returns a short string identifying the type of statement.
func (*ValuesClause) StatementTag() string { return "VALUES" }

func (n *AlterAggregate) String() string { return AsString(n) }
func (n *AlterCollation) String() string { return AsString(n) }
func (n *AlterConversion) String() string { return AsString(n) }
func (n *AlterIndex) String() string { return AsString(n) }
func (n *AlterDatabaseOwner) String() string { return AsString(n) }
func (n *AlterSchema) String() string { return AsString(n) }
Expand Down
12 changes: 6 additions & 6 deletions testing/generation/command_docs/output/abort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestAbort(t *testing.T) {
Converts("ABORT"),
Converts("ABORT WORK"),
Converts("ABORT TRANSACTION"),
Unimplemented("ABORT AND CHAIN"),
Unimplemented("ABORT WORK AND CHAIN"),
Unimplemented("ABORT TRANSACTION AND CHAIN"),
Unimplemented("ABORT AND NO CHAIN"),
Unimplemented("ABORT WORK AND NO CHAIN"),
Unimplemented("ABORT TRANSACTION AND NO CHAIN"),
Converts("ABORT AND CHAIN"),
Converts("ABORT WORK AND CHAIN"),
Converts("ABORT TRANSACTION AND CHAIN"),
Converts("ABORT AND NO CHAIN"),
Converts("ABORT WORK AND NO CHAIN"),
Converts("ABORT TRANSACTION AND NO CHAIN"),
}
RunTests(t, tests)
}
Loading

0 comments on commit e90f5d4

Please sign in to comment.