bkmgit commented on a change in pull request #10296: URL: https://github.com/apache/arrow/pull/10296#discussion_r776187613
########## File path: docs/source/cpp/authoring_compute_functions.rst ########## @@ -0,0 +1,423 @@ +.. 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. + +.. default-domain:: cpp +.. highlight:: cpp +.. cpp:namespace:: arrow::compute + +=========================== +Authoring Compute Functions +=========================== + +Compute Functions +================= + +An introduction to compute functions is provided in https://arrow.apache.org/docs/cpp/compute.html. + +The [compute submodule](https://github.com/edponce/arrow/tree/master/cpp/src/arrow/compute) contains analytical functions that process primarily columnar data for either scalar or Arrow-based array inputs. These are intended for use inside query engines, data frame libraries, etc. + +Many functions have SQL-like semantics in that they perform element-wise or scalar operations on whole arrays at a time. Other functions are not SQL-like and compute results that may be a different length or whose results depend on the order of the values. + +Terminology: +* The term compute "function" refers to a particular general operation that may have many different implementations corresponding to different combinations of types or function behavior options. +* A specific implementation of a function is a "kernel". Selecting a viable kernel for executing a function is referred to as "dispatching". When executing a function on inputs, we must first select a suitable kernel corresponding to the value types of the inputs is selected. +* Functions along with their kernel implementations are collected in a "function registry". Given a function name and argument types, we can look up that function and dispatch to a compatible kernel. + +[Compute functions](https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/function.h) have the following principal attributes: +* A unique ["name"](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4NK5arrow7compute8Function4nameEv) used for function invocation and language bindings +* A ["kind"](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4N5arrow7compute8Function4KindE) + which indicates in what context it is valid for use + * Input/output [types](https://arrow.apache.org/docs/cpp/compute.html#type-categories) and [shapes](https://arrow.apache.org/docs/cpp/compute.html#input-shapes) + * Compute functions can also be further "categorized" based on the type of operation performed. For example, `Scalar Arithmetic` vs `Scalar String`. +* Compute functions (see [FunctionImpl and subclasses](https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/function.h)) contain ["kernels"](https://github.com/edponce/arrow/tree/master/cpp/src/arrow/compute/kernels) which are implementations for specific argument signatures. +* An ["arity"](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4N5arrow7compute5ArityE) which states the number of required arguments +for its core operation. Functions are commonly nullary, unary, binary, or ternary, but can also be variadic. +* ["Documentation"](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4N5arrow7compute11FunctionDocE) describing the function's functionality and behavior +* ["Options"](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4N5arrow7compute15FunctionOptionsE) specifying configuration of the function's behavior. + +Compute functions are grouped in source files based on their "kind" in https://github.com/apache/arrow/tree/master/cpp/src/arrow/compute. +Kernels of compute functions are grouped in source files based on their "kind" and category, see https://github.com/edponce/arrow/tree/master/cpp/src/arrow/compute/kernels. + + +Kinds of compute functions +-------------------------- + +Arrow uses an enumerated type to identify the kind of a compute function, refer to +https://github.com/edponce/arrow/tree/master/cpp/src/arrow/compute/function.h + +Scalar +~~~~~~ + +A function that performs scalar data operations on whole arrays of +data. Can generally process Array or Scalar values. The size of the +output will be the same as the size (or broadcasted size, in the case +of mixing Array and Scalar inputs) of the input. + +https://arrow.apache.org/docs/cpp/compute.html#arithmetic-functions + +**Categories of Scalar functions** + +* Arithmetic +* Comparisons +* Logical +* String + * predicates + * transforms + * trimming + * splitting + * extraction +* Containment tests +* Structural transforms +* Conversions + + +Vector +~~~~~~ + +A function with array input and output whose behavior depends on the +values of the entire arrays passed, rather than the value of each scalar value. + +**Categories of Vector functions** + +* Associative transforms +* Selections +* Sorts and partitions +* Structural transforms + + +Scalar aggregate +~~~~~~~~~~~~~~~~ + +A function that computes scalar summary statistics from array input. + +### Hash aggregate + +A function that computes grouped summary statistics from array input +and an array of group identifiers. + +Meta +~~~~ + +A function that dispatches to other functions and does not contain its own kernels. + + + +Kernels +------- + +Kernels are simple ``structs`` containing only function pointers (the "methods" of the kernel) and attribute flags. Each function kind corresponds to a class of Kernel with methods representing each stage of the function's execution. For example, :struct:`ScalarKernel` includes (optionally) :member:`ScalarKernel::init` to initialize any state necessary for execution and :member:`ScalarKernel::exec` to perform the computation. + +Since many kernels are closely related in operation and differ only in their input types, it's frequently useful to leverage c++'s powerful template system to efficiently generate kernels' methods. For example, the "add" compute function accepts all numeric types and its kernels' methods are instantiations of the same function template. + +Function options +---------------- + +[FunctionOptions](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4N5arrow7compute15FunctionOptionsE) + + +Function documentation +---------------------- + +[FunctionDoc](https://arrow.apache.org/docs/cpp/api/compute.html#_CPPv4N5arrow7compute11FunctionDocE) + + +Files and structures of the computer layer +========================================== + +This section describes the general structure of files/directory and principal code structures of the compute layer. + +* arrow/util/int_util_internal.h - defines utility functions + * Function definitions suffixed with `WithOverflow` to support "safe math" for arithmetic kernels. Helper macros are included to create the definitions which invoke the corresponding operation in [`portable_snippets`](https://github.com/apache/arrow/blob/master/cpp/src/arrow/vendored/portable-snippets/safe-math.h) library. + +* compute/api_scalar.h - contains + * Subclasses of `FunctionOptions` for specific categories of compute functions + * API/prototypes for all `Scalar` compute functions. Note that there is a single API version for each compute function. +* *compute/api_scalar.cc* - defines `Scalar` compute functions as wrappers over ["CallFunction"](https://arrow.apache.org/docs/cpp/api/compute.html?highlight=one%20shot#_CPPv412CallFunctionRKNSt6stringERKNSt6vectorI5DatumEEPK15FunctionOptionsP11ExecContext) (one-shot function). Arrow provides macros to easily define compute functions based on their `arity` and invocation mode. + * Macros of the form `SCALAR_EAGER_*` invoke `CallFunction` directly and only require one function name. + * Macros of the form `SCALAR_*` invoke `CallFunction` after checking for overflow and require two function names (default and `_checked` variant). Review comment: [Underflow](https://en.wikipedia.org/wiki/Arithmetic_underflow) may also be problematic. -- 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]
