-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for in-memory TPCH tests #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,215 @@ | ||
| // 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. | ||
|
|
||
| /// Print Peak RSS, Peak Commit, Page Faults based on mimalloc api | ||
| pub fn print_memory_stats() { | ||
| // removed as not used in this project. | ||
| use dashmap::{DashMap, Entry}; | ||
| use datafusion::arrow::record_batch::RecordBatch; | ||
| use datafusion::common::tree_node::{Transformed, TreeNode}; | ||
| use datafusion::common::{exec_err, extensions_options, plan_err}; | ||
| use datafusion::config::{ConfigExtension, ConfigOptions}; | ||
| use datafusion::error::DataFusionError; | ||
| use datafusion::execution::{FunctionRegistry, SendableRecordBatchStream, TaskContext}; | ||
| use datafusion::physical_optimizer::PhysicalOptimizerRule; | ||
| use datafusion::physical_plan::stream::RecordBatchStreamAdapter; | ||
| use datafusion::physical_plan::{ | ||
| displayable, DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, | ||
| }; | ||
| use datafusion_proto::physical_plan::PhysicalExtensionCodec; | ||
| use futures::{FutureExt, StreamExt}; | ||
| use prost::Message; | ||
| use std::any::Any; | ||
| use std::fmt::Formatter; | ||
| use std::sync::{Arc, LazyLock}; | ||
| use tokio::sync::OnceCell; | ||
|
|
||
| type Key = (String, usize); | ||
| type Value = Arc<OnceCell<Vec<RecordBatch>>>; | ||
| static CACHE: LazyLock<DashMap<Key, Value>> = LazyLock::new(DashMap::default); | ||
|
|
||
| /// Caches all the record batches in a global [CACHE] on the first run, and serves | ||
| /// them from the cache in any subsequent run. | ||
| #[derive(Debug, Clone)] | ||
| pub struct InMemoryCacheExec { | ||
| inner: Arc<dyn ExecutionPlan>, | ||
| } | ||
|
|
||
| extensions_options! { | ||
| /// Marker used by the [InMemoryCacheExec] that determines wether its fine | ||
gabotechs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// to load data from disk because we are warming up, or not. | ||
| /// | ||
| /// If this marker is not present during InMemoryCacheExec::execute(), and | ||
| /// the data was not loaded in-memory already, the query will fail. | ||
| pub struct WarmingUpMarker { | ||
| is_warming_up: bool, default = false | ||
| } | ||
| } | ||
|
|
||
| impl ConfigExtension for WarmingUpMarker { | ||
| const PREFIX: &'static str = "in-memory-cache-exec"; | ||
| } | ||
|
|
||
| impl WarmingUpMarker { | ||
| pub fn warming_up() -> Self { | ||
| Self { | ||
| is_warming_up: true, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ExecutionPlan for InMemoryCacheExec { | ||
| fn name(&self) -> &str { | ||
| "InMemoryDataSourceExec" | ||
| } | ||
|
|
||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn properties(&self) -> &PlanProperties { | ||
| self.inner.properties() | ||
| } | ||
|
|
||
| fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> { | ||
| vec![&self.inner] | ||
| } | ||
|
|
||
| fn with_new_children( | ||
| self: Arc<Self>, | ||
| children: Vec<Arc<dyn ExecutionPlan>>, | ||
| ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> { | ||
| Ok(Arc::new(Self { | ||
| inner: children[0].clone(), | ||
| })) | ||
| } | ||
|
|
||
| fn execute( | ||
| &self, | ||
| partition: usize, | ||
| context: Arc<TaskContext>, | ||
| ) -> datafusion::common::Result<SendableRecordBatchStream> { | ||
| let once = { | ||
| let inner_display = displayable(self.inner.as_ref()).one_line().to_string(); | ||
| let entry = CACHE.entry((inner_display, partition)); | ||
| if matches!(entry, Entry::Vacant(_)) | ||
| && !context | ||
| .session_config() | ||
| .options() | ||
| .extensions | ||
| .get::<WarmingUpMarker>() | ||
| .map(|v| v.is_warming_up) | ||
| .unwrap_or_default() | ||
| { | ||
| return exec_err!("InMemoryCacheExec is not yet warmed up"); | ||
| } | ||
| let once = entry.or_insert(Arc::new(OnceCell::new())); | ||
| once.value().clone() | ||
| }; | ||
|
|
||
| let inner = Arc::clone(&self.inner); | ||
|
|
||
| let stream = async move { | ||
| let batches = once | ||
| .get_or_try_init(|| async move { | ||
| let mut stream = inner.execute(partition, context)?; | ||
| let mut batches = vec![]; | ||
| while let Some(batch) = stream.next().await { | ||
| batches.push(batch?); | ||
| } | ||
| Ok::<_, DataFusionError>(batches) | ||
| }) | ||
| .await?; | ||
| Ok(batches.clone()) | ||
| } | ||
| .into_stream() | ||
| .map(|v| match v { | ||
| Ok(batch) => futures::stream::iter(batch.into_iter().map(Ok)).boxed(), | ||
| Err(err) => futures::stream::once(async { Err(err) }).boxed(), | ||
| }) | ||
| .flatten(); | ||
|
|
||
| Ok(Box::pin(RecordBatchStreamAdapter::new( | ||
| self.inner.schema(), | ||
| stream, | ||
| ))) | ||
| } | ||
| } | ||
|
|
||
| impl DisplayAs for InMemoryCacheExec { | ||
| fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result { | ||
| writeln!(f, "InMemoryDataSourceExec") | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, PartialEq, ::prost::Message)] | ||
| struct InMemoryCacheExecProto { | ||
| #[prost(string, tag = "1")] | ||
| name: String, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct InMemoryCacheExecCodec; | ||
|
|
||
| impl PhysicalExtensionCodec for InMemoryCacheExecCodec { | ||
| fn try_decode( | ||
| &self, | ||
| buf: &[u8], | ||
| inputs: &[Arc<dyn ExecutionPlan>], | ||
| _registry: &dyn FunctionRegistry, | ||
| ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> { | ||
| let Ok(proto) = InMemoryCacheExecProto::decode(buf) else { | ||
| return plan_err!("no InMemoryDataSourceExecProto"); | ||
| }; | ||
| if proto.name != "InMemoryDataSourceExec" { | ||
| return plan_err!("unsupported InMemoryDataSourceExec proto: {:?}", proto.name); | ||
| }; | ||
| Ok(Arc::new(InMemoryCacheExec { | ||
| inner: inputs[0].clone(), | ||
| })) | ||
| } | ||
|
|
||
| fn try_encode( | ||
| &self, | ||
| node: Arc<dyn ExecutionPlan>, | ||
| buf: &mut Vec<u8>, | ||
| ) -> datafusion::common::Result<()> { | ||
| if !node.as_any().is::<InMemoryCacheExec>() { | ||
| return plan_err!("no InMemoryDataSourceExec"); | ||
| }; | ||
| let proto = InMemoryCacheExecProto { | ||
| name: "InMemoryDataSourceExec".to_string(), | ||
| }; | ||
| let Ok(_) = proto.encode(buf) else { | ||
| return plan_err!("no InMemoryDataSourceExecProto"); | ||
| }; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Wraps any plan without children with an [InMemoryCacheExec] node. | ||
gabotechs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[derive(Debug)] | ||
| pub struct InMemoryDataSourceRule; | ||
|
|
||
| impl PhysicalOptimizerRule for InMemoryDataSourceRule { | ||
| fn optimize( | ||
| &self, | ||
| plan: Arc<dyn ExecutionPlan>, | ||
| _config: &ConfigOptions, | ||
| ) -> datafusion::common::Result<Arc<dyn ExecutionPlan>> { | ||
| Ok(plan | ||
| .transform_up(|plan| { | ||
| if plan.children().is_empty() { | ||
| Ok(Transformed::yes(Arc::new(InMemoryCacheExec { | ||
| inner: plan.clone(), | ||
| }))) | ||
| } else { | ||
| Ok(Transformed::no(plan)) | ||
| } | ||
| })? | ||
| .data) | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "InMemoryDataSourceRule" | ||
| } | ||
|
|
||
| fn schema_check(&self) -> bool { | ||
| true | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.