Run a simple query against a [`MemTable`]
()
| 43 | |
| 44 | /// Run a simple query against a [`MemTable`] |
| 45 | pub async fn query_memtable() -> Result<()> { |
| 46 | let mem_table = create_memtable()?; |
| 47 | |
| 48 | // create local execution context |
| 49 | let ctx = SessionContext::new(); |
| 50 | |
| 51 | // Register the in-memory table containing the data |
| 52 | ctx.register_table("users", Arc::new(mem_table))?; |
| 53 | |
| 54 | // running a SQL query results in a "DataFrame", which can be used |
| 55 | // to execute the query and collect the results |
| 56 | let dataframe = ctx.sql("SELECT * FROM users;").await?; |
| 57 | |
| 58 | // Calling 'show' on the dataframe will execute the query and |
| 59 | // print the results |
| 60 | dataframe.clone().show().await?; |
| 61 | |
| 62 | // calling 'collect' on the dataframe will execute the query and |
| 63 | // buffer the results into a vector of RecordBatch. There are other |
| 64 | // APIs on DataFrame for incrementally generating results (e.g. streaming) |
| 65 | let result = dataframe.collect().await?; |
| 66 | |
| 67 | // Use the assert_batches_eq macro to compare the results |
| 68 | assert_batches_eq!( |
| 69 | [ |
| 70 | "+----+--------------+", |
| 71 | "| id | bank_account |", |
| 72 | "+----+--------------+", |
| 73 | "| 1 | 9000 |", |
| 74 | "+----+--------------+", |
| 75 | ], |
| 76 | &result |
| 77 | ); |
| 78 | |
| 79 | Ok(()) |
| 80 | } |
| 81 | |
| 82 | fn create_memtable() -> Result<MemTable> { |
| 83 | MemTable::try_new(get_schema(), vec![vec![create_record_batch()?]]) |
no test coverage detected
searching dependent graphs…