You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by jo...@apache.org on 2021/06/04 03:37:14 UTC

[arrow-datafusion] branch master updated: simplify python function definitions (#477)

This is an automated email from the ASF dual-hosted git repository.

jorgecarleitao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new 53792ec  simplify python function definitions (#477)
53792ec is described below

commit 53792ecf0bcaca6f15d36ca6a7e7f2b591c45831
Author: Jiayu Liu <Ji...@users.noreply.github.com>
AuthorDate: Fri Jun 4 11:37:03 2021 +0800

    simplify python function definitions (#477)
---
 python/README.md        |  16 ++-
 python/src/functions.rs | 352 ++++++++++++------------------------------------
 2 files changed, 98 insertions(+), 270 deletions(-)

diff --git a/python/README.md b/python/README.md
index 1859fca..50143ae 100644
--- a/python/README.md
+++ b/python/README.md
@@ -115,7 +115,7 @@ df = df.aggregate(
 )
 ```
 
-## How to install
+## How to install (from pip)
 
 ```bash
 pip install datafusion
@@ -135,12 +135,18 @@ cd arrow-datafusion/python
 
 # prepare development environment (used to build wheel / install in development)
 python3 -m venv venv
-pip install maturin==0.10.4 toml==0.10.1 pyarrow==1.0.0
+
+# activate the venv
+source venv/bin/activate
+
+# install dependencies
+pip install maturin==0.10.6 toml==0.10.1 pyarrow==4.0.0
 ```
 
-Whenever rust code changes (your changes or via git pull):
+Whenever rust code changes (your changes or via `git pull`):
 
 ```bash
-venv/bin/maturin develop
-venv/bin/python -m unittest discover tests
+# make sure you activate the venv using "source venv/bin/activate" first
+maturin develop
+python -m unittest discover tests
 ```
diff --git a/python/src/functions.rs b/python/src/functions.rs
index f46dd3e..b03004f 100644
--- a/python/src/functions.rs
+++ b/python/src/functions.rs
@@ -15,16 +15,13 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use std::sync::Arc;
-
-use datafusion::arrow::datatypes::DataType;
-use pyo3::{prelude::*, wrap_pyfunction};
-
-use datafusion::logical_plan;
-
 use crate::udaf;
 use crate::udf;
 use crate::{expression, types::PyDataType};
+use datafusion::arrow::datatypes::DataType;
+use datafusion::logical_plan;
+use pyo3::{prelude::*, wrap_pyfunction};
+use std::sync::Arc;
 
 /// Expression representing a column on the existing plan.
 #[pyfunction]
@@ -52,55 +49,6 @@ fn array(value: Vec<expression::Expression>) -> expression::Expression {
 }
 
 #[pyfunction]
-fn ascii(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::ascii(value.expr),
-    }
-}
-
-#[pyfunction]
-fn sum(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::sum(value.expr),
-    }
-}
-
-#[pyfunction]
-fn bit_length(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::bit_length(value.expr),
-    }
-}
-
-#[pyfunction]
-fn btrim(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::btrim(value.expr),
-    }
-}
-
-#[pyfunction]
-fn character_length(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::character_length(value.expr),
-    }
-}
-
-#[pyfunction]
-fn chr(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::chr(value.expr),
-    }
-}
-
-#[pyfunction]
-fn concat_ws(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::concat_ws(value.expr),
-    }
-}
-
-#[pyfunction]
 fn in_list(
     expr: expression::Expression,
     value: Vec<expression::Expression>,
@@ -115,215 +63,87 @@ fn in_list(
     }
 }
 
-#[pyfunction]
-fn initcap(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::initcap(value.expr),
-    }
-}
-
-#[pyfunction]
-fn left(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::left(value.expr),
-    }
-}
-
-#[pyfunction]
-fn lower(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::lower(value.expr),
-    }
-}
-
-#[pyfunction]
-fn lpad(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::lpad(value.expr),
-    }
-}
-
-#[pyfunction]
-fn ltrim(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::ltrim(value.expr),
-    }
-}
-
-#[pyfunction]
-fn md5(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::md5(value.expr),
-    }
-}
-
-#[pyfunction]
-fn octet_length(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::octet_length(value.expr),
-    }
-}
-
-#[pyfunction]
-fn regexp_replace(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::regexp_replace(value.expr),
-    }
-}
-
-#[pyfunction]
-fn repeat(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::repeat(value.expr),
-    }
-}
-
-#[pyfunction]
-fn replace(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::replace(value.expr),
-    }
-}
-
-#[pyfunction]
-fn reverse(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::reverse(value.expr),
-    }
-}
-
-#[pyfunction]
-fn right(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::right(value.expr),
-    }
-}
-
-#[pyfunction]
-fn rpad(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::rpad(value.expr),
-    }
-}
-
-#[pyfunction]
-fn rtrim(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::rtrim(value.expr),
-    }
-}
-
-#[pyfunction]
-fn sha224(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::sha224(value.expr),
-    }
-}
-
-#[pyfunction]
-fn sha256(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::sha256(value.expr),
-    }
-}
-
-#[pyfunction]
-fn sha384(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::sha384(value.expr),
-    }
-}
-
-#[pyfunction]
-fn sha512(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::sha512(value.expr),
-    }
-}
-
-#[pyfunction]
-fn split_part(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::split_part(value.expr),
-    }
-}
-
-#[pyfunction]
-fn starts_with(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::starts_with(value.expr),
-    }
-}
-
-#[pyfunction]
-fn strpos(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::strpos(value.expr),
-    }
-}
-
-#[pyfunction]
-fn substr(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::substr(value.expr),
-    }
-}
-
-#[pyfunction]
-fn to_hex(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::to_hex(value.expr),
-    }
-}
-
-#[pyfunction]
-fn translate(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::translate(value.expr),
-    }
-}
-
-#[pyfunction]
-fn trim(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::trim(value.expr),
-    }
-}
-
-#[pyfunction]
-fn upper(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::upper(value.expr),
-    }
-}
-
-#[pyfunction]
-fn avg(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::avg(value.expr),
-    }
-}
-
-#[pyfunction]
-fn min(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::min(value.expr),
-    }
-}
-
-#[pyfunction]
-fn max(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::max(value.expr),
-    }
-}
-
-#[pyfunction]
-fn count(value: expression::Expression) -> expression::Expression {
-    expression::Expression {
-        expr: logical_plan::count(value.expr),
-    }
-}
+macro_rules! define_function {
+    ($NAME: ident) => {
+        #[doc = "This function is not documented yet"]
+        #[pyfunction]
+        fn $NAME(value: expression::Expression) -> expression::Expression {
+            expression::Expression {
+                expr: logical_plan::$NAME(value.expr),
+            }
+        }
+    };
+    ($NAME: ident, $DOC: expr) => {
+        #[doc = $DOC]
+        #[pyfunction]
+        fn $NAME(value: expression::Expression) -> expression::Expression {
+            expression::Expression {
+                expr: logical_plan::$NAME(value.expr),
+            }
+        }
+    };
+}
+
+define_function!(ascii, "Returns the numeric code of the first character of the argument. In UTF8 encoding, returns the Unicode code point of the character. In other multibyte encodings, the argument must be an ASCII character.");
+define_function!(sum);
+define_function!(
+    bit_length,
+    "Returns number of bits in the string (8 times the octet_length)."
+);
+define_function!(btrim, "Removes the longest string containing only characters in characters (a space by default) from the start and end of string.");
+define_function!(
+    character_length,
+    "Returns number of characters in the string."
+);
+define_function!(chr, "Returns the character with the given code.");
+define_function!(concat_ws, "Concatenates all but the first argument, with separators. The first argument is used as the separator string, and should not be NULL. Other NULL arguments are ignored.");
+define_function!(initcap, "Converts the first letter of each word to upper case and the rest to lower case. Words are sequences of alphanumeric characters separated by non-alphanumeric characters.");
+define_function!(left, "Returns first n characters in the string, or when n is negative, returns all but last |n| characters.");
+define_function!(lower, "Converts the string to all lower case");
+define_function!(lpad, "Extends the string to length length by prepending the characters fill (a space by default). If the string is already longer than length then it is truncated (on the right).");
+define_function!(ltrim, "Removes the longest string containing only characters in characters (a space by default) from the start of string.");
+define_function!(
+    md5,
+    "Computes the MD5 hash of the argument, with the result written in hexadecimal."
+);
+define_function!(now);
+define_function!(octet_length, "Returns number of bytes in the string. Since this version of the function accepts type character directly, it will not strip trailing spaces.");
+define_function!(random, "Returns a random value in the range 0.0 <= x < 1.0");
+define_function!(
+    replace,
+    "Replaces all occurrences in string of substring from with substring to."
+);
+define_function!(repeat, "Repeats string the specified number of times.");
+define_function!(
+    regexp_replace,
+    "Replaces substring(s) matching a POSIX regular expression"
+);
+define_function!(
+    reverse,
+    "Reverses the order of the characters in the string."
+);
+define_function!(right, "Returns last n characters in the string, or when n is negative, returns all but first |n| characters.");
+define_function!(rpad, "Extends the string to length length by appending the characters fill (a space by default). If the string is already longer than length then it is truncated.");
+define_function!(rtrim, "Removes the longest string containing only characters in characters (a space by default) from the end of string.");
+define_function!(sha224);
+define_function!(sha256);
+define_function!(sha384);
+define_function!(sha512);
+define_function!(split_part, "Splits string at occurrences of delimiter and returns the n'th field (counting from one).");
+define_function!(starts_with, "Returns true if string starts with prefix.");
+define_function!(strpos,"Returns starting index of specified substring within string, or zero if it's not present. (Same as position(substring in string), but note the reversed argument order.)");
+define_function!(substr);
+define_function!(
+    to_hex,
+    "Converts the number to its equivalent hexadecimal representation."
+);
+define_function!(translate, "Replaces each character in string that matches a character in the from set with the corresponding character in the to set. If from is longer than to, occurrences of the extra characters in from are deleted.");
+define_function!(trim, "Removes the longest string containing only characters in characters (a space by default) from the start, end, or both ends (BOTH is the default) of string.");
+define_function!(upper, "Converts the string to all upper case.");
+define_function!(avg);
+define_function!(min);
+define_function!(max);
+define_function!(count);
 
 /*
 #[pyfunction]
@@ -414,8 +234,10 @@ pub fn init(module: &PyModule) -> PyResult<()> {
     module.add_function(wrap_pyfunction!(lower, module)?)?;
     module.add_function(wrap_pyfunction!(lpad, module)?)?;
     module.add_function(wrap_pyfunction!(md5, module)?)?;
+    module.add_function(wrap_pyfunction!(now, module)?)?;
     module.add_function(wrap_pyfunction!(ltrim, module)?)?;
     module.add_function(wrap_pyfunction!(octet_length, module)?)?;
+    module.add_function(wrap_pyfunction!(random, module)?)?;
     module.add_function(wrap_pyfunction!(regexp_replace, module)?)?;
     module.add_function(wrap_pyfunction!(repeat, module)?)?;
     module.add_function(wrap_pyfunction!(replace, module)?)?;