Skip to main content

vortex_array/builders/
primitive.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::any::Any;
5use std::mem::MaybeUninit;
6
7use vortex_buffer::BufferMut;
8use vortex_error::VortexExpect;
9use vortex_error::VortexResult;
10use vortex_error::vortex_ensure;
11use vortex_mask::Mask;
12
13use crate::ArrayRef;
14use crate::IntoArray;
15use crate::LEGACY_SESSION;
16use crate::VortexSessionExecute;
17use crate::arrays::PrimitiveArray;
18use crate::builders::ArrayBuilder;
19use crate::builders::DEFAULT_BUILDER_CAPACITY;
20use crate::builders::LazyBitBufferBuilder;
21use crate::canonical::Canonical;
22#[expect(deprecated)]
23use crate::canonical::ToCanonical as _;
24use crate::dtype::DType;
25use crate::dtype::NativePType;
26use crate::dtype::Nullability;
27use crate::scalar::Scalar;
28
29/// The builder for building a [`PrimitiveArray`], parametrized by the `PType`.
30pub struct PrimitiveBuilder<T> {
31    dtype: DType,
32    values: BufferMut<T>,
33    nulls: LazyBitBufferBuilder,
34}
35
36impl<T: NativePType> PrimitiveBuilder<T> {
37    /// Creates a new `PrimitiveBuilder` with a capacity of [`DEFAULT_BUILDER_CAPACITY`].
38    pub fn new(nullability: Nullability) -> Self {
39        Self::with_capacity(nullability, DEFAULT_BUILDER_CAPACITY)
40    }
41
42    /// Creates a new `PrimitiveBuilder` with the given `capacity`.
43    pub fn with_capacity(nullability: Nullability, capacity: usize) -> Self {
44        Self {
45            values: BufferMut::with_capacity(capacity),
46            nulls: LazyBitBufferBuilder::new(capacity),
47            dtype: DType::Primitive(T::PTYPE, nullability),
48        }
49    }
50
51    /// Appends a primitive `value` to the builder.
52    pub fn append_value(&mut self, value: T) {
53        self.values.push(value);
54        self.nulls.append_non_null();
55    }
56
57    /// Appends `n` copies of `value` as non-null entries, directly writing into the buffer.
58    pub fn append_n_values(&mut self, value: T, n: usize) {
59        self.values.push_n(value, n);
60        self.nulls.append_n_non_nulls(n);
61    }
62
63    /// Returns the raw primitive values in this builder as a slice.
64    pub fn values(&self) -> &[T] {
65        self.values.as_ref()
66    }
67
68    /// Returns the raw primitive values in this builder as a mutable slice.
69    pub fn values_mut(&mut self) -> &mut [T] {
70        self.values.as_mut()
71    }
72
73    /// Create a new handle to the next `len` uninitialized values in the builder.
74    ///
75    /// All reads/writes through the handle to the values buffer or the validity buffer will operate
76    /// on indices relative to the start of the range.
77    ///
78    /// # Panics
79    ///
80    /// Panics if `len` is 0 or if the current length of the builder plus `len` would exceed the
81    /// capacity of the builder's memory.
82    ///
83    /// ## Example
84    ///
85    /// ```
86    /// use std::mem::MaybeUninit;
87    /// use vortex_array::builders::{ArrayBuilder, PrimitiveBuilder};
88    /// use vortex_array::dtype::Nullability;
89    ///
90    /// // Create a new builder.
91    /// let mut builder: PrimitiveBuilder<i32> =
92    ///     PrimitiveBuilder::with_capacity(Nullability::NonNullable, 5);
93    ///
94    /// // Populate the values.
95    /// let mut uninit_range = builder.uninit_range(5);
96    /// uninit_range.copy_from_slice(0, &[0, 1, 2, 3, 4]);
97    ///
98    /// // SAFETY: We have initialized all 5 values in the range, and since the array builder is
99    /// // non-nullable, we don't need to set any null bits.
100    /// unsafe { uninit_range.finish(); }
101    ///
102    /// let built = builder.finish_into_primitive();
103    ///
104    /// assert_eq!(built.as_slice::<i32>(), &[0i32, 1, 2, 3, 4]);
105    /// ```
106    pub fn uninit_range(&mut self, len: usize) -> UninitRange<'_, T> {
107        assert_ne!(0, len, "cannot create an uninit range of length 0");
108
109        let current_len = self.values.len();
110        assert!(
111            current_len + len <= self.values.capacity(),
112            "uninit_range of len {len} exceeds builder with length {} and capacity {}",
113            current_len,
114            self.values.capacity()
115        );
116
117        UninitRange { len, builder: self }
118    }
119
120    /// Finishes the builder directly into a [`PrimitiveArray`].
121    pub fn finish_into_primitive(&mut self) -> PrimitiveArray {
122        let validity = self
123            .nulls
124            .finish_with_nullability(self.dtype().nullability());
125
126        PrimitiveArray::new(std::mem::take(&mut self.values).freeze(), validity)
127    }
128
129    /// Extends the primitive array with an iterator.
130    pub fn extend_with_iterator(&mut self, iter: impl IntoIterator<Item = T>, mask: Mask) {
131        self.values.extend(iter);
132        self.nulls.append_validity_mask(mask);
133    }
134}
135
136impl<T: NativePType> ArrayBuilder for PrimitiveBuilder<T> {
137    fn as_any(&self) -> &dyn Any {
138        self
139    }
140
141    fn as_any_mut(&mut self) -> &mut dyn Any {
142        self
143    }
144
145    fn dtype(&self) -> &DType {
146        &self.dtype
147    }
148
149    fn len(&self) -> usize {
150        self.values.len()
151    }
152
153    fn append_zeros(&mut self, n: usize) {
154        self.values.push_n(T::default(), n);
155        self.nulls.append_n_non_nulls(n);
156    }
157
158    unsafe fn append_nulls_unchecked(&mut self, n: usize) {
159        self.values.push_n(T::default(), n);
160        self.nulls.append_n_nulls(n);
161    }
162
163    fn append_scalar(&mut self, scalar: &Scalar) -> VortexResult<()> {
164        vortex_ensure!(
165            scalar.dtype() == self.dtype(),
166            "PrimitiveBuilder expected scalar with dtype {}, got {}",
167            self.dtype(),
168            scalar.dtype()
169        );
170
171        if let Some(pv) = scalar.as_primitive().pvalue() {
172            self.append_value(pv.cast::<T>()?)
173        } else {
174            self.append_null()
175        }
176
177        Ok(())
178    }
179
180    unsafe fn extend_from_array_unchecked(&mut self, array: &ArrayRef) {
181        #[expect(deprecated)]
182        let array = array.to_primitive();
183
184        // This should be checked in `extend_from_array` but we can check it again.
185        debug_assert_eq!(
186            array.ptype(),
187            T::PTYPE,
188            "Cannot extend from array with different ptype"
189        );
190
191        self.values.extend_from_slice(array.as_slice::<T>());
192        self.nulls.append_validity_mask(
193            array
194                .as_ref()
195                .validity()
196                .vortex_expect("validity_mask")
197                .execute_mask(
198                    array.as_ref().len(),
199                    &mut LEGACY_SESSION.create_execution_ctx(),
200                )
201                .vortex_expect("Failed to compute validity mask"),
202        );
203    }
204
205    fn reserve_exact(&mut self, additional: usize) {
206        self.values.reserve(additional);
207        self.nulls.reserve_exact(additional);
208    }
209
210    unsafe fn set_validity_unchecked(&mut self, validity: Mask) {
211        self.nulls = LazyBitBufferBuilder::new(validity.len());
212        self.nulls.append_validity_mask(validity);
213    }
214
215    fn finish(&mut self) -> ArrayRef {
216        self.finish_into_primitive().into_array()
217    }
218
219    fn finish_into_canonical(&mut self) -> Canonical {
220        Canonical::Primitive(self.finish_into_primitive())
221    }
222}
223
224/// A range of uninitialized values in the primitive builder that can be filled.
225pub struct UninitRange<'a, T> {
226    /// The length of the uninitialized range.
227    ///
228    /// This is guaranteed to be within the memory capacity of the builder.
229    len: usize,
230
231    /// A mutable reference to the builder.
232    ///
233    /// Since this is a mutable reference, we can guarantee that nothing else can modify the builder
234    /// while this `UninitRange` exists.
235    builder: &'a mut PrimitiveBuilder<T>,
236}
237
238impl<T> UninitRange<'_, T> {
239    /// Returns the length of this uninitialized range.
240    #[inline]
241    pub fn len(&self) -> usize {
242        self.len
243    }
244
245    /// Returns true if this range has zero length.
246    #[inline]
247    pub fn is_empty(&self) -> bool {
248        self.len == 0
249    }
250
251    /// Set a value at the given index within this range.
252    ///
253    /// # Panics
254    ///
255    /// Panics if the index is out of bounds.
256    #[inline]
257    pub fn set_value(&mut self, index: usize, value: T) {
258        assert!(index < self.len, "index out of bounds");
259        let spare = self.builder.values.spare_capacity_mut();
260        spare[index] = MaybeUninit::new(value);
261    }
262
263    /// Append a [`Mask`] to this builder's null buffer.
264    ///
265    /// # Panics
266    ///
267    /// Panics if the mask length is not equal to the the length of the current `UninitRange`.
268    ///
269    /// # Safety
270    ///
271    /// - The caller must ensure that they safely initialize `mask.len()` primitive values via
272    ///   [`UninitRange::copy_from_slice`].
273    /// - The caller must also ensure that they only call this method once.
274    pub unsafe fn append_mask(&mut self, mask: Mask) {
275        assert_eq!(
276            mask.len(),
277            self.len,
278            "Tried to append a mask to an `UninitRange` that was beyond the allowed range"
279        );
280
281        // TODO(connor): Ideally, we would call this function `set_mask` and directly set all of the
282        // bits (so that we can call this multiple times), but the underlying `BooleanBuffer` does
283        // not have an easy way to do this correctly.
284
285        self.builder.nulls.append_validity_mask(mask);
286    }
287
288    /// Set a validity bit at the given index.
289    ///
290    /// The index is relative to the start of this range (not relative to the values already in the
291    /// builder).
292    ///
293    /// Note that this will have no effect if the builder is non-nullable.
294    pub fn set_validity_bit(&mut self, index: usize, v: bool) {
295        assert!(index < self.len, "set_bit index out of bounds");
296        // Note that this won't panic because we can only create an `UninitRange` within the
297        // capacity of the builder (it will not automatically resize).
298        let absolute_index = self.builder.values.len() + index;
299        self.builder.nulls.set_bit(absolute_index, v);
300    }
301
302    /// Set values from an initialized range.
303    ///
304    /// Note that the input `offset` should be an offset relative to the local `UninitRange`, not
305    /// the entire `PrimitiveBuilder`.
306    pub fn copy_from_slice(&mut self, local_offset: usize, src: &[T])
307    where
308        T: Copy,
309    {
310        debug_assert!(
311            local_offset + src.len() <= self.len,
312            "tried to copy a slice into a `UninitRange` past its boundary"
313        );
314
315        // SAFETY: &[T] and &[MaybeUninit<T>] have the same layout.
316        let uninit_src: &[MaybeUninit<T>] = unsafe { std::mem::transmute(src) };
317
318        // Note: spare_capacity_mut() returns the spare capacity starting from the current length,
319        // so we just use local_offset directly.
320        let dst =
321            &mut self.builder.values.spare_capacity_mut()[local_offset..local_offset + src.len()];
322        dst.copy_from_slice(uninit_src);
323    }
324
325    /// Get a mutable slice of uninitialized memory at the specified offset within this range.
326    ///
327    /// Note that the offsets are relative to this local range, not to the values already in the
328    /// builder.
329    ///
330    /// # Safety
331    ///
332    /// The caller must ensure that they properly initialize the returned memory before calling
333    /// `finish()` on this range.
334    ///
335    /// # Panics
336    ///
337    /// Panics if `offset + len` exceeds the range bounds.
338    pub unsafe fn slice_uninit_mut(&mut self, offset: usize, len: usize) -> &mut [MaybeUninit<T>] {
339        assert!(
340            offset + len <= self.len,
341            "slice_uninit_mut: offset {} + len {} exceeds range length {}",
342            offset,
343            len,
344            self.len
345        );
346        &mut self.builder.values.spare_capacity_mut()[offset..offset + len]
347    }
348
349    /// Finish building this range, marking it as initialized and advancing the length of the
350    /// underlying values buffer.
351    ///
352    /// # Safety
353    ///
354    /// The caller must ensure that they have safely initialized all `len` values via
355    /// [`copy_from_slice()`] or [`set_value()`], as well as correctly set all of the null bits via
356    /// [`set_validity_bit()`] or [`append_mask()`] if the builder is nullable.
357    ///
358    /// [`copy_from_slice()`]: UninitRange::copy_from_slice
359    /// [`set_value()`]: UninitRange::set_value
360    /// [`set_validity_bit()`]: UninitRange::set_validity_bit
361    /// [`append_mask()`]: UninitRange::append_mask
362    pub unsafe fn finish(self) {
363        // SAFETY: constructor enforces that current length + len does not exceed the capacity of the array.
364        let new_len = self.builder.values.len() + self.len;
365        unsafe { self.builder.values.set_len(new_len) };
366    }
367}
368
369#[cfg(test)]
370mod tests {
371    use vortex_error::VortexExpect;
372
373    use super::*;
374    use crate::assert_arrays_eq;
375
376    /// REGRESSION TEST: This test verifies that multiple sequential ranges have correct offsets.
377    ///
378    /// This would have caught the `Deref` bug where it always returned from the start of the
379    /// buffer.
380    #[test]
381    fn test_multiple_uninit_ranges_correct_offsets() {
382        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::NonNullable, 10);
383
384        // First range.
385        let mut range1 = builder.uninit_range(3);
386        range1.copy_from_slice(0, &[1, 2, 3]);
387
388        // SAFETY: We initialized all 3 values.
389        unsafe {
390            range1.finish();
391        }
392
393        // Verify the builder now has these values.
394        assert_eq!(builder.values(), &[1, 2, 3]);
395
396        // Second range - this would fail with the old Deref implementation.
397        let mut range2 = builder.uninit_range(2);
398
399        // Set values using copy_from_slice.
400        range2.copy_from_slice(0, &[4, 5]);
401
402        // SAFETY: We initialized both values.
403        unsafe {
404            range2.finish();
405        }
406
407        // Verify the builder now has all 5 values.
408        assert_eq!(builder.values(), &[1, 2, 3, 4, 5]);
409
410        let array = builder.finish_into_primitive();
411        assert_arrays_eq!(array, PrimitiveArray::from_iter([1i32, 2, 3, 4, 5]));
412    }
413
414    /// REGRESSION TEST: This test verifies that `append_mask` was correctly moved from
415    /// `PrimitiveBuilder` to `UninitRange`.
416    ///
417    /// The old API had `append_mask` on the builder, which was confusing when used with ranges.
418    /// This test ensures the new API works correctly.
419    #[test]
420    fn test_append_mask_on_uninit_range() {
421        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::Nullable, 5);
422        let mut range = builder.uninit_range(3);
423
424        // Create a mask for 3 values.
425        let mask = Mask::from_iter([true, false, true]);
426
427        // SAFETY: We're about to initialize the values.
428        unsafe {
429            range.append_mask(mask);
430        }
431
432        // Initialize the values.
433        range.copy_from_slice(0, &[10, 20, 30]);
434
435        // SAFETY: We've initialized all values and set the mask.
436        unsafe {
437            range.finish();
438        }
439
440        let array = builder.finish_into_primitive();
441        assert_eq!(array.len(), 3);
442        // Check validity using scalar_at - nulls will return is_null() = true.
443        assert!(
444            !array
445                .execute_scalar(0, &mut LEGACY_SESSION.create_execution_ctx())
446                .unwrap()
447                .is_null()
448        );
449        assert!(
450            array
451                .execute_scalar(1, &mut LEGACY_SESSION.create_execution_ctx())
452                .unwrap()
453                .is_null()
454        );
455        assert!(
456            !array
457                .execute_scalar(2, &mut LEGACY_SESSION.create_execution_ctx())
458                .unwrap()
459                .is_null()
460        );
461    }
462
463    /// REGRESSION TEST: This test verifies that `append_mask` validates the mask length.
464    ///
465    /// This ensures that masks can only be appended if they match the range length.
466    #[test]
467    #[should_panic(
468        expected = "Tried to append a mask to an `UninitRange` that was beyond the allowed range"
469    )]
470    fn test_append_mask_wrong_length_panics() {
471        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::Nullable, 10);
472        let mut range = builder.uninit_range(5);
473
474        // Try to append a mask with wrong length (3 instead of 5).
475        let wrong_mask = Mask::from_iter([true, false, true]);
476
477        // SAFETY: This is expected to panic due to length mismatch.
478        unsafe {
479            range.append_mask(wrong_mask);
480        }
481    }
482
483    /// Test that `copy_from_slice` works correctly with different offsets.
484    ///
485    /// This verifies the new simplified API without the redundant `len` parameter.
486    #[test]
487    fn test_copy_from_slice_with_offsets() {
488        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::NonNullable, 10);
489        let mut range = builder.uninit_range(6);
490
491        // Copy to different offsets.
492        range.copy_from_slice(0, &[1, 2]);
493        range.copy_from_slice(2, &[3, 4]);
494        range.copy_from_slice(4, &[5, 6]);
495
496        // SAFETY: We've initialized all 6 values.
497        unsafe {
498            range.finish();
499        }
500
501        let array = builder.finish_into_primitive();
502        assert_arrays_eq!(array, PrimitiveArray::from_iter([1i32, 2, 3, 4, 5, 6]));
503    }
504
505    /// Test that `set_bit` uses relative indexing within the range.
506    ///
507    /// Note: `set_bit` requires the null buffer to already be initialized, so we first
508    /// use `append_mask` to set up the buffer, then demonstrate that `set_bit` can
509    /// modify individual bits with relative indexing.
510    #[test]
511    fn test_set_bit_relative_indexing() {
512        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::Nullable, 10);
513
514        // First add some values to the builder.
515        builder.append_value(100);
516        builder.append_value(200);
517
518        // Create a range for new values.
519        let mut range = builder.uninit_range(3);
520
521        // Use append_mask to initialize the validity buffer for this range.
522        let initial_mask = Mask::from_iter([false, false, false]);
523        // SAFETY: We're about to initialize the values.
524        unsafe {
525            range.append_mask(initial_mask);
526        }
527
528        // Now we can use set_bit to modify individual bits with relative indexing.
529        range.set_validity_bit(0, true); // Change first bit to valid
530        range.set_validity_bit(2, true); // Change third bit to valid
531        // Leave middle bit as false (null)
532
533        // Initialize the values.
534        range.copy_from_slice(0, &[10, 20, 30]);
535
536        // SAFETY: We've initialized all 3 values and set their validity.
537        unsafe {
538            range.finish();
539        }
540
541        let array = builder.finish_into_primitive();
542
543        // Verify the total length and values.
544        assert_eq!(array.len(), 5);
545        assert_eq!(array.as_slice::<i32>(), &[100, 200, 10, 20, 30]);
546
547        // Check validity - the first two should be valid (from append_value).
548        assert!(
549            !array
550                .execute_scalar(0, &mut LEGACY_SESSION.create_execution_ctx())
551                .unwrap()
552                .is_null()
553        ); // initial value 100
554        assert!(
555            !array
556                .execute_scalar(1, &mut LEGACY_SESSION.create_execution_ctx())
557                .unwrap()
558                .is_null()
559        ); // initial value 200
560
561        // Check the range items with modified validity.
562        assert!(
563            !array
564                .execute_scalar(2, &mut LEGACY_SESSION.create_execution_ctx())
565                .unwrap()
566                .is_null()
567        ); // range index 0 - set to valid
568        assert!(
569            array
570                .execute_scalar(3, &mut LEGACY_SESSION.create_execution_ctx())
571                .unwrap()
572                .is_null()
573        ); // range index 1 - left as null
574        assert!(
575            !array
576                .execute_scalar(4, &mut LEGACY_SESSION.create_execution_ctx())
577                .unwrap()
578                .is_null()
579        ); // range index 2 - set to valid
580    }
581
582    /// Test that creating a zero-length uninit range panics.
583    #[test]
584    #[should_panic(expected = "cannot create an uninit range of length 0")]
585    fn test_zero_length_uninit_range_panics() {
586        let mut builder = PrimitiveBuilder::<i32>::new(Nullability::NonNullable);
587        let _range = builder.uninit_range(0);
588    }
589
590    /// Test that creating an uninit range exceeding capacity panics.
591    #[test]
592    #[should_panic(
593        expected = "uninit_range of len 10 exceeds builder with length 0 and capacity 6"
594    )]
595    fn test_uninit_range_exceeds_capacity_panics() {
596        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::NonNullable, 5);
597        let _range = builder.uninit_range(10);
598    }
599
600    /// Test that `copy_from_slice` debug asserts on out-of-bounds access.
601    ///
602    /// Note: This only panics in debug mode due to `debug_assert!`.
603    #[test]
604    #[cfg(debug_assertions)]
605    #[should_panic(expected = "tried to copy a slice into a `UninitRange` past its boundary")]
606    fn test_copy_from_slice_out_of_bounds() {
607        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::NonNullable, 10);
608        let mut range = builder.uninit_range(3);
609
610        // Try to copy 3 elements starting at offset 1 (would need 4 slots total).
611        range.copy_from_slice(1, &[1, 2, 3]);
612    }
613
614    /// Test that the unsafe contract of `finish` is documented and works correctly.
615    ///
616    /// This test demonstrates proper usage of the unsafe `finish` method.
617    #[test]
618    fn test_finish_unsafe_contract() {
619        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::Nullable, 5);
620        let mut range = builder.uninit_range(3);
621
622        // Set validity mask.
623        let mask = Mask::from_iter([true, true, false]);
624        // SAFETY: We're about to initialize the matching number of values.
625        unsafe {
626            range.append_mask(mask);
627        }
628
629        // Initialize all values.
630        range.copy_from_slice(0, &[10, 20, 30]);
631
632        // SAFETY: We have initialized all 3 values and set their validity.
633        unsafe {
634            range.finish();
635        }
636
637        let array = builder.finish_into_primitive();
638        assert_eq!(array.len(), 3);
639        assert_eq!(array.as_slice::<i32>(), &[10, 20, 30]);
640    }
641
642    #[test]
643    fn test_append_scalar() {
644        use crate::dtype::DType;
645        use crate::scalar::Scalar;
646
647        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::Nullable, 10);
648
649        // Test appending a valid primitive value.
650        let scalar1 = Scalar::primitive(42i32, Nullability::Nullable);
651        builder.append_scalar(&scalar1).unwrap();
652
653        // Test appending another value.
654        let scalar2 = Scalar::primitive(84i32, Nullability::Nullable);
655        builder.append_scalar(&scalar2).unwrap();
656
657        // Test appending null value.
658        let null_scalar = Scalar::null(DType::Primitive(
659            crate::dtype::PType::I32,
660            Nullability::Nullable,
661        ));
662        builder.append_scalar(&null_scalar).unwrap();
663
664        let array = builder.finish_into_primitive();
665        assert_eq!(array.len(), 3);
666
667        // Check actual values.
668        let values = array.as_slice::<i32>();
669        assert_eq!(values[0], 42);
670        assert_eq!(values[1], 84);
671        // values[2] might be any value since it's null.
672
673        // Check validity - first two should be valid, third should be null.
674        assert!(
675            array
676                .validity()
677                .vortex_expect("primitive validity should be derivable")
678                .is_valid(0)
679                .unwrap()
680        );
681        assert!(
682            array
683                .validity()
684                .vortex_expect("primitive validity should be derivable")
685                .is_valid(1)
686                .unwrap()
687        );
688        assert!(
689            !array
690                .validity()
691                .vortex_expect("primitive validity should be derivable")
692                .is_valid(2)
693                .unwrap()
694        );
695
696        // Test wrong dtype error.
697        let mut builder = PrimitiveBuilder::<i32>::with_capacity(Nullability::NonNullable, 10);
698        let wrong_scalar = Scalar::from(true);
699        assert!(builder.append_scalar(&wrong_scalar).is_err());
700    }
701}