|
I'm working on writing some code that will pull large engineering datasets from Postgres and process them using numpy. I've written type-converters to override the defaults and return numpy types from my queries, but I'm concerned that this may be less than ideal performance-wise for large datasets. (I am currently just prototyping using toy data.)
Thank you for any hints! import asyncpg
import numpy as np
def float32_decoder( buf ):
return np.frombuffer( buf, dtype='>f4')[0]
def float32_encoder( val ):
if not isinstance(val, np.float32):
val = np.float32(val)
return val.newbyteorder('>').tobytes()
def int32_encoder( val ):
if not isinstance(val, np.int32):
val = np.int32(val)
return val.newbyteorder('>').tobytes()
def int32_decoder( buf ):
return np.frombuffer( buf, dtype='>i4')[0]
def int16_encoder( val ):
if not isinstance(val, np.int16):
val = np.int16(val)
return val.newbyteorder('>').tobytes()
def int16_decoder( buf ):
return np.frombuffer( buf, dtype='>i2')[0]
async def register_adapters(conn: asyncpg.Connection):
"""
register type converters to conn
"""
await conn.set_type_codec(
'real',
encoder = float32_encoder,
decoder = float32_decoder,
schema = 'pg_catalog',
format = 'binary'
)
await conn.set_type_codec(
'integer',
encoder = int32_encoder,
decoder = int32_decoder,
schema = 'pg_catalog',
format = 'binary'
)
await conn.set_type_codec(
'smallint',
encoder = int16_encoder,
decoder = int16_decoder,
schema = 'pg_catalog',
format = 'binary'
) |
Answered by
j-carson
Aug 27, 2021
Replies: 2 comments 4 replies
There is: |
4 replies
|
So after trying various options, it seems like the most performant one is to let asyncpg return the native python type with its faster code and cast it back to the desired "right-sized" type later. |
0 replies
Answer selected by
j-carson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So after trying various options, it seems like the most performant one is to let asyncpg return the native python type with its faster code and cast it back to the desired "right-sized" type later.