Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BBF hook to adjust numeric result to correct scale #529

Open
wants to merge 29 commits into
base: BABEL_5_X_DEV__PG_17_X
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ca61bea
Added BBF hook to truncate numeric result to correct scale
Feb 5, 2025
2c1cf43
added type cast on expr argument
Feb 5, 2025
467717c
Merge branch 'BABEL_5_X_DEV__PG_17_X' into BABEL-5467
Feb 5, 2025
18799df
refactored code
Feb 5, 2025
7ab691d
retrigger workflows
Feb 5, 2025
1968f4e
numeric_trunc should only be used for strict function
Feb 5, 2025
ffac900
retrigger workflows
Feb 5, 2025
675c72d
refactored code
Feb 5, 2025
c54c6ac
Added some sanity checks on hooks
Feb 6, 2025
7b10e5f
removed redundant brackets
Feb 6, 2025
4d792f6
Merge branch 'BABEL_5_X_DEV__PG_17_X' into BABEL-5467
Feb 10, 2025
70daba6
Adde BBF hook to truncate numeric result of aggregate to correct scale
Feb 11, 2025
7a855d1
hooks should be marked as PGDLLIMPORT
Feb 12, 2025
a1c521c
refactored code
Feb 14, 2025
4480b71
Merge branch 'BABEL_5_X_DEV__PG_17_X' into BABEL-5467
Feb 14, 2025
c4fce3c
added result type check on hook execution
Feb 14, 2025
c91f17e
updated hook pltsql_trunc_numeric_result_hook to pass the plan
Feb 17, 2025
144cc05
Added hook pltsql_ExecInitResultTypeTL_hook
Feb 18, 2025
a548481
moved pltsql_ExecInitResultTypeTL_hook inside ExecInitResultTypeTL fu…
Feb 18, 2025
ae52d1f
refactor code
Feb 18, 2025
8ec70df
refactor code
Feb 18, 2025
2e50f79
refactored code
Feb 25, 2025
ddb3c57
Merge branch 'BABEL_5_X_DEV__PG_17_X' into BABEL-5467
Feb 27, 2025
109d095
renamed hooks and moved checks around hooks inside hooks
Feb 27, 2025
d4c32f7
added sanity checks on pointers accessing in arguments
Feb 27, 2025
ee28e78
Added hook in non strict function execution as well
Feb 28, 2025
b10e7bb
Merge branch 'BABEL_5_X_DEV__PG_17_X' into BABEL-5467
Feb 28, 2025
119177b
renamed trunc_numeric_result_hook to adjust_numeric_result_hook
Mar 9, 2025
8e9596e
code refactor
Mar 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/backend/executor/execExprInterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
*/
#if defined(EEO_USE_COMPUTED_GOTO)

/* Hook to truncate result to correct scale, when resulttype is numeric */
adjust_numeric_result_hook_type adjust_numeric_result_hook = NULL;

/* struct for jump target -> opcode lookup table */
typedef struct ExprEvalOpLookup
{
Expand Down Expand Up @@ -741,6 +744,9 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
*op->resvalue = d;
*op->resnull = fcinfo->isnull;

if (adjust_numeric_result_hook && fcinfo->flinfo != NULL)
*op->resvalue = adjust_numeric_result_hook(NULL, fcinfo->flinfo->fn_expr, d, *op->resnull, InvalidOid, -1);

EEO_NEXT();
}

Expand All @@ -765,6 +771,9 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
*op->resvalue = d;
*op->resnull = fcinfo->isnull;

if (adjust_numeric_result_hook && fcinfo->flinfo != NULL)
*op->resvalue = adjust_numeric_result_hook(NULL, fcinfo->flinfo->fn_expr, d, *op->resnull, InvalidOid, -1);

strictfail:
EEO_NEXT();
}
Expand Down
5 changes: 5 additions & 0 deletions src/backend/executor/execTuples.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const TupleTableSlotOps TTSOpsHeapTuple;
const TupleTableSlotOps TTSOpsMinimalTuple;
const TupleTableSlotOps TTSOpsBufferHeapTuple;

/* Hook to update typmod of all the entries of a previously initialized tuple descriptor */
ExecUpdateResultTypeTL_hook_type ExecUpdateResultTypeTL_hook = NULL;

