Skip to content

Commit 6ba3145

Browse files
feat(executors): add support for gquery expansion for macros
1 parent dd208a7 commit 6ba3145

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

libs/executors/garf/executors/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ def setup_executor(
5757
'ApiExecutionContext',
5858
]
5959

60-
__version__ = '1.0.5'
60+
__version__ = '1.0.6'

libs/executors/garf/executors/query_processor.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""qQuery can be used as a parameter in garf queries."""
16+
1517
import contextlib
1618

17-
from garf.core import query_editor
18-
from garf.executors import exceptions, execution_context
19+
from garf.core import query_editor, query_parser
20+
from garf.executors import execution_context
1921

2022

21-
def process_gquery(
22-
context: execution_context.ExecutionContext,
23-
) -> execution_context.ExecutionContext:
24-
for k, v in context.fetcher_parameters.items():
23+
class GqueryError(query_parser.GarfQueryError):
24+
"""Errors on incorrect qQuery syntax."""
25+
26+
27+
def _handle_sub_context(context, sub_context):
28+
for k, v in sub_context.items():
2529
if isinstance(v, str) and v.startswith('gquery'):
2630
no_writer_context = context.model_copy(update={'writer': None})
2731
try:
28-
_, alias, query = v.split(':', maxsplit=3)
32+
_, alias, *query = v.split(':', maxsplit=3)
2933
except ValueError:
30-
raise exceptions.GarfExecutorError(
34+
raise GqueryError(
3135
f'Incorrect gquery format, should be gquery:alias:query, got {v}'
3236
)
37+
if not alias:
38+
raise GqueryError(f'Missing alias in gquery: {v}')
39+
if not query:
40+
raise GqueryError(f'Missing query text in gquery: {v}')
3341
if alias == 'sqldb':
3442
from garf.executors import sql_executor
3543

@@ -43,19 +51,27 @@ def process_gquery(
4351
**context.fetcher_parameters
4452
)
4553
else:
46-
raise exceptions.GarfExecutorError(
47-
f'Unsupported alias for gquery: {alias}'
48-
)
49-
with contextlib.suppress(query_editor.GarfResourceError):
54+
raise GqueryError(f'Unsupported alias {alias} for gquery: {v}')
55+
with contextlib.suppress(
56+
query_editor.GarfResourceError, query_parser.GarfVirtualColumnError
57+
):
58+
query = ':'.join(query)
5059
query_spec = query_editor.QuerySpecification(
5160
text=query, args=context.query_parameters
5261
).generate()
5362
if len(columns := [c for c in query_spec.column_names if c != '_']) > 1:
54-
raise exceptions.GarfExecutorError(
55-
f'Multiple columns in gquery: {columns}'
56-
)
63+
raise GqueryError(f'Multiple columns in gquery definition: {columns}')
5764
res = gquery_executor.execute(
5865
query=query, title='gquery', context=no_writer_context
5966
)
60-
context.fetcher_parameters[k] = res.to_list(row_type='scalar')
67+
if len(columns := [c for c in res.column_names if c != '_']) > 1:
68+
raise GqueryError(f'Multiple columns in gquery result: {columns}')
69+
sub_context[k] = res.to_list(row_type='scalar')
70+
71+
72+
def process_gquery(
73+
context: execution_context.ExecutionContext,
74+
) -> execution_context.ExecutionContext:
75+
_handle_sub_context(context, context.fetcher_parameters)
76+
_handle_sub_context(context, context.query_parameters.macro)
6177
return context

libs/executors/tests/unit/test_query_processor.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@ def test_process_gquery_returns_unchanged_context():
2828
('gquery', 'error_message'),
2929
[
3030
(
31-
'gquery:SELECT 1',
32-
'Incorrect gquery format, should be gquery:alias:query, '
33-
'got gquery:SELECT 1',
31+
'gquery',
32+
'Incorrect gquery format, should be gquery:alias:query, got gquery',
33+
),
34+
(
35+
'gquery::SELECT 1',
36+
'Missing alias in gquery: gquery::SELECT 1',
37+
),
38+
(
39+
'gquery:sqldb',
40+
'Missing query text in gquery: gquery:sqldb',
3441
),
3542
(
3643
'gquery:unknown_alias:SELECT 1',
37-
'Unsupported alias for gquery: unknown_alias',
44+
'Unsupported alias unknown_alias for gquery: gquery:unknown_alias:SELECT 1',
3845
),
3946
(
4047
'gquery:sqldb:SELECT 1 AS id, 2 AS second_id',
41-
"Multiple columns in gquery: ['id', 'second_id']",
48+
"Multiple columns in gquery definition: ['id', 'second_id']",
4249
),
4350
],
4451
)
@@ -47,7 +54,7 @@ def test_process_gquery_raises_error(gquery, error_message):
4754
fetcher_parameters={'id': gquery}
4855
)
4956
with pytest.raises(
50-
exceptions.GarfExecutorError,
57+
query_processor.GqueryError,
5158
match=re.escape(error_message),
5259
):
5360
query_processor.process_gquery(context)

0 commit comments

Comments
 (0)