gustavodemorais commented on code in PR #28110: URL: https://github.com/apache/flink/pull/28110#discussion_r3187872948
########## flink-table/flink-table-common/src/test/java/org/apache/flink/table/data/binary/BinaryStringDataFromUtf8BytesTest.java: ########## @@ -0,0 +1,106 @@ +/* + * 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.flink.table.data.binary; + +import org.apache.flink.table.data.StringData; + +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Tests for {@link BinaryStringData#fromUtf8Bytes(byte[])} and {@link StringData#fromUtf8Bytes}. + */ +class BinaryStringDataFromUtf8BytesTest { + + @Test + void testFromUtf8Bytes() { + // Valid input: ASCII, multi-byte, empty, and the offset/length variant all yield the + // same instance you would get from the unchecked fromBytes / fromString factory. + final byte[] hello = "Hello, world!".getBytes(StandardCharsets.UTF_8); + assertThat(BinaryStringData.fromUtf8Bytes(hello)) + .isEqualTo(BinaryStringData.fromBytes(hello)); + + final byte[] multibyte = "é€😀".getBytes(StandardCharsets.UTF_8); + assertThat(BinaryStringData.fromUtf8Bytes(multibyte)) + .isEqualTo(BinaryStringData.fromBytes(multibyte)); + + assertThat(BinaryStringData.fromUtf8Bytes(new byte[0])) + .isEqualTo(BinaryStringData.EMPTY_UTF8); + + // Offset/length variant validates only the inner range. + final byte[] padded = {(byte) 0x80, 'O', 'K', (byte) 0x80}; + assertThat(BinaryStringData.fromUtf8Bytes(padded, 1, 2)) + .isEqualTo(BinaryStringData.fromString("OK")); + + // null in -> null out so connector authors can forward upstream nullability without + // an extra guard at every call site. + assertThat(BinaryStringData.fromUtf8Bytes((byte[]) null)).isNull(); + assertThat(BinaryStringData.fromUtf8Bytes(null, 0, 0)).isNull(); + + // StringData facade delegates with the same null behavior. + assertThat(StringData.fromUtf8Bytes(hello)) + .isEqualTo(StringData.fromString("Hello, world!")); + assertThat(StringData.fromUtf8Bytes((byte[]) null)).isNull(); + assertThat(StringData.fromUtf8Bytes(null, 0, 0)).isNull(); + } + + @Test + void testFromUtf8BytesRejectsInvalid() { Review Comment: Yes, I've consolidated tests into StringUtf8UtilsTest and parameterized them ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/data/binary/BinaryStringData.java: ########## @@ -81,20 +81,59 @@ public static BinaryStringData fromString(String str) { } } - /** Creates a {@link BinaryStringData} instance from the given UTF-8 bytes. */ + /** + * Creates a {@link BinaryStringData} instance by wrapping the given UTF-8 bytes in O(1) without + * copying or validating them. The caller is responsible for ensuring the bytes are well-formed + * UTF-8; use {@link #fromUtf8Bytes(byte[])} if validation is required. + */ public static BinaryStringData fromBytes(byte[] bytes) { return fromBytes(bytes, 0, bytes.length); } /** - * Creates a {@link BinaryStringData} instance from the given UTF-8 bytes with offset and number - * of bytes. + * Creates a {@link BinaryStringData} instance by wrapping the given UTF-8 byte range in O(1) + * without copying or validating it. The caller is responsible for ensuring the bytes are + * well-formed UTF-8; use {@link #fromUtf8Bytes(byte[], int, int)} if validation is required. */ public static BinaryStringData fromBytes(byte[] bytes, int offset, int numBytes) { return new BinaryStringData( new MemorySegment[] {MemorySegmentFactory.wrap(bytes)}, offset, numBytes); } + /** + * Creates a {@link BinaryStringData} instance from the given UTF-8 bytes, walking the input + * once in O(n) to verify it is well-formed UTF-8. Returns {@code null} if the input is {@code + * null}. Throws {@link IllegalArgumentException} on invalid UTF-8. Use {@link Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
