Skip to content

Commit a178433

Browse files
[core] chore: dipslay virtual column value when raising GarfVirtualColumnError
1 parent fb2e058 commit a178433

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

libs/core/garf_core/query_parser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ class ExtractedLineElements(pydantic.BaseModel):
238238

239239
field: str | None
240240
alias: str | None
241-
virtual_column: VirtualColumn | None
242-
customizer: Customizer | None
241+
virtual_column: VirtualColumn | None = None
242+
customizer: Customizer | None = None
243243

244244
@classmethod
245245
def from_query_line(
@@ -260,7 +260,9 @@ def from_query_line(
260260
if not (customizer := processed_field.customizer):
261261
customizer = None
262262
if virtual_column and not alias:
263-
raise GarfVirtualColumnError('Virtual attributes should be aliased')
263+
raise GarfVirtualColumnError(
264+
f'Virtual attributes should be aliased: {virtual_column.value}'
265+
)
264266
return ExtractedLineElements(
265267
field=_format_type_field_name(field)
266268
if not virtual_column and field
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from garf_core.query_parser import (
17+
Customizer,
18+
ExtractedLineElements,
19+
GarfVirtualColumnError,
20+
SliceField,
21+
VirtualColumn,
22+
)
23+
24+
25+
class TestExtractedLineElemens:
26+
@pytest.mark.parametrize(
27+
('line', 'expected'),
28+
[
29+
(
30+
'field AS column',
31+
ExtractedLineElements(field='field', alias='column'),
32+
),
33+
(
34+
'field',
35+
ExtractedLineElements(field='field', alias='field'),
36+
),
37+
(
38+
'1 + 1 AS column',
39+
ExtractedLineElements(
40+
field=None,
41+
alias='column',
42+
virtual_column=VirtualColumn(
43+
type='expression',
44+
value='1 + 1',
45+
fields=[],
46+
substitute_expression='1 + 1',
47+
),
48+
),
49+
),
50+
(
51+
'field + 1 AS column',
52+
ExtractedLineElements(
53+
field=None,
54+
alias='column',
55+
virtual_column=VirtualColumn(
56+
type='expression',
57+
value='field + 1',
58+
fields=['field'],
59+
substitute_expression='{field} + 1',
60+
),
61+
),
62+
),
63+
(
64+
'field[0].value AS column',
65+
ExtractedLineElements(
66+
field='field',
67+
alias='column',
68+
customizer=Customizer(
69+
type='slice',
70+
value=SliceField(slice_literal=slice(0, 1, None), value='value'),
71+
),
72+
),
73+
),
74+
],
75+
)
76+
def test_from_query_line_returns_correct_elements(self, line, expected):
77+
elements = ExtractedLineElements.from_query_line(line)
78+
assert elements == expected
79+
80+
def test_from_query_line_raises_error_on_unaliased_virtual_column(self):
81+
with pytest.raises(
82+
GarfVirtualColumnError,
83+
match='Virtual attributes should be aliased: 1',
84+
):
85+
ExtractedLineElements.from_query_line('1')

0 commit comments

Comments
 (0)