110110
111111#include "access/htup_details.h"
112112#include "access/parallel.h"
113+ #include "catalog/pg_statistic.h"
114+ #include "catalog/pg_namespace.h"
113115#include "executor/executor.h"
114116#include "executor/hashjoin.h"
115117#include "executor/instrument.h" /* Instrumentation */
118120#include "executor/nodeRuntimeFilter.h"
119121#include "miscadmin.h"
120122#include "pgstat.h"
123+ #include "utils/datum.h"
121124#include "utils/guc.h"
122125#include "utils/fmgroids.h"
126+ #include "utils/lsyscache.h"
123127#include "utils/memutils.h"
128+ #include "utils/rel.h"
124129#include "utils/sharedtuplestore.h"
125130
126131#include "cdb/cdbvars.h"
@@ -168,10 +173,10 @@ static bool IsEqualOp(Expr *expr);
168173static bool CheckEqualArgs (Expr * expr , AttrNumber * lattno , AttrNumber * rattno );
169174static bool CheckTargetNode (PlanState * node ,
170175 AttrNumber attno ,
171- AttrNumber * lattno );
176+ AttrNumber * lattno , Oid * collation , Oid * var_type );
172177static List * FindTargetNodes (HashJoinState * hjstate ,
173178 AttrNumber attno ,
174- AttrNumber * lattno );
179+ AttrNumber * lattno , Oid * collation , Oid * var_type );
175180static AttrFilter * CreateAttrFilter (PlanState * target ,
176181 AttrNumber lattno ,
177182 AttrNumber rattno ,
@@ -2192,6 +2197,8 @@ CreateRuntimeFilter(HashJoinState* hjstate)
21922197 AttrFilter * attr_filter ;
21932198 ListCell * lc ;
21942199 List * targets ;
2200+ Oid var_type ;
2201+ Oid collation ;
21952202
21962203 /*
21972204 * A build-side Bloom filter tells us if a row is definitely not in the build
@@ -2232,7 +2239,7 @@ CreateRuntimeFilter(HashJoinState* hjstate)
22322239 if (lattno < 1 || rattno < 1 )
22332240 continue ;
22342241
2235- targets = FindTargetNodes (hjstate , lattno , & lattno );
2242+ targets = FindTargetNodes (hjstate , lattno , & lattno , & collation , & var_type );
22362243 if (lattno == -1 || targets == NULL )
22372244 continue ;
22382245
@@ -2243,6 +2250,8 @@ CreateRuntimeFilter(HashJoinState* hjstate)
22432250
22442251 attr_filter = CreateAttrFilter (target , lattno , rattno ,
22452252 hstate -> ps .plan -> plan_rows );
2253+ attr_filter -> vartype = var_type ;
2254+ attr_filter -> collation = collation ;
22462255 if (attr_filter -> blm_filter )
22472256 hstate -> filters = lappend (hstate -> filters , attr_filter );
22482257 else
@@ -2329,7 +2338,7 @@ CheckEqualArgs(Expr *expr, AttrNumber *lattno, AttrNumber *rattno)
23292338}
23302339
23312340static bool
2332- CheckTargetNode (PlanState * node , AttrNumber attno , AttrNumber * lattno )
2341+ CheckTargetNode (PlanState * node , AttrNumber attno , AttrNumber * lattno , Oid * collation , Oid * var_type )
23332342{
23342343 Var * var ;
23352344 TargetEntry * te ;
@@ -2348,6 +2357,8 @@ CheckTargetNode(PlanState *node, AttrNumber attno, AttrNumber *lattno)
23482357 return false;
23492358
23502359 * lattno = var -> varattno ;
2360+ * collation = var -> varcollid ;
2361+ * var_type = var -> vartype ;
23512362
23522363 return true;
23532364}
@@ -2360,7 +2371,7 @@ CheckTargetNode(PlanState *node, AttrNumber attno, AttrNumber *lattno)
23602371 * SeqScan <- target
23612372 */
23622373static List *
2363- FindTargetNodes (HashJoinState * hjstate , AttrNumber attno , AttrNumber * lattno )
2374+ FindTargetNodes (HashJoinState * hjstate , AttrNumber attno , AttrNumber * lattno , Oid * collation , Oid * var_type )
23642375{
23652376 Var * var ;
23662377 PlanState * child , * parent ;
@@ -2386,7 +2397,7 @@ FindTargetNodes(HashJoinState *hjstate, AttrNumber attno, AttrNumber *lattno)
23862397 * result
23872398 * seqscan
23882399 */
2389- if (!CheckTargetNode (child , attno , lattno ))
2400+ if (!CheckTargetNode (child , attno , lattno , collation , var_type ))
23902401 return NULL ;
23912402
23922403 targetNodes = lappend (targetNodes , child );
@@ -2404,7 +2415,7 @@ FindTargetNodes(HashJoinState *hjstate, AttrNumber attno, AttrNumber *lattno)
24042415 for (int i = 0 ; i < as -> as_nplans ; i ++ )
24052416 {
24062417 child = as -> appendplans [i ];
2407- if (!CheckTargetNode (child , attno , lattno ))
2418+ if (!CheckTargetNode (child , attno , lattno , collation , var_type ))
24082419 return NULL ;
24092420
24102421 targetNodes = lappend (targetNodes , child );
@@ -2452,12 +2463,25 @@ CreateAttrFilter(PlanState *target, AttrNumber lattno, AttrNumber rattno,
24522463{
24532464 AttrFilter * attr_filter = palloc0 (sizeof (AttrFilter ));
24542465 attr_filter -> empty = true;
2466+ attr_filter -> hasnulls = false;
24552467 attr_filter -> target = target ;
24562468
24572469 attr_filter -> lattno = lattno ;
24582470 attr_filter -> rattno = rattno ;
2471+ attr_filter -> n_distinct = 0.0 ;
24592472
2460- attr_filter -> blm_filter = bloom_create_aggresive (plan_rows , work_mem , random ());
2473+ attr_filter -> blm_filter = bloom_create_aggresive (plan_rows , work_mem , gp_session_id );
2474+
2475+ if (target && IsA (target , SeqScanState ))
2476+ {
2477+ HeapTuple statstuple ;
2478+ SeqScanState * scan = (SeqScanState * )target ;
2479+ statstuple = get_att_stats (RelationGetRelid (scan -> ss .ss_currentRelation ), lattno );
2480+ if (HeapTupleIsValid (statstuple ))
2481+ {
2482+ attr_filter -> n_distinct = ((Form_pg_statistic ) GETSTRUCT (statstuple ))-> stadistinct ;
2483+ }
2484+ }
24612485
24622486 StaticAssertDecl (sizeof (LONG_MAX ) == sizeof (Datum ), "sizeof(LONG_MAX) should be equal to sizeof(Datum)" );
24632487 StaticAssertDecl (sizeof (LONG_MIN ) == sizeof (Datum ), "sizeof(LONG_MIN) should be equal to sizeof(Datum)" );
0 commit comments