-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
634 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Union, Optional, List | ||
|
||
try: | ||
from typing import Literal | ||
except ImportError: | ||
from typing_extensions import Literal | ||
|
||
from ..structures.distinct import Distinct | ||
from ..functions.base import BaseFunction | ||
from ..properties import Array | ||
from ..properties.property import Property | ||
from ..structures.cast_operator import CastOperator | ||
from ..structures.case import Case | ||
from ..structures.order import Order | ||
|
||
|
||
class ArrayAgg(BaseFunction): | ||
obj: Literal['functions.StringAgg'] = 'functions.ArrayAgg' | ||
property: Union[Property, BaseFunction, Array, CastOperator, Distinct, Case] | ||
order: Optional[List[Order]] | ||
position: Optional[int] | ||
alias: str = None | ||
|
||
def to_sql(self) -> str: | ||
sql = f"{self.property}" | ||
|
||
if self.order: | ||
sql = f"{sql} ORDER BY {', '.join(map(str, self.order))}" | ||
|
||
sql = f"ARRAY_AGG({sql})" | ||
|
||
if self.position: | ||
sql = f"({sql})[{self.position}]" | ||
|
||
if self.alias is not None: | ||
sql = f"{sql} AS {self.alias}" | ||
|
||
return sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Union, List | ||
|
||
try: | ||
from typing import Literal | ||
except ImportError: | ||
from typing_extensions import Literal | ||
|
||
from .base import BaseFunction | ||
from ..properties.constant import Constant | ||
from ..structures.order import Order | ||
from ..properties.property import Property | ||
from ..structures.cast_operator import CastOperator | ||
from ..structures.case import Case | ||
|
||
|
||
class FirstValue(BaseFunction): | ||
obj: Literal['functions.FirstValue'] = 'functions.FirstValue' | ||
property: Union[Property, BaseFunction, Constant, CastOperator, Case] | ||
order: List[Order] | ||
partition: List[Union[Property, BaseFunction, Constant, CastOperator, Case]] = [] | ||
alias: str = None | ||
|
||
def to_sql(self) -> str: | ||
sql = "" | ||
|
||
if self.partition: | ||
sql = f"PARTITION BY {', '.join(map(str, self.partition))}" | ||
|
||
sql = f"{sql} ORDER BY {', '.join(map(str, self.order))}" | ||
|
||
sql = f"FIRST_VALUE({self.property}) OVER ({sql})" | ||
|
||
if self.alias is not None: | ||
sql = f"{sql} AS {self.alias}" | ||
|
||
return sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from duckql import Property, Order, ArrayAgg | ||
|
||
|
||
def test_simple(): | ||
stm = ArrayAgg( | ||
property=Property(name='warehouse_logs.state'), | ||
) | ||
|
||
assert str(stm) == 'ARRAY_AGG(warehouse_logs.state)' | ||
|
||
|
||
def test_order_by(): | ||
stm = ArrayAgg( | ||
property=Property(name='warehouse_logs.state'), | ||
order=[ | ||
Order( | ||
property=Property(name='warehouse_logs.created_at') | ||
) | ||
] | ||
) | ||
|
||
assert str(stm) == 'ARRAY_AGG(warehouse_logs.state ORDER BY warehouse_logs.created_at ASC)' | ||
|
||
|
||
def test_position(): | ||
stm = ArrayAgg( | ||
property=Property(name='warehouse_logs.state'), | ||
position=1, | ||
order=[ | ||
Order( | ||
property=Property(name='warehouse_logs.created_at') | ||
) | ||
] | ||
) | ||
|
||
assert str(stm) == '(ARRAY_AGG(warehouse_logs.state ORDER BY warehouse_logs.created_at ASC))[1]' | ||
|
||
|
||
def test_alias(): | ||
stm = ArrayAgg( | ||
property=Property(name='warehouse_logs.state'), | ||
position=1, | ||
order=[ | ||
Order( | ||
property=Property(name='warehouse_logs.created_at') | ||
) | ||
], | ||
alias="first_volume" | ||
) | ||
|
||
assert str(stm) == '(ARRAY_AGG(warehouse_logs.state ORDER BY warehouse_logs.created_at ASC))[1] AS first_volume' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from duckql import Property, FirstValue, Order | ||
|
||
|
||
def test_property(): | ||
stm = FirstValue( | ||
property=Property(name='users.name'), | ||
partition=[ | ||
Property(name='users.age'), | ||
Property(name='users.city') | ||
], | ||
order=[ | ||
Order( | ||
property=Property(name='users.created_at') | ||
) | ||
] | ||
) | ||
|
||
assert str(stm) == 'FIRST_VALUE(users.name) OVER (PARTITION BY users.age, users.city ORDER BY users.created_at ' \ | ||
'ASC)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.11.1' | ||
__version__ = '0.12.0' |
Oops, something went wrong.