gfphoenix78 commented on code in PR #724:
URL: https://github.com/apache/cloudberry/pull/724#discussion_r1855726047
##########
src/backend/executor/nodeHash.c:
##########
@@ -4126,3 +4151,138 @@ get_hash_mem(void)
return (int) mem_limit;
}
+
+/*
+ * Convert AttrFilter to ScanKeyData and send these runtime filters to the
+ * target node(seqscan).
+ */
+void
+PushdownRuntimeFilter(HashState *node)
+{
+ ListCell *lc;
+ List *scankeys;
+ ScanKey sk;
+ AttrFilter *attr_filter;
+
+ foreach (lc, node->filters)
+ {
+ scankeys = NIL;
+
+ attr_filter = lfirst(lc);
+ if (!IsA(attr_filter->target, SeqScanState) ||
attr_filter->empty)
+ continue;
+
+ /* bloom filter */
+ sk = (ScanKey)palloc0(sizeof(ScanKeyData));
+ sk->sk_flags = SK_BLOOM_FILTER;
+ sk->sk_attno = attr_filter->lattno;
+ sk->sk_subtype = INT8OID;
+ sk->sk_argument = PointerGetDatum(attr_filter->blm_filter);
+ scankeys = lappend(scankeys, sk);
+
+ /* range filter */
+ sk = (ScanKey)palloc0(sizeof(ScanKeyData));
+ sk->sk_flags = 0;
+ sk->sk_attno = attr_filter->lattno;
+ sk->sk_strategy = BTGreaterEqualStrategyNumber;
+ sk->sk_subtype = INT8OID;
+ sk->sk_argument = attr_filter->min;
+ scankeys = lappend(scankeys, sk);
+
+ sk = (ScanKey)palloc0(sizeof(ScanKeyData));
+ sk->sk_flags = 0;
+ sk->sk_attno = attr_filter->lattno;
+ sk->sk_strategy = BTLessEqualStrategyNumber;
+ sk->sk_subtype = INT8OID;
+ sk->sk_argument = attr_filter->max;
+ scankeys = lappend(scankeys, sk);
+
+ /* append new runtime filters to target node */
+ SeqScanState *sss = castNode(SeqScanState, attr_filter->target);
+ sss->filters = list_concat(sss->filters, scankeys);
+ }
+}
+
+static void
+BuildRuntimeFilter(HashState *node, TupleTableSlot *slot)
+{
+ Datum val;
+ bool isnull;
+ ListCell *lc;
+ AttrFilter *attr_filter;
+
+ foreach (lc, node->filters)
+ {
+ attr_filter = (AttrFilter *) lfirst(lc);
+
+ val = slot_getattr(slot, attr_filter->rattno, &isnull);
+ if (isnull)
+ continue;
+
+ attr_filter->empty = false;
+
+ if ((int64_t)val < (int64_t)attr_filter->min)
+ attr_filter->min = val;
+
+ if ((int64_t)val > (int64_t)attr_filter->max)
+ attr_filter->max = val;
+
+ if (attr_filter->blm_filter)
+ bloom_add_element(attr_filter->blm_filter, (unsigned
char *)&val, sizeof(Datum));
+ }
+}
+
+void
+FreeRuntimeFilter(HashState *node)
+{
+ ListCell *lc;
+ AttrFilter *attr_filter;
+
+ if (!node->filters)
+ return;
+
+ foreach (lc, node->filters)
+ {
+ attr_filter = lfirst(lc);
+ if (attr_filter->blm_filter)
+ bloom_free(attr_filter->blm_filter);
+ }
+
+ list_free_deep(node->filters);
+ node->filters = NIL;
+}
+
+void
+ResetRuntimeFilter(HashState *node)
+{
+ ListCell *lc;
+ AttrFilter *attr_filter;
+ SeqScanState *sss;
+
+ if (!node->filters)
+ return;
+
+ foreach (lc, node->filters)
+ {
+ attr_filter = lfirst(lc);
+ attr_filter->empty = true;
+
+ if (IsA(attr_filter->target, SeqScanState))
+ {
+ sss = castNode(SeqScanState, attr_filter->target);
+ if (sss->filters)
+ {
+ list_free_deep(sss->filters);
+ sss->filters = NIL;
+ }
+ }
+
+ if (attr_filter->blm_filter)
+ bloom_free(attr_filter->blm_filter);
+
+ attr_filter->blm_filter =
bloom_create_aggresive(node->ps.plan->plan_rows,
+ work_mem,
random());
+ attr_filter->min = LLONG_MAX;
+ attr_filter->max = LLONG_MIN;
Review Comment:
LLONG_MAX, LLONG_MIN are platform-spec value, i.e. the bound value for
`unsigned long long`, which may not be exactly the same width as Datum. For
safety, static assert could be considered.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]