-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support insert into
statement in sqllogictest
#4496
Conversation
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 think this is great -- thank you @xudong963.
I suggest we try and avoid the changes to add interior mutability to MemTable if we can avoid it (I left a suggestion below) and I am happy to help make this happen if need be.
But all in all this is wonderful and I think we could merge this PR in as is and iterate on master as well
@@ -958,6 +958,22 @@ impl SessionContext { | |||
} | |||
} | |||
|
|||
/// Return a [`TabelProvider`] for the specified table. | |||
pub fn table_provider<'a>( |
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.
👍 it might be nice to refactor fn table()
to call this function now to avoid some duplication.
|
||
/// DataFusion sql-logicaltest error | ||
#[derive(Debug)] | ||
pub enum DFSqlLogicTestError { |
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.
👍 this is a good idea
NotImplemented(String), | ||
/// Error returned from DFSqlLogicTest inner | ||
Internal(String), |
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 suggest we simply panic in the sqllogic runner in these cases so the location of the error is easier to see
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.
LGTM
// Check if the sql is `insert` | ||
if sql.trim_start().to_lowercase().starts_with("insert") { | ||
// Process the insert statement | ||
insert(ctx, sql).await?; | ||
return Ok("".to_string()); | ||
} |
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 like this basic approach (special case the sql and route it to the test runner implementation).
One thing that might be worth doing is to actually try and parse the input into sql, to detect INSERT statements though I think string manipulation is fine too or we could do this later
// Handle any test only special case statements
let sql = sql.into();
match DFParser::parse_sql(&sql) {
Ok(Statement(Insert)) => {
//debug!("Parsed statement: {:#?}", stmt);
}
Err(_) => {
// ignore anything else, including errors -- they will be handled by the sql context below
}
};
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.
Yes, more generic way, will fix in next PR.
11 20 | ||
|
||
# Test insert into a undefined table | ||
statement error |
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.
👍
# under the License. | ||
|
||
statement ok | ||
CREATE TABLE users AS VALUES(1,2),(2,3); |
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.
// Todo: check columns match table schema | ||
table_name = name.to_string(); | ||
match &*source.body { | ||
SetExpr::Values(values) => { |
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.
This is very clever
} | ||
|
||
// Final, append the `RecordBatch` to memtable's batches | ||
let mut table_batches = table_batches.write(); |
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.
Rather than changing the batches in the existing memtable, what would you think about creating a new memtable with the same name with the new values (rather than modifying the original one)
I think you might be able to avoid changes to SessionContext and MemTable entirely.
Something like this (untested)
// fetch existing batches
let mut existing_batches = ctx.table(table_name.as_str()).collect();
// append new batch
exsiting_batches.extend(insert_batches)
// Replace table provider provider
let new_provider = MemTable::try_new(batches[0].schema(), vec![batches]);
ctx.register_table(table_name, new_provider)
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.
Yes, I have thought about the way you mentioned, (also need to delete original memtable).
-- But for performance reasons, I choose the current way: modifying the original one.
If you think the changes to add interior mutability to MemTable don't make sense, I can change it in the following ticket! (I don't have a strong bias for both ways)
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 think keeping MemTable as simple as possible is likely the best approach -- so for that reason I prefer to remove the interior mutability.
I can give it a shot if you agree -- I think the performance of copying record batches (for reasonably small data such as what is in the test) will be ok
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.
Ok, agree! Thanks @alamb . I'll refactor it 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.
Thank you very much!
Benchmark runs are scheduled for baseline = df41267 and contender = b5b50ba. b5b50ba is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
Which issue does this PR close?
Closes #4397
Rationale for this change
Support initial
insert into
statement in sqllogictest framework which will help a lot migrate tests!What changes are included in this PR?
insert into
statement.insert into
sqllogictest.Are these changes tested?
Yes
Are there any user-facing changes?
No