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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
#


import logging
from dataclasses import dataclass
from functools import lru_cache, reduce, wraps
from pickle import dumps, loads
from typing import Any, Iterator, List, Optional, Union

import numpy as np
import pandas as pd
import polars as pl
import pyarrow as pa
import pyarrow.compute as pc
import pyarrow.parquet as pq
import torch
from fairseq2.data.data_pipeline import (
    DataPipeline,
    DataPipelineBuilder,
    read_iterator,
    read_sequence,
)
from fairseq2.data.parquet.tools import (
    NestedDict,
    NestedDictValue,
    add_partitioning_values,
    compute_rows_length,
    get_dataset_fragments,
    split_fragment_in_row_groups,
)
from joblib import Parallel, delayed
from numpy.typing import NDArray
from pyarrow.dataset import get_partition_keys
from retrying import retry
from stopes.modules.preprocess.sonar_text_embedding import (
    LangColumnConfig,
    SonarTextBatchEmbedder,
    SonarTextEmbedderConfig,
)
from stopes.pipelines.monolingual.utils.sentence_split import get_split_algo
from stopes.utils.arrow_utils import (
    hstack_pyarray_list,
    is_list_like,
    pyarrow_column_to_array,
    simple_array_to_nested,
)
from tqdm.auto import tqdm

from lcm.datasets.configs import (
    ColumnsNames,
    ParquetDatasetLimitOptions,
    SonarTextColumn,
)
from lcm.utils.common import batched

try:
    from numba import njit
except ModuleNotFoundError:
    print("Numba is not installed. Fall-back to the non-recompiled version")

    def empty_jit(f):
        @wraps(f)
        def _f(*args, **kwargs):
            return f(*args, **kwargs)

        return _f

    njit = empty_jit


loading_retry = retry(
    retry_on_exception=lambda exception: isinstance(exception, OSError),
    stop_max_attempt_number=1,
    wait_exponential_multiplier=2,
    wait_exponential_max=20,
)


logger = logging.getLogger(__name__)


def prefix_and_suffix_one_list_column(
    table: pa.Table, column: str, prefix_array: pa.Array, suffix_array: pa.Array
):
    prefix_extended = pa.chunked_array(
        [pa.ListArray.from_arrays([0, len(prefix_array)], prefix_array)] * len(table)
    )
    suffix_extended = pa.chunked_array(
        [pa.ListArray.from_arrays([0, len(suffix_array)], suffix_array)] * len(table)
    )
    target_dtype = table[column].type
    if prefix_extended.type != target_dtype:
        prefix_extended = prefix_extended.cast(target_dtype)
    if suffix_extended.type != target_dtype:
        suffix_extended = suffix_extended.cast(target_dtype)

    new_array = hstack_pyarray_list(prefix_extended, table[column], suffix_extended)
    return table.drop([column]).append_column(column, new_array)


def define_parquet_dataset(parquet_path: str, partition_filters) -> pq.ParquetDataset:
    return pq.ParquetDataset(
        parquet_path,
        filters=partition_filters,
    )


@lru_cache()
def default_sonar_pipeline() -> SonarTextBatchEmbedder:
    local_sonar_config = SonarTextEmbedderConfig(
        column_config=[
            LangColumnConfig("input_text", lang_value="eng_Latn"),
        ],
        batch_size=10,
        device="cpu",
    )
    return SonarTextBatchEmbedder(local_sonar_config)


@lru_cache(2000)
def _get_embed_sentences(text: Optional[str]) -> pa.Array:
    sentences_splitter = get_split_algo("eng_Latn", "default")
    lstbe = default_sonar_pipeline()
    sentences = pa.array(sentences_splitter(text) if text else [""])
    input_table = pa.Table.from_pydict({"input_text": sentences})
    vectors = pyarrow_column_to_array(lstbe(input_table)["input_text_sonar_emb"])
    if not text:
        # empty output of the right type
        vectors = vectors.slice(0, 0)
        sentences = sentences.slice(0, 0)
    return vectors, sentences


def prepare_suffix_prefix_embeddings(*args):
    if all(xx is None for xx in args):  # to avoid loading SonarModel
        return [(None, None) for _ in args]

    return [_get_embed_sentences(xx) for xx in args]


