-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy path05_scan_options.rs
More file actions
179 lines (168 loc) · 5.25 KB
/
05_scan_options.rs
File metadata and controls
179 lines (168 loc) · 5.25 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
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
169
170
171
172
173
174
175
176
177
178
179
//! Scan options: projection, limit, and combined queries
//!
//! Run: cargo run --example 05_scan_options
use std::sync::Arc;
use arrow_schema::{DataType, Field, Schema};
use tonbo::prelude::*;
#[derive(Record)]
struct Order {
#[metadata(k = "tonbo.key", v = "true")]
id: String,
customer: String,
product: String,
quantity: i64,
price: i64,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db = DbBuilder::from_schema(Order::schema())?
.on_disk("/tmp/tonbo_scan_options")?
.open()
.await?;
// Insert sample data
let orders = vec![
Order {
id: "o1".into(),
customer: "Alice".into(),
product: "Laptop".into(),
quantity: 1,
price: 999,
},
Order {
id: "o2".into(),
customer: "Bob".into(),
product: "Mouse".into(),
quantity: 2,
price: 29,
},
Order {
id: "o3".into(),
customer: "Alice".into(),
product: "Keyboard".into(),
quantity: 1,
price: 79,
},
Order {
id: "o4".into(),
customer: "Carol".into(),
product: "Monitor".into(),
quantity: 1,
price: 299,
},
Order {
id: "o5".into(),
customer: "Bob".into(),
product: "Headphones".into(),
quantity: 1,
price: 149,
},
Order {
id: "o6".into(),
customer: "Alice".into(),
product: "Webcam".into(),
quantity: 1,
price: 89,
},
];
let mut builders = Order::new_builders(orders.len());
builders.append_rows(orders);
db.ingest(builders.finish().into_record_batch()).await?;
// 1. Limit: get first 3 orders
println!("1. LIMIT 3:");
let batches = db.scan().limit(3).collect().await?;
for batch in &batches {
for o in batch.iter_views::<Order>()?.try_flatten()? {
println!(" {} - {} bought {}", o.id, o.customer, o.product);
}
}
// 2. Projection: select only id and customer columns
println!("\n2. SELECT id, customer:");
let projected_schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Utf8, false),
Field::new("customer", DataType::Utf8, false),
]));
let batches = db.scan().projection(projected_schema).collect().await?;
// Note: with projection, we read raw columns instead of typed views
for batch in &batches {
let ids = batch
.column(0)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.unwrap();
let customers = batch
.column(1)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.unwrap();
for i in 0..batch.num_rows() {
println!(" {} - {}", ids.value(i), customers.value(i));
}
}
// 3. Filter + Limit: high price orders, max 2
println!("\n3. WHERE price > 100 LIMIT 2:");
let filter = Expr::gt("price", ScalarValue::from(100_i64));
let batches = db.scan().filter(filter).limit(2).collect().await?;
for batch in &batches {
for o in batch.iter_views::<Order>()?.try_flatten()? {
println!(" {} - {} (${}) ", o.id, o.product, o.price);
}
}
// 4. Filter + Projection: high-value orders, show only id and price
println!("\n4. SELECT id, price WHERE price > 100:");
let filter = Expr::gt("price", ScalarValue::from(100_i64));
let projected_schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Utf8, false),
Field::new("price", DataType::Int64, false),
]));
let batches = db
.scan()
.filter(filter)
.projection(projected_schema)
.collect()
.await?;
for batch in &batches {
let ids = batch
.column(0)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.unwrap();
let prices = batch
.column(1)
.as_any()
.downcast_ref::<arrow_array::Int64Array>()
.unwrap();
for i in 0..batch.num_rows() {
println!(" {} - ${}", ids.value(i), prices.value(i));
}
}
// 5. All combined: Filter + Projection + Limit
println!("\n5. SELECT id, product WHERE quantity = 1 LIMIT 3:");
let filter = Expr::eq("quantity", ScalarValue::from(1_i64));
let projected_schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Utf8, false),
Field::new("product", DataType::Utf8, false),
]));
let batches = db
.scan()
.filter(filter)
.projection(projected_schema)
.limit(3)
.collect()
.await?;
for batch in &batches {
let ids = batch
.column(0)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.unwrap();
let products = batch
.column(1)
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.unwrap();
for i in 0..batch.num_rows() {
println!(" {} - {}", ids.value(i), products.value(i));
}
}
Ok(())
}