Skip to content

Commit 97d5efa

Browse files
Added config tests to check if DATENAME is STABLE
Task: BABEL-5846 Signed-off-by: Manisha Deshpande <mmdeshp@amazon.com>
1 parent 8b559a0 commit 97d5efa

17 files changed

+386
-24
lines changed

contrib/babelfishpg_tsql/sql/sys_functions.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ BEGIN
20832083
RETURN result;
20842084
END;
20852085
$BODY$
2086-
LANGUAGE plpgsql IMMUTABLE;
2086+
LANGUAGE plpgsql STABLE;
20872087

20882088
-- Duplicate function with arg TEXT since ANYELEMENT cannot handle type unknown.
20892089
CREATE OR REPLACE FUNCTION sys.datename(IN dp PG_CATALOG.TEXT, IN arg TEXT) RETURNS TEXT AS
@@ -2108,7 +2108,7 @@ BEGIN
21082108
RETURN result;
21092109
END;
21102110
$BODY$
2111-
LANGUAGE plpgsql IMMUTABLE;
2111+
LANGUAGE plpgsql STABLE;
21122112

21132113
-- These come from the built-in pg_catalog.count in pg_aggregate.dat
21142114
CREATE AGGREGATE sys.count(*)

contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.2.0--5.3.0.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ BEGIN
10341034
RETURN result;
10351035
END;
10361036
$BODY$
1037-
LANGUAGE plpgsql IMMUTABLE;
1037+
LANGUAGE plpgsql STABLE;
10381038

10391039
-- Duplicate function with arg TEXT since ANYELEMENT cannot handle type unknown.
10401040
CREATE OR REPLACE FUNCTION sys.datename(IN dp PG_CATALOG.TEXT, IN arg TEXT) RETURNS TEXT AS
@@ -1059,7 +1059,7 @@ BEGIN
10591059
RETURN result;
10601060
END;
10611061
$BODY$
1062-
LANGUAGE plpgsql IMMUTABLE;
1062+
LANGUAGE plpgsql STABLE;
10631063

10641064
-- Drops the temporary procedure used by the upgrade script.
10651065
-- Please have this be one of the last statements executed in this upgrade script.

test/JDBC/expected/babel_function.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,8 @@ text
12161216
-- test invalid argument, expect error
12171217
select datename(invalid_interval, cast('2016-12-26 23:30:05.523456' as date));
12181218
GO
1219+
~~START~~
1220+
text
12191221
~~ERROR (Code: 33557097)~~
12201222

12211223
~~ERROR (Message: 'invalid_interval' is not a recognized datepart option)~~
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
DROP TABLE datename_tzoffset_test_cases;
22
GO
33

4+
DROP TABLE datename_stability_test;
5+
GO
6+
47
DROP VIEW datename_tzoffset_no_offset_tests_view;
58
GO

