Use ts_extract_expr_args for HypertableRestrictInfo

Refactor HypertableRestrictInfo code to make use of the newly added
ts_extract_expr_args function.
This commit is contained in:
Sven Klemm 2024-05-03 13:31:39 +02:00 committed by Sven Klemm
parent 0075bb0c81
commit c5ef48965c

View File

@ -12,6 +12,7 @@
#include <utils/builtins.h>
#include <utils/lsyscache.h>
#include <utils/typcache.h>
#include <tcop/tcopprot.h>
#include "hypertable_restrict_info.h"
@ -20,15 +21,13 @@
#include "dimension.h"
#include "dimension_slice.h"
#include "dimension_vector.h"
#include "expression_utils.h"
#include "guc.h"
#include "hypercube.h"
#include "partitioning.h"
#include "scan_iterator.h"
#include "utils.h"
#include <inttypes.h>
#include <tcop/tcopprot.h>
typedef struct DimensionRestrictInfo
{
const Dimension *dimension;
@ -307,13 +306,12 @@ hypertable_restrict_info_get(HypertableRestrictInfo *hri, AttrNumber attno)
typedef DimensionValues *(*get_dimension_values)(Const *c, bool use_or);
static bool
hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root, List *expr_args,
Oid op_oid, get_dimension_values func_get_dim_values, bool use_or)
static void
hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root, Var *v,
Expr *expr, Oid op_oid, get_dimension_values func_get_dim_values,
bool use_or)
{
Expr *leftop, *rightop, *expr;
DimensionRestrictInfo *dri;
Var *v;
Const *c;
RangeTblEntry *rte;
Oid columntype;
@ -322,46 +320,21 @@ hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root
Oid lefttype, righttype;
DimensionValues *dimvalues;
if (list_length(expr_args) != 2)
return false;
leftop = linitial(expr_args);
rightop = lsecond(expr_args);
if (IsA(leftop, RelabelType))
leftop = ((RelabelType *) leftop)->arg;
if (IsA(rightop, RelabelType))
rightop = ((RelabelType *) rightop)->arg;
if (IsA(leftop, Var))
{
v = (Var *) leftop;
expr = rightop;
}
else if (IsA(rightop, Var))
{
v = (Var *) rightop;
expr = leftop;
op_oid = get_commutator(op_oid);
}
else
return false;
dri = hypertable_restrict_info_get(hri, v->varattno);
/* the attribute is not a dimension */
if (dri == NULL)
return false;
return;
expr = (Expr *) eval_const_expressions(root, (Node *) expr);
if (!IsA(expr, Const) || !OidIsValid(op_oid) || !op_strict(op_oid))
return false;
return;
c = (Const *) expr;
/* quick check for a NULL constant */
if (c->constisnull)
return false;
return;
rte = rt_fetch(v->varno, root->parse->rtable);
@ -369,11 +342,14 @@ hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root
tce = lookup_type_cache(columntype, TYPECACHE_BTREE_OPFAMILY);
if (!op_in_opfamily(op_oid, tce->btree_opf))
return false;
return;
get_op_opfamily_properties(op_oid, tce->btree_opf, false, &strategy, &lefttype, &righttype);
dimvalues = func_get_dim_values(c, use_or);
return dimension_restrict_info_add(dri, strategy, c->constcollid, dimvalues);
if (dimension_restrict_info_add(dri, strategy, c->constcollid, dimvalues))
{
hri->num_base_restrictions++;
}
}
static DimensionValues *
@ -426,7 +402,9 @@ static void
hypertable_restrict_info_add_restrict_info(HypertableRestrictInfo *hri, PlannerInfo *root,
RestrictInfo *ri)
{
bool added = false;
Oid opno;
Var *var;
Expr *arg_value;
Expr *e = ri->clause;
@ -434,40 +412,31 @@ hypertable_restrict_info_add_restrict_info(HypertableRestrictInfo *hri, PlannerI
if (contain_mutable_functions((Node *) e))
return;
switch (nodeTag(e))
if (ts_extract_expr_args(e, &var, &arg_value, &opno, NULL))
{
case T_OpExpr:
get_dimension_values value_func;
bool use_or;
switch (nodeTag(e))
{
OpExpr *op_expr = (OpExpr *) e;
added = hypertable_restrict_info_add_expr(hri,
root,
op_expr->args,
op_expr->opno,
dimension_values_create_from_single_element,
false);
break;
case T_OpExpr:
{
value_func = dimension_values_create_from_single_element;
use_or = false;
break;
}
case T_ScalarArrayOpExpr:
{
value_func = dimension_values_create_from_array;
use_or = castNode(ScalarArrayOpExpr, e)->useOr;
break;
}
default:
/* we don't support other node types */
return;
}
case T_ScalarArrayOpExpr:
{
ScalarArrayOpExpr *scalar_expr = (ScalarArrayOpExpr *) e;
added = hypertable_restrict_info_add_expr(hri,
root,
scalar_expr->args,
scalar_expr->opno,
dimension_values_create_from_array,
scalar_expr->useOr);
break;
}
default:
/* we don't support other node types */
break;
hypertable_restrict_info_add_expr(hri, root, var, arg_value, opno, value_func, use_or);
}
if (added)
hri->num_base_restrictions++;
}
void