Skip to content

Commit b3928b2

Browse files
committed
Fixed ANY using bindings
1 parent 5da9081 commit b3928b2

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

server/expression/any.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,19 @@ func (a *AnyExpr) WithChildren(children ...sql.Expression) (sql.Expression, erro
237237
return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), 2)
238238
}
239239

240+
leftExpr := children[0]
241+
rightExpr := children[1]
242+
// Unmodified BindVars use deferred type resolution, so we replace the deference with the left's type in array form
243+
if bv, ok := rightExpr.(*expression.BindVar); ok {
244+
if _, ok = bv.Typ.(*pgtypes.DoltgresType); !ok {
245+
if leftType, ok := leftExpr.Type().(*pgtypes.DoltgresType); ok {
246+
bv.Typ = leftType.ToArrayType()
247+
}
248+
}
249+
}
240250
anyExpr := &AnyExpr{
241-
leftExpr: children[0],
242-
rightExpr: children[1],
251+
leftExpr: leftExpr,
252+
rightExpr: rightExpr,
243253
subOperator: a.subOperator,
244254
name: a.name,
245255
}

testing/go/binding_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,42 @@ func TestBindingWithOidZero(t *testing.T) {
4949
require.NoError(t, result.Err)
5050
}
5151

52+
func TestIssue2386(t *testing.T) {
53+
// https://github.com/dolthub/doltgresql/issues/2386
54+
ctx, connection, controller := CreateServer(t, "postgres")
55+
defer controller.Stop()
56+
conn := connection.Default
57+
_, err := connection.Exec(ctx, "CREATE TABLE users (id INT PRIMARY KEY, name TEXT NOT NULL);")
58+
require.NoError(t, err)
59+
_, err = connection.Exec(ctx, "INSERT INTO users VALUES (1, 'alice'), (2, 'bob'), (3, 'carol'), (4, 'dave');")
60+
require.NoError(t, err)
61+
targetIDs := []int32{1, 3}
62+
rows, err := conn.Query(ctx,
63+
`SELECT id, name FROM users WHERE id = ANY($1)`,
64+
targetIDs,
65+
)
66+
require.NoError(t, err)
67+
defer rows.Close()
68+
i := 0
69+
for rows.Next() {
70+
var id int32
71+
var name string
72+
err = rows.Scan(&id, &name)
73+
require.NoError(t, err)
74+
switch i {
75+
case 0:
76+
require.Equal(t, int32(1), id)
77+
require.Equal(t, "alice", name)
78+
case 1:
79+
require.Equal(t, int32(3), id)
80+
require.Equal(t, "carol", name)
81+
default:
82+
t.FailNow()
83+
}
84+
i++
85+
}
86+
}
87+
5288
func TestBindingWithTextArray(t *testing.T) {
5389
ctx, connection, controller := CreateServer(t, "postgres")
5490
defer controller.Stop()

0 commit comments

Comments
 (0)