Skip to content

Commit 609d28b

Browse files
authored
fix the binary function: from_base64, from_base32 (#17466)
1 parent d245edc commit 609d28b

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/scalar/IoTDBFromBase32ColumnFunctionIT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public class IoTDBFromBase32ColumnFunctionIT {
8080
// Case 10: Optional padding test (decoder should handle missing padding)
8181
// 'foo' (0x666f6f) -> Base32 without padding: MZXW6
8282
"INSERT INTO table1(time, c_text, c_string) VALUES (10, 'MZXW6', 'MZXW6')",
83+
// case 11: lower case
84+
"INSERT INTO table1(time, c_text, c_string) VALUES (11, 'mzxw6', 'mzxw6')",
8385
};
8486

8587
@BeforeClass
@@ -119,6 +121,8 @@ public void testFromBase32OnValidInputs() {
119121
"1970-01-01T00:00:00.009Z,0x666f6f6201,0x666f6f6201,",
120122
// 10. Optional padding
121123
"1970-01-01T00:00:00.010Z,0x666f6f,0x666f6f,",
124+
// 11. lower case will be considered as upper case
125+
"1970-01-01T00:00:00.011Z,0x666f6f,0x666f6f,",
122126
};
123127

124128
tableResultSetEqualTest(

integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/scalar/IoTDBFromBase64ColumnFunctionIT.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public class IoTDBFromBase64ColumnFunctionIT {
5454
"INSERT INTO table1(time, c_text, c_string) VALUES (3, '', '')",
5555
"INSERT INTO table1(time, c_int) VALUES (4, 404)",
5656
// invalid base64
57-
"INSERT INTO table1(time, c_text, c_string) VALUES (5, 'not_base64', 'not_base64')"
57+
"INSERT INTO table1(time, c_text, c_string) VALUES (5, 'not_base64', 'not_base64')",
58+
"INSERT INTO table1(time, c_text, c_string) VALUES (6, 'abc', 'abc')"
5859
};
5960

6061
@BeforeClass
@@ -95,10 +96,15 @@ public void testFromBase64OnTextString() {
9596
public void testFromBase64OnInvalidBase64() {
9697
String expectedErrorMessage =
9798
TSStatusCode.SEMANTIC_ERROR.getStatusCode()
98-
+ ": Failed to execute function 'from_base64' due to an invalid input format. Problematic value: not_base64";
99+
+ ": Failed to execute function 'from_base64' due to an invalid input format. Problematic value: ";
99100
tableAssertTestFail(
100101
"SELECT from_base64(c_text) FROM table1 WHERE time = 5",
101-
expectedErrorMessage,
102+
expectedErrorMessage + "not_base64",
103+
DATABASE_NAME);
104+
105+
tableAssertTestFail(
106+
"SELECT from_base64(c_text) FROM table1 WHERE time = 6",
107+
expectedErrorMessage + "abc",
102108
DATABASE_NAME);
103109
}
104110

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/transformation/dag/column/unary/scalar/factory/CodecStrategiesFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public final class CodecStrategiesFactory {
4646
public static final CodecStrategy FROM_BASE64 =
4747
(input) -> {
4848
try {
49+
if (input.length % 4 != 0) {
50+
throw new SemanticException(
51+
"Base64 length must be a multiple of 4 (including padding '=')");
52+
}
4953
return Base64.getDecoder().decode(input);
5054
} catch (IllegalArgumentException e) {
5155
// wrap the specific exception in dependency into a general one for uniform handling in
@@ -71,7 +75,8 @@ public final class CodecStrategiesFactory {
7175
public static final CodecStrategy FROM_BASE32 =
7276
(input) -> {
7377
try {
74-
return GUAVA_BASE32_ENCODING.decode(new String(input, TSFileConfig.STRING_CHARSET));
78+
return GUAVA_BASE32_ENCODING.decode(
79+
new String(input, TSFileConfig.STRING_CHARSET).toUpperCase());
7580
} catch (IllegalArgumentException e) {
7681
throw new SemanticException("decode base32 error");
7782
}

0 commit comments

Comments
 (0)