haochunchang commented on code in PR #25114:
URL: https://github.com/apache/datafusion/pull/25114#discussion_r4367102461


##########
datafusion/execution/src/runtime_options.rs:
##########
@@ -0,0 +1,508 @@
+// 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.
+
+//! Typed schema for the `datafusion.runtime.*` configuration namespace.
+//!
+//! [`RuntimeOptions`] owns the runtime keys, their descriptions, their 
defaults
+//! and their parsing, using the same [`ConfigField`] machinery that backs
+//! `ConfigOptions`. It does not live inside `ConfigOptions` because
+//! `datafusion-execution` depends on `datafusion-common`, never the reverse.
+
+use std::fmt::{self, Display, Formatter};
+use std::time::Duration;
+
+use crate::cache::cache_manager::{
+    DEFAULT_FILE_STATISTICS_MEMORY_LIMIT, 
DEFAULT_LIST_FILES_CACHE_MEMORY_LIMIT,
+    DEFAULT_LIST_FILES_CACHE_TTL, DEFAULT_METADATA_CACHE_LIMIT,
+};
+use crate::disk_manager::{
+    DEFAULT_MAX_SPILL_MERGE_FAN_IN, DEFAULT_MAX_TEMP_DIRECTORY_SIZE, 
DiskManagerBuilder,
+};
+use crate::memory_pool::MemoryLimit;
+use crate::runtime_env::{RuntimeEnv, RuntimeEnvBuilder};
+
+use datafusion_common::config::{ConfigEntry, ConfigField, Visit};
+use datafusion_common::{
+    DataFusionError, Result, config_field, config_namespace, 
plan_datafusion_err,
+    plan_err,
+};
+
+/// Prefix shared by every key in this namespace.
+pub const RUNTIME_CONFIG_PREFIX: &str = "datafusion.runtime";
+
+/// A byte capacity written as a plain number of bytes (`0`) or a number with a
+/// `K`, `M` or `G` suffix (`512K`, `100M`, `1.5G`).
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct CapacityLimit(pub usize);
+
+impl Display for CapacityLimit {
+    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+        const GB: usize = 1024 * 1024 * 1024;
+        const MB: usize = 1024 * 1024;
+        const KB: usize = 1024;
+
+        match self.0 {
+            s if s >= GB => write!(f, "{}G", s / GB),
+            s if s >= MB => write!(f, "{}M", s / MB),
+            s if s >= KB => write!(f, "{}K", s / KB),
+            s => write!(f, "{s}"),
+        }
+    }
+}
+
+config_field!(CapacityLimit, value => CapacityLimit(parse_capacity(value)?));
+
+/// A cache time-to-live written as minutes and/or seconds (`90s`, `2m`, 
`1m30s`).
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct CacheTtl(pub Duration);
+
+impl Display for CacheTtl {
+    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+        let total = self.0.as_secs();
+        write!(f, "{}m{}s", total / 60, total % 60)
+    }
+}
+
+config_field!(CacheTtl, value => CacheTtl(parse_ttl(value)?));
+
+/// A count that must not be negative.
+///
+/// A plain `usize` would report `Error parsing '-1' as usize`, naming the Rust
+/// type rather than the constraint.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct NonNegativeCount(pub usize);
+
+impl Display for NonNegativeCount {
+    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+config_field!(NonNegativeCount, value => NonNegativeCount(
+    value.parse::<usize>().map_err(|e| plan_datafusion_err!(
+        "Failed to parse non-negative integer from value '{value}': {e}"
+    ))?
+));
+
+config_namespace! {
+    /// Options that configure the [`RuntimeEnv`] shared by a session.
+    ///
+    /// Field order below is the order entries are reported in.
+    pub struct RuntimeOptions {
+        /// Maximum memory limit for query execution. Supports suffixes K 
(kilobytes), M (megabytes), and G (gigabytes) or '0' for 0. Example: '2G' for 2 
gigabytes.
+        pub memory_limit: Option<CapacityLimit>, default = None
+
+        /// Maximum temporary file directory size. Supports suffixes K 
(kilobytes), M (megabytes), and G (gigabytes) or '0' for 0. Example: '2G' for 2 
gigabytes.
+        pub max_temp_directory_size: CapacityLimit, default = 
CapacityLimit(DEFAULT_MAX_TEMP_DIRECTORY_SIZE as usize)

Review Comment:
   I turned `CapacityLimit` into a private struct.
   - Disk sizes use `from_bytes` / `as_u64`, with no conversion.
   - In-memory sizes use `From<usize>` (lossless) and `as_usize`, which uses 
`usize::try_from`. 
   
   On a target where the value doesn't fit, SET returns an error naming the key 
instead of truncating.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to