Collecting column data from a ColumnarRowset into a vector of values #82
Replies: 1 comment 10 replies
-
Hello @waynehsmith , thanks for giving this crate a spin. What you are trying to do is likely not supported well at the moment, depending on what exactly you are trying to achieve it's maybe still possible though. It will however at least be akward at until row wise bulk fetch is implemented. So let's start with That being said there are three things, which might work for you.
Kindly let me know if I understood your question correctly and if any of this is helpful to you. If not please let me know more about your usecase. let insert_sql = format!("INSERT INTO {} (a,b) VALUES ('A', 1), ('B',2)", table_name);
conn.execute(&insert_sql, ()).unwrap();
// Now that the table is created and filled with some values lets query it and put its contents
// into a `Vec`
let query_sql = format!("SELECT a,b FROM {}", table_name);
let cursor = conn.execute(&query_sql, ()).unwrap().unwrap();
let buf_desc = [
BufferDescription {
nullable: true,
kind: BufferKind::Text { max_str_len: 50 },
},
BufferDescription {
nullable: false,
kind: BufferKind::I32,
},
];
let buffer = ColumnarRowSet::new(1, buf_desc.iter().copied());
let mut cursor = cursor.bind_buffer(buffer).unwrap();
let mut actual = Vec::new();
while let Some(batch) = cursor.fetch().unwrap() {
// Extract first column known to contain text
let mut col_a = match batch.column(0) {
AnyColumnView::Text(col) => col,
_ => panic!("First column is supposed to be text"),
};
// Extract second column known to contain non nullable i32
let col_b = match batch.column(1) {
AnyColumnView::I32(col) => col,
_ => panic!("Secord column is supposed to be an i32"),
};
for row in 0..batch.num_rows() {
let a = col_a
.next()
.unwrap()
.map(|bytes| str::from_utf8(bytes).unwrap().to_owned());
let b = col_b[row];
actual.push((a, b))
}
}
assert_eq!(
actual,
[(Some("A".to_string()), 1), (Some("B".to_string()), 2)]
)
} |
Beta Was this translation helpful? Give feedback.
-
Since AnyColumnView does not implement the Clone trait, is there another recommended mechanism to collect the values of a row into a vector?
Beta Was this translation helpful? Give feedback.
All reactions