pgx icon indicating copy to clipboard operation
pgx copied to clipboard

Passing slice of int64 to a query fails with 'cannot find encode plan'

Open jarri-abidi opened this issue 1 year ago • 5 comments

Description when i pass in a slice of []int64 to a query with IN clause i.e. select id from t where id in ($1);, it returns the following error:

failed to encode args[0]: unable to encode []int64{1, 2, 3} into binary format for int8 (OID 20): cannot find encode plan

To Reproduce

package main

import (
	"context"
	"log"
	"os"
	"testing"
	
	"github.com/stretchr/testify/assert"
	"github.com/jackc/pgx/v5"
)

func TestInSliceQuery(t *testing.T) {
	conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
	defer closeConn(t, conn)

	rows, err := conn.Query(context.TODO(), "create temporary table t2 (id bigserial);")
	assert.NoError(t, err)
	rows.Close()

	args := []int64{1, 2, 3}
	rows, err = conn.Query(context.TODO(), "select id from t2 where id in ($1);", args)
	assert.NoError(t, err)
	rows.Close()
}

Expected behavior I'm pretty sure this used to work before but not sure what happened suddenly. I checked dependencies and runtime and nothing seems to have changed.

Version

  • Go: go1.21.3 darwin/amd64
  • PostgreSQL: PostgreSQL 16.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
  • pgx: v5.4.3

jarri-abidi avatar Oct 28 '23 14:10 jarri-abidi

See https://github.com/jackc/pgx/issues/1630.

jackc avatar Oct 28 '23 21:10 jackc

I can confirm this, using []int32 slice type as argument for IN operator in WHERE caluse throws error below:

failed to encode args[0]: unable to encode []int32{} into binary format for int8 (OID 20): cannot find encode plan

reza-ebrahimi avatar Jan 25 '24 14:01 reza-ebrahimi

@reza-ebrahimi See https://github.com/jackc/pgx/issues/1630#issuecomment-1576823340

jackc avatar Jan 25 '24 15:01 jackc

Thank you, sir!

rxynrg avatar Apr 23 '24 18:04 rxynrg

problem caused by things like this where f.id in (@facility_ids) (problematic case ) where f.id = any (@facility_ids) (working version ) ANY is PostgreSQL-specific.

DavidAbgaryan avatar Jun 29 '24 20:06 DavidAbgaryan