聚集Limit下推优化

This commit is contained in:
‘ljy’
2023-05-29 14:43:02 +08:00
parent 5d6882b3d8
commit 658aea19b8
40 changed files with 2588 additions and 63 deletions

View File

@ -410,6 +410,7 @@ typedef struct AggStatePerPhaseData {
FmgrInfo* eqfunctions; /* per-grouping-field equality fns */
Agg* aggnode; /* Agg node for phase data */
Sort* sortnode; /* Sort node for input ordering for phase */
SortGroup* sortGroupNode; /* SortGroup node for input ordering for phase */
AggStrategy aggstrategy; /* strategy mode */
ExprState *evaltrans; /* evaluation of transition functions */
} AggStatePerPhaseData;

View File

@ -0,0 +1,26 @@
/* -------------------------------------------------------------------------
*
* nodeSortGroup.h
* support for the openGauss executor module
*
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
* Portions Copyright (c) 2022, openGauss Contributors
*
* src/include/executor/nodeSortGroup.h
*
* -------------------------------------------------------------------------
*/
#ifndef NODESORTGROUP_H
#define NODESORTGROUP_H
#include "nodes/execnodes.h"
extern SortGroupState *ExecInitSortGroup(SortGroup *node, EState *estate, int eflags);
extern void ExecEndSortGroup(SortGroupState *node);
extern void ExecReScanSortGroup(SortGroupState *node);
#endif /*NODESORTGROUP_H*/

View File

