[FEAT MERGE] Lob SQL refactoring (Mem-LobLocator, expressions and dbms_lob adaptions)

Co-authored-by: chaser-ch <chaser.ch@antgroup.com>
This commit is contained in:
obdev
2023-01-28 20:40:15 +08:00
committed by ob-robot
parent 4bb1033505
commit 3d4f554258
350 changed files with 19091 additions and 3918 deletions

View File

@ -19,6 +19,7 @@
#include "sql/engine/expr/ob_expr_ascii.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_lob_utils.h"
namespace oceanbase
{
@ -72,6 +73,18 @@ int ObExprAscii::calc_result_type1(ObExprResType &type,
return ret;
}
inline void calc_ascii_inner(common::ObObj &obj, ObExprCtx &expr_ctx, const ObString &str_val)
{
uint8_t tiny_val;
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
if (str_val.length() <= 0){
tiny_val = 0;
} else {
tiny_val = static_cast<uint8_t>(str_val[0]);
}
obj.set_utinyint(tiny_val);
}
int ObExprAscii::calc(common::ObObj &obj,
const common::ObObj &obj1,
ObExprCtx &expr_ctx)
@ -83,21 +96,29 @@ int ObExprAscii::calc(common::ObObj &obj,
LOG_WARN("varchar buffer not init", K(ret));
} else if (obj1.is_null()) {
obj.set_null();
} else {
uint8_t tiny_val;
} else if (!ob_is_text_tc(obj1.get_type())) {
ObString str_val = obj1.get_string();
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
if (str_val.length() <= 0){
tiny_val = 0;
calc_ascii_inner(obj, expr_ctx, str_val);
} else {
ObString str_val = obj1.get_string();
if (OB_FAIL(ObTextStringHelper::read_prefix_string_data(expr_ctx.calc_buf_, obj1, str_val))) {
LOG_WARN("failed to get string data", K(ret), K(obj1.get_meta()));
} else {
tiny_val = static_cast<uint8_t>(str_val[0]);
calc_ascii_inner(obj, expr_ctx, str_val);
}
obj.set_utinyint(tiny_val);
}
return ret;
}
inline void calc_ascii_expr_inner(ObDatum &res_datum, const ObString &str_val)
{
if (str_val.empty()) {
res_datum.set_int32(0);
} else {
res_datum.set_int32(static_cast<uint8_t>(str_val[0]));
}
}
int ObExprAscii::calc_ascii_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
int ret = OB_SUCCESS;
@ -111,12 +132,22 @@ int ObExprAscii::calc_ascii_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &re
LOG_WARN("eval arg failed", K(ret));
} else if (s_datum->is_null()) {
res_datum.set_null();
} else {
} else if (!ob_is_text_tc(expr.args_[0]->datum_meta_.type_)) {
const ObString &str_val = s_datum->get_string();
if (str_val.empty()) {
res_datum.set_int32(0);
calc_ascii_expr_inner(res_datum, str_val);
} else { // text tc only
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &temp_allocator = tmp_alloc_g.get_allocator();
ObString str_val = s_datum->get_string();
if (OB_FAIL(ObTextStringHelper::read_prefix_string_data(ctx,
*s_datum,
expr.args_[0]->datum_meta_,
expr.args_[0]->obj_meta_.has_lob_header(),
&temp_allocator,
str_val))) {
LOG_WARN("failed to get lob data", K(ret), K(expr.args_[0]->datum_meta_));
} else {
res_datum.set_int32(static_cast<uint8_t>(str_val[0]));
calc_ascii_expr_inner(res_datum, str_val);
}
}
}
@ -160,6 +191,33 @@ int ObExprOrd::calc_result_type1(ObExprResType &type,
return ret;
}
inline int calc_ord_inner(uint8_t &type, const ObString &str_val,
const ObCollationType &cs_type, common::ObObj &obj)
{
int ret = OB_SUCCESS;
const char *str_ptr = str_val.ptr();
if (NULL != str_ptr) {
const char *end = str_val.ptr() + str_val.length();
const ObCharsetInfo *cs = ObCharset::get_charset(cs_type);
uint64_t n = 0, char_len = ob_ismbchar(cs, str_ptr, end);
if (char_len > str_val.length()){
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ob_ismbchar return wrong value",
K(ret), K(char_len), K(str_val.length()));
} else if (0 == char_len){
type = 1;
} else {
while (char_len--){
n = (n << 8) | (uint64_t)((uint8_t) *str_ptr++);
}
obj.set_uint64(n);
}
} else {
type = 1;
}
return ret;
}
int ObExprOrd::calc(common::ObObj &obj,
const common::ObObj &obj1,
ObExprCtx &expr_ctx)
@ -177,26 +235,12 @@ int ObExprOrd::calc(common::ObObj &obj,
// test mb then calc
// String trans
ObString str_val = obj1.get_string();
const char *str_ptr = str_val.ptr();
if (NULL != str_ptr) {
const char *end = str_val.ptr() + str_val.length();
const ObCharsetInfo *cs = ObCharset::get_charset(cs_type);
uint64_t n = 0, char_len = ob_ismbchar(cs, str_ptr, end);
if (char_len > str_val.length()){
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ob_ismbchar return wrong value",
K(ret), K(char_len), K(str_val.length()));
} else if (0 == char_len){
type = 1;
} else {
while (char_len--){
n = (n << 8) | (uint64_t)((uint8_t) *str_ptr++);
}
obj.set_uint64(n);
}
if (!ob_is_text_tc(obj1.get_type())) {
ret = calc_ord_inner(type, str_val, cs_type, obj);
} else if (OB_FAIL(ObTextStringHelper::read_prefix_string_data(expr_ctx.calc_buf_, obj1, str_val))) {
LOG_WARN("failed to get lob data", K(ret), K(obj1.get_meta()));
} else {
type = 1;
ret = calc_ord_inner(type, str_val, cs_type, obj);
}
} else {
type = 1;
@ -212,7 +256,45 @@ int ObExprOrd::calc(common::ObObj &obj,
return ret;
}
// Oracle模式下的ascii表达式的计算逻辑也使用本函数
static int calc_ord_expr_inner(const ObCollationType &cs_type,
const ObString &str_val,
int64_t &res_int,
bool &res_null)
{
int ret = OB_SUCCESS;
const ObCharsetInfo *cs = ObCharset::get_charset(cs_type);
if (OB_ISNULL(cs)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid cs_type", K(ret), K(cs_type));
} else if (str_val.empty()) {
if (is_oracle_mode()) {
res_null = true;
} else {
res_int = 0;
}
} else if (ObCharset::usemb(cs_type)) {
const char *str_ptr = str_val.ptr();
const char *end = str_val.ptr() + str_val.length();
uint32_t n = 0;
uint32_t char_len = ob_ismbchar(cs, str_ptr, end);
if (char_len > str_val.length()){
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ob_ismbchar return wrong value", K(ret), K(char_len), K(str_val.length()));
} else if (0 == char_len){
res_int = static_cast<uint8_t>(str_val[0]);
} else {
while (char_len--){
n = (n << 8) | static_cast<uint32_t>(static_cast<uint8_t>(*str_ptr++));
}
res_int = n;
}
} else {
res_int = static_cast<uint8_t>(str_val[0]);
}
return ret;
}
// Oracle mode ascii expression also use this
int ObExprOrd::calc_ord_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
int ret = OB_SUCCESS;
@ -225,35 +307,24 @@ int ObExprOrd::calc_ord_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_da
int64_t res_int = 0;
bool res_null = false;
const ObCollationType cs_type = expr.args_[0]->datum_meta_.cs_type_;
const ObCharsetInfo *cs = ObCharset::get_charset(cs_type);
const ObString &str_val = s_datum->get_string();
if (OB_ISNULL(cs)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid cs_type", K(ret), K(cs_type));
} else if (str_val.empty()) {
if (is_oracle_mode()) {
res_null = true;
if (!ob_is_text_tc(expr.args_[0]->datum_meta_.type_)) {
const ObString &str_val = s_datum->get_string();
ret = calc_ord_expr_inner(cs_type, str_val, res_int, res_null);
} else { // text tc only
ObString str_val = s_datum->get_string();
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &temp_allocator = tmp_alloc_g.get_allocator();
if (OB_FAIL(ObTextStringHelper::read_prefix_string_data(ctx,
*s_datum,
expr.args_[0]->datum_meta_,
expr.args_[0]->obj_meta_.has_lob_header(),
&temp_allocator,
str_val))) {
LOG_WARN("failed to get string data", K(ret), K(expr.args_[0]->datum_meta_));
} else {
res_int = 0;
ret = calc_ord_expr_inner(cs_type, str_val, res_int, res_null);
}
} else if (ObCharset::usemb(cs_type)) {
const char *str_ptr = str_val.ptr();
const char *end = str_val.ptr() + str_val.length();
uint32_t n = 0;
uint32_t char_len = ob_ismbchar(cs, str_ptr, end);
if (char_len > str_val.length()){
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ob_ismbchar return wrong value", K(ret), K(char_len), K(str_val.length()));
} else if (0 == char_len){
res_int = static_cast<uint8_t>(str_val[0]);
} else {
while (char_len--){
n = (n << 8) | static_cast<uint32_t>(static_cast<uint8_t>(*str_ptr++));
}
res_int = n;
}
} else {
res_int = static_cast<uint8_t>(str_val[0]);
// Notice: str_val cannot be used outside of this branch, it may from temp allocator
}
if (OB_SUCC(ret)) {