You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/09 04:07:37 UTC

[GitHub] [arrow-datafusion] matthewmturner commented on a change in pull request #1946: Add examples to use MemTable and TableProvider (#1864)

matthewmturner commented on a change in pull request #1946:
URL: https://github.com/apache/arrow-datafusion/pull/1946#discussion_r822269909



##########
File path: datafusion-examples/examples/custom_datasource.rs
##########
@@ -0,0 +1,269 @@
+// 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 async_trait::async_trait;
+use datafusion::arrow::array::{Array, UInt64Builder, UInt8Builder};
+use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::datasource::TableProvider;
+use datafusion::error::{DataFusionError, Result};
+use datafusion::execution::dataframe_impl::DataFrameImpl;
+use datafusion::execution::runtime_env::RuntimeEnv;
+use datafusion::logical_plan::{Expr, LogicalPlanBuilder};
+use datafusion::physical_plan::expressions::PhysicalSortExpr;
+use datafusion::physical_plan::memory::MemoryStream;
+use datafusion::physical_plan::{
+    project_schema, ExecutionPlan, SendableRecordBatchStream, Statistics,
+};
+use datafusion::prelude::*;
+use std::any::Any;
+use std::collections::{BTreeMap, HashMap};
+use std::fmt::{Debug, Formatter};
+use std::sync::{Arc, Mutex};
+use std::time::Duration;
+use tokio::time::timeout;
+
+/// This example demonstrates executing a simple query against a custom datasource
+#[tokio::main]
+async fn main() -> Result<()> {
+    // create our custom datasource and adding some users
+    let db = CustomDataSource::default();
+    db.populate_users();
+
+    search_accounts(db.clone(), None, 3).await?;
+    search_accounts(db.clone(), Some(col("bank_account").gt(lit(8000u64))), 1).await?;
+    search_accounts(db.clone(), Some(col("bank_account").gt(lit(200u64))), 2).await?;
+
+    Ok(())
+}
+
+async fn search_accounts(
+    db: CustomDataSource,
+    filter: Option<Expr>,
+    expected_result_length: usize,
+) -> Result<()> {
+    // create local execution context
+    let ctx = ExecutionContext::new();
+
+    // create logical plan composed of a single TableScan

Review comment:
       Perhaps its worth expanding why `LogicalPlanBuilder` is used as opposed to using something like `ctx.register_table`.  At least for me, it would be useful when each is useful and why.




-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org