File: math.c

package info (click to toggle)
orafce 4.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856 kB
  • sloc: ansic: 12,914; sql: 8,984; lex: 1,054; makefile: 131; yacc: 82; python: 7; sh: 2
file content (205 lines) | stat: -rw-r--r-- 4,404 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "postgres.h"

#include <math.h>

#include "funcapi.h"
#include "fmgr.h"
#include "utils/numeric.h"
#include "utils/builtins.h"


#include "orafce.h"
#include "builtins.h"

PG_FUNCTION_INFO_V1(orafce_reminder_smallint);
PG_FUNCTION_INFO_V1(orafce_reminder_int);
PG_FUNCTION_INFO_V1(orafce_reminder_bigint);
PG_FUNCTION_INFO_V1(orafce_reminder_numeric);

/*
 * CREATE OR REPLACE FUNCTION oracle.remainder(smallint, smallint)
 * RETURNS smallint
 */
Datum
orafce_reminder_smallint(PG_FUNCTION_ARGS)
{
	int16		arg1 = PG_GETARG_INT16(0);
	int16		arg2 = PG_GETARG_INT16(1);

	if (arg2 == 0)
	{
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
	}

	if (arg2 == -1)
		PG_RETURN_INT16(0);

	PG_RETURN_INT16(arg1 - ((int16) round(((double) arg1) / ((double) arg2)) * arg2));
}

/*
 * CREATE OR REPLACE FUNCTION oracle.remainder(int, int)
 * RETURNS int
 */
Datum
orafce_reminder_int(PG_FUNCTION_ARGS)
{
	int32		arg1 = PG_GETARG_INT32(0);
	int32		arg2 = PG_GETARG_INT32(1);

	if (arg2 == 0)
	{
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
	}

	if (arg2 == -1)
		PG_RETURN_INT32(0);

	PG_RETURN_INT32(arg1 - ((int32) round(((double) arg1) / ((double) arg2)) * arg2));
}

/*
 * CREATE OR REPLACE FUNCTION oracle.remainder(bigint, bigint)
 * RETURNS bigint
 */
Datum
orafce_reminder_bigint(PG_FUNCTION_ARGS)
{
	int64		arg1 = PG_GETARG_INT64(0);
	int64		arg2 = PG_GETARG_INT64(1);

	if (arg2 == 0)
	{
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
	}

	if (arg2 == -1)
		PG_RETURN_INT32(0);

	PG_RETURN_INT64(arg1 - ((int64) round(((long double) arg1) / ((long double) arg2)) * arg2));
}

/*
 * This will handle NaN and Infinity cases
 */
static Numeric
duplicate_numeric(Numeric num)
{
	Numeric		res;

	res = (Numeric) palloc(VARSIZE(num));
	memcpy(res, num, VARSIZE(num));
	return res;
}

static Numeric
get_numeric_in(const char *str)
{
	return DatumGetNumeric(
						   DirectFunctionCall3(numeric_in,
											   CStringGetDatum(str),
											   ObjectIdGetDatum(0),
											   Int32GetDatum(-1)));
}

static bool
orafce_numeric_is_inf(Numeric num)
{

#if PG_VERSION_NUM >= 140000

	return numeric_is_inf(num);

#else

	/* older releases doesn't support +-Infinitity in numeric type */

	return false;

#endif

}

/*
 * CREATE OR REPLACE FUNCTION oracle.remainder(numeric, numeric)
 * RETURNS numeric
 */
Datum
orafce_reminder_numeric(PG_FUNCTION_ARGS)
{
	Numeric		num1 = PG_GETARG_NUMERIC(0);
	Numeric		num2 = PG_GETARG_NUMERIC(1);
	Numeric		result;
	float8		val2;

	if (numeric_is_nan(num1))
		duplicate_numeric(num1);
	if (numeric_is_nan(num2))
		duplicate_numeric(num2);

	val2 = DatumGetFloat8(DirectFunctionCall1(numeric_float8, NumericGetDatum(num2)));

	if (val2 == 0)
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));

	if (orafce_numeric_is_inf(num1))
		PG_RETURN_NUMERIC(get_numeric_in("NaN"));

	if (orafce_numeric_is_inf(num2))
		duplicate_numeric(num1);

#if PG_VERSION_NUM >= 190000

	result = numeric_sub_safe(
							  num1,
							  numeric_mul_safe(
											   DatumGetNumeric(
															   DirectFunctionCall2(
																				   numeric_round,
																				   NumericGetDatum(
																								   numeric_div_safe(num1, num2, NULL)),
																				   Int32GetDatum(0))),
											   num2,
											   NULL),
							  NULL);

#elif PG_VERSION_NUM >= 150000

	result = numeric_sub_opt_error(
								   num1,
								   numeric_mul_opt_error(
														 DatumGetNumeric(
																		 DirectFunctionCall2(
																							 numeric_round,
																							 NumericGetDatum(
																											 numeric_div_opt_error(num1, num2, NULL)),
																							 Int32GetDatum(0))),
														 num2,
														 NULL),
								   NULL);

#else

	result = DatumGetNumeric(
							 DirectFunctionCall2(numeric_sub,
												 NumericGetDatum(num1),
												 DirectFunctionCall2(numeric_mul,
																	 DirectFunctionCall2(numeric_round,
																						 DirectFunctionCall2(numeric_div,
																											 NumericGetDatum(num1),
																											 NumericGetDatum(num2)),
																						 Int32GetDatum(0)),
																	 NumericGetDatum(num2))));

#endif

	PG_RETURN_NUMERIC(result);
}