-
Notifications
You must be signed in to change notification settings - Fork 18
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
How should I handle arrays of structs, #17
Comments
See #5 |
Sorry I think I got confused by the mention of struct columns. Seems like the issue is something else, let me take a look. Regardless, the error message should be better |
My best guess is that it's the transformation of the arrays of struct values to json.
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: Adrian Garcia Badaracco ***@***.***>
Sent: Thursday, June 15, 2023 8:46:35 PM
To: adriangb/pgpq ***@***.***>
Cc: Daniel Rappaport ***@***.***>; Author ***@***.***>
Subject: Re: [adriangb/pgpq] How should I handle arrays of structs, (Issue #17)
Sorry I think I got confused by the mention of struct columns. Seems like the issue is something else, let me take a look.
Regardless, the error message should be better
—
Reply to this email directly, view it on GitHub<#17 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A7NUCNWGUG47ESW6FKQBTQLXLPJJXANCNFSM6AAAAAAZISQWGA>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
I think the issue is how you were creating the encoders. In particular I don't see import psycopg
import pyarrow as pa
from pgpq import ArrowToPostgresBinaryEncoder
from pgpq.encoders import ListEncoderBuilder, LargeStringEncoderBuilder
from pgpq.schema import Jsonb
fields = [
('a', pa.int32()),
('b', pa.bool_()),
]
schema = pa.schema([
('col', pa.list_(pa.large_string())),
])
batch = pa.RecordBatch.from_arrays(
[
pa.array([['{"a":123,"b":false}', '{"a":456,"b":true}']])
],
schema=schema,
)
encoder = ArrowToPostgresBinaryEncoder.new_with_encoders(
batch.schema,
{
'col': ListEncoderBuilder.new_with_inner(
schema.field(0),
LargeStringEncoderBuilder.new_with_output(
schema.field(0).type.field(0),
Jsonb(),
)
)
}
)
ddl = f"DROP TABLE IF EXISTS data;CREATE TABLE data (data jsonb[]);"
with psycopg.connect("postgres://postgres:postgres@localhost:5432/postgres") as conn:
with conn.cursor() as cursor:
cursor.execute(ddl) # type: ignore
with cursor.copy("COPY data FROM STDIN WITH (FORMAT BINARY)") as copy:
copy.write(encoder.write_header())
copy.write(encoder.write_batch(batch))
copy.write(encoder.finish())
conn.commit() I do feel like there should be a simpler way to build these encoders, but I'm not sure what the right API is just yet. |
I noticed that you have single quotes surrounding the struct in the array of structs. So I will do that transformation:
And I am receiving the "jsonb not-supported error", and I am not clear the work-around while using the release 0.7.3. I tried making the receiving table JSON, but I don't know how to set the output type of the encoder to Json().
|
The single quotes are just because json uses double quotes so it avoids escaping. You should get the same result if you convert a struct to json, I just skipped that step to simplify the example.
I'm not sure I'm understanding what issue you are having or where you're getting that error. The jist of it is that you need to make the encoder a list of Jsonb, what you have there looks about right, and make the column in the table |
I did not change your code that you sent 12 hours ago... Any ideas? Warm regards, |
Are you sure you're using |
yes, but just to make sure, I am going to create a venv |
try doing something like |
It worked, so is that functionally the same as [{'a': 123, 'b': False}, {'a': 456, 'b': True}] ? |
Yes at that point this is in Postgres, what I gave you is just the Postgres syntax for working with arrays: https://www.postgresql.org/docs/current/arrays.html |
It worked, so finish off this evaluation, I need to create an encoder for all the arrays of structs:
and then possibly apply the uint64 -> uint 32 transformation that I saw in the outstanding issues. Hopefully it will just work!! Much appreciated! |
I think this is resolved, can we close this issue? |
I am blocked by this. I reduced the number of fields to just one to try to isolate the problem.:
When converting to json, I need to set orient='records' so the metadata falls away. I don't know how to stick that into this function:
THis is how is was able to converrt the items in the arrays of structs to json strings, but I think it is capturing the metadata, but I am having difficulty troubleshooting:
|
How do I handle a field with array of struct values like the following? I will send a full example shortly :
I made an attempt with the following encoder but it fails on the copy because the output tye is Jsonb() instead of jsonb[]:
Error:
The text was updated successfully, but these errors were encountered: