|
19 | 19 |
|
20 | 20 | package org.apache.datafusion; |
21 | 21 |
|
| 22 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
22 | 23 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
23 | 25 |
|
| 26 | +import org.apache.arrow.memory.BufferAllocator; |
| 27 | +import org.apache.arrow.memory.RootAllocator; |
| 28 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 29 | +import org.apache.arrow.vector.ipc.ArrowReader; |
24 | 30 | import org.junit.jupiter.api.Test; |
25 | 31 |
|
26 | 32 | class DataFrameTransformationsTest { |
@@ -49,4 +55,34 @@ void showWithLimitDoesNotThrow() { |
49 | 55 | df.show(1_000_000); |
50 | 56 | } |
51 | 57 | } |
| 58 | + |
| 59 | + @Test |
| 60 | + void selectProjectsAndReordersColumns() throws Exception { |
| 61 | + try (BufferAllocator allocator = new RootAllocator(); |
| 62 | + SessionContext ctx = new SessionContext(); |
| 63 | + DataFrame source = ctx.sql("SELECT 1 AS a, 2 AS b, 3 AS c"); |
| 64 | + DataFrame projected = source.select("b", "a"); |
| 65 | + ArrowReader reader = projected.collect(allocator)) { |
| 66 | + assertTrue(reader.loadNextBatch()); |
| 67 | + VectorSchemaRoot root = reader.getVectorSchemaRoot(); |
| 68 | + assertEquals(1, root.getRowCount()); |
| 69 | + assertArrayEquals( |
| 70 | + new String[] {"b", "a"}, |
| 71 | + root.getSchema().getFields().stream().map(f -> f.getName()).toArray(String[]::new)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void selectIsNonDestructive() { |
| 77 | + try (SessionContext ctx = new SessionContext(); |
| 78 | + DataFrame source = ctx.sql("SELECT 1 AS a, 2 AS b")) { |
| 79 | + try (DataFrame first = source.select("a")) { |
| 80 | + assertEquals(1L, first.count()); |
| 81 | + } |
| 82 | + try (DataFrame second = source.select("b")) { |
| 83 | + assertEquals(1L, second.count()); |
| 84 | + } |
| 85 | + assertEquals(1L, source.count()); |
| 86 | + } |
| 87 | + } |
52 | 88 | } |
0 commit comments