-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-39602][table] Add IS_VALID_UTF8 and MAKE_VALID_UTF8 built-in functions #28111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5e76616
[FLINK-39602][table] Add IS_VALID_UTF8 and MAKE_VALID_UTF8 built-in f…
gustavodemorais 34ace5d
[FLINK-39602][table] Address review: docs wording
gustavodemorais b03222d
[FLINK-39602][table] Delegate IS_VALID_UTF8 to StringUtf8Utils after …
gustavodemorais 57dddc2
[FLINK-39602][python] Add Python bindings for IS_VALID_UTF8 and MAKE_…
gustavodemorais File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...e-planner/src/test/java/org/apache/flink/table/planner/functions/Utf8FunctionsITCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * 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.planner.functions; | ||
|
|
||
| import org.apache.flink.table.api.DataTypes; | ||
| import org.apache.flink.table.functions.BuiltInFunctionDefinitions; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.apache.flink.table.api.Expressions.$; | ||
|
|
||
| /** | ||
| * Tests for {@link BuiltInFunctionDefinitions#IS_VALID_UTF8} and {@link | ||
| * BuiltInFunctionDefinitions#MAKE_VALID_UTF8}. | ||
| */ | ||
| public class Utf8FunctionsITCase extends BuiltInFunctionTestBase { | ||
|
|
||
| private static final byte[] HELLO = "Hello".getBytes(StandardCharsets.UTF_8); | ||
| private static final byte[] MULTIBYTE = "é€😀".getBytes(StandardCharsets.UTF_8); | ||
| private static final byte[] INVALID_START = {(byte) 0x80}; | ||
| private static final byte[] TRUNCATED = {(byte) 0xE2, (byte) 0x82}; | ||
| private static final byte[] OVERLONG = {(byte) 0xC0, (byte) 0xAF}; | ||
| private static final byte[] SURROGATE = {(byte) 0xED, (byte) 0xA0, (byte) 0x80}; | ||
|
|
||
| @Override | ||
| Stream<TestSetSpec> getTestSetSpecs() { | ||
| return Stream.of(isValidUtf8Cases(), makeValidUtf8Cases()); | ||
| } | ||
|
|
||
| private TestSetSpec isValidUtf8Cases() { | ||
| return TestSetSpec.forFunction(BuiltInFunctionDefinitions.IS_VALID_UTF8) | ||
| .onFieldsWithData( | ||
| null, HELLO, MULTIBYTE, INVALID_START, TRUNCATED, OVERLONG, SURROGATE) | ||
| .andDataTypes( | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES()) | ||
| .testSqlResult("IS_VALID_UTF8(f0)", null, DataTypes.BOOLEAN().nullable()) | ||
| .testSqlResult("IS_VALID_UTF8(f1)", true, DataTypes.BOOLEAN().nullable()) | ||
| .testSqlResult("IS_VALID_UTF8(f2)", true, DataTypes.BOOLEAN().nullable()) | ||
| .testSqlResult("IS_VALID_UTF8(f3)", false, DataTypes.BOOLEAN().nullable()) | ||
| .testSqlResult("IS_VALID_UTF8(f4)", false, DataTypes.BOOLEAN().nullable()) | ||
| .testSqlResult("IS_VALID_UTF8(f5)", false, DataTypes.BOOLEAN().nullable()) | ||
| .testSqlResult("IS_VALID_UTF8(f6)", false, DataTypes.BOOLEAN().nullable()) | ||
| // Table API method routes to the same definition. | ||
| .testTableApiResult($("f1").isValidUtf8(), true, DataTypes.BOOLEAN().nullable()) | ||
| .testTableApiResult($("f3").isValidUtf8(), false, DataTypes.BOOLEAN().nullable()); | ||
| } | ||
|
|
||
| private TestSetSpec makeValidUtf8Cases() { | ||
| // Lenient decode equivalent to new String(b, UTF_8). Java follows the W3C | ||
| // maximal-subpart rule: one replacement char per maximal ill-formed subpart. | ||
| final byte[] mixed = {'A', 'B', (byte) 0x80, 'C', 'D'}; | ||
| return TestSetSpec.forFunction(BuiltInFunctionDefinitions.MAKE_VALID_UTF8) | ||
| .onFieldsWithData( | ||
| null, | ||
| HELLO, | ||
| MULTIBYTE, | ||
| INVALID_START, | ||
| TRUNCATED, | ||
| OVERLONG, | ||
| SURROGATE, | ||
| mixed) | ||
| .andDataTypes( | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES(), | ||
| DataTypes.BYTES()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f0)", null, DataTypes.STRING().nullable()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f1)", "Hello", DataTypes.STRING().nullable()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f2)", "é€😀", DataTypes.STRING().nullable()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f3)", "�", DataTypes.STRING().nullable()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f4)", "�", DataTypes.STRING().nullable()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f5)", "��", DataTypes.STRING().nullable()) | ||
| // JDK's UTF-8 decoder consumes the entire 3-byte surrogate attempt and emits | ||
| // a single U+FFFD; this differs from the W3C maximal-subpart count of 3. | ||
| .testSqlResult("MAKE_VALID_UTF8(f6)", "�", DataTypes.STRING().nullable()) | ||
| .testSqlResult("MAKE_VALID_UTF8(f7)", "AB�CD", DataTypes.STRING().nullable()) | ||
| .testTableApiResult($("f1").makeValidUtf8(), "Hello", DataTypes.STRING().nullable()) | ||
| .testTableApiResult($("f3").makeValidUtf8(), "�", DataTypes.STRING().nullable()); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...me/src/main/java/org/apache/flink/table/runtime/functions/scalar/IsValidUtf8Function.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * 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.runtime.functions.scalar; | ||
|
|
||
| import org.apache.flink.annotation.Internal; | ||
| import org.apache.flink.table.data.binary.StringUtf8Utils; | ||
| import org.apache.flink.table.functions.BuiltInFunctionDefinitions; | ||
| import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Implementation of {@link BuiltInFunctionDefinitions#IS_VALID_UTF8}. */ | ||
| @Internal | ||
| public final class IsValidUtf8Function extends BuiltInScalarFunction { | ||
|
|
||
| public IsValidUtf8Function(SpecializedContext context) { | ||
| super(BuiltInFunctionDefinitions.IS_VALID_UTF8, context); | ||
| } | ||
|
|
||
| public @Nullable Boolean eval(final @Nullable byte[] bytes) { | ||
| if (bytes == null) { | ||
| return null; | ||
| } | ||
| return StringUtf8Utils.firstInvalidUtf8ByteIndex(bytes, 0, bytes.length) < 0; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
.../src/main/java/org/apache/flink/table/runtime/functions/scalar/MakeValidUtf8Function.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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.runtime.functions.scalar; | ||
|
|
||
| import org.apache.flink.annotation.Internal; | ||
| import org.apache.flink.table.data.StringData; | ||
| import org.apache.flink.table.functions.BuiltInFunctionDefinitions; | ||
| import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** | ||
| * Implementation of {@link BuiltInFunctionDefinitions#MAKE_VALID_UTF8}. | ||
| * | ||
| * <p>Decodes UTF-8 bytes leniently, replacing each invalid sequence with the Unicode replacement | ||
| * character {@code U+FFFD}. The substitution is lossy and irreversible. | ||
| */ | ||
| @Internal | ||
| public final class MakeValidUtf8Function extends BuiltInScalarFunction { | ||
|
|
||
| public MakeValidUtf8Function(SpecializedContext context) { | ||
| super(BuiltInFunctionDefinitions.MAKE_VALID_UTF8, context); | ||
| } | ||
|
|
||
| public @Nullable StringData eval(final @Nullable byte[] bytes) { | ||
| if (bytes == null) { | ||
| return null; | ||
| } | ||
| return StringData.fromString(new String(bytes, StandardCharsets.UTF_8)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was expecting. boolean return type here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is expression API. You are constructing expressions, not evaluating data. The return type is another expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey David, isValidUtf8() doesn't run the check - it just builds a small piece of an SQL plan that says "validate UTF-8 here". The actual true/false is computed later, on every row, on the cluster. So the method has to return something you can keep chaining onto (.and(...), .filter(...), etc.) - that's what OutType is. It's the same return type every other Table API method uses, like isNull() or like()