@@ -3,13 +3,17 @@ use std::collections::HashMap;
33use sqlquack_syntax:: Dialect ;
44
55use crate :: dialects:: constants:: POSTGRES ;
6- use crate :: lineage:: models:: TransformKind ;
6+ use crate :: lineage:: main:: statement_lineage:: statement_lineage;
7+ use crate :: lineage:: models:: { LineageContext , Nullability , SchemaColumn , TransformKind } ;
78use crate :: lineage:: tests:: helpers:: output_ranges;
89use crate :: lineage:: tests:: helpers:: { duckdb_shop_schema, typed_column, typed_duckdb_shop_schema} ;
10+ use crate :: lineage:: tests:: test_types:: DialectTypeTestCase ;
911use crate :: lineage:: tests:: test_types:: QueryLineageTestCase ;
12+ use crate :: lineage:: tests:: test_types:: StarOrderTestCase ;
1013use crate :: lineage:: tests:: test_types:: StrictRangeTestCase ;
1114use crate :: lineage:: tests:: test_types:: TypedShopSchema ;
1215use crate :: postgres:: profile:: tests:: helpers:: lineage_outputs;
16+ use crate :: postgres:: profile:: tests:: helpers:: native_postgres_statement;
1317
1418#[ test]
1519fn given_unaliased_postgres_outputs_when_tracing_lineage_then_each_takes_figurecolnames_name ( ) {
@@ -276,3 +280,86 @@ fn given_a_strict_parse_when_tracing_lineage_then_each_output_has_its_items_byte
276280 ) ;
277281 }
278282}
283+
284+ #[ test]
285+ fn given_joined_and_comma_items_when_expanding_a_star_then_each_group_places_its_own_keys ( ) {
286+ let test_cases = [
287+ StarOrderTestCase {
288+ description : "a USING key first in its own comma group, after the earlier item" ,
289+ sql : "SELECT * FROM customers a, customers b JOIN customers c USING (name)" ,
290+ expected_names : & [
291+ "id" , "name" , "country" , "name" , "id" , "country" , "id" , "country" ,
292+ ] ,
293+ } ,
294+ StarOrderTestCase {
295+ description : "a nested USING key once, first" ,
296+ sql : "SELECT * FROM customers a JOIN customers b JOIN customers c USING (name) USING (name)" ,
297+ expected_names : & [ "name" , "id" , "country" , "id" , "country" , "id" , "country" ] ,
298+ } ,
299+ ] ;
300+ let schema = duckdb_shop_schema ( ) ;
301+ for test_case in test_cases {
302+ let context = LineageContext {
303+ profile : & POSTGRES ,
304+ schema : & schema,
305+ sql : Some ( test_case. sql ) ,
306+ } ;
307+ let names: Vec < String > =
308+ statement_lineage ( & native_postgres_statement ( test_case. sql ) , context)
309+ . outputs
310+ . into_iter ( )
311+ . map ( |output| output. name )
312+ . collect ( ) ;
313+ assert_eq ! ( names, test_case. expected_names, "{}" , test_case. description) ;
314+ }
315+ }
316+
317+ #[ test]
318+ fn given_an_outer_join_after_a_comma_when_tracing_nullability_then_only_its_group_is_nulled ( ) {
319+ let test_cases = [ DialectTypeTestCase {
320+ description : "a RIGHT JOIN nulls only its own group's left side" ,
321+ sql : "SELECT a.name, b.status, c.name FROM customers a, orders b RIGHT JOIN customers c ON b.id = c.id" ,
322+ dialect : Dialect :: PostgreSQL ,
323+ profile : & POSTGRES ,
324+ expected_outputs : & [
325+ ( "name" , Some ( "TEXT" ) , Nullability :: NonNull ) ,
326+ ( "status" , Some ( "TEXT" ) , Nullability :: Nullable ) ,
327+ ( "name" , Some ( "TEXT" ) , Nullability :: NonNull ) ,
328+ ] ,
329+ } ] ;
330+ let column = |name : & str | SchemaColumn {
331+ nullability : Nullability :: NonNull ,
332+ ..typed_column ( name, "TEXT" )
333+ } ;
334+ let schema = TypedShopSchema {
335+ closed : HashMap :: from ( [
336+ ( "orders" . to_owned ( ) , vec ! [ column( "id" ) , column( "status" ) ] ) ,
337+ ( "customers" . to_owned ( ) , vec ! [ column( "id" ) , column( "name" ) ] ) ,
338+ ] ) ,
339+ ..TypedShopSchema :: default ( )
340+ } ;
341+ for test_case in test_cases {
342+ let context = LineageContext {
343+ profile : test_case. profile ,
344+ schema : & schema,
345+ sql : Some ( test_case. sql ) ,
346+ } ;
347+ let lineage = statement_lineage ( & native_postgres_statement ( test_case. sql ) , context) ;
348+ let outputs: Vec < ( & str , Option < & str > , Nullability ) > = lineage
349+ . outputs
350+ . iter ( )
351+ . map ( |output| {
352+ (
353+ output. name . as_str ( ) ,
354+ output. data_type . as_deref ( ) ,
355+ output. nullability ,
356+ )
357+ } )
358+ . collect ( ) ;
359+ assert_eq ! (
360+ outputs, test_case. expected_outputs,
361+ "{}" ,
362+ test_case. description
363+ ) ;
364+ }
365+ }
0 commit comments