2022-11-15 20:31:10 +05:30
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
Helper module to handle data sampling
|
|
|
|
for the profiler
|
|
|
|
"""
|
2023-01-16 22:17:46 +05:30
|
|
|
import math
|
|
|
|
import random
|
2023-03-01 08:20:38 +01:00
|
|
|
from typing import Any, Optional
|
2022-11-15 20:31:10 +05:30
|
|
|
|
2023-01-16 22:17:46 +05:30
|
|
|
from metadata.generated.schema.entity.data.table import ProfileSampleType, TableData
|
2023-03-01 08:20:38 +01:00
|
|
|
from metadata.profiler.api.models import ProfileSampleConfig
|
2023-01-16 22:17:46 +05:30
|
|
|
from metadata.utils.constants import CHUNKSIZE
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
RANDOM_LABEL = "random"
|
|
|
|
|
|
|
|
|
|
|
|
class DatalakeSampler:
|
|
|
|
"""
|
|
|
|
Generates a sample of the data to not
|
|
|
|
run the query in the whole table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
session: Optional[Any],
|
|
|
|
table,
|
2023-01-16 22:17:46 +05:30
|
|
|
profile_sample_config: Optional[ProfileSampleConfig] = None,
|
2022-11-15 20:31:10 +05:30
|
|
|
profile_sample_query: Optional[str] = None,
|
|
|
|
):
|
2023-01-16 22:17:46 +05:30
|
|
|
self.profile_sample = None
|
|
|
|
self.profile_sample_type = None
|
|
|
|
if profile_sample_config:
|
|
|
|
self.profile_sample = profile_sample_config.profile_sample
|
|
|
|
self.profile_sample_type = profile_sample_config.profile_sample_type
|
2022-11-15 20:31:10 +05:30
|
|
|
self.session = session
|
|
|
|
self.table = table
|
|
|
|
self._profile_sample_query = profile_sample_query
|
|
|
|
self.sample_limit = 100
|
|
|
|
self._sample_rows = None
|
|
|
|
|
2023-01-16 22:17:46 +05:30
|
|
|
def _fetch_rows(self, data_frame):
|
|
|
|
from pandas import notnull # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
sampled_data_frame = data_frame.sample(
|
|
|
|
n=(int(self.profile_sample) or 100)
|
|
|
|
if self.profile_sample_type == ProfileSampleType.ROWS
|
|
|
|
else None,
|
|
|
|
frac=self.profile_sample
|
|
|
|
if self.profile_sample_type == ProfileSampleType.PERCENTAGE
|
|
|
|
else None,
|
|
|
|
random_state=random.randint(0, 100),
|
|
|
|
replace=True,
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
sampled_data_frame.astype(object)
|
|
|
|
.where(
|
|
|
|
notnull(sampled_data_frame),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.values.tolist()
|
|
|
|
)
|
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
def get_col_row(self, data_frame):
|
2023-01-16 22:17:46 +05:30
|
|
|
"""
|
|
|
|
Fetches columns and rows from the data_frame
|
|
|
|
"""
|
|
|
|
from pandas import DataFrame # pylint: disable=import-outside-toplevel
|
2022-11-17 10:11:54 +01:00
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
cols = []
|
|
|
|
chunk = None
|
|
|
|
if isinstance(data_frame, DataFrame):
|
|
|
|
return (
|
2023-02-22 09:42:34 +01:00
|
|
|
data_frame.columns.tolist(),
|
2023-01-16 22:17:46 +05:30
|
|
|
self._fetch_rows(data_frame),
|
2022-11-15 20:31:10 +05:30
|
|
|
)
|
2023-01-16 22:17:46 +05:30
|
|
|
chunk_limit = math.ceil(self.profile_sample / CHUNKSIZE)
|
2023-02-22 09:42:34 +01:00
|
|
|
cols = data_frame[0].columns.tolist()
|
2023-01-16 22:17:46 +05:30
|
|
|
rows = []
|
|
|
|
for index, chunk in enumerate(data_frame):
|
|
|
|
if index >= chunk_limit:
|
|
|
|
break
|
|
|
|
rows.extend(self._fetch_rows(chunk))
|
|
|
|
return cols, rows
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
def fetch_dl_sample_data(self) -> TableData:
|
2022-11-17 10:11:54 +01:00
|
|
|
from pandas import DataFrame # pylint: disable=import-outside-toplevel
|
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
cols, rows = self.get_col_row(
|
|
|
|
data_frame=self.table[0]
|
|
|
|
if not isinstance(self.table, DataFrame)
|
|
|
|
else self.table
|
|
|
|
)
|
|
|
|
return TableData(columns=cols, rows=rows)
|