Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement datediff and dateadd in c to improve performance #1998

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f2376cb
Reimplement datediff and dateadd in c to improve performance
Nov 8, 2023
6fc01d1
Remove comments
Nov 8, 2023
5ee47a5
Updated upgrade files and fix day in date
Nov 8, 2023
26c4de4
Merge branch 'BABEL_3_X_DEV' into jira-babel-4496
Nov 8, 2023
2d4689d
Updated testing
Nov 8, 2023
ed0d56c
Fix function removal
Nov 8, 2023
602dfd9
Updated dependency file
Nov 8, 2023
e97b681
Change upgrade drop function call
Nov 9, 2023
8aa2cf1
Update error handling
Nov 9, 2023
4319c8a
Fix testing output diff
Nov 9, 2023
f2429fc
Fix datetime out of range message
Nov 9, 2023
781b906
Fix error messages
Nov 14, 2023
10a3058
Remove changes from upgrade script temp
Nov 14, 2023
33a7851
Merge branch 'BABEL_3_X_DEV' into jira-babel-4496
Nov 14, 2023
1d43425
Remove expected dependency
Nov 14, 2023
11d8e3b
Update expected tests
Nov 14, 2023
2a7588f
Fix rounding issue
Nov 14, 2023
6ba70a7
Add upgrade file functions
Nov 14, 2023
36f68c6
Drop functions to avoid error
Nov 14, 2023
0d7e504
Fix syntax error
Nov 14, 2023
3340558
Fix incorrect errcode
Nov 15, 2023
bf70129
Merge branch 'BABEL_3_X_DEV' into jira-babel-4496
Nov 15, 2023
a0cee91
Fix errocode output in test files
Nov 15, 2023
3d1e5a3
Resolve format_datetime mvu failure
Nov 15, 2023
cefaae3
Modify error calls in datediff
Nov 15, 2023
b85969c
Unify error calls and cleanup test files
Nov 15, 2023
d5f039f
Merge branch 'BABEL_3_X_DEV' into jira-babel-4496
Nov 16, 2023
0d37ae3
Add appropriate error codes
Nov 16, 2023
5ccd559
Fix merge conflict
Nov 16, 2023
1593b72
Updated expected output error messages
Nov 16, 2023
4256536
Updated output files with proper error codes
Nov 16, 2023
329a733
Update TestErrorHelperFunctionsUpgrade-vu-verify.out
Nov 16, 2023
77a21cf
Empty-Commit to restart test
Nov 16, 2023
7c5c9d8
Consolidate error messages and add test case
Nov 17, 2023
17147ba
Merge branch 'BABEL_3_X_DEV' into jira-babel-4496
Nov 17, 2023
81491c7
Remove datediff_internal_date from new schedules
Nov 17, 2023
f819833
Remove tests from schedule
Nov 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
589 changes: 587 additions & 2 deletions contrib/babelfishpg_common/src/datetime.c

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions contrib/babelfishpg_common/src/datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ extern Timestamp initializeToDefaultDatetime(void);
extern double calculateDaysFromDefaultDatetime(Timestamp timestamp_left);
extern int roundFractionalSeconds(int fractseconds);

extern int days_in_date(int day, int month, int year);
extern char* datetypeName(int num);

extern bool int64_multiply_add(int64 val, int64 multiplier, int64 *sum);
extern bool int32_multiply_add(int32 val, int32 multiplier, int32 *sum);

/* Range-check a datetime */
#define IS_VALID_DATETIME(t) (MIN_DATETIME <= (t) && (t) < END_DATETIME)

Expand Down
105 changes: 105 additions & 0 deletions contrib/babelfishpg_common/src/datetimeoffset.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "utils/datetime.h"
#include "libpq/pqformat.h"
#include "utils/timestamp.h"
#include "parser/scansup.h"

#include "fmgr.h"
#include "miscadmin.h"
Expand Down Expand Up @@ -63,6 +64,9 @@ PG_FUNCTION_INFO_V1(datetimeoffset_datetime2);
PG_FUNCTION_INFO_V1(datetimeoffset_scale);

PG_FUNCTION_INFO_V1(get_datetimeoffset_tzoffset_internal);
PG_FUNCTION_INFO_V1(dateadd_datetimeoffset);

#define DTK_NANO 32


