// Copyright 2024 RisingWave Labs // // Licensed 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 std::sync::Arc; use arrow_array::{Int32Array, RecordBatch}; use arrow_schema::{DataType, Field, Schema}; use arrow_udf_js::{CallMode, Runtime}; fn main() { let mut runtime = Runtime::new().unwrap(); runtime .add_function( "gcd", DataType::Int32, CallMode::ReturnNullOnNullInput, r#" export function gcd(a, b) { while (b != 0) { let t = b; b = a % b; a = t; } return a; } "#, ) .unwrap(); runtime .add_function( "fib", DataType::Int32, CallMode::ReturnNullOnNullInput, r#" export function fib(x) { if (x <= 1) return x; return fib(x - 1) + fib(x - 2); } "#, ) .unwrap(); println!("call gcd"); let input = RecordBatch::try_new( Arc::new(Schema::new(vec![ Field::new("a", DataType::Int32, true), Field::new("b", DataType::Int32, true), ])), vec![ Arc::new(Int32Array::from(vec![Some(15), None])), Arc::new(Int32Array::from(vec![25, 2])), ], ) .unwrap(); let output = runtime.call("gcd", &input).unwrap(); arrow_cast::pretty::print_batches(std::slice::from_ref(&input)).unwrap(); arrow_cast::pretty::print_batches(std::slice::from_ref(&output)).unwrap(); println!("call fib"); let input = RecordBatch::try_new( Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)])), vec![Arc::new(Int32Array::from(vec![10]))], ) .unwrap(); let output = runtime.call("fib", &input).unwrap(); arrow_cast::pretty::print_batches(std::slice::from_ref(&input)).unwrap(); arrow_cast::pretty::print_batches(std::slice::from_ref(&output)).unwrap(); }