Skip to content

Commit 9fbc812

Browse files
committed
feat: add SessionContextBuilder with typed setters
1 parent b8d7bca commit 9fbc812

3 files changed

Lines changed: 174 additions & 0 deletions

File tree

‎src/main/java/org/apache/datafusion/SessionContext.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public SessionContext() {
5454
}
5555
}
5656

57+
/** Start configuring a {@link SessionContext}. */
58+
public static SessionContextBuilder builder() {
59+
return new SessionContextBuilder();
60+
}
61+
5762
/**
5863
* Parse and plan {@code query}, returning a lazy {@link DataFrame}. The query is not executed
5964
* until {@link DataFrame#collect} is called.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.datafusion;
21+
22+
import org.apache.datafusion.protobuf.MemoryLimit;
23+
import org.apache.datafusion.protobuf.SessionOptions;
24+
25+
/**
26+
* Builder for a configured {@link SessionContext}. Each setter is optional; unset fields leave the
27+
* DataFusion default in place.
28+
*/
29+
public final class SessionContextBuilder {
30+
private Integer batchSize;
31+
private Integer targetPartitions;
32+
private Boolean collectStatistics;
33+
private Boolean informationSchema;
34+
private Long memoryLimitBytes;
35+
private Double memoryLimitFraction;
36+
private String tempDirectory;
37+
38+
SessionContextBuilder() {}
39+
40+
public SessionContextBuilder batchSize(int batchSize) {
41+
this.batchSize = batchSize;
42+
return this;
43+
}
44+
45+
public SessionContextBuilder targetPartitions(int targetPartitions) {
46+
this.targetPartitions = targetPartitions;
47+
return this;
48+
}
49+
50+
public SessionContextBuilder collectStatistics(boolean collectStatistics) {
51+
this.collectStatistics = collectStatistics;
52+
return this;
53+
}
54+
55+
public SessionContextBuilder informationSchema(boolean informationSchema) {
56+
this.informationSchema = informationSchema;
57+
return this;
58+
}
59+
60+
/** Cap the memory pool at {@code maxMemoryBytes}, reserving {@code fraction} of it for queries. */
61+
public SessionContextBuilder memoryLimit(long maxMemoryBytes, double fraction) {
62+
this.memoryLimitBytes = maxMemoryBytes;
63+
this.memoryLimitFraction = fraction;
64+
return this;
65+
}
66+
67+
/** Directory the DiskManager uses for spill files. */
68+
public SessionContextBuilder tempDirectory(String path) {
69+
this.tempDirectory = path;
70+
return this;
71+
}
72+
73+
byte[] toBytes() {
74+
SessionOptions.Builder b = SessionOptions.newBuilder();
75+
if (batchSize != null) {
76+
b.setBatchSize(batchSize);
77+
}
78+
if (targetPartitions != null) {
79+
b.setTargetPartitions(targetPartitions);
80+
}
81+
if (collectStatistics != null) {
82+
b.setCollectStatistics(collectStatistics);
83+
}
84+
if (informationSchema != null) {
85+
b.setInformationSchema(informationSchema);
86+
}
87+
if (memoryLimitBytes != null && memoryLimitFraction != null) {
88+
b.setMemoryLimit(
89+
MemoryLimit.newBuilder()
90+
.setMaxMemoryBytes(memoryLimitBytes)
91+
.setMemoryFraction(memoryLimitFraction)
92+
.build());
93+
}
94+
if (tempDirectory != null) {
95+
b.setTempDirectory(tempDirectory);
96+
}
97+
return b.build().toByteArray();
98+
}
99+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.datafusion;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
import org.apache.datafusion.protobuf.SessionOptions;
27+
import org.junit.jupiter.api.Test;
28+
29+
class SessionContextBuilderTest {
30+
31+
@Test
32+
void protoRoundTripPreservesAllFields() throws Exception {
33+
byte[] bytes =
34+
SessionContext.builder()
35+
.batchSize(8192)
36+
.targetPartitions(4)
37+
.collectStatistics(true)
38+
.informationSchema(true)
39+
.memoryLimit(1L << 30, 0.8)
40+
.tempDirectory("/tmp/df")
41+
.toBytes();
42+
43+
SessionOptions parsed = SessionOptions.parseFrom(bytes);
44+
assertTrue(parsed.hasBatchSize());
45+
assertEquals(8192L, parsed.getBatchSize());
46+
assertTrue(parsed.hasTargetPartitions());
47+
assertEquals(4L, parsed.getTargetPartitions());
48+
assertTrue(parsed.hasCollectStatistics());
49+
assertTrue(parsed.getCollectStatistics());
50+
assertTrue(parsed.hasInformationSchema());
51+
assertTrue(parsed.getInformationSchema());
52+
assertTrue(parsed.hasMemoryLimit());
53+
assertEquals(1L << 30, parsed.getMemoryLimit().getMaxMemoryBytes());
54+
assertEquals(0.8, parsed.getMemoryLimit().getMemoryFraction(), 1e-9);
55+
assertTrue(parsed.hasTempDirectory());
56+
assertEquals("/tmp/df", parsed.getTempDirectory());
57+
}
58+
59+
@Test
60+
void unsetFieldsAreAbsentInProto() throws Exception {
61+
byte[] bytes = SessionContext.builder().batchSize(8192).toBytes();
62+
SessionOptions parsed = SessionOptions.parseFrom(bytes);
63+
assertTrue(parsed.hasBatchSize());
64+
assertFalse(parsed.hasTargetPartitions());
65+
assertFalse(parsed.hasCollectStatistics());
66+
assertFalse(parsed.hasInformationSchema());
67+
assertFalse(parsed.hasMemoryLimit());
68+
assertFalse(parsed.hasTempDirectory());
69+
}
70+
}

0 commit comments

Comments
 (0)