Skip to content

Commit d310330

Browse files
jdrakens-circle-ci
andauthored
LEXIO-37880 Adds support for group by all (#2)
* all * version * Version bumped to 0.3.0 Co-authored-by: ns-circle-ci <[email protected]>
1 parent a8a3605 commit d310330

File tree

16 files changed

+59
-59
lines changed

16 files changed

+59
-59
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
default_stages: [commit]
33

44
default_language_version:
5-
python: python3
5+
python: python3.9
66

77
exclude: "examples/.*$"
88

@@ -74,7 +74,7 @@ repos:
7474
entry: mypy -p pysaql
7575
language: python
7676
types: [ file, python ]
77-
additional_dependencies: [ mypy==0.910 ]
77+
additional_dependencies: [ mypy==0.940 ]
7878
pass_filenames: false
7979

8080
- id: flake8

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 = "pysaql"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "Python SAQL query builder"
55
authors = ["Jonathan Drake <[email protected]>"]
66
license = "BSD-3-Clause"

pysaql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Python SAQL query builder"""
22

3-
__version__ = "0.2.0"
3+
__version__ = "0.3.0"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Contains aggregation function definitions"""
22

3-
from typing import Optional, Sequence, Tuple, Union
3+
from __future__ import annotations
44

5-
from typing_extensions import Self
5+
from typing import Optional, Sequence, Tuple, Union
66

77
from .enums import Order
88
from .function import Function, NullaryFunction
@@ -34,7 +34,7 @@ def over(
3434
row_range: Tuple[Optional[int], Optional[int]],
3535
reset_groups: Union[str, Sequence[Scalar]],
3636
order_by: Sequence[Union[Scalar, Tuple[Scalar, Order]]],
37-
) -> Self:
37+
) -> WindowFunction:
3838
"""Set the windowing function parameters
3939
4040
Args:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,6 @@ def __str__(self) -> str:
329329
end = ",".join(str(arg) for arg in self.end_date._args)
330330
return f"[dateRange([{start}], [{end}])]"
331331
else:
332-
start = self.start_date or ""
333-
end = self.end_date or ""
332+
start = str(self.start_date) if self.start_date else ""
333+
end = str(self.end_date) if self.end_date else ""
334334
return f"[{start}..{end}]"
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Contains core function definitions"""
22

3-
from typing import Any, List, Optional
3+
from typing import Any, Optional, Sequence
44

55
from .scalar import Scalar
66

77

88
class Function(Scalar):
99
"""Base function definition"""
1010

11-
_args: List[Any]
11+
_args: Sequence[Any]
1212
_name: Optional[str] = None
1313

1414
def __init__(self, *args: Any) -> None:
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Contains definition of a scalar"""
22

3+
from __future__ import annotations
4+
35
from abc import ABC, abstractmethod
46
import operator
57
from typing import Any, Callable, Optional, Sequence, Union
68

7-
from typing_extensions import Self
8-
99
from .expression import Expression
1010
from .util import escape_identifier, stringify
1111

@@ -43,7 +43,7 @@ class Operation:
4343
class BooleanOperation(Operation):
4444
"""Mixin that defines boolean comparison methods"""
4545

46-
def __and__(self, obj: Any) -> "BinaryOperation":
46+
def __and__(self, obj: Any) -> BinaryOperation:
4747
"""Creates a binary operation using the `and` operator
4848
4949
Args:
@@ -55,7 +55,7 @@ def __and__(self, obj: Any) -> "BinaryOperation":
5555
"""
5656
return BinaryOperation(operator.and_, self, obj)
5757

58-
def __or__(self, obj: Any) -> "BinaryOperation":
58+
def __or__(self, obj: Any) -> BinaryOperation:
5959
"""Creates a binary operation using the `or` operator
6060
6161
Args:
@@ -67,7 +67,7 @@ def __or__(self, obj: Any) -> "BinaryOperation":
6767
"""
6868
return BinaryOperation(operator.or_, self, obj, wrap=True)
6969

70-
def __invert__(self) -> "BinaryOperation":
70+
def __invert__(self) -> UnaryOperation:
7171
"""Creates a unary operation using the `inv` operator
7272
7373
Returns:
@@ -134,7 +134,7 @@ class Scalar(Expression, BooleanOperation, ABC):
134134

135135
_alias: Optional[str] = None
136136

137-
def alias(self, name: str) -> Self:
137+
def alias(self, name: str) -> Scalar:
138138
"""Set the alias name for a scalar expression
139139
140140
Args:
@@ -165,7 +165,7 @@ def __str__(self) -> str:
165165

166166
return s
167167

168-
def __eq__(self, obj: Any) -> BinaryOperation:
168+
def __eq__(self, obj: Any) -> BinaryOperation: # type: ignore[override]
169169
"""Creates a binary operation using the `eq` or `is` operator
170170
171171
Args:
@@ -178,7 +178,7 @@ def __eq__(self, obj: Any) -> BinaryOperation:
178178
op = operator.is_ if obj is None else operator.eq
179179
return BinaryOperation(op, self, obj)
180180

181-
def __ne__(self, obj: Any) -> BinaryOperation:
181+
def __ne__(self, obj: Any) -> BinaryOperation: # type: ignore[override]
182182
"""Creates a binary operation using the `ne` or `is_not` operator
183183
184184
Args:
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Contains stream expressions and statements"""
22

3+
from __future__ import annotations
4+
35
from abc import ABC
46
import functools
57
import operator
68
from typing import List, Optional, Sequence, Tuple, Union
79

