polars_core/chunked_array/ops/
row_encode.rs

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
use arrow::compute::utils::combine_validities_and_many;
use polars_row::{
    convert_columns, RowEncodingCategoricalContext, RowEncodingContext, RowEncodingOptions,
    RowsEncoded,
};
use polars_utils::itertools::Itertools;
use rayon::prelude::*;

use crate::prelude::*;
use crate::utils::_split_offsets;
use crate::POOL;

pub fn encode_rows_vertical_par_unordered(by: &[Column]) -> PolarsResult<BinaryOffsetChunked> {
    let n_threads = POOL.current_num_threads();
    let len = by[0].len();
    let splits = _split_offsets(len, n_threads);

    let chunks = splits.into_par_iter().map(|(offset, len)| {
        let sliced = by
            .iter()
            .map(|s| s.slice(offset as i64, len))
            .collect::<Vec<_>>();
        let rows = _get_rows_encoded_unordered(&sliced)?;
        Ok(rows.into_array())
    });
    let chunks = POOL.install(|| chunks.collect::<PolarsResult<Vec<_>>>());

    Ok(BinaryOffsetChunked::from_chunk_iter(
        PlSmallStr::EMPTY,
        chunks?,
    ))
}

// Almost the same but broadcast nulls to the row-encoded array.
pub fn encode_rows_vertical_par_unordered_broadcast_nulls(
    by: &[Column],
) -> PolarsResult<BinaryOffsetChunked> {
    let n_threads = POOL.current_num_threads();
    let len = by[0].len();
    let splits = _split_offsets(len, n_threads);

    let chunks = splits.into_par_iter().map(|(offset, len)| {
        let sliced = by
            .iter()
            .map(|s| s.slice(offset as i64, len))
            .collect::<Vec<_>>();
        let rows = _get_rows_encoded_unordered(&sliced)?;

        let validities = sliced
            .iter()
            .flat_map(|s| {
                let s = s.rechunk();
                #[allow(clippy::unnecessary_to_owned)]
                s.as_materialized_series()
                    .chunks()
                    .to_vec()
                    .into_iter()
                    .map(|arr| arr.validity().cloned())
            })
            .collect::<Vec<_>>();

        let validity = combine_validities_and_many(&validities);
        Ok(rows.into_array().with_validity_typed(validity))
    });
    let chunks = POOL.install(|| chunks.collect::<PolarsResult<Vec<_>>>());

    Ok(BinaryOffsetChunked::from_chunk_iter(
        PlSmallStr::EMPTY,
        chunks?,
    ))
}

pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContext> {
    match dtype {
        DataType::Boolean
        | DataType::UInt8
        | DataType::UInt16
        | DataType::UInt32
        | DataType::UInt64
        | DataType::Int8
        | DataType::Int16
        | DataType::Int32
        | DataType::Int64
        | DataType::Int128
        | DataType::Float32
        | DataType::Float64
        | DataType::String
        | DataType::Binary
        | DataType::BinaryOffset
        | DataType::Null
        | DataType::Time
        | DataType::Date
        | DataType::Datetime(_, _)
        | DataType::Duration(_) => None,

        DataType::Unknown(_) => panic!("Unsupported in row encoding"),

        #[cfg(feature = "object")]
        DataType::Object(_, _) => panic!("Unsupported in row encoding"),

        #[cfg(feature = "dtype-decimal")]
        DataType::Decimal(precision, _) => {
            Some(RowEncodingContext::Decimal(precision.unwrap_or(38)))
        },

        #[cfg(feature = "dtype-array")]
        DataType::Array(dtype, _) => get_row_encoding_dictionary(dtype),
        DataType::List(dtype) => get_row_encoding_dictionary(dtype),
        #[cfg(feature = "dtype-categorical")]
        DataType::Categorical(revmap, ordering) | DataType::Enum(revmap, ordering) => {
            let revmap = revmap.as_ref().unwrap();

            let (num_known_categories, lexical_sort_idxs) = match revmap.as_ref() {
                RevMapping::Global(map, _, _) => {
                    let num_known_categories = map.keys().max().copied().map_or(0, |m| m + 1);

                    // @TODO: This should probably be cached.
                    let lexical_sort_idxs =
                        matches!(ordering, CategoricalOrdering::Lexical).then(|| {
                            let read_map = crate::STRING_CACHE.read_map();
                            let payloads = read_map.get_current_payloads();
                            assert!(payloads.len() >= num_known_categories as usize);

                            let mut idxs = (0..num_known_categories).collect::<Vec<u32>>();
                            idxs.sort_by_key(|&k| payloads[k as usize].as_str());
                            let mut sort_idxs = vec![0; num_known_categories as usize];
                            for (i, idx) in idxs.into_iter().enumerate_u32() {
                                sort_idxs[idx as usize] = i;
                            }
                            sort_idxs
                        });

                    (num_known_categories, lexical_sort_idxs)
                },
                RevMapping::Local(values, _) => {
                    // @TODO: This should probably be cached.
                    let lexical_sort_idxs =
                        matches!(ordering, CategoricalOrdering::Lexical).then(|| {
                            assert_eq!(values.null_count(), 0);
                            let values: Vec<&str> = values.values_iter().collect();

                            let mut idxs = (0..values.len() as u32).collect::<Vec<u32>>();
                            idxs.sort_by_key(|&k| values[k as usize]);
                            let mut sort_idxs = vec![0; values.len()];
                            for (i, idx) in idxs.into_iter().enumerate_u32() {
                                sort_idxs[idx as usize] = i;
                            }
                            sort_idxs
                        });

                    (values.len() as u32, lexical_sort_idxs)
                },
            };

            let ctx = RowEncodingCategoricalContext {
                num_known_categories,
                is_enum: matches!(dtype, DataType::Enum(_, _)),
                lexical_sort_idxs,
            };
            Some(RowEncodingContext::Categorical(ctx))
        },
        #[cfg(feature = "dtype-struct")]
        DataType::Struct(fs) => {
            let mut out = Vec::new();

            for (i, f) in fs.iter().enumerate() {
                if let Some(dict) = get_row_encoding_dictionary(f.dtype()) {
                    out.reserve(fs.len());
                    out.extend(std::iter::repeat_n(None, i));
                    out.push(Some(dict));
                    break;
                }
            }

            if out.is_empty() {
                return None;
            }

            out.extend(
                fs[out.len()..]
                    .iter()
                    .map(|f| get_row_encoding_dictionary(f.dtype())),
            );

            Some(RowEncodingContext::Struct(out))
        },
    }
}

