Skip to content

Commit b5b0193

Browse files
authored
Merge pull request #887 from dolthub/fulghum/ping
Bug fix: Make `db.Ping(ctx)` work correctly with Doltgres
2 parents c84a300 + 2c3276e commit b5b0193

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

postgres/parser/parser/parse.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"strings"
3939

4040
"github.com/cockroachdb/errors"
41+
vitess "github.com/dolthub/vitess/go/vt/sqlparser"
4142

4243
"github.com/dolthub/doltgresql/postgres/parser/pgcode"
4344
"github.com/dolthub/doltgresql/postgres/parser/pgerror"
@@ -120,8 +121,10 @@ func (p *Parser) parseOneWithDepth(depth int, sql string) (Statement, error) {
120121
if err != nil {
121122
return Statement{}, err
122123
}
123-
if len(stmts) != 1 {
124+
if len(stmts) > 1 {
124125
return Statement{}, errors.AssertionFailedf("expected 1 statement, but found %d", len(stmts))
126+
} else if len(stmts) == 0 {
127+
return Statement{}, vitess.ErrEmpty
125128
}
126129
return stmts[0], nil
127130
}

postgres/parser/parser/sql/sql_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (p *PostgresParser) ParseWithOptions(ctx context.Context, query string, del
5656
return nil, "", "", fmt.Errorf("only a single statement at a time is currently supported")
5757
}
5858
if len(stmts) == 0 {
59-
return nil, q, "", nil
59+
return nil, q, "", vitess.ErrEmpty
6060
}
6161

6262
vitessAST, err := ast.Convert(stmts[0])

testing/go/framework.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func RunScript(t *testing.T, script ScriptTest, normalizeRows bool) {
130130
Default: pgxConn,
131131
Current: pgxConn,
132132
}
133+
require.NoError(t, pgxConn.Ping(ctx))
133134
defer func() {
134135
conn.Close(ctx)
135136
}()
@@ -309,6 +310,9 @@ func CreateServer(t *testing.T, database string) (context.Context, *Connection,
309310

310311
conn, err := pgx.Connect(ctx, fmt.Sprintf("postgres://postgres:[email protected]:%d/%s", port, database))
311312
require.NoError(t, err)
313+
314+
// Ping the connection to test that it's alive, and also to test that Doltgres handles empty queries correctly
315+
require.NoError(t, conn.Ping(ctx))
312316
return ctx, &Connection{
313317
Default: conn,
314318
Current: conn,

testing/go/smoke_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ func TestSmokeTests(t *testing.T) {
7171
Query: "SELECT NULL = NULL",
7272
Expected: []sql.Row{{nil}},
7373
},
74+
{
75+
Query: ";",
76+
Expected: []sql.Row{},
77+
},
78+
{
79+
Query: " ; ",
80+
Expected: []sql.Row{},
81+
},
82+
{
83+
Query: "-- this is only a comment",
84+
Expected: []sql.Row{},
85+
},
7486
},
7587
},
7688
{

testing/go/sql_parser_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package _go
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
vitess "github.com/dolthub/vitess/go/vt/sqlparser"
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/dolthub/doltgresql/postgres/parser/parser/sql"
25+
)
26+
27+
// TestParserBehaviors tests behaviors, such as empty statement handling, for the Doltgres parser.
28+
func TestParserBehaviors(t *testing.T) {
29+
parser := sql.NewPostgresParser()
30+
31+
// Parser implementations should return Vitess' ErrEmpty error for empty statements.
32+
t.Run("empty statement parsing", func(t *testing.T) {
33+
emptyStatements := []string{"", " ", "\t", ";", "-- comment", "/* comment */"}
34+
for _, statement := range emptyStatements {
35+
parsed, err := parser.ParseSimple(statement)
36+
require.Nil(t, parsed)
37+
require.ErrorIs(t, err, vitess.ErrEmpty)
38+
39+
parsed, _, _, err = parser.Parse(nil, statement, true)
40+
require.Nil(t, parsed)
41+
require.ErrorIs(t, err, vitess.ErrEmpty)
42+
43+
parsed, _, err = parser.ParseOneWithOptions(context.Background(), statement, vitess.ParserOptions{})
44+
require.Nil(t, parsed)
45+
require.ErrorIs(t, err, vitess.ErrEmpty)
46+
47+
parsed, _, _, err = parser.ParseWithOptions(context.Background(), statement, ';', false, vitess.ParserOptions{})
48+
require.Nil(t, parsed)
49+
require.ErrorIs(t, err, vitess.ErrEmpty)
50+
}
51+
})
52+
}

0 commit comments

Comments
 (0)