@@ -123,7 +123,8 @@ void chainFilterSelectCount() {
123123
124124 @ Test
125125 void methodsThrowAfterClose () {
126- try (SessionContext ctx = new SessionContext ()) {
126+ try (SessionContext ctx = new SessionContext ();
127+ DataFrame other = ctx .sql ("SELECT 1 AS x" )) {
127128 DataFrame df = ctx .sql ("SELECT 1 AS x" );
128129 df .close ();
129130 assertThrows (IllegalStateException .class , () -> df .select ("x" ));
@@ -135,6 +136,14 @@ void methodsThrowAfterClose() {
135136 assertThrows (IllegalStateException .class , () -> df .withColumnRenamed ("x" , "y" ));
136137 assertThrows (IllegalStateException .class , () -> df .withColumn ("y" , "x + 1" ));
137138 assertThrows (IllegalStateException .class , () -> df .unnestColumns ("x" ));
139+ assertThrows (IllegalStateException .class , () -> df .union (other ));
140+ assertThrows (IllegalStateException .class , () -> df .unionDistinct (other ));
141+ assertThrows (IllegalStateException .class , () -> df .unionByName (other ));
142+ assertThrows (IllegalStateException .class , () -> df .unionByNameDistinct (other ));
143+ assertThrows (IllegalStateException .class , () -> df .intersect (other ));
144+ assertThrows (IllegalStateException .class , () -> df .intersectDistinct (other ));
145+ assertThrows (IllegalStateException .class , () -> df .except (other ));
146+ assertThrows (IllegalStateException .class , () -> df .exceptDistinct (other ));
138147 assertThrows (IllegalStateException .class , df ::count );
139148 assertThrows (IllegalStateException .class , df ::show );
140149 assertThrows (IllegalStateException .class , () -> df .show (5 ));
@@ -145,7 +154,8 @@ void methodsThrowAfterClose() {
145154 void methodsThrowAfterCollect () throws Exception {
146155 try (BufferAllocator allocator = new RootAllocator ();
147156 SessionContext ctx = new SessionContext ();
148- DataFrame df = ctx .sql ("SELECT 1 AS x" )) {
157+ DataFrame df = ctx .sql ("SELECT 1 AS x" );
158+ DataFrame other = ctx .sql ("SELECT 1 AS x" )) {
149159 try (ArrowReader reader = df .collect (allocator )) {
150160 assertTrue (reader .loadNextBatch ());
151161 }
@@ -158,6 +168,14 @@ void methodsThrowAfterCollect() throws Exception {
158168 assertThrows (IllegalStateException .class , () -> df .withColumnRenamed ("x" , "y" ));
159169 assertThrows (IllegalStateException .class , () -> df .withColumn ("y" , "x + 1" ));
160170 assertThrows (IllegalStateException .class , () -> df .unnestColumns ("x" ));
171+ assertThrows (IllegalStateException .class , () -> df .union (other ));
172+ assertThrows (IllegalStateException .class , () -> df .unionDistinct (other ));
173+ assertThrows (IllegalStateException .class , () -> df .unionByName (other ));
174+ assertThrows (IllegalStateException .class , () -> df .unionByNameDistinct (other ));
175+ assertThrows (IllegalStateException .class , () -> df .intersect (other ));
176+ assertThrows (IllegalStateException .class , () -> df .intersectDistinct (other ));
177+ assertThrows (IllegalStateException .class , () -> df .except (other ));
178+ assertThrows (IllegalStateException .class , () -> df .exceptDistinct (other ));
161179 assertThrows (IllegalStateException .class , df ::count );
162180 assertThrows (IllegalStateException .class , df ::show );
163181 assertThrows (IllegalStateException .class , () -> df .show (5 ));
@@ -504,4 +522,150 @@ void unnestColumnsRejectsNullArgs() {
504522 () -> df .unnestColumns (new UnnestOptions (), (String []) null ));
505523 }
506524 }
525+
526+ // -- Set operations -------------------------------------------------------
527+
528+ @ Test
529+ void unionKeepsDuplicates () {
530+ try (SessionContext ctx = new SessionContext ();
531+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (2), (2)) AS t(x)" );
532+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (2), (3)) AS t(x)" );
533+ DataFrame combined = left .union (right )) {
534+ // union = UNION ALL: 3 + 2 = 5 rows, duplicates preserved.
535+ assertEquals (5L , combined .count ());
536+ }
537+ }
538+
539+ @ Test
540+ void unionDistinctRemovesDuplicates () {
541+ try (SessionContext ctx = new SessionContext ();
542+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (2), (2)) AS t(x)" );
543+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (2), (3)) AS t(x)" );
544+ DataFrame combined = left .unionDistinct (right )) {
545+ // unionDistinct = UNION (deduplicated): {1, 2, 3} = 3 rows.
546+ assertEquals (3L , combined .count ());
547+ }
548+ }
549+
550+ @ Test
551+ void unionByNameAlignsColumnsByName () {
552+ try (SessionContext ctx = new SessionContext ();
553+ // Left: (a, b). Right: (b, a) -- same names, swapped column positions.
554+ DataFrame left = ctx .sql ("SELECT 1 AS a, 2 AS b" );
555+ DataFrame right = ctx .sql ("SELECT 4 AS b, 3 AS a" );
556+ DataFrame combined = left .unionByName (right )) {
557+ // 2 rows: positional union would mix columns, but unionByName aligns them.
558+ assertEquals (2L , combined .count ());
559+ }
560+ }
561+
562+ @ Test
563+ void unionByNameDistinctRemovesDuplicates () {
564+ try (SessionContext ctx = new SessionContext ();
565+ DataFrame left = ctx .sql ("SELECT 1 AS a, 2 AS b" );
566+ DataFrame right = ctx .sql ("SELECT 2 AS b, 1 AS a" );
567+ DataFrame combined = left .unionByNameDistinct (right )) {
568+ // After name-aligning, both rows are (a=1, b=2). Distinct collapses to 1.
569+ assertEquals (1L , combined .count ());
570+ }
571+ }
572+
573+ @ Test
574+ void intersectKeepsCommonRowsWithDuplicates () {
575+ try (SessionContext ctx = new SessionContext ();
576+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (2), (2), (3)) AS t(x)" );
577+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (2), (2), (3)) AS t(x)" );
578+ DataFrame inter = left .intersect (right )) {
579+ // INTERSECT ALL: row r appears min(count(r) in left, count(r) in right) times.
580+ // 2 appears 2 times in both => 2 rows; 3 appears 1 time in both => 1 row. Total 3.
581+ assertEquals (3L , inter .count ());
582+ }
583+ }
584+
585+ @ Test
586+ void intersectDistinctReturnsUniqueCommonRows () {
587+ try (SessionContext ctx = new SessionContext ();
588+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (2), (2), (3)) AS t(x)" );
589+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (2), (2), (3)) AS t(x)" );
590+ DataFrame inter = left .intersectDistinct (right )) {
591+ // INTERSECT (deduplicated): {2, 3} = 2 rows.
592+ assertEquals (2L , inter .count ());
593+ }
594+ }
595+
596+ @ Test
597+ void exceptKeepsLeftMinusRightWithDuplicates () {
598+ try (SessionContext ctx = new SessionContext ();
599+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (1), (2), (2), (3)) AS t(x)" );
600+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (1), (3)) AS t(x)" );
601+ DataFrame diff = left .except (right )) {
602+ // DataFusion's EXCEPT ALL is implemented as a LeftAnti join, so a left row is kept iff
603+ // its key has no match in right. With left {1, 1, 2, 2, 3} and right {1, 3}: both 1s and
604+ // the 3 are dropped, both 2s are kept. Total 2 rows -- duplicates of 2 preserved.
605+ assertEquals (2L , diff .count ());
606+ }
607+ }
608+
609+ @ Test
610+ void exceptDistinctReturnsUniqueLeftOnlyRows () {
611+ try (SessionContext ctx = new SessionContext ();
612+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (1), (2), (2), (3)) AS t(x)" );
613+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (1), (3)) AS t(x)" );
614+ DataFrame diff = left .exceptDistinct (right )) {
615+ // EXCEPT (DISTINCT): left is deduplicated to {1, 2, 3}, then anti-joined against
616+ // right {1, 3}. Result: {2} = 1 row.
617+ assertEquals (1L , diff .count ());
618+ }
619+ }
620+
621+ @ Test
622+ void setOpsAreNonDestructive () {
623+ try (SessionContext ctx = new SessionContext ();
624+ DataFrame left = ctx .sql ("SELECT * FROM (VALUES (1), (2)) AS t(x)" );
625+ DataFrame right = ctx .sql ("SELECT * FROM (VALUES (2), (3)) AS t(x)" )) {
626+ try (DataFrame combined = left .union (right )) {
627+ assertEquals (4L , combined .count ());
628+ }
629+ // Both originals still usable after the set-op call.
630+ assertEquals (2L , left .count ());
631+ assertEquals (2L , right .count ());
632+ }
633+ }
634+
635+ @ Test
636+ void setOpsRejectNullOther () {
637+ try (SessionContext ctx = new SessionContext ();
638+ DataFrame df = ctx .sql ("SELECT 1 AS x" )) {
639+ assertThrows (IllegalArgumentException .class , () -> df .union (null ));
640+ assertThrows (IllegalArgumentException .class , () -> df .unionDistinct (null ));
641+ assertThrows (IllegalArgumentException .class , () -> df .unionByName (null ));
642+ assertThrows (IllegalArgumentException .class , () -> df .unionByNameDistinct (null ));
643+ assertThrows (IllegalArgumentException .class , () -> df .intersect (null ));
644+ assertThrows (IllegalArgumentException .class , () -> df .intersectDistinct (null ));
645+ assertThrows (IllegalArgumentException .class , () -> df .except (null ));
646+ assertThrows (IllegalArgumentException .class , () -> df .exceptDistinct (null ));
647+ }
648+ }
649+
650+ @ Test
651+ void setOpsRejectClosedOther () {
652+ try (SessionContext ctx = new SessionContext ();
653+ DataFrame df = ctx .sql ("SELECT 1 AS x" )) {
654+ DataFrame other = ctx .sql ("SELECT 1 AS x" );
655+ other .close ();
656+ assertThrows (IllegalStateException .class , () -> df .union (other ));
657+ assertThrows (IllegalStateException .class , () -> df .intersectDistinct (other ));
658+ assertThrows (IllegalStateException .class , () -> df .exceptDistinct (other ));
659+ }
660+ }
661+
662+ @ Test
663+ void unionWithIncompatibleSchemaThrows () {
664+ // Different column count -- LogicalPlanBuilder rejects at plan-build time.
665+ try (SessionContext ctx = new SessionContext ();
666+ DataFrame left = ctx .sql ("SELECT 1 AS x, 2 AS y" );
667+ DataFrame right = ctx .sql ("SELECT 1 AS x" )) {
668+ assertThrows (RuntimeException .class , () -> left .union (right ));
669+ }
670+ }
507671}
0 commit comments