Skip to content

Commit 0b4d96e

Browse files
authored
fix(ingest/powerbi): support comments in m-query grammar (#12177)
1 parent 8e9fc20 commit 0b4d96e

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

metadata-ingestion/src/datahub/ingestion/source/powerbi/powerbi-lexical-grammar.rule

+16-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
// | empty_string
2222
// | empty_string "," argument_list
2323
// - Added sql_string in any_literal
24+
// - Added WS_INLINE? in field expression
25+
// Added to ignore any comments
26+
// %ignore WS // Ignore whitespace
27+
// %ignore CPP_COMMENT // Ignore single-line comments
28+
// %ignore C_COMMENT // Ignore multi-line comments
2429

2530
lexical_unit: lexical_elements?
2631

@@ -245,6 +250,8 @@ operator_or_punctuator: ","
245250
| "=>"
246251
| ".."
247252
| "..."
253+
| "{{"
254+
| "}}"
248255

249256
document: section_document
250257
| expression_document
@@ -275,6 +282,7 @@ expression: logical_or_expression
275282
| if_expression
276283
| error_raising_expression
277284
| error_handling_expression
285+
| outer_expression
278286

279287

280288
logical_or_expression: logical_and_expression
@@ -376,6 +384,8 @@ sql_content: /(?:[^\"\\]|\\[\"]|\"\"|\#\(lf\))+/
376384

377385
sql_string: "\"" sql_content "\""
378386

387+
outer_expression: "{{" expression "}}"
388+
379389
argument_list: WS_INLINE? expression
380390
| WS_INLINE? expression WS_INLINE? "," WS_INLINE? argument_list
381391
| WS_INLINE? sql_string
@@ -409,7 +419,7 @@ record_expression: "[" field_list? "]"
409419
field_list: field
410420
| field "," field_list
411421

412-
field: field_name WS_INLINE? "=" WS_INLINE? expression
422+
field: WS_INLINE? field_name WS_INLINE? "=" WS_INLINE? expression
413423

414424
field_name: generalized_identifier
415425
| quoted_identifier
@@ -621,4 +631,8 @@ any_literal: record_literal
621631
%import common.DIGIT
622632
%import common.LF
623633
%import common.CR
624-
%import common.ESCAPED_STRING
634+
%import common.ESCAPED_STRING
635+
636+
%ignore WS // Ignore whitespace
637+
%ignore CPP_COMMENT // Ignore single-line comments
638+
%ignore C_COMMENT // Ignore multi-line comments

metadata-ingestion/tests/integration/powerbi/test_m_parser.py

+36
Original file line numberDiff line numberDiff line change
@@ -1171,3 +1171,39 @@ def test_m_query_timeout(mock_get_lark_parser):
11711171
assert (
11721172
is_entry_present
11731173
), 'Warning message "M-Query Parsing Timeout" should be present in reporter'
1174+
1175+
1176+
def test_comments_in_m_query():
1177+
q: str = 'let\n Source = Snowflake.Databases("xaa48144.snowflakecomputing.com", "COMPUTE_WH", [Role="ACCOUNTADMIN"]),\n SNOWFLAKE_SAMPLE_DATA_Database = Source{[Name="SNOWFLAKE_SAMPLE_DATA", Kind="Database"]}[Data],\n TPCDS_SF100TCL_Schema = SNOWFLAKE_SAMPLE_DATA_Database{[Name="TPCDS_SF100TCL", Kind="Schema"]}[Data],\n ITEM_Table = TPCDS_SF100TCL_Schema{[Name="ITEM", Kind="Table"]}[Data],\n \n // Group by I_BRAND and calculate the count\n BrandCountsTable = Table.Group(ITEM_Table, {"I_BRAND"}, {{"BrandCount", each Table.RowCount(_), Int64.Type}})\nin\n BrandCountsTable'
1178+
1179+
table: powerbi_data_classes.Table = powerbi_data_classes.Table(
1180+
columns=[],
1181+
measures=[],
1182+
expression=q,
1183+
name="pet_price_index",
1184+
full_name="datalake.sandbox_pet.pet_price_index",
1185+
)
1186+
1187+
reporter = PowerBiDashboardSourceReport()
1188+
1189+
ctx, config, platform_instance_resolver = get_default_instances()
1190+
1191+
data_platform_tables: List[DataPlatformTable] = parser.get_upstream_tables(
1192+
table,
1193+
reporter,
1194+
ctx=ctx,
1195+
config=config,
1196+
platform_instance_resolver=platform_instance_resolver,
1197+
parameters={
1198+
"hostname": "xyz.databricks.com",
1199+
"http_path": "/sql/1.0/warehouses/abc",
1200+
"catalog": "cat",
1201+
"schema": "public",
1202+
},
1203+
)[0].upstreams
1204+
1205+
assert len(data_platform_tables) == 1
1206+
assert (
1207+
data_platform_tables[0].urn
1208+
== "urn:li:dataset:(urn:li:dataPlatform:snowflake,snowflake_sample_data.tpcds_sf100tcl.item,PROD)"
1209+
)

0 commit comments

Comments
 (0)