def from_pyarrow_to_torch_tensor(
    arr: Union[pa.Array, pa.ChunkedArray], strict: bool = False
) -> NestedDictValue:
    """
    struct_array = pa.Array.from_pandas([{"x": 4, "y": "RR"}] * 10)
    nest_array = pa.Array.from_pandas([[{'a': 1}, {'a': 2}]])
    """
    # for future ideas https://arrow.apache.org/docs/python/generated/pyarrow.Tensor.html
    # for sparse matrix support https://github.com/apache/arrow/blob/main/python/pyarrow/tests/test_sparse_tensor.py

    if arr.null_count != 0:
        raise ValueError("to torch conversion does not support null values")

    arr = pyarrow_column_to_array(arr)

    arr_type = arr.type
    if pa.types.is_primitive(arr_type):
        try:
            return torch.from_numpy(arr.to_numpy(zero_copy_only=True))
        except Exception:
            pass

    try:
        return torch.from_numpy(arr.to_numpy(zero_copy_only=True))
    except pa.ArrowInvalid:
        pass

    if pa.types.is_dictionary(arr_type):
        return from_pyarrow_to_torch_tensor(arr.dictionary_decode())

    if pa.types.is_string(arr_type):
        return arr.to_pandas().tolist()

    if pa.types.is_list(arr_type) or pa.types.is_large_list(arr_type):
        if pa.types.is_primitive(arr_type.value_type):
            return arr.to_pandas().map(torch.from_numpy).tolist()

        if pa.types.is_fixed_size_list(arr_type.value_type) and pa.types.is_primitive(
            arr_type.value_type.value_type
        ):
            return (
                arr.to_pandas()
                .map(
                    lambda x: torch.from_numpy(
                        np.vstack(x) if len(x) > 0 else np.array([], dtype=np.float32)
                    )
                )
                .tolist()
            )

    if pa.types.is_fixed_size_list(arr_type):
        if pa.types.is_primitive(arr_type.value_type):
            return torch.from_numpy(np.reshape(arr.values, (-1, arr_type.list_size)))

    if pa.types.is_struct(arr_type):
        return {
            arr_type.field(i).name: from_pyarrow_to_torch_tensor(arr.field(i))
            for i in range(arr_type.num_fields)
        }

    if pa.types.is_nested(arr_type):
        # TODO: deal with arr = [[{'a': 1}, {'a': 2}]]
        pass

    if strict:
        raise NotImplementedError(f"{arr_type} cannot be converted to torch.Tensor")
    else:
        return arr  # keeping as in the orignal pyarrow form


def pyarrow_table_to_torch_dict(tt: pa.Table, strict: bool = False) -> NestedDict:
    out = {}
    for col in tt.column_names:
        try:
            out[col] = from_pyarrow_to_torch_tensor(tt[col], strict)
        except ValueError as e:
            logger.info(
                f"Column {col} of type {tt[col].type} was not converted to torch as expected",
                str(e),
            )
            out[col] = tt[col]
    return out


def add_fragments_trace(table: pa.Table, fragment: pa.dataset.Fragment) -> pa.Table:
    table = table.append_column(
        "__row_groups_ids",
        len(table)
        * [np.array([int(rg.id) for rg in fragment.row_groups], dtype=np.int32)],
    )
    table = table.append_column(
        "__index_in_fragement", pa.array(np.arange(len(table), dtype=np.int32))
    )
    return table


def shuffle_table(table: pa.Table, random_state: np.random.RandomState) -> pa.Table:
    permutation = pa.array(random_state.permutation(len(table)))
    return table.take(permutation)


class SafeFragment:
    """
    Experimental :
    Simple wrapper around `ParquetFileFragment` that allows to reinit the state of filesystem
    if aws session token has expired.
    """

    fragment: pa.dataset.ParquetFileFragment

    def __init__(self, fragment: pa.dataset.ParquetFileFragment):
        self.fragment = fragment

    def __repr__(self) -> str:
        out = ""
        out += "SafeFragment \n"
        out += "path = " + self.fragment.path + "\n"
        out += f"row_groups = {[int(rg.id) for rg in self.fragment.row_groups]} \n"
        out += f"physical_schema = \n {self.fragment.physical_schema} \n"
        return out

    @loading_retry
    def load(self, columns: Optional[List[str]] = None) -> pa.Table:
        if columns is not None:
            fragment_columns = [
                col for col in columns if col in self.fragment.physical_schema.names
            ]
        else:
            fragment_columns = self.fragment.physical_schema.names
        # adding technical columns for tracking
        fragment_columns = list(fragment_columns) + [
            "__batch_index",
            "__fragment_index",
            "__filename",
        ]
        try:
            fragment_table = self.fragment.to_table(
                columns=fragment_columns, use_threads=False
            )

        except OSError as e:
            logger.info(
                "could not load fragment, reinit the fragment state. Error: ", str(e)
            )
            self.fragment = loads(dumps(self.fragment))
            fragment_table = self.fragment.to_table(
                columns=fragment_columns, use_threads=False
            )

        fragment_table = add_partitioning_values(fragment_table, self.fragment, columns)
        fragment_table = add_fragments_trace(fragment_table, self.fragment)
        return fragment_table