pub fn encode_rows_unordered(by: &[Column]) -> PolarsResult<BinaryOffsetChunked> {
    let rows = _get_rows_encoded_unordered(by)?;
    Ok(BinaryOffsetChunked::with_chunk(
        PlSmallStr::EMPTY,
        rows.into_array(),
    ))
}

pub fn _get_rows_encoded_unordered(by: &[Column]) -> PolarsResult<RowsEncoded> {
    let mut cols = Vec::with_capacity(by.len());
    let mut opts = Vec::with_capacity(by.len());
    let mut dicts = Vec::with_capacity(by.len());

    // Since ZFS exists, we might not actually have any arrays and need to get the length from the
    // columns.
    let num_rows = by.first().map_or(0, |c| c.len());

    for by in by {
        debug_assert_eq!(by.len(), num_rows);

        let by = by.as_materialized_series();
        let arr = by.to_physical_repr().rechunk().chunks()[0].to_boxed();
        let opt = RowEncodingOptions::new_unsorted();
        let dict = get_row_encoding_dictionary(by.dtype());

        cols.push(arr);
        opts.push(opt);
        dicts.push(dict);
    }
    Ok(convert_columns(num_rows, &cols, &opts, &dicts))
}

pub fn _get_rows_encoded(
    by: &[Column],
    descending: &[bool],
    nulls_last: &[bool],
) -> PolarsResult<RowsEncoded> {
    debug_assert_eq!(by.len(), descending.len());
    debug_assert_eq!(by.len(), nulls_last.len());

    let mut cols = Vec::with_capacity(by.len());
    let mut opts = Vec::with_capacity(by.len());
    let mut dicts = Vec::with_capacity(by.len());

    // Since ZFS exists, we might not actually have any arrays and need to get the length from the
    // columns.
    let num_rows = by.first().map_or(0, |c| c.len());

    for ((by, desc), null_last) in by.iter().zip(descending).zip(nulls_last) {
        debug_assert_eq!(by.len(), num_rows);

        let by = by.as_materialized_series();
        let arr = by.to_physical_repr().rechunk().chunks()[0].to_boxed();
        let opt = RowEncodingOptions::new_sorted(*desc, *null_last);
        let dict = get_row_encoding_dictionary(by.dtype());

        cols.push(arr);
        opts.push(opt);
        dicts.push(dict);
    }
    Ok(convert_columns(num_rows, &cols, &opts, &dicts))
}

pub fn _get_rows_encoded_ca(
    name: PlSmallStr,
    by: &[Column],
    descending: &[bool],
    nulls_last: &[bool],
) -> PolarsResult<BinaryOffsetChunked> {
    _get_rows_encoded(by, descending, nulls_last)
        .map(|rows| BinaryOffsetChunked::with_chunk(name, rows.into_array()))
}

pub fn _get_rows_encoded_arr(
    by: &[Column],
    descending: &[bool],
    nulls_last: &[bool],
) -> PolarsResult<BinaryArray<i64>> {
    _get_rows_encoded(by, descending, nulls_last).map(|rows| rows.into_array())
}

pub fn _get_rows_encoded_ca_unordered(
    name: PlSmallStr,
    by: &[Column],
) -> PolarsResult<BinaryOffsetChunked> {
    _get_rows_encoded_unordered(by)
        .map(|rows| BinaryOffsetChunked::with_chunk(name, rows.into_array()))
}