Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clickhouse-jdbc not support localDateTime #583

Closed
gao1399677 opened this issue Mar 2, 2021 · 12 comments · Fixed by #418 or #589
Closed

clickhouse-jdbc not support localDateTime #583

gao1399677 opened this issue Mar 2, 2021 · 12 comments · Fixed by #418 or #589
Assignees
Milestone

Comments

@gao1399677
Copy link

gao1399677 commented Mar 2, 2021

versions: clickhouse-jdbc 0.2.6 with mybatis3.4.5

reappear:
set sql param type localDateTime with value("2021-02-24 00:00:00")

exception:
Error querying database. Cause: ru.yandex.clickhouse.except.ClickHouseException: ClickHouse exception, code: 41, host: ******, port: **; Code: 41, e.displayText() = DB::Exception: Cannot parse datetime 2021-02-23T00:00: while converting '2021-02-23T00:00' to DateTime (version 20.8.2.3 (official build))

issues
clickhouse-jdbc convert LocalDateTime and LocalDate to string in related code
missing formatLocalDate() and formatLocalDateTime() method

public static String formatDate(Date date, TimeZone timeZone) {
        getDateFormat().setTimeZone(timeZone);
        return getDateFormat().format(date);
    }

    public static String formatTime(Time time, TimeZone timeZone) {
        getDateTimeFormat().setTimeZone(timeZone);
        return getDateTimeFormat().format(time);
    }

    public static String formatTimestamp(Timestamp time, TimeZone timeZone) {
        getDateTimeFormat().setTimeZone(timeZone);
        return getDateTimeFormat().format(time);
    }
@zhicwu zhicwu added this to the 0.3.0 release milestone Mar 2, 2021
@zhicwu zhicwu linked a pull request Mar 2, 2021 that will close this issue
@zhicwu
Copy link
Contributor

zhicwu commented Mar 2, 2021

It's available in develop branch and it will be released in v0.3.0. I think I still need a week or two to add more test cases to ensure the driver works well.

@gao1399677
Copy link
Author

gao1399677 commented Mar 2, 2021

It's available in develop branch and it will be released in v0.3.0. I think I still need a week or two to add more test cases to ensure the driver works well.

versions: clickhouse-jdbc developer

It also can't convert the dateTime to LocalDateTime ,When I get the response value from clickhouse to java entity in branch of developer

exception:
Cause: java.lang.NullPointerException: text] with root cause
java.lang.NullPointerException: text
at java.util.Objects.requireNonNull(Objects.java:228)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1848)
at java.time.LocalDate.parse(LocalDate.java:400)
at ru.yandex.clickhouse.response.ClickHouseResultSet.getObject(ClickHouseResultSet.java:669)
at ru.yandex.clickhouse.response.ClickHouseResultSet.getObject(ClickHouseResultSet.java:682)

issues:
related code
missing getLocalDate() and getLocalDateTime() methods

Will it be available in v0.3.0 ?

@zhicwu
Copy link
Contributor

zhicwu commented Mar 2, 2021

Are you expecting to get LocalDate by calling ResultSet.getObject() in above case? It looks like a bug of handling null value, which I can fix in 0.3.0.

@enqueue
Copy link
Contributor

enqueue commented Mar 2, 2021

Let met know if I can help.

@gao1399677 Note that there isn't any method getLocalDate() or getLocalDateTime() defined for JDBC ResultSet

@gao1399677
Copy link
Author

Are you expecting to get LocalDate by calling ResultSet.getObject() in above case? It looks like a bug of handling null value, which I can fix in 0.3.0.

Yes I am get results(entity with LocalDateTime field) from mybatis and calling ResultSet.getObject() in above case

@gao1399677
Copy link
Author

Let met know if I can help.

@gao1399677 Note that there isn't any method getLocalDate() or getLocalDateTime() defined for JDBC ResultSet

I am get results(entity with LocalDateTime field) from mybatis and calling ResultSet.getObject() in above case

@gao1399677
Copy link
Author

gao1399677 commented Mar 2, 2021

Let met know if I can help.

@gao1399677 Note that there isn't any method getLocalDate() or getLocalDateTime() defined for JDBC ResultSet

when I change the
related code to

public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
       
        if (type.equals(LocalDateTime.class)) {
            return (T) LocalDateTime.parse( getString(columnIndex), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
       
        if (type.equals(LocalDate.class)) {
            return (T) LocalDate.parse( getString(columnIndex), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
    }
It will be corrected, so I think it may be missing the method with LocalDateTime 

@enqueue
Copy link
Contributor

enqueue commented Mar 4, 2021

I would love to help, but I do not understand the problem. Your initial post points at some error when passing a LocalDateTime object as parameter, whereas in the comments you mention some error when working with the ResultSet. Could you perhaps write down the steps you are taking to reproduce the error, including the query? Sorry about the misunderstanding.

@zhicwu zhicwu self-assigned this Mar 5, 2021
@zhicwu
Copy link
Contributor

zhicwu commented Mar 5, 2021

I think we need to implement a mechanism for type conversion. For now, I'm good to merge the workaround into ResultSet.getObject method. I'll include this in the pull request for more data types.

@gao1399677
Copy link
Author

gao1399677 commented Mar 5, 2021

I would love to help, but I do not understand the problem. Your initial post points at some error when passing a LocalDateTime object as parameter, whereas in the comments you mention some error when working with the ResultSet. Could you perhaps write down the steps you are taking to reproduce the error, including the query? Sorry about the misunderstanding.

Thanks for your help!

Mybatis will call the getObject() method in ClickHouseResultSetTest.java ,when set the return entity as LocalDateTime, such as the next example:

I add a test toClickHouseResultSetTest.java ,which caused the similar exception

@Test
    public void testLocalDateTime() throws Exception {
        String response =
                "SiteName\n" +
                        "Nullable(DateTime)\n" +
                        "2021-01-08 11:05:14\n" +
                        "there.com\n" +
                        "\n" +
                        "\\N\n" +
                        "other.com\n" +
                        "\n" + // with totals separator row
                        "\\N\n";// with totals values row

        ByteArrayInputStream is = new ByteArrayInputStream(response.getBytes("UTF-8"));
        ClickHouseResultSet rs = buildResultSet(is, 1024, "db", "table", true, null, null, props);
        rs.next();
        
        LocalDateTime expectedTime = LocalDateTime.parse("2021-01-08 11:05:14", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        assertEquals(expectedTime, rs.getObject(1, LocalDateTime.class));
    }

Maybe there need a interface java.time.LocalDateTime getLocalDateTime(String columnLabel) throws SQLException; in AbstractResultSet.java

@zhicwu
Copy link
Contributor

zhicwu commented Mar 12, 2021

@gao1399677, Could you try again using latest code on develop branch? It works on my end but would be great if you can confirm.

@gao1399677
Copy link
Author

@gao1399677, Could you try again using latest code on develop branch? It works on my end but would be great if you can confirm.

The new developer branch's LocaLDateTime worked will in my project, Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants