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

GH-40249: [Java] Fix NPE in ArrowDatabaseMetadata #40988

Merged
merged 1 commit into from
Apr 7, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -197,31 +198,36 @@ public boolean isReadOnly() throws SQLException {
@Override
public String getSQLKeywords() throws SQLException {
return convertListSqlInfoToString(
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_KEYWORDS, List.class));
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_KEYWORDS, List.class))
.orElse("");
}

@Override
public String getNumericFunctions() throws SQLException {
return convertListSqlInfoToString(
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_NUMERIC_FUNCTIONS, List.class));
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_NUMERIC_FUNCTIONS, List.class))
.orElse("");
}

@Override
public String getStringFunctions() throws SQLException {
return convertListSqlInfoToString(
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_STRING_FUNCTIONS, List.class));
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_STRING_FUNCTIONS, List.class))
.orElse("");
}

@Override
public String getSystemFunctions() throws SQLException {
return convertListSqlInfoToString(
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SYSTEM_FUNCTIONS, List.class));
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_SYSTEM_FUNCTIONS, List.class))
.orElse("");
}

@Override
public String getTimeDateFunctions() throws SQLException {
return convertListSqlInfoToString(
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DATETIME_FUNCTIONS, List.class));
getSqlInfoAndCacheIfCacheIsEmpty(SqlInfo.SQL_DATETIME_FUNCTIONS, List.class))
.orElse("");
}

@Override
Expand Down Expand Up @@ -753,8 +759,12 @@ private <T> T getSqlInfoAndCacheIfCacheIsEmpty(final SqlInfo sqlInfoCommand,
return desiredType.cast(cachedSqlInfo.get(sqlInfoCommand));
}

private String convertListSqlInfoToString(final List<?> sqlInfoList) {
return sqlInfoList.stream().map(Object::toString).collect(Collectors.joining(", "));
private Optional<String> convertListSqlInfoToString(final List<?> sqlInfoList) {
if (sqlInfoList == null) {
normanj-bitquill marked this conversation as resolved.
Show resolved Hide resolved
return Optional.empty();
} else {
return Optional.of(sqlInfoList.stream().map(Object::toString).collect(Collectors.joining(", ")));
}
}

private boolean getSqlInfoEnumOptionAndCacheIfCacheIsEmpty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@
public class ArrowDatabaseMetadataTest {
public static final boolean EXPECTED_MAX_ROW_SIZE_INCLUDES_BLOBS = false;
private static final MockFlightSqlProducer FLIGHT_SQL_PRODUCER = new MockFlightSqlProducer();
private static final MockFlightSqlProducer FLIGHT_SQL_PRODUCER_EMPTY_SQLINFO =
new MockFlightSqlProducer();
@ClassRule
public static final FlightServerTestRule FLIGHT_SERVER_TEST_RULE = FlightServerTestRule
.createStandardTestRule(FLIGHT_SQL_PRODUCER);
@ClassRule
public static final FlightServerTestRule FLIGHT_SERVER_EMPTY_SQLINFO_TEST_RULE =
FlightServerTestRule.createStandardTestRule(FLIGHT_SQL_PRODUCER_EMPTY_SQLINFO);
private static final int ROW_COUNT = 10;
private static final List<List<Object>> EXPECTED_GET_CATALOGS_RESULTS =
range(0, ROW_COUNT)
Expand Down Expand Up @@ -604,7 +609,7 @@ public static void setUpBeforeClass() throws SQLException {

@AfterClass
public static void tearDown() throws Exception {
AutoCloseables.close(connection, FLIGHT_SQL_PRODUCER);
AutoCloseables.close(connection, FLIGHT_SQL_PRODUCER, FLIGHT_SQL_PRODUCER_EMPTY_SQLINFO);
}


Expand Down Expand Up @@ -1420,4 +1425,16 @@ public void testSqlToRegexLike() {
Assert.assertEquals("\\*", ArrowDatabaseMetadata.sqlToRegexLike("*"));
Assert.assertEquals("T\\*E.S.*T", ArrowDatabaseMetadata.sqlToRegexLike("T*E_S%T"));
}

@Test
public void testEmptySqlInfo() throws Exception {
try (final Connection testConnection = FLIGHT_SERVER_EMPTY_SQLINFO_TEST_RULE.getConnection(false)) {
final DatabaseMetaData metaData = testConnection.getMetaData();
collector.checkThat(metaData.getSQLKeywords(), is(""));
collector.checkThat(metaData.getNumericFunctions(), is(""));
collector.checkThat(metaData.getStringFunctions(), is(""));
collector.checkThat(metaData.getSystemFunctions(), is(""));
collector.checkThat(metaData.getTimeDateFunctions(), is(""));
}
}
}
Loading