Skip to content

Commit 78daf54

Browse files
committed
refactor
1 parent 0b954c1 commit 78daf54

File tree

1 file changed

+62
-18
lines changed
  • lib/dl_formula_ref/dl_formula_ref/functions

1 file changed

+62
-18
lines changed

lib/dl_formula_ref/dl_formula_ref/functions/native.py

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
_ = get_gettext()
1010

1111

12-
# Common text templates
1312
_COMMON_ARG_DESCRIPTION = _(
1413
"The first argument {arg:0} must be a constant string with the name of the "
1514
"database function to call. All subsequent arguments are passed to the "
16-
"native function and can be of any type."
15+
"native function and can be of any type, including types that are not currently supported by DataLens."
16+
)
17+
18+
_COMMON_EXECUTION_NOTE = _(
19+
"The function is executed for every row in the dataset (non-aggregated). "
20+
"Parameters are passed in the same type as written in the formula."
1721
)
1822

1923
_COMMON_NAME_CONSTRAINT = _(
20-
"The function name must contain only alphanumeric characters, underscore "
21-
"and colon characters."
24+
"The function name must contain only alphanumeric characters, underscore and colon characters."
2225
)
2326

2427
_COMMON_NOTE = Note(
@@ -33,8 +36,9 @@
3336
def _db_call_description(return_type: str) -> str:
3437
"""Generate description for DB_CALL_* functions."""
3538
return _(
36-
f"Calls a native database function by name. Native function should return {return_type}."
37-
"\n"
39+
f"Calls a native database function by name. Native function should return {return_type}. "
40+
f"{_COMMON_EXECUTION_NOTE}"
41+
"\n\n"
3842
f"{_COMMON_ARG_DESCRIPTION}\n"
3943
"\n"
4044
f"{_COMMON_NAME_CONSTRAINT}"
@@ -47,9 +51,13 @@ def _db_call_description(return_type: str) -> str:
4751
description=_db_call_description("an integer result"),
4852
notes=[_COMMON_NOTE],
4953
examples=[
50-
SimpleExample('DB_CALL_INT("sign", -5) = -1'),
51-
SimpleExample('DB_CALL_INT("sign", 5) = 1'),
52-
SimpleExample('DB_CALL_INT("positionCaseInsensitive", "Hello", "l") = 3'),
54+
SimpleExample(
55+
'DB_CALL_INT("positionCaseInsensitive", "Hello", "l") = 3 '
56+
'-- ClickHouse: find first occurrence of "l" in "Hello" ignoring case'
57+
),
58+
SimpleExample(
59+
'DB_CALL_INT("JSON_VALUE", [json_field], "$.age") -- PostgreSQL: extract age attribute from JSON'
60+
),
5361
],
5462
)
5563

@@ -59,9 +67,9 @@ def _db_call_description(return_type: str) -> str:
5967
description=_db_call_description("a float result"),
6068
notes=[_COMMON_NOTE],
6169
examples=[
62-
SimpleExample('DB_CALL_FLOAT("sign", -5.0) = -1.0'),
63-
SimpleExample('DB_CALL_FLOAT("sign", 5.0) = 1.0'),
64-
SimpleExample('DB_CALL_FLOAT("log10", 100.0) = 2.0'),
70+
SimpleExample('DB_CALL_FLOAT("sign", -5.0) = -1.0 -- ClickHouse: sign of -5.0 is -1.0'),
71+
SimpleExample('DB_CALL_FLOAT("sign", 5.0) = 1.0 -- ClickHouse: sign of 5.0 is 1.0'),
72+
SimpleExample('DB_CALL_FLOAT("log10", 100.0) = 2.0 -- ClickHouse: log10 of 100.0 is 2.0'),
6573
],
6674
)
6775

