snuyanzin commented on code in PR #30146:
URL: https://github.com/apache/flink/pull/30146#discussion_r4792877611


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/JsonPathReader.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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.apache.flink.annotation.Internal;
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.data.binary.StringUtf8Utils;
+import org.apache.flink.table.runtime.functions.SqlJsonUtils.JsonValueContext;
+
+import org.apache.flink.shaded.com.jayway.jsonpath.JsonPath;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.UnaryOperator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Parses a JSON document like {@link SqlJsonUtils#jsonParse}, but builds only 
what fixed paths
+ * reach: the value at the end of a path in full, and the objects and arrays 
on the way with only
+ * the members the paths need. An array keeps its length, with null for the 
elements no path needs,
+ * so that JsonPath evaluates the paths on the result as on the whole document.
+ *
+ * <p>A document this reader fails on is parsed by {@link 
SqlJsonUtils#jsonParse}, which reports the
+ * failure as before. Jackson's limit on the length of a string applies only 
to the strings this
+ * reader builds.
+ */
+@Internal
+public final class JsonPathReader implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    // a property or an index of a compiled JsonPath, as it prints them
+    private static final Pattern STEP =
+            Pattern.compile("\\['([^'\\\\]*)']|\\[(0|[1-9][0-9]{0,8})]");
+
+    // Bytes are always UTF-8, as the decoding of the full parse reads them. 
Charset detection would
+    // skip a BOM or read NUL bytes as UTF-16 or UTF-32.
+    private static final JsonFactory BYTES_FACTORY =
+            SqlJsonUtils.JSON_FACTORY
+                    .rebuild()
+                    .disable(JsonFactory.Feature.CHARSET_DETECTION)
+                    .build();
+
+    // read-only, so the reader is stateless
+    private final JsonPathNode root;
+
+    /**
+     * @param paths the steps of each path, see {@link #steps}
+     */
+    public JsonPathReader(Object[][] paths) {
+        this.root = JsonPathNode.of(paths, UnaryOperator.identity());
+    }
+
+    /**
+     * The steps of a JSONPath that JsonPath reads as properties and indices 
from {@code $} only: a
+     * {@link String} for a property, an {@link Integer} for an index. Null 
for any other path.
+     */
+    @Nullable
+    public static Object[] steps(String path) {
+        // JsonPath prints a name without escaping it, so a name with a quote, 
which only an escape
+        // or double quotes allow, can't be read back from the compiled path
+        if (path.indexOf('\\') >= 0 || path.indexOf('"') >= 0) {
+            return null;
+        }
+        final String compiled;
+        try {
+            // as JsonPath reads it, like "$['address']['city']" for 
"$.address.city"
+            compiled = JsonPath.compile(path).getPath();

Review Comment:
   yep, makes sense turned to simplepath



-- 
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]

Reply via email to