Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-stream/src/nodes/io_sinks2/components/hstack_columns.rs
7884 views
1
use std::sync::Arc;
2
3
use polars_core::prelude::Column;
4
use polars_core::schema::Schema;
5
use polars_utils::marked_usize::MarkedUsize;
6
7
/// Applies a `with_columns()` operation with pre-computed indices.
8
#[derive(Clone)]
9
pub struct HStackColumns {
10
gather_indices: Arc<[MarkedUsize]>,
11
}
12
13
impl HStackColumns {
14
/// Note:
15
/// * Dtypes of the schemas are unused.
16
pub fn new(output_schema: &Schema, schema_left: &Schema, schema_right: &Schema) -> Self {
17
assert!(schema_left.len() <= MarkedUsize::UNMARKED_MAX);
18
assert!(schema_right.len() <= MarkedUsize::UNMARKED_MAX);
19
20
let gather_indices: Arc<[MarkedUsize]> = output_schema
21
.iter_names()
22
.map(|name| {
23
if let Some((idx, ..)) = schema_right.get_full(name) {
24
MarkedUsize::new(idx, true)
25
} else {
26
MarkedUsize::new(schema_left.get_full(name).unwrap().0, false)
27
}
28
})
29
.collect();
30
31
Self { gather_indices }
32
}
33
34
#[expect(unused)]
35
pub fn output_width(&self) -> usize {
36
self.gather_indices.len()
37
}
38
39
/// Broadcasts unit-length columns from the RHS.
40
pub fn hstack_columns_broadcast(
41
&self,
42
height: usize,
43
cols_left: &[Column],
44
cols_right: &[Column],
45
) -> Vec<Column> {
46
self.gather_indices
47
.iter()
48
.copied()
49
.map(|mi| {
50
let i = mi.to_usize();
51
52
if mi.marked() {
53
let c = &cols_right[i];
54
55
if c.len() != height {
56
assert_eq!(c.len(), 1);
57
c.new_from_index(0, height)
58
} else {
59
c.clone()
60
}
61
} else {
62
cols_left[i].clone()
63
}
64
})
65
.collect()
66
}
67
}
68
69