Skip to content

Commit 6fa1d6a

Browse files
committed
Parameterize some tests.
1 parent f0c911b commit 6fa1d6a

File tree

2 files changed

+87
-141
lines changed

2 files changed

+87
-141
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -110,114 +110,96 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
110110

111111
test("cast string to timestamp") {
112112
for (tz <- ALL_TIMEZONES) {
113-
val timeZoneId = Option(tz.getID)
114-
checkEvaluation(cast(Literal("123"), TimestampType, timeZoneId), null)
113+
def checkCastStringToTimestamp(str: String, expected: Timestamp): Unit = {
114+
checkEvaluation(cast(Literal(str), TimestampType, Option(tz.getID)), expected)
115+
}
116+
117+
checkCastStringToTimestamp("123", null)
115118

116119
var c = Calendar.getInstance(tz)
117120
c.set(2015, 0, 1, 0, 0, 0)
118121
c.set(Calendar.MILLISECOND, 0)
119-
checkEvaluation(cast(Literal("2015"), TimestampType, timeZoneId),
120-
new Timestamp(c.getTimeInMillis))
122+
checkCastStringToTimestamp("2015", new Timestamp(c.getTimeInMillis))
121123
c = Calendar.getInstance(tz)
122124
c.set(2015, 2, 1, 0, 0, 0)
123125
c.set(Calendar.MILLISECOND, 0)
124-
checkEvaluation(cast(Literal("2015-03"), TimestampType, timeZoneId),
125-
new Timestamp(c.getTimeInMillis))
126+
checkCastStringToTimestamp("2015-03", new Timestamp(c.getTimeInMillis))
126127
c = Calendar.getInstance(tz)
127128
c.set(2015, 2, 18, 0, 0, 0)
128129
c.set(Calendar.MILLISECOND, 0)
129-
checkEvaluation(cast(Literal("2015-03-18"), TimestampType, timeZoneId),
130-
new Timestamp(c.getTimeInMillis))
131-
checkEvaluation(cast(Literal("2015-03-18 "), TimestampType, timeZoneId),
132-
new Timestamp(c.getTimeInMillis))
133-
checkEvaluation(cast(Literal("2015-03-18T"), TimestampType, timeZoneId),
134-
new Timestamp(c.getTimeInMillis))
130+
checkCastStringToTimestamp("2015-03-18", new Timestamp(c.getTimeInMillis))
131+
checkCastStringToTimestamp("2015-03-18 ", new Timestamp(c.getTimeInMillis))
132+
checkCastStringToTimestamp("2015-03-18T", new Timestamp(c.getTimeInMillis))
135133

136134
c = Calendar.getInstance(tz)
137135
c.set(2015, 2, 18, 12, 3, 17)
138136
c.set(Calendar.MILLISECOND, 0)
139-
checkEvaluation(cast(Literal("2015-03-18 12:03:17"), TimestampType, timeZoneId),
140-
new Timestamp(c.getTimeInMillis))
141-
checkEvaluation(cast(Literal("2015-03-18T12:03:17"), TimestampType, timeZoneId),
142-
new Timestamp(c.getTimeInMillis))
137+
checkCastStringToTimestamp("2015-03-18 12:03:17", new Timestamp(c.getTimeInMillis))
138+
checkCastStringToTimestamp("2015-03-18T12:03:17", new Timestamp(c.getTimeInMillis))
143139

144140
// If the string value includes timezone string, it represents the timestamp string
145141
// in the timezone regardless of the timeZoneId parameter.
146142
c = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
147143
c.set(2015, 2, 18, 12, 3, 17)
148144
c.set(Calendar.MILLISECOND, 0)
149-
checkEvaluation(cast(Literal("2015-03-18T12:03:17Z"), TimestampType, timeZoneId),
150-
new Timestamp(c.getTimeInMillis))
151-
checkEvaluation(cast(Literal("2015-03-18 12:03:17Z"), TimestampType, timeZoneId),
152-
new Timestamp(c.getTimeInMillis))
145+
checkCastStringToTimestamp("2015-03-18T12:03:17Z", new Timestamp(c.getTimeInMillis))
146+
checkCastStringToTimestamp("2015-03-18 12:03:17Z", new Timestamp(c.getTimeInMillis))
153147

154148
c = Calendar.getInstance(TimeZone.getTimeZone("GMT-01:00"))
155149
c.set(2015, 2, 18, 12, 3, 17)
156150
c.set(Calendar.MILLISECOND, 0)
157-
checkEvaluation(cast(Literal("2015-03-18T12:03:17-1:0"), TimestampType, timeZoneId),
158-
new Timestamp(c.getTimeInMillis))
159-
checkEvaluation(cast(Literal("2015-03-18T12:03:17-01:00"), TimestampType, timeZoneId),
160-
new Timestamp(c.getTimeInMillis))
151+
checkCastStringToTimestamp("2015-03-18T12:03:17-1:0", new Timestamp(c.getTimeInMillis))
152+
checkCastStringToTimestamp("2015-03-18T12:03:17-01:00", new Timestamp(c.getTimeInMillis))
161153

162154
c = Calendar.getInstance(TimeZone.getTimeZone("GMT+07:30"))
163155
c.set(2015, 2, 18, 12, 3, 17)
164156
c.set(Calendar.MILLISECOND, 0)
165-
checkEvaluation(cast(Literal("2015-03-18T12:03:17+07:30"), TimestampType, timeZoneId),
166-
new Timestamp(c.getTimeInMillis))
157+
checkCastStringToTimestamp("2015-03-18T12:03:17+07:30", new Timestamp(c.getTimeInMillis))
167158

168159
c = Calendar.getInstance(TimeZone.getTimeZone("GMT+07:03"))
169160
c.set(2015, 2, 18, 12, 3, 17)
170161
c.set(Calendar.MILLISECOND, 0)
171-
checkEvaluation(cast(Literal("2015-03-18T12:03:17+7:3"), TimestampType, timeZoneId),
172-
new Timestamp(c.getTimeInMillis))
162+
checkCastStringToTimestamp("2015-03-18T12:03:17+7:3", new Timestamp(c.getTimeInMillis))
173163

174164
// tests for the string including milliseconds.
175165
c = Calendar.getInstance(tz)
176166
c.set(2015, 2, 18, 12, 3, 17)
177167
c.set(Calendar.MILLISECOND, 123)
178-
checkEvaluation(cast(Literal("2015-03-18 12:03:17.123"), TimestampType, timeZoneId),
179-
new Timestamp(c.getTimeInMillis))
180-
checkEvaluation(cast(Literal("2015-03-18T12:03:17.123"), TimestampType, timeZoneId),
181-
new Timestamp(c.getTimeInMillis))
168+
checkCastStringToTimestamp("2015-03-18 12:03:17.123", new Timestamp(c.getTimeInMillis))
169+
checkCastStringToTimestamp("2015-03-18T12:03:17.123", new Timestamp(c.getTimeInMillis))
182170

183171
// If the string value includes timezone string, it represents the timestamp string
184172
// in the timezone regardless of the timeZoneId parameter.
185173
c = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
186174
c.set(2015, 2, 18, 12, 3, 17)
187175
c.set(Calendar.MILLISECOND, 456)
188-
checkEvaluation(cast(Literal("2015-03-18T12:03:17.456Z"), TimestampType, timeZoneId),
189-
new Timestamp(c.getTimeInMillis))
190-
checkEvaluation(cast(Literal("2015-03-18 12:03:17.456Z"), TimestampType, timeZoneId),
191-
new Timestamp(c.getTimeInMillis))
176+
checkCastStringToTimestamp("2015-03-18T12:03:17.456Z", new Timestamp(c.getTimeInMillis))
177+
checkCastStringToTimestamp("2015-03-18 12:03:17.456Z", new Timestamp(c.getTimeInMillis))
192178

193179
c = Calendar.getInstance(TimeZone.getTimeZone("GMT-01:00"))
194180
c.set(2015, 2, 18, 12, 3, 17)
195181
c.set(Calendar.MILLISECOND, 123)
196-
checkEvaluation(cast(Literal("2015-03-18T12:03:17.123-1:0"), TimestampType, timeZoneId),
197-
new Timestamp(c.getTimeInMillis))
198-
checkEvaluation(cast(Literal("2015-03-18T12:03:17.123-01:00"), TimestampType, timeZoneId),
199-
new Timestamp(c.getTimeInMillis))
182+
checkCastStringToTimestamp("2015-03-18T12:03:17.123-1:0", new Timestamp(c.getTimeInMillis))
183+
checkCastStringToTimestamp("2015-03-18T12:03:17.123-01:00", new Timestamp(c.getTimeInMillis))
200184

201185
c = Calendar.getInstance(TimeZone.getTimeZone("GMT+07:30"))
202186
c.set(2015, 2, 18, 12, 3, 17)
203187
c.set(Calendar.MILLISECOND, 123)
204-
checkEvaluation(cast(Literal("2015-03-18T12:03:17.123+07:30"), TimestampType, timeZoneId),
205-
new Timestamp(c.getTimeInMillis))
188+
checkCastStringToTimestamp("2015-03-18T12:03:17.123+07:30", new Timestamp(c.getTimeInMillis))
206189

207190
c = Calendar.getInstance(TimeZone.getTimeZone("GMT+07:03"))
208191
c.set(2015, 2, 18, 12, 3, 17)
209192
c.set(Calendar.MILLISECOND, 123)
210-
checkEvaluation(cast(Literal("2015-03-18T12:03:17.123+7:3"), TimestampType, timeZoneId),
211-
new Timestamp(c.getTimeInMillis))
212-
213-
checkEvaluation(cast(Literal("2015-03-18 123142"), TimestampType, timeZoneId), null)
214-
checkEvaluation(cast(Literal("2015-03-18T123123"), TimestampType, timeZoneId), null)
215-
checkEvaluation(cast(Literal("2015-03-18X"), TimestampType, timeZoneId), null)
216-
checkEvaluation(cast(Literal("2015/03/18"), TimestampType, timeZoneId), null)
217-
checkEvaluation(cast(Literal("2015.03.18"), TimestampType, timeZoneId), null)
218-
checkEvaluation(cast(Literal("20150318"), TimestampType, timeZoneId), null)
219-
checkEvaluation(cast(Literal("2015-031-8"), TimestampType, timeZoneId), null)
220-
checkEvaluation(cast(Literal("2015-03-18T12:03:17-0:70"), TimestampType, timeZoneId), null)
193+
checkCastStringToTimestamp("2015-03-18T12:03:17.123+7:3", new Timestamp(c.getTimeInMillis))
194+
195+
checkCastStringToTimestamp("2015-03-18 123142", null)
196+
checkCastStringToTimestamp("2015-03-18T123123", null)
197+
checkCastStringToTimestamp("2015-03-18X", null)
198+
checkCastStringToTimestamp("2015/03/18", null)
199+
checkCastStringToTimestamp("2015.03.18", null)
200+
checkCastStringToTimestamp("20150318", null)
201+
checkCastStringToTimestamp("2015-031-8", null)
202+
checkCastStringToTimestamp("2015-03-18T12:03:17-0:70", null)
221203
}
222204
}
223205

0 commit comments

Comments
 (0)