def _parquet_fragments_to_pipeline_builder(
    file_ds_fragments: List[pa.dataset.Fragment],
    nb_epochs: int = 1,
    shuffle: bool = True,
    seed: Optional[int] = None,
) -> DataPipelineBuilder:
    if shuffle:
        if seed is None:
            seed = int(torch.randint(0, 2**31, ()).item())

        rsg = np.random.RandomState(seed)
        ds_fragments_ = np.asarray(file_ds_fragments, dtype="O")
        ds_fragments = np.concatenate(
            [rsg.permutation(ds_fragments_) for _ in range(nb_epochs)]
        ).tolist()
    else:
        ds_fragments = file_ds_fragments * nb_epochs

    pipeline_builder = read_sequence(ds_fragments)
    pipeline_builder = pipeline_builder.map(SafeFragment)
    return pipeline_builder


def list_parquet_fragments(
    parquet_ds: pq.ParquetDataset,
    nb_epochs: int = 1,
    split_to_row_groups: bool = True,
    shuffle: bool = True,
    seed: Optional[int] = None,
    limit_options: Optional[ParquetDatasetLimitOptions] = None,
    nb_jobs: int = 10,
) -> DataPipelineBuilder:
    if limit_options is None:
        limit_options = ParquetDatasetLimitOptions()

    file_ds_fragments = get_dataset_fragments(parquet_ds, parquet_ds._filter_expression)
    proxy_ds_path = "/".join(parquet_ds.files[0].split("=")[0].split("/")[:-1])

    logger.info(f"{proxy_ds_path} : full number of files {len(file_ds_fragments)}")
    if limit_options.fraction_of_files is not None:
        file_ds_fragments = file_ds_fragments[
            : max(
                int(round(limit_options.fraction_of_files * len(file_ds_fragments))), 1
            )
        ]
        logger.info(
            f"{proxy_ds_path} : reducing number of files to {len(file_ds_fragments)} because of fraction_of_files={limit_options.fraction_of_files}"
        )
    if limit_options.nb_files is not None and limit_options.nb_files < len(
        file_ds_fragments
    ):
        file_ds_fragments = file_ds_fragments[: limit_options.nb_files]
        logger.info(
            f"{proxy_ds_path} : reducing number of files to {len(file_ds_fragments)} because of nb_files={limit_options.nb_files}"
        )

    output_fragments = []
    total_nb_rows = 0
    if split_to_row_groups:
        logger.info(f"{proxy_ds_path} : starting split in row groups")

        with Parallel(backend="threading", n_jobs=nb_jobs) as parallel:
            total_nb_fragments = 0
            early_stop = False

            for batch_of_files in batched(file_ds_fragments, 20 * nb_jobs):
                row_groups = parallel(
                    delayed(split_fragment_in_row_groups)(ff) for ff in batch_of_files
                )
                new_file_fragments = [x for y in row_groups for x in y]
                if limit_options.nb_rows is not None:
                    new_file_fragments_stats = parallel(
                        delayed(lambda frag: frag.row_groups[0].num_rows)(ff)
                        for ff in new_file_fragments
                    )
                else:
                    new_file_fragments_stats = [0] * len(new_file_fragments)

                for nb_row, frag in zip(new_file_fragments_stats, new_file_fragments):
                    output_fragments.append(frag)
                    total_nb_rows += nb_row
                    total_nb_fragments += 1
                    if (
                        limit_options.nb_fragments is not None
                        and total_nb_fragments >= limit_options.nb_fragments
                    ):
                        early_stop = True
                        if limit_options.nb_rows is not None:
                            logger.info(
                                f"{proxy_ds_path} : nb_fragments limit {limit_options.nb_fragments} was reached with around {total_nb_rows} rows"
                            )
                        else:
                            logger.info(
                                f"{proxy_ds_path} : nb_fragments limit {limit_options.nb_fragments} was reached"
                            )
                        break
                    if (
                        limit_options.nb_rows is not None
                        and total_nb_rows >= limit_options.nb_rows
                    ):
                        early_stop = True
                        logger.info(
                            f"{proxy_ds_path} : nb_rows limit {limit_options.nb_rows} was reached with around {total_nb_fragments} fragments"
                        )
                        break
                if early_stop:
                    break
    else:
        for frag in file_ds_fragments[: limit_options.nb_fragments]:
            output_fragments.append(frag)
            if limit_options.nb_rows is not None:
                total_nb_rows += frag.count_rows()
                if total_nb_rows >= limit_options.nb_rows:
                    break

    logger.info(f"{proxy_ds_path} : finding fragments {len(output_fragments)}")

    return _parquet_fragments_to_pipeline_builder(
        output_fragments,
        nb_epochs=nb_epochs,
        shuffle=shuffle,
        seed=seed,
    )


