Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions be/src/vec/functions/functions_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
Expand Down
34 changes: 31 additions & 3 deletions be/src/vec/functions/modulo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion docs/.vuepress/sidebar/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,10 @@ module.exports = [
{
title: "Math Functions",
directoryPath: "math-functions/",
children: ["conv"],
children: [
"conv",
"pmod"
],
},
{
title: "table functions",
Expand Down
5 changes: 4 additions & 1 deletion docs/.vuepress/sidebar/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ module.exports = [
{
title: "数学函数",
directoryPath: "math-functions/",
children: ["conv"],
children: [
"conv",
"pmod"
],
},
{
title: "加密和信息摘要函数",
Expand Down
54 changes: 54 additions & 0 deletions docs/en/sql-reference/sql-functions/math-functions/pmod.md
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions docs/zh-CN/sql-reference/sql-functions/math-functions/pmod.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@

[['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'],
Expand Down