Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TMMWC-0003eE-ME for pgsql-jdbc@postgresql.org; Thu, 11 Oct 2012 17:25:57 +0000 Received: from mail-lb0-f174.google.com ([209.85.217.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TMMW8-0004SY-Ob for pgsql-jdbc@postgresql.org; Thu, 11 Oct 2012 17:25:56 +0000 Received: by mail-lb0-f174.google.com with SMTP id n3so1476700lbo.19 for ; Thu, 11 Oct 2012 10:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:subject:message-id:date:to:mime-version:x-mailer; bh=MmecQisVdN3ygnVXtpHKy8aYXH5G27KRQd//xWAmnXI=; b=xY/J5OM7fWl4uaPedQ8NAlxmBGfwX2YP/o5RHlHGKvNYIku0kMMXzIqib9R9AUyHs/ 2TIpkxgrX8zGAdVL3lqmRg64a1HCAP7BIJvPwvRKkDXiUxdVdrcl+bjSXOENlWxZeVKB uENvNzv5p/Wrw4AvwgVJJQXKeSV+JVykkVgS5tYOfpuoeTD59psN0alkGfZ4WEzSVBf/ ozydrLDDxS88N/sUZQLOVP2rLzU1tfJvOdI5b71UWozn+6NSf1TmBYeNmBraTBqrSlRh rHBozF/5vUY7AlMV5AgtWoxHINZdrfOL7NHMW64d8pkNMNn0ky5OMF40UFqt8sgadtsD 6Ysw== Received: by 10.112.87.162 with SMTP id az2mr733284lbb.42.1349976350923; Thu, 11 Oct 2012 10:25:50 -0700 (PDT) Received: from [10.0.0.9] (15.80-202-62.nextgentel.com. [80.202.62.15]) by mx.google.com with ESMTPS id jk8sm1539294lab.7.2012.10.11.10.25.49 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 11 Oct 2012 10:25:50 -0700 (PDT) From: =?iso-8859-1?Q?Thor_Michael_St=F8re?= Content-Type: multipart/alternative; boundary="Apple-Mail=_9DB502A6-D712-4F05-BA11-B33A8614B9EA" Subject: ResultSetMetaData.isNullable(i) and outer joined columns Message-Id: <63DBC81F-2AB0-4C02-AC08-2B05C31FFCA6@gmail.com> Date: Thu, 11 Oct 2012 19:25:48 +0200 To: pgsql-jdbc@postgresql.org Mime-Version: 1.0 (Mac OS X Mail 6.1 \(1498\)) X-Mailer: Apple Mail (2.1498) X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/48 X-Sequence-Number: 23372 --Apple-Mail=_9DB502A6-D712-4F05-BA11-B33A8614B9EA Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=iso-8859-1 Hello, I've just noticed that ResultSetMetaData.isNullable(i) in the PostgreSQL = JDBC driver doesn't reflect whether fields of result set column i may be = null, but the nullability of the table column it's selected from. These = may differ for result set columns that originate from a outer joined = table, in which case fields may of course be null even if the source = table column is not nullable, but in such cases isNullable still = indicates the column is not nullable. Surely this isn't right? = http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#i= sNullable(int) Although the spec doesn't explicitly distinguish between result set and = source table only the former seems sensible to me -- what I expect the = isNullable return value to reflect is whether an invocation of the = ResultSet.getXXX(i) methods always returns an object (or sensible = primitive), if they may return null or if that's unknown. Test case: package tmdbctest; import java.sql.*; /* CREATE TABLE employee ( id SERIAL PRIMARY KEY, name TEXT UNIQUE, salary NUMERIC, hire_date TIMESTAMP, fire_date TIMESTAMP, picture BYTEA ) WITHOUT OIDS; INSERT INTO employee ( name, salary, hire_date, fire_date ) VALUES ( 'Foo Johnson', 50, '2007-02-05 09:00:00', null ), ( 'Bar Mannson', 60, '2007-08-02 09:00:00', null ), ( 'Bruce Woolloomooloo', 70, '2007-08-15 08:00:00', '2008-06-09 = 15:00:00' ), ( 'Janus Larsen', 65, '2006-08-15 08:30:00', '2008-03-14 16:30:00' = ); CREATE TABLE worklog ( id SERIAL PRIMARY KEY, employee_id INT4 REFERENCES employee(id) DEFERRABLE NOT NULL, workday DATE NOT NULL, hours_worked NUMERIC NOT NULL ) WITHOUT OIDS; INSERT INTO worklog ( employee_id, workday, hours_worked ) VALUES ( 1, '2007-10-15', 5 ), ( 1, '2007-10-16', 6 ), ( 1, '2007-10-17', 7 ), ( 1, '2007-10-18', 8 ), ( 2, '2007-10-16', 2 ), ( 2, '2007-10-17', 7 ), ( 2, '2007-10-18', 3 ), ( 2, '2007-10-19', 5 ); */ public class Testcase { static final String SQL_QUERY =3D "SELECT employee.id, name, salary, worklog.id AS worklog_id, = workday, hours_worked\n"+ " FROM employee\n"+ " LEFT JOIN worklog ON ( employee.id =3D = worklog.employee_id )"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection( = "jdbc:postgresql://localhost/postgres?user=3Dtmdbc_sample&password=3Dtmdbc= 2008"); } public static void main(String[] args) throws Exception { Connection conn =3D getConnection(); PreparedStatement stmnt =3D conn.prepareStatement(SQL_QUERY); ResultSetMetaData rsmd =3D stmnt.getMetaData(); int columnCount =3D rsmd.getColumnCount(); System.out.println( "Metadata:" ); for( int i =3D 1; i <=3D columnCount; i++ ){ System.out.println( rsmd.getColumnLabel(i) +": "+ = parseNullable( rsmd.isNullable(i) ) ); } ResultSet rs =3D stmnt.executeQuery(); System.out.println( "Data:" ); for( int i =3D 1; i <=3D columnCount; i++ ){ if( i !=3D 1 ) System.out.print( "," ); System.out.print( rsmd.getColumnLabel(i) ); } System.out.println(); while( rs.next() ){ for( int i =3D 1; i <=3D columnCount; i++ ){ if( i !=3D 1 ) System.out.print( "," ); System.out.print( rs.getString(i) ); } System.out.println(); } } public static String parseNullable(int i){ switch(i){ case ResultSetMetaData.columnNoNulls: return "NoNulls"; case ResultSetMetaData.columnNullable: return "Nullable"; case ResultSetMetaData.columnNullableUnknown: return = "NullableUnknown"; default: throw new IllegalArgumentException(); } } } Output: Metadata: id: NoNulls name: NoNulls salary: Nullable worklog_id: NoNulls workday: NoNulls hours_worked: NoNulls Data: id,name,salary,worklog_id,workday,hours_worked 1,Foo Johnson,50.00,1,2007-10-15,5 1,Foo Johnson,50.00,2,2007-10-16,6 1,Foo Johnson,50.00,3,2007-10-17,7 1,Foo Johnson,50.00,4,2007-10-18,8 2,Bar Mannson,60.00,5,2007-10-16,2 2,Bar Mannson,60.00,6,2007-10-17,7 2,Bar Mannson,60.00,7,2007-10-18,3 2,Bar Mannson,60.00,8,2007-10-19,5 4,Janus Larsen,65.00,null,null,null 3,Bruce Woolloomooloo,70.00,null,null,null Tested with postgresql-9.1-903.jdbc4 Thanks, Thor Michael St=F8re --Apple-Mail=_9DB502A6-D712-4F05-BA11-B33A8614B9EA Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=iso-8859-1 http://docs.oracle.com/javase/7/docs/api/java/sql/= ResultSetMetaData.html#isNullable(int)

Althou= gh the spec doesn't explicitly distinguish between result set and source = table only the former seems sensible to me -- what I expect the = isNullable return value to reflect is whether an invocation of the = ResultSet.getXXX(i) methods always returns an object (or sensible = primitive), if they may return null or if that's = unknown.


Test = case:

package = tmdbctest;

import = java.sql.*;

/*
CREATE TABLE employee = (
  id SERIAL PRIMARY KEY,
  name TEXT = UNIQUE,
  salary NUMERIC,
  hire_date = TIMESTAMP,
  fire_date TIMESTAMP,
  = picture BYTEA
)
WITHOUT = OIDS;

INSERT INTO employee ( name, salary, = hire_date, fire_date ) VALUES
    ( 'Foo Johnson', = 50, '2007-02-05 09:00:00', null ),
    ( 'Bar = Mannson', 60, '2007-08-02 09:00:00', null ),
    ( = 'Bruce Woolloomooloo', 70, '2007-08-15 08:00:00', '2008-06-09 15:00:00' = ),
    ( 'Janus Larsen', 65, '2006-08-15 08:30:00', = '2008-03-14 16:30:00' );

CREATE TABLE worklog = (
  id SERIAL PRIMARY KEY,
  employee_id = INT4 REFERENCES employee(id) DEFERRABLE NOT NULL,
  = workday DATE NOT NULL,
  hours_worked NUMERIC NOT = NULL
)
WITHOUT OIDS;

INSERT = INTO worklog ( employee_id, workday, hours_worked ) = VALUES
    ( 1, '2007-10-15', 5 ),
  =   ( 1, '2007-10-16', 6 ),
    ( 1, = '2007-10-17', 7 ),
    ( 1, '2007-10-18', 8 = ),
    ( 2, '2007-10-16', 2 ),
  =   ( 2, '2007-10-17', 7 ),
    ( 2, = '2007-10-18', 3 ),
    ( 2, '2007-10-19', 5 = );
*/
public class Testcase {
  =   static final String SQL_QUERY =3D
      =       "SELECT employee.id, name, salary, worklog.id AS = worklog_id, workday, hours_worked\n"+
      =       "  FROM employee\n"+
    =         "  LEFT JOIN worklog ON ( employee.id =3D= worklog.employee_id )";

    public = static Connection getConnection() throws SQLException {
  =       return = DriverManager.getConnection(
        =     = "jdbc:postgresql://localhost/postgres?user=3Dtmdbc_sample&password=3Dt= mdbc2008");
    }
    public = static void main(String[] args) throws Exception {
  =       Connection conn =3D = getConnection();
        PreparedStatement = stmnt =3D = conn.prepareStatement(SQL_QUERY);

    =     ResultSetMetaData rsmd =3D = stmnt.getMetaData();

      =   int columnCount =3D = rsmd.getColumnCount();

      =   System.out.println( "Metadata:" );
      =   for( int i =3D 1; i <=3D columnCount; i++ ){
  =           System.out.println( = rsmd.getColumnLabel(i) +": "+ parseNullable( rsmd.isNullable(i) ) = );
        = }

        ResultSet rs =3D = stmnt.executeQuery();

      =   System.out.println( "Data:" );
      =   for( int i =3D 1; i <=3D columnCount; i++ ){
  =           if( i !=3D 1 )
  =               System.out.print( "," = );

            = System.out.print( rsmd.getColumnLabel(i) );
    =     }
        = System.out.println();
        while( = rs.next() ){
            for( = int i =3D 1; i <=3D columnCount; i++ ){
    =             if( i !=3D 1 = )
                =     System.out.print( "," );

  =               System.out.print( = rs.getString(i) );
            = }
            = System.out.println();
        = }
    }
    public static String = parseNullable(int i){
        = switch(i){
            case = ResultSetMetaData.columnNoNulls: return "NoNulls";
  =           case = ResultSetMetaData.columnNullable: return "Nullable";
  =           case = ResultSetMetaData.columnNullableUnknown: return = "NullableUnknown";
            = default:
              =   throw new IllegalArgumentException();
    =     }
    = }
}



Output:

Metadata:
id: = NoNulls
name: NoNulls
salary: = Nullable
worklog_id: NoNulls
workday: = NoNulls
hours_worked: = NoNulls
Data:
id,name,salary,worklog_id,workday,hours_= worked
1,Foo Johnson,50.00,1,2007-10-15,5
1,Foo = Johnson,50.00,2,2007-10-16,6
1,Foo = Johnson,50.00,3,2007-10-17,7
1,Foo = Johnson,50.00,4,2007-10-18,8
2,Bar = Mannson,60.00,5,2007-10-16,2
2,Bar = Mannson,60.00,6,2007-10-17,7
2,Bar = Mannson,60.00,7,2007-10-18,3
2,Bar = Mannson,60.00,8,2007-10-19,5
4,Janus = Larsen,65.00,null,null,null
3,Bruce = Woolloomooloo,70.00,null,null,null

Tested = with = postgresql-9.1-903.jdbc4

Thanks,
Thor = Michael St=F8re

= --Apple-Mail=_9DB502A6-D712-4F05-BA11-B33A8614B9EA--