• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16204612549

10 Jul 2025 07:50PM UTC coverage: 81.152% (+2.9%) from 78.263%
16204612549

Pull #3825

github

web-flow
Merge d0d2717da into be9c2fd3e
Pull Request #3825: feat: Add optimize ArrayOp with VBView implementation

178 of 211 new or added lines in 4 files covered. (84.36%)

330 existing lines in 34 files now uncovered.

45433 of 55985 relevant lines covered (81.15%)

145951.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.7
/vortex-expr/src/exprs/cast.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::fmt::Display;
5

6
use vortex_array::compute::cast as compute_cast;
7
use vortex_array::{ArrayRef, DeserializeMetadata, ProstMetadata};
8
use vortex_dtype::DType;
9
use vortex_error::{VortexResult, vortex_bail, vortex_err};
10
use vortex_proto::expr as pb;
11

12
use crate::{AnalysisExpr, ExprEncodingRef, ExprId, ExprRef, IntoExpr, Scope, VTable, vtable};
13

14
vtable!(Cast);
15

16
#[allow(clippy::derived_hash_with_manual_eq)]
17
#[derive(Debug, Clone, Hash)]
18
pub struct CastExpr {
19
    target: DType,
20
    child: ExprRef,
21
}
22

23
impl PartialEq for CastExpr {
UNCOV
24
    fn eq(&self, other: &Self) -> bool {
×
UNCOV
25
        self.target == other.target && self.child.eq(&other.child)
×
26
    }
×
27
}
28

29
pub struct CastExprEncoding;
30

31
impl VTable for CastVTable {
32
    type Expr = CastExpr;
33
    type Encoding = CastExprEncoding;
34
    type Metadata = ProstMetadata<pb::CastOpts>;
35

UNCOV
36
    fn id(_encoding: &Self::Encoding) -> ExprId {
×
UNCOV
37
        ExprId::new_ref("cast")
×
38
    }
×
39

40
    fn encoding(_expr: &Self::Expr) -> ExprEncodingRef {
×
UNCOV
41
        ExprEncodingRef::new_ref(CastExprEncoding.as_ref())
×
42
    }
×
43

44
    fn metadata(expr: &Self::Expr) -> Option<Self::Metadata> {
×
UNCOV
45
        Some(ProstMetadata(pb::CastOpts {
×
46
            target: Some((&expr.target).into()),
×
47
        }))
×
48
    }
×
49

50
    fn children(expr: &Self::Expr) -> Vec<&ExprRef> {
1✔
51
        vec![&expr.child]
1✔
52
    }
1✔
53

54
    fn with_children(expr: &Self::Expr, children: Vec<ExprRef>) -> VortexResult<Self::Expr> {
1✔
55
        Ok(CastExpr {
1✔
56
            target: expr.target.clone(),
1✔
57
            child: children[0].clone(),
1✔
58
        })
1✔
59
    }
1✔
60

UNCOV
61
    fn build(
×
UNCOV
62
        _encoding: &Self::Encoding,
×
63
        metadata: &<Self::Metadata as DeserializeMetadata>::Output,
×
64
        children: Vec<ExprRef>,
×
65
    ) -> VortexResult<Self::Expr> {
×
66
        if children.len() != 1 {
×
67
            vortex_bail!(
×
68
                "Cast expression must have exactly 1 child, got {}",
×
69
                children.len()
×
70
            );
×
71
        }
×
72
        let target: DType = metadata
×
73
            .target
×
74
            .as_ref()
×
75
            .ok_or_else(|| vortex_err!("missing target dtype in CastOpts"))?
×
76
            .try_into()?;
×
77
        Ok(CastExpr {
×
78
            target,
×
79
            child: children[0].clone(),
×
80
        })
×
81
    }
×
82

83
    fn evaluate(expr: &Self::Expr, scope: &Scope) -> VortexResult<ArrayRef> {
1✔
84
        let array = expr.child.evaluate(scope)?;
1✔
85
        compute_cast(&array, &expr.target)
1✔
86
    }
1✔
87

88
    fn return_dtype(expr: &Self::Expr, _scope: &DType) -> VortexResult<DType> {
2✔
89
        Ok(expr.target.clone())
2✔
90
    }
2✔
91
}
92

93
impl CastExpr {
94
    pub fn new(child: ExprRef, target: DType) -> Self {
3✔
95
        Self { target, child }
3✔
96
    }
3✔
97
}
98

99
impl Display for CastExpr {
UNCOV
100
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
UNCOV
101
        write!(f, "cast({}, {})", self.child, self.target)
×
102
    }
×
103
}
104

105
impl AnalysisExpr for CastExpr {}
106

107
pub fn cast(child: ExprRef, target: DType) -> ExprRef {
3✔
108
    CastExpr::new(child, target).into_expr()
3✔
109
}
3✔
110

111
#[cfg(test)]
112
mod tests {
113
    use vortex_array::IntoArray;
114
    use vortex_array::arrays::StructArray;
115
    use vortex_buffer::buffer;
116
    use vortex_dtype::{DType, Nullability, PType};
117

118
    use crate::{ExprRef, Scope, cast, get_item, root, test_harness};
119

120
    #[test]
121
    fn dtype() {
1✔
122
        let dtype = test_harness::struct_dtype();
1✔
123
        assert_eq!(
1✔
124
            cast(root(), DType::Bool(Nullability::NonNullable))
1✔
125
                .return_dtype(&dtype)
1✔
126
                .unwrap(),
1✔
127
            DType::Bool(Nullability::NonNullable)
1✔
128
        );
1✔
129
    }
1✔
130

131
    #[test]
132
    fn replace_children() {
1✔
133
        let expr = cast(root(), DType::Bool(Nullability::Nullable));
1✔
134
        let _ = expr.with_children(vec![root()]);
1✔
135
    }
1✔
136

137
    #[test]
138
    fn evaluate() {
1✔
139
        let test_array = StructArray::from_fields(&[
1✔
140
            ("a", buffer![0i32, 1, 2].into_array()),
1✔
141
            ("b", buffer![4i64, 5, 6].into_array()),
1✔
142
        ])
1✔
143
        .unwrap()
1✔
144
        .into_array();
1✔
145

1✔
146
        let expr: ExprRef = cast(
1✔
147
            get_item("a", root()),
1✔
148
            DType::Primitive(PType::I64, Nullability::NonNullable),
1✔
149
        );
1✔
150
        let result = expr.evaluate(&Scope::new(test_array)).unwrap();
1✔
151

1✔
152
        assert_eq!(
1✔
153
            result.dtype(),
1✔
154
            &DType::Primitive(PType::I64, Nullability::NonNullable)
1✔
155
        );
1✔
156
    }
1✔
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc