1use std::collections::{HashMap, HashSet};
4use std::sync::Arc;
5
6use crate::data::source::{collect_source, copy_source_string, reserve_source};
7use crate::data::{binding::VectorSource, DataSourceInfo, RawKey};
8use crate::Error;
9
10#[derive(Debug, Clone)]
12pub struct SourceRows {
13 rows: Arc<[usize]>,
14 source: VectorSource,
15}
16
17impl SourceRows {
18 pub fn new(rows: impl IntoIterator<Item = usize>) -> Result<Self, Error> {
20 let rows = collect_source(rows.into_iter().map(Ok))?;
21 let mut seen = HashSet::new();
22 crate::operation::capacity::<usize>(seen.capacity(), seen.len(), rows.len())?;
23 seen.try_reserve(rows.len())
24 .map_err(|error| Error::Data(format!("source-row identity storage: {error}").into()))?;
25 for &row in &rows {
26 crate::operation::work(1)?;
27 if !seen.insert(row) {
28 return Err(Error::Data(
29 format!("source-row identities must be unique; found duplicate {row}").into(),
30 ));
31 }
32 }
33 crate::operation::memory(rows.len() as u128 * std::mem::size_of::<usize>() as u128)?;
34 Ok(Self {
35 source: VectorSource::direct(rows.len()),
36 rows: rows.into(),
37 })
38 }
39
40 #[doc(hidden)]
42 pub fn with_source(mut self, source: DataSourceInfo) -> crate::Result<Self> {
43 self.source = VectorSource::repeated(source, 1, self.len())?;
44 Ok(self)
45 }
46
47 #[doc(hidden)]
49 pub fn get_source(&self) -> &DataSourceInfo {
50 self.source.info()
51 }
52
53 #[must_use]
55 pub fn len(&self) -> usize {
56 self.rows.len()
57 }
58
59 #[must_use]
61 pub fn is_empty(&self) -> bool {
62 self.rows.is_empty()
63 }
64
65 #[must_use]
67 pub fn as_slice(&self) -> &[usize] {
68 &self.rows
69 }
70
71 #[must_use]
73 pub fn get(&self, index: usize) -> Option<usize> {
74 self.rows.get(index).copied()
75 }
76
77 pub fn iter(&self) -> impl ExactSizeIterator<Item = usize> + '_ {
79 self.rows.iter().copied()
80 }
81}
82
83impl PartialEq for SourceRows {
84 fn eq(&self, other: &Self) -> bool {
85 self.rows == other.rows
86 }
87}
88
89impl Eq for SourceRows {}
90
91#[derive(Debug, Clone, PartialEq, Eq)]
92enum Validity {
93 All,
94 Bitmap(Arc<[u64]>),
95}
96
97impl Validity {
98 fn from_flags(flags: &[bool]) -> Self {
99 if flags.iter().all(|valid| *valid) {
100 return Self::All;
101 }
102 Self::bitmap(flags, vec![0_u64; flags.len().div_ceil(64)])
103 }
104
105 fn try_from_flags(flags: &[bool]) -> crate::Result<Self> {
106 crate::operation::work(flags.len())?;
107 if flags.iter().filter(|&&valid| valid).count() == flags.len() {
108 return Ok(Self::All);
109 }
110 let count = flags.len().div_ceil(64);
111 let mut words = Vec::new();
112 reserve_source(&mut words, count)?;
113 words.resize(count, 0);
114 crate::operation::work(flags.len())?;
115 crate::operation::memory(count as u128 * std::mem::size_of::<u64>() as u128)?;
116 Ok(Self::bitmap(flags, words))
117 }
118
119 fn bitmap(flags: &[bool], mut words: Vec<u64>) -> Self {
120 for (index, valid) in flags.iter().copied().enumerate() {
121 if valid {
122 words[index / 64] |= 1_u64 << (index % 64);
123 }
124 }
125 Self::Bitmap(words.into())
126 }
127
128 fn is_valid(&self, index: usize) -> bool {
129 match self {
130 Self::All => true,
131 Self::Bitmap(words) => words
132 .get(index / 64)
133 .is_some_and(|word| word & (1_u64 << (index % 64)) != 0),
134 }
135 }
136}
137
138#[derive(Debug, Clone)]
140pub struct NumericValues {
141 name: Arc<str>,
142 values: Arc<[f64]>,
143 validity: Validity,
144 field_key: Option<Arc<(RawKey, Arc<str>)>>,
145 source: VectorSource,
146}
147
148impl NumericValues {
149 #[must_use]
151 pub fn new(name: impl Into<String>, values: impl IntoIterator<Item = f64>) -> Self {
152 let values = values.into_iter().collect::<Vec<_>>();
153 Self {
154 name: Arc::from(name.into()),
155 source: VectorSource::direct(values.len()),
156 values: values.into(),
157 validity: Validity::All,
158 field_key: None,
159 }
160 }
161
162 #[must_use]
164 pub fn nullable(
165 name: impl Into<String>,
166 values: impl IntoIterator<Item = Option<f64>>,
167 ) -> Self {
168 let values = values.into_iter().collect::<Vec<_>>();
169 let validity =
170 Validity::from_flags(&values.iter().map(Option::is_some).collect::<Vec<_>>());
171 let values = values
172 .into_iter()
173 .map(|value| value.unwrap_or(0.0))
174 .collect::<Vec<_>>();
175 Self {
176 name: Arc::from(name.into()),
177 source: VectorSource::direct_nullable(
178 values.len(),
179 !matches!(&validity, Validity::All),
180 ),
181 values: values.into(),
182 validity,
183 field_key: None,
184 }
185 }
186
187 #[must_use]
189 pub fn name(&self) -> &str {
190 self.field_key
191 .as_deref()
192 .map_or(&self.name, |(_, name)| name)
193 }
194
195 #[doc(hidden)]
197 pub fn raw_name(&self) -> &str {
198 &self.name
199 }
200
201 pub(crate) fn raw_field(&self) -> crate::data::category::RawField {
202 let key = self
203 .field_key()
204 .cloned()
205 .unwrap_or_else(|| RawKey::from(self.raw_name()));
206 crate::data::category::RawField::from_key(Arc::clone(&self.name), key)
207 }
208
209 #[doc(hidden)]
211 #[must_use]
212 pub fn with_field_key(mut self, key: RawKey, name: impl Into<String>) -> Self {
213 self.field_key = Some(Arc::new((key, Arc::from(name.into()))));
214 self
215 }
216
217 #[doc(hidden)]
219 pub fn field_key(&self) -> Option<&RawKey> {
220 self.field_key.as_deref().map(|(key, _)| key)
221 }
222
223 #[doc(hidden)]
225 pub fn with_source(self, source: DataSourceInfo) -> crate::Result<Self> {
226 self.with_repeated_source(source, 1)
227 }
228
229 #[doc(hidden)]
231 pub fn with_repeated_source(
232 mut self,
233 source: DataSourceInfo,
234 copies: usize,
235 ) -> crate::Result<Self> {
236 self.source = VectorSource::repeated(source, copies, self.len())?.with_original_validity(
237 self.len(),
238 self.has_nulls(),
239 |slot| self.validity.is_valid(slot),
240 )?;
241 Ok(self)
242 }
243
244 #[doc(hidden)]
246 pub fn with_source_slots(
247 mut self,
248 source: DataSourceInfo,
249 slots: Vec<usize>,
250 ) -> crate::Result<Self> {
251 self.source = VectorSource::gathered(source, slots, self.len())?.with_original_validity(
252 self.len(),
253 self.has_nulls(),
254 |slot| self.validity.is_valid(slot),
255 )?;
256 Ok(self)
257 }
258
259 #[doc(hidden)]
261 pub fn get_source(&self) -> &DataSourceInfo {
262 self.source.info()
263 }
264
265 pub(crate) fn vector_source(&self) -> &VectorSource {
266 &self.source
267 }
268
269 pub(crate) fn has_nulls(&self) -> bool {
270 !matches!(self.validity, Validity::All)
271 }
272
273 #[doc(hidden)]
275 pub fn source_slot(&self, index: usize) -> Option<usize> {
276 (index < self.len()).then(|| self.source.slot(index))
277 }
278
279 pub(crate) fn select(&self, indices: &[usize]) -> crate::Result<Self> {
280 let values = collect_source(indices.iter().map(|&index| {
281 self.values.get(index).copied().ok_or_else(|| {
282 Error::Data(format!("numeric selection index {index} is out of bounds").into())
283 })
284 }))?;
285 let validity = if matches!(self.validity, Validity::All) {
286 Validity::All
287 } else {
288 let flags = collect_source(
289 indices
290 .iter()
291 .map(|&index| Ok(self.validity.is_valid(index))),
292 )?;
293 Validity::try_from_flags(&flags)?
294 };
295 crate::operation::memory(values.len() as u128 * std::mem::size_of::<f64>() as u128)?;
296 Ok(Self {
297 name: self.name.clone(),
298 values: values.into(),
299 validity,
300 field_key: self.field_key.clone(),
301 source: self.source.select(indices)?,
302 })
303 }
304
305 #[must_use]
307 pub fn len(&self) -> usize {
308 self.values.len()
309 }
310
311 #[must_use]
313 pub fn is_empty(&self) -> bool {
314 self.values.is_empty()
315 }
316
317 #[must_use]
320 pub fn get(&self, row: usize) -> Option<Option<f64>> {
321 self.values
322 .get(row)
323 .map(|value| self.validity.is_valid(row).then_some(*value))
324 }
325
326 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<f64>> + '_ {
328 (0..self.len()).map(|row| self.get(row).expect("in-bounds source slot"))
329 }
330
331 #[must_use]
333 pub fn is_valid(&self, row: usize) -> Option<bool> {
334 (row < self.len()).then(|| self.validity.is_valid(row))
335 }
336}
337
338impl PartialEq for NumericValues {
339 fn eq(&self, other: &Self) -> bool {
340 self.name == other.name
341 && self.field_key == other.field_key
342 && self.len() == other.len()
343 && self
344 .iter()
345 .map(|value| value.map(f64::to_bits))
346 .eq(other.iter().map(|value| value.map(f64::to_bits)))
347 }
348}
349
350impl Eq for NumericValues {}
351
352#[derive(Debug, Clone)]
354pub struct CategoryValues {
355 name: Arc<str>,
356 levels: Arc<[String]>,
357 codes: Arc<[u32]>,
358 validity: Validity,
359 original_keys: Option<Arc<crate::data::KeyValues>>,
360 field_key: Option<Arc<(RawKey, Arc<str>)>>,
361 source: VectorSource,
362}
363
364impl CategoryValues {
365 pub fn new<S>(
367 name: impl Into<String>,
368 values: impl IntoIterator<Item = S>,
369 ) -> Result<Self, Error>
370 where
371 S: Into<String>,
372 {
373 Self::from_optional(
374 name.into(),
375 values.into_iter().map(|value| Ok(Some(value.into()))),
376 )
377 }
378
379 pub fn nullable<S>(
381 name: impl Into<String>,
382 values: impl IntoIterator<Item = Option<S>>,
383 ) -> Result<Self, Error>
384 where
385 S: Into<String>,
386 {
387 Self::from_optional(
388 name.into(),
389 values.into_iter().map(|value| Ok(value.map(Into::into))),
390 )
391 }
392
393 fn from_optional(
394 name: String,
395 values: impl IntoIterator<Item = Result<Option<String>, Error>>,
396 ) -> Result<Self, Error> {
397 let values = collect_source(values.into_iter())?;
398 let mut levels = Vec::new();
399 let mut indices = HashMap::new();
400 let mut codes = Vec::new();
401 let mut valid = Vec::new();
402 reserve_source(&mut codes, values.len())?;
403 reserve_source(&mut valid, values.len())?;
404 for value in values {
405 crate::operation::work(1)?;
406 let Some(value) = value else {
407 valid.push(false);
408 codes.push(0);
409 continue;
410 };
411 valid.push(true);
412 let code = if let Some(code) = indices.get(&value).copied() {
413 code
414 } else {
415 let code = u32::try_from(levels.len()).map_err(|_| {
416 Error::Data(
417 format!(
418 "categorical values {name:?} exceed the supported distinct-level count"
419 )
420 .into(),
421 )
422 })?;
423 reserve_source(&mut levels, 1)?;
424 crate::operation::capacity::<(String, u32)>(indices.capacity(), indices.len(), 1)?;
425 indices.try_reserve(1).map_err(|error| {
426 Error::Data(format!("category dictionary storage: {error}").into())
427 })?;
428 indices.insert(copy_source_string(&value)?, code);
429 levels.push(value);
430 code
431 };
432 codes.push(code);
433 }
434 let validity = Validity::try_from_flags(&valid)?;
435 crate::operation::memory(
436 codes.len() as u128 * std::mem::size_of::<u32>() as u128
437 + levels.len() as u128 * std::mem::size_of::<String>() as u128
438 + name.len() as u128,
439 )?;
440 Ok(Self {
441 name: Arc::from(name),
442 levels: levels.into(),
443 source: VectorSource::direct_nullable(codes.len(), !matches!(validity, Validity::All)),
444 codes: codes.into(),
445 validity,
446 original_keys: None,
447 field_key: None,
448 })
449 }
450
451 #[must_use]
453 pub fn name(&self) -> &str {
454 &self.name
455 }
456
457 #[must_use]
459 pub fn len(&self) -> usize {
460 self.codes.len()
461 }
462
463 #[must_use]
465 pub fn is_empty(&self) -> bool {
466 self.codes.is_empty()
467 }
468
469 #[must_use]
472 pub fn get(&self, row: usize) -> Option<Option<&str>> {
473 self.codes.get(row).map(|code| {
474 if !self.validity.is_valid(row) {
475 return None;
476 }
477 usize::try_from(*code)
478 .ok()
479 .and_then(|index| self.levels.get(index))
480 .map(String::as_str)
481 })
482 }
483
484 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<&str>> + '_ {
486 (0..self.len()).map(|row| self.get(row).expect("in-bounds source slot"))
487 }
488
489 #[must_use]
491 pub fn is_valid(&self, row: usize) -> Option<bool> {
492 (row < self.len()).then(|| self.validity.is_valid(row))
493 }
494
495 #[must_use]
497 pub fn levels(&self) -> &[String] {
498 &self.levels
499 }
500
501 #[doc(hidden)]
505 pub fn with_key_values(mut self, keys: crate::data::KeyValues) -> Result<Self, Error> {
506 if self.len() != keys.len()
507 || (0..self.len()).any(|index| self.is_valid(index) != keys.is_valid(index))
508 {
509 return Err(Error::Data(
510 "raw keys must align with their category projection".into(),
511 ));
512 }
513 let key_count = match &keys {
514 crate::data::KeyValues::Strings(values) => values.levels().len(),
515 crate::data::KeyValues::Raw(values) => values.levels().len(),
516 };
517 let mut first_slots = Vec::new();
518 reserve_source(&mut first_slots, self.levels.len())?;
519 first_slots.resize(self.levels.len(), None);
520 let aligned = key_count == self.levels.len()
523 && self.codes.iter().enumerate().all(|(row, &code)| {
524 if !self.validity.is_valid(row) {
525 return true;
526 }
527 let first = *first_slots[code as usize].get_or_insert(row);
528 match &keys {
529 crate::data::KeyValues::Strings(values) => values.get(first) == values.get(row),
530 crate::data::KeyValues::Raw(values) => values.get(first) == values.get(row),
531 }
532 });
533 if !aligned {
534 return Err(Error::Data(
535 "category projection must preserve the distinct original keys".into(),
536 ));
537 }
538 let source = match &keys {
539 crate::data::KeyValues::Strings(values) => values.vector_source(),
540 crate::data::KeyValues::Raw(values) => values.vector_source(),
541 };
542 if self.source.info() == source.info() {
543 self.source.retain_original_validity(source);
544 }
545 self.original_keys = Some(Arc::new(keys));
546 Ok(self)
547 }
548
549 #[doc(hidden)]
552 #[must_use]
553 pub fn with_field_key(mut self, key: crate::data::RawKey, name: impl Into<String>) -> Self {
554 self.field_key = Some(Arc::new((key, Arc::from(name.into()))));
555 self
556 }
557
558 #[doc(hidden)]
560 pub fn original_name(&self) -> &str {
561 self.field_key
562 .as_deref()
563 .map_or(&self.name, |(_, name)| name)
564 }
565
566 #[doc(hidden)]
568 pub fn field_key(&self) -> Option<&RawKey> {
569 self.field_key.as_deref().map(|(key, _)| key)
570 }
571
572 #[doc(hidden)]
574 pub fn with_source(self, source: DataSourceInfo) -> crate::Result<Self> {
575 self.with_repeated_source(source, 1)
576 }
577
578 #[doc(hidden)]
580 pub fn with_repeated_source(
581 mut self,
582 source: DataSourceInfo,
583 copies: usize,
584 ) -> crate::Result<Self> {
585 self.source = VectorSource::repeated(source, copies, self.len())?.with_original_validity(
586 self.len(),
587 self.has_nulls(),
588 |slot| self.validity.is_valid(slot),
589 )?;
590 Ok(self)
591 }
592
593 #[doc(hidden)]
595 pub fn with_source_slots(
596 mut self,
597 source: DataSourceInfo,
598 slots: Vec<usize>,
599 ) -> crate::Result<Self> {
600 self.source = VectorSource::gathered(source, slots, self.len())?.with_original_validity(
601 self.len(),
602 self.has_nulls(),
603 |slot| self.validity.is_valid(slot),
604 )?;
605 Ok(self)
606 }
607
608 #[doc(hidden)]
610 pub fn with_cycled_source(mut self, source: DataSourceInfo) -> crate::Result<Self> {
611 self.source = VectorSource::cycled(source, self.len())?.with_original_validity(
612 self.len(),
613 self.has_nulls(),
614 |slot| self.validity.is_valid(slot),
615 )?;
616 Ok(self)
617 }
618
619 #[doc(hidden)]
621 pub fn get_source(&self) -> &DataSourceInfo {
622 self.source.info()
623 }
624
625 pub(crate) fn vector_source(&self) -> &VectorSource {
626 &self.source
627 }
628
629 pub(crate) fn has_nulls(&self) -> bool {
630 !matches!(self.validity, Validity::All)
631 }
632
633 #[doc(hidden)]
635 pub fn source_slot(&self, index: usize) -> Option<usize> {
636 (index < self.len()).then(|| self.source.slot(index))
637 }
638
639 pub(crate) fn select(&self, indices: &[usize]) -> crate::Result<Self> {
640 let mut values = Self::from_optional(
641 copy_source_string(self.name())?,
642 indices.iter().map(|&index| {
643 let value = self.get(index).ok_or_else(|| {
644 Error::Data(format!("category selection index {index} is out of bounds").into())
645 })?;
646 value.map(copy_source_string).transpose()
647 }),
648 )?;
649 values.field_key.clone_from(&self.field_key);
650 values.source = self.source.select(indices)?;
651 values.original_keys = self
652 .original_keys
653 .as_ref()
654 .map(|keys| keys.select(indices).map(Arc::new))
655 .transpose()?;
656 Ok(values)
657 }
658
659 pub(crate) fn original_field(&self) -> crate::data::RawKey {
660 self.field_key.as_deref().map_or_else(
661 || crate::data::RawKey::from(self.name()),
662 |(key, _)| key.clone(),
663 )
664 }
665
666 #[doc(hidden)]
668 #[must_use]
669 pub fn with_field_name(mut self, name: &str) -> Self {
670 if self.name() != name {
671 self.name = Arc::from(name);
672 }
673 self.field_key = None;
674 self
675 }
676
677 #[must_use]
680 pub fn key_values(&self) -> crate::data::KeyValues {
681 if let Some(keys) = &self.original_keys {
682 return match keys.as_ref() {
683 crate::data::KeyValues::Strings(values) => {
684 let mut values = values.clone();
685 values.source = self.source.clone();
686 values.field_key.clone_from(&self.field_key);
687 crate::data::KeyValues::Strings(values)
688 }
689 crate::data::KeyValues::Raw(values) => crate::data::KeyValues::Raw(
690 values.clone().with_vector_source(self.source.clone()),
691 ),
692 };
693 }
694 let mut values = self.clone();
695 if let Some((_, name)) = values.field_key.as_deref() {
696 values.name = Arc::clone(name);
697 }
698 crate::data::KeyValues::Strings(values)
699 }
700
701 pub(crate) fn key_for_level(&self, level: &str) -> Option<RawKey> {
703 if self.original_keys.is_none() {
704 return self
705 .levels()
706 .iter()
707 .any(|value| value == level)
708 .then(|| RawKey::from(level));
709 }
710 self.key_at(self.iter().position(|value| value == Some(level))?)
711 }
712
713 pub(crate) fn key_at(&self, slot: usize) -> Option<RawKey> {
714 match self.original_keys.as_deref() {
715 None => self.get(slot)?.map(RawKey::from),
716 Some(crate::data::KeyValues::Strings(values)) => values.get(slot)?.map(RawKey::from),
717 Some(crate::data::KeyValues::Raw(values)) => values.get(slot)?.cloned(),
718 }
719 }
720
721 pub(crate) fn raw_field(&self) -> crate::data::category::RawField {
722 crate::data::category::RawField::from_key(Arc::clone(&self.name), self.original_field())
723 }
724
725 pub(crate) fn same_level_key(&self, level: &str, other: &Self, other_level: &str) -> bool {
726 if self.original_keys.is_none() && other.original_keys.is_none() {
727 level == other_level
728 } else {
729 self.key_for_level(level) == other.key_for_level(other_level)
730 }
731 }
732
733 pub(crate) fn raw_category(
734 &self,
735 level: &str,
736 ) -> crate::Result<crate::data::category::RawCategory> {
737 use crate::data::category::{shared_text, RawCategory};
738 let key = self.key_for_level(level).ok_or_else(|| {
739 Error::Render(format!("guide level {level:?} has no original category").into())
740 })?;
741 if matches!(&key, RawKey::String(value) if value == level) {
742 RawCategory::new(level)
743 } else {
744 Ok(RawCategory::from_key(shared_text(level)?, key))
745 }
746 }
747}
748
749impl PartialEq for CategoryValues {
750 fn eq(&self, other: &Self) -> bool {
751 self.name == other.name
752 && self.levels == other.levels
753 && self.codes == other.codes
754 && self.validity == other.validity
755 && self.original_keys == other.original_keys
756 && self.field_key == other.field_key
757 }
758}
759
760impl Eq for CategoryValues {}
761
762#[derive(Debug, Clone)]
764pub struct BooleanValues {
765 name: Arc<str>,
766 values: Arc<[bool]>,
767 validity: Validity,
768 field_key: Option<Arc<(RawKey, Arc<str>)>>,
769 source: VectorSource,
770}
771
772impl BooleanValues {
773 #[must_use]
775 pub fn new(name: impl Into<String>, values: impl IntoIterator<Item = bool>) -> Self {
776 let values = values.into_iter().collect::<Vec<_>>();
777 Self {
778 name: Arc::from(name.into()),
779 field_key: None,
780 source: VectorSource::direct(values.len()),
781 values: values.into(),
782 validity: Validity::All,
783 }
784 }
785
786 #[must_use]
788 pub fn nullable(
789 name: impl Into<String>,
790 values: impl IntoIterator<Item = Option<bool>>,
791 ) -> Self {
792 let values = values.into_iter().collect::<Vec<_>>();
793 let validity =
794 Validity::from_flags(&values.iter().map(Option::is_some).collect::<Vec<_>>());
795 let values = values
796 .into_iter()
797 .map(|value| value.unwrap_or(false))
798 .collect::<Vec<_>>();
799 Self {
800 name: Arc::from(name.into()),
801 field_key: None,
802 source: VectorSource::direct_nullable(
803 values.len(),
804 !matches!(&validity, Validity::All),
805 ),
806 values: values.into(),
807 validity,
808 }
809 }
810
811 #[must_use]
813 pub fn name(&self) -> &str {
814 self.field_key
815 .as_deref()
816 .map_or(&self.name, |(_, name)| name)
817 }
818
819 #[doc(hidden)]
821 #[must_use]
822 pub fn with_field_key(mut self, key: RawKey, name: impl Into<String>) -> Self {
823 self.field_key = Some(Arc::new((key, Arc::from(name.into()))));
824 self
825 }
826
827 #[doc(hidden)]
829 pub fn field_key(&self) -> Option<&RawKey> {
830 self.field_key.as_deref().map(|(key, _)| key)
831 }
832
833 #[doc(hidden)]
835 pub fn with_source(self, source: DataSourceInfo) -> crate::Result<Self> {
836 self.with_repeated_source(source, 1)
837 }
838
839 #[doc(hidden)]
841 pub fn with_repeated_source(
842 mut self,
843 source: DataSourceInfo,
844 copies: usize,
845 ) -> crate::Result<Self> {
846 self.source = VectorSource::repeated(source, copies, self.len())?.with_original_validity(
847 self.len(),
848 self.has_nulls(),
849 |slot| self.validity.is_valid(slot),
850 )?;
851 Ok(self)
852 }
853
854 #[doc(hidden)]
856 pub fn with_source_slots(
857 mut self,
858 source: DataSourceInfo,
859 slots: Vec<usize>,
860 ) -> crate::Result<Self> {
861 self.source = VectorSource::gathered(source, slots, self.len())?.with_original_validity(
862 self.len(),
863 self.has_nulls(),
864 |slot| self.validity.is_valid(slot),
865 )?;
866 Ok(self)
867 }
868
869 #[doc(hidden)]
871 pub fn get_source(&self) -> &DataSourceInfo {
872 self.source.info()
873 }
874
875 pub(crate) fn vector_source(&self) -> &VectorSource {
876 &self.source
877 }
878
879 pub(crate) fn has_nulls(&self) -> bool {
880 !matches!(self.validity, Validity::All)
881 }
882
883 #[doc(hidden)]
885 pub fn source_slot(&self, index: usize) -> Option<usize> {
886 (index < self.len()).then(|| self.source.slot(index))
887 }
888
889 #[must_use]
891 pub fn len(&self) -> usize {
892 self.values.len()
893 }
894
895 #[must_use]
897 pub fn is_empty(&self) -> bool {
898 self.values.is_empty()
899 }
900
901 #[must_use]
903 pub fn get(&self, row: usize) -> Option<Option<bool>> {
904 self.values
905 .get(row)
906 .map(|value| self.validity.is_valid(row).then_some(*value))
907 }
908
909 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<bool>> + '_ {
911 (0..self.len()).map(|row| self.get(row).expect("in-bounds source slot"))
912 }
913
914 #[must_use]
916 pub fn is_valid(&self, row: usize) -> Option<bool> {
917 (row < self.len()).then(|| self.validity.is_valid(row))
918 }
919}
920
921impl PartialEq for BooleanValues {
922 fn eq(&self, other: &Self) -> bool {
923 self.name == other.name && self.values == other.values && self.validity == other.validity
924 }
925}
926
927impl Eq for BooleanValues {}