Skip to content

Commit 87f65eb

Browse files
committed
AGG functions
1 parent 94b447d commit 87f65eb

File tree

1 file changed

+63
-0
lines changed
  • lib/dl_formula_ref/dl_formula_ref/functions

1 file changed

+63
-0
lines changed

lib/dl_formula_ref/dl_formula_ref/functions/native.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"Parameters are passed in the same type as written in the formula."
2121
)
2222

23+
_COMMON_AGG_EXECUTION_NOTE = _(
24+
"The function is executed as an aggregation across grouped rows. "
25+
"Parameters are passed in the same type as written in the formula."
26+
)
27+
2328
_COMMON_NAME_CONSTRAINT = _(
2429
"The function name must contain only alphanumeric characters, underscore and colon characters."
2530
)
@@ -45,6 +50,18 @@ def _db_call_description(return_type: str) -> str:
4550
)
4651

4752

53+
def _db_call_agg_description(return_type: str) -> str:
54+
"""Generate description for DB_CALL_AGG_* functions."""
55+
return _(
56+
f"Calls a native database aggregate function by name. Native function should return {return_type}. "
57+
f"{_COMMON_AGG_EXECUTION_NOTE}"
58+
"\n\n"
59+
f"{_COMMON_ARG_DESCRIPTION}\n"
60+
"\n"
61+
f"{_COMMON_NAME_CONSTRAINT}"
62+
)
63+
64+
4865
FUNCTION_DB_CALL_INT = FunctionDocRegistryItem(
4966
name="db_call_int",
5067
category=CATEGORY_NATIVE,
@@ -160,6 +177,49 @@ def _db_call_description(return_type: str) -> str:
160177
],
161178
)
162179

180+
FUNCTION_DB_CALL_AGG_INT = FunctionDocRegistryItem(
181+
name="db_call_agg_int",
182+
category=CATEGORY_NATIVE,
183+
description=_db_call_agg_description("an integer result"),
184+
notes=[_COMMON_NOTE],
185+
examples=[
186+
SimpleExample(
187+
'DB_CALL_AGG_INT("uniqMerge", [uniqStateField]) '
188+
"-- ClickHouse: merge uniqState aggregations to get unique count"
189+
),
190+
],
191+
)
192+
193+
FUNCTION_DB_CALL_AGG_FLOAT = FunctionDocRegistryItem(
194+
name="db_call_agg_float",
195+
category=CATEGORY_NATIVE,
196+
description=_db_call_agg_description("a float result"),
197+
notes=[_COMMON_NOTE],
198+
examples=[
199+
SimpleExample(
200+
'DB_CALL_AGG_FLOAT("avgWeighted", [amount], [weight_field]) '
201+
"-- ClickHouse: calculate weighted average of amount by weight"
202+
),
203+
SimpleExample(
204+
'DB_CALL_AGG_FLOAT("corr", [x], [y]) '
205+
"-- ClickHouse: calculate correlation coefficient between x and y"
206+
),
207+
],
208+
)
209+
210+
FUNCTION_DB_CALL_AGG_STRING = FunctionDocRegistryItem(
211+
name="db_call_agg_string",
212+
category=CATEGORY_NATIVE,
213+
description=_db_call_agg_description("a string result"),
214+
notes=[_COMMON_NOTE],
215+
examples=[
216+
SimpleExample(
217+
'DB_CALL_AGG_STRING("anyHeavy", [str_field]) '
218+
"-- ClickHouse: select a frequently occurring value (more intelligent than random any)"
219+
),
220+
],
221+
)
222+
163223
FUNCTIONS_NATIVE = [
164224
FUNCTION_DB_CALL_INT,
165225
FUNCTION_DB_CALL_FLOAT,
@@ -168,4 +228,7 @@ def _db_call_description(return_type: str) -> str:
168228
FUNCTION_DB_CALL_ARRAY_INT,
169229
FUNCTION_DB_CALL_ARRAY_FLOAT,
170230
FUNCTION_DB_CALL_ARRAY_STRING,
231+
FUNCTION_DB_CALL_AGG_INT,
232+
FUNCTION_DB_CALL_AGG_FLOAT,
233+
FUNCTION_DB_CALL_AGG_STRING,
171234
]

0 commit comments

Comments
 (0)