Skip to content

Commit

Permalink
chore: remove unnecessary query in snowflake (#2099)
Browse files Browse the repository at this point in the history
Merge two queries, `EXISTS` and `CREATE`, into a single query `CREATE IF NOT EXISTS`.
  • Loading branch information
abcpro1 authored Sep 28, 2023
1 parent 72de18d commit 41c2c49
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
7 changes: 0 additions & 7 deletions dozer-ingestion/src/connectors/snowflake/connection/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,6 @@ impl<'env> Client<'env> {
exec_first_exists(&self.pool, &query).map_or_else(Self::parse_not_exist_error, Ok)
}

pub fn table_exist(&self, table_name: &String) -> Result<bool, SnowflakeError> {
let query =
format!("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{table_name}';");

exec_first_exists(&self.pool, &query).map_or_else(Self::parse_not_exist_error, Ok)
}

pub fn drop_stream(&self, stream_name: &String) -> Result<bool, SnowflakeError> {
let query = format!("DROP STREAM IF EXISTS {stream_name}");

Expand Down
11 changes: 4 additions & 7 deletions dozer-ingestion/src/connectors/snowflake/stream_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,13 @@ impl StreamConsumer {
) -> Result<(), ConnectorError> {
let temp_table_name = Self::get_stream_temp_table_name(table_name, &client.get_name());
let stream_name = Self::get_stream_table_name(table_name, &client.get_name());
let temp_table_exist = client.table_exist(&temp_table_name)?;

if !temp_table_exist {
let query = format!(
"CREATE OR REPLACE TEMP TABLE {temp_table_name} AS
let query = format!(
"CREATE TEMP TABLE IF NOT EXISTS {temp_table_name} AS
SELECT * FROM {stream_name} ORDER BY METADATA$ACTION;"
);
);

client.exec(&query)?;
}
client.exec(&query)?;

let rows = client.fetch(format!("SELECT * FROM {temp_table_name};"))?;
if let Some(schema) = rows.schema() {
Expand Down

0 comments on commit 41c2c49

Please sign in to comment.