Skip to content

Commit

Permalink
sql: add parsing delete statement (#923)
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Obuchowski <obuchowski.maciej@gmail.com>
  • Loading branch information
mobuchowski committed Jul 18, 2022
1 parent a34b3a3 commit d37b770
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
17 changes: 17 additions & 0 deletions integration/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ fn parse_stmt(stmt: &Statement, context: &mut Context) -> Result<(), String> {
}
Ok(())
}
Statement::Delete {
table_name,
using,
selection,
} => {
let table_name = get_table_name_from_table_factor(table_name)?;
context.add_output(&table_name.to_string());

if let Some(using) = using {
parse_table_factor(using, context)?;
}

if let Some(expr) = selection {
parse_expr(expr, context)?;
}
Ok(())
}
_ => Ok(()),
}
}
Expand Down
5 changes: 3 additions & 2 deletions integration/sql/tests/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use openlineage_sql::{get_dialect, get_generic_dialect, parse_sql, parse_multiple_statements, DbTableMeta, SqlMeta};
use openlineage_sql::{
get_dialect, get_generic_dialect, parse_multiple_statements, parse_sql, DbTableMeta, SqlMeta,
};
use sqlparser::dialect::PostgreSqlDialect;

pub fn test_sql(sql: &str) -> SqlMeta {
Expand All @@ -19,7 +21,6 @@ pub fn test_multiple_sql_dialect(sqls: Vec<&str>, dialect: &str) -> SqlMeta {
}
}


pub fn test_sql_dialect(sql: &str, dialect: &str) -> SqlMeta {
match parse_sql(sql, get_dialect(dialect), None) {
Ok(meta) => meta,
Expand Down
6 changes: 3 additions & 3 deletions integration/sql/tests/tests_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use test_utils::*;
#[test]
fn parse_copy_from() {
assert_eq!(
parse_sql("
parse_sql(
"
COPY INTO SCHEMA.SOME_MONITORING_SYSTEM
FROM (
SELECT
Expand Down Expand Up @@ -47,8 +48,7 @@ fn parse_copy_from() {
Arc::new(SnowflakeDialect {}),
None
)
.unwrap_err(),
.unwrap_err(),
"sql parser error: Expected FROM or TO, found: SCHEMA"
)
}

7 changes: 5 additions & 2 deletions integration/sql/tests/tests_cte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ fn parse_recursive_cte() {
#[test]
fn multiple_ctes() {
assert_eq!(
test_sql("
test_sql(
"
WITH customers AS (
SELECT * FROM DEMO_DB.public.stg_customers
),
Expand All @@ -99,7 +100,9 @@ fn multiple_ctes() {
FROM customers c
JOIN orders o
ON c.id = o.customer_id
"), SqlMeta {
"
),
SqlMeta {
in_tables: tables(vec![
"DEMO_DB.public.stg_customers",
"DEMO_DB.public.stg_orders"
Expand Down
38 changes: 38 additions & 0 deletions integration/sql/tests/tests_delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018-2022 contributors to the OpenLineage project
// SPDX-License-Identifier: Apache-2.0

#[macro_use]
mod test_utils;

use openlineage_sql::SqlMeta;
use test_utils::*;

#[test]
fn delete_from() {
assert_eq!(
test_sql("DELETE FROM a.b WHERE x = 0",),
SqlMeta {
in_tables: vec![],
out_tables: table("a.b")
}
);
}

#[test]
fn delete_from_using() {
assert_eq!(
test_sql(
"DELETE FROM a.b AS t
USING (
SELECT col
FROM b.c
WHERE col = 'x'
) AS duplicates
WHERE a.b.col = duplicates.col",
),
SqlMeta {
in_tables: table("b.c"),
out_tables: table("a.b")
}
);
}

0 comments on commit d37b770

Please sign in to comment.