!1985 修复SQL语句中同时出现窗口函数和子查询时因子查询被提升导致执行失败的问题

Merge pull request !1985 from april01xxx/first_value
This commit is contained in:
opengauss-bot
2022-08-22 12:07:58 +00:00
committed by Gitee
5 changed files with 51 additions and 1 deletions

View File

@ -7144,7 +7144,8 @@ static List* make_windowInputTargetList(PlannerInfo* root, List* tlist, List* ac
if (IsA(node, Var)) {
if (!tlist_member(node, new_tlist)) {
if (var_from_dependency_rel(parse, (Var *)node, dep_oids) ||
var_from_sublink_pulluped(parse, (Var *) node)) {
var_from_sublink_pulluped(parse, (Var *) node) ||
var_from_subquery_pulluped(parse, (Var *) node)) {
flattenable_vars_final = lappend(flattenable_vars_final, node);
}
}

View File

@ -710,3 +710,16 @@ bool var_from_sublink_pulluped(Query *parse, Var *var)
return tbl->sublink_pull_up;
}
/*
* Check if var comes from subquery pulled up
* @in parse: the parse tree contained the vars.
* @in var: the vars need be checked.
*/
bool var_from_subquery_pulluped(Query *parse, Var *var)
{
Index varno = var->varno;
RangeTblEntry *tbl = rt_fetch((int)varno, parse->rtable);
return tbl->pulled_from_subquery;
}

View File

@ -47,4 +47,5 @@ extern void get_tlist_group_vars_split(Query* parse, List* tlist, List** group_c
extern List* get_dependency_var_list(Query* parse, List* group_cols, List* non_group_vars);
extern bool var_from_dependency_rel(Query* parse, Var* var, List* dep_oids);
extern bool var_from_sublink_pulluped(Query *parse, Var *var);
extern bool var_from_subquery_pulluped(Query *parse, Var *var);
#endif /* TLIST_H */

View File

@ -1403,3 +1403,23 @@ SELECT nth_value_def(ten) OVER (PARTITION BY four order by unique1) as No1, *
1 | 6701 | 7 | 1 | 1 | 1 | 1 | 1 | 701 | 701 | 1701 | 6701 | 2 | 3 | TXAAAA | HAAAAA | VVVVxx
(10 rows)
drop table bmsql_item;
create table bmsql_item ( i_price numeric(5,2),i_data varchar(50));
insert into bmsql_item values('1.0',1);
select
first_value(avg(alias7.alias4)) over (order by 1) alias12,
length(alias7.alias5) alias14
from (select
bmsql_item.i_price alias4,
cast(bmsql_item.i_data as varchar(100))as alias5
from
bmsql_item
) alias7
group by
alias7.alias5;
alias12 | alias14
------------------------+---------
1.00000000000000000000 | 1
(1 row)
drop table bmsql_item;

View File

@ -346,3 +346,18 @@ CREATE FUNCTION nth_value_def(val anyelement, n integer = 1) RETURNS anyelement
SELECT nth_value_def(ten) OVER (PARTITION BY four order by unique1) as No1, *
FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten) s order by No1;
drop table bmsql_item;
create table bmsql_item ( i_price numeric(5,2),i_data varchar(50));
insert into bmsql_item values('1.0',1);
select
first_value(avg(alias7.alias4)) over (order by 1) alias12,
length(alias7.alias5) alias14
from (select
bmsql_item.i_price alias4,
cast(bmsql_item.i_data as varchar(100))as alias5
from
bmsql_item
) alias7
group by
alias7.alias5;
drop table bmsql_item;