From 948ac8764c01b4369f1c8026586c7017dc25f98d Mon Sep 17 00:00:00 2001 From: qingzhu521 Date: Fri, 19 Apr 2024 04:55:41 +0000 Subject: [PATCH] [CP] User can not modify the hidden pk. the key is controlled by db --- src/sql/resolver/dml/ob_del_upd_resolver.cpp | 116 +++++++++++++------ src/sql/resolver/dml/ob_del_upd_resolver.h | 3 + src/sql/resolver/dml/ob_dml_resolver.cpp | 11 +- 3 files changed, 91 insertions(+), 39 deletions(-) diff --git a/src/sql/resolver/dml/ob_del_upd_resolver.cpp b/src/sql/resolver/dml/ob_del_upd_resolver.cpp index 9cb3d759c..cfb17c9b9 100644 --- a/src/sql/resolver/dml/ob_del_upd_resolver.cpp +++ b/src/sql/resolver/dml/ob_del_upd_resolver.cpp @@ -1103,6 +1103,70 @@ int ObDelUpdResolver::check_same_base_table(const TableItem &table_item, return ret; } + +int ObDelUpdResolver::add_select_item_func(ObSelectStmt &select_stmt, ColumnItem &col) { + int ret = OB_SUCCESS; + bool found_in_select = false; + FOREACH_CNT_X(si, select_stmt.get_select_items(), !found_in_select) { + if (si->expr_ == col.expr_) { + found_in_select = true; + } + } + if (!found_in_select) { + SelectItem select_item; + select_item.implicit_filled_ = true; + select_item.expr_ = col.expr_; + // concat column's table name and column name as select item's alias name + const int32_t size = col.expr_->get_table_name().length() + + 1 // "." + + col.expr_->get_column_name().length(); + char *buf = static_cast(allocator_->alloc(size)); + if (OB_ISNULL(buf)) { + ret = OB_ALLOCATE_MEMORY_FAILED; + LOG_WARN("allocate memory failed", K(ret), K(size)); + } else { + char *p = buf; + MEMCPY(p, col.expr_->get_table_name().ptr(), col.expr_->get_table_name().length()); + p += col.expr_->get_table_name().length(); + *p = '.'; + p++; + MEMCPY(p, col.expr_->get_column_name().ptr(), col.expr_->get_column_name().length()); + select_item.alias_name_.assign_ptr(buf, size); + } + if (OB_SUCC(ret)) { + if (OB_FAIL(select_stmt.add_select_item(select_item))) { + LOG_WARN("add select item failed", K(ret)); + } + } + } + return ret; +} + +int ObDelUpdResolver::select_items_has_pk(const ObSelectStmt& select_stmt, bool &has_pk) { + int ret = OB_SUCCESS; + has_pk = false; + + for (int64_t i = 0; i < select_stmt.get_select_items().count() && !has_pk && OB_SUCC(ret); ++i) { + const SelectItem &si = select_stmt.get_select_items().at(i); + ObSEArray used_column_ids; + if (OB_ISNULL(si.expr_)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("select item expr is null", K(ret)); + } else if (OB_FAIL(ObRawExprUtils::extract_column_ids(si.expr_, used_column_ids))) { + LOG_WARN("failed to extract column ids", K(ret)); + } else { + for (int64_t idx = 0; idx < used_column_ids.count() && !has_pk; ++idx) { + if (used_column_ids.at(idx) == OB_HIDDEN_PK_INCREMENT_COLUMN_ID) { + has_pk = true; + } + } + } + } + + return ret; +} + + int ObDelUpdResolver::add_all_column_to_updatable_view(ObDMLStmt &stmt, const TableItem &table_item, const bool &has_need_fired_tg_on_view) @@ -1130,47 +1194,23 @@ int ObDelUpdResolver::add_all_column_to_updatable_view(ObDMLStmt &stmt, } } if (OB_SUCC(ret)) { - auto add_select_item_func = [&](ObSelectStmt &select_stmt, ColumnItem &col) { - int ret = OB_SUCCESS; - bool found_in_select = false; - FOREACH_CNT_X(si, select_stmt.get_select_items(), !found_in_select) { - if (si->expr_ == col.expr_) { - found_in_select = true; - } - } - if (!found_in_select) { - SelectItem select_item; - select_item.implicit_filled_ = true; - select_item.expr_ = col.expr_; - // concat column's table name and column name as select item's alias name - const int32_t size = col.expr_->get_table_name().length() - + 1 // "." - + col.expr_->get_column_name().length(); - char *buf = static_cast(allocator_->alloc(size)); - if (OB_ISNULL(buf)) { - ret = OB_ALLOCATE_MEMORY_FAILED; - LOG_WARN("allocate memory failed", K(ret), K(size)); - } else { - char *p = buf; - MEMCPY(p, col.expr_->get_table_name().ptr(), col.expr_->get_table_name().length()); - p += col.expr_->get_table_name().length(); - *p = '.'; - p++; - MEMCPY(p, col.expr_->get_column_name().ptr(), col.expr_->get_column_name().length()); - select_item.alias_name_.assign_ptr(buf, size); - } - if (OB_SUCC(ret)) { - if (OB_FAIL(select_stmt.add_select_item(select_item))) { - LOG_WARN("add select item failed", K(ret)); - } - } - } - return ret; - }; ColumnItem *col_item = NULL; if (table_item.is_basic_table() || table_item.is_link_table()) { + bool has_pk = false; + if (stmt::T_SELECT == stmt.get_stmt_type()) { + // select_items_has_pk must happend before add_select_item_func + if (OB_FAIL(select_items_has_pk(static_cast(stmt), has_pk))) { + LOG_WARN("failed to extract pk", K(ret)); + } else if (has_pk) { + ret = OB_ERR_BAD_FIELD_ERROR; + LOG_WARN("view has hidden pk in modify stmt", K(ret)); + } + } + const ObTableSchema *table_schema = NULL; - if (OB_FAIL(schema_checker_->get_table_schema(params_.session_info_->get_effective_tenant_id(), table_item.ref_id_, table_schema, table_item.is_link_table()))) { + if (OB_FAIL(ret)) { + // do nothing + } else if (OB_FAIL(schema_checker_->get_table_schema(params_.session_info_->get_effective_tenant_id(), table_item.ref_id_, table_schema, table_item.is_link_table()))) { LOG_WARN("get table schema failed", K(ret)); } else if (OB_ISNULL(table_schema)) { ret = OB_ERR_UNEXPECTED; diff --git a/src/sql/resolver/dml/ob_del_upd_resolver.h b/src/sql/resolver/dml/ob_del_upd_resolver.h index 141f38ee4..90f4a3703 100644 --- a/src/sql/resolver/dml/ob_del_upd_resolver.h +++ b/src/sql/resolver/dml/ob_del_upd_resolver.h @@ -273,6 +273,9 @@ protected: int check_need_match_all_params(const common::ObIArray &value_desc, bool &need_match); int resolve_json_partial_update_flag(ObIArray &table_assigns, ObStmtScope scope); int mark_json_partial_update_flag(const ObColumnRefRawExpr *ref_expr, ObRawExpr *expr, int depth, bool &allow_json_partial_update); + int add_select_item_func(ObSelectStmt &select_stmt, ColumnItem &col); + int select_items_has_pk(const ObSelectStmt& select_stmt, bool &has_pk); + private: common::hash::ObPlacementHashSet insert_column_ids_; bool is_column_specify_; diff --git a/src/sql/resolver/dml/ob_dml_resolver.cpp b/src/sql/resolver/dml/ob_dml_resolver.cpp index 98455ec4c..e6c4b07ff 100755 --- a/src/sql/resolver/dml/ob_dml_resolver.cpp +++ b/src/sql/resolver/dml/ob_dml_resolver.cpp @@ -2695,7 +2695,14 @@ int ObDMLResolver::resolve_basic_column_item(const TableItem &table_item, ret = OB_ERR_UNEXPECTED; LOG_WARN("not base table or alias from base table", K_(table_item.type), K(ret)); } else if (NULL != (col_item = stmt->get_column_item(table_item.table_id_, column_name))) { - //exist, ignore resolve... + if (!include_hidden) { + if (!ObCharset::case_insensitive_equal(column_name, OB_HIDDEN_PK_INCREMENT_COLUMN_NAME)) { + //do nothing + } else if (current_scope_ == T_UPDATE_SCOPE || current_scope_ == T_INSERT_SCOPE) { + ret = OB_ERR_BAD_FIELD_ERROR; + LOG_WARN("not allowed update insert hidden pk increment column", K(ret)); + } + } } else { bool is_uni = false; bool is_mul = false; @@ -2708,6 +2715,8 @@ int ObDMLResolver::resolve_basic_column_item(const TableItem &table_item, if (!include_hidden) { if (!ObCharset::case_insensitive_equal(column_name, OB_HIDDEN_PK_INCREMENT_COLUMN_NAME)) { //do nothing + } else if (current_scope_ == T_UPDATE_SCOPE || current_scope_ == T_INSERT_SCOPE) { + //do nothing include is false } else if (ObResolverUtils::is_restore_user(*session_info_) || ObResolverUtils::is_drc_user(*session_info_) || session_info_->is_inner()) {