@@ -294,4 +294,53 @@ void dropColumnsSilentlyIgnoresUnknownNames() throws Exception {
294294 root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
295295 }
296296 }
297+
298+ @ Test
299+ void withColumnRenamedChangesColumnName () throws Exception {
300+ try (BufferAllocator allocator = new RootAllocator ();
301+ SessionContext ctx = new SessionContext ();
302+ DataFrame source = ctx .sql ("SELECT 1 AS a, 2 AS b" );
303+ DataFrame renamed = source .withColumnRenamed ("a" , "alpha" );
304+ ArrowReader reader = renamed .collect (allocator )) {
305+ assertTrue (reader .loadNextBatch ());
306+ VectorSchemaRoot root = reader .getVectorSchemaRoot ();
307+ assertArrayEquals (
308+ new String [] {"alpha" , "b" },
309+ root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
310+ }
311+ }
312+
313+ @ Test
314+ void withColumnRenamedIsNonDestructive () throws Exception {
315+ try (BufferAllocator allocator = new RootAllocator ();
316+ SessionContext ctx = new SessionContext ();
317+ DataFrame source = ctx .sql ("SELECT 1 AS a, 2 AS b" )) {
318+ try (DataFrame renamed = source .withColumnRenamed ("a" , "alpha" )) {
319+ assertEquals (1L , renamed .count ());
320+ }
321+ try (DataFrame again = source .select ("a" );
322+ ArrowReader reader = again .collect (allocator )) {
323+ assertTrue (reader .loadNextBatch ());
324+ VectorSchemaRoot root = reader .getVectorSchemaRoot ();
325+ assertArrayEquals (
326+ new String [] {"a" },
327+ root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
328+ }
329+ }
330+ }
331+
332+ @ Test
333+ void withColumnRenamedUnknownColumnIsNoOp () throws Exception {
334+ try (BufferAllocator allocator = new RootAllocator ();
335+ SessionContext ctx = new SessionContext ();
336+ DataFrame df = ctx .sql ("SELECT 1 AS x" );
337+ DataFrame renamed = df .withColumnRenamed ("not_a_column" , "y" );
338+ ArrowReader reader = renamed .collect (allocator )) {
339+ assertTrue (reader .loadNextBatch ());
340+ VectorSchemaRoot root = reader .getVectorSchemaRoot ();
341+ assertArrayEquals (
342+ new String [] {"x" },
343+ root .getSchema ().getFields ().stream ().map (f -> f .getName ()).toArray (String []::new ));
344+ }
345+ }
297346}
0 commit comments