|
| 1 | +/* |
| 2 | + * Parseable Server (C) 2022 - 2023 Parseable, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +use async_trait::async_trait; |
| 20 | +use datafusion::arrow::datatypes::{Schema, SchemaRef}; |
| 21 | +use datafusion::datasource::{MemTable, TableProvider}; |
| 22 | +use datafusion::error::DataFusionError; |
| 23 | +use datafusion::execution::context::SessionState; |
| 24 | +use datafusion::logical_expr::TableType; |
| 25 | +use datafusion::physical_plan::empty::EmptyExec; |
| 26 | +use datafusion::physical_plan::union::UnionExec; |
| 27 | +use datafusion::physical_plan::ExecutionPlan; |
| 28 | +use datafusion::prelude::Expr; |
| 29 | +use std::any::Any; |
| 30 | +use std::sync::Arc; |
| 31 | + |
| 32 | +use crate::storage::staging::ReadBuf; |
| 33 | +use crate::storage::ObjectStorage; |
| 34 | +use crate::utils::arrow::adapt_batch; |
| 35 | + |
| 36 | +pub struct QueryTableProvider { |
| 37 | + storage_prefixes: Vec<String>, |
| 38 | + storage: Arc<dyn ObjectStorage + Send>, |
| 39 | + readable_buffer: Vec<ReadBuf>, |
| 40 | + schema: Arc<Schema>, |
| 41 | +} |
| 42 | + |
| 43 | +impl QueryTableProvider { |
| 44 | + pub fn new( |
| 45 | + storage_prefixes: Vec<String>, |
| 46 | + storage: Arc<dyn ObjectStorage + Send>, |
| 47 | + readable_buffer: Vec<ReadBuf>, |
| 48 | + schema: Arc<Schema>, |
| 49 | + ) -> Self { |
| 50 | + Self { |
| 51 | + storage_prefixes, |
| 52 | + storage, |
| 53 | + readable_buffer, |
| 54 | + schema, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async fn create_physical_plan( |
| 59 | + &self, |
| 60 | + ctx: &SessionState, |
| 61 | + projection: Option<&Vec<usize>>, |
| 62 | + filters: &[Expr], |
| 63 | + limit: Option<usize>, |
| 64 | + ) -> Result<Arc<dyn ExecutionPlan>, DataFusionError> { |
| 65 | + let memexec = self.get_mem_exec(ctx, projection, filters, limit).await?; |
| 66 | + let table = self |
| 67 | + .storage |
| 68 | + .query_table(self.storage_prefixes.clone(), Arc::clone(&self.schema))?; |
| 69 | + |
| 70 | + let mut exec = Vec::new(); |
| 71 | + if let Some(memexec) = memexec { |
| 72 | + exec.push(memexec); |
| 73 | + } |
| 74 | + |
| 75 | + if let Some(ref storage_listing) = table { |
| 76 | + exec.push( |
| 77 | + storage_listing |
| 78 | + .scan(ctx, projection, filters, limit) |
| 79 | + .await?, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + if exec.is_empty() { |
| 84 | + Ok(Arc::new(EmptyExec::new(false, Arc::clone(&self.schema)))) |
| 85 | + } else { |
| 86 | + Ok(Arc::new(UnionExec::new(exec))) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + async fn get_mem_exec( |
| 91 | + &self, |
| 92 | + ctx: &SessionState, |
| 93 | + projection: Option<&Vec<usize>>, |
| 94 | + filters: &[Expr], |
| 95 | + limit: Option<usize>, |
| 96 | + ) -> Result<Option<Arc<dyn ExecutionPlan>>, DataFusionError> { |
| 97 | + if self.readable_buffer.is_empty() { |
| 98 | + return Ok(None); |
| 99 | + } |
| 100 | + |
| 101 | + let mem_records: Vec<Vec<_>> = self |
| 102 | + .readable_buffer |
| 103 | + .iter() |
| 104 | + .map(|r| { |
| 105 | + r.buf |
| 106 | + .iter() |
| 107 | + .cloned() |
| 108 | + .map(|rb| adapt_batch(&self.schema, rb)) |
| 109 | + .collect() |
| 110 | + }) |
| 111 | + .collect(); |
| 112 | + |
| 113 | + let memtable = MemTable::try_new(Arc::clone(&self.schema), mem_records)?; |
| 114 | + let memexec = memtable.scan(ctx, projection, filters, limit).await?; |
| 115 | + Ok(Some(memexec)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[async_trait] |
| 120 | +impl TableProvider for QueryTableProvider { |
| 121 | + fn as_any(&self) -> &dyn Any { |
| 122 | + self |
| 123 | + } |
| 124 | + |
| 125 | + fn schema(&self) -> SchemaRef { |
| 126 | + Arc::clone(&self.schema) |
| 127 | + } |
| 128 | + |
| 129 | + fn table_type(&self) -> TableType { |
| 130 | + TableType::Base |
| 131 | + } |
| 132 | + |
| 133 | + async fn scan( |
| 134 | + &self, |
| 135 | + ctx: &SessionState, |
| 136 | + projection: Option<&Vec<usize>>, |
| 137 | + filters: &[Expr], |
| 138 | + limit: Option<usize>, |
| 139 | + ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> { |
| 140 | + self.create_physical_plan(ctx, projection, filters, limit) |
| 141 | + .await |
| 142 | + } |
| 143 | +} |
0 commit comments