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 10 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
539 changes: 537 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
87 changes: 87 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,86 @@ 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 *d = PG_GETARG_DATETIMEOFFSET(2);

char *lowunits;
int type,
val;
tsql_datetimeoffset *result;
Interval *interval;

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:
elog(ERROR, "the datepart %s is not supported by function dateadd for datatype datetimeoffset",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the error msg is different
and could you combine too err out places together

lowunits);
break;
}
} else {
elog(ERROR, "the datepart %s is not supported by function dateadd for datatype datetimeoffset", lowunits);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original msg on the script impl is :

	RAISE EXCEPTION '"%" is not a recognized dateadd option.', datepart;

}

result = (tsql_datetimeoffset *) DirectFunctionCall2(datetimeoffset_pl_interval, DatetimeoffsetGetDatum(d), PointerGetDatum(interval));

CheckDatetimeoffsetRange(result);
PG_RETURN_DATETIMEOFFSET(result);
}
Loading
Loading