Return Postgres Arrays as Type 'arrayValue'
See original GitHub issueI know there’s some other discussion regarding Postgres arrays but this seemed to warrant a separate ticket.
Currently if I retrieve a column from Postgres of type _text
(text array), the returned metadata for the column using local-data-api looks like:
{'arrayBaseColumnType': 0, 'isAutoIncrement': False, 'isCaseSensitive': True, 'isCurrency': False, 'isSigned': False, 'label': 'container_type', 'name': 'container_type', 'nullable': 0, 'precision': 2147483647, 'scale': 0, 'schemaName': '', 'tableName': 'required_attributes', 'type': 12, 'typeName': 'text'}
Notice ‘typeName’ returning text
instead of _text
. When deployed on AWS, Data API returns for the same column:
{'arrayBaseColumnType': 0, 'isAutoIncrement': False, 'isCaseSensitive': True, 'isCurrency': False, 'isSigned': False, 'label': 'attributes', 'name': 'attributes', 'nullable': 1, 'precision': 2147483647, 'scale': 0, 'schemaName': '', 'tableName': 'required_attributes', 'type': 2003, 'typeName': '_text'}
Notice ‘typeName’ indeed reflects the Postgres type _text
.
This behavior continues with respect to the payload returned from the database, where local-data-api will return the value of the column as
{'stringValue': '{Volume}'}
where ‘Volume’ is the single item in the Postgres array. Meanwhile on AWS, Data API will return the following as value for the column:
{'arrayValue': {'stringValues': ['Volume']}}
Data API’s awareness of Postgress arrays seems to be better during read than during write!
I’m using:
- boto3
- Postgres 10.x
- Python 3.7
In code terms, the difference is captured in the two functions I maintain for local versus deployed Data API:
# Local Data API emulation currently returns arrays from database differently than
# AWS Data API, so let's establish at instantiation which function is doing our parsing.
def parse_array_local(field: Dict):
"""When running locally, parse string representation of array/set."""
return next(iter(field.values())).lstrip('{').rstrip('}').split(',')
def parse_array_deployed(field: Dict):
"""When running on AWS, return nicely formatted - albeit nested - list."""
return next(iter(field.values()))['stringValues']
Thank you, again. -jeff
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
This looks good! I tried with text/int/float arrays in Postgres and received the following correct column metadata:
Note ‘typeName’ having notation of ‘_<type>’.
And corresponding payload returned looks good, too, a couple of my test records being:
Note arrays correctly in ‘arrayValue’ object whose nested object correctly has Data API type for key and value of properly-parsed values as list.
Busy week this week, happy to look at this soon, though.