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

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 64fb8da  [feature] (function)(vec) support pmod function (#7977)
64fb8da is described below

commit 64fb8dab39358d70f8898f468b858e86ab101667
Author: Pxl <952130...@qq.com>
AuthorDate: Sat Feb 12 16:00:11 2022 +0800

    [feature] (function)(vec) support pmod function (#7977)
---
 be/src/vec/functions/functions_comparison.h        | 64 ----------------------
 be/src/vec/functions/modulo.cpp                    | 34 +++++++++++-
 docs/.vuepress/sidebar/en.js                       |  5 +-
 docs/.vuepress/sidebar/zh-CN.js                    |  5 +-
 .../sql-functions/math-functions/pmod.md           | 54 ++++++++++++++++++
 .../sql-functions/math-functions/pmod.md           | 53 ++++++++++++++++++
 gensrc/script/doris_builtins_functions.py          |  4 +-
 7 files changed, 148 insertions(+), 71 deletions(-)

diff --git a/be/src/vec/functions/functions_comparison.h 
b/be/src/vec/functions/functions_comparison.h
index 99070c2..8a6e102 100644
--- a/be/src/vec/functions/functions_comparison.h
+++ b/be/src/vec/functions/functions_comparison.h
@@ -126,70 +126,6 @@ struct GenericComparisonImpl {
     }
 };
 
-#if USE_EMBEDDED_COMPILER
-
-template <template <typename, typename> typename Op>
-struct CompileOp;
-
-template <>
-struct CompileOp<EqualsOp> {
-    static llvm::Value* compile(llvm::IRBuilder<>& b, llvm::Value* x, 
llvm::Value* y,
-                                bool /*is_signed*/) {
-        return x->getType()->is_integer_ty() ? b.CreateICmpEQ(x, y)
-                                             : b.CreateFCmpOEQ(x, y); /// 
qNaNs always compare false
-    }
-};
-
-template <>
-struct CompileOp<NotEqualsOp> {
-    static llvm::Value* compile(llvm::IRBuilder<>& b, llvm::Value* x, 
llvm::Value* y,
-                                bool /*is_signed*/) {
-        return x->getType()->is_integer_ty() ? b.CreateICmpNE(x, y) : 
b.CreateFCmpONE(x, y);
-    }
-};
-
-template <>
-struct CompileOp<LessOp> {
-    static llvm::Value* compile(llvm::IRBuilder<>& b, llvm::Value* x, 
llvm::Value* y,
-                                bool is_signed) {
-        return x->getType()->is_integer_ty()
-                       ? (is_signed ? b.CreateICmpSLT(x, y) : 
b.CreateICmpULT(x, y))
-                       : b.CreateFCmpOLT(x, y);
-    }
-};
-
-template <>
-struct CompileOp<GreaterOp> {
-    static llvm::Value* compile(llvm::IRBuilder<>& b, llvm::Value* x, 
llvm::Value* y,
-                                bool is_signed) {
-        return x->getType()->is_integer_ty()
-                       ? (is_signed ? b.CreateICmpSGT(x, y) : 
b.CreateICmpUGT(x, y))
-                       : b.CreateFCmpOGT(x, y);
-    }
-};
-
-template <>
-struct CompileOp<LessOrEqualsOp> {
-    static llvm::Value* compile(llvm::IRBuilder<>& b, llvm::Value* x, 
llvm::Value* y,
-                                bool is_signed) {
-        return x->getType()->is_integer_ty()
-                       ? (is_signed ? b.CreateICmpSLE(x, y) : 
b.CreateICmpULE(x, y))
-                       : b.CreateFCmpOLE(x, y);
-    }
-};
-
-template <>
-struct CompileOp<GreaterOrEqualsOp> {
-    static llvm::Value* compile(llvm::IRBuilder<>& b, llvm::Value* x, 
llvm::Value* y,
-                                bool is_signed) {
-        return x->getType()->is_integer_ty()
-                       ? (is_signed ? b.CreateICmpSGE(x, y) : 
b.CreateICmpUGE(x, y))
-                       : b.CreateFCmpOGE(x, y);
-    }
-};
-
-#endif
-
 struct NameEquals {
     static constexpr auto name = "eq";
 };
diff --git a/be/src/vec/functions/modulo.cpp b/be/src/vec/functions/modulo.cpp
index e0d2e6d..8fbfb8b 100644
--- a/be/src/vec/functions/modulo.cpp
+++ b/be/src/vec/functions/modulo.cpp
@@ -52,10 +52,32 @@ struct ModuloImpl {
         null_map[index] = b == DecimalV2Value(0);
         return a % (b + DecimalV2Value(b == DecimalV2Value(0)));
     }
+};
 
