2022-11-10 10:54:31 +01:00
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""Partition utility"""
|
|
|
|
|
2024-02-28 07:11:00 +01:00
|
|
|
from typing import List, Optional
|
2022-11-10 10:54:31 +01:00
|
|
|
|
|
|
|
from metadata.generated.schema.entity.data.table import (
|
2024-02-28 07:11:00 +01:00
|
|
|
PartitionColumnDetails,
|
|
|
|
PartitionIntervalTypes,
|
2023-12-27 19:13:44 +01:00
|
|
|
PartitionIntervalUnit,
|
2022-11-10 10:54:31 +01:00
|
|
|
PartitionProfilerConfig,
|
|
|
|
Table,
|
|
|
|
)
|
|
|
|
from metadata.generated.schema.entity.services.databaseService import (
|
|
|
|
DatabaseServiceType,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_partition_details(entity: Table) -> Optional[PartitionProfilerConfig]:
|
|
|
|
"""Build PartitionProfilerConfig object from entity
|
|
|
|
|
|
|
|
Args:
|
|
|
|
entity (Table): entity table
|
|
|
|
Returns:
|
|
|
|
PartitionProfilerConfig
|
|
|
|
"""
|
|
|
|
if (
|
|
|
|
hasattr(entity, "tableProfilerConfig")
|
|
|
|
and hasattr(entity.tableProfilerConfig, "partitioning")
|
|
|
|
and entity.tableProfilerConfig.partitioning
|
|
|
|
):
|
|
|
|
return entity.tableProfilerConfig.partitioning
|
|
|
|
|
|
|
|
if (
|
|
|
|
hasattr(entity, "serviceType")
|
|
|
|
and entity.serviceType == DatabaseServiceType.BigQuery
|
|
|
|
):
|
|
|
|
if hasattr(entity, "tablePartition") and entity.tablePartition:
|
2024-02-28 07:11:00 +01:00
|
|
|
column_partitions: Optional[
|
|
|
|
List[PartitionColumnDetails]
|
|
|
|
] = entity.tablePartition.columns
|
|
|
|
if not column_partitions:
|
|
|
|
raise TypeError("table partition missing. Skipping table")
|
|
|
|
|
|
|
|
partiton = column_partitions[0]
|
|
|
|
|
|
|
|
if partiton.intervalType == PartitionIntervalTypes.TIME_UNIT:
|
2022-11-10 10:54:31 +01:00
|
|
|
return PartitionProfilerConfig(
|
|
|
|
enablePartitioning=True,
|
2024-02-28 07:11:00 +01:00
|
|
|
partitionColumnName=partiton.columnName,
|
2023-12-27 19:13:44 +01:00
|
|
|
partitionIntervalUnit=PartitionIntervalUnit.DAY
|
2024-02-28 07:11:00 +01:00
|
|
|
if partiton.interval != "HOUR"
|
|
|
|
else partiton.interval,
|
2023-12-27 19:13:44 +01:00
|
|
|
partitionInterval=1,
|
2024-02-28 07:11:00 +01:00
|
|
|
partitionIntervalType=partiton.intervalType.value,
|
2022-11-10 10:54:31 +01:00
|
|
|
partitionValues=None,
|
2023-03-01 08:20:38 +01:00
|
|
|
partitionIntegerRangeStart=None,
|
|
|
|
partitionIntegerRangeEnd=None,
|
2022-11-10 10:54:31 +01:00
|
|
|
)
|
2024-02-28 07:11:00 +01:00
|
|
|
if partiton.intervalType == PartitionIntervalTypes.INGESTION_TIME:
|
2022-11-10 10:54:31 +01:00
|
|
|
return PartitionProfilerConfig(
|
|
|
|
enablePartitioning=True,
|
|
|
|
partitionColumnName="_PARTITIONDATE"
|
2024-02-28 07:11:00 +01:00
|
|
|
if partiton.interval == "DAY"
|
2022-11-10 10:54:31 +01:00
|
|
|
else "_PARTITIONTIME",
|
2023-12-27 19:13:44 +01:00
|
|
|
partitionIntervalUnit=PartitionIntervalUnit.DAY
|
2024-02-28 07:11:00 +01:00
|
|
|
if partiton.interval != "HOUR"
|
|
|
|
else partiton.interval,
|
2023-12-27 19:13:44 +01:00
|
|
|
partitionInterval=1,
|
2024-02-28 07:11:00 +01:00
|
|
|
partitionIntervalType=partiton.intervalType.value,
|
2022-11-10 10:54:31 +01:00
|
|
|
partitionValues=None,
|
2023-03-01 08:20:38 +01:00
|
|
|
partitionIntegerRangeStart=None,
|
|
|
|
partitionIntegerRangeEnd=None,
|
|
|
|
)
|
2024-02-28 07:11:00 +01:00
|
|
|
if partiton.intervalType == PartitionIntervalTypes.INTEGER_RANGE:
|
2023-03-01 08:20:38 +01:00
|
|
|
return PartitionProfilerConfig(
|
|
|
|
enablePartitioning=True,
|
2024-02-28 07:11:00 +01:00
|
|
|
partitionColumnName=partiton.columnName,
|
2023-03-01 08:20:38 +01:00
|
|
|
partitionIntervalUnit=None,
|
2023-12-27 19:13:44 +01:00
|
|
|
partitionInterval=None,
|
2024-02-28 07:11:00 +01:00
|
|
|
partitionIntervalType=partiton.intervalType.value,
|
2023-03-01 08:20:38 +01:00
|
|
|
partitionValues=None,
|
|
|
|
partitionIntegerRangeStart=1,
|
|
|
|
partitionIntegerRangeEnd=10000,
|
2022-11-10 10:54:31 +01:00
|
|
|
)
|
|
|
|
raise TypeError(
|
2024-02-28 07:11:00 +01:00
|
|
|
f"Unsupported partition type {partiton.intervalType}. Skipping table"
|
2022-11-10 10:54:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return None
|