rubenada commented on code in PR #5266:
URL: https://github.com/apache/calcite/pull/5266#discussion_r4024486535
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java:
##########
@@ -136,10 +138,57 @@ static String stripQuotes(String s) {
? s.substring(1, s.length() - 1) : s;
}
- private static String escapeSpecialSymbols(String s) {
+ /** Escapes the two characters that would terminate a JSON string. A SQL
+ * name or alias concatenated between double quotes stays a single JSON
+ * string only if backslash and double quote are doubled. Callers that
+ * embed the value inside a JSON body must route it through this helper
+ * before calling {@link #quote}. */
+ static String escapeSpecialSymbols(String s) {
return s.replace("\\", "\\\\").replace("\"", "\\\"");
}
+ /** Shape of field paths that may be embedded in a scripted-field
+ * script using the historical dot notation, for example {@code city},
+ * {@code b.a} or {@code loc[0]}. Possessive quantifiers avoid
+ * backtracking on inputs that almost, but do not quite, match. */
+ private static final Pattern SIMPLE_SCRIPT_PATH =
+ Pattern.compile("[A-Za-z_][A-Za-z0-9_]*+"
+ + "(?:\\.[A-Za-z_][A-Za-z0-9_]*+)*+(?:\\[[0-9]++\\])*+");
+
+ /** Trailing array subscripts of a field path, for example {@code [0]}
+ * in {@code loc[0]}. Possessive quantifiers as above. */
+ private static final Pattern TRAILING_SUBSCRIPTS =
+ Pattern.compile("(?:\\[[0-9]++\\])++$");
+
+ /** Builds the script snippet that reads {@code path} from the document
+ * source, for example {@code params._source.city}.
+ *
+ * <p>Simple paths keep the historical dot notation. Any other path is
+ * accessed via map subscripts (for example
+ * {@code params._source['a-b']}), with the field name confined to a
+ * single-quoted script string literal (backslashes and quotes escaped),
+ * so that a field name containing script punctuation is always treated
+ * as data instead of being parsed as Painless/Groovy code. */
Review Comment:
Both Painless/Groovy (newer and older ES versions). I'd prefer not to
mention it on the function name, in case another one arrives in an ulterior ES
version.
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java:
##########
@@ -136,10 +138,57 @@ static String stripQuotes(String s) {
? s.substring(1, s.length() - 1) : s;
}
- private static String escapeSpecialSymbols(String s) {
+ /** Escapes the two characters that would terminate a JSON string. A SQL
+ * name or alias concatenated between double quotes stays a single JSON
+ * string only if backslash and double quote are doubled. Callers that
+ * embed the value inside a JSON body must route it through this helper
+ * before calling {@link #quote}. */
+ static String escapeSpecialSymbols(String s) {
return s.replace("\\", "\\\\").replace("\"", "\\\"");
}
+ /** Shape of field paths that may be embedded in a scripted-field
+ * script using the historical dot notation, for example {@code city},
+ * {@code b.a} or {@code loc[0]}. Possessive quantifiers avoid
+ * backtracking on inputs that almost, but do not quite, match. */
+ private static final Pattern SIMPLE_SCRIPT_PATH =
+ Pattern.compile("[A-Za-z_][A-Za-z0-9_]*+"
+ + "(?:\\.[A-Za-z_][A-Za-z0-9_]*+)*+(?:\\[[0-9]++\\])*+");
+
+ /** Trailing array subscripts of a field path, for example {@code [0]}
+ * in {@code loc[0]}. Possessive quantifiers as above. */
+ private static final Pattern TRAILING_SUBSCRIPTS =
+ Pattern.compile("(?:\\[[0-9]++\\])++$");
+
+ /** Builds the script snippet that reads {@code path} from the document
Review Comment:
Script language is Painless (newest ElasticSearch) and Groove (legacy
ElasticSearch), each will receive a different prefix value (which is extracted
from the ES table). Added this info at the beginning of the javadoc
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java:
##########
@@ -136,10 +138,57 @@ static String stripQuotes(String s) {
? s.substring(1, s.length() - 1) : s;
}
- private static String escapeSpecialSymbols(String s) {
+ /** Escapes the two characters that would terminate a JSON string. A SQL
+ * name or alias concatenated between double quotes stays a single JSON
+ * string only if backslash and double quote are doubled. Callers that
+ * embed the value inside a JSON body must route it through this helper
+ * before calling {@link #quote}. */
+ static String escapeSpecialSymbols(String s) {
return s.replace("\\", "\\\\").replace("\"", "\\\"");
}
+ /** Shape of field paths that may be embedded in a scripted-field
+ * script using the historical dot notation, for example {@code city},
+ * {@code b.a} or {@code loc[0]}. Possessive quantifiers avoid
+ * backtracking on inputs that almost, but do not quite, match. */
+ private static final Pattern SIMPLE_SCRIPT_PATH =
+ Pattern.compile("[A-Za-z_][A-Za-z0-9_]*+"
+ + "(?:\\.[A-Za-z_][A-Za-z0-9_]*+)*+(?:\\[[0-9]++\\])*+");
+
+ /** Trailing array subscripts of a field path, for example {@code [0]}
Review Comment:
Changed
##########
elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java:
##########
@@ -136,10 +138,57 @@ static String stripQuotes(String s) {
? s.substring(1, s.length() - 1) : s;
}
- private static String escapeSpecialSymbols(String s) {
+ /** Escapes the two characters that would terminate a JSON string. A SQL
+ * name or alias concatenated between double quotes stays a single JSON
+ * string only if backslash and double quote are doubled. Callers that
+ * embed the value inside a JSON body must route it through this helper
+ * before calling {@link #quote}. */
+ static String escapeSpecialSymbols(String s) {
return s.replace("\\", "\\\\").replace("\"", "\\\"");
}
+ /** Shape of field paths that may be embedded in a scripted-field
+ * script using the historical dot notation, for example {@code city},
+ * {@code b.a} or {@code loc[0]}. Possessive quantifiers avoid
Review Comment:
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]