def compute_length_splits(
    length_col: NDArray[np.int32],
    max_tokens: int,
    order_by_length: bool = True,
    drop_long_sample: bool = True,
) -> List[NDArray[np.int32]]:
    """split sequence of length_col in the chunks such that total length is ~ max_tokens
        countint the padding to max length of elements in a chunk

    Args:
        length_col (np.ndarray):
        max_tokens (int):
        order_by_length (bool):
        drop_long_sample (bool):

    Returns:
        List[np.ndarray]: splits that contain indices over the original length_col
    """
    argsort_ind = (
        np.argsort(length_col)
        if order_by_length
        else np.arange(len(length_col), dtype=np.int32)
    )

    sorted_length_col = length_col[argsort_ind]

    small_elements_masks = sorted_length_col <= max_tokens
    big_elements_inds = argsort_ind[~small_elements_masks]

    argsort_ind = argsort_ind[small_elements_masks]
    sorted_length_col = sorted_length_col[small_elements_masks]

    size = len(sorted_length_col)
    splits = []
    begin, end = 0, 0
    while end < size:
        current_max_len = sorted_length_col[begin]
        begin = end
        while end < size:
            current_max_len = max(current_max_len, sorted_length_col[end])
            if current_max_len * (end + 1 - begin) > max_tokens:
                splits.append(argsort_ind[begin:end])
                break
            end += 1
    else:
        if begin < size:
            splits.append(argsort_ind[begin:])

    # adding big sample at the end one by one
    if not drop_long_sample and len(big_elements_inds):
        splits.extend(np.array_split(big_elements_inds, len(big_elements_inds)))

    return splits


def build_batching_loop_over_one_table(
    table: pa.Table,
    order_by_length: bool = False,
    length_column: List[Optional[str]] = None,
    batch_size: Optional[int] = None,
    max_tokens: Optional[int] = None,
    shuffle: bool = True,
    seed: Optional[int] = None,
    num_parallel_calls: int = 1,
) -> DataPipeline:
    if max_tokens is not None:
        assert length_column is not None, (
            "Need to provide a column to compute the number of tokens"
        )

    random_state = np.random.RandomState(seed)
    if length_column is not None and len(length_column) > 0:
        length_col = reduce(
            np.add, (compute_rows_length(table[lc]) for lc in length_column)
        )
    else:
        if shuffle:
            length_col = random_state.randint(0, 2**23, len(table))
        else:
            length_col = np.zeros(len(table), dtype=np.int32)

    if batch_size is not None:
        if order_by_length:
            sorting_ind = np.argsort(length_col, kind="stable")
        else:
            sorting_ind = np.arange(len(length_col), dtype=np.int32)

        order_tt = pa.Table.from_arrays([pa.array(sorting_ind)], ["order"])
        batches = [ind["order"] for ind in order_tt.to_batches(batch_size)]
    elif max_tokens is not None:
        batches = compute_length_splits(
            length_col, max_tokens, order_by_length=order_by_length
        )
    else:
        raise ValueError("unknown batching method")

    if shuffle:
        batches = [batches[i] for i in random_state.permutation(len(batches))]

    def _getter(ind):
        try:
            tt = table.take(ind)
            return tt
        except Exception as e:
            logger.warn(f"Unexpected error : \n {str(e)} \n {table} \n {ind}")
            return None

    return (
        read_sequence(batches)
        .map(_getter, num_parallel_calls=num_parallel_calls)
        .filter(lambda tt: bool(tt is not None))
        .and_return(max_num_warnings=4)
    )


def filter_long_short_sentence_document(
    batch: pa.Table,
    column: str,
    max_sentence_len: Optional[int],
    min_sentence_len: Optional[int],
) -> pa.Table:
    assert max_sentence_len is not None or min_sentence_len is not None
    if min_sentence_len is None:
        min_sentence_len = 0

    if max_sentence_len is None:
        max_sentence_len = 2**32

    tt = pl.from_arrow(batch.select([column]), rechunk=False)
    assert isinstance(tt, pl.DataFrame)
    filter_ = tt.with_columns(
        (
            pl.col(column).list.eval(pl.col("").str.len_bytes()).list.max()
            <= max_sentence_len
        )
        & (
            pl.col(column).list.eval(pl.col("").str.len_bytes()).list.min()
            <= max_sentence_len
        )
    )[column].to_arrow()

    if pc.all(filter_).as_py():
        return batch
    return batch.filter(filter_)


