This is an automated email from the ASF dual-hosted git repository.
mbudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new a6973bde81 [CALCITE-6386] Elasticsearch adapter throws
NullPointerException when used with with model.json and no username, password
or pathPrefix
a6973bde81 is described below
commit a6973bde8107a7c2a605a82c779a64fe28bdb5d1
Author: xuzifu666 <[email protected]>
AuthorDate: Fri Jun 27 14:53:30 2025 +0800
[CALCITE-6386] Elasticsearch adapter throws NullPointerException when used
with with model.json and no username, password or pathPrefix
---
.../elasticsearch/ElasticsearchSchemaFactory.java | 14 +++++++++++++-
.../elasticsearch/ElasticSearchAdapterTest.java | 18 ++++++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)
diff --git
a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
index aeaec2fa0f..84030378b8 100644
---
a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
+++
b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
@@ -45,6 +45,7 @@
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
@@ -212,7 +213,18 @@ private static RestClient connect(List<HttpHost> hosts,
String pathPrefix,
checkArgument(!hosts.isEmpty(), "no ES hosts specified");
// Two lists are considered equal when all of their corresponding elements
are equal
// making a list of RestClient params a suitable cache key.
- List cacheKey = ImmutableList.of(hosts, pathPrefix, username, password);
+ ArrayList<Object> config = new ArrayList<>();
+ config.add(hosts);
+ if (pathPrefix != null) {
+ config.add(pathPrefix);
+ }
+ if (username != null) {
+ config.add(username);
+ }
+ if (password != null) {
+ config.add(password);
+ }
+ List cacheKey = ImmutableList.copyOf(config);
try {
return REST_CLIENTS.get(cacheKey, new Callable<RestClient>() {
diff --git
a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
index eb112e5138..eb241fe328 100644
---
a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
+++
b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
@@ -163,6 +163,24 @@ private CalciteAssert.AssertThat calciteAssert() {
.returnsCount(0);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6386">[CALCITE-6386]
+ * NPE when using ES adapter with model.json and no specified username,
password
+ * or pathPrefix</a>. */
+ @Test void testConnectNoSpecifiedUserOrpathPrefix() throws SQLException {
+ Connection connection =
DriverManager.getConnection("jdbc:calcite:lex=JAVA");
+ final CalciteConnection calciteConnection =
connection.unwrap(CalciteConnection.class);
+ final ElasticsearchSchemaFactory esSchemaFactory = new
ElasticsearchSchemaFactory();
+
+ Map<String, Object> options = new HashMap<>();
+ String coordinates = String.format(Locale.ROOT, "[\"%s\"]",
NODE.httpHost());
+ options.put("hosts", coordinates);
+
+ final Schema esSchmea =
+ esSchemaFactory.create(calciteConnection.getRootSchema(),
"elasticsearch", options);
+ assertNotNull(esSchmea);
+ }
+
@Test void testDisableSSL() throws SQLException {
Connection connection =
DriverManager.getConnection("jdbc:calcite:lex=JAVA");