Skip to content

Commit

Permalink
fix:json compare with const string, behavior not consistent with mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
obdev authored and ob-robot committed Dec 13, 2023
1 parent 82edabc commit e28f81d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/share/object/ob_obj_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6012,11 +6012,12 @@ static int string_json(const ObObjType expect_type, ObObjCastParams &params,
j_base = &j_null;
} else if (!is_oracle && CS_TYPE_BINARY == in.get_collation_type()) {
j_base = &j_opaque;
} else if (!is_oracle
&& CM_IS_IMPLICIT_CAST(cast_mode)
&& !CM_IS_COLUMN_CONVERT(cast_mode)
&& !CM_IS_JSON_VALUE(cast_mode)
&& is_convert_jstr_type) {
} else if (!is_oracle && (
(CM_IS_SQL_AS_JSON_SCALAR(cast_mode) && ob_is_string_type(in_type))
|| (CM_IS_IMPLICIT_CAST(cast_mode)
&& !CM_IS_COLUMN_CONVERT(cast_mode)
&& !CM_IS_JSON_VALUE(cast_mode)
&& is_convert_jstr_type))) {
// consistent with mysql: TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. We want to treat them like strings
ret = OB_SUCCESS;
j_base = &j_string;
Expand Down
4 changes: 4 additions & 0 deletions src/share/object/ob_obj_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace common
#define CM_GEOMETRY_TYPE_RESERVED3 (1ULL << 14)
#define CM_GEOMETRY_TYPE_RESERVED4 (1ULL << 15)
#define CM_GEOMETRY_TYPE_RESERVED5 (1ULL << 16)
#define CM_SQL_TO_JSON_SCALAR (1ULL << 17)
// string->integer(int/uint)时默认进行round(round to nearest),
// 如果设置该标记,则会进行trunc(round to zero)
// ceil(round to +inf)以及floor(round to -inf)暂时没有支持
Expand Down Expand Up @@ -117,6 +118,9 @@ typedef uint64_t ObCastMode;
#define CM_IS_STRICT_JSON(mode) ((CM_STRICT_JSON & (mode)) != 0)
#define CM_IS_JSON_VALUE(mode) CM_IS_ERROR_ON_SCALE_OVER(mode)
#define CM_IS_TO_COLUMN_CS_LEVEL(mode) ((CM_TO_COLUMN_CS_LEVEL & (mode)) != 0)
// for json type cast
#define CM_IS_SQL_AS_JSON_SCALAR(mode) ((CM_SQL_TO_JSON_SCALAR & (mode)) != 0)
#define CM_SET_SQL_AS_JSON_SCALAR(mode) (CM_SQL_TO_JSON_SCALAR | (mode))
// for geomerty type cast
#define CM_IS_GEOMETRY_GEOMETRY(mode) ((((mode) >> 12) & 0x1F) == 0)
#define CM_IS_GEOMETRY_POINT(mode) ((((mode) >> 12) & 0x1F) == 1)
Expand Down
1 change: 1 addition & 0 deletions src/sql/engine/expr/ob_datum_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,7 @@ static int common_string_json(const ObExpr &expr,
is_null_res = true;
} else if (!is_oracle
&& (is_enumset_to_str
|| (CM_IS_SQL_AS_JSON_SCALAR(expr.extra_) && ob_is_string_type(in_type))
|| (CM_IS_IMPLICIT_CAST(expr.extra_)
&& !CM_IS_COLUMN_CONVERT(expr.extra_)
&& !CM_IS_JSON_VALUE(expr.extra_)
Expand Down
3 changes: 1 addition & 2 deletions src/sql/engine/expr/ob_expr_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,7 @@ int ObExprOperator::aggregate_result_type_for_merge(
} else if (ob_is_extend(res_type)) {
OZ (aggregate_extend_accuracy_for_merge(type, types, param_num));
} else if (ob_is_json(res_type)) {
type.set_collation_type(CS_TYPE_UTF8MB4_BIN);
type.set_collation_level(CS_LEVEL_IMPLICIT);
type.set_json();
} else if (ob_is_geometry(res_type)) {
type.set_geometry();
type.set_length((ObAccuracy::DDL_DEFAULT_ACCURACY[ObGeometryType]).get_length());
Expand Down
2 changes: 1 addition & 1 deletion src/sql/engine/expr/ob_expr_relational_cmp_type.map
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ static ObObjType RELATIONAL_CMP_TYPE[ObMaxType][ObMaxType] =
ObNVarchar2Type, /* ObNCharType */
ObNullType, /* ObURowIDType */
ObNullType, /* ObLobType */
ObJsonType, /* ObJsonType */
ObVarcharType, /* ObJsonType */
ObHexStringType, /* ObGeometryType */
},

Expand Down
7 changes: 7 additions & 0 deletions src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,13 @@ int ObRawExprDeduceType::try_add_cast_expr(RawExprType &parent,
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_WARN("cast to lob type not allowed", K(ret));
}

// for consistent with mysql, if const cast as json, should regard as scalar, don't need parse
if (ObStringTC == ori_tc && ObJsonTC == expect_tc
&& IS_BASIC_CMP_OP(parent.get_expr_type())) {
uint64_t extra = new_expr->get_extra();
new_expr->set_extra(CM_SET_SQL_AS_JSON_SCALAR(extra));
}
OZ(parent.replace_param_expr(child_idx, new_expr));
if (OB_FAIL(ret) && my_session_->is_varparams_sql_prepare()) {
ret = OB_SUCCESS;
Expand Down

0 comments on commit e28f81d

Please sign in to comment.