-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove interior mutability of MemTable
#4514
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,15 @@ mod util; | |
|
||
use crate::error::Result; | ||
use crate::insert::util::LogicTestContextProvider; | ||
use arrow::record_batch::RecordBatch; | ||
use datafusion::datasource::MemTable; | ||
use datafusion::prelude::SessionContext; | ||
use datafusion_common::{DFSchema, DataFusionError}; | ||
use datafusion_expr::Expr as DFExpr; | ||
use datafusion_sql::planner::SqlToRel; | ||
use sqlparser::ast::{Expr, SetExpr, Statement as SQLStatement}; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
pub async fn insert(ctx: &SessionContext, insert_stmt: &SQLStatement) -> Result<String> { | ||
// First, use sqlparser to get table name and insert values | ||
|
@@ -52,14 +54,10 @@ pub async fn insert(ctx: &SessionContext, insert_stmt: &SQLStatement) -> Result< | |
_ => unreachable!(), | ||
} | ||
|
||
// Second, get table by table name | ||
// Here we assume table must be in memory table. | ||
let table_provider = ctx.table_provider(table_name.as_str())?; | ||
let table_batches = table_provider | ||
.as_any() | ||
.downcast_ref::<MemTable>() | ||
.unwrap() | ||
.get_batches(); | ||
// Second, get batches in table and destroy the old table | ||
let mut origin_batches = ctx.table(table_name.as_str())?.collect().await?; | ||
let schema = ctx.table_provider(table_name.as_str())?.schema(); | ||
ctx.deregister_table(table_name.as_str())?; | ||
|
||
// Third, transfer insert values to `RecordBatch` | ||
// Attention: schema info can be ignored. (insert values don't contain schema info) | ||
|
@@ -74,12 +72,20 @@ pub async fn insert(ctx: &SessionContext, insert_stmt: &SQLStatement) -> Result< | |
.collect::<std::result::Result<Vec<DFExpr>, DataFusionError>>()?; | ||
// Directly use `select` to get `RecordBatch` | ||
let dataframe = ctx.read_empty()?; | ||
insert_batches.push(dataframe.select(logical_exprs)?.collect().await?) | ||
insert_batches.extend(dataframe.select(logical_exprs)?.collect().await?) | ||
} | ||
|
||
// Final, append the `RecordBatch` to memtable's batches | ||
let mut table_batches = table_batches.write(); | ||
table_batches.extend(insert_batches); | ||
// Replace new batches schema to old schema | ||
for batch in insert_batches.iter_mut() { | ||
origin_batches.push(RecordBatch::try_new( | ||
schema.clone(), | ||
batch.columns().to_vec(), | ||
)?); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ideal way should be concating columns of But seems arrow-rs doesn't provide similar APIs, so I just use the current way. |
||
} | ||
|
||
// Final, create new memtable with same schema. | ||
let new_provider = MemTable::try_new(schema, vec![origin_batches])?; | ||
ctx.register_table(table_name.as_str(), Arc::new(new_provider))?; | ||
|
||
Ok("".to_string()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if you could make this
And avoid the need for
insert_batches
entirely 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, nice suggestion!