Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb163baa

Browse files
committed
Clean up some problems with redundant cross-type arithmetic operators. Add
int2-and-int8 implementations of the basic arithmetic operators +, -, *, /.This doesn't really add any new functionality, but it avoids "operator is notunique" failures that formerly occurred in these cases because the parsercouldn't decide whether to promote the int2 to int4 or int8. We couldalternatively have removed the existing cross-type operators, butexperimentation shows that the cost of an additional type coercion expressionnode is noticeable compared to such cheap operators; so let's not give up anyperformance here. On the other hand, I removed the int2-and-int4 modulo (%)operators since they didn't seem as important from a performance standpoint.Per a complaint last January from ykhuang.
1 parent4274726 commitb163baa

File tree

7 files changed

+223
-50
lines changed

7 files changed

+223
-50
lines changed

‎src/backend/utils/adt/int.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.81 2008/01/01 19:45:52 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.82 2008/06/17 19:10:56 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1085,36 +1085,6 @@ int2mod(PG_FUNCTION_ARGS)
10851085
PG_RETURN_INT16(arg1 %arg2);
10861086
}
10871087

1088-
Datum
1089-
int24mod(PG_FUNCTION_ARGS)
1090-
{
1091-
int16arg1=PG_GETARG_INT16(0);
1092-
int32arg2=PG_GETARG_INT32(1);
1093-
1094-
if (arg2==0)
1095-
ereport(ERROR,
1096-
(errcode(ERRCODE_DIVISION_BY_ZERO),
1097-
errmsg("division by zero")));
1098-
/* No overflow is possible */
1099-
1100-
PG_RETURN_INT32(arg1 %arg2);
1101-
}
1102-
1103-
Datum
1104-
int42mod(PG_FUNCTION_ARGS)
1105-
{
1106-
int32arg1=PG_GETARG_INT32(0);
1107-
int16arg2=PG_GETARG_INT16(1);
1108-
1109-
if (arg2==0)
1110-
ereport(ERROR,
1111-
(errcode(ERRCODE_DIVISION_BY_ZERO),
1112-
errmsg("division by zero")));
1113-
/* No overflow is possible */
1114-
1115-
PG_RETURN_INT32(arg1 %arg2);
1116-
}
1117-
11181088

