Skip to content
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

Merged
merged 2 commits into from
Dec 4, 2022

Conversation

xudong963
Copy link
Member

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?

  1. Add Error handle.
  2. Add insert into statement.
  3. Add insert into sqllogictest.

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added the core Core DataFusion crate label Dec 4, 2022
@xudong963 xudong963 requested a review from alamb December 4, 2022 07:26
@xudong963
Copy link
Member Author

cc @mvanschellebeeck

@xudong963 xudong963 added the sqllogictest SQL Logic Tests (.slt) label Dec 4, 2022
Copy link
Contributor

@alamb alamb left a 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>(
Copy link
Contributor

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 {
Copy link
Contributor

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

Comment on lines +38 to +40
NotImplemented(String),
/// Error returned from DFSqlLogicTest inner
Internal(String),
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +144 to +149
// 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());
}
Copy link
Contributor

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
        }
    };

Copy link
Member Author

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
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its-so-beautiful-crying-gif

// Todo: check columns match table schema
table_name = name.to_string();
match &*source.body {
SetExpr::Values(values) => {
Copy link
Contributor

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();
Copy link
Contributor

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)

Copy link
Member Author

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)

Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!

@xudong963
Copy link
Member Author

@alamb I'll merge it first and do some changes in next ticket. We can avoid some conflicts, such as #4497

@xudong963 xudong963 merged commit b5b50ba into apache:master Dec 4, 2022
@xudong963 xudong963 deleted the insert branch December 4, 2022 13:18
@ursabot
Copy link

ursabot commented Dec 4, 2022

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.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support INSERT INTO statement
3 participants