Skip to content

Conversation

@jonathanc-n
Copy link
Contributor

Which issue does this PR close?

What changes are included in this PR?

Added parsing for OR REPLACE in create external tables + functionality for execution

Are these changes tested?

Yes slt tests and unit tests for the parser

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) catalog Related to the catalog crate proto Related to proto crate labels Sep 16, 2025
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.

Thank you @jonathanc-n -- I think this is very close.

I think it needs a few more tests and a small fix to the creation logic but then it will be good to go.

Thanks again

match cmd.if_not_exists {
true => return self.return_empty_dataframe(),
false => {
match (cmd.if_not_exists, cmd.or_replace) {
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 it makes sense to make this behavior consistent with create_memory_table

self.register_table(cmd.name.clone(), table_provider)?;
return self.return_empty_dataframe();
}
_ => return exec_err!("View '{}' doesn't exist.", cmd.name),
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 the idea is that CREATE OR REPLACE EXTERNA TABLE ... will succeed regardless of if the table already exists (that is what create_memory_table) does. Can you please make this similarly consistent?

Copy link
Contributor Author

@jonathanc-n jonathanc-n Sep 16, 2025

Choose a reason for hiding this comment

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

This entire clause is wrapped by an exist flag, so it shouldnt be able to return Ok(false). I will refactor the whole logic to be similar to how memory table handles the flags to make it more clear

Copy link
Contributor Author

@jonathanc-n jonathanc-n Sep 16, 2025

Choose a reason for hiding this comment

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

I added the full code functionality for each branch to make it more clear

statement error Execution error: 'IF NOT EXISTS' cannot coexist with 'REPLACE'
CREATE OR REPLACE TABLE IF NOT EXISTS table_without_values(field1 BIGINT, field2 BIGINT);

# CREATE OR REPLACE
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also please test:

  1. Using CREATE OR REPLACE when the table doesn't already exist
  2. Using CREATE OR REPLACE to replace the same name with a different definition (e.g. a parquet file rather than CSV) and show it is different?

@jonathanc-n
Copy link
Contributor Author

Added the extra tests + clarified the logic! Should be good for another look

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.

looks great -- thank you so much @jonathanc-n

@blaginin
Copy link
Collaborator

Merge wasn't working at the time of review but it is! Going to merge if there are no objections

@alamb
Copy link
Contributor

alamb commented Sep 17, 2025

Let's do it @blaginin !

@blaginin blaginin added this pull request to the merge queue Sep 17, 2025
Merged via the queue into apache:main with commit da89395 Sep 17, 2025
28 checks passed
@alamb
Copy link
Contributor

alamb commented Sep 17, 2025

gogogogo -- the code is flowing now. Love it

@blaginin
Copy link
Collaborator

maybe we should do it more often haha 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

catalog Related to the catalog crate core Core DataFusion crate logical-expr Logical plan and expressions proto Related to proto crate sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support OR REPLACE for CREATE EXTERNAL TABLE

3 participants