Skip to content

Commit f7f2594

Browse files
author
Vincent Russell
committed
fix date parsing bug
1 parent 545c7e1 commit f7f2594

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,12 @@ more results? (y/n): n
712712

713713
# Change Log
714714

715+
## [1.23](https://github.com/vincentrussell/sql-to-mongo-db-query-converter/tree/sql-to-mongo-db-query-converter-1.23) (2025-11-17)
716+
717+
**Bugs:**
718+
719+
- Throw an exception if dates in queries are passed in with the following invalid formats: YYYY-MM, YYYY
720+
715721
## [1.22](https://github.com/vincentrussell/sql-to-mongo-db-query-converter/tree/sql-to-mongo-db-query-converter-1.22) (2024-04-09)
716722

717723
**Bugs:**

src/main/java/com/github/vincentrussell/query/mongodb/sql/converter/util/SqlUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public final class SqlUtils {
7373
YY_MM_DDFORMATTER,
7474
YYMMDDFORMATTER));
7575

76+
private static final Collection<Pattern> BAD_DATE_REGEXES = Collections.unmodifiableList(Arrays.asList(
77+
Pattern.compile("^\\d{4}-\\d{2}$"), Pattern.compile("^\\d{4}$")));
78+
7679
private static final Character NEGATIVE_NUMBER_SIGN = Character.valueOf('-');
7780

7881
private SqlUtils() {
@@ -322,14 +325,19 @@ public static Object getObjectAsDate(final Object value) throws ParseException {
322325
//noop
323326
}
324327
}
328+
for (Pattern pattern : BAD_DATE_REGEXES) {
329+
if (pattern.matcher((String) value).matches()) {
330+
throw new ParseException("could not convert " + value + " to a valid date");
331+
}
332+
}
325333
try {
326334
return parseNaturalLanguageDate((String) value);
327335
} catch (Exception e) {
328336
//noop
329337
}
330338

331339
}
332-
throw new ParseException("could not convert " + value + " to a date");
340+
throw new ParseException("could not convert " + value + " to a valid date");
333341
}
334342

335343
/**

src/test/java/com/github/vincentrussell/query/mongodb/sql/converter/QueryConverterTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private void withTimeZone(TimeZone timeZone, MyRunnable runnable) throws Excepti
389389
@SuppressWarnings("unchecked")
390390
public void selectAllFromTableWithSimpleWhereClauseLongOverrideWitUnparseableNaturalDateGT() throws ParseException {
391391
expectedException.expect(ParseException.class);
392-
expectedException.expectMessage(containsString("could not convert who cares to a date"));
392+
expectedException.expectMessage(containsString("could not convert who cares to a valid date"));
393393
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select * from my_table where value > \"who cares\"").fieldNameToFieldTypeMapping(new HashMap(){{
394394
put("value",FieldType.DATE);
395395
}}).build();
@@ -3100,6 +3100,17 @@ public void testAvgSelectWithHavingClause() throws ParseException, IOException {
31003100
" }\n" +
31013101
"}])",byteArrayOutputStream.toString("UTF-8"));
31023102
}
3103+
3104+
@Test
3105+
@SuppressWarnings("unchecked")
3106+
public void dateFormatYYYYMM() throws ParseException {
3107+
expectedException.expect(ParseException.class);
3108+
expectedException.expectMessage(containsString("could not convert 2013-07 to a valid date"));
3109+
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select * from my_table where value > \"2013-07\"")
3110+
.fieldNameToFieldTypeMapping(new HashMap(){{
3111+
put("value",FieldType.DATE);
3112+
}}).build();
3113+
}
31033114

31043115
private static Document document(String key, Object... values) {
31053116
Document document = new Document();

0 commit comments

Comments
 (0)