Skip to content

Commit

Permalink
stable expressions as constant in vectorized filters
Browse files Browse the repository at this point in the history
  • Loading branch information
akuzm committed Oct 17, 2023
1 parent 5912a82 commit 8f8d17d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tsl/src/nodes/decompress_chunk/compressed_batch.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ apply_vector_quals(DecompressChunkState *chunk_state, DecompressBatchState *batc
/* For now we only support "Var ? Const" predicates. */
OpExpr *oe = castNode(OpExpr, lfirst(lc));
Var *var = castNode(Var, linitial(oe->args));
Ensure(IsA(lsecond(oe->args), Const),
"failed to evaluate runtime constant in vectorized filter");
Const *constnode = castNode(Const, lsecond(oe->args));

/*
Expand Down
14 changes: 14 additions & 0 deletions tsl/src/nodes/decompress_chunk/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <nodes/bitmapset.h>
#include <nodes/makefuncs.h>
#include <nodes/nodeFuncs.h>
#include <optimizer/optimizer.h>
#include <parser/parsetree.h>
#include <rewrite/rewriteManip.h>
#include <utils/datum.h>
Expand Down Expand Up @@ -479,6 +480,19 @@ decompress_chunk_begin(CustomScanState *node, EState *estate, int eflags)
{
elog(ERROR, "debug: batch sorted merge is required but not used");
}

/* Constify stable expressions in vectorized predicates. */
PlannerGlobal glob = {
.boundParams = node->ss.ps.state->es_param_list_info,
};
PlannerInfo root = {
.glob = &glob,
};
ListCell *lc;
foreach (lc, chunk_state->vectorized_quals)
{
lfirst(lc) = estimate_expression_value(&root, (Node *) lfirst(lc));
}
}

/*
Expand Down
41 changes: 39 additions & 2 deletions tsl/src/nodes/decompress_chunk/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,43 @@ find_attr_pos_in_tlist(List *targetlist, AttrNumber pos)
pg_unreachable();
}

static bool
contains_volatile_functions_checker(Oid func_id, void *context)
{
return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
}

static bool
is_not_runtime_constant_walker(Node *node, void *context)
{
switch (nodeTag(node))
{
case T_Var:
case T_Param:
case T_PlaceHolderVar:
return true;
default:
if (check_functions_in_node(node,
contains_volatile_functions_checker,
/* context = */ NULL))
{
return true;
}
return expression_tree_walker(node,
is_not_runtime_constant_walker,
/* context = */ NULL);
}
}

static bool
is_not_runtime_constant(Node *node)
{
bool result = expression_tree_walker(node,
is_not_runtime_constant_walker,
/* context = */ NULL);
return result;
}

static bool
qual_is_vectorizable(DecompressChunkPath *path, Node *qual)
{
Expand All @@ -407,7 +444,7 @@ qual_is_vectorizable(DecompressChunkPath *path, Node *qual)
return false;
}

if (IsA(lsecond(o->args), Var) && IsA(linitial(o->args), Const))
if (IsA(lsecond(o->args), Var))
{
/* Try to commute the operator if the constant is on the right. */
Oid commutator_opno = get_commutator(o->opno);
Expand All @@ -423,7 +460,7 @@ qual_is_vectorizable(DecompressChunkPath *path, Node *qual)
}
}

if (!IsA(linitial(o->args), Var) || !IsA(lsecond(o->args), Const))
if (!IsA(linitial(o->args), Var) || is_not_runtime_constant(lsecond(o->args)))
{
return false;
}
Expand Down

0 comments on commit 8f8d17d

Please sign in to comment.