def filter_document_by_quality(
    batch: pa.Table,
    column: str,
    min_score=Optional[float],
    max_score=Optional[float],
) -> pa.Table:
    if min_score is None and max_score is None:
        return batch

    if min_score is None:
        min_score = -float(np.inf)
    if max_score is None:
        max_score = float(np.inf)

    tt = pl.from_arrow(batch.select([column]), rechunk=False)
    assert isinstance(tt, pl.DataFrame)
    filter_ = tt.with_columns(
        (pl.col(column).list.max() <= max_score)
        & (pl.col(column).list.min() >= min_score)
    )[column].to_arrow()
    if pc.all(filter_).as_py():
        return batch
    return batch.filter(filter_)


def renaming(inp: NestedDict, mapper: dict, name: str) -> NestedDict:
    renamed_name = ColumnsNames.dataset_name.value
    if isinstance(inp, dict):
        out_dict = {mapper.get(key, key): value for key, value in inp.items()}
        out_dict[renamed_name] = name
        res = out_dict
    elif isinstance(inp, pd.DataFrame):
        out_pd = inp.rename(mapper=mapper, axis=1)
        out_pd[renamed_name] = name
        res = out_pd
    elif isinstance(inp, pa.Table):
        out_pa: pa.Table = inp.rename_columns(
            [mapper.get(key, key) for key in inp.column_names],
        )
        out_pa = out_pa.append_column(renamed_name, pa.array([name] * len(out_pa)))
        res = out_pa
    return res


def materialize_sequence(
    table: pa.Table,
    column_sequence: List[SonarTextColumn],
    vector_name: str,
    text_name: str,
) -> pa.Table:
    """
    Given `table`, it materializes `column_sequence`.
    Different elements from `column_sequence` are concatenated sequentially.
    Constant text elements will be sentencized and sonarized.
    It also accepts columns with single text and embeddings values instead of list.

    It returns a new table with two new columns with sequences of sentences and corresponding sequences of their embeddings.
    """

    table_len = len(table)
    sentences_seq = []
    vectors_seq = []

    target_dtype = None
    for col in column_sequence:
        if col.sonar_column is not None:
            target_dtype = table[col.sonar_column].type
            break

    for col in column_sequence:
        if col.text_value is not None:
            vectors, sentences = _get_embed_sentences(col.text_value)
            vectors_extended = pa.chunked_array(
                [pa.ListArray.from_arrays([0, len(vectors)], vectors)] * table_len
            )
            sentences_extended = pa.chunked_array(
                [pa.ListArray.from_arrays([0, len(sentences)], sentences)] * table_len
            )
        else:
            assert (col.text_column is not None) and (col.sonar_column is not None)
            vectors_extended = table[col.sonar_column]
            sentences_extended = table[col.text_column]
            if is_list_like(vectors_extended):
                assert is_list_like(sentences_extended)
            else:
                vectors_extended = simple_array_to_nested(vectors_extended)
                sentences_extended = simple_array_to_nested(sentences_extended)

        if target_dtype and vectors_extended.type != target_dtype:
            vectors_extended = vectors_extended.cast(target_dtype)

        vectors_seq.append(vectors_extended)
        sentences_seq.append(sentences_extended)

    new_vectors_array = hstack_pyarray_list(*vectors_seq)
    new_sentences_array = hstack_pyarray_list(*sentences_seq)
    del vectors_seq, sentences_seq
    table = table.append_column(vector_name, new_vectors_array)
    table = table.append_column(text_name, new_sentences_array)
    return table


@njit
def _get_hierarchical_indices_and_offsets(
    pagaraphs_lengths: List[np.ndarray], max_seq_len: int
):
    indices = []
    new_lens = [0]
    hierarchy_new_lens = [0]

    for i, current_lens in enumerate(pagaraphs_lengths):
        tmp_lens_sum = 0
        nb_blocks = 0
        for ll in current_lens:
            if ll + tmp_lens_sum > max_seq_len:
                indices.append(i)
                new_lens.append(new_lens[-1] + tmp_lens_sum)
                hierarchy_new_lens.append(hierarchy_new_lens[-1] + nb_blocks)

                tmp_lens_sum = ll
                nb_blocks = 0
            else:
                tmp_lens_sum += ll

            nb_blocks += 1

        if nb_blocks > 0:
            indices.append(i)
            new_lens.append(new_lens[-1] + tmp_lens_sum)
            hierarchy_new_lens.append(hierarchy_new_lens[-1] + nb_blocks)

    return (
        np.array(indices, dtype=np.int32),
        np.array(new_lens, dtype=np.int32),
        np.array(hierarchy_new_lens, dtype=np.int32),
    )


