|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.relational.it.query.recent.scalar; |
| 21 | + |
| 22 | +import org.apache.iotdb.it.env.EnvFactory; |
| 23 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 24 | +import org.apache.iotdb.itbase.category.TableClusterIT; |
| 25 | +import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; |
| 26 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 27 | + |
| 28 | +import org.junit.AfterClass; |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.experimental.categories.Category; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | + |
| 34 | +import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData; |
| 35 | +import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail; |
| 36 | +import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest; |
| 37 | + |
| 38 | +@RunWith(IoTDBTestRunner.class) |
| 39 | +@Category({TableLocalStandaloneIT.class, TableClusterIT.class}) |
| 40 | +public class IoTDBFromBase32ColumnFunctionIT { |
| 41 | + |
| 42 | + private static final String DATABASE_NAME = "test_from_base32_function"; |
| 43 | + |
| 44 | + private static final String[] createSqls = |
| 45 | + new String[] { |
| 46 | + "CREATE DATABASE " + DATABASE_NAME, |
| 47 | + "USE " + DATABASE_NAME, |
| 48 | + "CREATE TABLE table1(c_text TEXT, c_string STRING, c_int INT32)", |
| 49 | + |
| 50 | + // Case 1: Basic ASCII string |
| 51 | + // 'IoTDB is fun!' -> Base32: JBCGC3LFN5ZGIIDUNBSSA3LSEI====== -> BLOB: |
| 52 | + "INSERT INTO table1(time, c_text, c_string) VALUES (1, 'JBCGC3LFN5ZGIIDUNBSSA3LSEI======', 'JBCGC3LFN5ZGIIDUNBSSA3LSEI======')", |
| 53 | + |
| 54 | + // Case 2: UTF-8 string |
| 55 | + // '你好, 世界!' -> Base32: 2W4V625443J56W4S4E73H5BUMM4A====== -> BLOB: |
| 56 | + // X'e4bda0e5a5bd2c20e4b896e7958c21' |
| 57 | + "INSERT INTO table1(time, c_text, c_string) VALUES (2, '2W4V625443J56W4S4E73H5BUMM4A======', '2W4V625443J56W4S4E73H5BUMM4A======')", |
| 58 | + |
| 59 | + // Case 3: Empty string |
| 60 | + // '' -> Base32: '' -> BLOB: X'' |
| 61 | + "INSERT INTO table1(time, c_text, c_string) VALUES (3, '', '')", |
| 62 | + |
| 63 | + // Case 4: Null value |
| 64 | + "INSERT INTO table1(time, c_int) VALUES (4, 100)", |
| 65 | + |
| 66 | + // Case 5: Padding scenarios for 1-4 bytes |
| 67 | + // 'f' (0x66) -> Base32: MY====== |
| 68 | + "INSERT INTO table1(time, c_text, c_string) VALUES (5, 'MY======', 'MY======')", |
| 69 | + // 'fo' (0x666f) -> Base32: MZXQ==== |
| 70 | + "INSERT INTO table1(time, c_text, c_string) VALUES (6, 'MZXQ====', 'MZXQ====')", |
| 71 | + // 'foo' (0x666f6f) -> Base32: MZXW6=== |
| 72 | + "INSERT INTO table1(time, c_text, c_string) VALUES (7, 'MZXW6===', 'MZXW6===')", |
| 73 | + // 'foob' (0x666f6f62) -> Base32: MZXW6YQ= |
| 74 | + "INSERT INTO table1(time, c_text, c_string) VALUES (8, 'MZXW6YQ=', 'MZXW6YQ=')", |
| 75 | + |
| 76 | + // Case 9: No padding needed (5 bytes) |
| 77 | + // 'fooba' -> Base32: MZXW6YQB |
| 78 | + "INSERT INTO table1(time, c_text, c_string) VALUES (9, 'MZXW6YQB', 'MZXW6YQB')", |
| 79 | + |
| 80 | + // Case 10: Optional padding test (decoder should handle missing padding) |
| 81 | + // 'foo' (0x666f6f) -> Base32 without padding: MZXW6 |
| 82 | + "INSERT INTO table1(time, c_text, c_string) VALUES (10, 'MZXW6', 'MZXW6')", |
| 83 | + }; |
| 84 | + |
| 85 | + @BeforeClass |
| 86 | + public static void setUp() throws Exception { |
| 87 | + EnvFactory.getEnv().initClusterEnvironment(); |
| 88 | + prepareTableData(createSqls); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterClass |
| 92 | + public static void tearDown() throws Exception { |
| 93 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 94 | + } |
| 95 | + |
| 96 | + /** Validates the from_base32() function on various supported and valid inputs. */ |
| 97 | + @Test |
| 98 | + public void testFromBase32OnValidInputs() { |
| 99 | + String[] expectedHeader = new String[] {"time", "from_base32(c_text)", "from_base32(c_string)"}; |
| 100 | + String[] retArray = |
| 101 | + new String[] { |
| 102 | + // 1. Basic ASCII |
| 103 | + "1970-01-01T00:00:00.001Z,0x4844616d656f726420746865206d7222,0x4844616d656f726420746865206d7222,", |
| 104 | + // 2. UTF-8 |
| 105 | + "1970-01-01T00:00:00.002Z,0xd5b95f6bbce6d3df5b92e13fb3f4346338,0xd5b95f6bbce6d3df5b92e13fb3f4346338,", |
| 106 | + // 3. Empty string |
| 107 | + "1970-01-01T00:00:00.003Z,0x,0x,", |
| 108 | + // 4. Null input |
| 109 | + "1970-01-01T00:00:00.004Z,null,null,", |
| 110 | + // 5. 1 byte with padding |
| 111 | + "1970-01-01T00:00:00.005Z,0x66,0x66,", |
| 112 | + // 6. 2 bytes with padding |
| 113 | + "1970-01-01T00:00:00.006Z,0x666f,0x666f,", |
| 114 | + // 7. 3 bytes with padding |
| 115 | + "1970-01-01T00:00:00.007Z,0x666f6f,0x666f6f,", |
| 116 | + // 8. 4 bytes with padding |
| 117 | + "1970-01-01T00:00:00.008Z,0x666f6f62,0x666f6f62,", |
| 118 | + // 9. 5 bytes, no padding |
| 119 | + "1970-01-01T00:00:00.009Z,0x666f6f6201,0x666f6f6201,", |
| 120 | + // 10. Optional padding |
| 121 | + "1970-01-01T00:00:00.010Z,0x666f6f,0x666f6f,", |
| 122 | + }; |
| 123 | + |
| 124 | + tableResultSetEqualTest( |
| 125 | + "SELECT time, from_base32(c_text) as \"from_base32(c_text)\", from_base32(c_string) as \"from_base32(c_string)\" FROM table1", |
| 126 | + expectedHeader, |
| 127 | + retArray, |
| 128 | + DATABASE_NAME); |
| 129 | + } |
| 130 | + |
| 131 | + /** Tests for invalid arguments passed to the from_base32() function. */ |
| 132 | + @Test |
| 133 | + public void testFromBase32FunctionOnInvalidArguments() { |
| 134 | + String expectedErrorMessage = |
| 135 | + TSStatusCode.SEMANTIC_ERROR.getStatusCode() |
| 136 | + + ": Scalar function from_base32 only accepts one argument and it must be TEXT or STRING data type."; |
| 137 | + |
| 138 | + // Test with invalid parameter type (INT32) |
| 139 | + tableAssertTestFail( |
| 140 | + "SELECT from_base32(c_int) FROM table1", expectedErrorMessage, DATABASE_NAME); |
| 141 | + |
| 142 | + // Test with invalid parameter count (0) |
| 143 | + tableAssertTestFail("SELECT from_base32() FROM table1", expectedErrorMessage, DATABASE_NAME); |
| 144 | + |
| 145 | + // Test with invalid parameter count (>1) |
| 146 | + tableAssertTestFail( |
| 147 | + "SELECT from_base32(c_text, c_string) FROM table1", expectedErrorMessage, DATABASE_NAME); |
| 148 | + } |
| 149 | + |
| 150 | + /** Validates that from_base32() fails when given incorrectly formatted Base32 strings. */ |
| 151 | + @Test |
| 152 | + public void testFromBase32FunctionOnInvalidDataFormat() { |
| 153 | + String baseErrorMessage = |
| 154 | + TSStatusCode.SEMANTIC_ERROR.getStatusCode() |
| 155 | + + ": Failed to execute function 'from_base32' due to an invalid input format. Problematic value:"; |
| 156 | + |
| 157 | + // Invalid character: '0' (not in Base32 alphabet) |
| 158 | + tableAssertTestFail( |
| 159 | + "SELECT from_base32('JBCGC3LFN5ZGIIDUNBSSA3LSE0==') FROM table1", // [FIXED] Added FROM |
| 160 | + // clause |
| 161 | + baseErrorMessage + " JBCGC3LFN5ZGIIDUNBSSA3LSE0==", |
| 162 | + DATABASE_NAME); |
| 163 | + |
| 164 | + // Invalid character: '1' (not in Base32 alphabet) |
| 165 | + tableAssertTestFail( |
| 166 | + "SELECT from_base32('JBCGC3LFN5ZGIIDUNBSSA3LSE1==') FROM table1", // [FIXED] Added FROM |
| 167 | + // clause |
| 168 | + baseErrorMessage + " JBCGC3LFN5ZGIIDUNBSSA3LSE1==", |
| 169 | + DATABASE_NAME); |
| 170 | + |
| 171 | + // Invalid character: '8' (not in Base32 alphabet) |
| 172 | + tableAssertTestFail( |
| 173 | + "SELECT from_base32('JBCGC3LFN5ZGIIDUNBSSA3LSE8==') FROM table1", // [FIXED] Added FROM |
| 174 | + // clause |
| 175 | + baseErrorMessage + " JBCGC3LFN5ZGIIDUNBSSA3LSE8==", |
| 176 | + DATABASE_NAME); |
| 177 | + |
| 178 | + // Invalid character: '9' (not in Base32 alphabet) |
| 179 | + tableAssertTestFail( |
| 180 | + "SELECT from_base32('JBCGC3LFN5ZGIIDUNBSSA3LSE9==') FROM table1", // [FIXED] Added FROM |
| 181 | + // clause |
| 182 | + baseErrorMessage + " JBCGC3LFN5ZGIIDUNBSSA3LSE9==", |
| 183 | + DATABASE_NAME); |
| 184 | + |
| 185 | + // Invalid character: '-' (from Base64URL) |
| 186 | + tableAssertTestFail( |
| 187 | + "SELECT from_base32('MZXW6YQ-') FROM table1", // [FIXED] Added FROM clause |
| 188 | + baseErrorMessage + " MZXW6YQ-", |
| 189 | + DATABASE_NAME); |
| 190 | + |
| 191 | + // Invalid padding: padding character in the middle |
| 192 | + tableAssertTestFail( |
| 193 | + "SELECT from_base32('MZXW6=Q=') FROM table1", // [FIXED] Added FROM clause |
| 194 | + baseErrorMessage + " MZXW6=Q=", |
| 195 | + DATABASE_NAME); |
| 196 | + } |
| 197 | +} |
0 commit comments