This is an automated email from the ASF dual-hosted git repository.
xuzifu666 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 6fab1a1b64 [CALCITE-7388] Redis Adapter operand config should not
support empty string
6fab1a1b64 is described below
commit 6fab1a1b64766ce5a7167b5be3e62505b99fad07
Author: Terran <[email protected]>
AuthorDate: Tue Jan 20 14:51:05 2026 +0800
[CALCITE-7388] Redis Adapter operand config should not support empty string
---
.../apache/calcite/adapter/redis/RedisSchema.java | 27 ++-
.../adapter/redis/RedisAdapterConfigCaseBase.java | 200 +++++++++++++++++++++
2 files changed, 219 insertions(+), 8 deletions(-)
diff --git
a/redis/src/main/java/org/apache/calcite/adapter/redis/RedisSchema.java
b/redis/src/main/java/org/apache/calcite/adapter/redis/RedisSchema.java
index 8115d93f6c..1aad9ae459 100644
--- a/redis/src/main/java/org/apache/calcite/adapter/redis/RedisSchema.java
+++ b/redis/src/main/java/org/apache/calcite/adapter/redis/RedisSchema.java
@@ -20,6 +20,7 @@
import org.apache.calcite.schema.Table;
import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.google.common.cache.CacheBuilder;
@@ -42,6 +43,11 @@
* is an HTML table on a URL.
*/
class RedisSchema extends AbstractSchema {
+ private static final String DATA_FORMAT = "dataFormat";
+ private static final String FIELDS = "fields";
+ private static final String KEY_DELIMITER = "keyDelimiter";
+ private static final String OPERAND = "operand";
+
public final String host;
public final int port;
public final int database;
@@ -85,17 +91,22 @@ public RedisTableFieldInfo getTableFieldInfo(String
tableName) {
for (JsonCustomTable jsonCustomTable : jsonCustomTables) {
if (jsonCustomTable.name.equals(tableName)) {
Map<String, Object> map =
- requireNonNull(jsonCustomTable.operand, "operand");
- if (map.get("dataFormat") == null) {
- throw new RuntimeException("dataFormat is null");
+ requireNonNull(jsonCustomTable.operand, OPERAND);
+ if (ObjectUtils.isEmpty(map.get(DATA_FORMAT))) {
+ throw new RuntimeException("dataFormat is invalid, it must be raw,
csv or json");
+ }
+ RedisDataFormat dataFormatEnum =
+ RedisDataFormat.fromTypeName(map.get(DATA_FORMAT).toString());
+ if (dataFormatEnum == null) {
+ throw new RuntimeException("dataFormat is invalid, it must be raw,
csv or json");
}
- if (map.get("fields") == null) {
+ if (ObjectUtils.isEmpty(map.get(FIELDS))) {
throw new RuntimeException("fields is null");
}
- dataFormat = map.get("dataFormat").toString();
- fields = (List<LinkedHashMap<String, Object>>) map.get("fields");
- if (map.get("keyDelimiter") != null) {
- keyDelimiter = map.get("keyDelimiter").toString();
+ dataFormat = map.get(DATA_FORMAT).toString();
+ fields = (List<LinkedHashMap<String, Object>>) map.get(FIELDS);
+ if (map.get(KEY_DELIMITER) != null) {
+ keyDelimiter = map.get(KEY_DELIMITER).toString();
}
break;
}
diff --git
a/redis/src/test/java/org/apache/calcite/adapter/redis/RedisAdapterConfigCaseBase.java
b/redis/src/test/java/org/apache/calcite/adapter/redis/RedisAdapterConfigCaseBase.java
new file mode 100644
index 0000000000..c1affc2bfb
--- /dev/null
+++
b/redis/src/test/java/org/apache/calcite/adapter/redis/RedisAdapterConfigCaseBase.java
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.adapter.redis;
+
+import org.apache.calcite.config.CalciteSystemProperty;
+import org.apache.calcite.test.CalciteAssert;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.junit.jupiter.api.Test;
+
+import redis.clients.jedis.Protocol;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * Tests for the {@code org.apache.calcite.adapter.redis} package.
+ */
+public class RedisAdapterConfigCaseBase extends RedisAdapterCaseBase {
+
+ private String model;
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7388">[CALCITE-7388]
+ * Redis Adapter operand config should not support empty string</a>. */
+ protected void readModelFromJsonString(String jsonString) {
+ String strResult = null;
+ try {
+ ObjectMapper objMapper = new ObjectMapper();
+ objMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
+ .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
+ .configure(JsonParser.Feature.ALLOW_COMMENTS, true);
+ JsonNode rootNode = objMapper.readTree(jsonString);
+ strResult =
+ rootNode.toString().replace(Integer.toString(Protocol.DEFAULT_PORT),
+ Integer.toString(getRedisServerPort()));
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to read model from json string", e);
+ }
+ model = strResult;
+ }
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7388">[CALCITE-7388]
+ * Redis Adapter operand config should not support empty string</a>. */
+ @Test void testDataFormatEmptyException() {
+ String jsonString = "{"
+ + " \"version\": \"1.0\","
+ + " \"defaultSchema\": \"redis\","
+ + " \"schemas\": ["
+ + " {"
+ + " \"type\": \"custom\","
+ + " \"name\": \"foodmart\","
+ + " \"factory\":
\"org.apache.calcite.adapter.redis.RedisSchemaFactory\","
+ + " \"operand\": {"
+ + " \"host\": \"localhost\","
+ + " \"port\": 6379 ,"
+ + " \"database\": 0,"
+ + " \"password\": \"\""
+ + " },"
+ + " \"tables\": ["
+ + " {"
+ + " \"name\": \"csv_05\","
+ + " \"type\": \"custom\","
+ + " \"factory\":
\"org.apache.calcite.adapter.redis.RedisTableFactory\","
+ + " \"operand\": {"
+ + " \"dataFormat\": \"\","
+ + " \"keyDelimiter\": \":\","
+ + " \"fields\": [\n"
+ + " {\n"
+ + " \"name\": \"DEPTNO\",\n"
+ + " \"type\": \"varchar\",\n"
+ + " \"mapping\": 0\n"
+ + " },\n"
+ + " {\n"
+ + " \"name\": \"NAME\",\n"
+ + " \"type\": \"varchar\",\n"
+ + " \"mapping\": 1\n"
+ + " }\n"
+ + " ]\n"
+ + " }"
+ + " }"
+ + " ]"
+ + " }"
+ + " ]"
+ + "}";
+ readModelFromJsonString(jsonString);
+ assertNotNull(model, "model cannot be null!");
+ CalciteAssert.model(model)
+ .enable(CalciteSystemProperty.TEST_REDIS.value())
+ .connectThrows("dataFormat is invalid, it must be raw, csv or json");
+ }
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7388">[CALCITE-7388]
+ * Redis Adapter operand config should not support empty string</a>. */
+ @Test void testDataFieldsEmptyException() {
+ String jsonString = "{"
+ + " \"version\": \"1.0\","
+ + " \"defaultSchema\": \"redis\","
+ + " \"schemas\": ["
+ + " {"
+ + " \"type\": \"custom\","
+ + " \"name\": \"foodmart\","
+ + " \"factory\":
\"org.apache.calcite.adapter.redis.RedisSchemaFactory\","
+ + " \"operand\": {"
+ + " \"host\": \"localhost\","
+ + " \"port\": 6379 ,"
+ + " \"database\": 0,"
+ + " \"password\": \"\""
+ + " },"
+ + " \"tables\": ["
+ + " {"
+ + " \"name\": \"csv_05\","
+ + " \"type\": \"custom\","
+ + " \"factory\":
\"org.apache.calcite.adapter.redis.RedisTableFactory\","
+ + " \"operand\": {"
+ + " \"dataFormat\": \"csv\","
+ + " \"keyDelimiter\": \":\","
+ + " \"fields\": []\n"
+ + " }"
+ + " }"
+ + " ]"
+ + " }"
+ + " ]"
+ + "}";
+ readModelFromJsonString(jsonString);
+ assertNotNull(model, "model cannot be null!");
+ CalciteAssert.model(model)
+ .enable(CalciteSystemProperty.TEST_REDIS.value())
+ .connectThrows("fields is null");
+ }
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7388">[CALCITE-7388]
+ * Redis Adapter operand config should not support empty string</a>. */
+ @Test void testDataFormatInvalidException() {
+ String jsonString = "{"
+ + " \"version\": \"1.0\","
+ + " \"defaultSchema\": \"redis\","
+ + " \"schemas\": ["
+ + " {"
+ + " \"type\": \"custom\","
+ + " \"name\": \"foodmart\","
+ + " \"factory\":
\"org.apache.calcite.adapter.redis.RedisSchemaFactory\","
+ + " \"operand\": {"
+ + " \"host\": \"localhost\","
+ + " \"port\": 6379 ,"
+ + " \"database\": 0,"
+ + " \"password\": \"\""
+ + " },"
+ + " \"tables\": ["
+ + " {"
+ + " \"name\": \"csv_05\","
+ + " \"type\": \"custom\","
+ + " \"factory\":
\"org.apache.calcite.adapter.redis.RedisTableFactory\","
+ + " \"operand\": {"
+ + " \"dataFormat\": \"CSV\","
+ + " \"keyDelimiter\": \":\","
+ + " \"fields\": [\n"
+ + " {\n"
+ + " \"name\": \"DEPTNO\",\n"
+ + " \"type\": \"varchar\",\n"
+ + " \"mapping\": 0\n"
+ + " },\n"
+ + " {\n"
+ + " \"name\": \"NAME\",\n"
+ + " \"type\": \"varchar\",\n"
+ + " \"mapping\": 1\n"
+ + " }\n"
+ + " ]\n"
+ + " }"
+ + " }"
+ + " ]"
+ + " }"
+ + " ]"
+ + "}";
+ readModelFromJsonString(jsonString);
+ assertNotNull(model, "model cannot be null!");
+ CalciteAssert.model(model)
+ .enable(CalciteSystemProperty.TEST_REDIS.value())
+ .connectThrows("dataFormat is invalid, it must be raw, csv or json");
+ }
+}