|
25 | 25 | import static org.junit.jupiter.api.Assertions.assertTrue; |
26 | 26 |
|
27 | 27 | import java.nio.file.Path; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Map; |
28 | 30 |
|
29 | 31 | import org.apache.arrow.memory.BufferAllocator; |
30 | 32 | import org.apache.arrow.memory.RootAllocator; |
@@ -113,4 +115,129 @@ void buildWithEveryKnobSetCanExecuteSelectOne(@TempDir Path tempDir) throws Exce |
113 | 115 | assertEquals(1, reader.getVectorSchemaRoot().getRowCount()); |
114 | 116 | } |
115 | 117 | } |
| 118 | + |
| 119 | + @Test |
| 120 | + void setOptionRoundTripsThroughProto() throws Exception { |
| 121 | + byte[] bytes = |
| 122 | + SessionContext.builder() |
| 123 | + .setOption("datafusion.execution.parquet.pushdown_filters", "true") |
| 124 | + .setOption("datafusion.optimizer.prefer_hash_join", "true") |
| 125 | + .toBytes(); |
| 126 | + SessionOptions parsed = SessionOptions.parseFrom(bytes); |
| 127 | + Map<String, String> opts = parsed.getOptionsMap(); |
| 128 | + assertEquals(2, opts.size()); |
| 129 | + assertEquals("true", opts.get("datafusion.execution.parquet.pushdown_filters")); |
| 130 | + assertEquals("true", opts.get("datafusion.optimizer.prefer_hash_join")); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void setOptionsBulkRoundTripsThroughProto() throws Exception { |
| 135 | + Map<String, String> entries = new LinkedHashMap<>(); |
| 136 | + entries.put("datafusion.execution.time_zone", "UTC"); |
| 137 | + entries.put("datafusion.optimizer.default_filter_selectivity", "10"); |
| 138 | + byte[] bytes = SessionContext.builder().setOptions(entries).toBytes(); |
| 139 | + SessionOptions parsed = SessionOptions.parseFrom(bytes); |
| 140 | + Map<String, String> opts = parsed.getOptionsMap(); |
| 141 | + assertEquals(entries, opts); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void setOptionRejectsNullKeyOrValue() { |
| 146 | + SessionContextBuilder b = SessionContext.builder(); |
| 147 | + assertThrows(IllegalArgumentException.class, () -> b.setOption(null, "v")); |
| 148 | + assertThrows(IllegalArgumentException.class, () -> b.setOption("k", null)); |
| 149 | + assertThrows(IllegalArgumentException.class, () -> b.setOptions(null)); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void setOptionAffectsRunningSession() { |
| 154 | + try (SessionContext ctx = |
| 155 | + SessionContext.builder() |
| 156 | + .setOption("datafusion.execution.parquet.pushdown_filters", "true") |
| 157 | + .build()) { |
| 158 | + assertEquals("true", ctx.getOption("datafusion.execution.parquet.pushdown_filters")); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void setOptionLastWriteWinsWithinMap() throws Exception { |
| 164 | + byte[] bytes = |
| 165 | + SessionContext.builder() |
| 166 | + .setOption("datafusion.execution.batch_size", "1024") |
| 167 | + .setOption("datafusion.execution.batch_size", "8192") |
| 168 | + .toBytes(); |
| 169 | + SessionOptions parsed = SessionOptions.parseFrom(bytes); |
| 170 | + assertEquals("8192", parsed.getOptionsMap().get("datafusion.execution.batch_size")); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void setOptionOverridesTypedSetterAtConstruction() { |
| 175 | + // Typed setter says batch_size = 8192; setOption overrides to 1024. |
| 176 | + // The override is a real semantic change, not just absence of a throw — |
| 177 | + // assert that's what DataFusion is actually running with. |
| 178 | + try (SessionContext ctx = |
| 179 | + SessionContext.builder() |
| 180 | + .batchSize(8192) |
| 181 | + .setOption("datafusion.execution.batch_size", "1024") |
| 182 | + .build()) { |
| 183 | + assertEquals("1024", ctx.getOption("datafusion.execution.batch_size")); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void unknownOptionKeyThrowsAtBuild() { |
| 189 | + SessionContextBuilder b = |
| 190 | + SessionContext.builder().setOption("datafusion.execution.bogus_made_up_key", "x"); |
| 191 | + RuntimeException thrown = assertThrows(RuntimeException.class, b::build); |
| 192 | + // The DataFusion error message includes the bad key text — assert it's |
| 193 | + // surfaced rather than swallowed. |
| 194 | + assertTrue( |
| 195 | + thrown.getMessage() != null && thrown.getMessage().contains("bogus_made_up_key"), |
| 196 | + "expected DataFusion error to mention bad key, got: " + thrown.getMessage()); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + void getOptionReadsTypedSetterValue() { |
| 201 | + try (SessionContext ctx = SessionContext.builder().batchSize(4096).build()) { |
| 202 | + assertEquals("4096", ctx.getOption("datafusion.execution.batch_size")); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + void getOptionWithNoExplicitValueReturnsDefault() { |
| 208 | + // batch_size has a non-null default in DataFusion, so the getter must |
| 209 | + // return the default rather than null when the user never set it. |
| 210 | + try (SessionContext ctx = SessionContext.builder().build()) { |
| 211 | + String batchSize = ctx.getOption("datafusion.execution.batch_size"); |
| 212 | + assertTrue( |
| 213 | + batchSize != null && !batchSize.isEmpty(), |
| 214 | + "expected default batch_size, got: " + batchSize); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + void getOptionRejectsNullKey() { |
| 220 | + try (SessionContext ctx = SessionContext.builder().build()) { |
| 221 | + assertThrows(IllegalArgumentException.class, () -> ctx.getOption(null)); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void getOptionThrowsOnUnknownKey() { |
| 227 | + try (SessionContext ctx = SessionContext.builder().build()) { |
| 228 | + RuntimeException thrown = |
| 229 | + assertThrows(RuntimeException.class, () -> ctx.getOption("datafusion.no.such.key")); |
| 230 | + assertTrue( |
| 231 | + thrown.getMessage() != null && thrown.getMessage().contains("no.such.key"), |
| 232 | + "expected error to mention bad key, got: " + thrown.getMessage()); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + void getOptionThrowsAfterClose() { |
| 238 | + SessionContext ctx = SessionContext.builder().build(); |
| 239 | + ctx.close(); |
| 240 | + assertThrows( |
| 241 | + IllegalStateException.class, () -> ctx.getOption("datafusion.execution.batch_size")); |
| 242 | + } |
116 | 243 | } |
0 commit comments