@ -67,6 +67,7 @@ typedef struct knl_session_attr_sql {
bool enable_sort;
bool enable_compress_spill;
bool enable_hashagg;
bool enable_sortgroup_agg;
bool enable_material;
bool enable_nestloop;
bool enable_mergejoin;

View File

@ -2412,6 +2412,21 @@ typedef struct SortState {
int64* space_size; /* spill size for temp table */
} SortState;
struct SortGroupStatePriv;
/* ----------------
* SortGroupState information
* ----------------
*/
typedef struct SortGroupState {
ScanState ss; /* its first field is NodeTag */
int64 bound; /* if bounded, how many group are needed */
struct SortGroupStatePriv *state; /* private state of nodeSortGroup.c */
bool sort_Done; /* sort completed yet? */
bool *new_group_trigger; /* indicates new groups where returning tuples */
const char *spaceType; /* type of space spaceUsed represents */
int64 spaceUsed; /* space used for explain */
} SortGroupState;
/* ---------------------
* GroupState information
* -------------------------
@ -2455,6 +2470,7 @@ typedef struct AggState {
AggStatePerAgg curperagg; /* identifies currently active aggregate */
bool input_done; /* indicates end of input */
bool agg_done; /* indicates completion of Agg scan */
bool new_group_trigger; /* indicates new groups where returning tuples*/
int projected_set; /* The last projected grouping set */
int current_set; /* The current grouping set being evaluated */
Bitmapset* grouped_cols; /* grouped cols in current projection */

View File

@ -75,6 +75,7 @@ typedef enum NodeTag {
T_HashJoin,
T_Material,
T_Sort,
T_SortGroup,
T_Group,
T_Agg,
T_WindowAgg,
@ -176,6 +177,7 @@ typedef enum NodeTag {
T_HashJoinState,
T_MaterialState,
T_SortState,
T_SortGroupState,
T_GroupState,
T_AggState,
T_WindowAggState,

View File

@ -1160,6 +1160,19 @@ typedef struct Sort {
OpMemInfo mem_info; /* Memory info for sort */
} Sort;
/* ----------------
* SortGroup node
* ----------------
*/
typedef struct SortGroup {
Plan plan;
int numCols; /* number of sort-key columns */
AttrNumber *sortColIdx; /* their indexes in the target list */
Oid *sortOperators; /* OIDs of operators to sort them by */
Oid *collations; /* OIDs of collations */
bool *nullsFirst; /* NULLS FIRST/LAST directions */
} SortGroup;
typedef struct VecSort : public Sort {
} VecSort;
@ -1196,7 +1209,8 @@ typedef struct VecGroup : public Group {
typedef enum AggStrategy {
AGG_PLAIN, /* simple agg across all input rows */
AGG_SORTED, /* grouped agg, input must be sorted */
AGG_HASHED /* grouped agg, use internal hashtable */
AGG_HASHED, /* grouped agg, use internal hashtable */
AGG_SORT_GROUP /* grouped agg, use sort group */
} AggStrategy;
#ifdef STREAMPLAN

View File

@ -385,6 +385,7 @@ typedef struct PlannerInfo {
bool hasPseudoConstantQuals; /* true if any RestrictInfo has
* pseudoconstant = true */
bool hasRecursion; /* true if planning a recursive WITH item */
bool consider_sortgroup_agg; /*ture if consider to use SORT GROUP agg */
/* Note: qualSecurityLevel is zero if there are no securityQuals */
Index qualSecurityLevel; /* minimum security_level for quals */

View File

@ -108,6 +108,8 @@ extern void cost_recursive_union(Plan* runion, Plan* nrterm, Plan* rterm);
extern void cost_sort(Path* path, List* pathkeys, Cost input_cost, double tuples, int width, Cost comparison_cost,
int sort_mem, double limit_tuples, bool col_store, int dop = 1, OpMemInfo* mem_info = NULL,
bool index_sort = false);
extern void cost_sort_group(Path *path, PlannerInfo *root, Cost input_cost, double tuples, int width,
Cost comparison_cost, int sort_mem, double dNumGroups);
extern void cost_merge_append(Path* path, PlannerInfo* root, List* pathkeys, int n_streams, Cost input_startup_cost,
Cost input_total_cost, double tuples);
extern void cost_material(Path* path, Cost input_startup_cost, Cost input_total_cost, double tuples, int width);

View File

@ -79,9 +79,12 @@ extern Sort* make_sort_from_pathkeys(
PlannerInfo* root, Plan* lefttree, List* pathkeys, double limit_tuples, bool can_parallel = false);
extern Sort* make_sort_from_sortclauses(PlannerInfo* root, List* sortcls, Plan* lefttree);
extern Sort* make_sort_from_groupcols(PlannerInfo* root, List* groupcls, AttrNumber* grpColIdx, Plan* lefttree);
extern SortGroup* make_sort_group_from_groupcols(PlannerInfo* root, List* groupcls, AttrNumber* grpColIdx, Plan* lefttree, double dNumGroup);
extern Sort* make_sort_from_targetlist(PlannerInfo* root, Plan* lefttree, double limit_tuples);
extern Sort* make_sort(PlannerInfo* root, Plan* lefttree, int numCols, AttrNumber* sortColIdx, Oid* sortOperators,
Oid* collations, bool* nullsFirst, double limit_tuples);
extern SortGroup* make_sortgroup(PlannerInfo* root, Plan* lefttree, int numCols, AttrNumber* sortColIdx, Oid* sortOperators,
Oid* collations, bool* nullsFirst, double dNumGroup);
extern Agg* make_agg(PlannerInfo* root, List* tlist, List* qual, AggStrategy aggstrategy,
const AggClauseCosts* aggcosts, int numGroupCols, AttrNumber* grpColIdx, Oid* grpOperators, Oid* grp_collations,
long numGroups, Plan* lefttree, WindowLists* wflists, bool need_stream, bool trans_agg, List* groupingSets = NIL,

View File

@ -64,6 +64,7 @@ extern void LogicalTapeWrite(LogicalTapeSet *lts, int tapenum, void *ptr, size_t
extern void LogicalTapeRewindForRead(LogicalTapeSet *lts, int tapenum, size_t buffer_size);
extern void LogicalTapeRewindForWrite(LogicalTapeSet *lts, int tapenum);
extern void LogicalTapeFreeze(LogicalTapeSet *lts, int tapenum, TapeShare *share = NULL);
extern void LogicalTapeSetExtend(LogicalTapeSet *lts, int nAdditional);
extern size_t LogicalTapeBackspace(LogicalTapeSet *lts, int tapenum, size_t size);
extern void LogicalTapeSeek(LogicalTapeSet *lts, int tapenum, long blocknum, int offset);
extern void LogicalTapeTell(LogicalTapeSet *lts, int tapenum, long *blocknum, int *offset);