raminqaf commented on code in PR #30040:
URL: https://github.com/apache/flink/pull/30040#discussion_r4640599464
##########
flink-core/src/main/java/org/apache/flink/util/SwarUtils.java:
##########
@@ -82,6 +82,28 @@ public static boolean containsAnyOrLessThan(
return false;
}
+ /**
+ * Whether {@code bytes[offset, offset + len)} contains a byte below
{@code lessThan}, comparing
+ * bytes unsigned.
+ *
+ * @param lessThan at most {@code 0x80}
+ */
+ public static boolean containsLessThan(byte[] bytes, int offset, int len,
int lessThan) {
Review Comment:
This is very similar to `containsAnyOrLessThan` can we re-use or merge them?
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/functions/SqlJsonUtilsSerializeJsonStringTest.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for {@link SqlJsonUtils#serializeJsonString}. */
+class SqlJsonUtilsSerializeJsonStringTest {
+
+ @ParameterizedTest
+ @ValueSource(
+ strings = {
+ "",
+ "HeLLo WoRLD",
+ "HÉLLo WÖRLD 😀",
+ "\177/",
+ "\"",
+ "\\",
+ "\b\f\n\r\t",
+ "\0\001\037",
+ "x\"y\\z\nw",
+ // above 0xFF, with a char to escape after it
+ "ĢŜĊ",
+ "Ģ\n",
+ // one '?' byte for two chars, so the bytes are shorter than
the string
+ "😀\n",
+ // Latin-1 but not ASCII, with a char to escape after it
+ "É\n",
+ // quote and backslash past the start, found by indexOf
+ "xxxxxxxx\"xxxxxxxx",
+ "xxxxxxxx\\xxxxxxxx",
+ // control chars in the SWAR word loop and in the tail
+ "xxxxxxxx\001xxxxxxxx",
+ "xxxxxxxxxxxxxxxx\n"
+ })
Review Comment:
Nit: a few cases reach the same branch as an earlier one. "ĢŜĊ" behaves like
"HÉLLo WÖRLD 😀".
"Ģ\n", "😀\n" and "É\n" all go the way of "\b\f\n\r\t", so one of them is
enough. Could you trim those?
##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/calls/JsonStringCallGen.scala:
##########
@@ -37,11 +37,17 @@ class JsonStringCallGen(call: RexCall, rexProgram:
RexProgram) extends CallGener
returnType: LogicalType): GeneratedExpression = {
val exprs = CodeGenUtils.getExprsFromProgramOrNull(rexProgram)
+ val operand = call.operands.get(0)
+ // JSON_OBJECT, JSON_ARRAY and JSON results are JSON already and are
passed through as is
+ val isRawJson =
+ isJsonObjectOrArrayOperand(operand, exprs) ||
isJsonFunctionOperand(operand, exprs)
Review Comment:
`isRawJson` repeats the condition in J`sonGenerateUtils.createNodeTerm`.
Could you extract one `isRawJsonOperand(operand, localRefs)` helper in
`JsonGenerateUtils` and call it from both places? Side question: can the
`isJsonFunctionOperand` half ever be true here?
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/functions/SqlJsonUtilsSerializeJsonStringTest.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for {@link SqlJsonUtils#serializeJsonString}. */
+class SqlJsonUtilsSerializeJsonStringTest {
+
+ @ParameterizedTest
+ @ValueSource(
+ strings = {
+ "",
+ "HeLLo WoRLD",
+ "HÉLLo WÖRLD 😀",
+ "\177/",
+ "\"",
+ "\\",
+ "\b\f\n\r\t",
+ "\0\001\037",
+ "x\"y\\z\nw",
+ // above 0xFF, with a char to escape after it
+ "ĢŜĊ",
+ "Ģ\n",
+ // one '?' byte for two chars, so the bytes are shorter than
the string
Review Comment:
Is the comment correct?
##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/calls/JsonStringCallGen.scala:
##########
@@ -37,11 +37,17 @@ class JsonStringCallGen(call: RexCall, rexProgram:
RexProgram) extends CallGener
returnType: LogicalType): GeneratedExpression = {
val exprs = CodeGenUtils.getExprsFromProgramOrNull(rexProgram)
+ val operand = call.operands.get(0)
+ // JSON_OBJECT, JSON_ARRAY and JSON results are JSON already and are
passed through as is
Review Comment:
No test for `JSON_STRING(JSON_ARRAY(...))`
--
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]