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

Zachmu/err handling repro #54

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion postgres/messages/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

package messages

import "github.com/dolthub/doltgresql/postgres/connection"
import (
"fmt"
"strings"

"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/go-mysql-server/sql"
)

func init() {
connection.InitializeDefaultMessage(Bind{})
Expand All @@ -30,6 +36,8 @@ type Bind struct {
ResultFormatCodes []int32
}

var _ sql.DebugStringer = Bind{}

// BindParameterValue are parameter values for the Bind message.
type BindParameterValue struct {
Data []byte
Expand Down Expand Up @@ -184,3 +192,34 @@ func (m Bind) Decode(s connection.MessageFormat) (connection.Message, error) {
func (m Bind) DefaultMessage() *connection.MessageFormat {
return &bindDefault
}

// DebugString returns a debug representation of the Bind message.
func (m Bind) DebugString() string {
var builder strings.Builder

builder.WriteString("Bind {\n")
builder.WriteString(fmt.Sprintf(" DestinationPortal: %s\n", m.DestinationPortal))
builder.WriteString(fmt.Sprintf(" SourcePreparedStatement: %s\n", m.SourcePreparedStatement))
builder.WriteString(" ParameterFormatCodes: [")
for _, code := range m.ParameterFormatCodes {
builder.WriteString(fmt.Sprintf("%d, ", code))
}
builder.WriteString("]\n")

builder.WriteString(" ParameterValues: [")
for _, param := range m.ParameterValues {
// Modify this part based on the structure of BindParameterValue.
builder.WriteString(fmt.Sprintf("%#v, ", param))
}
builder.WriteString("]\n")

builder.WriteString(" ResultFormatCodes: [")
for _, code := range m.ResultFormatCodes {
builder.WriteString(fmt.Sprintf("%d, ", code))
}
builder.WriteString("]\n")

builder.WriteString("}")

return builder.String()
}
7 changes: 7 additions & 0 deletions postgres/messages/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/go-mysql-server/sql"
)

func init() {
Expand All @@ -31,6 +32,8 @@ type Describe struct {
Target string
}

var _ sql.DebugStringer = Describe{}

var describeDefault = connection.MessageFormat{
Name: "Describe",
Fields: connection.FieldGroup{
Expand Down Expand Up @@ -97,3 +100,7 @@ func (m Describe) Decode(s connection.MessageFormat) (connection.Message, error)
func (m Describe) DefaultMessage() *connection.MessageFormat {
return &describeDefault
}

func (m Describe) DebugString() string {
return fmt.Sprintf("Describe { IsPrepared: %v, Target: %s }", m.IsPrepared, m.Target)
}
14 changes: 13 additions & 1 deletion postgres/messages/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package messages

import "github.com/dolthub/doltgresql/postgres/connection"
import (
"fmt"

"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/go-mysql-server/sql"
)

func init() {
connection.InitializeDefaultMessage(Execute{})
Expand All @@ -27,6 +32,8 @@ type Execute struct {
RowMax int32
}

var _ sql.DebugStringer = Execute{}

var executeDefault = connection.MessageFormat{
Name: "Execute",
Fields: connection.FieldGroup{
Expand Down Expand Up @@ -80,3 +87,8 @@ func (m Execute) Decode(s connection.MessageFormat) (connection.Message, error)
func (m Execute) DefaultMessage() *connection.MessageFormat {
return &executeDefault
}

func (m Execute) DebugString() string {
return fmt.Sprintf("Execute {\n Portal: %s\n RowMax: %d\n}", m.Portal, m.RowMax)
}

28 changes: 27 additions & 1 deletion postgres/messages/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

package messages

import "github.com/dolthub/doltgresql/postgres/connection"
import (
"fmt"
"strings"

"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/go-mysql-server/sql"
)

func init() {
connection.InitializeDefaultMessage(Parse{})
Expand All @@ -28,6 +34,8 @@ type Parse struct {
ParameterObjectIDs []int32
}

var _ sql.DebugStringer = Parse{}

var parseDefault = connection.MessageFormat{
Name: "Parse",
Fields: connection.FieldGroup{
Expand Down Expand Up @@ -104,3 +112,21 @@ func (m Parse) Decode(s connection.MessageFormat) (connection.Message, error) {
func (m Parse) DefaultMessage() *connection.MessageFormat {
return &parseDefault
}


// DebugString returns a debug representation of the Parse message.
func (m Parse) DebugString() string {
var builder strings.Builder

builder.WriteString("Parse {\n")
builder.WriteString(fmt.Sprintf(" Name: %s\n", m.Name))
builder.WriteString(fmt.Sprintf(" Query: %s\n", m.Query))
builder.WriteString(" ParameterObjectIDs: [")
for _, id := range m.ParameterObjectIDs {
builder.WriteString(fmt.Sprintf("%d, ", id))
}
builder.WriteString("]\n")
builder.WriteString("}")

return builder.String()
}
13 changes: 12 additions & 1 deletion postgres/messages/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package messages

import "github.com/dolthub/doltgresql/postgres/connection"
import (
"fmt"

"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/go-mysql-server/sql"
)

func init() {
connection.InitializeDefaultMessage(Query{})
Expand All @@ -26,6 +31,8 @@ type Query struct {
String string
}

var _ sql.DebugStringer = &Query{}

var queryDefault = connection.MessageFormat{
Name: "Query",
Fields: connection.FieldGroup{
Expand Down Expand Up @@ -72,3 +79,7 @@ func (m Query) Decode(s connection.MessageFormat) (connection.Message, error) {
func (m Query) DefaultMessage() *connection.MessageFormat {
return &queryDefault
}

func (m Query) DebugString() string {
return fmt.Sprintf("Query { %s }", m.String)
}
10 changes: 9 additions & 1 deletion postgres/messages/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package messages

import "github.com/dolthub/doltgresql/postgres/connection"
import (
"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/go-mysql-server/sql"
)

func init() {
connection.InitializeDefaultMessage(Sync{})
Expand Down Expand Up @@ -43,6 +46,7 @@ var syncDefault = connection.MessageFormat{
}

var _ connection.Message = Sync{}
var _ sql.DebugStringer = Sync{}

// Encode implements the interface connection.Message.
func (m Sync) Encode() (connection.MessageFormat, error) {
Expand All @@ -61,3 +65,7 @@ func (m Sync) Decode(s connection.MessageFormat) (connection.Message, error) {
func (m Sync) DefaultMessage() *connection.MessageFormat {
return &syncDefault
}

func (m Sync) DebugString() string {
return "Sync {}"
}
14 changes: 7 additions & 7 deletions postgres/parser/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var (
DBoolFalse = &constDBoolFalse

// DNull is the NULL Datum.
DNull Datum = dNull{}
DNull Datum = NullLiteral{}

// DTimeMaxTimeRegex is a compiled regex for parsing the 24:00 time value.
DTimeMaxTimeRegex = regexp.MustCompile(`^([0-9-]*(\s|T))?\s*24:00(:00(.0+)?)?\s*$`)
Expand Down Expand Up @@ -1601,18 +1601,18 @@ func (d *DTuple) ContainsNull() bool {
return false
}

type dNull struct{}
type NullLiteral struct{}

// ResolvedType implements the TypedExpr interface.
func (dNull) ResolvedType() *types.T {
func (NullLiteral) ResolvedType() *types.T {
return types.Unknown
}

// AmbiguousFormat implements the Datum interface.
func (dNull) AmbiguousFormat() bool { return false }
func (NullLiteral) AmbiguousFormat() bool { return false }

// Format implements the NodeFormatter interface.
func (dNull) Format(ctx *FmtCtx) {
func (NullLiteral) Format(ctx *FmtCtx) {
if ctx.HasFlags(fmtPgwireFormat) {
// NULL sub-expressions in pgwire text values are represented with
// the empty string.
Expand All @@ -1622,7 +1622,7 @@ func (dNull) Format(ctx *FmtCtx) {
}

// Size implements the Datum interface.
func (d dNull) Size() uintptr {
func (d NullLiteral) Size() uintptr {
return unsafe.Sizeof(d)
}

Expand Down Expand Up @@ -2050,7 +2050,7 @@ func wrapWithOid(d Datum, oid oid.Oid) Datum {
case *DInt:
case *DString:
case *DArray:
case dNull, *DOidWrapper:
case NullLiteral, *DOidWrapper:
panic(errors.AssertionFailedf("cannot wrap %T with an Oid", v))
default:
// Currently only *DInt, *DString, *DArray are hooked up to work with
Expand Down
6 changes: 3 additions & 3 deletions postgres/parser/sem/tree/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,6 @@ func (node *UnaryExpr) String() string { return AsString(node) }
func (node DefaultVal) String() string { return AsString(node) }
func (node PartitionMaxVal) String() string { return AsString(node) }
func (node PartitionMinVal) String() string { return AsString(node) }
func (node *Placeholder) String() string { return AsString(node) }
func (node dNull) String() string { return AsString(node) }
func (list *NameList) String() string { return AsString(list) }
func (node *Placeholder) String() string { return AsString(node) }
func (d NullLiteral) String() string { return AsString(d) }
func (list *NameList) String() string { return AsString(list) }
4 changes: 2 additions & 2 deletions postgres/parser/sem/tree/pgwire_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (d *DTuple) pgwireFormat(ctx *FmtCtx) {
for _, v := range d.D {
ctx.WriteString(comma)
switch dv := UnwrapDatum(v).(type) {
case dNull:
case NullLiteral:
case *DString:
pgwireFormatStringInTuple(&ctx.Buffer, string(*dv))
case *DCollatedString:
Expand Down Expand Up @@ -121,7 +121,7 @@ func (d *DArray) pgwireFormat(ctx *FmtCtx) {
for _, v := range d.Array {
ctx.WriteString(comma)
switch dv := UnwrapDatum(v).(type) {
case dNull:
case NullLiteral:
ctx.WriteString("NULL")
case *DString:
pgwireFormatStringInArray(&ctx.Buffer, string(*dv))
Expand Down
2 changes: 1 addition & 1 deletion postgres/parser/sem/tree/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ func (d *DOidWrapper) TypeCheck(_ context.Context, _ *SemaContext, _ *types.T) (

// TypeCheck implements the Expr interface. It is implemented as an idempotent
// identity function for Datum.
func (d dNull) TypeCheck(_ context.Context, _ *SemaContext, desired *types.T) (TypedExpr, error) {
func (d NullLiteral) TypeCheck(_ context.Context, _ *SemaContext, desired *types.T) (TypedExpr, error) {
return d, nil
}

Expand Down
2 changes: 1 addition & 1 deletion postgres/parser/sem/tree/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func (expr *DUuid) Walk(_ Visitor) Expr { return expr }
func (expr *DIPAddr) Walk(_ Visitor) Expr { return expr }

// Walk implements the Expr interface.
func (expr dNull) Walk(_ Visitor) Expr { return expr }
func (d NullLiteral) Walk(_ Visitor) Expr { return d }

// Walk implements the Expr interface.
func (expr *DString) Walk(_ Visitor) Expr { return expr }
Expand Down
2 changes: 2 additions & 0 deletions server/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ func nodeExpr(node tree.Expr) (vitess.Expr, error) {
Name: vitess.NewColIdent(node.Parts[0]),
Qualifier: tableName,
}, nil
case tree.NullLiteral:
return &vitess.NullVal{}, nil
case nil:
return nil, nil
default:
Expand Down
1 change: 1 addition & 0 deletions server/converted_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ type ConvertedQuery struct {
String string
AST vitess.Statement
}

12 changes: 12 additions & 0 deletions server/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (
"sync/atomic"

"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/mysql_db"
"github.com/dolthub/vitess/go/mysql"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/sirupsen/logrus"

"github.com/dolthub/doltgresql/postgres/connection"
"github.com/dolthub/doltgresql/postgres/messages"
Expand Down Expand Up @@ -146,6 +148,12 @@ func (l *Listener) HandleConnection(conn net.Conn) {
returnErr = err
return
}

if ds, ok := message.(sql.DebugStringer); ok {
logrus.Warnf("Received message: %s", ds.DebugString())
} else {
logrus.Warnf("Received message: %v", message)
}

stop, endOfMessages, err := l.handleMessage(message, conn, mysqlConn, preparedStatements, portals)
if err != nil || endOfMessages {
Expand Down Expand Up @@ -253,6 +261,7 @@ func (l *Listener) handleMessage(
return true, false, nil
case messages.Execute:
// TODO: implement the RowMax
logrus.Warnf("executing portal %s with contents %v", message.Portal, portals[message.Portal])
return false, false, l.execute(conn, mysqlConn, portals[message.Portal])
case messages.Query:
handled, err := l.handledPSQLCommands(conn, mysqlConn, message.String)
Expand Down Expand Up @@ -305,6 +314,7 @@ func (l *Listener) handleMessage(
case messages.Sync:
return false, true, nil
case messages.Bind:
logrus.Warnf("binding portal %q to prepared statement %s", message.DestinationPortal, message.SourcePreparedStatement)
// TODO: fully support prepared statements
portals[message.DestinationPortal] = preparedStatements[message.SourcePreparedStatement]
return false, false, connection.Send(conn, messages.BindComplete{})
Expand Down Expand Up @@ -411,6 +421,8 @@ func (l *Listener) execute(conn net.Conn, mysqlConn *mysql.Conn, query Converted

// describe handles the description of the given query. This will post the ParameterDescription and RowDescription messages.
func (l *Listener) describe(conn net.Conn, mysqlConn *mysql.Conn, message messages.Describe, statement ConvertedQuery) error {
logrus.Warnf("describing statement %v", statement)

//TODO: fully support prepared statements
if err := connection.Send(conn, messages.ParameterDescription{
ObjectIDs: nil,
Expand Down
Loading
Loading