11191089
/* int[24]abs()
11201090
* Absolute value

‎src/backend/utils/adt/int8.c

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.69 2008/04/21 00:26:45 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.70 2008/06/17 19:10:56 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -922,6 +922,184 @@ int48div(PG_FUNCTION_ARGS)
922922
PG_RETURN_INT64((int64)arg1 /arg2);
923923
}
924924

925+
Datum
926+
int82pl(PG_FUNCTION_ARGS)
927+
{
928+
int64arg1=PG_GETARG_INT64(0);
929+
int16arg2=PG_GETARG_INT16(1);
930+
int64result;
931+
932+
result=arg1+arg2;
933+
934+
/*
935+
* Overflow check.If the inputs are of different signs then their sum
936+
* cannot overflow. If the inputs are of the same sign, their sum had
937+
* better be that sign too.
938+
*/
939+
if (SAMESIGN(arg1,arg2)&& !SAMESIGN(result,arg1))
940+
ereport(ERROR,
941+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
942+
errmsg("bigint out of range")));
943+
PG_RETURN_INT64(result);
944+
}
945+
946+
Datum
947+
int82mi(PG_FUNCTION_ARGS)
948+
{
949+
int64arg1=PG_GETARG_INT64(0);
950+
int16arg2=PG_GETARG_INT16(1);
951+
int64result;
952+
953+
result=arg1-arg2;
954+
955+
/*
956+
* Overflow check.If the inputs are of the same sign then their
957+
* difference cannot overflow.If they are of different signs then the
958+
* result should be of the same sign as the first input.
959+
*/
960+
if (!SAMESIGN(arg1,arg2)&& !SAMESIGN(result,arg1))
961+
ereport(ERROR,
962+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
963+
errmsg("bigint out of range")));
964+
PG_RETURN_INT64(result);
965+
}
966+
967+
Datum
968+
int82mul(PG_FUNCTION_ARGS)
969+
{
970+
int64arg1=PG_GETARG_INT64(0);
971+
int16arg2=PG_GETARG_INT16(1);
972+
int64result;
973+
974+
result=arg1*arg2;
975+
976+
/*
977+
* Overflow check.We basically check to see if result / arg1 gives arg2
978+
* again. There is one case where this fails: arg1 = 0 (which cannot
979+
* overflow).
980+
*
981+
* Since the division is likely much more expensive than the actual
982+
* multiplication, we'd like to skip it where possible. The best bang for
983+
* the buck seems to be to check whether both inputs are in the int32
984+
* range; if so, no overflow is possible.
985+
*/
986+
if (arg1!= (int64) ((int32)arg1)&&
987+
result /arg1!=arg2)
988+
ereport(ERROR,
989+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
990+
errmsg("bigint out of range")));
991+
PG_RETURN_INT64(result);
992+
}
993+
994+
Datum
995+
int82div(PG_FUNCTION_ARGS)
996+
{
997+
int64arg1=PG_GETARG_INT64(0);
998+
int16arg2=PG_GETARG_INT16(1);
999+
int64result;
1000+
1001+
if (arg2==0)
1002+
ereport(ERROR,
1003+
(errcode(ERRCODE_DIVISION_BY_ZERO),
1004+
errmsg("division by zero")));
1005+
1006+
result=arg1 /arg2;
1007+
1008+
/*
1009+
* Overflow check.The only possible overflow case is for arg1 =
1010+
* INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
1011+
* can't be represented on a two's-complement machine.
1012+
*/
1013+
if (arg2==-1&&arg1<0&&result<0)
1014+
ereport(ERROR,
1015+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1016+
errmsg("bigint out of range")));
1017+
PG_RETURN_INT64(result);
1018+
}
1019+
1020+
Datum
1021+
int28pl(PG_FUNCTION_ARGS)
1022+
{
1023+
int16arg1=PG_GETARG_INT16(0);
1024+
int64arg2=PG_GETARG_INT64(1);
1025+
int64result;
1026+
1027+
result=arg1+arg2;
1028+
1029+
/*
1030+
* Overflow check.If the inputs are of different signs then their sum
1031+
* cannot overflow. If the inputs are of the same sign, their sum had
1032+
* better be that sign too.
1033+
*/
1034+
if (SAMESIGN(arg1,arg2)&& !SAMESIGN(result,arg1))
1035+
ereport(ERROR,
1036+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1037+
errmsg("bigint out of range")));
1038+
PG_RETURN_INT64(result);
1039+
}
1040+
1041+
Datum
1042+
int28mi(PG_FUNCTION_ARGS)
1043+
{
1044+
int16arg1=PG_GETARG_INT16(0);
1045+
int64arg2=PG_GETARG_INT64(1);
1046+
int64result;
1047+
1048+
result=arg1-arg2;
1049+
1050+
/*
1051+
* Overflow check.If the inputs are of the same sign then their
1052+
* difference cannot overflow.If they are of different signs then the
1053+
* result should be of the same sign as the first input.
1054+
*/
1055+
if (!SAMESIGN(arg1,arg2)&& !SAMESIGN(result,arg1))
1056+
ereport(ERROR,
1057+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1058+
errmsg("bigint out of range")));
1059+
PG_RETURN_INT64(result);
1060+
}
1061+
1062+
Datum
1063+
int28mul(PG_FUNCTION_ARGS)
1064+
{
1065+
int16arg1=PG_GETARG_INT16(0);
1066+
int64arg2=PG_GETARG_INT64(1);
1067+
int64result;
1068+
1069+
result=arg1*arg2;
1070+
1071+
/*
1072+
* Overflow check.We basically check to see if result / arg2 gives arg1
1073+
* again. There is one case where this fails: arg2 = 0 (which cannot
1074+
* overflow).
1075+
*
1076+
* Since the division is likely much more expensive than the actual
1077+
* multiplication, we'd like to skip it where possible. The best bang for
1078+
* the buck seems to be to check whether both inputs are in the int32
1079+
* range; if so, no overflow is possible.
1080+
*/
1081+
if (arg2!= (int64) ((int32)arg2)&&
1082+
result /arg2!=arg1)
1083+
ereport(ERROR,
1084+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1085+
errmsg("bigint out of range")));
1086+
PG_RETURN_INT64(result);
1087+
}
1088+
1089+
Datum
1090+
int28div(PG_FUNCTION_ARGS)
1091+
{
1092+
int16arg1=PG_GETARG_INT16(0);
1093+
int64arg2=PG_GETARG_INT64(1);
1094+
1095+
if (arg2==0)
1096+
ereport(ERROR,
1097+
(errcode(ERRCODE_DIVISION_BY_ZERO),
1098+
errmsg("division by zero")));
1099+
/* No overflow is possible */
1100+
PG_RETURN_INT64((int64)arg1 /arg2);
1101+
}
1102+
9251103
/* Binary arithmetics
9261104
*
9271105
*int8and- returns arg1 & arg2

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.462 2008/05/27 00:13:09 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.463 2008/06/17 19:10:56 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200805261
56+
#defineCATALOG_VERSION_NO200806171
5757

5858
#endif

‎src/include/catalog/pg_operator.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.159 2008/05/27 00:13:09 tgl Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.160 2008/06/17 19:10:56 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -204,8 +204,6 @@ DATA(insert OID = 544 ( "*" PGNSP PGUID b f f212323 545 0 int24mul - - )
204204
DATA(insertOID=545 ("*"PGNSPPGUIDbff2321235440int42mul-- ));
205205
DATA(insertOID=546 ("/"PGNSPPGUIDbff21232300int24div-- ));
206206
DATA(insertOID=547 ("/"PGNSPPGUIDbff23212300int42div-- ));
207-
DATA(insertOID=548 ("%"PGNSPPGUIDbff21232300int24mod-- ));
208-
DATA(insertOID=549 ("%"PGNSPPGUIDbff23212300int42mod-- ));
209207
DATA(insertOID=550 ("+"PGNSPPGUIDbff2121215500int2pl-- ));
210208
DATA(insertOID=551 ("+"PGNSPPGUIDbff2323235510int4pl-- ));
211209
DATA(insertOID=552 ("+"PGNSPPGUIDbff2123235530int24pl-- ));
@@ -321,6 +319,7 @@ DATA(insert OID = 684 ( "+" PGNSP PGUID b f f202020 684 0 int8pl - - ));
321319
DATA(insertOID=685 ("-"PGNSPPGUIDbff20202000int8mi-- ));
322320
DATA(insertOID=686 ("*"PGNSPPGUIDbff2020206860int8mul-- ));
323321
DATA(insertOID=687 ("/"PGNSPPGUIDbff20202000int8div-- ));
322+
324323
DATA(insertOID=688 ("+"PGNSPPGUIDbff2023206920int84pl-- ));
325324
DATA(insertOID=689 ("-"PGNSPPGUIDbff20232000int84mi-- ));
326325
DATA(insertOID=690 ("*"PGNSPPGUIDbff2023206940int84mul-- ));
@@ -330,6 +329,15 @@ DATA(insert OID = 693 ( "-" PGNSP PGUID b f f232020 0 0 int48mi - - ));
330329
DATA(insertOID=694 ("*"PGNSPPGUIDbff2320206900int48mul-- ));
331330
DATA(insertOID=695 ("/"PGNSPPGUIDbff23202000int48div-- ));
332331

332+
DATA(insertOID=818 ("+"PGNSPPGUIDbff2021208220int82pl-- ));
333+
DATA(insertOID=819 ("-"PGNSPPGUIDbff20212000int82mi-- ));
334+
DATA(insertOID=820 ("*"PGNSPPGUIDbff2021208240int82mul-- ));
335+
DATA(insertOID=821 ("/"PGNSPPGUIDbff20212000int82div-- ));
336+
DATA(insertOID=822 ("+"PGNSPPGUIDbff2120208180int28pl-- ));
337+
DATA(insertOID=823 ("-"PGNSPPGUIDbff21202000int28mi-- ));
338+
DATA(insertOID=824 ("*"PGNSPPGUIDbff2120208200int28mul-- ));
339+
DATA(insertOID=825 ("/"PGNSPPGUIDbff21202000int28div-- ));
340+
333341
DATA(insertOID=706 ("<->"PGNSPPGUIDbff6036037017060box_distance-- ));
334342
DATA(insertOID=707 ("<->"PGNSPPGUIDbff6026027017070path_distance-- ));
335343
DATA(insertOID=708 ("<->"PGNSPPGUIDbff6286287017080line_distance-- ));

‎src/include/catalog/pg_proc.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.502 2008/05/29 22:48:07 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.503 2008/06/17 19:10:56 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -351,10 +351,6 @@ DATA(insert OID = 172 ( int24div PGNSP PGUID 12 1 0 f f t f i 2 23 "21 23"
351351
DESCR("divide");
352352
DATA(insertOID=173 (int42divPGNSPPGUID1210fftfi223"23 21"_null__null__null_int42div-_null__null_ ));
353353
DESCR("divide");
354-
DATA(insertOID=174 (int24modPGNSPPGUID1210fftfi223"21 23"_null__null__null_int24mod-_null__null_ ));
355-
DESCR("modulus");
356-
DATA(insertOID=175 (int42modPGNSPPGUID1210fftfi223"23 21"_null__null__null_int42mod-_null__null_ ));
357-
DESCR("modulus");
358354
DATA(insertOID=176 (int2plPGNSPPGUID1210fftfi221"21 21"_null__null__null_int2pl-_null__null_ ));
359355
DESCR("add");
360356
DATA(insertOID=177 (int4plPGNSPPGUID1210fftfi223"23 23"_null__null__null_int4pl-_null__null_ ));
@@ -1177,10 +1173,6 @@ DATA(insert OID = 940 ( mod PGNSP PGUID 12 1 0 f f t f i 2 21 "21 21" _nul
11771173
DESCR("modulus");
11781174
DATA(insertOID=941 (modPGNSPPGUID1210fftfi223"23 23"_null__null__null_int4mod-_null__null_ ));
11791175
DESCR("modulus");
1180-
DATA(insertOID=942 (modPGNSPPGUID1210fftfi223"21 23"_null__null__null_int24mod-_null__null_ ));
1181-
DESCR("modulus");
1182-
DATA(insertOID=943 (modPGNSPPGUID1210fftfi223"23 21"_null__null__null_int42mod-_null__null_ ));
1183-
DESCR("modulus");
11841176

11851177
DATA(insertOID=945 (int8modPGNSPPGUID1210fftfi220"20 20"_null__null__null_int8mod-_null__null_ ));
11861178
DESCR("modulus");
@@ -1570,6 +1562,23 @@ DESCR("multiply");
15701562
DATA(insertOID=1281 (int48divPGNSPPGUID1210fftfi220"23 20"_null__null__null_int48div-_null__null_ ));
15711563
DESCR("divide");
15721564

1565+
DATA(insertOID=837 (int82plPGNSPPGUID1210fftfi220"20 21"_null__null__null_int82pl-_null__null_ ));
1566+
DESCR("add");
1567+
DATA(insertOID=838 (int82miPGNSPPGUID1210fftfi220"20 21"_null__null__null_int82mi-_null__null_ ));
1568+
DESCR("subtract");
1569+
DATA(insertOID=839 (int82mulPGNSPPGUID1210fftfi220"20 21"_null__null__null_int82mul-_null__null_ ));
1570+
DESCR("multiply");
1571+
DATA(insertOID=840 (int82divPGNSPPGUID1210fftfi220"20 21"_null__null__null_int82div-_null__null_ ));
1572+
DESCR("divide");
1573+
DATA(insertOID=841 (int28plPGNSPPGUID1210fftfi220"21 20"_null__null__null_int28pl-_null__null_ ));
1574+
DESCR("add");
1575+
DATA(insertOID=942 (int28miPGNSPPGUID1210fftfi220"21 20"_null__null__null_int28mi-_null__null_ ));
1576+
DESCR("subtract");
1577+
DATA(insertOID=943 (int28mulPGNSPPGUID1210fftfi220"21 20"_null__null__null_int28mul-_null__null_ ));
1578+
DESCR("multiply");
1579+
DATA(insertOID=948 (int28divPGNSPPGUID1210fftfi220"21 20"_null__null__null_int28div-_null__null_ ));
1580+
DESCR("divide");
1581+
15731582
DATA(insertOID=1287 (oidPGNSPPGUID1210fftfi126"20"_null__null__null_i8tooid-_null__null_ ));
15741583
DESCR("convert int8 to oid");
15751584
DATA(insertOID=1288 (int8PGNSPPGUID1210fftfi120"26"_null__null__null_oidtoi8-_null__null_ ));

‎src/include/utils/builtins.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.316 2008/05/27 00:13:09 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.317 2008/06/17 19:10:56 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -190,8 +190,6 @@ extern Datum int42mul(PG_FUNCTION_ARGS);
190190
externDatumint42div(PG_FUNCTION_ARGS);
191191
externDatumint4mod(PG_FUNCTION_ARGS);
192192
externDatumint2mod(PG_FUNCTION_ARGS);
193-
externDatumint24mod(PG_FUNCTION_ARGS);
194-
externDatumint42mod(PG_FUNCTION_ARGS);
195193
externDatumint2larger(PG_FUNCTION_ARGS);
196194
externDatumint2smaller(PG_FUNCTION_ARGS);
197195
externDatumint4larger(PG_FUNCTION_ARGS);

‎src/include/utils/int8.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/int8.h,v 1.48 2008/01/01 19:45:59 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/int8.h,v 1.49 2008/06/17 19:10:56 tgl Exp $
1111
*
1212
* NOTES
1313
* These data types are supported on all 64-bit architectures, and may
@@ -96,6 +96,16 @@ extern Datum int48mi(PG_FUNCTION_ARGS);
9696
externDatumint48mul(PG_FUNCTION_ARGS);
9797
externDatumint48div(PG_FUNCTION_ARGS);
9898

99+
externDatumint82pl(PG_FUNCTION_ARGS);
100+
externDatumint82mi(PG_FUNCTION_ARGS);
101+
externDatumint82mul(PG_FUNCTION_ARGS);
102+
externDatumint82div(PG_FUNCTION_ARGS);
103+
104+
externDatumint28pl(PG_FUNCTION_ARGS);
105+
externDatumint28mi(PG_FUNCTION_ARGS);
106+
externDatumint28mul(PG_FUNCTION_ARGS);
107+
externDatumint28div(PG_FUNCTION_ARGS);
108+
99109
externDatumint48(PG_FUNCTION_ARGS);
100110
externDatumint84(PG_FUNCTION_ARGS);
101111

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp