Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Query fails in redshift. #330

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

Closed
glennpierce opened this issue Mar 14, 2018 · 14 comments
Closed

Query fails in redshift. #330

glennpierce opened this issue Mar 14, 2018 · 14 comments

Comments

@glennpierce
Copy link

glennpierce commented Mar 14, 2018

Hi

I have some code of a simple query

let results : postgres::Result<Rows> = self.conn.query("SELECT id, name FROM sensors WHERE id = ANY($1)", &[&sensor_ids]);

where

sensor_ids is sensor_ids : Vec<i32>

This works fine against my local postgres db (9.5)

I am now trying to move to redshift but I get the error

Err(Error(Db(DbError { severity: "ERROR", parsed_severity: None, code: SqlState("22P03"), message: "invalid array flags", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("/home/ec2-user/padb/src/pg/src/backend/utils/adt/arrayfuncs.c"), line: Some(1174), routine: Some("array_recv") })))
Any idea what this could be ?

PS.

If I query through my ide
SELECT id, name FROM sensors WHERE id = ANY(ARRAY[596304,597992,610978,597998])

It works

Thanks

@sfackler
Copy link
Owner

It looks like Redshift is looking for a Vec<i64> instead of a Vec<i32> for the parameter.

@glennpierce
Copy link
Author

glennpierce commented Mar 14, 2018

Even though the column is a integer which according to the docs is
INTEGER, INT, or INT4 ?

@sfackler
Copy link
Owner

Oh wait, I was responding to the initial error of Err(Error(Conversion(WrongType(Type(Int8Array))))). What changed to go from that error to the "invalid array flags" one?

@glennpierce
Copy link
Author

I had the column as biginteger so I changed it to integer which it should have been. Sorry I should have maybe created another issue.

@sfackler
Copy link
Owner

The Redshift docs claim that arrays aren't supported at all, but I'm not sure why a query using them would fail in that way: https://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-datatypes.html.

@glennpierce
Copy link
Author

That's weird as I can issue
SELECT id, name FROM sensors WHERE id = ANY(ARRAY[596304,597992,610978,597998]) from my database ide and it works fine.

Also I am using ANY here as I don't think the IN clause works with rust-postgres ?

@sfackler
Copy link
Owner

It works just fine - it just takes a sequence of separate values rather than an array of values.

One possibility is that Redshift is forked off of Postgres 8.1 or older, where they didn't support null array elements (support was added in 2005!). We should be able to make this work by only setting the null flag if there are actually null values.

@glennpierce
Copy link
Author

I have tried the IN clause now as well. (hacked as a single str)

ie
let results : postgres::Result<Rows> = conn.query("SELECT id, name FROM sensors WHERE id IN (596304,597992,610978,597998)", &[]);

From rust-postgres I get the same error of invalid array flags

@sfackler
Copy link
Owner

If that exact query generates that error, then that seems like a bug on the Redshift side of things. We're not producing any arrays in that case, we're just sending a string to Redshift.

@glennpierce
Copy link
Author

Odd. If I copy and paste that query into my ide (datagrip) it works fine.
I must be doing something silly in the code then.

@glennpierce
Copy link
Author

Out of curiosity is there away to pass an array to a sequence so I can do something like

conn.query("SELECT id, name FROM sensors WHERE id IN ($1)", &[596304,597992,610978,597998]);

@sfackler
Copy link
Owner

Nope - #326 (comment)

@glennpierce
Copy link
Author

Ok I got the In clause working.

So to recap in redshift

SELECT id, name FROM sensors WHERE id = ANY(ARRAY[596304,597992,610978,597998])
fails
but

let results : postgres::Result<Rows> = conn.query("SELECT id, name FROM sensors WHERE id IN (596304,597992,610978,597998)", &[]);

works.

Thanks

@sfackler
Copy link
Owner

Wait, so even ARRAY[...] literals in the query string are broken with Redshift? I though it was just transferring array values as parameters that didn't work.

sfackler added a commit that referenced this issue Mar 15, 2018
This is necessary to support arrays of non-null values on Postgres 8.1
and older.

Closes #330
@sfackler sfackler closed this as completed Apr 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants