*
* int8.c
* Internal 64-bit integer operations
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
* Portions Copyright (c) 2021, openGauss Contributors
*
* IDENTIFICATION
* src/backend/utils/adt/int8.c
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "knl/knl_variable.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include "common/int.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "plugin_utils/int8.h"
#include "utils/builtins.h"
#include "plugin_commands/mysqlmode.h"
#ifdef DOLPHIN
#include "plugin_utils/varbit.h"
#include "plugin_utils/varlena.h"
#include "plugin_utils/int16.h"
#include "plugin_utils/date.h"
#include "plugin_utils/datetime.h"
#include "plugin_utils/timestamp.h"
#include "plugin_utils/year.h"
#include "utils/lsyscache.h"
PG_FUNCTION_INFO_V1_PUBLIC(bit_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(float4_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(float8_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(date_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(timestamp_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(timestamptz_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(time_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(timetz_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(set_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(uint8_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(year_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(bpchar_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(varchar_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(nvarchar2_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(text_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(varlena_cast_int8);
PG_FUNCTION_INFO_V1_PUBLIC(dolphin_int8xor);
extern "C" DLL_PUBLIC Datum bit_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum float4_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum float8_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum date_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum timestamp_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum timestamptz_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum time_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum timetz_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum set_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum uint8_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum year_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum bpchar_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum varchar_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum nvarchar2_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum text_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum varlena_cast_int8(PG_FUNCTION_ARGS);
extern "C" DLL_PUBLIC Datum dolphin_int8xor(PG_FUNCTION_ARGS);
#endif
#define MAXINT8LEN 25
typedef struct {
int64 current;
int64 finish;
int64 step;
} generate_series_fctx;
#ifdef DOLPHIN
void CheckSpaceAndDotInternal(char& digitAfterDot, const char** ptr, bool checkDecimal, int endChar)
{
if (checkDecimal && **ptr == '.') {
(*ptr)++;
while (**ptr != '\0' && **ptr != endChar && isdigit(**ptr)) {
if (digitAfterDot == '\0') {
digitAfterDot = **ptr;
}
(*ptr)++;
}
}
while (**ptr != '\0' && **ptr != endChar) {
if (!isspace(**ptr)) {
return;
}
(*ptr)++;
}
}
#endif
**
** Routines for 64-bit integers.
**
***********************************************************************/
* Formatting and conversion routines.
* --------------------------------------------------------- */
enum NumUtilsConvertResult {
RES_OK,
RES_OUT_OF_RANGE,
RES_NOT_A_NUMBER,
RES_UNEXPECTED_TRAILING_CHARS,
};
* Convert input string to a signed 64 bit integer.
* Allows any number of leading or trailing whitespace characters. Will return
* `NumUtilsConvertResult` upon bad input format or overflow.
*/
static inline NumUtilsConvertResult pg_strtoint64_internal(const char* s, int64* result)
{
Assert(result);
*result = 0;
const char* ptr = s;
uint64 tmp = 0;
bool neg = false;
while (isspace(static_cast<unsigned char>(*ptr))) {
ptr++;
}
if (*ptr == '-') {
ptr++;
neg = true;
} else if (*ptr == '+') {
ptr++;
}
if (unlikely(!isdigit(static_cast<unsigned char>(*ptr)))) {
return RES_NOT_A_NUMBER;
}
for (;;) {
uint8 digit = *ptr - '0';
if (digit >= 10) {
break;
}
ptr++;
if (unlikely(tmp > -(PG_INT64_MIN / 10))) {
*result = neg ? PG_INT64_MIN : PG_INT64_MAX;
return RES_OUT_OF_RANGE;
}
tmp = tmp * 10 + digit;
}
while (isspace(static_cast<unsigned char>(*ptr))) {
ptr++;
}
if (unlikely(*ptr != '\0')) {
return RES_UNEXPECTED_TRAILING_CHARS;
}
if (neg) {
if (unlikely(pg_neg_u64_overflow(tmp, result))) {
*result = PG_INT64_MIN;
return RES_OUT_OF_RANGE;
}
return RES_OK;
}
if (unlikely(tmp > PG_INT64_MAX)) {
*result = PG_INT64_MAX;
return RES_OUT_OF_RANGE;
}
*result = static_cast<int64>(tmp);
return RES_OK;
}
* Try to parse `str` into an `int8`.
*
* If errorOK is false, ereport a useful error message if the string is bad.
* If errorOK is true, just return "false" for bad input.
*
* Param can_ignore is true when using ignore hint, which will ignore errors of
* overflowing or invalid input.
*/
bool scanint8(const char* str, bool errorOK, int64* result, bool can_ignore)
{
NumUtilsConvertResult res = pg_strtoint64_internal(str, result);
if (res == RES_OK) {
return true;
}
if (errorOK) {
return false;
}
if (res == RES_OUT_OF_RANGE) {
if (!can_ignore) {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type %s", str, "bigint")));
}
ereport(WARNING, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type %s. truncated automatically", str, "bigint")));
} else if (res == RES_NOT_A_NUMBER) {
if (DB_IS_CMPT(B_FORMAT)) {
return true;
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "bigint", str)));
}
} else if (res == RES_UNEXPECTED_TRAILING_CHARS) {
if (!can_ignore) {
ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "bigint", str)));
}
}
return true;
}
Datum int8in(PG_FUNCTION_ARGS)
{
char* str = PG_GETARG_CSTRING(0);
int64 result;
#ifdef DOLPHIN
result = PgStrToIntInternal<false>(str, fcinfo->can_ignore || !SQL_MODE_STRICT(),
PG_INT64_MAX, PG_INT64_MIN, "bigint");
#else
if (DB_IS_CMPT(A_FORMAT) && ACCEPT_FLOAT_STR_AS_INT) {
result = PgStrToIntInternal<false>(str, fcinfo->can_ignore, PG_INT64_MAX, PG_INT64_MIN, "bigint");
} else {
(void)scanint8(str, false, &result, fcinfo->can_ignore);
}
#endif
PG_RETURN_INT64(result);
}
Datum int8out(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
char buf[MAXINT8LEN + 1];
char* result = NULL;
pg_lltoa(val, buf);
result = pstrdup(buf);
PG_RETURN_CSTRING(result);
}
* int8recv - converts external binary format to int8
*/
Datum int8recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
PG_RETURN_INT64(pq_getmsgint64(buf));
}
* int8send - converts int8 to binary format
*/
Datum int8send(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint64(&buf, arg1);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
* Relational operators for int8s, including cross-data-type comparisons.
* --------------------------------------------------------- */
* Is val1 relop val2?
*/
Datum int8eq(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 == val2);
}
Datum int8ne(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 != val2);
}
Datum int8lt(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 < val2);
}
Datum int8gt(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 > val2);
}
Datum int8le(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 <= val2);
}
Datum int8ge(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 >= val2);
}
* Is 64-bit val1 relop 32-bit val2?
*/
Datum int84eq(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 == val2);
}
Datum int84ne(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 != val2);
}
Datum int84lt(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 < val2);
}
Datum int84gt(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 > val2);
}
Datum int84le(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 <= val2);
}
Datum int84ge(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int32 val2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(val1 >= val2);
}
* Is 32-bit val1 relop 64-bit val2?
*/
Datum int48eq(PG_FUNCTION_ARGS)
{
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 == val2);
}
Datum int48ne(PG_FUNCTION_ARGS)
{
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 != val2);
}
Datum int48lt(PG_FUNCTION_ARGS)
{
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 < val2);
}
Datum int48gt(PG_FUNCTION_ARGS)
{
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 > val2);
}
Datum int48le(PG_FUNCTION_ARGS)
{
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 <= val2);
}
Datum int48ge(PG_FUNCTION_ARGS)
{
int32 val1 = PG_GETARG_INT32(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 >= val2);
}
* Is 64-bit val1 relop 16-bit val2?
*/
Datum int82eq(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int16 val2 = PG_GETARG_INT16(1);
PG_RETURN_BOOL(val1 == val2);
}
Datum int82ne(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int16 val2 = PG_GETARG_INT16(1);
PG_RETURN_BOOL(val1 != val2);
}
Datum int82lt(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int16 val2 = PG_GETARG_INT16(1);
PG_RETURN_BOOL(val1 < val2);
}
Datum int82gt(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int16 val2 = PG_GETARG_INT16(1);
PG_RETURN_BOOL(val1 > val2);
}
Datum int82le(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int16 val2 = PG_GETARG_INT16(1);
PG_RETURN_BOOL(val1 <= val2);
}
Datum int82ge(PG_FUNCTION_ARGS)
{
int64 val1 = PG_GETARG_INT64(0);
int16 val2 = PG_GETARG_INT16(1);
PG_RETURN_BOOL(val1 >= val2);
}
* Is 16-bit val1 relop 64-bit val2?
*/
Datum int28eq(PG_FUNCTION_ARGS)
{
int16 val1 = PG_GETARG_INT16(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 == val2);
}
Datum int28ne(PG_FUNCTION_ARGS)
{
int16 val1 = PG_GETARG_INT16(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 != val2);
}
Datum int28lt(PG_FUNCTION_ARGS)
{
int16 val1 = PG_GETARG_INT16(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 < val2);
}
Datum int28gt(PG_FUNCTION_ARGS)
{
int16 val1 = PG_GETARG_INT16(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 > val2);
}
Datum int28le(PG_FUNCTION_ARGS)
{
int16 val1 = PG_GETARG_INT16(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 <= val2);
}
Datum int28ge(PG_FUNCTION_ARGS)
{
int16 val1 = PG_GETARG_INT16(0);
int64 val2 = PG_GETARG_INT64(1);
PG_RETURN_BOOL(val1 >= val2);
}
* Arithmetic operators on 64-bit integers.
* --------------------------------------------------------- */
Datum int8um(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
int64 result;
if (unlikely(arg == PG_INT64_MIN))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
result = -arg;
PG_RETURN_INT64(result);
}
Datum int8up(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
PG_RETURN_INT64(arg);
}
Datum int8pl(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
if (unlikely(pg_add_s64_overflow(arg1, arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int8mi(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
if (unlikely(pg_sub_s64_overflow(arg1, arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int8mul(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int8div(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
float8 result;
if (arg2 == 0) {
#ifdef DOLPHIN
CheckErrDivByZero(fcinfo->can_ignore);
PG_RETURN_NULL();
#endif
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero")));
PG_RETURN_NULL();
}
if (arg1 == 0) {
PG_RETURN_FLOAT8(0);
}
* INT64_MIN / -1 is problematic, since the result can't be represented on
* a two's-complement machine. Some machines produce INT64_MIN, some
* produce zero, some throw an exception. We produce an exception like
* C db and D db do.
*/
if (arg2 == -1) {
int128 res = (int128)arg1 * (-1);
if (SUPPORT_BIND_DIVIDE && unlikely(res > PG_INT64_MAX))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_FLOAT8(res);
}
result = (arg1 * 1.0) / (arg2 * 1.0);
PG_RETURN_FLOAT8(result);
}
* Absolute value
*/
Datum int8abs(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 result;
if (unlikely(arg1 == PG_INT64_MIN))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
result = (arg1 < 0) ? -arg1 : arg1;
PG_RETURN_INT64(result);
}
* Modulo operation.
*/
Datum int8mod(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
if (unlikely(arg2 == 0)) {
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero")));
PG_RETURN_NULL();
}
* Some machines throw a floating-point exception for INT64_MIN % -1,
* which is a bit silly since the correct answer is perfectly
* well-defined, namely zero.
*/
if (arg2 == -1)
PG_RETURN_INT64(0);
PG_RETURN_INT64(arg1 % arg2);
}
Datum int8inc(PG_FUNCTION_ARGS)
{
* When int8 is pass-by-reference, we provide this special case to avoid
* palloc overhead for COUNT(): when called as an aggregate, we know that
* the argument is modifiable local storage, so just update it in-place.
* (If int8 is pass-by-value, then of course this is useless as well as
* incorrect, so just ifdef it out.)
*/
#ifndef USE_FLOAT8_BYVAL
if (AggCheckCallContext(fcinfo, NULL)) {
int64* arg = (int64*)PG_GETARG_POINTER(0);
if (unlikely(pg_add_s64_overflow(*arg, 1, arg)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_POINTER(arg);
} else
#endif
{
int64 arg = PG_GETARG_INT64(0);
int64 result;
if (unlikely(pg_add_s64_overflow(arg, 1, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
}
Datum int8dec(PG_FUNCTION_ARGS)
{
* When int8 is pass-by-reference, we provide this special case to avoid
* palloc overhead for COUNT(): when called as an aggregate, we know that
* the argument is modifiable local storage, so just update it in-place.
* (If int8 is pass-by-value, then of course this is useless as well as
* incorrect, so just ifdef it out.)
*/
#ifndef USE_FLOAT8_BYVAL
if (AggCheckCallContext(fcinfo, NULL)) {
int64* arg = (int64*)PG_GETARG_POINTER(0);
if (unlikely(pg_sub_s64_overflow(*arg, 1, arg)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_POINTER(arg);
} else
#endif
{
int64 arg = PG_GETARG_INT64(0);
int64 result;
if (unlikely(pg_sub_s64_overflow(arg, 1, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
}
* These functions are exactly like int8inc but are used for aggregates that
* count only non-null values. Since the functions are declared strict,
* the null checks happen before we ever get here, and all we need do is
* increment the state value. We could actually make these pg_proc entries
* point right at int8inc, but then the opr_sanity regression test would
* complain about mismatched entries for a built-in function.
*/
Datum int8inc_any(PG_FUNCTION_ARGS)
{
return int8inc(fcinfo);
}
Datum int8inc_float8_float8(PG_FUNCTION_ARGS)
{
return int8inc(fcinfo);
}
Datum int8larger(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = ((arg1 > arg2) ? arg1 : arg2);
PG_RETURN_INT64(result);
}
Datum int8smaller(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = ((arg1 < arg2) ? arg1 : arg2);
PG_RETURN_INT64(result);
}
Datum int84pl(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int32 arg2 = PG_GETARG_INT32(1);
int64 result;
if (unlikely(pg_add_s64_overflow(arg1, (int64)arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int84mi(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int32 arg2 = PG_GETARG_INT32(1);
int64 result;
if (unlikely(pg_sub_s64_overflow(arg1, (int64)arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int84mul(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int32 arg2 = PG_GETARG_INT32(1);
int64 result;
if (unlikely(pg_mul_s64_overflow(arg1, (int64)arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int84div(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int32 arg2 = PG_GETARG_INT32(1);
float8 result;
if (arg2 == 0) {
#ifdef DOLPHIN
CheckErrDivByZero(fcinfo->can_ignore);
PG_RETURN_NULL();
#endif
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero")));
PG_RETURN_NULL();
}
if (arg1 == 0) {
PG_RETURN_FLOAT8(0);
}
* INT64_MIN / -1 is problematic, since the result can't be represented on
* a two's-complement machine. Some machines produce INT64_MIN, some
* produce zero, some throw an exception. We produce an exception like
* C db and D db do.
*/
if (arg2 == -1) {
int128 res = (int128)arg1 * (-1);
if (SUPPORT_BIND_DIVIDE && unlikely(res > PG_INT64_MAX))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_FLOAT8(res);
}
result = (arg1 * 1.0) / (arg2 * 1.0);
PG_RETURN_FLOAT8(result);
}
Datum int48pl(PG_FUNCTION_ARGS)
{
int32 arg1 = PG_GETARG_INT32(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
if (unlikely(pg_add_s64_overflow((int64)arg1, arg2, &result)))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int48mi(PG_FUNCTION_ARGS)
{
int32 arg1 = PG_GETARG_INT32(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = arg1 - arg2;
* Overflow check. If the inputs are of the same sign then their
* difference cannot overflow. If they are of different signs then the
* result should be of the same sign as the first input.
*/
if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int48mul(PG_FUNCTION_ARGS)
{
int32 arg1 = PG_GETARG_INT32(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = arg1 * arg2;
* Overflow check. We basically check to see if result / arg2 gives arg1
* again. There is one case where this fails: arg2 = 0 (which cannot
* overflow).
*
* Since the division is likely much more expensive than the actual
* multiplication, we'd like to skip it where possible. The best bang for
* the buck seems to be to check whether both inputs are in the int32
* range; if so, no overflow is possible.
*/
if (arg2 != (int64)((int32)arg2) && result / arg2 != arg1)
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int48div(PG_FUNCTION_ARGS)
{
int32 arg1 = PG_GETARG_INT32(0);
int64 arg2 = PG_GETARG_INT64(1);
if (arg2 == 0) {
#ifdef DOLPHIN
CheckErrDivByZero(fcinfo->can_ignore);
PG_RETURN_NULL();
#endif
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero")));
PG_RETURN_NULL();
}
if (arg1 == 0) {
PG_RETURN_FLOAT8(0);
}
PG_RETURN_FLOAT8((arg1 * 1.0) / (arg2 * 1.0));
}
Datum int82pl(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int16 arg2 = PG_GETARG_INT16(1);
int64 result;
result = arg1 + arg2;
* Overflow check. If the inputs are of different signs then their sum
* cannot overflow. If the inputs are of the same sign, their sum had
* better be that sign too.
*/
if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int82mi(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int16 arg2 = PG_GETARG_INT16(1);
int64 result;
result = arg1 - arg2;
* Overflow check. If the inputs are of the same sign then their
* difference cannot overflow. If they are of different signs then the
* result should be of the same sign as the first input.
*/
if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int82mul(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int16 arg2 = PG_GETARG_INT16(1);
int64 result;
result = arg1 * arg2;
* Overflow check. We basically check to see if result / arg1 gives arg2
* again. There is one case where this fails: arg1 = 0 (which cannot
* overflow).
*
* Since the division is likely much more expensive than the actual
* multiplication, we'd like to skip it where possible. The best bang for
* the buck seems to be to check whether both inputs are in the int32
* range; if so, no overflow is possible.
*/
if (arg1 != (int64)((int32)arg1) && result / arg1 != arg2)
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int82div(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int16 arg2 = PG_GETARG_INT16(1);
float8 result;
if (arg2 == 0) {
#ifdef DOLPHIN
CheckErrDivByZero(fcinfo->can_ignore);
PG_RETURN_NULL();
#endif
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero")));
PG_RETURN_NULL();
}
if (arg1 == 0) {
PG_RETURN_FLOAT8(0);
}
* INT64_MIN / -1 is problematic, since the result can't be represented on
* a two's-complement machine. Some machines produce INT64_MIN, some
* produce zero, some throw an exception. We produce an exception like
* C db and D db do.
*/
if (arg2 == -1) {
int128 res = (int128)arg1 * (-1);
if (SUPPORT_BIND_DIVIDE && unlikely(res > PG_INT64_MAX))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_FLOAT8(res);
}
result = (arg1 * 1.0) / (arg2 * 1.0);
PG_RETURN_FLOAT8(result);
}
Datum int28pl(PG_FUNCTION_ARGS)
{
int16 arg1 = PG_GETARG_INT16(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = arg1 + arg2;
* Overflow check. If the inputs are of different signs then their sum
* cannot overflow. If the inputs are of the same sign, their sum had
* better be that sign too.
*/
if (SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int28mi(PG_FUNCTION_ARGS)
{
int16 arg1 = PG_GETARG_INT16(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = arg1 - arg2;
* Overflow check. If the inputs are of the same sign then their
* difference cannot overflow. If they are of different signs then the
* result should be of the same sign as the first input.
*/
if (!SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int28mul(PG_FUNCTION_ARGS)
{
int16 arg1 = PG_GETARG_INT16(0);
int64 arg2 = PG_GETARG_INT64(1);
int64 result;
result = arg1 * arg2;
* Overflow check. We basically check to see if result / arg2 gives arg1
* again. There is one case where this fails: arg2 = 0 (which cannot
* overflow).
*
* Since the division is likely much more expensive than the actual
* multiplication, we'd like to skip it where possible. The best bang for
* the buck seems to be to check whether both inputs are in the int32
* range; if so, no overflow is possible.
*/
if (arg2 != (int64)((int32)arg2) && result / arg2 != arg1)
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
PG_RETURN_INT64(result);
}
Datum int28div(PG_FUNCTION_ARGS)
{
int16 arg1 = PG_GETARG_INT16(0);
int64 arg2 = PG_GETARG_INT64(1);
if (arg2 == 0) {
#ifdef DOLPHIN
CheckErrDivByZero(fcinfo->can_ignore);
PG_RETURN_NULL();
#endif
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero")));
PG_RETURN_NULL();
}
if (arg1 == 0) {
PG_RETURN_FLOAT8(0);
}
PG_RETURN_FLOAT8((arg1 * 1.0) / (arg2 * 1.0));
}
*
* int8and - returns arg1 & arg2
* int8or - returns arg1 | arg2
* int8xor - returns arg1 # arg2
* int8not - returns ~arg1
* int8shl - returns arg1 << arg2
* int8shr - returns arg1 >> arg2
*/
Datum int8and(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
PG_RETURN_INT64(arg1 & arg2);
}
Datum int8or(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
PG_RETURN_INT64(arg1 | arg2);
}
Datum int8xor(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
PG_RETURN_INT64(arg1 ^ arg2);
}
Datum int8not(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
PG_RETURN_INT64(~arg1);
}
Datum int8shl(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int32 arg2 = PG_GETARG_INT32(1);
PG_RETURN_INT64(arg1 << arg2);
}
Datum int8shr(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int32 arg2 = PG_GETARG_INT32(1);
PG_RETURN_INT64(arg1 >> arg2);
}
* Conversion operators.
* --------------------------------------------------------- */
Datum int8_bool(PG_FUNCTION_ARGS)
{
if (PG_GETARG_INT64(0) == 0)
PG_RETURN_BOOL(false);
else
PG_RETURN_BOOL(true);
}
Datum bool_int8(PG_FUNCTION_ARGS)
{
if (PG_GETARG_BOOL(0) == false)
PG_RETURN_INT64(0);
else
PG_RETURN_INT64(1);
}
Datum int48(PG_FUNCTION_ARGS)
{
int32 arg = PG_GETARG_INT32(0);
PG_RETURN_INT64((int64)arg);
}
Datum int84(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
#ifdef DOLPHIN
PG_RETURN_INT32(int84_internal(arg, fcinfo->can_ignore));
#endif
}
Datum int28(PG_FUNCTION_ARGS)
{
int16 arg = PG_GETARG_INT16(0);
PG_RETURN_INT64((int64)arg);
}
Datum int82(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
int16 result;
result = (int16)arg;
if ((int64)result != arg) {
if (fcinfo->can_ignore || !SQL_MODE_STRICT()) {
ereport(WARNING, (errmsg("smallint out of range")));
if (arg < 0) {
result = INT16_MIN;
} else {
result = INT16_MAX;
}
} else {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range")));
}
}
PG_RETURN_INT16(result);
}
Datum i8tod(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
float8 result;
result = arg;
PG_RETURN_FLOAT8(result);
}
* Convert float8 to 8-byte integer.
*/
Datum dtoi8(PG_FUNCTION_ARGS)
{
float8 num = PG_GETARG_FLOAT8(0);
* Get rid of any fractional part in the input. This is so we don't fail
* on just-out-of-range values that would round into range. Note
* assumption that round() will pass through a NaN or Inf unchanged.
*/
num = round(num);
* Range check. We must be careful here that the boundary values are
* expressed exactly in the float domain. We expect PG_INT64_MIN to be an
* exact power of 2, so it will be represented exactly; but PG_INT64_MAX
* isn't, and might get rounded off, so avoid using it.
*/
if (isnan(num))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
if (num < (float8)PG_INT64_MIN || num >= -((float8)PG_INT64_MIN)) {
if (fcinfo->can_ignore || !SQL_MODE_STRICT()) {
ereport(WARNING, (errmsg("bigint out of range")));
PG_RETURN_INT64(num < (float8)PG_INT64_MIN ? LONG_MIN : LONG_MAX);
} else {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
}
}
PG_RETURN_INT64((int64)num);
}
Datum i8tof(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
float4 result;
result = arg;
PG_RETURN_FLOAT4(result);
}
* Convert float4 to 8-byte integer.
*/
Datum ftoi8(PG_FUNCTION_ARGS)
{
float4 num = PG_GETARG_FLOAT4(0);
* Get rid of any fractional part in the input. This is so we don't fail
* on just-out-of-range values that would round into range. Note
* assumption that round() will pass through a NaN or Inf unchanged.
*/
num = round(num);
* Range check. We must be careful here that the boundary values are
* expressed exactly in the float domain. We expect PG_INT64_MIN to be an
* exact power of 2, so it will be represented exactly; but PG_INT64_MAX
* isn't, and might get rounded off, so avoid using it.
*/
if (isnan(num))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
if (num < (float4)PG_INT64_MIN || num >= -((float4)PG_INT64_MIN)) {
if (fcinfo->can_ignore || !SQL_MODE_STRICT()) {
ereport(WARNING, (errmsg("bigint out of range")));
PG_RETURN_INT64(num < (float4)PG_INT64_MIN ? LONG_MIN : LONG_MAX);
} else {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
}
}
PG_RETURN_INT64((int64)num);
}
Datum i8tooid(PG_FUNCTION_ARGS)
{
int64 arg = PG_GETARG_INT64(0);
Oid result;
result = (Oid)arg;
if ((int64)result != arg)
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("OID out of range")));
PG_RETURN_OID(result);
}
Datum oidtoi8(PG_FUNCTION_ARGS)
{
Oid arg = PG_GETARG_OID(0);
PG_RETURN_INT64((int64)arg);
}
* non-persistent numeric series generator
*/
Datum generate_series_int8(PG_FUNCTION_ARGS)
{
return generate_series_step_int8(fcinfo);
}
Datum generate_series_step_int8(PG_FUNCTION_ARGS)
{
FuncCallContext* funcctx = NULL;
generate_series_fctx* fctx = NULL;
int64 result;
MemoryContext oldcontext;
if (SRF_IS_FIRSTCALL()) {
int64 start = PG_GETARG_INT64(0);
int64 finish = PG_GETARG_INT64(1);
int64 step = 1;
if (PG_NARGS() == 3)
step = PG_GETARG_INT64(2);
if (step == 0)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("step size cannot equal zero")));
funcctx = SRF_FIRSTCALL_INIT();
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
fctx = (generate_series_fctx*)palloc(sizeof(generate_series_fctx));
* Use fctx to keep state from call to call. Seed current with the
* original start value
*/
fctx->current = start;
fctx->finish = finish;
fctx->step = step;
funcctx->user_fctx = fctx;
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
* get the saved state and use current as the result for this iteration
*/
fctx = (generate_series_fctx*)funcctx->user_fctx;
result = fctx->current;
if ((fctx->step > 0 && fctx->current <= fctx->finish) || (fctx->step < 0 && fctx->current >= fctx->finish)) {
fctx->current += fctx->step;
if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
fctx->step = 0;
SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
}
SRF_RETURN_DONE(funcctx);
}
ScalarVector* vint4mul(PG_FUNCTION_ARGS)
{
ScalarValue* parg1 = PG_GETARG_VECVAL(0);
ScalarValue* parg2 = PG_GETARG_VECVAL(1);
int32 nvalues = PG_GETARG_INT32(2);
ScalarValue* presult = PG_GETARG_VECVAL(3);
bool* pselection = PG_GETARG_SELECTION(4);
uint8* pflags1 = (uint8*)(PG_GETARG_VECTOR(0)->m_flag);
uint8* pflags2 = (uint8*)(PG_GETARG_VECTOR(1)->m_flag);
uint8* pflagsRes = PG_GETARG_VECTOR(3)->m_flag;
uint32 mask = 0;
int i;
int32 arg1, arg2, result;
if (likely(pselection == NULL)) {
for (i = 0; i < nvalues; i++) {
if (BOTH_NOT_NULL(pflags1[i], pflags2[i])) {
arg1 = (int32)parg1[i];
arg2 = (int32)parg2[i];
result = arg1 * arg2;
mask |= (!(arg1 >= (int32)SHRT_MIN && arg1 <= (int32)SHRT_MAX && arg2 >= (int32)SHRT_MIN &&
arg2 <= (int32)SHRT_MAX) &&
arg2 != 0 && ((result / arg2 != arg1) || (arg2 == -1 && arg1 < 0 && result < 0)));
presult[i] = Int32GetDatum(result);
SET_NOTNULL(pflagsRes[i]);
} else
SET_NULL(pflagsRes[i]);
}
} else {
for (i = 0; i < nvalues; i++) {
if (pselection[i]) {
if (BOTH_NOT_NULL(pflags1[i], pflags2[i])) {
arg1 = (int32)parg1[i];
arg2 = (int32)parg2[i];
result = arg1 * arg2;
mask |= (!(arg1 >= (int32)SHRT_MIN && arg1 <= (int32)SHRT_MAX && arg2 >= (int32)SHRT_MIN &&
arg2 <= (int32)SHRT_MAX) &&
arg2 != 0 && ((result / arg2 != arg1) || (arg2 == -1 && arg1 < 0 && result < 0)));
presult[i] = Int32GetDatum(result);
SET_NOTNULL(pflagsRes[i]);
} else
SET_NULL(pflagsRes[i]);
}
}
}
if (mask != 0)
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range")));
PG_GETARG_VECTOR(3)->m_rows = nvalues;
PG_GETARG_VECTOR(3)->m_desc.typeId = INT4OID;
return PG_GETARG_VECTOR(3);
}
ScalarVector* vint4mi(PG_FUNCTION_ARGS)
{
ScalarValue* parg1 = PG_GETARG_VECVAL(0);
ScalarValue* parg2 = PG_GETARG_VECVAL(1);
int32 nvalues = PG_GETARG_INT32(2);
ScalarValue* presult = PG_GETARG_VECVAL(3);
bool* pselection = PG_GETARG_SELECTION(4);
uint8* pflags1 = (uint8*)(PG_GETARG_VECTOR(0)->m_flag);
uint8* pflags2 = (uint8*)(PG_GETARG_VECTOR(1)->m_flag);
uint8* pflagsRes = (uint8*)(PG_GETARG_VECTOR(3)->m_flag);
uint32 mask = 0;
int i;
int32 arg1, arg2, result;
if (likely(pselection == NULL)) {
for (i = 0; i < nvalues; i++) {
if (BOTH_NOT_NULL(pflags1[i], pflags2[i])) {
arg1 = (int32)parg1[i];
arg2 = (int32)parg2[i];
result = arg1 - arg2;
mask |= !SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1);
presult[i] = Int32GetDatum(result);
SET_NOTNULL(pflagsRes[i]);
} else
SET_NULL(pflagsRes[i]);
}
} else {
for (i = 0; i < nvalues; i++) {
if (pselection[i]) {
if (BOTH_NOT_NULL(pflags1[i], pflags2[i])) {
arg1 = (int32)parg1[i];
arg2 = (int32)parg2[i];
result = arg1 - arg2;
mask |= !SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1);
presult[i] = Int32GetDatum(result);
SET_NOTNULL(pflagsRes[i]);
} else
SET_NULL(pflagsRes[i]);
}
}
}
if (mask != 0) {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range")));
}
PG_GETARG_VECTOR(3)->m_rows = nvalues;
PG_GETARG_VECTOR(3)->m_desc.typeId = INT4OID;
return PG_GETARG_VECTOR(3);
}
ScalarVector* vint4pl(PG_FUNCTION_ARGS)
{
ScalarValue* parg1 = PG_GETARG_VECVAL(0);
ScalarValue* parg2 = PG_GETARG_VECVAL(1);
int32 nvalues = PG_GETARG_INT32(2);
ScalarValue* presult = PG_GETARG_VECVAL(3);
bool* pselection = PG_GETARG_SELECTION(4);
uint8* pflags1 = (uint8*)(PG_GETARG_VECTOR(0)->m_flag);
uint8* pflags2 = (uint8*)(PG_GETARG_VECTOR(1)->m_flag);
uint8* pflagsRes = (uint8*)(PG_GETARG_VECTOR(3)->m_flag);
uint32 mask = 0;
int i;
int32 arg1, arg2, result;
if (likely(pselection == NULL)) {
for (i = 0; i < nvalues; i++) {
if (BOTH_NOT_NULL(pflags1[i], pflags2[i])) {
arg1 = (int32)parg1[i];
arg2 = (int32)parg2[i];
result = arg1 + arg2;
mask |= SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1);
presult[i] = Int32GetDatum(result);
SET_NOTNULL(pflagsRes[i]);
} else
SET_NULL(pflagsRes[i]);
}
} else {
for (i = 0; i < nvalues; i++) {
if (pselection[i]) {
if (BOTH_NOT_NULL(pflags1[i], pflags2[i])) {
arg1 = (int32)parg1[i];
arg2 = (int32)parg2[i];
result = arg1 + arg2;
mask |= SAMESIGN(arg1, arg2) && !SAMESIGN(result, arg1);
presult[i] = Int32GetDatum(result);
SET_NOTNULL(pflagsRes[i]);
} else
SET_NULL(pflagsRes[i]);
}
}
}
if (mask != 0) {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range")));
}
PG_GETARG_VECTOR(3)->m_rows = nvalues;
PG_GETARG_VECTOR(3)->m_desc.typeId = INT4OID;
return PG_GETARG_VECTOR(3);
}
Datum int8_text(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
char* tmp = NULL;
Datum result;
tmp = DatumGetCString(DirectFunctionCall1(int8out, arg1));
result = DirectFunctionCall1(textin, CStringGetDatum(tmp));
pfree_ext(tmp);
PG_RETURN_DATUM(result);
}
Datum varchar_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
Datum result;
tmp = DatumGetCString(DirectFunctionCall1(varcharout, txt));
#ifdef DOLPHIN
result = DirectFunctionCall1Coll(int8in, InvalidOid, CStringGetDatum(tmp), fcinfo->can_ignore);
#else
result = DirectFunctionCall1(int8in, CStringGetDatum(tmp));
#endif
pfree_ext(tmp);
PG_RETURN_DATUM(result);
}
Datum int8_varchar(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
char* tmp = NULL;
Datum result;
tmp = DatumGetCString(DirectFunctionCall1(int8out, val));
result = DirectFunctionCall3(varcharin, CStringGetDatum(tmp), ObjectIdGetDatum(0), Int32GetDatum(-1));
pfree_ext(tmp);
PG_RETURN_DATUM(result);
}
* @Description: int8 convert to bpchar.
* @in arg1 - bigint type numeric.
* @return bpchar type string.
*/
Datum int8_bpchar(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
char* tmp = NULL;
Datum result;
tmp = DatumGetCString(DirectFunctionCall1(int8out, arg1));
result = DirectFunctionCall3(bpcharin, CStringGetDatum(tmp), ObjectIdGetDatum(0), Int32GetDatum(-1));
pfree_ext(tmp);
PG_RETURN_DATUM(result);
}
Datum text_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
Datum result;
tmp = DatumGetCString(DirectFunctionCall1(textout, txt));
#ifdef DOLPHIN
result = DirectFunctionCall1Coll(int8in, InvalidOid, CStringGetDatum(tmp), fcinfo->can_ignore);
#else
result = DirectFunctionCall1(int8in, CStringGetDatum(tmp));
#endif
pfree_ext(tmp);
PG_RETURN_DATUM(result);
}
Datum bpchar_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
Datum result;
tmp = DatumGetCString(DirectFunctionCall1(bpcharout, txt));
#ifdef DOLPHIN
result = DirectFunctionCall1Coll(int8in, InvalidOid, CStringGetDatum(tmp), fcinfo->can_ignore);
#else
result = DirectFunctionCall1(int8in, CStringGetDatum(tmp));
#endif
pfree_ext(tmp);
PG_RETURN_DATUM(result);
}
#ifdef DOLPHIN
Datum ftoi8_floor(PG_FUNCTION_ARGS)
{
float4 num = PG_GETARG_FLOAT4(0);
num = floor(num);
if (isnan(num))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
if (num < (float4)PG_INT64_MIN || num >= -((float4)PG_INT64_MIN)) {
if (fcinfo->can_ignore || !SQL_MODE_STRICT()) {
ereport(WARNING, (errmsg("bigint out of range")));
PG_RETURN_INT64(num < (float4)PG_INT64_MIN ? LONG_MIN : LONG_MAX);
} else {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
}
}
PG_RETURN_INT64((int64)num);
}
Datum dtoi8_floor(PG_FUNCTION_ARGS)
{
float8 num = PG_GETARG_FLOAT8(0);
num = floor(num);
if (isnan(num))
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
if (num < (float8)PG_INT64_MIN || num >= -((float8)PG_INT64_MIN)) {
if (fcinfo->can_ignore || !SQL_MODE_STRICT()) {
ereport(WARNING, (errmsg("bigint out of range")));
PG_RETURN_INT64(num < (float8)PG_INT64_MIN ? LONG_MIN : LONG_MAX);
} else {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
}
}
PG_RETURN_INT64((int64)num);
}
Datum checkSignedRange(int128 result, PG_FUNCTION_ARGS)
{
if (result > (int128)INT64_MAX || result < (int128)INT64_MIN) {
if (result > (int128)UINT64_MAX) {
result = -1;
} else {
result = result > 0 ? (int64)result : INT64_MIN;
}
ereport((fcinfo->can_ignore || !SQL_MODE_STRICT()) ? WARNING : ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
}
PG_RETURN_INT64((int64)result);
}
Datum bit_cast_int8(PG_FUNCTION_ARGS)
{
VarBit* arg = PG_GETARG_VARBIT_P(0);
uint128 result;
bits8* r = NULL;
if ((uint32)VARBITLEN(arg) - GetLeadingZeroLen(arg) > sizeof(uint64) * BITS_PER_BYTE) {
ereport(SQL_MODE_STRICT() ? ERROR : WARNING,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
}
result = 0;
for (r = VARBITS(arg); r < VARBITEND(arg); r++) {
result <<= BITS_PER_BYTE;
result |= *r;
}
result >>= VARBITPAD(arg);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum float4_cast_int8(PG_FUNCTION_ARGS)
{
float4 result = PG_GETARG_FLOAT4(0);
if (isnan(result)) {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
}
if (result >= (float4)((int128)INT64_MAX) || result <= (float4)INT64_MIN) {
result = result > 0 ? INT64_MAX : INT64_MIN;
ereport((fcinfo->can_ignore || !SQL_MODE_STRICT()) ? WARNING : ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
}
PG_RETURN_INT64((int128)round(result));
}
Datum float8_cast_int8(PG_FUNCTION_ARGS)
{
float8 result = PG_GETARG_FLOAT8(0);
if (isnan(result)) {
ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range")));
}
if (result >= (float8)((int128)INT64_MAX) || result <= (float8)INT64_MIN) {
ereport((fcinfo->can_ignore || !SQL_MODE_STRICT()) ? WARNING : ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
PG_RETURN_INT64((result > 0 ? INT64_MAX : INT64_MIN));
}
PG_RETURN_INT64((int128)round(result));
}
Datum date_cast_int8(PG_FUNCTION_ARGS)
{
DateADT date = PG_GETARG_DATEADT(0);
struct pg_tm tt;
struct pg_tm* tm = &tt;
if (unlikely(date > 0 && (INT_MAX - date < POSTGRES_EPOCH_JDATE))) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("input julian date is overflow")));
}
j2date(date + POSTGRES_EPOCH_JDATE, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
int128 result = date2int(tm);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum timestamp_cast_int8(PG_FUNCTION_ARGS)
{
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
int128 result = timestamp_int128(timestamp);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum timestamptz_cast_int8(PG_FUNCTION_ARGS)
{
TimestampTz timestampTz = PG_GETARG_TIMESTAMPTZ(0);
int128 result = timestamptz_int128(timestampTz);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum time_cast_int8(PG_FUNCTION_ARGS)
{
TimeADT time = PG_GETARG_TIMEADT(0);
struct pg_tm tt;
struct pg_tm* tm = &tt;
fsec_t fsec;
time2tm(time, tm, &fsec);
int128 result = tmfsec2uint(tm);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum timetz_cast_int8(PG_FUNCTION_ARGS)
{
TimeTzADT* time = PG_GETARG_TIMETZADT_P(0);
struct pg_tm tt;
struct pg_tm* tm = &tt;
fsec_t fsec;
int tz;
timetz2tm(time, tm, &fsec, &tz);
int128 result = tmfsec2uint(tm);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum set_cast_int8(PG_FUNCTION_ARGS)
{
VarBit *bitmap = PG_GETARG_VARBIT_P(0);
int128 result = 0;
int typmod = VARBITLEN(bitmap);
bits8 *base = (bits8*)VARBITS(bitmap) + sizeof(Oid);
int1 bitlen = typmod - sizeof(Oid) * BITS_PER_BYTE;
for (int1 order = 0; order < bitlen; order++) {
bits8 *r = base + order / BITS_PER_BYTE;
bool bitset = (*r) & (1 << (order % BITS_PER_BYTE));
if (bitset) {
result |= (1UL << order);
}
}
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum uint8_cast_int8(PG_FUNCTION_ARGS)
{
uint64 result = PG_GETARG_UINT64(0);
PG_RETURN_INT64((int64)result);
}
Datum year_cast_int8(PG_FUNCTION_ARGS) {
YearADT year = PG_GETARG_YEARADT(0);
if (IS_YEAR2(year))
YEAR_CONVERT(year);
year = YearADT_to_Year(year);
PG_RETURN_INT64(year);
}
Datum bpchar_cast_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
int128 result;
tmp = DatumGetCString(DirectFunctionCall1(bpcharout, txt));
result = DatumGetInt128(DirectFunctionCall1Coll(int16in, InvalidOid, CStringGetDatum(tmp), fcinfo->can_ignore));
pfree_ext(tmp);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum varchar_cast_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
int128 result;
tmp = DatumGetCString(DirectFunctionCall1(varcharout, txt));
result = DatumGetInt128(DirectFunctionCall1Coll(int16in, InvalidOid, CStringGetDatum(tmp), fcinfo->can_ignore));
pfree_ext(tmp);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum nvarchar2_cast_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
int128 result;
tmp = DatumGetCString(DirectFunctionCall1(nvarchar2out, txt));
result = DatumGetInt128(DirectFunctionCall1(int16in, CStringGetDatum(tmp)));
pfree_ext(tmp);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum text_cast_int8(PG_FUNCTION_ARGS)
{
Datum txt = PG_GETARG_DATUM(0);
char* tmp = NULL;
int128 result;
Oid typeOutput;
bool typIsVarlena;
getTypeOutputInfo(fcinfo->argTypes[0], &typeOutput, &typIsVarlena);
tmp = DatumGetCString(OidOutputFunctionCall(typeOutput, txt));
result = DatumGetInt128(DirectFunctionCall1Coll(int16in, InvalidOid, CStringGetDatum(tmp), fcinfo->can_ignore));
pfree_ext(tmp);
PG_RETURN_INT64(checkSignedRange(result, fcinfo));
}
Datum varlena_cast_int8(PG_FUNCTION_ARGS)
{
char* tmp = NULL;
int128 result;
bool hasLenError = false;
tmp = AnyElementGetCString(fcinfo->argTypes[0], PG_GETARG_DATUM(0), &hasLenError);
bool hasError = false;
(void)scanint16(tmp, fcinfo->can_ignore || !SQL_MODE_STRICT(), &result, &hasError);
if (result > (int128)INT64_MAX || result < (int128)INT64_MIN) {
if (result > (int128)UINT64_MAX) {
result = -1;
} else {
result = result > 0 ? (int64)result : INT64_MIN;
}
ereport((fcinfo->can_ignore || !SQL_MODE_STRICT()) ? WARNING : ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
} else if (!hasError && hasLenError) {
ereport((!fcinfo->can_ignore && SQL_MODE_STRICT()) ? ERROR : WARNING,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("Data truncated for input data: \"%s\"", tmp)));
}
PG_RETURN_INT64((int64)result);
}
Datum dolphin_int8xor(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 arg2 = PG_GETARG_INT64(1);
PG_RETURN_INT64 ((arg1 != 0) ^ (arg2 != 0));
}
#endif