* int.c
* Functions for the built-in integer types (except int8).
*
- * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.79 2007/02/27 23:48:08 tgl Exp $
+ * src/backend/utils/adt/int.c
*
*-------------------------------------------------------------------------
*/
* int2in, int2out, int2recv, int2send
* int4in, int4out, int4recv, int4send
* int2vectorin, int2vectorout, int2vectorrecv, int2vectorsend
- * Conversion routines:
- * itoi, int2_text, int4_text
* Boolean operators:
* inteq, intne, intlt, intle, intgt, intge
* Arithmetic operators:
#define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
-#define Int2VectorSize(n) (offsetof(int2vector, values) + (n) * sizeof(int2))
+#define Int2VectorSize(n) (offsetof(int2vector, values) + (n) * sizeof(int16))
typedef struct
{
* If int2s is NULL then caller must fill values[] afterward
*/
int2vector *
-buildint2vector(const int2 *int2s, int n)
+buildint2vector(const int16 *int2s, int n)
{
int2vector *result;
result = (int2vector *) palloc0(Int2VectorSize(n));
if (n > 0 && int2s)
- memcpy(result->values, int2s, n * sizeof(int2));
+ memcpy(result->values, int2s, n * sizeof(int16));
/*
* Attach standard array header. For historical reasons, we set the index
for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
{
- if (sscanf(intString, "%hd", &result->values[n]) != 1)
- break;
while (*intString && isspace((unsigned char) *intString))
intString++;
+ if (*intString == '\0')
+ break;
+ result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
while (*intString && !isspace((unsigned char) *intString))
intString++;
}
* fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
* parameter.
*/
- InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 3, NULL, NULL);
+ InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 3,
+ InvalidOid, NULL, NULL);
locfcinfo.arg[0] = PointerGetDatum(buf);
locfcinfo.arg[1] = ObjectIdGetDatum(INT2OID);
Assert(!locfcinfo.isnull);
- /* sanity checks: int2vector must be 1-D, no nulls */
+ /* sanity checks: int2vector must be 1-D, 0-based, no nulls */
if (ARR_NDIM(result) != 1 ||
ARR_HASNULL(result) ||
- ARR_ELEMTYPE(result) != INT2OID)
+ ARR_ELEMTYPE(result) != INT2OID ||
+ ARR_LBOUND(result)[0] != 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("invalid int2vector data")));
+
+ /* check length for consistency with int2vectorin() */
+ if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("oidvector has too many elements")));
+
PG_RETURN_POINTER(result);
}
if (a->dim1 != b->dim1)
PG_RETURN_BOOL(false);
- PG_RETURN_BOOL(memcmp(a->values, b->values, a->dim1 * sizeof(int2)) == 0);
+ PG_RETURN_BOOL(memcmp(a->values, b->values, a->dim1 * sizeof(int16)) == 0);
}
PG_RETURN_INT16((int16) arg1);
}
-Datum
-int2_text(PG_FUNCTION_ARGS)
-{
- int16 arg1 = PG_GETARG_INT16(0);
- text *result = (text *) palloc(7 + VARHDRSZ); /* sign,5 digits, '\0' */
-
- pg_itoa(arg1, VARDATA(result));
- SET_VARSIZE(result, strlen(VARDATA(result)) + VARHDRSZ);
- PG_RETURN_TEXT_P(result);
-}
-
-Datum
-text_int2(PG_FUNCTION_ARGS)
-{
- text *string = PG_GETARG_TEXT_P(0);
- Datum result;
- int len;
- char *str;
-
- len = VARSIZE(string) - VARHDRSZ;
-
- str = palloc(len + 1);
- memcpy(str, VARDATA(string), len);
- *(str + len) = '\0';
-
- result = DirectFunctionCall1(int2in, CStringGetDatum(str));
- pfree(str);
-
- return result;
-}
-
-Datum
-int4_text(PG_FUNCTION_ARGS)
-{
- int32 arg1 = PG_GETARG_INT32(0);
- text *result = (text *) palloc(12 + VARHDRSZ); /* sign,10 digits,'\0' */
-
- pg_ltoa(arg1, VARDATA(result));
- SET_VARSIZE(result, strlen(VARDATA(result)) + VARHDRSZ);
- PG_RETURN_TEXT_P(result);
-}
-
-Datum
-text_int4(PG_FUNCTION_ARGS)
-{
- text *string = PG_GETARG_TEXT_P(0);
- Datum result;
- int len;
- char *str;
-
- len = VARSIZE(string) - VARHDRSZ;
-
- str = palloc(len + 1);
- memcpy(str, VARDATA(string), len);
- *(str + len) = '\0';
-
- result = DirectFunctionCall1(int4in, CStringGetDatum(str));
- pfree(str);
-
- return result;
-}
-
/* Cast int4 -> bool */
Datum
int4_bool(PG_FUNCTION_ARGS)
int32 arg2 = PG_GETARG_INT32(1);
int32 result;
-#ifdef WIN32
-
- /*
- * Win32 doesn't throw a catchable exception for SELECT -2147483648 *
- * (-1); -- INT_MIN
- */
- if (arg2 == -1 && arg1 == INT_MIN)
- ereport(ERROR,
- (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("integer out of range")));
-#endif
-
result = arg1 * arg2;
/*
if (!(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)))
+ ((arg2 == -1 && arg1 < 0 && result < 0) ||
+ result / arg2 != arg1))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("integer out of range")));
int32 result;
if (arg2 == 0)
+ {
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
-
-#ifdef WIN32
+ /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+ PG_RETURN_NULL();
+ }
/*
- * Win32 doesn't throw a catchable exception for SELECT -2147483648 /
- * (-1); -- INT_MIN
+ * INT_MIN / -1 is problematic, since the result can't be represented on a
+ * two's-complement machine. Some machines produce INT_MIN, some produce
+ * zero, some throw an exception. We can dodge the problem by recognizing
+ * that division by -1 is the same as negation.
*/
- if (arg2 == -1 && arg1 == INT_MIN)
- ereport(ERROR,
- (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("integer out of range")));
-#endif
+ if (arg2 == -1)
+ {
+ result = -arg1;
+ /* overflow check (needed for INT_MIN) */
+ if (arg1 != 0 && SAMESIGN(result, arg1))
+ ereport(ERROR,
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("integer out of range")));
+ PG_RETURN_INT32(result);
+ }
+
+ /* No overflow is possible */
result = arg1 / arg2;
- /*
- * Overflow check. The only possible overflow case is for arg1 = INT_MIN,
- * arg2 = -1, where the correct result is -INT_MIN, which can't be
- * represented on a two's-complement machine.
- */
- if (arg2 == -1 && arg1 < 0 && result < 0)
- ereport(ERROR,
- (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("integer out of range")));
PG_RETURN_INT32(result);
}
int16 result;
if (arg2 == 0)
+ {
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
-
- result = arg1 / arg2;
+ /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+ PG_RETURN_NULL();
+ }
/*
- * Overflow check. The only possible overflow case is for arg1 =
- * SHRT_MIN, arg2 = -1, where the correct result is -SHRT_MIN, which can't
- * be represented on a two's-complement machine.
+ * SHRT_MIN / -1 is problematic, since the result can't be represented on
+ * a two's-complement machine. Some machines produce SHRT_MIN, some
+ * produce zero, some throw an exception. We can dodge the problem by
+ * recognizing that division by -1 is the same as negation.
*/
- if (arg2 == -1 && arg1 < 0 && result < 0)
- ereport(ERROR,
- (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("smallint out of range")));
+ if (arg2 == -1)
+ {
+ result = -arg1;
+ /* overflow check (needed for SHRT_MIN) */
+ if (arg1 != 0 && SAMESIGN(result, arg1))
+ ereport(ERROR,
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("smallint out of range")));
+ PG_RETURN_INT16(result);
+ }
+
+ /* No overflow is possible */
+
+ result = arg1 / arg2;
+
PG_RETURN_INT16(result);
}
int32 arg2 = PG_GETARG_INT32(1);
if (arg2 == 0)
+ {
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
+ /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+ PG_RETURN_NULL();
+ }
+
/* No overflow is possible */
PG_RETURN_INT32((int32) arg1 / arg2);
}
int32 result;
if (arg2 == 0)
+ {
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
-
- result = arg1 / arg2;
+ /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+ PG_RETURN_NULL();
+ }
/*
- * Overflow check. The only possible overflow case is for arg1 = INT_MIN,
- * arg2 = -1, where the correct result is -INT_MIN, which can't be
- * represented on a two's-complement machine.
+ * INT_MIN / -1 is problematic, since the result can't be represented on a
+ * two's-complement machine. Some machines produce INT_MIN, some produce
+ * zero, some throw an exception. We can dodge the problem by recognizing
+ * that division by -1 is the same as negation.
*/
- if (arg2 == -1 && arg1 < 0 && result < 0)
- ereport(ERROR,
- (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
- errmsg("integer out of range")));
+ if (arg2 == -1)
+ {
+ result = -arg1;
+ /* overflow check (needed for INT_MIN) */
+ if (arg1 != 0 && SAMESIGN(result, arg1))
+ ereport(ERROR,
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("integer out of range")));
+ PG_RETURN_INT32(result);
+ }
+
+ /* No overflow is possible */
+
+ result = arg1 / arg2;
+
PG_RETURN_INT32(result);
}
int32 arg2 = PG_GETARG_INT32(1);
if (arg2 == 0)
+ {
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
+ /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+ PG_RETURN_NULL();
+ }
- /* SELECT ((-2147483648)::int4) % (-1); causes a floating point exception */
- if (arg1 == INT_MIN && arg2 == -1)
+ /*
+ * Some machines throw a floating-point exception for INT_MIN % -1, which
+ * is a bit silly since the correct answer is perfectly well-defined,
+ * namely zero.
+ */
+ if (arg2 == -1)
PG_RETURN_INT32(0);
/* No overflow is possible */
int16 arg2 = PG_GETARG_INT16(1);
if (arg2 == 0)
+ {
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
- /* No overflow is possible */
-
- PG_RETURN_INT16(arg1 % arg2);
-}
-
-Datum
-int24mod(PG_FUNCTION_ARGS)
-{
- int16 arg1 = PG_GETARG_INT16(0);
- int32 arg2 = PG_GETARG_INT32(1);
-
- if (arg2 == 0)
- ereport(ERROR,
- (errcode(ERRCODE_DIVISION_BY_ZERO),
- errmsg("division by zero")));
- /* No overflow is possible */
-
- PG_RETURN_INT32(arg1 % arg2);
-}
+ /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+ PG_RETURN_NULL();
+ }
-Datum
-int42mod(PG_FUNCTION_ARGS)
-{
- int32 arg1 = PG_GETARG_INT32(0);
- int16 arg2 = PG_GETARG_INT16(1);
+ /*
+ * Some machines throw a floating-point exception for INT_MIN % -1, which
+ * is a bit silly since the correct answer is perfectly well-defined,
+ * namely zero. (It's not clear this ever happens when dealing with
+ * int16, but we might as well have the test for safety.)
+ */
+ if (arg2 == -1)
+ PG_RETURN_INT16(0);
- if (arg2 == 0)
- ereport(ERROR,
- (errcode(ERRCODE_DIVISION_BY_ZERO),
- errmsg("division by zero")));
/* No overflow is possible */
- PG_RETURN_INT32(arg1 % arg2);
+ PG_RETURN_INT16(arg1 % arg2);
}
/* increment current in preparation for next iteration */
fctx->current += fctx->step;
+ /* if next-value computation overflows, this is the final result */
+ if (SAMESIGN(result, fctx->step) && !SAMESIGN(result, fctx->current))
+ fctx->step = 0;
+
/* do when there is more left to send */
SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
}