ydb-java-sdk icon indicating copy to clipboard operation
ydb-java-sdk copied to clipboard

Dates, datetime and timestamps are not allowed before 1.1.1970

Open planesweep opened this issue 4 years ago • 4 comments

Problem

The ydb-java-sdk does not allow to store a date, datetime or timestamp before 1.1.1970. In my opinion this does NOT make any sense. How do you suppose to store a birthdate before 1.1.1970.

How to reproduce

  1. Create a customer table
        TableDescription customerTable = TableDescription.newBuilder()
                .addNullableColumn("email", PrimitiveType.utf8())
                .addNullableColumn("firstname", PrimitiveType.utf8())
                .addNullableColumn("lastname", PrimitiveType.utf8())
                .addNullableColumn("birthdate", PrimitiveType.date())
                .addNullableColumn("modifier", PrimitiveType.utf8())
                .addNullableColumn("lastupdate", PrimitiveType.timestamp())
                .setPrimaryKey("email")
                .build();
  1. Store a birthdate before 1.1.1970 -> save birthdate 1.1.1930

  2. Finally you get this exception

Exception in thread "main" java.lang.IllegalArgumentException: negative daysSinceEpoch: -354285
	at com.yandex.ydb.table.values.PrimitiveValue.date(PrimitiveValue.java:205)
	at com.yandex.ydb.table.values.PrimitiveValue.date(PrimitiveValue.java:215)
	at com.yandex.ydb.examples.simple.BenchmarkDatabase.addDataToCustomerTable(BenchmarkDatabase.java:172)
	at com.yandex.ydb.examples.simple.BenchmarkDatabase.fillData(BenchmarkDatabase.java:157)

Issue (weird restriction of negative days since Epoch)

Please check PrimitiveValue.java https://github.com/yandex-cloud/ydb-java-sdk/blob/master/table/src/main/java/com/yandex/ydb/table/values/PrimitiveValue.java

   public static PrimitiveValue date(long daysSinceEpoch) {
        if (daysSinceEpoch < 0) {
            throw new IllegalArgumentException("negative daysSinceEpoch: " + daysSinceEpoch);
        }
        return new InstantValue(PrimitiveType.date(), TimeUnit.DAYS.toMicros(daysSinceEpoch));
    }

    public static PrimitiveValue date(LocalDate value) {
        return date(value.toEpochDay());
    }

    public static PrimitiveValue date(Instant value) {
        return date(TimeUnit.SECONDS.toDays(value.getEpochSecond()));
    }

All dates are finally converted into a long but a negative long is not allowed, why? Linux and Java can handle date before the epoch.

planesweep avatar Aug 05 '21 07:08 planesweep

Unfortunately the problem is deeper. Current implementation of Date, Datetime and Timestamp types in database core supports limited range of dates starting from 1.1.1970. It's not normal to my opinion and must be changed in some way.

fomichev3000 avatar Aug 05 '21 18:08 fomichev3000

Unfortunately the problem is deeper. Current implementation of Date, Datetime and Timestamp types in database core supports limited range of dates starting from 1.1.1970. It's not normal to my opinion and must be changed in some way.

@fomichev3000: What do you think, when can you fix this - eta? I would highly appreciate a fix because our data model uses dates for several reasons.

planesweep avatar Aug 06 '21 07:08 planesweep

@fomichev3000: What do you think, when can you fix this - eta? I would highly appreciate a fix because our data model uses dates for several reasons. We need to add new functionality -- new datatypes. Then release it a new major version. So 4+ months at least. The problem is very deep, we need to add new datatypes and migrate old ones.

fomichev3000 avatar Aug 08 '21 13:08 fomichev3000

In the meantime, until new datatypes are available, a workaround could be to use the Int32 datatype instead, encoding the date as YYYYMMDD. That will work for all comparisons, and allow almost arbitrary date values - including the B.C. values, with negative numbers.

For sure this workaround has some drawbacks, like complicated date component extraction (like getting the week number and performing the week day alignment), and the large subset of unchecked incorrect values (like 20221597, being a valid Int32, does not represent a valid date).

zinal avatar Aug 15 '22 08:08 zinal