diff --git a/src/sql/engine/expr/ob_expr_current_user.cpp b/src/sql/engine/expr/ob_expr_current_user.cpp index 99a349d2b5..8e016e4a9f 100644 --- a/src/sql/engine/expr/ob_expr_current_user.cpp +++ b/src/sql/engine/expr/ob_expr_current_user.cpp @@ -56,12 +56,49 @@ int ObExprCurrentUser::eval_current_user(const ObExpr &expr, ObEvalCtx &ctx, { int ret = OB_SUCCESS; UNUSED(expr); - const ObBasicSessionInfo *session_info = NULL; + const ObSQLSessionInfo *session_info = NULL; if (OB_ISNULL(session_info = ctx.exec_ctx_.get_my_session())) { ret = OB_ERR_UNEXPECTED; LOG_WARN("session info is null", K(ret)); } else { - expr_datum.set_string(session_info->get_user_at_host()); + ObSchemaGetterGuard schema_guard; + uint64_t tenant_id = session_info->get_effective_tenant_id(); + uint64_t priv_user_id = session_info->get_priv_user_id(); + const ObUserInfo *user_info = nullptr; + if (OB_ISNULL(GCTX.schema_service_)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("unexpected NULL GCTX.schema_service_", K(ret)); + } else if (OB_FAIL(GCTX.schema_service_->get_tenant_schema_guard( + tenant_id, schema_guard))) { + LOG_WARN("failed to get tenant schema guard", K(ret)); + } else if (OB_FAIL(schema_guard.get_user_info(tenant_id, + priv_user_id, + user_info))) { + LOG_WARN("failed to get user info", K(ret), K(tenant_id), K(priv_user_id)); + } else if (OB_ISNULL(user_info)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("unexpected NULL user_info", K(ret), K(tenant_id), K(priv_user_id)); + } else { + const ObString &user_name = user_info->get_user_name_str(); + const ObString &hostname = user_info->get_host_name_str(); + size_t buf_len = user_name.length() + hostname.length() + 2; + size_t pos=0; + + char *buf = static_cast( + ctx.get_expr_res_alloc().alloc(buf_len)); + if (OB_ISNULL(buf)) { + ret = OB_ALLOCATE_MEMORY_FAILED; + LOG_WARN("failed to alloc memory for result buf", K(ret), K(user_info)); + } else if (OB_UNLIKELY( + (pos = snprintf(buf, buf_len, "%.*s@%.*s", + user_name.length(), user_name.ptr(), + hostname.length(), hostname.ptr())) < 0)) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("failed to snprintf", K(ret), K(user_info), K(buf_len)); + } else { + expr_datum.set_string(buf, pos); + } + } } return ret; } diff --git a/src/sql/session/ob_sql_session_info.h b/src/sql/session/ob_sql_session_info.h index 8ccce3809c..179aba4b21 100644 --- a/src/sql/session/ob_sql_session_info.h +++ b/src/sql/session/ob_sql_session_info.h @@ -1185,7 +1185,7 @@ public: void set_prelock(bool prelock) { prelock_ = prelock; } void set_priv_user_id(uint64_t priv_user_id) { priv_user_id_ = priv_user_id; } - uint64_t get_priv_user_id() { + uint64_t get_priv_user_id() const { return (priv_user_id_ == OB_INVALID_ID) ? get_user_id() : priv_user_id_; } int64_t get_xa_end_timeout_seconds() const; int set_xa_end_timeout_seconds(int64_t seconds);