Skip to content

Commit

Permalink
Refactor utility function to be used by both SUM and AVG Aggregates (y…
Browse files Browse the repository at this point in the history
…ugabyte#82)

Description
Refactor the previously introduced helper function into a common utility (bigint_utility) function which can be used by both SUM and AVG aggregates for BIGINT datatype

Issues Resolved
BABEL-3507

Extension PR
babelfish-for-postgresql/babelfish_extensions#1045

Signed-off-by: Nirmit Shah nirmisha@amazon.com
  • Loading branch information
shah-nirmit authored and abhinab-yb committed Nov 14, 2024
1 parent 00848ef commit 0e33a25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/postgres/src/backend/utils/adt/numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -5941,8 +5941,14 @@ numeric_poly_sum(PG_FUNCTION_ARGS)
#endif
}

/*
* Final function for BIGINT datatype for TSQL Aggregates (SUM,AVG).
* The tsql aggregate return non-default return types when compared with
* Default aggregate for integer datatypes , this function takes the accumlated
* State and tranform the result into return type expected by tsql.
*/
Datum
bigint_poly_sum(PG_FUNCTION_ARGS)
bigint_poly_aggr_final(FunctionCallInfo fcinfo, tsqlAggType aggType)
{

PolyNumAggState *state;
Expand All @@ -5969,10 +5975,23 @@ bigint_poly_sum(PG_FUNCTION_ARGS)
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("Arithmetic overflow error converting expression to data type bigint.")));
}
else
PG_RETURN_INT64((int64) result);
else {
if (aggType == TSQL_SUM)
PG_RETURN_INT64((int64) result);
/* If the aggregate type is TSQL_AVG */
else
{
result /= state->N;
PG_RETURN_INT64((int64) result);
}
}
#else
temp = numeric_sum(fcinfo);
if (tsqlAggType == TSQL_SUM)
temp = numeric_sum(fcinfo);
/* If the aggregate type is TSQL_AVG */
else
temp = numeric_avg(fcinfo);

init_var(&nvar);
set_var_from_num(DatumGetNumeric(temp), &nvar);
is_overflow = !(numericvar_to_int64(&nvar, &result));
Expand Down
8 changes: 7 additions & 1 deletion src/postgres/src/include/utils/numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
struct NumericData;
typedef struct NumericData *Numeric;

/* Enum type for bigint aggregates for tsql dialect */
typedef enum tsqlAggType {
TSQL_SUM,
TSQL_AVG
} tsqlAggType;

/*
* fmgr interface macros
*/
Expand Down Expand Up @@ -95,7 +101,7 @@ extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2,
bool *have_error);
extern int32 numeric_int4_opt_error(Numeric num, bool *error);

extern Datum bigint_poly_sum(PG_FUNCTION_ARGS);
extern Datum bigint_poly_aggr_final(FunctionCallInfo fcinfo, tsqlAggType aggType);

/* Hook interface to calculate exact numeric digits before generating numeric overflow error in TSQL */
typedef bool (*detect_numeric_overflow_hook_type) (int weight, int dscale, int first_block, int numeric_base);
Expand Down

0 comments on commit 0e33a25

Please sign in to comment.