Bug #72859 string_to_date and NULL gives stackoverflow error
Submitted: 3 Jun 2014 22:28 Modified: 17 Jul 2014 10:18
Reporter: Harpreet Sohal Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.5.34 OS:Any
Assigned to: Assigned Account CPU Architecture:Any

[3 Jun 2014 22:28] Harpreet Sohal
Description:
string_to_date and NULL gives stackoverflow error

How to repeat:
Select CASE WHEN length(testDt) > 0 THEN STR_TO_DATE(testDt, '%Y-%m-%d') ELSE NULL END as testDt from tablename 

fails with java.lang.StackOverflowError on mysql version 5.5.34 when accessed through jdbc. MySQL version 5.6 works fine through jdbc 

- java.lang.StackOverflowError
	at com.mysql.jdbc.ResultSetImpl.getDefaultTimeZone(ResultSetImpl.java:2404)
	at com.mysql.jdbc.ResultSetImpl.getNativeDate(ResultSetImpl.java:3943)
	at com.mysql.jdbc.ResultSetImpl.getNativeDate(ResultSetImpl.java:3905)
	at com.mysql.jdbc.ResultSetImpl.getNativeConvertToString(ResultSetImpl.java:3831)
	at com.mysql.jdbc.ResultSetImpl.getNativeString(ResultSetImpl.java:4627)
	at com.mysql.jdbc.ResultSetImpl.getNativeDateViaParseConversion(ResultSetImpl.java:3976)
	at com.mysql.jdbc.ResultSetRow.getNativeDateTimeValue(ResultSetRow.java:510)
	at com.mysql.jdbc.BufferRow.getNativeDateTimeValue(BufferRow.java:741)
	at com.mysql.jdbc.ResultSetImpl.getNativeDate(ResultSetImpl.java:3945)
	at com.mysql.jdbc.ResultSetImpl.getNativeDate(ResultSetImpl.java:3905)
	at com.mysql.jdbc.ResultSetImpl.getNativeConvertToString(ResultSetImpl.java:3831)
	at com.mysql.jdbc.ResultSetImpl.getNativeString(ResultSetImpl.java:4627)
	at com.mysql.jdbc.ResultSetImpl.getNativeDateViaParseConversion(ResultSetImpl.java:3976)
	at com.mysql.jdbc.ResultSetRow.getNativeDateTimeValue(ResultSetRow.java:510)
	at com.mysql.jdbc.BufferRow.getNativeDateTimeValue(BufferRow.java:741)
	at com.mysql.jdbc.ResultSetImpl.getNativeDate(ResultSetImpl.java:3945)
	at com.mysql.jdbc.ResultSetImpl.getNativeDate(ResultSetImpl.java:3905)

Suggested fix:
Replacing NULL with empty strings ' ' works Select CASE WHEN length(testDt) > 0 THEN STR_TO_DATE(testDt, '%Y-%m-%d') ELSE ' 'END as testDt from tablename
[3 Jun 2014 23:00] Harpreet Sohal
The error get reproduced with three or more case statements in select statement

CASE WHEN length(testDt) > 0 THEN STR_TO_DATE(testDt, '%Y-%m-%d') ELSE NULL END
[4 Jun 2014 15:36] Todd Farmer
Hello Harpreet,

Thank you for the bug report and the stack trace.  I tried to reproduce the reported problem using the following code:

		c.createStatement().execute("SET @testDt = NULL");
		ResultSet rs = c.createStatement().executeQuery("SELECT CASE WHEN length(@testDt) > 0 THEN STR_TO_DATE(@testDt, '%Y-%m-%d') ELSE NULL END");
		while(rs.next()){
			rs.getDate(1);
		}

I was unable to trigger the StackOverflowError, on either 5.5.34 or 5.6.13.  I recognize that your SELECT statement likely actually references a table.  After inspecting the Connector/J code, it seems the most likely explanation is that the server is returning a null field value, but not flagging it as null in the metadata.  As such, I'll need more context, such as the table definition used, to reproduce this effectively - obviously, my simplified test case using a user variable doesn't trigger the same server behavior.
[10 Jun 2014 1:35] Filipe Silva
Hi Harpreet,

I have been analyzing this bug report and I've found something that may be related with this issue, however I still can't reproduce the exact behavior you reported.

I want to pursue this further but for that I need a little more information:
- The Connector/J version.
- The Java version.
- A minimal test case with the MySQL connection string from your code.

From what I've seen, you should be able to reproduce this problem by running the sample code that Todd put here before, only replacing the variable initialization to something like: conn.createStatement().executeQuery("SET @testDt='2014-06-09'");

Thank you,
[12 Jun 2014 0:05] Harpreet Sohal
mysql-connector-java-5.1.18

java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
[12 Jun 2014 9:18] Filipe Silva
Hi Harpreet,

Thank you, but I still need the Connection String (no passwords) and a sample code or at least the snippet from establishing the connection to fetching the results from the ResultSet returned.
[12 Jun 2014 21:33] Harpreet Sohal
getConnection(\"jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true&useCursorFetch=true&jdbcCompliantTruncation=false&useUnicode=true&characterEncoding=UTF-8&defaultFetchSize=100\")\
[17 Jun 2014 10:18] Filipe Silva
Hi Harpreet,

I'm still unable to reproduce this. Can you provide a sample test code with a smaller query? I'll also need to check your server configurations file to try to match you settings in my environment.

Thank you,
[18 Jul 2014 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".