Skip to content

Apache DataFusion SQL Query Engine

refactor: improve PoolType argument handling for CLI (#10940)

Trent Hauck9b1bb68e3768
diff --git a/datafusion-cli/src/lib.rs b/datafusion-cli/src/lib.rs
index 139a60b8cf..5081436aa6 100644
--- a/datafusion-cli/src/lib.rs
+++ b/datafusion-cli/src/lib.rs
@@ -25,5 +25,6 @@ pub mod functions;
pub mod helper;
pub mod highlighter;
pub mod object_storage;
+pub mod pool_type;
pub mod print_format;
pub mod print_options;
diff --git a/datafusion-cli/src/main.rs b/datafusion-cli/src/main.rs
index f2b29fe786..f469fda4f9 100644
--- a/datafusion-cli/src/main.rs
+++ b/datafusion-cli/src/main.rs
@@ -19,7 +19,6 @@ use std::collections::HashMap;
use std::env;
use std::path::Path;
use std::process::ExitCode;
-use std::str::FromStr;
use std::sync::{Arc, OnceLock};
use datafusion::error::{DataFusionError, Result};
@@ -31,6 +30,7 @@ use datafusion_cli::catalog::DynamicFileCatalog;
use datafusion_cli::functions::ParquetMetadataFunc;
use datafusion_cli::{
exec,
+ pool_type::PoolType,
print_format::PrintFormat,
print_options::{MaxRows, PrintOptions},
DATAFUSION_CLI_VERSION,
@@ -42,24 +42,6 @@ use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
-#[derive(PartialEq, Debug)]
-enum PoolType {
- Greedy,
- Fair,
-}
-
-impl FromStr for PoolType {
- type Err = String;
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- match s {
- "Greedy" | "greedy" => Ok(PoolType::Greedy),
- "Fair" | "fair" => Ok(PoolType::Fair),
- _ => Err(format!("Invalid memory pool type '{}'", s)),
- }
- }
-}
-
#[derive(Debug, Parser, PartialEq)]
#[clap(author, version, about, long_about= None)]
struct Args {
@@ -127,9 +109,10 @@ struct Args {
#[clap(
long,
- help = "Specify the memory pool type 'greedy' or 'fair', default to 'greedy'"
+ help = "Specify the memory pool type 'greedy' or 'fair'",
+ default_value_t = PoolType::Greedy
)]
- mem_pool_type: Option<PoolType>,
+ mem_pool_type: PoolType,
#[clap(
long,
@@ -181,9 +164,9 @@ async fn main_inner() -> Result<()> {
let memory_limit = extract_memory_pool_size(&memory_limit).unwrap();
// set memory pool type
match args.mem_pool_type {
- Some(PoolType::Fair) => rt_config
+ PoolType::Fair => rt_config
.with_memory_pool(Arc::new(FairSpillPool::new(memory_limit))),
- _ => rt_config
+ PoolType::Greedy => rt_config
.with_memory_pool(Arc::new(GreedyMemoryPool::new(memory_limit)))
}
} else {
diff --git a/datafusion-cli/src/pool_type.rs b/datafusion-cli/src/pool_type.rs
new file mode 100644
index 0000000000..25763eba5c
--- /dev/null
+++ b/datafusion-cli/src/pool_type.rs
@@ -0,0 +1,48 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::{
+ fmt::{self, Display, Formatter},
+ str::FromStr,
+};
+
+#[derive(PartialEq, Debug)]
+pub enum PoolType {
+ Greedy,
+ Fair,
+}
+
+impl FromStr for PoolType {
+ type Err = String;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "Greedy" | "greedy" => Ok(PoolType::Greedy),
+ "Fair" | "fair" => Ok(PoolType::Fair),
+ _ => Err(format!("Invalid memory pool type '{}'", s)),
+ }
+ }
+}
+
+impl Display for PoolType {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ match self {
+ PoolType::Greedy => write!(f, "greedy"),
+ PoolType::Fair => write!(f, "fair"),
+ }
+ }
+}