You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/06/19 14:31:38 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6713: Add async UDF example

alamb commented on code in PR #6713:
URL: https://github.com/apache/arrow-datafusion/pull/6713#discussion_r1234144367


##########
datafusion-examples/examples/async_udf.rs:
##########
@@ -0,0 +1,405 @@
+// 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 arrow_schema::{Field, Schema, SchemaRef};
+use async_trait::async_trait;
+use datafusion::{
+    arrow::{
+        array::{ArrayRef, Float32Array, Float64Array},
+        datatypes::DataType,
+        record_batch::RecordBatch,
+    },
+    execution::{
+        context::{QueryPlanner, SessionState},
+        runtime_env::RuntimeEnv,
+        TaskContext,
+    },
+    logical_expr::Volatility,
+    physical_expr::PhysicalSortExpr,
+    physical_plan::{
+        stream::RecordBatchStreamAdapter, DisplayFormatType, Distribution, ExecutionPlan,
+        Partitioning, SendableRecordBatchStream, Statistics,
+    },
+    physical_planner::{DefaultPhysicalPlanner, ExtensionPlanner, PhysicalPlanner},
+};
+
+use datafusion::prelude::*;
+use datafusion::{error::Result, physical_plan::functions::make_scalar_function};
+use datafusion_common::{
+    cast::{as_float32_array, as_float64_array},
+    tree_node::{Transformed, TreeNode},
+    DFSchemaRef, DataFusionError,
+};
+use datafusion_expr::{
+    expr::ScalarUDF, Extension, LogicalPlan, Subquery, UserDefinedLogicalNode,
+    UserDefinedLogicalNodeCore,
+};
+use datafusion_optimizer::{optimize_children, OptimizerConfig, OptimizerRule};
+use std::{
+    any::Any,
+    fmt::{self, Debug},
+    sync::Arc,
+};
+
+use futures::{FutureExt, StreamExt};
+
+// create local execution context with an in-memory table
+fn create_context() -> Result<SessionContext> {
+    // define a schema.
+    let schema = Arc::new(Schema::new(vec![
+        Field::new("a", DataType::Float32, false),
+        Field::new("b", DataType::Float64, false),
+    ]));
+
+    // define data.
+    let batch = RecordBatch::try_new(
+        schema,
+        vec![
+            Arc::new(Float32Array::from(vec![2.1, 3.1, 4.1, 5.1])),
+            Arc::new(Float64Array::from(vec![1.0, 2.0, 3.0, 4.0])),
+        ],
+    )?;
+
+    // declare a state with a query planner and an optimizer rule
+    let config = SessionConfig::new();
+    let runtime = Arc::new(RuntimeEnv::default());
+    let state = SessionState::with_config_rt(config, runtime)
+        .with_query_planner(Arc::new(PowQueryPlanner {}))
+        .add_optimizer_rule(Arc::new(PowOptimizerRule {}));
+
+    // declare a new context. In spark API, this corresponds to a new spark SQLsession
+    let ctx = SessionContext::with_state(state);
+
+    // declare a table in memory. In spark API, this corresponds to createDataFrame(...).
+    ctx.register_batch("t", batch)?;
+    Ok(ctx)
+}
+
+// pow is similar to the pow function in simple_udf example

Review Comment:
   Thank you @marshauf  -- really appreciated.
   
   I wonder what you would think about updating this example to be something slightly more realistic, like a UDF like "fetch_url" that gets a string as an argument and then "fetch"es some data remotely (which we would mock out for the example)?
   
   I can help update the PR if you like. 



-- 
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