@@ -71,7 +79,18 @@ def _db_call_description(return_type: str) -> str:
7179
description=_db_call_description("a string result"),
7280
notes=[_COMMON_NOTE],
7381
examples=[
74-
SimpleExample('DB_CALL_STRING("reverse", "hello") = "olleh"'),
82+
SimpleExample(
83+
'DB_CALL_STRING("dictGetStringOrDefault", "categories", "category_name", [category_id], "other") '
84+
"-- ClickHouse: read from dictionary with default value"
85+
),
86+
SimpleExample(
87+
'DB_CALL_STRING("JSONExtractString", [json_field], "category", "last_order_date") '
88+
"-- ClickHouse: extract nested JSON attribute"
89+
),
90+
SimpleExample(
91+
'DB_CALL_STRING("hex", DB_CALL_STRING("SHA1", [salt_param] + [some_id])) '
92+
"-- ClickHouse: calculate hash with salt"
93+
),
7594
],
7695
)
7796

@@ -81,8 +100,12 @@ def _db_call_description(return_type: str) -> str:
81100
description=_db_call_description("a boolean result"),
82101
notes=[_COMMON_NOTE],
83102
examples=[
84-
SimpleExample('DB_CALL_BOOL("isFinite", 5) = TRUE'),
85-
SimpleExample('DB_CALL_BOOL("isInfinite", 5) = FALSE'),
103+
SimpleExample('DB_CALL_BOOL("isFinite", 5) = TRUE -- ClickHouse: check if 5 is a finite number'),
104+
SimpleExample('DB_CALL_BOOL("isInfinite", 5) = FALSE -- ClickHouse: check if 5 is an infinite number'),
105+
SimpleExample(
106+
'DB_CALL_BOOL("bitTest", [int_field], 4) '
107+
"-- ClickHouse: check if the 4th bit equals 1, useful when multiple values are encoded in one field"
108+
),
86109
],
87110
)
88111

@@ -92,7 +115,14 @@ def _db_call_description(return_type: str) -> str:
92115
description=_db_call_description("an array of integers"),
93116
notes=[_COMMON_NOTE],
94117
examples=[
95-
SimpleExample('DB_CALL_ARRAY_INT("range", 5) = ARRAY(0, 1, 2, 3, 4)'),
118+
SimpleExample(
119+
'DB_CALL_ARRAY_INT("range", 5) = ARRAY(0, 1, 2, 3, 4) '
120+
"-- ClickHouse: generate array of integers from 0 to 4"
121+
),
122+
SimpleExample(
123+
'DB_CALL_ARRAY_INT("arrayCompact", [int_arr_field]) '
124+
"-- ClickHouse: remove duplicate consecutive values from array of integers"
125+
),
96126
],
97127
)
98128

@@ -102,7 +132,14 @@ def _db_call_description(return_type: str) -> str:
102132
description=_db_call_description("an array of floats"),
103133
notes=[_COMMON_NOTE],
104134
examples=[
105-
SimpleExample('DB_CALL_ARRAY_FLOAT("arrayConcat", ARRAY(1.0, 2.0), ARRAY(3.0)) = ARRAY(1.0, 2.0, 3.0)'),
135+
SimpleExample(
136+
'DB_CALL_ARRAY_FLOAT("arrayConcat", ARRAY(1.0, 2.0), ARRAY(3.0)) = ARRAY(1.0, 2.0, 3.0) '
137+
"-- ClickHouse: concatenate arrays of floats"
138+
),
139+
SimpleExample(
140+
'DB_CALL_ARRAY_FLOAT("arrayCompact", [float_arr_field]) '
141+
"-- ClickHouse: remove duplicate consecutive values from array of floats"
142+
),
106143
],
107144
)
108145

@@ -112,7 +149,14 @@ def _db_call_description(return_type: str) -> str:
112149
description=_db_call_description("an array of strings"),
113150
notes=[_COMMON_NOTE],
114151
examples=[
115-
SimpleExample('DB_CALL_ARRAY_STRING("splitByChar", ",", "a,b,c") = ARRAY("a", "b", "c")'),
152+
SimpleExample(
153+
'DB_CALL_ARRAY_STRING("splitByChar", ",", "a,b,c") = ARRAY("a", "b", "c") '
154+
"-- ClickHouse: split string by comma"
155+
),
156+
SimpleExample(
157+
'DB_CALL_ARRAY_STRING("arrayCompact", [string_arr_field]) '
158+
"-- ClickHouse: remove duplicate consecutive values from array of strings"
159+
),
116160
],
117161
)
118162

0 commit comments

Comments
 (0)