Skip to content

Commit

Permalink
936 Handle setup of nested arrays containing BigInt (#937)
Browse files Browse the repository at this point in the history
* handle setup of nested arrays

* added a test for nested arrays
  • Loading branch information
dantownsend authored Mar 4, 2024
1 parent 4f66bd8 commit 894efe5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
9 changes: 9 additions & 0 deletions piccolo/columns/column_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2588,6 +2588,15 @@ def column_type(self):
return "ARRAY"
raise Exception("Unrecognized engine type")

def _setup_base_column(self, table_class: t.Type[Table]):
"""
Called from the ``Table.__init_subclass__`` - makes sure
that the ``base_column`` has a reference to the parent table.
"""
self.base_column._meta._table = table_class
if isinstance(self.base_column, Array):
self.base_column._setup_base_column(table_class=table_class)

def __getitem__(self, value: int) -> Array:
"""
Allows queries which retrieve an item from the array. The index starts
Expand Down
4 changes: 2 additions & 2 deletions piccolo/engine/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def convert_array_in(value: list):
"""
Converts a list value into a string.
"""
if value and type(value[0]) not in [str, int, float]:
raise ValueError("Can only serialise str, int and float.")
if value and type(value[0]) not in [str, int, float, list]:
raise ValueError("Can only serialise str, int, float, and list.")

return dump_json(value)

Expand Down
2 changes: 1 addition & 1 deletion piccolo/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def __init_subclass__(
column._meta._table = cls

if isinstance(column, Array):
column.base_column._meta._table = cls
column._setup_base_column(table_class=cls)

if isinstance(column, Email):
email_columns.append(column)
Expand Down
60 changes: 48 additions & 12 deletions tests/columns/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from piccolo.columns.column_types import Array, Integer
from piccolo.columns.column_types import Array, BigInt, Integer
from piccolo.table import Table
from tests.base import engines_only, sqlite_only

Expand All @@ -22,7 +22,7 @@ def test_array_default(self):

class TestArray(TestCase):
"""
Make sure an Array column can be created.
Make sure an Array column can be created, and work correctly.
"""

def setUp(self):
Expand All @@ -35,9 +35,9 @@ def tearDown(self):
def test_storage(self):
"""
Make sure data can be stored and retrieved.
"""
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
MyTable(value=[1, 2, 3]).save().run_sync()

Expand All @@ -48,9 +48,9 @@ def test_storage(self):
def test_index(self):
"""
Indexes should allow individual array elements to be queried.
"""
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
MyTable(value=[1, 2, 3]).save().run_sync()

Expand All @@ -63,9 +63,9 @@ def test_all(self):
"""
Make sure rows can be retrieved where all items in an array match a
given value.
"""
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
MyTable(value=[1, 1, 1]).save().run_sync()

Expand All @@ -90,9 +90,9 @@ def test_any(self):
"""
Make sure rows can be retrieved where any items in an array match a
given value.
"""
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
MyTable(value=[1, 2, 3]).save().run_sync()

Expand All @@ -116,9 +116,9 @@ def test_any(self):
def test_cat(self):
"""
Make sure values can be appended to an array.
"""
"""
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
MyTable(value=[1, 1, 1]).save().run_sync()

Expand Down Expand Up @@ -163,3 +163,39 @@ def test_cat_sqlite(self):
str(manager.exception),
"Only Postgres and Cockroach support array appending.",
)


class NestedArrayTable(Table):
value = Array(base_column=Array(base_column=BigInt()))


class TestNestedArray(TestCase):
"""
Make sure that tables with nested arrays can be created, and work
correctly.
Related to this bug, with nested array columns containing ``BigInt``:
https://github.com/piccolo-orm/piccolo/issues/936
"""

def setUp(self):
NestedArrayTable.create_table().run_sync()

def tearDown(self):
NestedArrayTable.alter().drop_table().run_sync()

@engines_only("postgres", "sqlite")
def test_storage(self):
"""
Make sure data can be stored and retrieved.
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
""" # noqa: E501
NestedArrayTable(value=[[1, 2, 3], [4, 5, 6]]).save().run_sync()

row = NestedArrayTable.objects().first().run_sync()
assert row is not None
self.assertEqual(row.value, [[1, 2, 3], [4, 5, 6]])

0 comments on commit 894efe5

Please sign in to comment.