Skip to content

Fix:修复达梦物化视图分页元数据分类#3430

Merged
t8y2 merged 4 commits into
t8y2:mainfrom
zipg:codex/issue3418-dameng-materialized-view
Jul 16, 2026
Merged

Fix:修复达梦物化视图分页元数据分类#3430
t8y2 merged 4 commits into
t8y2:mainfrom
zipg:codex/issue3418-dameng-materialized-view

Conversation

@zipg

@zipg zipg commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

问题

达梦物化视图会记录在 USER_MVIEWS 中,但当前测试版本的 ALL_OBJECTS 和 ALL_TAB_COMMENTS 仍将其标记为 VIEW。

原有非分页元数据路径会通过 USER_MVIEWS 将物化视图与普通视图区分开;v0.5.56 包含的分页元数据优化改为直接按 ALL_OBJECTS.OBJECT_TYPE 过滤,绕过了达梦专用分类逻辑,导致物化视图进入普通视图分组。后续查看源码时会按 VIEW 调用 DBMS_METADATA.GET_DDL,从而报对象不存在。

修复

  • 通过 USER_MVIEWS.SCHID 和 schema 对象识别物化视图
  • listTables 和 listObjects 均先重分类,再进行类型过滤、排序和分页
  • 兼容目录直接返回 MATERIALIZED VIEW 的达梦版本
  • 不修改 Agent 版本号

验证

  • 使用真实 DM8 创建 DBX_TEST.ISSUE_3418_MV 复现
  • 普通视图列表仅返回 V_CITY_SALES
  • 物化视图列表仅返回 ISSUE_3418_MV,类型为 MATERIALIZED_VIEW
  • 对象列表能同时返回正确分类的 VIEW 和 MATERIALIZED_VIEW
  • get_object_source 能成功返回完整 CREATE MATERIALIZED VIEW DDL
  • ./gradlew :dameng:check:33 项测试通过

Fixes #3418

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two metadata correctness and regression issues to address.

  1. USER_MVIEWS only covers materialized views owned by the connected user. When a privileged account browses another owner, a materialized view reported as VIEW by ALL_OBJECTS still misses the join and is later passed to DBMS_METADATA.GET_DDL as a normal view. Please support the connected-user-differs-from-requested-schema case with an appropriate catalog/fallback and regression coverage.
  2. The materialized-view join is currently added to every constrained metadata query, including table-only and procedure/function-only searches. This adds unnecessary catalog work and makes unrelated queries depend on USER_MVIEWS. Please add the classification join only when VIEW/MATERIALIZED_VIEW classification is relevant.

@zipg

zipg commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

已按评审意见修复并推送 8fdab6e

  1. 跨 schema 分类:实测当前 DM8 不提供 ALL_MVIEWS;USER_MVIEWS 的定义直接来自 SYS.SYSOBJECTS,目录名不能按 Oracle 语义处理。主查询现改为使用 SYS.SYSOBJECTS 的物化视图标志,并通过 SCHID 映射所属 schema,能够支持连接用户与目标 owner 不同的场景;USER_MVIEWS 保留为系统目录受限或版本差异时的 fallback。
  2. 无关查询开销:仅在请求类型包含 VIEW 或 MATERIALIZED_VIEW 时加入分类 JOIN。TABLE-only、PROCEDURE/FUNCTION-only 查询继续直接按 ALL_OBJECTS.OBJECT_TYPE 过滤,不依赖任何物化视图目录。
  3. 回归覆盖:新增跨 owner 查询结构、TABLE-only 无 JOIN、routine-only 无 JOIN 的测试。

验证结果:

  • ./gradlew :dameng:check 通过;
  • 使用真实 DM8,以 SYSDBA 连接并浏览 DBX_TEST:普通视图仅返回 V_CITY_SALES,物化视图仅返回 ISSUE_3418_MV;list_objects 分类正确;get_object_source 成功返回完整 CREATE MATERIALIZED VIEW DDL。

@zipg
zipg requested a review from t8y2 July 14, 2026 15:47

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request changes: 跨 schema 的物化视图分类还有一个权限场景没有覆盖。

新的 SYS.SYSOBJECTS 查询在具备系统目录权限时可以正确分类,但普通用户可能只能通过对象级授权访问另一个 schema。实测这种用户可以在 ALL_OBJECTS 中看到目标普通视图和物化视图,但两者都显示为 VIEW;同时该用户无权查询 SYS.SYSOBJECTS,而 USER_MVIEWS 只包含当前用户自己的物化视图。

