raminqaf commented on code in PR #30146: URL: https://github.com/apache/flink/pull/30146#discussion_r4787870981
########## 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: Question on the design: steps reads the steps back from `JsonPath.compile(path).getPath()` with a regex. `SimplePath` in `flink-table-common` already parses key and index paths, and `SelectiveParseJsonCodeGen` uses it for `VARIANT_GET`. Why not `SimplePath.parse(path).getSteps()` here, with the full parse when it throws? It handles escaped quotes, so the backslash and double-quote check could go. ########## 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(); + } catch (RuntimeException e) { + return null; + } + if (!compiled.startsWith("$")) { + return null; + } + final List<Object> steps = new ArrayList<>(); + final Matcher matcher = STEP.matcher(compiled); + int position = 1; + while (position < compiled.length()) { + if (!matcher.find(position) || matcher.start() != position) { + return null; + } + if (matcher.group(1) != null) { + steps.add(matcher.group(1)); + } else { + steps.add(Integer.parseInt(matcher.group(2))); + } + position = matcher.end(); + } + return steps.toArray(); + } + + /** + * Parses {@code json} for the paths, from its UTF-8 bytes unless it has a Java string already. + */ + public JsonValueContext parse(StringData json) { + final BinaryStringData string = (BinaryStringData) json; + if (string.getJavaObject() != null) { + // a lone surrogate of the string is encoded as '?', so the string is read as it is + return parse(string.toString()); + } + final MemorySegment[] segments = string.getSegments(); + final int offset; + final byte[] bytes; + final int size = string.getSizeInBytes(); + // null off the heap + final byte[] heap = segments[0].getHeapMemory(); + if (heap != null && string.getOffset() + size <= segments[0].size()) { + bytes = heap; + offset = string.getOffset(); + } else { + bytes = string.toBytes(); + offset = 0; + } + // bytes the decoder of the full parse replaces by U+FFFD may be read differently here + if (StringUtf8Utils.firstInvalidUtf8ByteIndex(bytes, offset, size) < 0) { + try (JsonParser parser = BYTES_FACTORY.createParser(bytes, offset, size)) { + final JsonValueContext parsed = read(parser); + if (parsed != null) { + return parsed; + } + } catch (Exception e) { + // reported by the full parse + } + } + return SqlJsonUtils.jsonParse(string.toString()); + } + + /** Parses {@code json} for the paths. */ + public JsonValueContext parse(String json) { + try (JsonParser parser = SqlJsonUtils.JSON_FACTORY.createParser(json)) { + final JsonValueContext parsed = read(parser); + if (parsed != null) { + return parsed; + } + } catch (Exception e) { + // reported by the full parse + } + return SqlJsonUtils.jsonParse(json); + } + + // null for a document without content + @Nullable + private JsonValueContext read(JsonParser parser) throws Exception { + final JsonToken token = parser.nextToken(); + // like the full parse, what follows the root value isn't read + return token == null ? null : JsonValueContext.withJavaObj(read(parser, token, root)); + } + + @Nullable + private static Object read(JsonParser parser, JsonToken token, JsonPathNode node) + throws Exception { Review Comment: Isn't this IOException? ########## 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(); + } catch (RuntimeException e) { + return null; + } + if (!compiled.startsWith("$")) { + return null; + } + final List<Object> steps = new ArrayList<>(); + final Matcher matcher = STEP.matcher(compiled); + int position = 1; + while (position < compiled.length()) { + if (!matcher.find(position) || matcher.start() != position) { + return null; + } + if (matcher.group(1) != null) { + steps.add(matcher.group(1)); + } else { + steps.add(Integer.parseInt(matcher.group(2))); + } + position = matcher.end(); + } + return steps.toArray(); + } + + /** + * Parses {@code json} for the paths, from its UTF-8 bytes unless it has a Java string already. + */ + public JsonValueContext parse(StringData json) { + final BinaryStringData string = (BinaryStringData) json; + if (string.getJavaObject() != null) { + // a lone surrogate of the string is encoded as '?', so the string is read as it is + return parse(string.toString()); + } + final MemorySegment[] segments = string.getSegments(); + final int offset; + final byte[] bytes; + final int size = string.getSizeInBytes(); + // null off the heap + final byte[] heap = segments[0].getHeapMemory(); + if (heap != null && string.getOffset() + size <= segments[0].size()) { + bytes = heap; + offset = string.getOffset(); + } else { + bytes = string.toBytes(); + offset = 0; + } + // bytes the decoder of the full parse replaces by U+FFFD may be read differently here + if (StringUtf8Utils.firstInvalidUtf8ByteIndex(bytes, offset, size) < 0) { + try (JsonParser parser = BYTES_FACTORY.createParser(bytes, offset, size)) { + final JsonValueContext parsed = read(parser); + if (parsed != null) { + return parsed; + } + } catch (Exception e) { + // reported by the full parse + } + } + return SqlJsonUtils.jsonParse(string.toString()); + } + + /** Parses {@code json} for the paths. */ + public JsonValueContext parse(String json) { Review Comment: A reason this is public? ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/JsonFunctionsITCase.java: ########## Review Comment: `JSON_EXISTS` with a non-literal path has no ITCase -- 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]
