Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ingest/snowflake): support for more operation types #8158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ def operational_data_for_time_window(
ON access_history.user_name = users.name
WHERE query_start_time >= to_timestamp_ltz({start_time_millis}, 3)
AND query_start_time < to_timestamp_ltz({end_time_millis}, 3)
AND query_history.query_type in ('INSERT', 'UPDATE', 'DELETE', 'CREATE', 'CREATE_TABLE', 'CREATE_TABLE_AS_SELECT')
AND access_history.objects_modified is not null
AND ARRAY_SIZE(access_history.objects_modified) > 0
ORDER BY query_start_time DESC
;"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
"CREATE": OperationTypeClass.CREATE,
"CREATE_TABLE": OperationTypeClass.CREATE,
"CREATE_TABLE_AS_SELECT": OperationTypeClass.CREATE,
"MERGE": OperationTypeClass.CUSTOM,
"COPY": OperationTypeClass.CUSTOM,
"TRUNCATE_TABLE": OperationTypeClass.CUSTOM,
Comment on lines +46 to +48
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that putting CUSTOM allows us to show the customOperationType, but for other sources we try to label these:

(unity)

    QueryStatementType.COPY: OperationTypeClass.INSERT,
    QueryStatementType.UPDATE: OperationTypeClass.UPDATE,
    QueryStatementType.MERGE: OperationTypeClass.UPDATE,
    QueryStatementType.DELETE: OperationTypeClass.DELETE,
    QueryStatementType.TRUNCATE: OperationTypeClass.DELETE,

(bigquery)

    "MERGE": OperationTypeClass.UPDATE,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MERGE is in fact UPDATE and/or DELETE and/or INSERT, so I was hesitant to mark it as UPDATE. CUSTOM operationType is not great to use. cc: @jjoyce0510 . We could add more operation types in model maybe ? I am proposing addition of more operation types to - MERGE, COPY, TRUNCATE respectively. Does that make sense ?

# TODO: Dataset for below query types are not detected by snowflake in snowflake.access_history.objects_modified.
# However it seems possible to support these using sql parsing in future.
# When this support is added, snowflake_query.operational_data_for_time_window needs to be updated.
# "CREATE_VIEW": OperationTypeClass.CREATE,
# "CREATE_EXTERNAL_TABLE": OperationTypeClass.CREATE,
# "ALTER_TABLE_MODIFY_COLUMN": OperationTypeClass.ALTER,
# "ALTER_TABLE_ADD_COLUMN": OperationTypeClass.ALTER,
# "RENAME_COLUMN": OperationTypeClass.ALTER,
# "ALTER_SET_TAG": OperationTypeClass.ALTER,
# "ALTER_TABLE_DROP_COLUMN": OperationTypeClass.ALTER,
# "ALTER": OperationTypeClass.ALTER,
}


Expand Down Expand Up @@ -328,12 +342,14 @@ def _check_usage_date_ranges(self) -> Any:
def _get_operation_aspect_work_unit(
self, event: SnowflakeJoinedAccessEvent, discovered_datasets: List[str]
) -> Iterable[MetadataWorkUnit]:
if event.query_start_time and event.query_type in OPERATION_STATEMENT_TYPES:
if event.query_start_time and event.query_type:
start_time = event.query_start_time
query_type = event.query_type
user_email = event.email
user_name = event.user_name
operation_type = OPERATION_STATEMENT_TYPES[query_type]
operation_type = OPERATION_STATEMENT_TYPES.get(
query_type, OperationTypeClass.CUSTOM
)
reported_time: int = int(time.time() * 1000)
last_updated_timestamp: int = int(start_time.timestamp() * 1000)
user_urn = make_user_urn(self.get_user_identifier(user_name, user_email))
Expand Down Expand Up @@ -363,6 +379,9 @@ def _get_operation_aspect_work_unit(
lastUpdatedTimestamp=last_updated_timestamp,
actor=user_urn,
operationType=operation_type,
customOperationType=query_type
if operation_type is OperationTypeClass.CUSTOM
else None,
Comment on lines +382 to +384
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there harm in passing this if the operation type class is not CUSTOM, for more info? I know we don't do this elsewhere in the code but could start here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I thought that myself as well. Then customOperationType just behaves more like "nativeOperationType" . cc - @jjoyce0510 - can you confirm this one as well please ?

)
mcp = MetadataChangeProposalWrapper(
entityUrn=dataset_urn,
Expand Down