Skip to content

Commit

Permalink
Disable query has SRF to AQUMV.
Browse files Browse the repository at this point in the history
SRF breaks the assumption: Query has aggregation without GROUP BY
clause has one row returned at most.
It makes complicated to consider LIMIT and ORDER BY clause during
answer query.

Authored-by: Zhang Mingli [email protected]
  • Loading branch information
avamingli committed Dec 10, 2024
1 parent 9e086a3 commit 726be06
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/backend/catalog/gp_matview_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "utils/lsyscache.h"
#include "storage/lockdefs.h"
#include "optimizer/optimizer.h"
#include "optimizer/transform.h"
#include "parser/parsetree.h"

static void InsertMatviewTablesEntries(Oid mvoid, List *relids);
Expand Down Expand Up @@ -73,6 +74,11 @@ GetViewBaseRelids(const Query *viewQuery, bool *has_foreign)
return NIL;
}

if (tlist_has_srf(viewQuery))
{
return NIL;
}

/* As we will use views, make it strict to unmutable. */
if (contain_mutable_functions((Node*)viewQuery))
return NIL;
Expand Down
13 changes: 11 additions & 2 deletions src/backend/optimizer/plan/aqumv.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "optimizer/planner.h"
#include "optimizer/prep.h"
#include "optimizer/tlist.h"
#include "optimizer/transform.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "parser/parse_node.h"
Expand Down Expand Up @@ -350,12 +351,16 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
limit_needed(viewQuery)) /* LIMIT, OFFSET is not supported on IMMV yet. */
continue;

if (tlist_has_srf(parse))
continue;

/*
* There is a trick for ORDER BY for both origin query and view query.
* As we has no Groupy By here, the aggregation results would be either one or
* zero rows that make the Order By clause pointless.
* zero rows that make the Order By clause pointless, except that there were
* SRF.
* We could avoid considering the sort columns if it's a junk for view matching.
* This in-place update raw_processed_tlist.
* This in-place updates raw_processed_tlist.
*/
if (parse->sortClause != NIL || viewQuery->sortClause != NIL)
{
Expand Down Expand Up @@ -431,6 +436,8 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
continue;

viewQuery->targetList = mv_final_tlist;
/* SRF is not supported now, but correct the field. */
viewQuery->hasTargetSRFs = parse->hasTargetSRFs;
viewQuery->hasAggs = false;
subroot->agginfos = NIL;
subroot->aggtransinfos = NIL;
Expand Down Expand Up @@ -483,6 +490,8 @@ answer_query_using_materialized_views(PlannerInfo *root, AqumvContext aqumv_cont
if (context->has_unmatched)
continue;

/* SRF is not supported now, but correct the field. */
viewQuery->hasTargetSRFs = parse->hasTargetSRFs;
viewQuery->groupClause = parse->groupClause;
viewQuery->groupingSets = parse->groupingSets;
viewQuery->sortClause = parse->sortClause;
Expand Down
7 changes: 3 additions & 4 deletions src/backend/optimizer/plan/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ static SubLink *make_sirvf_subselect(FuncExpr *fe);
static Query *make_sirvf_subquery(FuncExpr *fe);
static bool safe_to_replace_sirvf_tle(Query *query);
static bool safe_to_replace_sirvf_rte(Query *query);
static bool tlist_has_srf(Query *query);

/**
* Normalize query before planning.
Expand Down Expand Up @@ -526,15 +525,15 @@ replace_sirvf_rte(Query *query, RangeTblEntry *rte)
/*
* Does target list have SRFs?
*/
static
bool tlist_has_srf(Query *query)
bool
tlist_has_srf(const Query *query)
{
if (query->hasTargetSRFs)
{
return true;
}

if (expression_returns_set( (Node *) query->targetList))
if (expression_returns_set((Node *) query->targetList))
{
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/include/optimizer/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ extern Query *remove_distinct_sort_clause(Query *query);

extern bool query_has_srf(Query *query);

extern bool tlist_has_srf(const Query *query);

#endif /* TRANSFORM_H */

0 comments on commit 726be06

Please sign in to comment.