yjshen commented on a change in pull request #1849:
URL: https://github.com/apache/arrow-datafusion/pull/1849#discussion_r808940636



##########
File path: datafusion/src/row/reader.rs
##########
@@ -44,6 +48,31 @@ pub fn read_as_batch(
     output.output().map_err(DataFusionError::ArrowError)
 }
 
+/// Read `data` of raw-bytes rows starting at `offsets` out to a record batch
+#[cfg(feature = "jit")]
+pub fn read_as_batch_jit(

Review comment:
       And this is the example usage pattern: generate code and compile once, 
and run repeatedly.
   
   

##########
File path: datafusion-jit/src/lib.rs
##########
@@ -0,0 +1,110 @@
+// 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.
+
+//! Just-In-Time compilation to accelerate DataFusion physical plan execution.
+
+pub mod api;
+pub mod ast;
+pub mod jit;
+
+#[cfg(test)]
+mod tests {
+    use crate::api::{Assembler, GeneratedFunction};
+    use crate::ast::I64;
+    use crate::jit::JIT;
+    use datafusion_common::Result;
+
+    #[test]
+    fn iterative_fib() -> Result<()> {
+        let expected = r#"fn iterative_fib_0(n: i64) -> r: i64 {
+    if n == 0 {
+        r = 0;
+    } else {
+        n = n - 1;
+        let a: i64;
+        a = 0;
+        r = 1;
+        while n != 0 {
+            let t: i64;
+            t = r;
+            r = r + a;
+            a = t;
+            n = n - 1;
+        }
+    }
+}"#;
+        let assembler = Assembler::default();
+        let mut builder = assembler
+            .new_func_builder("iterative_fib")
+            .param("n", I64)
+            .ret("r", I64);
+        let mut fn_body = builder.enter_block();
+
+        fn_body.if_block(
+            |cond| cond.eq(cond.id("n")?, cond.lit_i(0)),
+            |t| {
+                t.assign("r", t.lit_i(0))?;
+                Ok(())
+            },
+            |e| {
+                e.assign("n", e.sub(e.id("n")?, e.lit_i(1))?)?;
+                e.declare_as("a", e.lit_i(0))?;
+                e.assign("r", e.lit_i(1))?;
+                e.while_block(
+                    |cond| cond.ne(cond.id("n")?, cond.lit_i(0)),
+                    |w| {
+                        w.declare_as("t", w.id("r")?)?;
+                        w.assign("r", w.add(w.id("r")?, w.id("a")?)?)?;
+                        w.assign("a", w.id("t")?)?;
+                        w.assign("n", w.sub(w.id("n")?, w.lit_i(1))?)?;
+                        Ok(())
+                    },
+                )?;
+                Ok(())
+            },
+        )?;

Review comment:
       This is how we create an iterative version of Fibonacci calculation with 
the introduced `FunctionBuilder` API.

##########
File path: datafusion/src/row/reader.rs
##########
@@ -260,6 +289,136 @@ fn read_row(row: &RowReader, batch: &mut 
MutableRecordBatch, schema: &Arc<Schema
     }
 }
 
+#[cfg(feature = "jit")]
+fn get_array(
+    batch: &mut MutableRecordBatch,
+    col_idx: usize,
+) -> &mut Box<dyn ArrayBuilder> {
+    let arrays: &mut [Box<dyn ArrayBuilder>] = batch.arrays.as_mut();
+    &mut arrays[col_idx]
+}
+
+#[cfg(feature = "jit")]
+macro_rules! reg_fn {
+    ($ASS:ident, $FN: ident, $PARAM: expr, $RET: expr) => {
+        $ASS.register_extern_fn(fn_name($FN), $FN as *const u8, $PARAM, $RET)?;
+    };
+}
+
+#[cfg(feature = "jit")]
+fn fn_name<T>(f: T) -> &'static str {
+    fn type_name_of<T>(_: T) -> &'static str {
+        std::any::type_name::<T>()
+    }
+    let name = type_name_of(f);
+
+    // Find and cut the rest of the path
+    match &name.rfind(':') {
+        Some(pos) => &name[pos + 1..name.len()],
+        None => name,
+    }
+}
+
+#[cfg(feature = "jit")]
+fn register_read_functions(asm: &Assembler) -> Result<()> {
+    let reader_param = vec![PTR, I64, PTR];
+    reg_fn!(asm, get_array, vec![PTR, I64], Some(PTR));
+    reg_fn!(asm, read_field_bool, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u8, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u16, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u32, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u64, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i8, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i16, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i32, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i64, reader_param.clone(), None);
+    reg_fn!(asm, read_field_f32, reader_param.clone(), None);
+    reg_fn!(asm, read_field_f64, reader_param.clone(), None);
+    reg_fn!(asm, read_field_date32, reader_param.clone(), None);
+    reg_fn!(asm, read_field_date64, reader_param.clone(), None);
+    reg_fn!(asm, read_field_utf8, reader_param.clone(), None);
+    reg_fn!(asm, read_field_binary, reader_param.clone(), None);
+    reg_fn!(asm, read_field_bool_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u8_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u16_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u32_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_u64_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i8_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i16_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i32_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_i64_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_f32_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_f64_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_date32_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_date64_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_utf8_nf, reader_param.clone(), None);
+    reg_fn!(asm, read_field_binary_nf, reader_param, None);
+    Ok(())
+}
+
+#[cfg(feature = "jit")]
+fn gen_read_row(schema: &Arc<Schema>, assembler: &Assembler) -> Result<*const 
u8> {

Review comment:
       This is the other example of how we generate code based on the schema to 
create a row to record batch deserializer.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to