lincoln-lil commented on code in PR #25176: URL: https://github.com/apache/flink/pull/25176#discussion_r1716717356
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/UnhexFunction.java: ########## @@ -0,0 +1,66 @@ +/* + * 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; + +/** Implementation of {@link BuiltInFunctionDefinitions#UNHEX}. */ +@Internal +public class UnhexFunction extends BuiltInScalarFunction { + + public UnhexFunction(SpecializedContext context) { + super(BuiltInFunctionDefinitions.UNHEX, context); + } + + public @Nullable byte[] eval(@Nullable StringData expr) { + if (expr == null) { + return null; + } + return unhex(expr.toBytes()); + } + + /** Inspired from {@link org.apache.flink.table.utils.EncodingUtils#decodeHex(String)}. */ + public static byte[] unhex(final byte[] bytes) { Review Comment: Make this unhex private or move it to `EncodingUtils`(add corresponding tests to `EncodingUtilsTest`). ########## docs/data/sql_functions.yml: ########## @@ -238,6 +238,13 @@ arithmetic: NUMERIC.hex() STRING.hex() description: Returns a string representation of an integer NUMERIC value or a STRING in hex format. Returns NULL if the argument is NULL. E.g. a numeric 20 leads to "14", a numeric 100 leads to "64", a string "hello,world" leads to "68656C6C6F2C776F726C64". + - sql: UNHEX(expr) + table: expr.unhex() + description: | + Converts hexadecimal string expr to BINARY. + If the length of expr is odd, the first character is discarded and the result is left padded with a null byte. + expr <CHAR | VARCHAR> Review Comment: Add a blank line before and after this one to make it look a little clearer. ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/MathFunctionsITCase.java: ########## @@ -141,4 +178,52 @@ Stream<TestSetSpec> getTestSetSpecs() { new BigDecimal("123.45"), DataTypes.DECIMAL(6, 2).notNull())); } + + private Stream<TestSetSpec> unhexTestCases() { Review Comment: Add a case: UNHEX(HEX('😀') -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org