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
//! [`TableView`] trait — public causal table API (ADR 0004).
//!
//! SPDX-License-Identifier: MIT OR Apache-2.0
use antecedent_core::{CausalSchema, VariableId};
use crate::column::ColumnView;
use crate::error::DataError;
/// Lossy `i64` → `f64` for the float analysis path (mantissa cannot hold all `i64`).
fn analysis_f64_from_i64(v: i64) -> f64 {
#[allow(clippy::cast_precision_loss)]
{
v as f64
}
}
/// Borrowed table access used by algorithms.
pub trait TableView {
/// Immutable causal schema.
fn schema(&self) -> &CausalSchema;
/// Number of rows.
fn row_count(&self) -> usize;
/// Column view for `id`.
///
/// # Errors
///
/// Unknown variable or type issues.
fn column(&self, id: VariableId) -> Result<ColumnView<'_>, DataError>;
/// Borrow a native `Float64` column as a contiguous slice (no allocation).
///
/// Unlike [`TableView::float64_values`], no coercion is attempted: `Int64`
/// and `Boolean` columns error so callers stay on the copying path for
/// them. Use [`TableView::float64_cow`] to get borrowed-when-possible
/// semantics with the same values as `float64_values`.
///
/// # Errors
///
/// Unknown variable or non-`Float64` column type.
fn float64_slice(&self, id: VariableId) -> Result<&[f64], DataError> {
match self.column(id)? {
ColumnView::Float64(c) => Ok(c.values.as_slice()),
_ => Err(DataError::TypeMismatch { id, expected: "native float64 (borrowed slice)" }),
}
}
/// Column values as `f64`, borrowed when the column is native `Float64`.
///
/// Yields exactly the same values as [`TableView::float64_values`]
/// (including the `Int64`/`Boolean` coercions), but avoids the copy for
/// native `Float64` columns.
///
/// # Errors
///
/// Unknown variable or unsupported column type.
fn float64_cow(&self, id: VariableId) -> Result<std::borrow::Cow<'_, [f64]>, DataError> {
match self.float64_slice(id) {
Ok(s) => Ok(std::borrow::Cow::Borrowed(s)),
Err(_) => self.float64_values(id).map(std::borrow::Cow::Owned),
}
}
/// Copy a column into an owned `f64` buffer.
///
/// Native `Float64` columns are copied as-is. `Int64` and `Boolean` columns
/// are coerced to `f64` (`true` → `1.0`, `false` → `0.0`); invalid rows become
/// `NaN`. Other column kinds (categorical, timestamp, fixed vector) error.
///
/// # Errors
///
/// Unknown variable or unsupported column type.
fn float64_values(&self, id: VariableId) -> Result<Vec<f64>, DataError> {
match self.column(id)? {
ColumnView::Float64(c) => Ok(c.values.to_vec()),
ColumnView::Int64(c) => {
let mut out = Vec::with_capacity(c.values.len());
for (i, &v) in c.values.iter().enumerate() {
out.push(if c.validity.is_valid(i) {
analysis_f64_from_i64(v)
} else {
f64::NAN
});
}
Ok(out)
}
ColumnView::Boolean(c) => {
let mut out = Vec::with_capacity(c.values.len());
for (i, &v) in c.values.iter().enumerate() {
out.push(if c.validity.is_valid(i) { f64::from(v) } else { f64::NAN });
}
Ok(out)
}
_ => Err(DataError::TypeMismatch {
id,
expected: "float64 (or coercible int64/boolean)",
}),
}
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use antecedent_core::{
CausalSchemaBuilder, MeasurementSpec, RoleHint, SmallRoleSet, ValueType, VariableId,
};
use super::*;
use crate::column::{BooleanColumn, Float64Column, Int64Column, OwnedColumn, ValidityBitmap};
use crate::dataset::TabularData;
use crate::storage::OwnedColumnarStorage;
fn schema_n(n: usize) -> antecedent_core::CausalSchema {
let mut b = CausalSchemaBuilder::new();
for i in 0..n {
b.add_variable(
format!("v{i}"),
ValueType::Continuous,
SmallRoleSet::from_hint(RoleHint::Context),
None,
None,
MeasurementSpec::default(),
)
.unwrap();
}
b.build().unwrap()
}
#[test]
fn float64_values_coerces_int64_and_boolean() {
let schema = schema_n(3);
let cols = vec![
OwnedColumn::Float64(
Float64Column::new(
VariableId::from_raw(0),
Arc::from([1.5_f64, 2.5]),
ValidityBitmap::all_valid(2),
)
.unwrap(),
),
OwnedColumn::Int64(
Int64Column::new(
VariableId::from_raw(1),
Arc::<[i64]>::from([3_i64, 4]),
ValidityBitmap::all_valid(2),
)
.unwrap(),
),
OwnedColumn::Boolean(
BooleanColumn::new(
VariableId::from_raw(2),
Arc::<[u8]>::from([1_u8, 0]),
ValidityBitmap::all_valid(2),
)
.unwrap(),
),
];
let storage = OwnedColumnarStorage::try_new(schema, cols, None, None).unwrap();
let data = TabularData::new(storage);
assert_eq!(data.float64_values(VariableId::from_raw(0)).unwrap(), vec![1.5, 2.5]);
assert_eq!(data.float64_values(VariableId::from_raw(1)).unwrap(), vec![3.0, 4.0]);
assert_eq!(data.float64_values(VariableId::from_raw(2)).unwrap(), vec![1.0, 0.0]);
}
}