def hierarchical_explode_table_with_max_length(
    table: pa.Table,
    columns: Union[str, List[str]],
    max_seq_len: int,
    page_len_column: str,
    page_embs_columns: Optional[Union[str, List[str]]],
) -> pa.Table:
    if isinstance(columns, str):
        columns = [columns]

    if isinstance(page_embs_columns, str):
        page_embs_columns = [page_embs_columns]
    elif page_embs_columns is None:
        page_embs_columns = []

    assert len(columns) > 0

    cols = [pc.fill_null(table[columns[0]], [None])]
    lengths = pc.list_value_length(cols[0]).to_numpy()

    for name in columns[1:]:
        col = pc.fill_null(table[name], [None])
        # checking that all columns list structures are parallel
        assert (lengths == pc.list_value_length(col).to_numpy()).all()
        cols.append(col)

    pagaraphs_lengths = table[page_len_column].to_pandas().to_list()
    # assert [x.sum() for x pagaraphs_lengths] == lengths.tolist()
    # next unroll with max_seq_len
    indices, new_offests, hierarchy_offsets = _get_hierarchical_indices_and_offsets(
        pagaraphs_lengths, max_seq_len
    )

    other_columns = list(table.schema.names)
    for name in set(columns + [page_len_column] + page_embs_columns):
        other_columns.remove(name)

    remaining_table = table.select(other_columns).take(indices)

    result_dict = {}
    for name in other_columns:
        result_dict[name] = remaining_table[name]

    for name, col in zip(columns, cols):
        rolled_array = pa.ListArray.from_arrays(
            offsets=new_offests,
            values=pyarrow_column_to_array(pc.list_flatten(col)),
        )
        result_dict[name] = rolled_array

    for name in set([page_len_column] + page_embs_columns):
        col = table[name]
        rolled_array = pa.ListArray.from_arrays(
            offsets=hierarchy_offsets,
            values=pyarrow_column_to_array(pc.list_flatten(col)),
        )
        result_dict[name] = rolled_array

    return pa.Table.from_pydict(result_dict, schema=table.schema)


def filter_table_with_different_lengths(
    table: pa.Table, columns: List[str]
) -> pa.Table:
    if len(columns) <= 1 or not all(is_list_like(table[col]) for col in columns):
        return table

    ref_lengths = pc.list_value_length(table[columns[0]])
    for col in columns[1:]:
        same_lens = pc.equal(pc.list_value_length(table[col]), ref_lengths)
        if pc.all(same_lens).as_py():
            continue
        else:
            logger.warn(
                f"filtering table whose nb sentences and nb sonar vectors are aligned, keeping {pc.sum(same_lens).as_py()} rows out of{len(table)}"
            )
            table = table.filter(same_lens)
    return table


@dataclass
class PFSState:
    nb_fully_read_files: int = 0
    nb_current_file_read_fragements: int = 0
    total_nb_fragments: int = 0
    total_nb_rows: int = 0