/*
* TupleTableSlotOps implementations.
Expand Down Expand Up @@ -1843,6 +1845,9 @@ ExecInitResultTypeTL(PlanState *planstate)
{
TupleDesc tupDesc = ExecTypeFromTL(planstate->plan->targetlist);

if (ExecUpdateResultTypeTL_hook)
ExecUpdateResultTypeTL_hook(planstate, tupDesc);

planstate->ps_ResultTupleDesc = tupDesc;
}

Expand Down
10 changes: 10 additions & 0 deletions src/backend/executor/nodeAgg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,16 @@ finalize_aggregate(AggState *aggstate,

result = FunctionCallInvoke(fcinfo);
*resultIsNull = fcinfo->isnull;
if (adjust_numeric_result_hook)
{
if (peragg->aggref != NULL)
result = adjust_numeric_result_hook(aggstate->ss.ps.plan,
(Node *) peragg->aggref,
result,
*resultIsNull,
peragg->aggref->aggtype,
-1);
}
*resultVal = MakeExpandedObjectReadOnly(result,
fcinfo->isnull,
peragg->resulttypeLen);
Expand Down
5 changes: 5 additions & 0 deletions src/backend/nodes/nodeFuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static bool planstate_walk_members(PlanState **planstates, int nplans,
void *context);

coalesce_typmod_hook_type coalesce_typmod_hook = NULL;
exprTypmod_hook_type exprTypmod_hook = NULL;

/*
* exprType -
Expand Down Expand Up @@ -539,6 +540,10 @@ exprTypmod(const Node *expr)
default:
break;
}

if (exprTypmod_hook)
return exprTypmod_hook(NULL, (Node *) expr);

return -1;
}

Expand Down
21 changes: 18 additions & 3 deletions src/backend/optimizer/util/clauses.c
Original file line number Diff line number Diff line change
Expand Up @@ -2631,19 +2631,23 @@ eval_const_expressions_mutator(Node *node,
List *args = expr->args;
Expr *simple;
OpExpr *newexpr;
int32 result_typmod = -1;

/*
* Need to get OID of underlying function. Okay to scribble
* on input to this extent.
*/
set_opfuncid(expr);

if (exprTypmod_hook)
result_typmod = exprTypmod_hook(NULL, node);

/*
* Code for op/func reduction is pretty bulky, so split it out
* as a separate function.
*/
simple = simplify_function(expr->opfuncid,
expr->opresulttype, -1,
expr->opresulttype, result_typmod,
expr->opcollid,
expr->inputcollid,
&args,
Expand Down Expand Up @@ -2694,6 +2698,7 @@ eval_const_expressions_mutator(Node *node,
bool has_nonconst_input = false;
Expr *simple;
DistinctExpr *newexpr;
int32 result_typmod = -1;

/*
* Reduce constants in the DistinctExpr's arguments. We know
Expand Down Expand Up @@ -2742,12 +2747,15 @@ eval_const_expressions_mutator(Node *node,
set_opfuncid((OpExpr *) expr); /* rely on struct
* equivalence */

if (exprTypmod_hook)
result_typmod = exprTypmod_hook(NULL, node);

/*
* Code for op/func reduction is pretty bulky, so split it
* out as a separate function.
*/
simple = simplify_function(expr->opfuncid,
expr->opresulttype, -1,
expr->opresulttype, result_typmod,
expr->opcollid,
expr->inputcollid,
&args,
Expand Down Expand Up @@ -2979,6 +2987,7 @@ eval_const_expressions_mutator(Node *node,
Oid intypioparam;
Expr *simple;
CoerceViaIO *newexpr;
int32 result_typmod = -1;

/* Make a List so we can use simplify_function */
args = list_make1(expr->arg);
Expand Down Expand Up @@ -3029,8 +3038,11 @@ eval_const_expressions_mutator(Node *node,
false,
true));

if (exprTypmod_hook)
result_typmod = exprTypmod_hook(NULL, node);

simple = simplify_function(infunc,
expr->resulttype, -1,
expr->resulttype, result_typmod,
expr->resultcollid,
InvalidOid,
&args,
Expand Down Expand Up @@ -5128,6 +5140,9 @@ evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
/* Release all the junk we just created */
FreeExecutorState(estate);

if (adjust_numeric_result_hook)
const_val = adjust_numeric_result_hook(NULL, (Node *) expr, const_val, const_is_null, result_type, result_typmod);

/*
* Make the constant result node.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/include/executor/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ extern PGDLLEXPORT TriggerRecuresiveCheck_hook_type TriggerRecuresiveCheck_hook;
typedef bool (*bbfViewHasInsteadofTrigger_hook_type) (Relation view, CmdType event);
extern PGDLLIMPORT bbfViewHasInsteadofTrigger_hook_type bbfViewHasInsteadofTrigger_hook;

typedef Datum (*adjust_numeric_result_hook_type) (Plan *plan, Node *expr, Datum result, bool result_isnull, Oid result_type, int32 result_typmod);
extern PGDLLIMPORT adjust_numeric_result_hook_type adjust_numeric_result_hook;

typedef void (*ExecUpdateResultTypeTL_hook_type) (PlanState *planstate, TupleDesc desc);
extern PGDLLIMPORT ExecUpdateResultTypeTL_hook_type ExecUpdateResultTypeTL_hook;

typedef bool (*check_rowcount_hook_type) (int es_processed);
extern PGDLLEXPORT check_rowcount_hook_type check_rowcount_hook;

Expand Down
4 changes: 4 additions & 0 deletions src/include/nodes/nodeFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define NODEFUNCS_H

#include "nodes/parsenodes.h"
#include "nodes/plannodes.h"

struct PlanState; /* avoid including execnodes.h too */

Expand Down Expand Up @@ -222,4 +223,7 @@ extern bool planstate_tree_walker_impl(struct PlanState *planstate,
typedef int32 (*coalesce_typmod_hook_type) (const CoalesceExpr *cexpr);
extern PGDLLEXPORT coalesce_typmod_hook_type coalesce_typmod_hook;

typedef int32 (*exprTypmod_hook_type)(Plan *plan, Node *expr);
extern PGDLLIMPORT exprTypmod_hook_type exprTypmod_hook;

#endif /* NODEFUNCS_H */