fhueske commented on code in PR #24967: URL: https://github.com/apache/flink/pull/24967#discussion_r1672309798
########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/JsonFunctionsITCase.java: ########## @@ -794,6 +796,218 @@ private static List<TestSetSpec> jsonObjectSpec() { STRING().notNull())); } + private static List<TestSetSpec> jsonQuoteSpec() { + + return Arrays.asList( + TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_QUOTE) + .onFieldsWithData(0) + .testResult( + nullOf(STRING()).jsonQuote(), + "JSON_QUOTE(CAST(NULL AS STRING))", + null, + STRING().nullable()), + TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_QUOTE) + .onFieldsWithData( + "V", + "\"null\"", + "[1, 2, 3]", + "This is a \t test \n with special characters: \" \\ \b \f \r \u0041", + "\"kv_pair_test\": \"\\b\\f\\r\"", + "\ttab and fwd slash /", + "this will not be quoted \\u006z", + "this will be quoted ≠", + null) + .andDataTypes( + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().nullable()) + .testResult( + $("f0").jsonQuote(), "JSON_QUOTE(f0)", "\"V\"", STRING().notNull()) + .testResult( + $("f1").jsonQuote(), + "JSON_QUOTE(f1)", + "\"\\\"null\\\"\"", + STRING().notNull()) + .testResult( + $("f2").jsonQuote(), + "JSON_QUOTE(f2)", + "\"[1, 2, 3]\"", + STRING().notNull()) + .testResult( + $("f3").jsonQuote(), + "JSON_QUOTE(f3)", + "\"This is a \\t test \\n with special characters: \\\" \\\\ \\b \\f \\r A\"", + STRING().notNull()) + .testResult( + $("f4").jsonQuote(), + "JSON_QUOTE(f4)", + "\"\\\"kv_pair_test\\\": \\\"\\\\b\\\\f\\\\r\\\"\"", + STRING().notNull()) + .testResult( + $("f5").jsonQuote(), + "JSON_QUOTE(f5)", + "\"\\ttab and fwd slash \\/\"", + STRING().notNull()) + .testResult( + $("f6").jsonQuote(), + "JSON_QUOTE(f6)", + "\"this will not be quoted \\\\u006z\"", + STRING().notNull()) + .testResult( + $("f7").jsonQuote(), + "JSON_QUOTE(f7)", + "\"this will be quoted \\u2260\"", + STRING().notNull()) + .testResult( + $("f8").jsonQuote(), "JSON_QUOTE(f8)", null, STRING().nullable())); + } + + private static List<TestSetSpec> jsonUnquoteSpecWithValidInput() { + + return Arrays.asList( + TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_UNQUOTE) + .onFieldsWithData(0) + .testResult( + nullOf(STRING()).jsonQuote(), + "JSON_UNQUOTE(CAST(NULL AS STRING))", + null, + STRING().nullable()), + TestSetSpec.forFunction(BuiltInFunctionDefinitions.JSON_UNQUOTE) + .onFieldsWithData( + "\"abc\"", + "\"[\"abc\"]\"", + "\"[\"\\u0041\"]\"", + "\"\\u0041\"", + "\"[1,2,3]", + "\"[1, 2, 3}", + "\"", + "\"[\"\\t\\u0032\"]\"", + "\"[\"This is a \\t test \\n with special characters: \\b \\f \\r \\u0041\"]\"", + "\"\"\"", + "\"\"\ufffa\"", + "\"a unicode \u2260\"", + "\"valid unicode literal \\uD801\\uDC00\"") + .andDataTypes( + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull(), + STRING().notNull()) + .testResult( + $("f0").jsonUnquote(), + "JSON_UNQUOTE(f0)", + "abc", + STRING().notNull()) + .testResult( + $("f1").jsonUnquote(), + "JSON_UNQUOTE(f1)", + "[\"abc\"]", + STRING().notNull()) + .testResult( + $("f2").jsonUnquote(), + "JSON_UNQUOTE(f2)", + "[\"A\"]", + STRING().notNull()) + .testResult( + $("f3").jsonUnquote(), "JSON_UNQUOTE(f3)", "A", STRING().notNull()) + .testResult( + $("f4").jsonUnquote(), + "JSON_UNQUOTE(f4)", + "\"[1,2,3]", + STRING().notNull()) + .testResult( + $("f5").jsonUnquote(), + "JSON_UNQUOTE(f5)", + "\"[1, 2, 3}", + STRING().notNull()) + .testResult( + $("f6").jsonUnquote(), "JSON_UNQUOTE(f6)", "\"", STRING().notNull()) + .testResult( + $("f7").jsonUnquote(), + "JSON_UNQUOTE(f7)", + "[\"\t2\"]", + STRING().notNull()) + .testResult( + $("f8").jsonUnquote(), + "JSON_UNQUOTE(f8)", + "[\"This is a \t test \n with special characters: \b \f \r A\"]", + STRING().notNull()) + .testResult( + $("f9").jsonUnquote(), "JSON_UNQUOTE(f9)", "\"", STRING().notNull()) + .testResult( + $("f10").jsonUnquote(), + "JSON_UNQUOTE(f10)", + "\"\ufffa", + STRING().notNull()) + .testResult( + $("f11").jsonUnquote(), + "JSON_UNQUOTE(f11)", + "a unicode ≠", + STRING().notNull()) + .testResult( + $("f12").jsonUnquote(), + "JSON_UNQUOTE(f12)", + "valid unicode literal \uD801\uDC00", + STRING().notNull())); + } + + private static List<TestSetSpec> jsonUnquoteSpecWithInValidInput() { Review Comment: nit: ```suggestion private static List<TestSetSpec> jsonUnquoteSpecWithInvalidInput() { ``` ########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/JsonUnquoteFunction.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.BinaryStringData; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; +import org.apache.flink.table.runtime.functions.SqlJsonUtils; + +import javax.annotation.Nullable; + +/** Implementation of {@link BuiltInFunctionDefinitions#JSON_UNQUOTE}. */ +@Internal +public class JsonUnquoteFunction extends BuiltInScalarFunction { + + public JsonUnquoteFunction(SpecializedContext context) { + super(BuiltInFunctionDefinitions.JSON_UNQUOTE, context); + } + + private static boolean isValidJsonVal(String jsonInString) { + // See also BuiltInMethods.scala, IS_JSON_VALUE + return SqlJsonUtils.isJsonValue(jsonInString); + } + + private String unquote(String inputStr) { + + StringBuilder result = new StringBuilder(); + int i = 1; Review Comment: we are skipping the first and the last character without checking whether they are double quotes. The string `[1, 2, 3]` is valid JSON and we return `1, 2, 3`. I think we also need to check if the input string is surrounded by double quotes (even before checking if it's valid JSON) and return the input string if the quotes are missing. For reference, this is MySQL's output: ``` mysql> select json_unquote('[1, 2, 3]'); +---------------------------+ | json_unquote('[1, 2, 3]') | +---------------------------+ | [1, 2, 3] | +---------------------------+ 1 row in set (0.00 sec) mysql> select json_unquote('"[1, 2, 3]"'); +-----------------------------+ | json_unquote('"[1, 2, 3]"') | +-----------------------------+ | [1, 2, 3] | +-----------------------------+ 1 row in set (0.00 sec) ``` These cases should also be covered by tests. -- 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