Skip to content

Commit 6ff3751

Browse files
author
Rucha Kulkarni
committed
Adding Hooks for OprExpr and NullTest selectivity
Signed-off-by: Rucha Kulkarni <ruchask@amazon.com>
1 parent cd314d6 commit 6ff3751

3 files changed

Lines changed: 58 additions & 17 deletions

File tree

src/backend/optimizer/path/clausesel.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include "utils/fmgroids.h"
2424
#include "utils/lsyscache.h"
2525
#include "utils/selfuncs.h"
26+
#include "parser/parser.h"
27+
28+
/* Hook for extensions to override OpExpr selectivity */
29+
opexpr_selectivity_hook_type opexpr_selectivity_hook = NULL;
2630

2731
/*
2832
* Data structure for accumulating info about possible range-query
@@ -844,11 +848,21 @@ clause_selectivity_ext(PlannerInfo *root,
844848
}
845849
else
846850
{
847-
/* Estimate selectivity for a restriction clause. */
848-
s1 = restriction_selectivity(root, opno,
849-
opclause->args,
850-
opclause->inputcollid,
851-
varRelid);
851+
/*
852+
* Allow extensions to override OpExpr selectivity estimation
853+
* via the opexpr_selectivity_hook (Babelfish only).
854+
*/
855+
if (!(sql_dialect == SQL_DIALECT_TSQL &&
856+
opexpr_selectivity_hook &&
857+
opexpr_selectivity_hook(root, clause, varRelid, jointype,
858+
sjinfo, use_extended_stats, &s1)))
859+
{
860+
/* Estimate selectivity for a restriction clause. */
861+
s1 = restriction_selectivity(root, opno,
862+
opclause->args,
863+
opclause->inputcollid,
864+
varRelid);
865+
}
852866
}
853867

854868
/*

src/backend/utils/adt/selfuncs.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@
141141
#include "utils/syscache.h"
142142
#include "utils/timestamp.h"
143143
#include "utils/typcache.h"
144+
#include "parser/parser.h"
145+
146+
/* Hook for extensions to override NullTest selectivity */
147+
nulltest_selectivity_hook_type nulltest_selectivity_hook = NULL;
144148

145149
#define DEFAULT_PAGE_CPU_MULTIPLIER 50.0
146150

@@ -1754,20 +1758,26 @@ nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg,
17541758
else
17551759
{
17561760
/*
1757-
* No ANALYZE stats available, so make a guess
1761+
* No ANALYZE stats available. Allow extensions to override
1762+
* NullTest selectivity via nulltest_selectivity_hook (Babelfish only).
17581763
*/
1759-
switch (nulltesttype)
1764+
if (!(sql_dialect == SQL_DIALECT_TSQL &&
1765+
nulltest_selectivity_hook &&
1766+
nulltest_selectivity_hook(root, nulltesttype, arg, varRelid, &selec)))
17601767
{
1761-
case IS_NULL:
1762-
selec = DEFAULT_UNK_SEL;
1763-
break;
1764-
case IS_NOT_NULL:
1765-
selec = DEFAULT_NOT_UNK_SEL;
1766-
break;
1767-
default:
1768-
elog(ERROR, "unrecognized nulltesttype: %d",
1769-
(int) nulltesttype);
1770-
return (Selectivity) 0; /* keep compiler quiet */
1768+
switch (nulltesttype)
1769+
{
1770+
case IS_NULL:
1771+
selec = DEFAULT_UNK_SEL;
1772+
break;
1773+
case IS_NOT_NULL:
1774+
selec = DEFAULT_NOT_UNK_SEL;
1775+
break;
1776+
default:
1777+
elog(ERROR, "unrecognized nulltesttype: %d",
1778+
(int) nulltesttype);
1779+
return (Selectivity) 0; /* keep compiler quiet */
1780+
}
17711781
}
17721782
}
17731783

src/include/utils/selfuncs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,21 @@ extern Selectivity scalararraysel_containment(PlannerInfo *root,
248248
Oid elemtype, bool isEquality, bool useOr,
249249
int varRelid);
250250

251+
/* Babelfish hooks for selectivity estimation override */
252+
typedef bool (*opexpr_selectivity_hook_type) (PlannerInfo *root,
253+
Node *clause,
254+
int varRelid,
255+
JoinType jointype,
256+
SpecialJoinInfo *sjinfo,
257+
bool use_extended_stats,
258+
Selectivity *selec);
259+
extern PGDLLEXPORT opexpr_selectivity_hook_type opexpr_selectivity_hook;
260+
261+
typedef bool (*nulltest_selectivity_hook_type) (PlannerInfo *root,
262+
NullTestType nulltesttype,
263+
Node *arg,
264+
int varRelid,
265+
Selectivity *selec);
266+
extern PGDLLEXPORT nulltest_selectivity_hook_type nulltest_selectivity_hook;
267+
251268
#endif /* SELFUNCS_H */

0 commit comments

Comments
 (0)