wiedld commented on code in PR #17213: URL: https://github.com/apache/datafusion/pull/17213#discussion_r2289604443
########## datafusion-examples/examples/memory_pool_execution_plan.rs: ########## @@ -0,0 +1,324 @@ +// 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. + +//! This example demonstrates how to implement custom ExecutionPlans that properly +//! use memory tracking through TrackConsumersPool. +//! +//! This shows the pattern for implementing memory-aware operators that: +//! - Register memory consumers with the pool +//! - Reserve memory before allocating +//! - Handle memory pressure by spilling to disk +//! - Release memory when done + +use arrow::array::{Int32Array, StringArray}; +use arrow::record_batch::RecordBatch; +use arrow_schema::{DataType, Field, Schema, SchemaRef}; +use datafusion::datasource::{memory::MemTable, DefaultTableSource}; +use datafusion::error::{DataFusionError, Result}; +use datafusion::execution::memory_pool::{MemoryConsumer, MemoryReservation}; +use datafusion::execution::runtime_env::RuntimeEnvBuilder; +use datafusion::execution::{SendableRecordBatchStream, TaskContext}; +use datafusion::logical_expr::LogicalPlanBuilder; +use datafusion::physical_plan::stream::RecordBatchStreamAdapter; +use datafusion::physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, Statistics, +}; +use datafusion::prelude::*; +use futures::stream::{StreamExt, TryStreamExt}; +use std::any::Any; +use std::fmt; +use std::sync::Arc; + +#[tokio::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { Review Comment: Output is: ``` === DataFusion ExecutionPlan Memory Tracking Example === Example: Custom Memory-Aware BufferingExecutionPlan --------------------------------------------------- Executing BufferingExecutionPlan with memory tracking... Memory limit: 15000 bytes - should trigger spill on later batches Added batch of 10776 bytes, total buffered: 10776 bytes Memory limit reached, spilling 10776 bytes to disk Spill #1: Writing 10776 bytes to disk Added batch of 10776 bytes, total buffered: 10776 bytes Memory limit reached, spilling 10776 bytes to disk Spill #2: Writing 10776 bytes to disk Added batch of 14872 bytes, total buffered: 14872 bytes Memory limit reached, spilling 14872 bytes to disk Spill #3: Writing 14872 bytes to disk Added batch of 10776 bytes, total buffered: 10776 bytes Finished processing, released 10776 bytes Successfully executed BufferingExecutionPlan! The BufferingExecutionPlan processed 4 input batches and demonstrated memory tracking with spilling behavior when the memory limit was exceeded by later batches. Check the console output above to see the spill messages. ``` -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org