因此主查询失败后,USER_MVIEWS fallback 仍无法识别跨 owner 的物化视图,最后会把它作为普通 VIEW 返回,并在加载源码时按 VIEW 调用 DBMS_METADATA.GET_DDL

请补充一个不依赖 SYS.SYSOBJECTS 权限的跨 owner fallback,并增加回归测试:连接用户仅被显式授予另一个 schema 下普通视图和物化视图的访问权限,确认两者仍能正确分类。

@zipg
zipg force-pushed the codex/issue3418-dameng-materialized-view branch from 8fdab6e to ad51e54 Compare July 15, 2026 01:05
@zipg

zipg commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

已按最新审核意见修复,提交:ad51e542。

  • 新增不依赖 SYS.SYSOBJECTS / USER_MVIEWS 的跨 owner fallback。
  • 系统目录查询失败时,从 ALL_OBJECTS 获取 VIEW 候选,通过 DBMS_METADATA.GET_DDL 按对象级权限逐项识别物化视图,再进行类型过滤、排序和分页。
  • fallback 只在 VIEW / MATERIALIZED_VIEW 分类相关请求且系统目录不可用时触发,TABLE-only 和 routine-only 查询不受影响。
  • 已补充受限用户回归测试,覆盖 list_tables 和 list_objects。

真实 DM8 验证:创建仅拥有 DBX_TEST.V_CITY_SALES 和 DBX_TEST.ISSUE_3418_MV 对象级 SELECT 权限的临时用户。该用户无 SYS.SYSOBJECTS 权限,且 ALL_OBJECTS / JDBC 均将两者报告为 VIEW;修改后普通视图和物化视图均能正确分类,物化视图源码也可正常读取。临时用户已清理。

本地验证::dameng:check --rerun-tasks 通过。

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. The constrained-query permission case is improved, but two blocking issues remain on the current head.

  1. The normal unconstrained listTables(schema) and listObjects(schema) paths still classify cross-owner materialized views through USER_MVIEWS. A restricted user browsing another owner can therefore still receive a materialized view as a regular VIEW, or have it omitted. The new privilege-safe fallback only runs for constrained requests. Please cover the normal overloads with the same restricted cross-owner scenario.

  2. The restricted-user fallback removes pagination and probes every candidate view using a separate DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW', ...) call. A small page request can therefore perform an unbounded scan plus N+1 catalog queries across the entire schema, and listTables and listObjects may repeat the work. Please replace this with a bulk or bounded classification strategy and add regression/performance coverage.

@zipg
zipg force-pushed the codex/issue3418-dameng-materialized-view branch from ad51e54 to 0cc861d Compare July 15, 2026 09:33
@zipg

zipg commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

已按本轮审核意见修复并推送 0cc861d9

  1. 普通入口已覆盖:listTables(schema)listTables(schema, objectTypes)listObjects(schema) 与约束/分页入口现在统一走同一套分类查询,不再回到仅依赖 USER_MVIEWS 的旧路径。
  2. 移除 N+1:受限用户无法访问 SYS.SYSOBJECTS 时,改用一次 ALL_DEPENDENCIES JOIN 批量识别跨 owner 物化视图,已删除逐对象调用 DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW', ...) 的探测逻辑。
  3. 保留数据库侧处理:类型过滤、名称过滤、排序和 LIMIT/OFFSET 均保留在批量 fallback SQL 中,小页请求不会先全量扫描候选再逐项分类。
  4. 回归测试覆盖普通 listTables/listObjects 与分页入口,并断言 fallback 使用 ALL_DEPENDENCIES、保留分页且不出现逐对象 GET_DDL

验证:

  • 已 rebase 最新 main
  • ./gradlew :dameng:check --rerun-tasks 通过;
  • 使用重新构建的 Agent 和真实 DM8 受限用户验证:该用户仅被授予另一个 schema 下一个普通视图和一个物化视图的 SELECT 权限,且查询 SYS.SYSOBJECTS 明确无权限;普通列表、对象列表、VIEW/MATERIALIZED_VIEW 类型过滤以及两页分页均分类正确。临时测试用户已清理。

@zipg
zipg requested a review from t8y2 July 15, 2026 09:52
@t8y2
t8y2 merged commit b4bf5e0 into t8y2:main Jul 16, 2026
7 checks passed
@t8y2

t8y2 commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution! Merged in b4bf5e0, will be released in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 达梦数据库中的 物化视图 错误的分在了视图中

2 participants