8-
from typing_extensions import Self
9-
1010
from .enums import FillDateTypeString, JoinType, Order
1111
from .expression import Expression
1212
from .scalar import BinaryOperation, field, Scalar
@@ -83,7 +83,7 @@ def add_statement(self, statement: StreamStatement) -> None:
8383
"""
8484
self._statements.append(statement)
8585

86-
def foreach(self, *fields: Scalar) -> Self:
86+
def foreach(self, *fields: Scalar) -> Stream:
8787
"""Applies a set of expressions to every row in a dataset.
8888
8989
This action is often referred to as projection
@@ -98,14 +98,15 @@ def foreach(self, *fields: Scalar) -> Self:
9898
self._statements.append(ProjectionStatement(self, fields))
9999
return self
100100

101-
def group(self, *fields: Scalar) -> Self:
101+
def group(self, *fields: Scalar) -> Stream:
102102
"""Organizes the rows returned from a query into groups
103103
104104
Within each group, you can apply an aggregate function, such as count() or sum()
105105
to get the number of items or sum, respectively.
106106
107107
Args:
108-
fields: One or more fields to group by
108+
fields: One or more fields to group by. If no fields are provided,
109+
"group by all" is assumed.
109110
110111
Returns:
111112
self
@@ -114,7 +115,7 @@ def group(self, *fields: Scalar) -> Self:
114115
self._statements.append(GroupStatement(self, fields))
115116
return self
116117

117-
def filter(self, *filters: BinaryOperation) -> Self:
118+
def filter(self, *filters: BinaryOperation) -> Stream:
118119
"""Selects rows from a dataset based on a filter predicate
119120
120121
Args:
@@ -128,7 +129,7 @@ def filter(self, *filters: BinaryOperation) -> Self:
128129
self._statements.append(FilterStatement(self, filters))
129130
return self
130131

131-
def order(self, *fields: Union[Scalar, Tuple[Scalar, Order]]) -> Self:
132+
def order(self, *fields: Union[Scalar, Tuple[Scalar, Order]]) -> Stream:
132133
"""Sorts in ascending or descending order on one or more fields.
133134
134135
Args:
@@ -141,7 +142,7 @@ def order(self, *fields: Union[Scalar, Tuple[Scalar, Order]]) -> Self:
141142
self._statements.append(OrderStatement(self, fields))
142143
return self
143144

144-
def limit(self, limit: int) -> Self:
145+
def limit(self, limit: int) -> Stream:
145146
"""Limits the number of rows returned.
146147
147148
Args:
@@ -159,7 +160,7 @@ def fill(
159160
date_cols: Sequence[field],
160161
date_type_string: FillDateTypeString,
161162
partition: Optional[field] = None,
162-
) -> Self:
163+
) -> Stream:
163164
"""Fills missing date values by adding rows in data stream
164165
165166
Args:
@@ -202,7 +203,7 @@ def __str__(self) -> str:
202203
class ProjectionStatement(StreamStatement):
203204
"""Statement to project columns from a stream"""
204205

205-
def __init__(self, stream: Stream, fields: List[Scalar]) -> None:
206+
def __init__(self, stream: Stream, fields: Sequence[Scalar]) -> None:
206207
"""Initializer
207208
208209
Args:
@@ -228,7 +229,7 @@ class OrderStatement(StreamStatement):
228229
def __init__(
229230
self,
230231
stream: Stream,
231-
fields: Union[Scalar, List[Scalar], List[Tuple[Scalar, Order]]],
232+
fields: Sequence[Union[Scalar, Tuple[Scalar, Order]]],
232233
) -> None:
233234
"""Initializer
234235
@@ -284,30 +285,29 @@ def __str__(self) -> str:
284285
class GroupStatement(StreamStatement):
285286
"""Statement to group rows in a stream"""
286287

287-
def __init__(self, stream: Stream, fields: List[Scalar]):
288+
def __init__(self, stream: Stream, fields: Sequence[Scalar]):
288289
"""Initializer
289290
290291
Args:
291292
stream: Stream containing this statement
292-
fields: One or more fields to group by
293+
fields: One or more fields to group by. If no fields are provided,
294+
"group by all" is assumed.
293295
294296
"""
295297
super().__init__()
296298
self.stream = stream
297-
if not fields:
298-
raise ValueError("At least one field is required")
299299
self.fields = fields
300300

301301
def __str__(self) -> str:
302302
"""Cast this group statement to a string"""
303-
fields = ", ".join(str(f) for f in self.fields)
303+
fields = stringify_list(self.fields) if self.fields else "all"
304304
return f"{self.stream.ref} = group {self.stream.ref} by {fields};"
305305

306306

307307
class FilterStatement(StreamStatement):
308308
"""Statement to filter rows in a stream"""
309309

310-
def __init__(self, stream: Stream, filters: List[BinaryOperation]) -> None:
310+
def __init__(self, stream: Stream, filters: Sequence[BinaryOperation]) -> None:
311311
"""Initializer
312312
313313
Args:
@@ -336,7 +336,7 @@ class CogroupStatement(StreamStatement):
336336
def __init__(
337337
self,
338338
stream: Stream,
339-
streams: List[Tuple[Stream, Scalar]],
339+
streams: Sequence[Tuple[Stream, Scalar]],
340340
join_type: JoinType = JoinType.inner,
341341
) -> None:
342342
"""Initializer

0 commit comments

Comments
 (0)