test/JDBC/expected/datename_tzoffset-vu-prepare.out

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,177 @@ SELECT
1212
datename(TZOFFSET, '14:30:15.1234567') AS timestamp_offset,
1313
datename(TZOFFSET, '') AS empty_string_offset;
1414
GO
15+
16+
-- Create table for stability testing of DATENAME
17+
-- to store results under different configurations
18+
CREATE TABLE datename_stability_test (
19+
config_type VARCHAR(20),
20+
config_value VARCHAR(30),
21+
test_date VARCHAR(50),
22+
datepart VARCHAR(20),
23+
result NVARCHAR(100)
24+
);
25+
GO
26+
27+
28+
-- Testing SET LANGUAGE stability
29+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
30+
VALUES ('Initial', 'default', '2025-06-30', 'weekday', DATENAME(weekday, '2025-06-30'));
31+
GO
32+
~~ROW COUNT: 1~~
33+
34+
35+
-- SET LANGUAGE 'us_english' (default)
36+
SET LANGUAGE 'us_english';
37+
GO
38+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
39+
VALUES ('LANGUAGE', 'us_english', '2025-06-30', 'weekday', DATENAME(weekday, '2025-06-30'));
40+
GO
41+
~~ROW COUNT: 1~~
42+
43+
44+
-- SET LANGUAGE 'French' (not fully supported but test anyway)
45+
SET LANGUAGE 'French';
46+
GO
47+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
48+
VALUES ('LANGUAGE', 'French', '2025-06-30', 'weekday', DATENAME(weekday, '2025-06-30'));
49+
GO
50+
~~ROW COUNT: 1~~
51+
52+
53+
-- Restore default language
54+
SET LANGUAGE 'us_english';
55+
GO
56+
57+
58+
-- Testing SET DATEFIRST stability
59+
-- SET DATEFIRST 7 (default - Sunday)
60+
SET DATEFIRST 7;
61+
GO
62+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
63+
VALUES ('DATEFIRST', '7', '2025-01-05', 'week', DATENAME(week, '2025-01-05'));
64+
GO
65+
~~ROW COUNT: 1~~
66+
67+
68+
-- SET DATEFIRST 1 (Monday)
69+
SET DATEFIRST 1;
70+
GO
71+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
72+
VALUES ('DATEFIRST', '1', '2025-01-05', 'week', DATENAME(week, '2025-01-05'));
73+
GO
74+
~~ROW COUNT: 1~~
75+
76+
77+
-- Restore default
78+
SET DATEFIRST 7;
79+
GO
80+
81+
82+
-- Testing SET DATEFORMAT stability
83+
-- SET DATEFORMAT mdy (default)
84+
SET DATEFORMAT mdy;
85+
GO
86+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
87+
VALUES ('DATEFORMAT', 'mdy', '03-06-2025', 'month', DATENAME(month, '03-06-2025'));
88+
GO
89+
~~ROW COUNT: 1~~
90+
91+
92+
-- SET DATEFORMAT dmy (not supported, treated as mdy)
93+
SET DATEFORMAT dmy;
94+
GO
95+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
96+
VALUES ('DATEFORMAT', 'dmy', '03-06-2025', 'month', DATENAME(month, '03-06-2025'));
97+
GO
98+
~~ROW COUNT: 1~~
99+
100+
101+
102+
-- SET DATEFORMAT ymd
103+
SET DATEFORMAT ymd;
104+
GO
105+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
106+
VALUES ('DATEFORMAT', 'ymd', '2025-03-06', 'month', DATENAME(month, '2025-03-06'));
107+
GO
108+
~~ROW COUNT: 1~~
109+
110+
111+
-- SET DATEFORMAT ydm (not supported, treated as ymd)
112+
SET DATEFORMAT ydm;
113+
GO
114+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
115+
VALUES ('DATEFORMAT', 'ydm', '2025-03-06', 'month', DATENAME(month, '2025-03-06'));
116+
GO
117+
~~ROW COUNT: 1~~
118+
119+
120+
121+
-- SET DATEFORMAT myd (not supported currently)
122+
SET DATEFORMAT myd;
123+
GO
124+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
125+
VALUES ('DATEFORMAT', 'myd', '06-2025-03', 'month', DATENAME(month, '06-2025-03'));
126+
GO
127+
~~ERROR (Code: 33557097)~~
128+
129+
~~ERROR (Message: date/time field value out of range: "06-2025-03")~~
130+
131+
132+
-- SET DATEFORMAT dym (not supported currently)
133+
SET DATEFORMAT dym;
134+
GO
135+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
136+
VALUES ('DATEFORMAT', 'dym', '06-2025-03', 'month', DATENAME(month, '06-2025-03'));
137+
GO
138+
~~ERROR (Code: 33557097)~~
139+
140+
~~ERROR (Message: date/time field value out of range: "06-2025-03")~~
141+
142+
143+
-- Restore default
144+
SET DATEFORMAT mdy;
145+
GO
146+
147+
148+
-- Testing SET_CONFIG('TIMEZONE') stability
149+
-- Set timezone to UTC
150+
SELECT set_config('timezone', 'UTC', false);
151+
GO
152+
~~START~~
153+
text
154+
UTC
155+
~~END~~
156+
157+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
158+
VALUES
159+
('TIMEZONE', 'UTC', '2025-06-30 12:00:00', 'hour', DATENAME(hour, '2025-06-30 12:00:00')),
160+
('TIMEZONE', 'UTC', '2025-01-01 05:30:45', 'hour', DATENAME(hour, '2025-01-01 05:30:45'));
161+
GO
162+
~~ROW COUNT: 2~~
163+
164+
165+
-- Set timezone to a different value
166+
SELECT set_config('timezone', 'Africa/Nairobi', false)
167+
GO
168+
~~START~~
169+
text
170+
Africa/Nairobi
171+
~~END~~
172+
173+
INSERT INTO datename_stability_test (config_type, config_value, test_date, datepart, result)
174+
VALUES
175+
('TIMEZONE', 'Africa/Nairobi', '2025-06-30 12:00:00', 'hour', DATENAME(hour, '2025-06-30 12:00:00')),
176+
('TIMEZONE', 'Africa/Nairobi', '2025-01-01 05:30:45', 'hour', DATENAME(hour, '2025-01-01 05:30:45'));
177+
GO
178+
~~ROW COUNT: 2~~
179+
180+
181+
-- Reset timezone to UTC
182+
SELECT set_config('timezone', 'UTC', false);
183+
GO
184+
~~START~~
185+
text
186+
UTC
187+
~~END~~
188+

