Skip to content

Commit 21ddb55

Browse files
authored
fix(ingest/mode): move sql logic to view properties aspect (#12196)
1 parent b6ea974 commit 21ddb55

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

metadata-ingestion/src/datahub/ingestion/source/mode.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
TagPropertiesClass,
9999
UpstreamClass,
100100
UpstreamLineageClass,
101+
ViewPropertiesClass,
101102
)
102103
from datahub.metadata.urns import QueryUrn
103104
from datahub.sql_parsing.sqlglot_lineage import (
@@ -930,16 +931,13 @@ def construct_query_or_dataset(
930931

931932
dataset_props = DatasetPropertiesClass(
932933
name=report_info.get("name") if is_mode_dataset else query_data.get("name"),
933-
description=f"""### Source Code
934-
``` sql
935-
{query_data.get("raw_query")}
936-
```
937-
""",
934+
description=None,
938935
externalUrl=externalUrl,
939936
customProperties=self.get_custom_props_from_dict(
940937
query_data,
941938
[
942-
"id" "created_at",
939+
"id",
940+
"created_at",
943941
"updated_at",
944942
"last_run_id",
945943
"data_source_id",
@@ -949,14 +947,23 @@ def construct_query_or_dataset(
949947
],
950948
),
951949
)
952-
953950
yield (
954951
MetadataChangeProposalWrapper(
955952
entityUrn=query_urn,
956953
aspect=dataset_props,
957954
).as_workunit()
958955
)
959956

957+
if raw_query := query_data.get("raw_query"):
958+
yield MetadataChangeProposalWrapper(
959+
entityUrn=query_urn,
960+
aspect=ViewPropertiesClass(
961+
viewLogic=raw_query,
962+
viewLanguage=QueryLanguageClass.SQL,
963+
materialized=False,
964+
),
965+
).as_workunit()
966+
960967
if is_mode_dataset:
961968
space_container_key = self.gen_space_key(space_token)
962969
yield from add_dataset_to_container(

metadata-ingestion/tests/integration/mode/mode_mces_golden.json

+41-2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
"datasets": [
177177
"urn:li:dataset:(urn:li:dataPlatform:mode,5450544,PROD)"
178178
],
179+
"dashboards": [],
179180
"lastModified": {
180181
"created": {
181182
"time": 1639169724316,
@@ -253,14 +254,15 @@
253254
"aspect": {
254255
"json": {
255256
"customProperties": {
257+
"id": "19780522",
258+
"created_at": "2024-09-02T07:38:43.755Z",
256259
"updated_at": "2024-09-02T07:40:44.046Z",
257260
"last_run_id": "3535709679",
258261
"data_source_id": "44763",
259262
"report_imports_count": "2"
260263
},
261264
"externalUrl": "https://app.mode.com/acryl/datasets/24f66e1701b6",
262265
"name": "Dataset 1",
263-
"description": "### Source Code\n``` sql\n-- Returns first 100 rows from DATAHUB_COMMUNITY.POSTGRES_PUBLIC.COMPANY\n SELECT \n\t\tAGE,\n\t\tID,\n\t\tNAME,\n\t\t_FIVETRAN_DELETED,\n\t\t_FIVETRAN_SYNCED\n FROM DATAHUB_COMMUNITY.POSTGRES_PUBLIC.COMPANY LIMIT 100;\n\n-- Returns first 100 rows from ETHAN_TEST_DB.PUBLIC.ACCOUNT_PHONE_NUMBER\n SELECT \n\t\tCOMMUNICATION_ACCOUNT_ID,\n\t\tID,\n\t\tMMS_CAPABLE,\n\t\tPHONE_NUMBER,\n\t\tSMS_CAPABLE,\n\t\tSTATUS,\n\t\tSTATUS_TLM,\n\t\tTLM,\n\t\tVOICE_CAPABLE,\n\t\tWHEN_CREATED\n FROM ETHAN_TEST_DB.PUBLIC.ACCOUNT_PHONE_NUMBER LIMIT 100;\n \n \n```\n ",
264266
"tags": []
265267
}
266268
},
@@ -270,6 +272,24 @@
270272
"lastRunId": "no-run-id-provided"
271273
}
272274
},
275+
{
276+
"entityType": "dataset",
277+
"entityUrn": "urn:li:dataset:(urn:li:dataPlatform:mode,5450544,PROD)",
278+
"changeType": "UPSERT",
279+
"aspectName": "viewProperties",
280+
"aspect": {
281+
"json": {
282+
"materialized": false,
283+
"viewLogic": "-- Returns first 100 rows from DATAHUB_COMMUNITY.POSTGRES_PUBLIC.COMPANY\n SELECT \n\t\tAGE,\n\t\tID,\n\t\tNAME,\n\t\t_FIVETRAN_DELETED,\n\t\t_FIVETRAN_SYNCED\n FROM DATAHUB_COMMUNITY.POSTGRES_PUBLIC.COMPANY LIMIT 100;\n\n-- Returns first 100 rows from ETHAN_TEST_DB.PUBLIC.ACCOUNT_PHONE_NUMBER\n SELECT \n\t\tCOMMUNICATION_ACCOUNT_ID,\n\t\tID,\n\t\tMMS_CAPABLE,\n\t\tPHONE_NUMBER,\n\t\tSMS_CAPABLE,\n\t\tSTATUS,\n\t\tSTATUS_TLM,\n\t\tTLM,\n\t\tVOICE_CAPABLE,\n\t\tWHEN_CREATED\n FROM ETHAN_TEST_DB.PUBLIC.ACCOUNT_PHONE_NUMBER LIMIT 100;\n \n ",
284+
"viewLanguage": "SQL"
285+
}
286+
},
287+
"systemMetadata": {
288+
"lastObserved": 1638860400000,
289+
"runId": "mode-test",
290+
"lastRunId": "no-run-id-provided"
291+
}
292+
},
273293
{
274294
"entityType": "dataset",
275295
"entityUrn": "urn:li:dataset:(urn:li:dataPlatform:mode,5450544,PROD)",
@@ -336,13 +356,14 @@
336356
"aspect": {
337357
"json": {
338358
"customProperties": {
359+
"id": "10149707",
360+
"created_at": "2021-12-10T20:55:24.361Z",
339361
"updated_at": "2021-12-10T23:12:53.273Z",
340362
"last_run_id": "1897576958",
341363
"data_source_id": "34499"
342364
},
343365
"externalUrl": "https://app.mode.com/acryl/reports/9d2da37fa91e/details/queries/6e26a9f3d4e2",
344366
"name": "Customer and staff",
345-
"description": "### Source Code\n``` sql\nSELECT rental.*, staff.first_name \"Staff First Name\", staff.last_name \"Staff Last Name\" FROM {{ @join_on_definition as rental }} join staff on staff.staff_id = rental.staff_id where selected_id = {{ selected_id }} \n{% form %}\nselected_id:\n type: text\n default: my_id\n{% endform %}\n```\n ",
346367
"tags": []
347368
}
348369
},
@@ -352,6 +373,24 @@
352373
"lastRunId": "no-run-id-provided"
353374
}
354375
},
376+
{
377+
"entityType": "dataset",
378+
"entityUrn": "urn:li:dataset:(urn:li:dataPlatform:mode,10149707,PROD)",
379+
"changeType": "UPSERT",
380+
"aspectName": "viewProperties",
381+
"aspect": {
382+
"json": {
383+
"materialized": false,
384+
"viewLogic": "SELECT rental.*, staff.first_name \"Staff First Name\", staff.last_name \"Staff Last Name\" FROM {{ @join_on_definition as rental }} join staff on staff.staff_id = rental.staff_id where selected_id = {{ selected_id }} \n{% form %}\nselected_id:\n type: text\n default: my_id\n{% endform %}",
385+
"viewLanguage": "SQL"
386+
}
387+
},
388+
"systemMetadata": {
389+
"lastObserved": 1638860400000,
390+
"runId": "mode-test",
391+
"lastRunId": "no-run-id-provided"
392+
}
393+
},
355394
{
356395
"entityType": "dataset",
357396
"entityUrn": "urn:li:dataset:(urn:li:dataPlatform:mode,10149707,PROD)",

0 commit comments

Comments
 (0)