Skip to content

Commit 17029dc

Browse files
committed
Distinct count implementation for pSQL πŸ™€
1 parent 1871a18 commit 17029dc

File tree

8 files changed

+95
-64
lines changed

8 files changed

+95
-64
lines changed

β€ŽCHANGELOG.mdβ€Ž

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

3+
## 0.7.0 : 2020-08-27
4+
5+
- **Feature**: Now is possible to pass `Distinct` to `Count` to have `COUNT(DISTINCT property)` syntax in pSQL
6+
- **Tests**: Test for
7+
38
## 0.6.1 : 2020-08-04
49

510
- **Fix**: Use native `typing.Literal` if possible (Python 3.8 compatibility fix)

β€Ždocs/functions/count.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| Attribute | Accepts | Required |
1414
|-----------------|----------------------------------------------------------|----------|
1515
| obj | `functions.Count` | True |
16-
| property | `properties.Property` `CastOperator` | True |
16+
| property | `properties.Property` `CastOperator` `Distinct` | True |
1717
| alias | String | False |
1818

1919
## JSON format

β€Žduckql/functions/count.pyβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from ..properties import Constant
1010
from ..properties.property import Property
1111
from ..structures.cast_operator import CastOperator
12+
from ..structures.distinct import Distinct
1213

1314

1415
class Count(BaseFunction):
1516
obj: Literal['functions.Count'] = 'functions.Count'
16-
property: Union[Property, BaseFunction, Constant, CastOperator]
17+
property: Union[Property, BaseFunction, Constant, CastOperator, Distinct]
1718
alias: str = None
1819

1920
def to_sql(self) -> str:

β€Žduckql/functions/tests/test_count.pyβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from duckql.exceptions import ParseError
44
from duckql.functions.count import Count
55
from duckql.properties.property import Property
6+
from duckql.structures.distinct import Distinct
67

78

89
def test_simple():
@@ -19,3 +20,13 @@ def test_parse_error():
1920
Count(
2021
property=Property(name='users.name', alias='name')
2122
)
23+
24+
25+
def test_distinct():
26+
my_count = Count(
27+
property=Distinct(
28+
property=Property(name='users.id')
29+
),
30+
alias='distinct_count'
31+
)
32+
assert str(my_count) == "COUNT(DISTINCT users.id) AS distinct_count"

β€Žduckql/properties/tests/test_property.pyβ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ def test_obj(valid_instance: Property):
1919
assert valid_instance.obj == 'properties.Property'
2020

2121

22+
def test_json_field():
23+
json_property = Property(
24+
name='users.additional_data -> title'
25+
)
26+
assert str(json_property) == 'users.additional_data -> title'
27+
28+
29+
def test_json_field_parser():
30+
json_property = Property.parse_raw(
31+
'{"obj": "properties.Property", "name": "users.additional_data -> title", "alias": "title"}'
32+
)
33+
assert json_property.name == 'users.additional_data -> title'
34+
assert str(json_property) == 'users.additional_data -> title AS "title"'
35+
36+
2237
def test_json_parse(valid_instance: Property):
2338
assert valid_instance.json() == '{"obj": "properties.Property", "name": "users.name", "alias": "name"}'
2439

β€Žduckql/version.pyβ€Ž

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

β€Žpoetry.lockβ€Ž

Lines changed: 59 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žpyproject.tomlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "duckql-python"
3-
version = "0.6.1"
3+
version = "0.7.0"
44
description = "JSON declarative SQL conversion library"
55
authors = ["Jakub Dubec <jakub.dubec@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
Β (0)