-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdemo.rs
More file actions
74 lines (66 loc) · 2.12 KB
/
Copy pathdemo.rs
File metadata and controls
74 lines (66 loc) · 2.12 KB
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
use std::sync::Arc;
use anyhow::Result;
use arrow::array::{Array, Float32Array, ListArray};
use datafusion::prelude::{CsvReadOptions, SessionContext};
use model::OnnxModelRegistry;
#[tokio::main]
async fn main() -> Result<()> {
// 1. Create a DataFusion context
let mut ctx = SessionContext::new();
// 2. Register the ETTh1 CSV as a table named "etth1"
ctx.register_csv(
"etth1",
"data/ETTh1.csv",
CsvReadOptions::new().has_header(true),
)
.await?;
// 3. Register the onnx_predict UDF (no model pre-registration needed)
let registry = Arc::new(OnnxModelRegistry::new());
registry.register_onnx_predict_udf(&mut ctx);
// 4. Build SQL: model path inline, loaded lazily on first call
let sql = r#"
SELECT
onnx_predict(
'models/TinyTimeMixer.onnx',
array_agg(etth1."HUFL"),
array_agg(etth1."HULL"),
array_agg(etth1."MUFL"),
array_agg(etth1."MULL"),
array_agg(etth1."LUFL"),
array_agg(etth1."LULL"),
array_agg(etth1."OT")
) AS forecast
FROM (
SELECT *
FROM etth1
LIMIT 512
) etth1
"#;
// 5. Execute the query
let df = ctx.sql(sql).await?;
let batches = df.collect().await?;
// 6. Extract and print the prediction array
for batch in &batches {
let col = batch
.column(batch.schema().index_of("forecast")?)
.as_any()
.downcast_ref::<ListArray>()
.expect("Forecast column should be ListArray");
if col.len() > 0 {
let forecast_array = col.value(0);
if let Some(float_arr) = forecast_array.as_any().downcast_ref::<Float32Array>() {
println!(
"Forecast values (length {}): {:?}",
float_arr.len(),
float_arr.values()
);
} else {
println!(
"Unexpected forecast array type: {:?}",
forecast_array.data_type()
);
}
}
}
Ok(())
}