Skip to content

ObjectIdentifier maps Date and Time to same value #125

@nwilly

Description

@nwilly

Date and Time both map to 1082 in the class ObjectIdentitfier. Time should be 1083. This caused a problem for me when writing to a time[] column.

https://github.com/PgBulkInsert/PgBulkInsert/blob/f1b9e4b679bc7b1ffdb9711dc0693148438dac02/PgBulkInsert/src/main/java/de/bytefish/pgbulkinsert/pgsql/constants/ObjectIdentifier.java#L140C1-L144C35

The following code produces this error:
de.bytefish.pgbulkinsert.exceptions.BinaryWriteFailedException: org.postgresql.util.PSQLException: ERROR: binary data has array element type 1082 (date) instead of expected 1083 (time without time zone) Where: COPY test, line 1, column time_array

String urlSrc =  "jdbc:postgresql://localhost:5433/mydb";
Properties props = new Properties();
props.setProperty("user","postgres");
props.setProperty("password","password");
String schema = "public";
String table = "test";
String column = "time_array";
String[] columns = new String[]{column};

final String dropQuery = String.format("DROP table if exists %s.%s;", schema, table);
final String createQuery = String.format("CREATE table %s.%s (%s time[]);", schema, table, column);
final String setupQuery = dropQuery + "\n" + createQuery;

try (Connection conn = DriverManager.getConnection(urlSrc,props);
){
    try(PreparedStatement ps = conn.prepareStatement(setupQuery)){
        ps.executeUpdate();
    }

    PGConnection pgConn = PostgreSqlUtils.getPGConnection(conn);
    SimpleRowWriter.Table pgTable = new SimpleRowWriter.Table(schema, table, columns);
    List<LocalTime> timeList = List.of(LocalTime.of(11,12));


    try(SimpleRowWriter writer = new SimpleRowWriter(pgTable, pgConn)) {
        writer.startRow((row) -> {
            row.setCollection(column, DataType.Time, timeList);
        });
    }

} catch (SQLException e) {
    throw new RuntimeException(e);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions