Skip to content

Commit c5ef489

Browse files
committed
Use ts_extract_expr_args for HypertableRestrictInfo
Refactor HypertableRestrictInfo code to make use of the newly added ts_extract_expr_args function.
1 parent 0075bb0 commit c5ef489

File tree

1 file changed

+37
-68
lines changed

1 file changed

+37
-68
lines changed

src/hypertable_restrict_info.c

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <utils/builtins.h>
1313
#include <utils/lsyscache.h>
1414
#include <utils/typcache.h>
15+
#include <tcop/tcopprot.h>
1516

1617
#include "hypertable_restrict_info.h"
1718

@@ -20,15 +21,13 @@
2021
#include "dimension.h"
2122
#include "dimension_slice.h"
2223
#include "dimension_vector.h"
24+
#include "expression_utils.h"
2325
#include "guc.h"
2426
#include "hypercube.h"
2527
#include "partitioning.h"
2628
#include "scan_iterator.h"
2729
#include "utils.h"
2830

29-
#include <inttypes.h>
30-
#include <tcop/tcopprot.h>
31-
3231
typedef struct DimensionRestrictInfo
3332
{
3433
const Dimension *dimension;
@@ -307,13 +306,12 @@ hypertable_restrict_info_get(HypertableRestrictInfo *hri, AttrNumber attno)
307306

308307
typedef DimensionValues *(*get_dimension_values)(Const *c, bool use_or);
309308

310-
static bool
311-
hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root, List *expr_args,
312-
Oid op_oid, get_dimension_values func_get_dim_values, bool use_or)
309+
static void
310+
hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root, Var *v,
311+
Expr *expr, Oid op_oid, get_dimension_values func_get_dim_values,
312+
bool use_or)
313313
{
314-
Expr *leftop, *rightop, *expr;
315314
DimensionRestrictInfo *dri;
316-
Var *v;
317315
Const *c;
318316
RangeTblEntry *rte;
319317
Oid columntype;
@@ -322,58 +320,36 @@ hypertable_restrict_info_add_expr(HypertableRestrictInfo *hri, PlannerInfo *root
322320
Oid lefttype, righttype;
323321
DimensionValues *dimvalues;
324322

325-
if (list_length(expr_args) != 2)
326-
return false;
327-
328-
leftop = linitial(expr_args);
329-
rightop = lsecond(expr_args);
330-
331-
if (IsA(leftop, RelabelType))
332-
leftop = ((RelabelType *) leftop)->arg;
333-
if (IsA(rightop, RelabelType))
334-
rightop = ((RelabelType *) rightop)->arg;
335-
336-
if (IsA(leftop, Var))
337-
{
338-
v = (Var *) leftop;
339-
expr = rightop;
340-
}
341-
else if (IsA(rightop, Var))
342-
{
343-
v = (Var *) rightop;
344-
expr = leftop;
345-
op_oid = get_commutator(op_oid);
346-
}
347-
else
348-
return false;
349-
350323
dri = hypertable_restrict_info_get(hri, v->varattno);
351324
/* the attribute is not a dimension */
352325
if (dri == NULL)
353-
return false;
326+
return;
354327

355328
expr = (Expr *) eval_const_expressions(root, (Node *) expr);
356329

357330
if (!IsA(expr, Const) || !OidIsValid(op_oid) || !op_strict(op_oid))
358-
return false;
331+
return;
359332

360333
c = (Const *) expr;
361334

362335
/* quick check for a NULL constant */
363336
if (c->constisnull)
364-
return false;
337+
return;
365338

366339
rte = rt_fetch(v->varno, root->parse->rtable);
367340

368341
columntype = get_atttype(rte->relid, dri->dimension->column_attno);
369342
tce = lookup_type_cache(columntype, TYPECACHE_BTREE_OPFAMILY);
370343

371344
if (!op_in_opfamily(op_oid, tce->btree_opf))
372-
return false;
345+
return;
373346

374347
get_op_opfamily_properties(op_oid, tce->btree_opf, false, &strategy, &lefttype, &righttype);
375348
dimvalues = func_get_dim_values(c, use_or);
376-
return dimension_restrict_info_add(dri, strategy, c->constcollid, dimvalues);
349+
if (dimension_restrict_info_add(dri, strategy, c->constcollid, dimvalues))
350+
{
351+
hri->num_base_restrictions++;
352+
}
377353
}
378354

379355
static DimensionValues *
@@ -426,48 +402,41 @@ static void
426402
hypertable_restrict_info_add_restrict_info(HypertableRestrictInfo *hri, PlannerInfo *root,
427403
RestrictInfo *ri)
428404
{
429-
bool added = false;
405+
Oid opno;
406+
Var *var;
407+
Expr *arg_value;
430408

431409
Expr *e = ri->clause;
432410

433411
/* Same as constraint_exclusion */
434412
if (contain_mutable_functions((Node *) e))
435413
return;
436414

437-
switch (nodeTag(e))
415+
if (ts_extract_expr_args(e, &var, &arg_value, &opno, NULL))
438416
{
439-
case T_OpExpr:
440-
{
441-
OpExpr *op_expr = (OpExpr *) e;
442-
443-
added = hypertable_restrict_info_add_expr(hri,
444-
root,
445-
op_expr->args,
446-
op_expr->opno,
447-
dimension_values_create_from_single_element,
448-
false);
449-
break;
450-
}
417+
get_dimension_values value_func;
418+
bool use_or;
451419

452-
case T_ScalarArrayOpExpr:
420+
switch (nodeTag(e))
453421
{
454-
ScalarArrayOpExpr *scalar_expr = (ScalarArrayOpExpr *) e;
455-
456-
added = hypertable_restrict_info_add_expr(hri,
457-
root,
458-
scalar_expr->args,
459-
scalar_expr->opno,
460-
dimension_values_create_from_array,
461-
scalar_expr->useOr);
462-
break;
422+
case T_OpExpr:
423+
{
424+
value_func = dimension_values_create_from_single_element;
425+
use_or = false;
426+
break;
427+
}
428+
case T_ScalarArrayOpExpr:
429+
{
430+
value_func = dimension_values_create_from_array;
431+
use_or = castNode(ScalarArrayOpExpr, e)->useOr;
432+
break;
433+
}
434+
default:
435+
/* we don't support other node types */
436+
return;
463437
}
464-
default:
465-
/* we don't support other node types */
466-
break;
438+
hypertable_restrict_info_add_expr(hri, root, var, arg_value, opno, value_func, use_or);
467439
}
468-
469-
if (added)
470-
hri->num_base_restrictions++;
471440
}
472441

473442
void

0 commit comments

Comments
 (0)