forked from postgres/postgres
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I added a new type named newtype based on int8. This type could be compared, sorted, and so on.
- Loading branch information
shawn
authored and
shawn
committed
Apr 13, 2020
1 parent
f1ac27b
commit cb5ae3c
Showing
10 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/*------------------------------------------------------------------------- | ||
* | ||
* newtype.c | ||
* Internal 64-bit integer operations | ||
* | ||
* Portions Copyright (c) 1996-2020, Shawn Wang | ||
* | ||
* IDENTIFICATION | ||
* src/backend/utils/adt/newtype.c | ||
* | ||
*------------------------------------------------------------------------- | ||
*/ | ||
#include "postgres.h" | ||
|
||
#include <ctype.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
|
||
#include "common/int.h" | ||
#include "funcapi.h" | ||
#include "libpq/pqformat.h" | ||
#include "nodes/nodeFuncs.h" | ||
#include "nodes/supportnodes.h" | ||
#include "optimizer/optimizer.h" | ||
#include "utils/builtins.h" | ||
#include "utils/int8.h" | ||
|
||
|
||
/*********************************************************************** | ||
** | ||
** Routines for 64-bit integers. | ||
** | ||
***********************************************************************/ | ||
|
||
/* newtypein() | ||
*/ | ||
Datum | ||
newtypein(PG_FUNCTION_ARGS) | ||
{ | ||
char *str = PG_GETARG_CSTRING(0); | ||
int64 result; | ||
|
||
(void) scanint8(str, false, &result); | ||
PG_RETURN_INT64(result); | ||
} | ||
|
||
|
||
/* newtypeout() | ||
*/ | ||
Datum | ||
newtypeout(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val = PG_GETARG_INT64(0); | ||
char buf[MAXINT8LEN + 1]; | ||
char *result; | ||
|
||
pg_lltoa(val, buf); | ||
result = pstrdup(buf); | ||
PG_RETURN_CSTRING(result); | ||
} | ||
|
||
/* | ||
* newtyperecv - converts external binary format to newtype | ||
*/ | ||
Datum | ||
newtyperecv(PG_FUNCTION_ARGS) | ||
{ | ||
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); | ||
|
||
PG_RETURN_INT64(pq_getmsgint64(buf)); | ||
} | ||
|
||
/* | ||
* int8send - converts newtype to binary format | ||
*/ | ||
Datum | ||
newtypesend(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)); | ||
} | ||
|
||
Datum | ||
newtypeeq(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val1 = PG_GETARG_INT64(0); | ||
int64 val2 = PG_GETARG_INT64(1); | ||
|
||
PG_RETURN_BOOL(val1 == val2); | ||
} | ||
|
||
Datum | ||
newtypene(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val1 = PG_GETARG_INT64(0); | ||
int64 val2 = PG_GETARG_INT64(1); | ||
|
||
PG_RETURN_BOOL(val1 != val2); | ||
} | ||
|
||
Datum | ||
newtypelt(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val1 = PG_GETARG_INT64(0); | ||
int64 val2 = PG_GETARG_INT64(1); | ||
|
||
PG_RETURN_BOOL(val1 < val2); | ||
} | ||
|
||
Datum | ||
newtypegt(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val1 = PG_GETARG_INT64(0); | ||
int64 val2 = PG_GETARG_INT64(1); | ||
|
||
PG_RETURN_BOOL(val1 > val2); | ||
} | ||
|
||
Datum | ||
newtypele(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val1 = PG_GETARG_INT64(0); | ||
int64 val2 = PG_GETARG_INT64(1); | ||
|
||
PG_RETURN_BOOL(val1 <= val2); | ||
} | ||
|
||
Datum | ||
newtypege(PG_FUNCTION_ARGS) | ||
{ | ||
int64 val1 = PG_GETARG_INT64(0); | ||
int64 val2 = PG_GETARG_INT64(1); | ||
|
||
PG_RETURN_BOOL(val1 >= val2); | ||
} | ||
|
||
Datum | ||
newtypelarger(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 | ||
newtypesmaller(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 | ||
btnewtypecmp(PG_FUNCTION_ARGS) | ||
{ | ||
int64 b1 = PG_GETARG_INT64(0); | ||
int64 b2 = PG_GETARG_INT64(1); | ||
int32 result = 0; | ||
|
||
if (b1 < b2) | ||
result = -1; | ||
else if (b1 > b2) | ||
result = 1; | ||
else | ||
result = 0; | ||
|
||
PG_RETURN_INT32(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters