Skip to content

Commit 446267a

Browse files
committed
Fixed JSON casts πŸ˜„
1 parent 7780f14 commit 446267a

File tree

7 files changed

+258
-262
lines changed

7 files changed

+258
-262
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.12.1 : 2022-05-10
4+
5+
- **Fixed**: Corrected cast of JSON fields
6+
37
## 0.12.0 : 2021-12-21
48

59
- **Feature**: `FirstValue` window function introduced

β€Žduckql/properties/property.pyβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Config:
1717
'description': "Object representation of SQL column/property"
1818
}
1919

20+
def is_json_field(self) -> bool:
21+
return '->>' in self.name or '->' in self.name
22+
2023
def to_sql(self) -> str:
2124
if '->>' in self.name or '->' in self.name:
2225
sql = ''

β€Žduckql/structures/cast_operator.pyβ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class DataType(Enum):
4040
alias: str = None
4141

4242
def to_sql(self) -> str:
43-
sql = f"{self.property}::{self.to.value}"
43+
if isinstance(self.property, Property) and self.property.is_json_field():
44+
sql = f"({self.property})::{self.to.value}"
45+
else:
46+
sql = f"{self.property}::{self.to.value}"
4447

4548
if self.alias is not None:
4649
sql = f"{sql} AS {self.alias}"

β€Žduckql/structures/tests/test_cast_operator.pyβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ def test_simple():
1010
)
1111

1212
assert str(my_structure) == 'users.age::varchar AS age_as_string'
13+
14+
15+
def test_json_field():
16+
my_structure = CastOperator(
17+
property=Property(name='organisations.statistics->campaigns_count'),
18+
to=CastOperator.DataType.INT,
19+
alias='campaigns_count'
20+
)
21+
22+
assert str(my_structure) == '(organisations.statistics ->> \'campaigns_count\')::int AS campaigns_count'

β€Žduckql/version.pyβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.12.0'
1+
__version__ = '0.12.1'

0 commit comments

Comments
Β (0)