test/JDBC/expected/datename_tzoffset-vu-verify.out

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,16 @@ text
126126
-- out of range
127127
SELECT DATENAME(TZOFFSET, CAST('2025-06-03 14:30:15 +14:01' AS DATETIMEOFFSET));
128128
GO
129+
~~START~~
130+
text
129131
~~ERROR (Code: 33557097)~~
130132

131133
~~ERROR (Message: The conversion of a varchar data type to a datetimeoffset data type resulted in an out-of-range value.)~~
132134

133135
SELECT DATENAME(TZOFFSET, '2025-06-03 14:30:15 -14:01');
134136
GO
137+
~~START~~
138+
text
135139
~~ERROR (Code: 33557097)~~
136140

137141
~~ERROR (Message: The conversion of a varchar data type to a datetimeoffset data type resulted in an out-of-range value.)~~
@@ -154,31 +158,74 @@ text
154158

155159

156160

161+
-- Checking stability of DATENAME for varying configs:
162+
SELECT * FROM datename_stability_test
163+
ORDER BY config_type, config_value;
164+
GO
165+
~~START~~
166+
varchar#!#varchar#!#varchar#!#varchar#!#nvarchar
167+
DATEFIRST#!#1#!#2025-01-05#!#week#!#1
168+
DATEFIRST#!#7#!#2025-01-05#!#week#!#2
169+
DATEFORMAT#!#dmy#!#03-06-2025#!#month#!#March
170+
DATEFORMAT#!#mdy#!#03-06-2025#!#month#!#March
171+
DATEFORMAT#!#ydm#!#2025-03-06#!#month#!#March
172+
DATEFORMAT#!#ymd#!#2025-03-06#!#month#!#March
173+
Initial#!#default#!#2025-06-30#!#weekday#!#Monday
174+
LANGUAGE#!#French#!#2025-06-30#!#weekday#!#Monday
175+
LANGUAGE#!#us_english#!#2025-06-30#!#weekday#!#Monday
176+
TIMEZONE#!#Africa/Nairobi#!#2025-06-30 12:00:00#!#hour#!#12
177+
TIMEZONE#!#Africa/Nairobi#!#2025-01-01 05:30:45#!#hour#!#5
178+
TIMEZONE#!#UTC#!#2025-06-30 12:00:00#!#hour#!#12
179+
TIMEZONE#!#UTC#!#2025-01-01 05:30:45#!#hour#!#5
180+
~~END~~
181+
182+
183+
-- Check for any variations in results for same inputs under different settings
184+
-- Expected: DATENAME with DATEFIRST setting
185+
SELECT config_type, test_date, datepart, COUNT(DISTINCT result) as distinct_results
186+
FROM datename_stability_test
187+
GROUP BY config_type, test_date, datepart
188+
HAVING COUNT(DISTINCT result) > 1;
189+
GO
190+
~~START~~
191+
varchar#!#varchar#!#varchar#!#int
192+
DATEFIRST#!#2025-01-05#!#week#!#2
193+
~~END~~
194+
195+
157196
-- Error Scenarios
158197
-- Unsupported date time datatype inputs
159198
SELECT datename(TZOFFSET, CAST('2025-06-03 14:30:15 +01:30' AS DATE)) AS date_type;
160199
GO
200+
~~START~~
201+
text
161202
~~ERROR (Code: 33557097)~~
162203

