@@ -62,6 +62,33 @@ private Literal timestampLiteral(String date) {
6262 return (Literal ) exp ;
6363 }
6464
65+ private String buildDate () {
66+ StringBuilder sb = new StringBuilder ();
67+ int length = randomIntBetween (4 , 9 );
68+
69+ if (randomBoolean ()) {
70+ sb .append ('-' );
71+ } else {
72+ if (length > 4 ) {
73+ sb .append ('-' );
74+ }
75+ }
76+
77+ for (int i = 1 ; i <= length ; i ++) {
78+ sb .append (i );
79+ }
80+ sb .append ("-05-10" );
81+ return sb .toString ();
82+ }
83+
84+ private String buildSecsAndFractional () {
85+ if (randomBoolean ()) {
86+ return ":55" + randomFrom ("" , ".1" , ".12" , ".123" , ".1234" , ".12345" , ".123456" ,
87+ ".1234567" , ".12345678" , ".123456789" );
88+ }
89+ return "" ;
90+ }
91+
6592 private Literal guidLiteral (String guid ) {
6693 Expression exp = parser .createExpression (buildExpression ("guid" , "'%s'" , guid ));
6794 assertThat (exp , instanceOf (Expression .class ));
@@ -185,7 +212,7 @@ public void testFunctionWithFunctionWithArgAndParams() {
185212 }
186213
187214 public void testDateLiteral () {
188- Literal l = dateLiteral ("2012-01-01" );
215+ Literal l = dateLiteral (buildDate () );
189216 assertThat (l .dataType (), is (DATE ));
190217 }
191218
@@ -197,7 +224,7 @@ public void testDateLiteralValidation() {
197224 }
198225
199226 public void testTimeLiteral () {
200- Literal l = timeLiteral ("12:23:56" );
227+ Literal l = timeLiteral ("12:23" + buildSecsAndFractional () );
201228 assertThat (l .dataType (), is (TIME ));
202229 }
203230
@@ -209,14 +236,27 @@ public void testTimeLiteralValidation() {
209236 }
210237
211238 public void testTimestampLiteral () {
212- Literal l = timestampLiteral ("2012-01-01 10:01:02.3456" );
239+ Literal l = timestampLiteral (buildDate () + " 10:20" + buildSecsAndFractional ());
240+ assertThat (l .dataType (), is (DATETIME ));
241+ l = timestampLiteral (buildDate () + "T11:22" + buildSecsAndFractional ());
213242 assertThat (l .dataType (), is (DATETIME ));
214243 }
215244
216245 public void testTimestampLiteralValidation () {
217- ParsingException ex = expectThrows (ParsingException .class , () -> timestampLiteral ("2012-01-01T10:01:02.3456" ));
246+ String date = buildDate ();
247+ ParsingException ex = expectThrows (ParsingException .class , () -> timestampLiteral (date + "_AB 10:01:02.3456" ));
248+ assertEquals (
249+ "line 1:2: Invalid timestamp received; Text '" + date + "_AB 10:01:02.3456' could not be parsed at index " +
250+ date .length (),
251+ ex .getMessage ());
252+ ex = expectThrows (ParsingException .class , () -> timestampLiteral ("20120101_AB 10:01:02.3456" ));
253+ assertEquals (
254+ "line 1:2: Invalid timestamp received; Text '20120101_AB 10:01:02.3456' could not be parsed at index 0" ,
255+ ex .getMessage ());
256+
257+ ex = expectThrows (ParsingException .class , () -> timestampLiteral (date ));
218258 assertEquals (
219- "line 1:2: Invalid timestamp received; Text '2012-01-01T10:01:02.3456 ' could not be parsed at index 10" ,
259+ "line 1:2: Invalid timestamp received; Text '" + date + " ' could not be parsed at index " + date . length () ,
220260 ex .getMessage ());
221261 }
222262
0 commit comments