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+
1517import 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
0 commit comments