Skip to content

Commit 70aa569

Browse files
authored
Merge pull request #2151 from dolthub/zachmu/empty-message
Correctly support empty messages
2 parents bf08254 + 76211ca commit 70aa569

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

server/connection_handler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ func (h *ConnectionHandler) handleQuery(message *pgproto3.Query) (endOfMessages
453453
return endOfMessages, err
454454
}
455455

456+
// empty query special case
457+
if query.AST == nil {
458+
return true, h.send(&pgproto3.EmptyQueryResponse{})
459+
}
460+
456461
return true, h.query(query)
457462
}
458463

testing/go/framework.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ type ScriptTestAssertion struct {
129129
CopyFromStdInFile string
130130
}
131131

132+
// EmptyCommandTag is special command tag placeholder to check for the empty string
133+
const EmptyCommandTag = "EMPTY_COMMAND_TAG"
134+
132135
// Connection contains the default and current connections.
133136
type Connection struct {
134137
Default *pgx.Conn
@@ -249,10 +252,14 @@ func runScript(t *testing.T, ctx context.Context, script ScriptTest, conn *Conne
249252
require.NoError(t, err)
250253
}
251254
} else if assertion.ExpectedTag != "" {
252-
// check for command tag
253255
commandTag, err := conn.Exec(ctx, assertion.Query)
254256
require.NoError(t, err)
255-
assert.Equal(t, assertion.ExpectedTag, commandTag.String())
257+
tag := assertion.ExpectedTag
258+
if tag == EmptyCommandTag {
259+
tag = ""
260+
}
261+
262+
assert.Equal(t, tag, commandTag.String())
256263
} else {
257264
rows, err := conn.Query(ctx, assertion.Query, assertion.BindVars...)
258265
require.NoError(t, err)

testing/go/smoke_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,25 @@ func TestSmokeTests(t *testing.T) {
823823
},
824824
})
825825
}
826+
827+
func TestEmptyQuery(t *testing.T) {
828+
RunScripts(t, []ScriptTest{
829+
{
830+
// TODO: we want to be able to assert that the empty query returns a specific postgres backend message,
831+
// EmptyQueryResponse. The pg library automatically converts this response to an empty-string CommandTag,
832+
// which we can't tell apart from other empty CommandTag responses. We do assert that the command tag is empty,
833+
// but it would nice to be able to assert a particular message type.
834+
Name: "Empty query test",
835+
Assertions: []ScriptTestAssertion{
836+
{
837+
Query: ";",
838+
ExpectedTag: EmptyCommandTag,
839+
},
840+
{
841+
Query: " ",
842+
ExpectedTag: EmptyCommandTag,
843+
},
844+
},
845+
},
846+
})
847+
}

0 commit comments

Comments
 (0)