-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgql_parser.py
411 lines (351 loc) · 12.5 KB
/
gql_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from typing import Dict, List, Optional
from gql import Client, gql
from pydantic import BaseModel
from metaphor.common.entity_id import normalize_full_dataset_name, to_person_entity_id
from metaphor.common.logger import get_logger
from metaphor.datahub.config import DatahubConfig
from metaphor.models.metadata_change_event import (
AssetDescription,
ColumnDescriptionAssignment,
ColumnTagAssignment,
)
from metaphor.models.metadata_change_event import DataPlatform as MetaphorDataPlatform
from metaphor.models.metadata_change_event import Dataset as MetaphorDataset
from metaphor.models.metadata_change_event import (
DatasetLogicalID,
DescriptionAssignment,
EntityType,
)
from metaphor.models.metadata_change_event import Ownership as MetaphorOwnership
from metaphor.models.metadata_change_event import OwnershipAssignment, TagAssignment
logger = get_logger()
DATAHUB_PLATFORM_MAPPING: Dict[str, MetaphorDataPlatform] = {
"adlsGen1": MetaphorDataPlatform.AZURE_DATA_LAKE_STORAGE,
"adlsGen2": MetaphorDataPlatform.AZURE_DATA_LAKE_STORAGE,
"external": MetaphorDataPlatform.EXTERNAL,
"hive": MetaphorDataPlatform.HIVE,
"s3": MetaphorDataPlatform.S3,
"kafka": MetaphorDataPlatform.KAFKA,
"kafka-connect": MetaphorDataPlatform.KAFKA,
"mariadb": MetaphorDataPlatform.MYSQL,
"mongodb": MetaphorDataPlatform.DOCUMENTDB,
"mysql": MetaphorDataPlatform.MYSQL,
"postgres": MetaphorDataPlatform.POSTGRESQL,
"snowflake": MetaphorDataPlatform.SNOWFLAKE,
"redshift": MetaphorDataPlatform.REDSHIFT,
"mssql": MetaphorDataPlatform.MSSQL,
"bigquery": MetaphorDataPlatform.BIGQUERY,
"glue": MetaphorDataPlatform.GLUE,
"elasticsearch": MetaphorDataPlatform.ELASTICSEARCH,
"trino": MetaphorDataPlatform.TRINO,
"databricks": MetaphorDataPlatform.UNITY_CATALOG,
"gcs": MetaphorDataPlatform.GCS,
"dynamodb": MetaphorDataPlatform.DYNAMODB,
"delta-lake": MetaphorDataPlatform.UNITY_CATALOG,
}
"""
Source: https://raw.githubusercontent.com/datahub-project/datahub/master/metadata-service/war/src/main/resources/boot/data_platforms.json
Currently unsupported datahub platforms:
- airflow
- ambry
- clickhouse
- couchbase
- hdfs
- hana
- iceberg
- kusto
- mode
- openapi
- oracle
- pinot
- presto
- tableau
- teradata
- voldemort
- druid
- looker
- feast
- sagemaker
- mlflow
- redash
- athena
- spark
- dbt
- Great Expectations
- powerbi
- presto-on-hive
- metabase
- nifi
- superset
- pulsar
- salesforce
- vertica
- fivetran
- csv
"""
class DatasetProperties(BaseModel):
description: Optional[str]
class DataPlatformProperties(BaseModel):
datasetNameDelimiter: str
class DataPlatform(BaseModel):
name: str
properties: Optional[DataPlatformProperties]
class OwnerTypeProperties(BaseModel):
email: Optional[str]
class OwnerType(BaseModel):
properties: Optional[OwnerTypeProperties]
class OwnershipTypeInfo(BaseModel):
name: str
class OwnershipTypeEntity(BaseModel):
info: Optional[OwnershipTypeInfo]
class Owner(BaseModel):
owner: OwnerType
ownershipType: Optional[OwnershipTypeEntity]
@property
def metaphor_ownership(self) -> Optional[MetaphorOwnership]:
contact_designation_name = None
if self.ownershipType and self.ownershipType.info:
contact_designation_name = self.ownershipType.info.name
person = None
if self.owner.properties and self.owner.properties.email:
person = str(to_person_entity_id(self.owner.properties.email))
if not person and not contact_designation_name:
return None
return MetaphorOwnership(
contact_designation_name=contact_designation_name,
person=person,
)
class Ownership(BaseModel):
owners: List[Owner]
class TagProperties(BaseModel):
name: str
description: Optional[str]
class Tag(BaseModel):
properties: Optional[TagProperties]
class TagAssociation(BaseModel):
tag: Tag
class GlobalTags(BaseModel):
tags: List[TagAssociation]
@property
def tag_names(self) -> List[str]:
return [tag.tag.properties.name for tag in self.tags if tag.tag.properties]
class SchemaField(BaseModel):
fieldPath: str
description: Optional[str]
tags: Optional[GlobalTags]
def column_description_assignment(self, author: str):
if not self.description:
return None
return ColumnDescriptionAssignment(
column_name=self.fieldPath,
asset_descriptions=[
AssetDescription(author=author, description=self.description)
],
)
def column_tag_assignment(self):
if not self.tags:
return None
return ColumnTagAssignment(
column_name=self.fieldPath,
tag_names=self.tags.tag_names,
)
class SchemaMetadata(BaseModel):
fields: List[SchemaField]
class EditableSchemaMetadata(BaseModel):
editableSchemaFieldInfo: List[SchemaField]
class Dataset(BaseModel):
properties: Optional[DatasetProperties]
editableProperties: Optional[DatasetProperties]
platform: DataPlatform
name: str
tags: Optional[GlobalTags]
ownership: Optional[Ownership]
schemaMetadata: Optional[SchemaMetadata]
editableSchemaMetadata: Optional[EditableSchemaMetadata]
def get_logical_id(self, config: DatahubConfig) -> DatasetLogicalID:
# It's possible that we want to split the name by the platform delimiters to get part names.
name = normalize_full_dataset_name(self.name)
metaphor_platform = DATAHUB_PLATFORM_MAPPING.get(
self.platform.name, MetaphorDataPlatform.UNKNOWN
)
if metaphor_platform is MetaphorDataPlatform.UNKNOWN:
logger.warning(
f"Found unknown data platform {self.platform.name}, will not ingest dataset {name}"
)
return DatasetLogicalID(
account=config.get_account(metaphor_platform),
name=name,
platform=metaphor_platform,
)
def get_schema_fields(self) -> Optional[List[SchemaField]]:
if self.editableSchemaMetadata:
return self.editableSchemaMetadata.editableSchemaFieldInfo
if self.schemaMetadata:
return self.schemaMetadata.fields
return None
def description_assignment(self, author: str) -> Optional[DescriptionAssignment]:
asset_descriptions = None
if self.editableProperties and self.editableProperties.description:
asset_descriptions = [
AssetDescription(
author=author, description=self.editableProperties.description
)
]
elif self.properties and self.properties.description:
asset_descriptions = [
AssetDescription(author=author, description=self.properties.description)
]
column_descriptions = None
schema_fields = self.get_schema_fields()
if schema_fields:
raw_col_descriptions = [
f.column_description_assignment(author) for f in schema_fields
]
filtered_col_descriptions = [x for x in raw_col_descriptions if x]
if len(filtered_col_descriptions):
column_descriptions = filtered_col_descriptions
if not asset_descriptions and not column_descriptions:
return None
return DescriptionAssignment(
asset_descriptions=asset_descriptions,
column_description_assignments=column_descriptions,
)
def ownership_assignment(self) -> Optional[OwnershipAssignment]:
if not self.ownership or not self.ownership.owners:
return None
raw_owners = [owner.metaphor_ownership for owner in self.ownership.owners]
filtered_owners = [x for x in raw_owners if x]
if not filtered_owners:
return None
return OwnershipAssignment(
ownerships=filtered_owners,
)
def tag_assignment(self) -> Optional[TagAssignment]:
tag_names = None
if self.tags:
tag_names = self.tags.tag_names
column_tag_assignments = None
schema_fields = self.get_schema_fields()
if schema_fields:
raw_column_tag_assignments = [
field.column_tag_assignment() for field in schema_fields
]
filtered_column_tag_assignments = [
x for x in raw_column_tag_assignments if x
]
if filtered_column_tag_assignments:
column_tag_assignments = filtered_column_tag_assignments
if not tag_names and not column_tag_assignments:
return None
return TagAssignment(
tag_names=tag_names, column_tag_assignments=column_tag_assignments
)
def as_metaphor_dataset(self, config: DatahubConfig) -> MetaphorDataset:
logical_id = self.get_logical_id(config)
ownership_assignment = self.ownership_assignment()
if config.description_author_email:
author = str(to_person_entity_id(config.description_author_email))
elif (
ownership_assignment
and ownership_assignment.ownerships
and ownership_assignment.ownerships[0].person
):
# Use the first owner as our author, datahub does not keep track of description authors
author = ownership_assignment.ownerships[0].person
else:
# Have to use a placeholder email
author = str(to_person_entity_id("[email protected]"))
return MetaphorDataset(
entity_type=EntityType.DATASET,
logical_id=logical_id,
ownership_assignment=self.ownership_assignment(),
description_assignment=self.description_assignment(author),
tag_assignment=self.tag_assignment(),
)
def get_dataset(client: Client, urn: str) -> Dataset:
query = gql(
"""
query getDatasetInfo ($urn: String!) {
dataset (urn: $urn) {
properties {
description
}
editableProperties {
description
}
name
platform {
name
properties {
datasetNameDelimiter
}
}
tags {
tags {
tag {
properties {
name
description
}
}
}
}
ownership {
owners {
ownershipType {
info {
name
}
}
owner {
... on CorpUser {
properties {
email
}
}
... on CorpGroup {
properties {
email
}
}
}
}
}
schemaMetadata {
fields {
fieldPath
description
tags {
tags {
tag {
properties {
name
description
}
}
}
}
}
}
editableSchemaMetadata {
editableSchemaFieldInfo {
fieldPath
description
tags {
tags {
tag {
properties {
name
description
}
}
}
}
}
}
}
}
"""
)
response = client.execute(query, variable_values={"urn": urn})
return Dataset.model_validate(response["dataset"])