-#if USE_EMBEDDED_COMPILER
-    static constexpr bool compilable = false; /// don't know how to throw from 
LLVM IR
-#endif
+template <typename A, typename B>
+struct PModuloImpl {
+    using ResultType = typename NumberTraits::ResultOfModulo<A, B>::Type;
+
+    template <typename Result = ResultType>
+    static inline Result apply(A a, B b, NullMap& null_map, size_t index) {
+        if constexpr (std::is_floating_point_v<Result>) {
+            null_map[index] = 0;
+            return std::fmod(std::fmod((double)a, (double)b) + (double)b, 
(double)b);
+        } else {
+            null_map[index] = b == 0;
+            return (typename NumberTraits::ToInteger<A>::Type(a) %
+                            (typename NumberTraits::ToInteger<B>::Type(b) + (b 
== 0)) +
+                    typename NumberTraits::ToInteger<B>::Type(b)) %
+                   (typename NumberTraits::ToInteger<B>::Type(b) + (b == 0));
+        }
+    }
+
+    static inline DecimalV2Value apply(DecimalV2Value a, DecimalV2Value b, 
NullMap& null_map,
+                                       size_t index) {
+        null_map[index] = b == DecimalV2Value(0);
+        return (a % (b + DecimalV2Value(b == DecimalV2Value(0))) + b) %
+               (b + DecimalV2Value(b == DecimalV2Value(0)));
+    }
 };
 
 template <typename A, typename B>
@@ -151,10 +173,16 @@ struct BinaryOperationImpl<Int32, Int64, 
ModuloImpl<Int32, Int64>>
 struct NameModulo {
     static constexpr auto name = "mod";
 };
+struct NamePModulo {
+    static constexpr auto name = "pmod";
+};
+
 using FunctionModulo = FunctionBinaryArithmeticToNullType<ModuloImpl, 
NameModulo, false>;
+using FunctionPModulo = FunctionBinaryArithmeticToNullType<PModuloImpl, 
NamePModulo, false>;
 
 void register_function_modulo(SimpleFunctionFactory& factory) {
     factory.register_function<FunctionModulo>();
+    factory.register_function<FunctionPModulo>();
 }
 
 } // namespace doris::vectorized
diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js
index c7db8e3..e59ce6d 100644
--- a/docs/.vuepress/sidebar/en.js
+++ b/docs/.vuepress/sidebar/en.js
@@ -475,7 +475,10 @@ module.exports = [
           {
             title: "Math Functions",
             directoryPath: "math-functions/",
-            children: ["conv"],
+            children: [
+              "conv",
+              "pmod"
+            ],
           },
           {
             title: "table functions",
diff --git a/docs/.vuepress/sidebar/zh-CN.js b/docs/.vuepress/sidebar/zh-CN.js
index ba13f70..d7a4bf1 100644
--- a/docs/.vuepress/sidebar/zh-CN.js
+++ b/docs/.vuepress/sidebar/zh-CN.js
@@ -467,7 +467,10 @@ module.exports = [
           {
             title: "数学函数",
             directoryPath: "math-functions/",
-            children: ["conv"],
+            children: [
+              "conv",
+              "pmod"
+            ],
           },
           {
             title: "加密和信息摘要函数",
diff --git a/docs/en/sql-reference/sql-functions/math-functions/pmod.md 
b/docs/en/sql-reference/sql-functions/math-functions/pmod.md
new file mode 100644
index 0000000..a9693a0
--- /dev/null
+++ b/docs/en/sql-reference/sql-functions/math-functions/pmod.md
@@ -0,0 +1,54 @@
+---
+{
+    "title": "pmod",
+    "language": "en"
+}
+---
+
+<!-- 
+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.
+-->
+
+# pmod
+
+## description
+### Syntax
+
+`BIGINT PMOD(BIGINT x, BIGINT y)`
+`DOUBLE PMOD(DOUBLE x, DOUBLE y)`
+Returns the positive result of x mod y in the residue systems.
+Formally, return `(x%y+y)%y`.
+
+## example
+
+```
+MySQL [test]> SELECT PMOD(13,5);
++-------------+
+| pmod(13, 5) |
++-------------+
+|           3 |
++-------------+
+
+MySQL [test]> SELECT PMOD(-13,5);
++-------------+
+| pmod(-13, 5) |
++-------------+
+|           2 |
++-------------+
+```
+
+## keyword
+       PMOD
diff --git a/docs/zh-CN/sql-reference/sql-functions/math-functions/pmod.md 
b/docs/zh-CN/sql-reference/sql-functions/math-functions/pmod.md
new file mode 100644
index 0000000..d97968b
--- /dev/null
+++ b/docs/zh-CN/sql-reference/sql-functions/math-functions/pmod.md
@@ -0,0 +1,53 @@
+---
+{
+    "title": "pmod",
+    "language": "zh-CN"
+}
+---
+
+<!-- 
+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.
+-->
+
+# pmod
+
+## description
+### Syntax
+
+`BIGINT PMOD(BIGINT x, BIGINT y)`
+`DOUBLE PMOD(DOUBLE x, DOUBLE y)`
+返回在模系下`x mod y`的最小正数解.
+具体地来说, 返回 `(x%y+y)%y`.
+
+## example
+
+```
+MySQL [test]> SELECT PMOD(13,5);
++-------------+
+| pmod(13, 5) |
++-------------+
+|           3 |
++-------------+
+MySQL [test]> SELECT PMOD(-13,5);
++-------------+
+| pmod(-13, 5) |
++-------------+
+|           2 |
++-------------+
+```
+
+## keyword
+       PMOD
diff --git a/gensrc/script/doris_builtins_functions.py 
b/gensrc/script/doris_builtins_functions.py
index 1ce3395..b82925f 100755
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -637,10 +637,10 @@ visible_functions = [
 
     [['pmod'], 'BIGINT', ['BIGINT', 'BIGINT'],
             '_ZN5doris13MathFunctions11pmod_bigintEPN9doris_udf'
-            '15FunctionContextERKNS1_9BigIntValES6_', '', '', '', 
'ALWAYS_NULLABLE'],
+            '15FunctionContextERKNS1_9BigIntValES6_', '', '', 'vec', 
'ALWAYS_NULLABLE'],
     [['pmod'], 'DOUBLE', ['DOUBLE', 'DOUBLE'],
             '_ZN5doris13MathFunctions11pmod_doubleEPN9doris_udf'
-            '15FunctionContextERKNS1_9DoubleValES6_', '', '', '', 
'ALWAYS_NULLABLE'],
+            '15FunctionContextERKNS1_9DoubleValES6_', '', '', 'vec', 
'ALWAYS_NULLABLE'],
     [['mod'], 'TINYINT', ['TINYINT', 'TINYINT'],
             '_ZN5doris9Operators29mod_tiny_int_val_tiny_int_valEPN9doris_udf'
             '15FunctionContextERKNS1_10TinyIntValES6_', '', '', '', 
'ALWAYS_NULLABLE'],

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to