/* datetimeoffset_in()
Expand Down Expand Up @@ -832,3 +836,104 @@ EncodeDatetimeoffsetTimezone(char *str, int tz, int style)

*tmp = '\0';
}

Datum
dateadd_datetimeoffset(PG_FUNCTION_ARGS) {
text *field = PG_GETARG_TEXT_PP(0);
int num = PG_GETARG_INT32(1);
tsql_datetimeoffset *init_startdate = PG_GETARG_DATETIMEOFFSET(2);
bool validDateAdd = true;
char *lowunits;
int type,
val;
tsql_datetimeoffset *result;
Interval *interval;
int timezone = DirectFunctionCall1(get_datetimeoffset_tzoffset_internal, DatetimeoffsetGetDatum(init_startdate)) * 2;
tsql_datetimeoffset *startdate = (tsql_datetimeoffset *) DirectFunctionCall2(datetimeoffset_pl_interval, DatetimeoffsetGetDatum(init_startdate), DirectFunctionCall7(make_interval, 0, 0, 0, 0, 0, timezone, 0));


lowunits = downcase_truncate_identifier(VARDATA_ANY(field),
VARSIZE_ANY_EXHDR(field),
false);

type = DecodeUnits(0, lowunits, &val);

if(strncmp(lowunits, "doy", 3) == 0 || strncmp(lowunits, "dayofyear", 9) == 0) {
type = UNITS;
val = DTK_DOY;
}

if(strncmp(lowunits, "nanosecond", 11) == 0) {
type = UNITS;
val = DTK_NANO;
}
if(strncmp(lowunits, "weekday", 7) == 0) {
type = UNITS;
val = DTK_DAY;
}

if(type == UNITS) {
switch(val) {
case DTK_YEAR:
interval = (Interval *) DirectFunctionCall7(make_interval, num, 0, 0, 0, 0, 0, 0);
break;
case DTK_QUARTER:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, num * 3, 0, 0, 0, 0, 0);
break;
case DTK_MONTH:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, num, 0, 0, 0, 0, 0);
break;
case DTK_WEEK:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, num, 0, 0, 0, 0);
break;
case DTK_DAY:
case DTK_DOY:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, num, 0, 0, 0);
break;
case DTK_HOUR:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, 0, num, 0, 0);
break;
case DTK_MINUTE:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, 0, 0, num, 0);
break;
case DTK_SECOND:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, 0, 0, 0, Float8GetDatum(num));
break;
case DTK_MILLISEC:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, 0, 0, 0, Float8GetDatum((float) num * 0.001));
break;
case DTK_MICROSEC:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, 0, 0, 0, Float8GetDatum((float) num * 0.000001));
break;
case DTK_NANO:
interval = (Interval *) DirectFunctionCall7(make_interval, 0, 0, 0, 0, 0, 0, Float8GetDatum((float) num * 0.000000001));
break;
default:
validDateAdd = false;
break;
}
} else {
validDateAdd = false;
}

if(!validDateAdd) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\'%s\' is not a recognized %s option", lowunits, "dateadd")));
}

PG_TRY();
{
result = (tsql_datetimeoffset *) DirectFunctionCall2(datetimeoffset_pl_interval, DatetimeoffsetGetDatum(startdate), PointerGetDatum(interval));

}
PG_CATCH();
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("Adding a value to a \'%s\' column caused an overflow.", "datetimeoffset")));
}
PG_END_TRY();

PG_RETURN_DATETIMEOFFSET(result);
}
5 changes: 4 additions & 1 deletion contrib/babelfishpg_tds/error_mapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,7 @@ XX000 ERRCODE_INTERNAL_ERROR "The table-valued parameter \"%s\" must be declared
0A000 ERRCODE_FEATURE_NOT_SUPPORTED "Column name or number of supplied values does not match table definition." SQL_ERROR_213 16
42501 ERRCODE_INSUFFICIENT_PRIVILEGE "Only members of the sysadmin role can execute this stored procedure." SQL_ERROR_15003 16
42809 ERRCODE_WRONG_OBJECT_TYPE "The target \"%s\" of the OUTPUT INTO clause cannot be a view or common table expression." SQL_ERROR_330 16

22008 ERRCODE_DATETIME_VALUE_OUT_OF_RANGE "The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart" SQL_ERROR_535 16
22023 ERRCODE_INVALID_PARAMETER_VALUE "\'%s\' is not a recognized %s option" SQL_ERROR_155 15
22023 ERRCODE_INVALID_PARAMETER_VALUE "The datepart %s is not supported by date function %s for data type %s." SQL_ERROR_9810 16
22008 ERRCODE_DATETIME_VALUE_OUT_OF_RANGE "Adding a value to a \'%s\' column caused an overflow." SQL_ERROR_517 16
Loading
Loading