@@ -133,6 +133,8 @@ void methodsThrowAfterClose() {
133133 assertThrows (IllegalStateException .class , df ::distinct );
134134 assertThrows (IllegalStateException .class , () -> df .dropColumns ("x" ));
135135 assertThrows (IllegalStateException .class , () -> df .withColumnRenamed ("x" , "y" ));
136+ assertThrows (IllegalStateException .class , () -> df .withColumn ("y" , "x + 1" ));
137+ assertThrows (IllegalStateException .class , () -> df .unnestColumns ("x" ));
136138 assertThrows (IllegalStateException .class , df ::count );
137139 assertThrows (IllegalStateException .class , df ::show );
138140 assertThrows (IllegalStateException .class , () -> df .show (5 ));
@@ -154,6 +156,8 @@ void methodsThrowAfterCollect() throws Exception {
154156 assertThrows (IllegalStateException .class , df ::distinct );
155157 assertThrows (IllegalStateException .class , () -> df .dropColumns ("x" ));
156158 assertThrows (IllegalStateException .class , () -> df .withColumnRenamed ("x" , "y" ));
159+ assertThrows (IllegalStateException .class , () -> df .withColumn ("y" , "x + 1" ));
160+ assertThrows (IllegalStateException .class , () -> df .unnestColumns ("x" ));
157161 assertThrows (IllegalStateException .class , df ::count );
158162 assertThrows (IllegalStateException .class , df ::show );
159163 assertThrows (IllegalStateException .class , () -> df .show (5 ));
@@ -353,4 +357,152 @@ void withColumnRenamedUnknownColumnIsNoOp() throws Exception {
353357 root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
354358 }
355359 }
360+
361+ @ Test
362+ void withColumnAppendsNewColumn () throws Exception {
363+ try (BufferAllocator allocator = new RootAllocator ();
364+ SessionContext ctx = new SessionContext ();
365+ DataFrame source = ctx .sql ("SELECT 1 AS a, 2 AS b" );
366+ DataFrame extended = source .withColumn ("c" , "a + b" );
367+ ArrowReader reader = extended .collect (allocator )) {
368+ assertTrue (reader .loadNextBatch ());
369+ VectorSchemaRoot root = reader .getVectorSchemaRoot ();
370+ assertEquals (1 , root .getRowCount ());
371+ assertArrayEquals (
372+ new String [] {"a" , "b" , "c" },
373+ root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
374+ assertEquals (3L , ((BigIntVector ) root .getVector ("c" )).get (0 ));
375+ }
376+ }
377+
378+ @ Test
379+ void withColumnReplacesExistingColumn () throws Exception {
380+ try (BufferAllocator allocator = new RootAllocator ();
381+ SessionContext ctx = new SessionContext ();
382+ DataFrame source = ctx .sql ("SELECT 1 AS a, 2 AS b" );
383+ DataFrame replaced = source .withColumn ("b" , "a * 10" );
384+ ArrowReader reader = replaced .collect (allocator )) {
385+ assertTrue (reader .loadNextBatch ());
386+ VectorSchemaRoot root = reader .getVectorSchemaRoot ();
387+ assertArrayEquals (
388+ new String [] {"a" , "b" },
389+ root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
390+ assertEquals (10L , ((BigIntVector ) root .getVector ("b" )).get (0 ));
391+ }
392+ }
393+
394+ @ Test
395+ void withColumnIsNonDestructive () {
396+ try (SessionContext ctx = new SessionContext ();
397+ DataFrame source = ctx .sql ("SELECT 1 AS a, 2 AS b" )) {
398+ try (DataFrame extended = source .withColumn ("c" , "a + b" )) {
399+ assertEquals (1L , extended .count ());
400+ }
401+ assertEquals (1L , source .count ());
402+ }
403+ }
404+
405+ @ Test
406+ void withColumnUnknownColumnRefThrows () {
407+ try (SessionContext ctx = new SessionContext ();
408+ DataFrame df = ctx .sql ("SELECT 1 AS x" )) {
409+ assertThrows (RuntimeException .class , () -> df .withColumn ("y" , "not_a_column + 1" ));
410+ }
411+ }
412+
413+ @ Test
414+ void withColumnRejectsNullArgs () {
415+ try (SessionContext ctx = new SessionContext ();
416+ DataFrame df = ctx .sql ("SELECT 1 AS x" )) {
417+ assertThrows (IllegalArgumentException .class , () -> df .withColumn (null , "x + 1" ));
418+ assertThrows (IllegalArgumentException .class , () -> df .withColumn ("y" , null ));
419+ }
420+ }
421+
422+ @ Test
423+ void unnestColumnsExpandsList () throws Exception {
424+ try (BufferAllocator allocator = new RootAllocator ();
425+ SessionContext ctx = new SessionContext ();
426+ DataFrame source = ctx .sql ("SELECT 1 AS id, [10, 20, 30] AS vals" );
427+ DataFrame unnested = source .unnestColumns ("vals" );
428+ ArrowReader reader = unnested .collect (allocator )) {
429+ int rows = 0 ;
430+ while (reader .loadNextBatch ()) {
431+ rows += reader .getVectorSchemaRoot ().getRowCount ();
432+ }
433+ assertEquals (3 , rows );
434+ }
435+ }
436+
437+ @ Test
438+ void unnestColumnsPreserveNullsTrueKeepsNullRow () throws Exception {
439+ String sql =
440+ "SELECT id, vals FROM (VALUES "
441+ + "(1, [10, 20]), "
442+ + "(2, CAST(NULL AS BIGINT[])), "
443+ + "(3, [30])) AS t(id, vals)" ;
444+ try (BufferAllocator allocator = new RootAllocator ();
445+ SessionContext ctx = new SessionContext ();
446+ DataFrame source = ctx .sql (sql );
447+ DataFrame unnested = source .unnestColumns (new UnnestOptions ().preserveNulls (true ), "vals" );
448+ ArrowReader reader = unnested .collect (allocator )) {
449+ int rows = 0 ;
450+ while (reader .loadNextBatch ()) {
451+ rows += reader .getVectorSchemaRoot ().getRowCount ();
452+ }
453+ assertEquals (4 , rows );
454+ }
455+ }
456+
457+ @ Test
458+ void unnestColumnsPreserveNullsFalseDropsNullRow () throws Exception {
459+ String sql =
460+ "SELECT id, vals FROM (VALUES "
461+ + "(1, [10, 20]), "
462+ + "(2, CAST(NULL AS BIGINT[])), "
463+ + "(3, [30])) AS t(id, vals)" ;
464+ try (BufferAllocator allocator = new RootAllocator ();
465+ SessionContext ctx = new SessionContext ();
466+ DataFrame source = ctx .sql (sql );
467+ DataFrame unnested =
468+ source .unnestColumns (new UnnestOptions ().preserveNulls (false ), "vals" );
469+ ArrowReader reader = unnested .collect (allocator )) {
470+ int rows = 0 ;
471+ while (reader .loadNextBatch ()) {
472+ rows += reader .getVectorSchemaRoot ().getRowCount ();
473+ }
474+ assertEquals (3 , rows );
475+ }
476+ }
477+
478+ @ Test
479+ void unnestColumnsIsNonDestructive () {
480+ try (SessionContext ctx = new SessionContext ();
481+ DataFrame source = ctx .sql ("SELECT 1 AS id, [10, 20] AS vals" )) {
482+ try (DataFrame unnested = source .unnestColumns ("vals" )) {
483+ assertEquals (2L , unnested .count ());
484+ }
485+ assertEquals (1L , source .count ());
486+ }
487+ }
488+
489+ @ Test
490+ void unnestColumnsUnknownColumnThrows () {
491+ try (SessionContext ctx = new SessionContext ();
492+ DataFrame df = ctx .sql ("SELECT 1 AS x" )) {
493+ assertThrows (RuntimeException .class , () -> df .unnestColumns ("not_a_column" ));
494+ }
495+ }
496+
497+ @ Test
498+ void unnestColumnsRejectsNullArgs () {
499+ try (SessionContext ctx = new SessionContext ();
500+ DataFrame df = ctx .sql ("SELECT 1 AS x, [1, 2] AS vals" )) {
501+ assertThrows (
502+ IllegalArgumentException .class , () -> df .unnestColumns ((UnnestOptions ) null , "vals" ));
503+ assertThrows (
504+ IllegalArgumentException .class ,
505+ () -> df .unnestColumns (new UnnestOptions (), (String []) null ));
506+ }
507+ }
356508}
0 commit comments