class ParquetFragmentStreamer:
    def __init__(
        self,
        parquet_ds: pq.ParquetDataset,
        split_to_row_groups: bool = True,
        limit_options: Optional[ParquetDatasetLimitOptions] = None,
        read_state: Optional[PFSState] = None,
    ):
        self.split_to_row_groups = split_to_row_groups
        self.limit_options = limit_options or ParquetDatasetLimitOptions()
        self.parquet_ds = parquet_ds

        if read_state is not None:
            self.state = read_state
        else:
            self.reset_state()

    def reset_state(self):
        self.state = PFSState()

    def __reduce__(self):
        return (
            self.__class__,
            (
                self.parquet_ds,
                self.split_to_row_groups,
                self.limit_options,
                self.state,
            ),
        )

    def truncate_files(
        self,
        parquet_ds: pq.ParquetDataset,
        fraction_of_files: Optional[float],
        nb_files: Optional[int],
    ) -> List[pa.dataset.Fragment]:
        file_ds_fragments = get_dataset_fragments(
            parquet_ds, parquet_ds._filter_expression
        )
        self.proxy_ds_path = "/".join(parquet_ds.files[0].split("=")[0].split("/")[:-1])
        logger.info(
            f"{self.proxy_ds_path} : full number of files {len(file_ds_fragments)}"
        )

        if fraction_of_files is not None:
            file_ds_fragments = file_ds_fragments[
                : max(
                    int(round(fraction_of_files * len(file_ds_fragments))),
                    1,
                )
            ]
            logger.info(
                f"{self.proxy_ds_path} : reducing number of files to {len(file_ds_fragments)} because of fraction_of_files={fraction_of_files}"
            )
        if nb_files is not None and nb_files < len(file_ds_fragments):
            file_ds_fragments = file_ds_fragments[:nb_files]
            logger.info(
                f"{self.proxy_ds_path} : reducing number of files to {len(file_ds_fragments)} because of nb_files={nb_files}"
            )
        return file_ds_fragments

    def __iter__(self):
        limit_options = self.limit_options

        file_ds_fragments = self.truncate_files(
            self.parquet_ds,
            limit_options.fraction_of_files,
            limit_options.nb_files,
        )

        if not self.split_to_row_groups:
            for frag in file_ds_fragments[
                self.state.nb_fully_read_files : limit_options.nb_fragments
            ]:
                self.state.nb_fully_read_files += 1
                yield frag

                if limit_options.nb_rows is not None:
                    self.state.total_nb_rows += frag.count_rows()
                    if self.state.total_nb_rows >= limit_options.nb_rows:
                        break
        else:
            early_stop = False
            logger.info(f"{self.proxy_ds_path} : starting split in row groups")

            for new_file in file_ds_fragments[self.state.nb_fully_read_files :]:
                new_file_fragments = split_fragment_in_row_groups(new_file)
                new_file_fragments = new_file_fragments[
                    self.state.nb_current_file_read_fragements :
                ]
                if limit_options.nb_rows is not None:
                    new_file_fragments_stats = [
                        frag.row_groups[0].num_rows for frag in new_file_fragments
                    ]
                else:
                    new_file_fragments_stats = [0] * len(new_file_fragments)

                for nb_row, frag in zip(new_file_fragments_stats, new_file_fragments):
                    self.state.total_nb_rows += nb_row
                    self.state.total_nb_fragments += 1
                    self.state.nb_current_file_read_fragements += (
                        1  # increate before yield
                    )
                    yield frag

                    if (
                        limit_options.nb_fragments is not None
                        and self.state.total_nb_fragments >= limit_options.nb_fragments
                    ):
                        early_stop = True
                        if limit_options.nb_rows is not None:
                            logger.info(
                                f"{self.proxy_ds_path} : nb_fragments limit {limit_options.nb_fragments} was reached with around {self.state.total_nb_rows} rows"
                            )
                        else:
                            logger.info(
                                f"{self.proxy_ds_path} : nb_fragments limit {limit_options.nb_fragments} was reached"
                            )
                        break
                    if (
                        limit_options.nb_rows is not None
                        and self.state.total_nb_rows >= limit_options.nb_rows
                    ):
                        early_stop = True
                        logger.info(
                            f"{self.proxy_ds_path} : nb_rows limit {limit_options.nb_rows} was reached with around {self.state.total_nb_fragments} fragments"
                        )
                        break
                if early_stop:
                    break
                # only when full file is read we increament this
                self.state.nb_fully_read_files += 1
                self.state.nb_current_file_read_fragements = 0


@dataclass
class ShuffledIteratorState:
    epoch_count: int
    current_window: List[Any]
    index: int
    random_state: np.random.RandomState


class ShuffledIterator(Iterator[Any]):
    def __init__(
        self,
        iterator,
        window_size: int,
        nb_epoch: int,
        seed: Optional[int],
        state: Optional[ShuffledIteratorState] = None,
    ):
        self.base_iterator = iterator
        self.window_size = window_size
        self.seed = seed
        self.nb_epoch = nb_epoch

        if state is None:
            state = ShuffledIteratorState(
                random_state=np.random.RandomState(self.seed),
                epoch_count=0,
                current_window=[],
                index=0,
            )
        self.state = state
        self.window_iterator = None

    def reset_state(self):
        self.state.random_state = np.random.RandomState(self.seed)
        self.state.epoch_count = 0
        self._reset_inner()

    def __reduce__(self):
        return (
            self.__class__,
            (
                self.base_iterator,
                self.window_size,
                self.nb_epoch,
                self.seed,
                self.state,
            ),
        )

    def _reset_inner(self):
        self.base_iterator.reset_state()
        self.state.index = 0
        self.state.current_window = []
        self.window_iterator = None

    def __iter__(self):
        return self

    def __next__(self) -> Any:
        if self.state.epoch_count >= self.nb_epoch:
            raise StopIteration

        # If current window is exhausted, fetch the next window
        if self.window_iterator is None:
            self.window_iterator = batched(self.base_iterator, self.window_size)  # type: ignore
        assert self.window_iterator is not None

        if self.state.index >= len(self.state.current_window):
            try:
                # Get the next window batch
                window = next(self.window_iterator)
                window = np.array(window, dtype="O")
                self.state.random_state.shuffle(window)
                self.state.current_window = window
                self.state.index = 0
            except StopIteration:
                # If no more batches, increment epoch count and reset iterator
                self.state.epoch_count += 1
                self._reset_inner()
                return self.__next__()

        # Return the next element from the current window
        result = self.state.current_window[self.state.index]
        self.state.index += 1
        return result


