Skip to content

Commit cd6ac21

Browse files
committed
minor tweaks to aggregation docs and tests
1 parent 9d69602 commit cd6ac21

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

docs/src/piccolo/query_types/select.rst

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Aggregate functions
8686
Count
8787
~~~~~
8888

89-
Returns the number of rows which match the query.
89+
Returns the number of rows which match the query:
9090

9191
.. code-block:: python
9292
@@ -96,7 +96,7 @@ Returns the number of rows which match the query.
9696
Avg
9797
~~~
9898

99-
Returns average of numeric rows which match the query.
99+
Returns the average for a given column:
100100

101101
.. code-block:: python
102102
@@ -108,7 +108,7 @@ Returns average of numeric rows which match the query.
108108
Sum
109109
~~~
110110

111-
Returns sum of numeric rows which match the query.
111+
Returns the sum for a given column:
112112

113113
.. code-block:: python
114114
@@ -120,7 +120,7 @@ Returns sum of numeric rows which match the query.
120120
Max
121121
~~~
122122

123-
Returns maximum of rows which match the query.
123+
Returns the maximum for a given column:
124124

125125
.. code-block:: python
126126
@@ -132,7 +132,7 @@ Returns maximum of rows which match the query.
132132
Min
133133
~~~
134134

135-
Returns minimum of rows which match the query.
135+
Returns the minimum for a given column:
136136

137137
.. code-block:: python
138138
@@ -144,7 +144,7 @@ Returns minimum of rows which match the query.
144144
Additional features
145145
~~~~~~~~~~~~~~~~~~~
146146

147-
You also can chain multiple different aggregate functions in one query,
147+
You also can chain multiple different aggregate functions in one query:
148148

149149
.. code-block:: python
150150
@@ -153,7 +153,7 @@ You also can chain multiple different aggregate functions in one query,
153153
>>> response
154154
{"avg": 750.0, "sum": 1500}
155155
156-
use aliases for aggregate functions like this
156+
And can use aliases for aggregate functions like this:
157157

158158
.. code-block:: python
159159
@@ -162,11 +162,7 @@ use aliases for aggregate functions like this
162162
>>> response["popularity_avg"]
163163
750.0
164164
165-
or use ``as_alias`` method for aggregate functions like this.
166-
167-
.. code-block:: python
168-
169-
>>> from piccolo.query import Avg
165+
# Alternatively, you can use the `as_alias` method.
170166
>>> response = Band.select(Avg(Band.popularity).as_alias("popularity_avg")).first().run_sync()
171167
>>> response["popularity_avg"]
172168
750.0

piccolo/query/methods/select.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
from piccolo.table import Table # noqa
2626

2727

28+
def is_numeric_column(column: Column) -> bool:
29+
return column.value_type in (int, decimal.Decimal, float)
30+
31+
2832
class Avg(Selectable):
2933
"""
3034
AVG() SQL function. Column type must be numeric to run the query.
@@ -35,20 +39,18 @@ class Avg(Selectable):
3539
"""
3640

3741
def __init__(self, column: Column, alias: str = "avg"):
38-
self.column = column
42+
if is_numeric_column(column):
43+
self.column = column
44+
else:
45+
raise ValueError("Column type must be numeric to run the query.")
3946
self.alias = alias
4047

4148
def as_alias(self, alias: str) -> Avg:
4249
self.alias = alias
4350
return self
4451

4552
def get_select_string(self, engine_type: str, just_alias=False) -> str:
46-
if self.column.value_type in (int, decimal.Decimal, float):
47-
column_name = self.column._meta.get_full_name(
48-
just_alias=just_alias
49-
)
50-
else:
51-
raise ValueError("Column type must be numeric to run the query.")
53+
column_name = self.column._meta.get_full_name(just_alias=just_alias)
5254
return f"AVG({column_name}) AS {self.alias}"
5355

5456

@@ -128,20 +130,18 @@ class Sum(Selectable):
128130
"""
129131

130132
def __init__(self, column: Column, alias: str = "sum"):
131-
self.column = column
133+
if is_numeric_column(column):
134+
self.column = column
135+
else:
136+
raise ValueError("Column type must be numeric to run the query.")
132137
self.alias = alias
133138

134139
def as_alias(self, alias: str) -> Sum:
135140
self.alias = alias
136141
return self
137142

138143
def get_select_string(self, engine_type: str, just_alias=False) -> str:
139-
if self.column.value_type in (int, decimal.Decimal, float):
140-
column_name = self.column._meta.get_full_name(
141-
just_alias=just_alias
142-
)
143-
else:
144-
raise ValueError("Column type must be numeric to run the query.")
144+
column_name = self.column._meta.get_full_name(just_alias=just_alias)
145145
return f"SUM({column_name}) AS {self.alias}"
146146

147147

tests/table/test_select.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,11 +735,11 @@ def test_chain_different_functions_alias(self):
735735
self.assertTrue(response["popularity_sum"] == 3010)
736736

737737
def test_avg_validation(self):
738-
with self.assertRaises(Exception):
738+
with self.assertRaises(ValueError):
739739
Band.select(Avg(Band.name)).run_sync()
740740

741741
def test_sum_validation(self):
742-
with self.assertRaises(Exception):
742+
with self.assertRaises(ValueError):
743743
Band.select(Sum(Band.name)).run_sync()
744744

745745
def test_columns(self):

0 commit comments

Comments
 (0)