163204
~~ERROR (Message: The datepart tzoffset is not supported by date function datename for data type date.)~~
164205

165206

166207
SELECT datename(TZOFFSET, CAST('14:30:15' AS TIME)) AS time_type;
167208
GO
209+
~~START~~
210+
text
168211
~~ERROR (Code: 33557097)~~
169212

170213
~~ERROR (Message: The datepart tzoffset is not supported by date function datename for data type time without time zone.)~~
171214

172215

173216
SELECT datename(TZOFFSET, CAST('2025-06-03 14:30:15' AS DATETIME)) AS datetime_type;
174217
GO
218+
~~START~~
219+
text
175220
~~ERROR (Code: 33557097)~~
176221

177222
~~ERROR (Message: The datepart tzoffset is not supported by date function datename for data type datetime.)~~
178223

179224

180225
SELECT datename(TZOFFSET, CAST('2025-06-03 14:30:15' AS SMALLDATETIME)) AS smalldatetime_type;
181226
GO
227+
~~START~~
228+
text
182229
~~ERROR (Code: 33557097)~~
183230

184231
~~ERROR (Message: The datepart tzoffset is not supported by date function datename for data type smalldatetime.)~~
@@ -188,6 +235,8 @@ GO
188235
-- Invalid date time inputs
189236
SELECT DATENAME(TZOFFSET, '2025-06-0314:30:15+01:59');
190237
GO
238+
~~START~~
239+
text
191240
~~ERROR (Code: 33557097)~~
192241

193242
~~ERROR (Message: Conversion failed when converting date and/or time from character string.)~~

test/JDBC/expected/datepart-before-16_10-or-17_6-vu-cleanup.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@ GO
1919
DROP PROCEDURE date_part_vu_prepare_sys_day_proc
2020
GO
2121

22-
DROP VIEW date_part_vu_prepare_ResultsView;
23-
GO
24-
2522
DROP TABLE date_part_vu_prepare_TestDates, date_part_vu_prepare_TestTimezones, date_part_vu_prepare_TestResults, date_part_vu_prepare_DateParts;
2623
GO

test/JDBC/expected/datepart-before-16_10-or-17_6-vu-prepare.out

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,11 +1396,6 @@ Africa/Nairobi
13961396
~~ROW COUNT: 1~~
13971397

13981398

1399-
-- Create view to store DATEPART results
1400-
CREATE VIEW date_part_vu_prepare_ResultsView AS
1401-
SELECT * FROM date_part_vu_prepare_TestResults
1402-
ORDER BY DataType, InputDate, TimeZone, DatePart;
1403-
GO
14041399

14051400
-- Reset Timezone
14061401
SELECT set_config('timezone', 'UTC', false)

test/JDBC/expected/datepart-before-16_10-or-17_6-vu-verify.out

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ int
507507

508508

509509
-- Analyze results
510-
SELECT * from date_part_vu_prepare_ResultsView;
510+
SELECT * from date_part_vu_prepare_TestResults
511+
ORDER BY DataType, InputDate, TimeZone, DatePart;
511512
GO
512513
~~START~~
513514
varchar#!#varchar#!#varchar#!#varchar#!#varchar#!#sql_variant#!#nvarchar
@@ -1121,7 +1122,7 @@ SELECT
11211122
DatePart,
11221123
COUNT(DISTINCT DatePartValue) AS UniqueDatePartValues,
11231124
COUNT(DISTINCT DateName) AS UniqueDateNames
1124-
FROM date_part_vu_prepare_ResultsView
1125+
FROM date_part_vu_prepare_TestResults
11251126
GROUP BY DataType, InputDate, DatePart
11261127
HAVING
11271128
COUNT(DISTINCT DatePartValue) > 1 OR

test/JDBC/expected/non_default_server_collation/chinese_prc_ci_as/babel_function.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,8 @@ text
12161216
-- test invalid argument, expect error
12171217
select datename(invalid_interval, cast('2016-12-26 23:30:05.523456' as date));
12181218
GO
1219+
~~START~~
1220+
text
12191221
~~ERROR (Code: 33557097)~~
12201222

12211223
~~ERROR (Message: 'invalid_interval' is not a recognized datepart option)~~

0 commit comments

Comments
 (0)