| 
									
										
										
										
											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. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2025-06-09 14:18:35 +05:30
										 |  |  | Helper module to handle data sampling for the profiler | 
					
						
							| 
									
										
										
										
											2025-02-16 20:58:27 +01:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2025-08-29 07:46:23 +05:30
										 |  |  | from sqlalchemy import Column, event, text | 
					
						
							|  |  |  | from sqlalchemy.orm import scoped_session, sessionmaker | 
					
						
							| 
									
										
										
										
											2025-07-08 17:55:20 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-09 14:18:35 +05:30
										 |  |  | class DatabricksSamplerInterface(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-08-29 07:46:23 +05:30
										 |  |  |         session_maker = sessionmaker(bind=self.connection) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @event.listens_for(session_maker, "after_begin") | 
					
						
							|  |  |  |         def set_catalog(session, transaction, connection): | 
					
						
							|  |  |  |             # Safely quote the catalog name to prevent SQL injection | 
					
						
							|  |  |  |             quoted_catalog = connection.dialect.identifier_preparer.quote( | 
					
						
							|  |  |  |                 self.service_connection_config.catalog | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             connection.execute(f"USE CATALOG {quoted_catalog};") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.session_factory = scoped_session(session_maker) | 
					
						
							| 
									
										
										
										
											2025-06-27 08:58:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}` | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         ) |