[FEAT MERGE]4_2_sql_feature

Co-authored-by: yinyj17 <yinyijun92@gmail.com>
Co-authored-by: xianyu-w <707512433@qq.com>
Co-authored-by: jingtaoye35 <1255153887@qq.com>
This commit is contained in:
zzg19950727
2023-04-28 11:12:11 +00:00
committed by ob-robot
parent 3bf92459f1
commit 3cada22bdc
161 changed files with 16883 additions and 4730 deletions

View File

@ -415,6 +415,9 @@ extern int eval_batch_ceil_floor(const ObExpr &, ObEvalCtx &, const ObBitVector
extern int eval_assign_question_mark_func(EVAL_FUNC_ARG_DECL);
extern int calc_timestamp_to_scn_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_scn_to_timestamp_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sqrt_expr_mysql_in_batch(const ObExpr &, ObEvalCtx &, const ObBitVector &, const int64_t);
extern int calc_sqrt_expr_oracle_double_in_batch(const ObExpr &, ObEvalCtx &, const ObBitVector &, const int64_t);
extern int calc_sqrt_expr_oracle_number_in_batch(const ObExpr &, ObEvalCtx &, const ObBitVector &, const int64_t);
// append only, can not delete, set to NULL for mark delete
static ObExpr::EvalFunc g_expr_eval_functions[] = {
@ -1140,7 +1143,10 @@ static ObExpr::EvalBatchFunc g_expr_eval_batch_functions[] = {
ObExprDecode::eval_decode_batch, /* 108 */
ObExprCoalesce::calc_batch_coalesce_expr, /* 109 */
ObExprIsNot::calc_batch_is_not_null, /* 110 */
NULL //ObExprNlsInitCap::calc_nls_initcap_batch /* 111 */
NULL, //ObExprNlsInitCap::calc_nls_initcap_batch /* 111 */
calc_sqrt_expr_mysql_in_batch, /* 114 */
calc_sqrt_expr_oracle_double_in_batch, /* 115 */
calc_sqrt_expr_oracle_number_in_batch /* 116 */
};
REG_SER_FUNC_ARRAY(OB_SFA_SQL_EXPR_EVAL,

View File

@ -3525,13 +3525,44 @@ int ObSubQueryRelationalExpr::setup_row(
int ObSubQueryRelationalExpr::cmp_one_row(
const ObExpr &expr, ObDatum &res,
ObExpr **l_row, ObEvalCtx &l_ctx, ObExpr **r_row, ObEvalCtx &r_ctx)
ObExpr **l_row, ObEvalCtx &l_ctx, ObExpr **r_row, ObEvalCtx &r_ctx,
bool left_all_null, bool right_all_null)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(T_OP_SQ_NSEQ == expr.type_)) {
ret = ObExprNullSafeEqual::ns_equal(expr, res, l_row, l_ctx, r_row, r_ctx);
if (left_all_null && right_all_null) {
res.set_true();
} else if (left_all_null || right_all_null) {
bool both_are_null = true;
ObDatum *l = NULL;
ObDatum *r = NULL;
for (int64_t i = 0; OB_SUCC(ret) && both_are_null && i < expr.inner_func_cnt_; i++) {
if (NULL == expr.inner_functions_[i]) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("NULL inner function", K(ret), K(i), K(expr));
} else if ((!left_all_null && OB_FAIL(l_row[i]->eval(l_ctx, l)))
|| (!right_all_null && OB_FAIL(r_row[i]->eval(r_ctx, r)))) {
LOG_WARN("expr evaluate failed", K(ret));
} else {
if ((left_all_null || l->is_null()) && (right_all_null || r->is_null())) {
both_are_null = true;
} else {
both_are_null = false;
}
}
}
if (OB_SUCC(ret)) {
res.set_int(both_are_null);
}
} else {
ret = ObExprNullSafeEqual::ns_equal(expr, res, l_row, l_ctx, r_row, r_ctx);
}
} else {
ret = ObRelationalExprOperator::row_cmp(expr, res, l_row, l_ctx, r_row, r_ctx);
if (left_all_null || right_all_null) {
res.set_null();
} else {
ret = ObRelationalExprOperator::row_cmp(expr, res, l_row, l_ctx, r_row, r_ctx);
}
}
return ret;
}
@ -3547,6 +3578,7 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval(
ObExpr **r_row = NULL;
ObEvalCtx *l_ctx = NULL;
ObEvalCtx *r_ctx = NULL;
bool left_all_null = false;
if (2 != expr.arg_cnt_) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected argument count", K(ret));
@ -3567,11 +3599,7 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval(
if (OB_ITER_END == ret) {
ret = OB_SUCCESS;
l_end = true;
// set row to NULL
for (int64_t i = 0; i < expr.inner_func_cnt_; i++) {
l_row[i]->locate_expr_datum(*l_ctx).set_null();
l_row[i]->set_evaluated_projected(*l_ctx);
}
left_all_null = true;
} else {
LOG_WARN("get next row failed", K(ret));
}
@ -3580,15 +3608,15 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval(
if (OB_SUCC(ret)) {
switch (info.subquery_key_) {
case T_WITH_NONE: {
ret = subquery_cmp_eval_with_none(expr, *l_ctx, expr_datum, l_row, *r_ctx, r_row, r_iter);
ret = subquery_cmp_eval_with_none(expr, *l_ctx, expr_datum, l_row, *r_ctx, r_row, r_iter, left_all_null);
break;
}
case T_WITH_ANY: {
ret = subquery_cmp_eval_with_any(expr, *l_ctx, expr_datum, l_row, *r_ctx, r_row, r_iter);
ret = subquery_cmp_eval_with_any(expr, *l_ctx, expr_datum, l_row, *r_ctx, r_row, r_iter, left_all_null);
break;
}
case T_WITH_ALL: {
ret = subquery_cmp_eval_with_all(expr, *l_ctx, expr_datum, l_row, *r_ctx, r_row, r_iter);
ret = subquery_cmp_eval_with_all(expr, *l_ctx, expr_datum, l_row, *r_ctx, r_row, r_iter, left_all_null);
break;
}
}
@ -3611,29 +3639,27 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval(
int ObSubQueryRelationalExpr::subquery_cmp_eval_with_none(
const ObExpr &expr, ObEvalCtx &l_ctx, ObDatum &res,
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter)
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter,
bool left_all_null)
{
int ret = OB_SUCCESS;
// %l_row, %r_row is checked no need to check.
// %iter may be NULL for with none.
bool iter_end = false;
bool right_all_null = false;
if (NULL != r_iter) {
if (OB_FAIL(r_iter->get_next_row())) {
if (OB_ITER_END == ret) {
ret = OB_SUCCESS;
iter_end = true;
// set row to NULL
for (int64_t i = 0; i < expr.inner_func_cnt_; i++) {
r_row[i]->locate_expr_datum(r_ctx).set_null();
r_row[i]->set_evaluated_projected(r_ctx);
}
right_all_null = true;
} else {
LOG_WARN("get next row failed", K(ret));
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(cmp_one_row(expr, res, l_row, l_ctx, r_row, r_ctx))) {
if (OB_FAIL(cmp_one_row(expr, res, l_row, l_ctx, r_row, r_ctx, left_all_null, right_all_null))) {
LOG_WARN("compare one row failed", K(ret));
}
}
@ -3655,10 +3681,12 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval_with_none(
// copy from ObSubQueryRelationalExpr::calc_result_with_any
int ObSubQueryRelationalExpr::subquery_cmp_eval_with_any(
const ObExpr &expr, ObEvalCtx &l_ctx, ObDatum &res,
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter)
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter,
bool left_all_null)
{
// %l_row, %r_row is checked no need to check.
int ret = OB_SUCCESS;
bool right_all_null = false;
if (OB_ISNULL(r_iter)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("iter should not be null", K(ret));
@ -3668,7 +3696,7 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval_with_any(
res.set_false();
while (OB_SUCC(ret) && OB_SUCC(r_iter->get_next_row())) {
// use subquery's eval ctx for right row to avoid ObEvalCtx::alloc_ expanding.
if (OB_FAIL(cmp_one_row(expr, res, l_row, l_ctx, r_row, r_ctx))) {
if (OB_FAIL(cmp_one_row(expr, res, l_row, l_ctx, r_row, r_ctx, left_all_null, right_all_null))) {
LOG_WARN("compare single row failed", K(ret));
} else if (res.is_true()) {
break;
@ -3692,10 +3720,12 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval_with_any(
// copy from ObSubQueryRelationalExpr::calc_result_with_all
int ObSubQueryRelationalExpr::subquery_cmp_eval_with_all(
const ObExpr &expr, ObEvalCtx &l_ctx, ObDatum &res,
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter)
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter,
bool left_all_null)
{
// %l_row, %r_row is checked no need to check.
int ret = OB_SUCCESS;
bool right_all_null = false;
if (OB_ISNULL(r_iter)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("iter should not be null", K(ret));
@ -3705,7 +3735,7 @@ int ObSubQueryRelationalExpr::subquery_cmp_eval_with_all(
res.set_true();
while (OB_SUCC(ret) && OB_SUCC(r_iter->get_next_row())) {
// use subquery's eval ctx for right row to avoid ObEvalCtx::alloc_ expanding.
if (OB_FAIL(cmp_one_row(expr, res, l_row, l_ctx, r_row, r_ctx))) {
if (OB_FAIL(cmp_one_row(expr, res, l_row, l_ctx, r_row, r_ctx, left_all_null, right_all_null))) {
LOG_WARN("compare single row failed", K(ret));
} else if (res.is_false()) {
break;

View File

@ -1463,16 +1463,17 @@ protected:
static int subquery_cmp_eval_with_none(
const ObExpr &expr, ObEvalCtx &l_ctx, ObDatum &res,
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter);
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter, bool left_all_null);
static int subquery_cmp_eval_with_any(
const ObExpr &expr, ObEvalCtx &l_ctx, ObDatum &res,
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter);
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter, bool left_all_null);
static int subquery_cmp_eval_with_all(
const ObExpr &expr, ObEvalCtx &l_ctx, ObDatum &res,
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter);
ObExpr **l_row, ObEvalCtx &r_ctx, ObExpr **r_row, ObSubQueryIterator *r_iter, bool left_all_null);
static int cmp_one_row(const ObExpr &expr, ObDatum &res,
ObExpr **l_row, ObEvalCtx &l_ctx, ObExpr **r_row, ObEvalCtx &r_ctx);
ObExpr **l_row, ObEvalCtx &l_ctx, ObExpr **r_row, ObEvalCtx &r_ctx,
bool left_all_null, bool right_all_null);
static int check_exists(const ObExpr &expr, ObEvalCtx &ctx, bool &exists);

View File

@ -13,6 +13,7 @@
#define USING_LOG_PREFIX SQL_ENG
#include "ob_expr_sqrt.h"
#include <cmath>
#include <type_traits>
#include "share/object/ob_obj_cast.h"
#include "objit/common/ob_item_type.h"
//#include "sql/engine/expr/ob_expr_promotion_util.h"
@ -78,6 +79,31 @@ int calc_sqrt_expr_mysql(const ObExpr &expr, ObEvalCtx &ctx,
return ret;
}
int calc_sqrt_expr_mysql_in_batch(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const int64_t batch_size)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
LOG_WARN("vectorized evaluate failed", K(ret), K(expr));
} else {
ObBitVector &eval_flag = expr.get_evaluated_flags(ctx);
ObDatumVector arg_datums = expr.args_[0]->locate_expr_datumvector(ctx);
ObDatum *res_datums = expr.locate_batch_datums(ctx);
for (int64_t i = 0; i < batch_size; ++i) {
if (!eval_flag.contain(i) && !skip.contain(i)) {
if (arg_datums.at(i)->is_null() || arg_datums.at(i)->get_double() < 0) {
res_datums[i].set_null();
} else {
res_datums[i].set_double(std::sqrt(arg_datums.at(i)->get_double()));
}
}
}
}
return ret;
}
int calc_sqrt_expr_oracle_double(const ObExpr &expr, ObEvalCtx &ctx,
ObDatum &res_datum)
{
@ -111,6 +137,72 @@ int calc_sqrt_expr_oracle_double(const ObExpr &expr, ObEvalCtx &ctx,
return ret;
}
template <typename T>
int calc_sqrt_expr_oracle_double_in_batch_impl(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const int64_t batch_size)
{
ObBitVector &eval_flag = expr.get_evaluated_flags(ctx);
ObDatumVector arg_datums = expr.args_[0]->locate_expr_datumvector(ctx);
ObDatum *res_datums = expr.locate_batch_datums(ctx);
int ret = OB_SUCCESS;
if (std::is_same<T, float>::value) {
for (int64_t i = 0; i < batch_size && OB_SUCC(ret); ++i) {
if (!eval_flag.contain(i) && !skip.contain(i)) {
ObDatum *arg = arg_datums.at(i);
if (arg->is_null()) {
res_datums[i].set_null();
} else if (arg->get_float() < 0) {
res_datums[i].set_float(NAN);
} else {
res_datums[i].set_float(std::sqrt(arg->get_float()));
}
}
}
} else if (std::is_same<T, double>::value) {
for (int64_t i = 0; i < batch_size && OB_SUCC(ret); ++i) {
if (!eval_flag.contain(i) && !skip.contain(i)) {
ObDatum *arg = arg_datums.at(i);
if (arg->is_null()) {
res_datums[i].set_null();
} else if (arg->get_double() < 0) {
res_datums[i].set_double(NAN);
} else {
res_datums[i].set_double(std::sqrt(arg->get_double()));
}
}
}
}
return ret;
}
int calc_sqrt_expr_oracle_double_in_batch(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const int64_t batch_size)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
LOG_WARN("vectorized evaluate failed", K(ret), K(expr));
} else {
ObObjType arg_type = expr.args_[0]->datum_meta_.type_;
switch (arg_type) {
case ObDoubleType:
ret = calc_sqrt_expr_oracle_double_in_batch_impl<double>(expr, ctx, skip, batch_size);
break;
case ObFloatType:
ret = calc_sqrt_expr_oracle_double_in_batch_impl<float>(expr, ctx, skip, batch_size);
break;
default:
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected arg type", K(ret), K(arg_type));
break;
}
}
return ret;
}
int calc_sqrt_expr_oracle_number(const ObExpr &expr, ObEvalCtx &ctx,
ObDatum &res_datum)
{
@ -133,6 +225,41 @@ int calc_sqrt_expr_oracle_number(const ObExpr &expr, ObEvalCtx &ctx,
return ret;
}
int calc_sqrt_expr_oracle_number_in_batch(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const int64_t batch_size)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
LOG_WARN("vectorized evaluate failed", K(ret), K(expr));
} else {
ObBitVector &eval_flag = expr.get_evaluated_flags(ctx);
ObDatumVector arg_datums = expr.args_[0]->locate_expr_datumvector(ctx);
ObDatum *res_datums = expr.locate_batch_datums(ctx);
number::ObNumber res_nmb;
number::ObNumber arg_nmb;
char temp_buf_alloc[number::ObNumber::MAX_CALC_BYTE_LEN];
ObDataBuffer temp_allocator(temp_buf_alloc, number::ObNumber::MAX_CALC_BYTE_LEN);
for (int64_t i = 0; i < batch_size && OB_SUCC(ret); ++i) {
if (!eval_flag.contain(i) && !skip.contain(i)) {
if (arg_datums.at(i)->is_null()) {
res_datums[i].set_null();
} else {
arg_nmb = arg_datums.at(i)->get_number();
if (OB_FAIL(arg_nmb.sqrt(res_nmb, temp_allocator))) {
LOG_WARN("calc sqrt failed", K(ret), K(arg_nmb), K(res_nmb));
} else {
res_datums[i].set_number(res_nmb);
}
temp_allocator.free();
}
}
}
}
return ret;
}
int ObExprSqrt::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
@ -147,8 +274,10 @@ int ObExprSqrt::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
if (lib::is_oracle_mode()) {
if (ObDoubleType == arg_res_type || ObFloatType == arg_res_type) {
rt_expr.eval_func_ = calc_sqrt_expr_oracle_double;
rt_expr.eval_batch_func_ = calc_sqrt_expr_oracle_double_in_batch;
} else if (ObNumberType == arg_res_type) {
rt_expr.eval_func_ = calc_sqrt_expr_oracle_number;
rt_expr.eval_batch_func_ = calc_sqrt_expr_oracle_number_in_batch;
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("arg type must be double or number in oracle mode", K(ret),
@ -157,6 +286,7 @@ int ObExprSqrt::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
} else {
if (ObDoubleType == arg_res_type) {
rt_expr.eval_func_ = calc_sqrt_expr_mysql;
rt_expr.eval_batch_func_ = calc_sqrt_expr_mysql_in_batch;
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("arg_res_type must be double in mysql mode", K(ret), K(arg_res_type));