Bug #82156 Integer data type COLUMN_SIZE and getPrecision() values are different
Submitted: 7 Jul 2016 21:39 Modified: 24 Mar 21:58
Reporter: Lisa Cabrera Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.39 OS:Any
Assigned to: CPU Architecture:Any

[7 Jul 2016 21:39] Lisa Cabrera
Description:
When retrieving column information about INT column types, the column size returned by DatabaseMetaData.getColumns(...).getString("COLUMN_SIZE") is always set to "10", regardless of the actual INT precision specified when the column was created.

How to repeat:
Create a table with an INT data type:
CREATE TABLE myTable (col_int INT(5));

Here is some Java code to reproduce the issue:
---------------------------------------------
public void printJavaDataTypes(String tableName) throws SQLException
{
	ResultSet rs = stmt.executeQuery("SELECT col_int from "+tableName);
	ResultSetMetaData rsmd = rs.getMetaData();
	
	for (int i = 1; i <= rsmd.getColumnCount(); i++)
	{
	   System.out.println("Name="+rsmd.getColumnName(i));
	   System.out.println("Type="+rsmd.getColumnType(i));
	   System.out.println("Precision="+rsmd.getPrecision(i));
	   System.out.println("Scale="+rsmd.getScale(i));
	   System.out.println("ColumnClassName="+rsmd.getColumnClassName(i));
	   System.out.println("ColumnType="+String.valueOf(rsmd.getColumnType(i)));
	}
	
	DatabaseMetaData dm = stmt.getConnection().getMetaData();
	ResultSet rsmd2 = dm.getColumns(null, null, tableName, "col_int");

	while(rsmd2.next())
	{
	   System.out.println("COLUMN_NAME="+rsmd2.getString("COLUMN_NAME"));
	   System.out.println("DATA_TYPE="+rsmd2.getString("DATA_TYPE"));
	   System.out.println("COLUMN_SIZE="+rsmd2.getString("COLUMN_SIZE"));
	   System.out.println("DECIMAL_DIGITS="+rsmd2.getString("DECIMAL_DIGITS"));
	   System.out.println("TYPE_NAME="+String.valueOf(rsmd2.getString("TYPE_NAME")));
	}
}
---------------------------------------------

Here's the output of the above code:
---------------------------------------------
Name=col_int
Type=4
Precision=5
Scale=0
ColumnClassName=java.lang.Integer
ColumnType=4

COLUMN_NAME=col_int
DATA_TYPE=4
COLUMN_SIZE=10
DECIMAL_DIGITS=0
TYPE_NAME=INT
---------------------------------------------

Output for table :: CREATE TABLE myTable (col_int INT);
---------------------------------------------
Name=col_int
Type=4
Precision=11
Scale=0
ColumnClassName=java.lang.Integer
ColumnType=4

COLUMN_NAME=col_int
DATA_TYPE=4
COLUMN_SIZE=10
DECIMAL_DIGITS=0
TYPE_NAME=INT
---------------------------------------------

Suggested fix:
COLUMN_SIZE should return the same value as JDBC ResultSetMetaData.getPrecision(int column) method.
[11 Jul 2016 5:47] Chiranjeevi Battula
Hello Lisa Cabrera,

Thank you for the bug report and test case.
Verified this behavior on MySQL Connector / J 5.1.39.

Thanks,
Chiranjeevi.
[11 Jul 2016 5:47] Chiranjeevi Battula
run:
Name=col_int
Type=4
Precision=5
Scale=0
ColumnClassName=java.lang.Integer
ColumnType=4
COLUMN_NAME=col_int
DATA_TYPE=4
COLUMN_SIZE=10
DECIMAL_DIGITS=0
TYPE_NAME=INT
BUILD SUCCESSFUL (total time: 0 seconds)
[12 Jul 2016 21:36] Filipe Silva
Just to note that the fix will make ResultSetMetaData.getPrecision(int column) return the same value as from DBMD.getColumns()->COLUMN_SIZE and not the opposite.
This is so because both methods should return the maximum precision for the type and not its display size, as explained in other bug reports, such as Bug#64069 and Bug#38171.
[13 Jul 2016 1:13] Lisa Cabrera
That makes sense. Also see related defect Bug#82206.
[22 Jul 2023 14:47] Ritika Verma
Is this bug solved?
[24 Mar 21:58] Filipe Silva
Fixed in Connector/J 8.0.19 by Bug#97413.