question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return Postgres Arrays as Type 'arrayValue'

See original GitHub issue

I 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:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ormu5commented, May 2, 2021

This looks good! I tried with text/int/float arrays in Postgres and received the following correct column metadata:

'columnMetadata': [
{'arrayBaseColumnType': 0, 'isAutoIncrement': False, 'isCaseSensitive': True, 'isCurrency': False, 'isSigned': False, 'label': 'name', 'name': 'name', 'nullable': 0, 'precision': 2147483647, 'scale': 0, 'schemaName': '', 'tableName': 'attributes', 'type': 12, 'typeName': 'text'}, 
{'arrayBaseColumnType': 0, 'isAutoIncrement': False, 'isCaseSensitive': True, 'isCurrency': False, 'isSigned': False, 'label': 'attributes', 'name': 'attributes', 'nullable': 1, 'precision': 2147483647, 'scale': 0, 'schemaName': '', 'tableName': 'attributes', 'type': 2003, 'typeName': '_text'}, 
{'arrayBaseColumnType': 0, 'isAutoIncrement': False, 'isCaseSensitive': False, 'isCurrency': False, 'isSigned': True, 'label': 'testing_int_array', 'name': 'testing_int_array', 'nullable': 1, 'precision': 10, 'scale': 0, 'schemaName': '', 'tableName': 'attributes', 'type': 2003, 'typeName': '_int4'}, 
{'arrayBaseColumnType': 0, 'isAutoIncrement': False, 'isCaseSensitive': False, 'isCurrency': False, 'isSigned': True, 'label': 'testing_float_array', 'name': 'testing_float_array', 'nullable': 1, 'precision': 17, 'scale': 17, 'schemaName': '', 'tableName': 'attributes', 'type': 2003, 'typeName': '_float8'}
]

Note ‘typeName’ having notation of ‘_<type>’.

And corresponding payload returned looks good, too, a couple of my test records being:

[{'stringValue': 'Thing'}, {'arrayValue': {'stringValues': ['Attribute']}}, {'arrayValue': {'longValues': [1, 3]}}, {'arrayValue': {'doubleValues': [1.231, 3.231]}}], 
[{'stringValue': 'Another Thing'}, {'arrayValue': {'stringValues': ['Attribute']}}, {'arrayValue': {'longValues': [1, 3]}}, {'arrayValue': {'doubleValues': [1.231, 3.231]}}]

Note arrays correctly in ‘arrayValue’ object whose nested object correctly has Data API type for key and value of properly-parsed values as list.

2reactions
ormu5commented, Apr 28, 2021

Busy week this week, happy to look at this soon, though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 8.15. Arrays - PostgreSQL
To write an array value as a literal constant, enclose the element values ... An array subscript expression will return null if either...
Read more >
PostgreSQL Array: Functions, Type, Example - Guru99
In PostgreSQL, we can define a column as an array of valid data types. The data type can be built-in, user-defined, or enumerated...
Read more >
How to return array of values from PostgreSQL function by INT id
You can declare INTEGER array instead of TEXT and avoid casting (counter::TEXT) as well as return type TEXT[] . (Added those for reference.)...
Read more >
PostgreSQL Array
In this tutorial, we have shown you how to work with PostgreSQL array data type and introduced you to some of the most...
Read more >
Using PostgreSQL Arrays with Golang - OpsDash
To read a Postgres array value into a Go slice, use: func getTags(db *sql.DB, title string) (tags []string) { // the select query,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found