|
| 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 | +} |
0 commit comments