Pull Request has been successfully merged by opengauss_bot.
(Thank you for DarkAthena's contribution.)Welcome To openGauss Community
Hey @darkathena , thanks for your contribution to the community.
Bot Usage Manual
I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.
Contact Guide
If you have any questions, please contact the SIG: AI, CM, CloudNative, SQLEngine, SecurityTechnology, StorageEngine ,
and any of the maintainers: @gaussyuexz, @hwworkholic, @lishifu_hw_db, @shaga, @totaj, @wangwei5, @yanghaos, @zhu_jinwei, @ziki77, @zjxqxf ,
and any of the committers: @CarrotGo, @bihua111, @cailei19, @chendong76, @chenxiaobin19, @congzhou2603, @dodders, @h_ray, @jemappellehc, @levy53071, @libiao2024, @lihaixiao, @mrzack, @quemingjian, @superlchf, @wangfeihuo, @wuyuechuan, @xiong_xjun, @zhangfengzhi123, @zjh_hw .


【标题】
【实现内容】:
修复connect by子句中使用视图会报错找不到文件的问题
SQL 场景
CREATE VIEW v1 AS SELECT 1 a FROM sys_dummy; SELECT 1 FROM sys_dummy CONNECT BY LEVEL<=(SELECT max(a) FROM v1);报错信息
【根因分析】:
视图本质:视图(VIEW)在
pg_class中没有对应的物理文件,其relfilenode是无效值正常流程: 视图应该在查询重写阶段通过
fireRIRrules()函数展开为其底层的 SQL 查询问题流程:
CONNECT BY LEVEL<=后的子查询(SELECT max(a) FROM v1)在 parse 阶段被提取到connect_by_level_qualstransformWhereClause()转换时,没有经过视图展开就进入了执行阶段v1当作普通表处理RelationGetNumberOfBlocks()尝试打开视图的物理文件核心问题: START WITH...CONNECT BY 重写机制与视图重写机制的交互存在缺陷
swoptions->connect_by_level_quals中的子查询未经过视图展开处理【实现方案】:
修改文件:
src/gausskernel/optimizer/rewrite/rewriteHandler.cpp修改位置:
fireRIRrules()函数,处理 CTE 列表的部分修改内容:
/* Recurse into subqueries in WITH */ foreach (lc, parsetree->cteList) { CommonTableExpr* cte = (CommonTableExpr*)lfirst(lc); /* * BUGFIX: For START WITH...CONNECT BY converted CTEs, also expand * views in connect_by_level_quals and start_with_quals which may * contain subqueries referencing views. */ if (cte->swoptions != NULL) { if (cte->swoptions->connect_by_level_quals != NULL) { (void)expression_tree_walker(cte->swoptions->connect_by_level_quals, (bool (*)())fireRIRonSubLink, (void*)activeRIRs); } if (cte->swoptions->start_with_quals != NULL) { (void)expression_tree_walker(cte->swoptions->start_with_quals, (bool (*)())fireRIRonSubLink, (void*)activeRIRs); } } cte->ctequery = (Node*)fireRIRrules((Query*)cte->ctequery, activeRIRs, false); }原理说明:
fireRIRonSubLink()会递归处理 SubLink(子查询)中的视图展开swoptions结构中存储的 quals 也应用视图展开规则expression_tree_walker()而不是query_tree_walker(),因为这些 quals 是 Node* 类型的表达式树CONNECT BY LEVEL<=中的子查询里的视图被正确展开【关联需求或issue】:
https://gitcode.com/opengauss/openGauss-server/issues/7783
【开发自验报告】:
否
否
否
否
否
是
【其他说明】: