raminqaf commented on code in PR #30033: URL: https://github.com/apache/flink/pull/30033#discussion_r4642732786
########## docs/data/functions/json/json_quote.yml: ########## @@ -0,0 +1,88 @@ +# 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 + +name: JSON_QUOTE +summary: Returns a string as a quoted JSON string. + +syntax: + sql: JSON_QUOTE(string) + java: $("string").jsonQuote() + python: col("string").json_quote() + +arguments: + - name: string + type: CHAR, VARCHAR, STRING + required: true + description: The string to quote. + +returns: + description: The string in double quotes. A double quote, a backslash, a slash and the control characters backspace, form feed, newline, carriage return and tab are escaped, such as `\"` and `\n`. Every non-ASCII character is escaped as `\uXXXX`. + nulls: Returns NULL if `string` is NULL. + type: "`STRING`, NOT NULL if `string` is NOT NULL." + +notes: + - Other control characters, such as `CHR(1)`, aren't escaped, so the result isn't valid JSON. + - A character above U+FFFF, such as an emoji, is escaped wrongly, and `JSON_UNQUOTE` doesn't give it back. The result still passes `IS JSON`. + - "`JSON_STRING` quotes a string as JSON without these two problems." + +see_also: + - {name: JSON_UNQUOTE, ref: json/json-unquote} + - {name: JSON_STRING, ref: json/json-string} + +examples: + - title: Quote a string + description: Double quotes, backslashes and slashes are escaped. + sql: | + SELECT + JSON_QUOTE('Hello, World!') AS plain, + JSON_QUOTE('say "hi"') AS quotes, + JSON_QUOTE('a/b\c') AS slashes; + result: | + | plain | quotes | slashes | + |-----------------|--------------|-----------| + | "Hello, World!" | "say \"hi\"" | "a\/b\\c" | + + - title: Control characters and non-ASCII characters + description: A tab and a newline are written as escape sequences, and non-ASCII characters as `\u` escapes. + sql: | + SELECT + JSON_QUOTE('tab' || CHR(9) || 'newline' || CHR(10)) AS escaped, + JSON_QUOTE('café') AS non_ascii; + result: | + | escaped | non_ascii | + |------------------|-------------| + | "tab\tnewline\n" | "caf\u00e9" | + + - title: Quote a column + description: A NULL string returns NULL. + sql: | + SELECT s, JSON_QUOTE(s) AS quoted + FROM (VALUES ('a"b'), (CAST(NULL AS STRING))) AS t(s); + result: | + | s | quoted | + |------|--------| + | a"b | "a\"b" | + | NULL | NULL | + + - title: Characters above U+FFFF + description: An emoji is escaped wrongly and doesn't come back from `JSON_UNQUOTE`. + sql: | + SELECT JSON_QUOTE('😀') AS emoji, JSON_UNQUOTE(JSON_QUOTE('😀')) = '😀' AS round_trips; Review Comment: Yes, it's a bug, and it's on our list together with `CHR(1)` not being escaped. We document the current behavior and say that it's wrong, as agreed with Martijn on #29864, since users who try it would otherwise think the docs are wrong. The fix will update this example in the same PR. I'm collecting the bugs found while writing these pages and will file them together once the last categories are 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]
