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

[NOID] Fixes #3496: Simple mysql select now(); Throws class java.time.LocalDateTime cannot be cast to class java.sql.Timestamp (#3975) #4036

Draft
wants to merge 2 commits into
base: 4.4
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions full/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ dependencies {
compileOnly group: 'org.ow2.asm', name: 'asm', version: '5.0.2'

// schemacrawler
implementation group: 'us.fatehi', name: 'schemacrawler', version: '15.04.01'
testImplementation group: 'us.fatehi', name: 'schemacrawler-mysql', version: '15.04.01'
implementation group: 'us.fatehi', name: 'schemacrawler', version: '16.20.8'
testImplementation group: 'us.fatehi', name: 'schemacrawler-mysql', version: '16.20.8'

testImplementation group: 'org.apache.hive', name: 'hive-jdbc', version: '1.2.2', withoutServers

Expand Down
14 changes: 8 additions & 6 deletions full/src/main/java/apoc/load/Jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.sql.*;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -106,7 +107,7 @@ private Stream<RowResult> executeQuery(
String url = getUrlOrKey(urlOrKey);
String query = getSqlOrKey(tableOrSelect);
try {
Connection connection = getConnection(url, loadJdbcConfig);
Connection connection = getConnection(url, loadJdbcConfig).get();
// see https://jdbc.postgresql.org/documentation/91/query.html#query-with-cursors
connection.setAutoCommit(loadJdbcConfig.isAutoCommit());
try {
Expand Down Expand Up @@ -162,7 +163,7 @@ private Stream<RowResult> executeUpdate(
String url = getUrlOrKey(urlOrKey);
LoadJdbcConfig jdbcConfig = new LoadJdbcConfig(config);
try {
Connection connection = getConnection(url, jdbcConfig);
Connection connection = getConnection(url, jdbcConfig).get();
try {
PreparedStatement stmt =
connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
Expand Down Expand Up @@ -277,21 +278,22 @@ private Object convert(Object value, int sqlType) {
if (Types.TIME_WITH_TIMEZONE == sqlType) {
return OffsetTime.parse(value.toString());
}
ZoneId zoneId = config.getZoneId();
if (Types.TIMESTAMP == sqlType) {
if (config.getZoneId() != null) {
if (zoneId != null) {
return ((java.sql.Timestamp) value)
.toInstant()
.atZone(config.getZoneId())
.atZone(zoneId)
.toOffsetDateTime();
} else {
return ((java.sql.Timestamp) value).toLocalDateTime();
}
}
if (Types.TIMESTAMP_WITH_TIMEZONE == sqlType) {
if (config.getZoneId() != null) {
if (zoneId != null) {
return ((java.sql.Timestamp) value)
.toInstant()
.atZone(config.getZoneId())
.atZone(zoneId)
.toOffsetDateTime();
} else {
return OffsetDateTime.parse(value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import java.net.URI;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.login.LoginContext;
import us.fatehi.utility.datasource.DatabaseConnectionSource;
import us.fatehi.utility.datasource.DatabaseConnectionSources;
import us.fatehi.utility.datasource.MultiUseUserCredentials;

public class JdbcUtil {

Expand All @@ -37,7 +38,7 @@ public class JdbcUtil {

private JdbcUtil() {}

public static Connection getConnection(String jdbcUrl, LoadJdbcConfig config) throws Exception {
public static DatabaseConnectionSource getConnection(String jdbcUrl, LoadJdbcConfig config) throws Exception {
if (config.hasCredentials()) {
return createConnection(
jdbcUrl,
Expand All @@ -52,11 +53,12 @@ public static Connection getConnection(String jdbcUrl, LoadJdbcConfig config) th
String[] user = userInfo.split(":");
return createConnection(cleanUrl, user[0], user[1]);
}
return DriverManager.getConnection(jdbcUrl);
return DatabaseConnectionSources.newDatabaseConnectionSource(jdbcUrl, new MultiUseUserCredentials());
}
}

private static Connection createConnection(String jdbcUrl, String userName, String password) throws Exception {
private static DatabaseConnectionSource createConnection(String jdbcUrl, String userName, String password)
throws Exception {
if (jdbcUrl.contains(";auth=kerberos")) {
String client = System.getProperty("java.security.auth.login.config.client", "KerberosClient");
LoginContext lc = new LoginContext(client, callbacks -> {
Expand All @@ -68,13 +70,15 @@ private static Connection createConnection(String jdbcUrl, String userName, Stri
lc.login();
Subject subject = lc.getSubject();
try {
return Subject.doAs(subject, (PrivilegedExceptionAction<Connection>)
() -> DriverManager.getConnection(jdbcUrl, userName, password));
return Subject.doAs(subject, (PrivilegedExceptionAction<DatabaseConnectionSource>)
() -> DatabaseConnectionSources.newDatabaseConnectionSource(
jdbcUrl, new MultiUseUserCredentials(userName, password)));
} catch (PrivilegedActionException pae) {
throw pae.getException();
}
} else {
return DriverManager.getConnection(jdbcUrl, userName, password);
return DatabaseConnectionSources.newDatabaseConnectionSource(
jdbcUrl, new MultiUseUserCredentials(userName, password));
}
}

Expand Down
7 changes: 2 additions & 5 deletions full/src/main/java/apoc/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
import schemacrawler.schema.*;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.utility.SchemaCrawlerUtility;
import schemacrawler.tools.utility.SchemaCrawlerUtility;

@Extended
public class Model {
Expand Down Expand Up @@ -79,9 +78,7 @@ public Stream<DatabaseModel> jdbc(
throws Exception {
String url = getUrlOrKey(urlOrKey);

SchemaCrawlerOptionsBuilder optionsBuilder =
SchemaCrawlerOptionsBuilder.builder().withSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
SchemaCrawlerOptions options = optionsBuilder.toOptions();
SchemaCrawlerOptions options = SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions();

Catalog catalog = SchemaCrawlerUtility.getCatalog(getConnection(url, new LoadJdbcConfig(config)), options);

Expand Down
171 changes: 139 additions & 32 deletions full/src/test/java/apoc/load/MySQLJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,169 @@

import static apoc.util.TestUtil.testCall;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import apoc.util.MySQLContainerExtension;
import apoc.util.TestUtil;
import apoc.util.Util;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

@RunWith(Enclosed.class)
public class MySQLJdbcTest extends AbstractJdbcTest {

@ClassRule
public static MySQLContainerExtension mysql = new MySQLContainerExtension();
public static class MySQLJdbcLatestVersionTest {

@ClassRule
public static DbmsRule db = new ImpermanentDbmsRule();
@ClassRule
public static MySQLContainerExtension mysql = new MySQLContainerExtension("mysql:8.0.31");

@BeforeClass
public static void setUpContainer() {
mysql.start();
TestUtil.registerProcedure(db, Jdbc.class);
@ClassRule
public static DbmsRule db = new ImpermanentDbmsRule();

@BeforeClass
public static void setUpContainer() {
mysql.start();
TestUtil.registerProcedure(db, Jdbc.class);
}

@AfterClass
public static void tearDown() {
mysql.stop();
db.shutdown();
}

@Test
public void testLoadJdbc() {
MySQLJdbcTest.testLoadJdbc(db, mysql);
}

@Test
public void testIssue3496() {
MySQLJdbcTest.testIssue3496(db, mysql);
}
}

@AfterClass
public static void tearDown() {
mysql.stop();
db.shutdown();
public static class MySQLJdbcFiveVersionTest {

@ClassRule
public static MySQLContainerExtension mysql = new MySQLContainerExtension("mysql:5.7");

@ClassRule
public static DbmsRule db = new ImpermanentDbmsRule();

@BeforeClass
public static void setUpContainer() {
mysql.start();
TestUtil.registerProcedure(db, Jdbc.class);
}

@AfterClass
public static void tearDown() {
mysql.stop();
db.shutdown();
}

@Test
public void testLoadJdbc() {
MySQLJdbcTest.testLoadJdbc(db, mysql);
}

@Test
public void testIssue3496() {
MySQLJdbcTest.testIssue3496(db, mysql);
}
}

@Test
public void testLoadJdbc() {
private static void testLoadJdbc(DbmsRule db, MySQLContainerExtension mysql) {
// with the config {timezone: 'UTC'} and `preserveInstants=true&connectionTimeZone=SERVER` to make the result
// deterministic,
// since `TIMESTAMP` values are automatically converted from the session time zone to UTC for storage, and vice
// versa.
testCall(
db,
"CALL apoc.load.jdbc($url, $table, [])",
Util.map("url", mysql.getJdbcUrl(), "table", "country"),
"CALL apoc.load.jdbc($url, $table, [], {timezone: 'UTC'})",
Util.map(
"url",
mysql.getJdbcUrl() + "&preserveInstants=true&connectionTimeZone=SERVER",
"table",
"country"),
row -> {
Map<String, Object> expected = Util.map(
"Code", "NLD",
"Name", "Netherlands",
"Continent", "Europe",
"Region", "Western Europe",
"SurfaceArea", 41526f,
"IndepYear", 1581,
"Population", 15864000,
"LifeExpectancy", 78.3f,
"GNP", 371362f,
"GNPOld", 360478f,
"LocalName", "Nederland",
"GovernmentForm", "Constitutional Monarchy",
"HeadOfState", "Beatrix",
"Capital", 5,
"Code2", "NL");
assertEquals(expected, row.get("row"));
"Code",
"NLD",
"Name",
"Netherlands",
"Continent",
"Europe",
"Region",
"Western Europe",
"SurfaceArea",
41526f,
"IndepYear",
1581,
"Population",
15864000,
"LifeExpectancy",
78.3f,
"GNP",
371362f,
"GNPOld",
360478f,
"LocalName",
"Nederland",
"GovernmentForm",
"Constitutional Monarchy",
"HeadOfState",
"Beatrix",
"Capital",
5,
"Code2",
"NL",
"myTime",
LocalTime.of(1, 0, 0),
"myTimeStamp",
ZonedDateTime.parse("2003-01-01T01:00Z"),
"myDate",
LocalDate.parse("2003-01-01"),
"myYear",
LocalDate.parse("2003-01-01"));
Map actual = (Map) row.get("row");
Object myDateTime = actual.remove("myDateTime");
assertTrue(myDateTime instanceof LocalDateTime);
assertEquals(expected, actual);
});
}

private static void testIssue3496(DbmsRule db, MySQLContainerExtension mysql) {
testCall(
db,
"CALL apoc.load.jdbc($url,'SELECT DATE(NOW()), NOW(), CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), DATE(UTC_TIMESTAMP());')",
Util.map("url", mysql.getJdbcUrl()),
r -> {
Map row = (Map) r.get("row");
assertEquals(8, row.size());

assertTrue(row.get("UTC_DATE()") instanceof LocalDate);
assertTrue(row.get("CURDATE()") instanceof LocalDate);

assertTrue(row.get("UTC_TIMESTAMP()") instanceof LocalDateTime);
assertTrue(row.get("NOW()") instanceof LocalDateTime);
assertTrue(row.get("DATE(UTC_TIMESTAMP())") instanceof LocalDate);
assertTrue(row.get("DATE(NOW())") instanceof LocalDate);

assertTrue(row.get("CURTIME()") instanceof LocalTime);
assertTrue(row.get("UTC_TIME()") instanceof LocalTime);
});
}
}
Loading
Loading