You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "ifsheldon (via GitHub)" <gi...@apache.org> on 2025/11/18 01:35:49 UTC
Re: [D] How can I construct a table with a field that is List of String? [datafusion]
GitHub user ifsheldon closed a discussion: How can I construct a table with a field that is List of String?
Hi, I am new to Datafusion, and I want to use it as an in-memory DB. I have read the doc and it seems I can construct a table in which a field, say `properties` can be an array/list of strings.
I try to modify this example https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/dataframe_in_memory.rs but I don't know how to insert the data of a list of lists of strings into the table.
I cannot find something useful in the documentation. Can anyone help? Or, if you have pointers to related info, that would be great!
Here is my code.
```rust
#[cfg(test)]
mod test {
use std::sync::Arc;
use datafusion::arrow::array::{Int32Array, ListArray, StringArray};
use datafusion::arrow::datatypes::{DataType, Field, Schema, Utf8Type};
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::error::Result;
use datafusion::prelude::*;
#[tokio::test]
async fn test_datafustion() -> Result<()> {
// define a schema.
let schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Utf8, false),
Field::new_list("properties", Arc::new(Field::new("property", DataType::Utf8, false)), true),
]));
// define data. Error Here
let batch = RecordBatch::try_new(
schema,
vec![
Arc::new(StringArray::from(vec!["a", "b", "c", "d"])),
// according to the code sample in the doc of ListArray::from_iter_primitive, seems type mismatched
Arc::new(ListArray::from_iter_primitive(vec![
Some(vec![Some("a"), Some("a")]),
Some(vec![Some("b"), Some("b")]),
Some(vec![Some("c"), Some("c")]),
Some(vec![Some("d"), Some("d")]),
])),
],
)?;
let ctx = SessionContext::new();
ctx.register_batch("t", batch)?;
let df = ctx.table("t").await?;
// find a row in which properties contains "b"
let filter = array_has(col("properties"), lit("b"));
let df = df.select_columns(&["id", "properties"])?.filter(filter)?;
df.show().await?;
Ok(())
}
}
```
GitHub link: https://github.com/apache/datafusion/discussions/12933
----
This is an automatically sent email for github@datafusion.apache.org.
To unsubscribe, please send an email to: github-unsubscribe@datafusion.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org