def stream_parquet_fragments(
    parquet_ds: pq.ParquetDataset,
    nb_epochs: int,
    split_to_row_groups: bool = True,
    shuffle: bool = True,
    seed: Optional[int] = None,
    limit_options: Optional[ParquetDatasetLimitOptions] = None,
    shuffling_window: int = 200,
) -> DataPipelineBuilder:
    fragments_iterator = ParquetFragmentStreamer(
        parquet_ds=parquet_ds,
        split_to_row_groups=split_to_row_groups,
        limit_options=limit_options,
    )

    def reset_fn(iterator):
        iterator.reset_state()
        return iterator

    pipeline = read_iterator(
        ShuffledIterator(
            fragments_iterator,
            window_size=shuffling_window if shuffle else 1,
            nb_epoch=nb_epochs,
            seed=seed,
        ),
        reset_fn,
        infinite=False,
    )

    return pipeline.map(SafeFragment)


def get_row_group_level_metadata(
    dataset: pq.ParquetDataset,
    columns: Optional[List[str]] = None,
    nb_jobs: int = 40,
    max_fragments: int = -1,
    seed: int = 123,
) -> pd.DataFrame:
    """
    Parses row group level metadata from a Parquet dataset and returns it as a pandas DataFrame.
    It's similar to `get_parquet_dataset_metadata`
    but present a unnested view on row groups statistics for only a subset of columns.
    This function can be used for any kind of downstream analysis.

    It uses joblib for parallel processing
    and tqdm for progress tracking, which are good practices for handling large datasets.

    Parameters:
    - dataset (pq.ParquetDataset): The Parquet dataset to parse.
    - columns (list of str, optional): The columns to include in the output DataFrame. If not specified, all columns are included.
                For `columns=[]` no column-vise information will be profided (which is generally much faster).
    - nb_jobs (int, default=40): The number of parallel jobs to run.
    - max_fragments (int, default=-1): The maximum number of fragments to include. If -1, all fragments are included.
    - seed (int, default=123): The seed for the random number generator, used when selecting fragments.

    Returns:
    - pd.DataFrame: A DataFrame containing the row group level metadata.
    Example:
        >>> import pyarrow as pa
        >>> import pyarrow.fs
        >>> import pyarrow.compute as pc
        >>> fs, parquet_uri = pa.fs.FileSystem.from_uri("s3://<bucket_name>/<dataset_name>/")
        >>> dataset = pq.ParquetDataset(parquet_uri, filesystem=fs, filters=pc.equal(pc.field("split"), "validation"))
        >>> df_stats = get_row_group_level_metadata(dataset, columns=["col1", "col2", ...])
    """
    assert max_fragments >= -1
    fragments = list(dataset._dataset.get_fragments(filter=dataset._filter_expression))

    if max_fragments != -1 and max_fragments < len(fragments):
        fragments = (
            np.random.RandomState(seed)
            .choice(np.array(fragments, dtype="O"), max_fragments, replace=False)
            .tolist()
        )

    physical_schema = fragments[0].physical_schema

    columns = columns if columns is not None else physical_schema.names
    # taking only existing columns
    non_existing_columns = tuple(set(columns) - set(physical_schema.names))
    if non_existing_columns:
        print(
            "Following colums are not present in physical schema and will be ignored",
            non_existing_columns,
        )
    columns = [col for col in columns if col in physical_schema.names]

    columns_index = [physical_schema.get_field_index(col) for col in columns]

    columns_to_exclude = set(["row_group_id", "num_rows", "total_byte_size"]) & set(
        columns
    )
    assert len(columns_to_exclude) == 0, (
        f"names conflict, rename/remove : {columns_to_exclude}"
    )

    def get_one_row_group_stats(row_group):
        metadata = row_group.metadata
        info = {
            "row_group_id": row_group.id,
            "num_rows": metadata.num_rows,
            "total_byte_size": metadata.total_byte_size,
        }
        for col, ind in zip(columns, columns_index):
            info[col] = metadata.column(ind).to_dict()
        return info

    def get_fragment_stats(frag):
        return {
            "rg_stats": list(map(get_one_row_group_stats, frag.row_groups)),
            "parquet_file_path": frag.path,
            **get_partition_keys(frag.partition_expression),
        }

    stats = Parallel(nb_jobs, backend="threading")(
        delayed(get_fragment_stats)(frag) for frag in tqdm(fragments)
    )

    stats = pd.DataFrame(stats).explode("rg_stats")
    flatten_row_df = pd.DataFrame(stats.pop("rg_stats").tolist(), index=stats.index)
    result_df = pd.concat([stats, flatten_row_df], axis=1)
    return result_df