getFuncNameSuffix(); $funcName = "{$left->pgName}{$suffix}"; $fn = "PG_FUNCTION_INFO_V1($funcName);\n"; $fn .= "Datum $funcName(PG_FUNCTION_ARGS) {\n"; $fn .= " char buf[{$left->getStrBufLenConstC()}];\n"; $fn .= " {$left->name} num = $left->pgGetArgMacro(1);\n"; $fn .= "\n"; $fn .= " Numeric numParsed = {$left->name}_to_numeric(num, buf, sizeof(buf));"; $fn .= "\n"; $fn .= " /* Initialize the FunctionCallInfoData for numeric_avg_accum */\n"; $fn .= " fcinfo->args[1].value = NumericGetDatum(numParsed);\n"; $fn .= "\n"; $fn .= " return numeric_avg_accum(fcinfo);\n"; $fn .= "}\n"; $funcName = "{$left->pgName}{$suffix}_inv"; $fn .= "\n"; $fn .= "PG_FUNCTION_INFO_V1($funcName);\n"; $fn .= "Datum $funcName(PG_FUNCTION_ARGS) {\n"; $fn .= " char buf[{$left->getStrBufLenConstC()}];\n"; $fn .= " {$left->name} num = $left->pgGetArgMacro(1);\n"; $fn .= "\n"; $fn .= " Numeric numParsed = {$left->name}_to_numeric(num, buf, sizeof(buf));"; $fn .= "\n"; $fn .= " /* Initialize the FunctionCallInfoData for numeric_accum_inv */\n"; $fn .= " fcinfo->args[1].value = NumericGetDatum(numParsed);\n"; $fn .= "\n"; $fn .= " return numeric_accum_inv(fcinfo);\n"; $fn .= "}\n"; return $fn; } function genMinMaxAggFunc(Type $left, AggOp $op): string { $_ = match ($op) { AggOp::Min, AggOp::Max => 0, default => new InvalidArgumentException("Only min/max ops are supported") }; $funcName = "{$left->pgName}{$op->getFuncNameSuffix()}"; $getArgMacro = $left->pgGetArgMacro; $returnMacro = $left->pgReturnMacro; $leftType = $left->name; $deref = ''; // Optimize operations by directly returning pointer to arg avoiding memory allocation if ($left->bitSize >= 128 && $left->pgGetArgPtrMacro && $left->pgReturnPtrMacro) { $leftType = "{$leftType}*"; $deref = "*"; $getArgMacro = $left->pgGetArgPtrMacro; $returnMacro = $left->pgReturnPtrMacro; } $sign = match($op) { AggOp::Min => "<", AggOp::Max => ">", }; return <<