2025-04-03 10:39:47 +05:30
|
|
|
# Copyright 2025 Collate
|
|
|
|
# Licensed under the Collate Community License, Version 1.0 (the "License");
|
2025-02-16 20:58:27 +01:00
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2025-04-03 10:39:47 +05:30
|
|
|
# https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE
|
2025-02-16 20:58:27 +01:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Interfaces with database for all database engine
|
|
|
|
supporting sqlalchemy abstraction layer
|
|
|
|
"""
|
2025-07-08 17:55:20 +05:30
|
|
|
|
|
|
|
from sqlalchemy import Column, text
|
|
|
|
|
2025-02-16 20:58:27 +01:00
|
|
|
from metadata.ingestion.source.database.databricks.connection import (
|
|
|
|
get_connection as databricks_get_connection,
|
|
|
|
)
|
2025-07-08 17:55:20 +05:30
|
|
|
from metadata.profiler.orm.types.custom_array import CustomArray
|
2025-02-16 20:58:27 +01:00
|
|
|
from metadata.sampler.sqlalchemy.sampler import SQASampler
|
|
|
|
|
|
|
|
|
|
|
|
class UnityCatalogSamplerInterface(SQASampler):
|
2025-06-27 08:58:25 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""Initialize with a single Databricks connection"""
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.connection = databricks_get_connection(self.service_connection_config)
|
|
|
|
|
2025-02-16 20:58:27 +01:00
|
|
|
def get_client(self):
|
|
|
|
"""client is the session for SQA"""
|
2025-03-25 13:48:18 +01:00
|
|
|
client = super().get_client()
|
|
|
|
self.set_catalog(client)
|
|
|
|
return client
|
2025-07-08 17:55:20 +05:30
|
|
|
|
|
|
|
def _handle_array_column(self, column: Column) -> bool:
|
|
|
|
"""Check if a column is an array type"""
|
|
|
|
return isinstance(column.type, CustomArray)
|
|
|
|
|
|
|
|
def _get_slice_expression(self, column: Column):
|
|
|
|
"""Generate SQL expression to slice array elements at query level
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column_name: Name of the column
|
|
|
|
max_elements: Maximum number of elements to extract
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
SQL expression string for array slicing
|
|
|
|
"""
|
|
|
|
max_elements = self._get_max_array_elements()
|
|
|
|
return text(
|
|
|
|
f"""
|
|
|
|
CASE
|
|
|
|
WHEN `{column.name}` IS NULL THEN NULL
|
|
|
|
ELSE slice(`{column.name}`, 1, {max_elements})
|
|
|
|
END AS `{column._label}`
|
|
|
|
"""
|
|
|
|
)
|