You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by "thomasmueller (via GitHub)" <gi...@apache.org> on 2023/11/03 08:40:06 UTC

[PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

thomasmueller opened a new pull request, #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187

   I had a to change a lot of tests, because they depended (too much) on implementation details.
   
   Even thought this would be a good idea, I did not switch to Json output so far, because:
   * some upstream UI tools depend on the query plans to have a certain form, which would break,
   * it would make the patch even larger,
   * the Lucene query string might contain double quotes, which would then be escaped, and so the output hard to read
   
   So basically, first the upstream UI tools would 
   
   The Elasticsearch query is Json, which wouldn't need to be escaped if we add it as a child object.


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1382003251


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java:
##########
@@ -427,10 +426,14 @@ public String getPlan(NodeState rootState) {
                 buff.append(index.getPlan(createFilter(true), rootState));
             }
         } else {
-            buff.append("no-index");
+            buff.append("no-index\n");
         }
         if (!selectorConstraints.isEmpty()) {

Review Comment:
   Well, it is not empty: I would like to keep the comment and the commented code...



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384454948


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.

Review Comment:
   Of course 😵‍💫 



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.

Review Comment:
   Of course 😵‍💫 



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1798083547

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [18 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![87.3%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '87.3%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [87.3% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384541250


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(@NotNull String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {

Review Comment:
   Yes. But that wouldn't necessarily be better. I prefer the "if else" in this case.



##########
oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/MultiPropertyOrTest.java:
##########
@@ -121,22 +121,17 @@ public void unionSortResultCount() throws Exception {
         root.commit();
 
         List<Integer> nodes = Lists.newArrayList();
-        Random r = new Random();
-        int seed = -2;
+        Random r = new Random(1);

Review Comment:
   Yes.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381999878


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {
+                while (++i < buff.length() && buff.charAt(i) != c) {
+                    // skip
+                }
+            } else if (c =='[') {
+                buff.insert(i + 1, "\n  ");
+                i += 3;
+            } else if (c <= ' ') {
+                String sub = buff.substring(i, Math.min(i + 10, buff.length()));
+                if (sub.startsWith(" and ")
+                        || sub.startsWith(" or ")
+                        || sub.startsWith(" order by ")
+                        || sub.startsWith(" option(")) {
+                    buff.setCharAt(i, '\n');
+                    buff.insert(i + 1, "  ");
+                    // just skip over the whitespace - but that's OK
+                    i += 2;
+                }
+            }
+        }
+        return buff.toString();
+    }
+
+    private static String formatSQL(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {
+                while (++i < buff.length() && buff.charAt(i) != c) {
+                    // skip
+                }

Review Comment:
   See the docs at https://jackrabbit.apache.org/oak/docs/query/grammar-sql2.html#static_operand and https://jackrabbit.apache.org/oak/docs/query/grammar-xpath.html#static_operand



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384457402


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();

Review Comment:
   I used this for consistency, and to prevent errors in maintenance.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381690261


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ * 
+ * Warning: formatting is also done within e.g. string literals. So there is no
+ * guarantee that the formatted query is semantically equal to the original one!
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     * 
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }            
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     * 
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        query = query.replaceAll("\\[", "\\[\n  ");
+        for (String term : new String[] {
+                "and ", "or ", "order by ", "option\\("
+        }) {
+            // xpath is case sensitive
+            query = query.replaceAll(" (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }
+    
+    private static String formatSQL(String query) {
+        for (String term : new String[] {
+                "union ", "from ", "where ", "and ", "or ", "order by ", "option\\("
+        }) {
+            // SQL is case insensitive, so we use (?i)
+            query = query.replaceAll("(?i) (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }

Review Comment:
   Well, actually, I implemented the slightly more complex solution now (including randomized test, which is anyway good to have).



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1792925158

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [17 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![87.2%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '87.2%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [87.2% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384455460


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);

Review Comment:
   The _language_ is case sensitive. The query, for SQL, is not (for XPath it is).



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "nfsantos (via GitHub)" <gi...@apache.org>.
nfsantos commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381485102


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ * 
+ * Warning: formatting is also done within e.g. string literals. So there is no
+ * guarantee that the formatted query is semantically equal to the original one!
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     * 
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }            
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     * 
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        query = query.replaceAll("\\[", "\\[\n  ");
+        for (String term : new String[] {
+                "and ", "or ", "order by ", "option\\("
+        }) {
+            // xpath is case sensitive
+            query = query.replaceAll(" (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }
+    
+    private static String formatSQL(String query) {
+        for (String term : new String[] {
+                "union ", "from ", "where ", "and ", "or ", "order by ", "option\\("
+        }) {
+            // SQL is case insensitive, so we use (?i)
+            query = query.replaceAll("(?i) (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }

Review Comment:
   Some of these keywords are common enough that they will likely appear in the text of some queries, which will then result in line breaks being added in the wrong places. Would it be very complicated to change the logic to detect if they words are used as sql keywords or not? Maybe something with checking if the word appears inside quotes or not? Or maybe it's ok for the output to sometimes be wrongly formatted, as this is mostly a visual aid.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "fabriziofortino (via GitHub)" <gi...@apache.org>.
fabriziofortino commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381806891


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java:
##########
@@ -427,10 +426,14 @@ public String getPlan(NodeState rootState) {
                 buff.append(index.getPlan(createFilter(true), rootState));
             }
         } else {
-            buff.append("no-index");
+            buff.append("no-index\n");
         }
         if (!selectorConstraints.isEmpty()) {

Review Comment:
   this `if` block is empty. Should we remove it?



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381541629


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ * 
+ * Warning: formatting is also done within e.g. string literals. So there is no
+ * guarantee that the formatted query is semantically equal to the original one!
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     * 
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }            
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     * 
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        query = query.replaceAll("\\[", "\\[\n  ");
+        for (String term : new String[] {
+                "and ", "or ", "order by ", "option\\("
+        }) {
+            // xpath is case sensitive
+            query = query.replaceAll(" (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }
+    
+    private static String formatSQL(String query) {
+        for (String term : new String[] {
+                "union ", "from ", "where ", "and ", "or ", "order by ", "option\\("
+        }) {
+            // SQL is case insensitive, so we use (?i)
+            query = query.replaceAll("(?i) (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }

Review Comment:
   Yes this is a concern, if the output is then run as a query... (By the human or automatically).
   I was thinking on how to solve it, but I don't think there is a simple solution that would cover all cases.
   Maybe we could add a comment in the output, but that's ugly as well...
   
   The output is only used in one place: LOG.debug, so I think the risk is not all that great now.
   My recommendation is to just accept that risk.
   But not use the code anywhere else, that's why I added the warning in the Javadoc.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1792588693

   SonarCloud Quality Gate failed.&nbsp; &nbsp; [![Quality Gate failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/failed-16px.png 'Quality Gate failed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [14 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![23.6%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/0-16px.png '23.6%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [23.6% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   
   
   ![idea](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/light_bulb-16px.png 'idea') Catch issues before they fail your Quality Gate with our IDE extension ![sonarlint](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/sonarlint-16px.png 'sonarlint') [SonarLint](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=sonarcloud-welcome)


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381999464


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {
+                while (++i < buff.length() && buff.charAt(i) != c) {
+                    // skip
+                }
+            } else if (c =='[') {
+                buff.insert(i + 1, "\n  ");
+                i += 3;
+            } else if (c <= ' ') {
+                String sub = buff.substring(i, Math.min(i + 10, buff.length()));
+                if (sub.startsWith(" and ")
+                        || sub.startsWith(" or ")
+                        || sub.startsWith(" order by ")
+                        || sub.startsWith(" option(")) {
+                    buff.setCharAt(i, '\n');
+                    buff.insert(i + 1, "  ");
+                    // just skip over the whitespace - but that's OK
+                    i += 2;
+                }
+            }
+        }
+        return buff.toString();
+    }
+
+    private static String formatSQL(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {
+                while (++i < buff.length() && buff.charAt(i) != c) {
+                    // skip
+                }

Review Comment:
   Backslash is not an escape character. The escape character for ' is ', so that it would be:
   
       "he said ""Hello"""
   
   and
   
       'he said ''Hello'''
   
   Because of this, we just need to search for the next ' / ". The "" and '' are correctly recognised. There's also a test for that.
   



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1382001550


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;

Review Comment:
   Actually... I think the current code is clearer in this case: The "not" can be confusing.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "nfsantos (via GitHub)" <gi...@apache.org>.
nfsantos commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381748274


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {
+                while (++i < buff.length() && buff.charAt(i) != c) {
+                    // skip
+                }
+            } else if (c =='[') {
+                buff.insert(i + 1, "\n  ");
+                i += 3;
+            } else if (c <= ' ') {
+                String sub = buff.substring(i, Math.min(i + 10, buff.length()));
+                if (sub.startsWith(" and ")
+                        || sub.startsWith(" or ")
+                        || sub.startsWith(" order by ")
+                        || sub.startsWith(" option(")) {
+                    buff.setCharAt(i, '\n');
+                    buff.insert(i + 1, "  ");
+                    // just skip over the whitespace - but that's OK
+                    i += 2;
+                }
+            }
+        }
+        return buff.toString();
+    }
+
+    private static String formatSQL(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {
+                while (++i < buff.length() && buff.charAt(i) != c) {
+                    // skip
+                }

Review Comment:
   Won't this fail if there is some string in the query that contains " inside it? For instance, a string like `"he said \"Hello\""`.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1792674489

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [18 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![87.2%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '87.2%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [87.2% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384539736


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(@NotNull String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);

Review Comment:
   > we could have a nice error message saying that we do not recognize the language
   
   I don't see a need for this currently. The current use case is debug log, where we actually always know which language it is.
   
   I do want to re-use this in the near future for http://oakutils.appspot.com/generate/index where the language detection is (unfortunately) incorrect.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1794464762

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [18 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![87.3%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '87.3%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [87.3% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384457402


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();

Review Comment:
   I used this for consistency.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1797969520

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [18 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![87.3%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '87.3%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [87.3% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1792355532

   SonarCloud Quality Gate failed.&nbsp; &nbsp; [![Quality Gate failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/failed-16px.png 'Quality Gate failed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [9 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![39.1%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/25-16px.png '39.1%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [39.1% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   
   
   ![idea](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/light_bulb-16px.png 'idea') Catch issues before they fail your Quality Gate with our IDE extension ![sonarlint](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/sonarlint-16px.png 'sonarlint') [SonarLint](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=sonarcloud-welcome)


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1382001550


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;

Review Comment:
   Actually... I think the current code is clearer in this case...



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381541629


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ * 
+ * Warning: formatting is also done within e.g. string literals. So there is no
+ * guarantee that the formatted query is semantically equal to the original one!
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     * 
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }            
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     * 
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        query = query.replaceAll("\\[", "\\[\n  ");
+        for (String term : new String[] {
+                "and ", "or ", "order by ", "option\\("
+        }) {
+            // xpath is case sensitive
+            query = query.replaceAll(" (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }
+    
+    private static String formatSQL(String query) {
+        for (String term : new String[] {
+                "union ", "from ", "where ", "and ", "or ", "order by ", "option\\("
+        }) {
+            // SQL is case insensitive, so we use (?i)
+            query = query.replaceAll("(?i) (" + term + ")", "\n  $1");
+        }
+        // remove duplicate newlines
+        query = query.replaceAll("\n+", "\n");
+        return query;
+    }

Review Comment:
   Yes this is a concern, if the output is then run as a query... (By the human or automatically).
   I was thinking on how to solve it, but I don't think there is a simple solution that would cover all cases. (Basically, don't use regular expressions.) Maybe we could add a comment in the output, but that's ugly as well...
   
   The output is only used in one place: LOG.debug, so I think the risk is not all that great now.
   My recommendation is to just accept that risk.
   But not use the code anywhere else, that's why I added the warning in the Javadoc.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "fabriziofortino (via GitHub)" <gi...@apache.org>.
fabriziofortino commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1381788455


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/nodetype/NodeTypeIndex.java:
##########
@@ -102,7 +102,10 @@ public Cursor query(Filter filter, NodeState root) {
     
     @Override
     public String getPlan(Filter filter, NodeState root) {
-        return "nodeType " + filter.toString();
+        return "nodeType\n" +
+                "    path: " + filter.getPath() + "\n" +
+                "    primaryTypes: " + filter.getPrimaryTypes().toString() + "\n" +
+                "    mixinTypes: " + filter.getMixinTypes().toString() + "\n";

Review Comment:
   `toString` calls are redundant:
   
   ```suggestion
                   "    primaryTypes: " + filter.getPrimaryTypes() + "\n" +
                   "    mixinTypes: " + filter.getMixinTypes() + "\n";
   ```



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();

Review Comment:
   should it be like this? can we add a test for this case?
   ```suggestion
                   query = query.substring("measure".length()).trim();
   ```



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(String query, String language) {

Review Comment:
   since the `query` param cannot be `null`, what about annotating it?
   ```suggestion
       public static String format(@NotNull String query, String language) {
   ```



##########
oak-core/src/test/java/org/apache/jackrabbit/oak/query/QueryFormatterTest.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Random;
+
+import org.apache.jackrabbit.oak.query.QueryFormatter;

Review Comment:
   not needed
   ```suggestion
   ```



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("explain".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;

Review Comment:
   this could be simplified 
   
   ```suggestion
           return !query.startsWith("select");
   ```



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/SelectorImpl.java:
##########
@@ -427,10 +426,14 @@ public String getPlan(NodeState rootState) {
                 buff.append(index.getPlan(createFilter(true), rootState));
             }
         } else {
-            buff.append("no-index");
+            buff.append("no-index\n");
         }
         if (!selectorConstraints.isEmpty()) {

Review Comment:
   this `if` block is empty. Should we move it?



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#issuecomment-1792907697

   Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187)
   
   [![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=BUG)  
   [![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=VULNERABILITY)  
   [![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=SECURITY_HOTSPOT)  
   [![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL) [18 Code Smells](https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&resolved=false&types=CODE_SMELL)
   
   [![87.2%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/60-16px.png '87.2%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list) [87.2% Coverage](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_coverage&view=list)  
   [![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=org.apache.jackrabbit%3Ajackrabbit-oak&pullRequest=1187&metric=new_duplicated_lines_density&view=list)
   
   ![warning](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning-16px.png 'warning') The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
   Read more [here](https://docs.sonarcloud.io/appendices/scanner-environment/)
   
   
   


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller merged PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187


-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "nfsantos (via GitHub)" <gi...@apache.org>.
nfsantos commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1383668625


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.

Review Comment:
   Don't you mean that it is recommended **not** to run queries...?



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);

Review Comment:
   If we do not want any locale related interpretation, it's better to use [Locale.ROOT](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#ROOT).



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();

Review Comment:
   Maybe replace with `substring(1)` since we are just removing the first character? I understand the reason for this convention when it is a word, prevents mistakes, but when it's only a single character, maybe it is simpler to use 1. 



##########
oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/property/MultiPropertyOrTest.java:
##########
@@ -121,22 +121,17 @@ public void unionSortResultCount() throws Exception {
         root.commit();
 
         List<Integer> nodes = Lists.newArrayList();
-        Random r = new Random();
-        int seed = -2;
+        Random r = new Random(1);

Review Comment:
   Why? You want the test to be deterministic?



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.

Review Comment:
   ```suggestion
        * Format the query into a more human-readable way by adding newlines.
   ```
   Adding newlines where? This sentence could be a bit more clear.



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.

Review Comment:
   ```suggestion
        * Warning: newlines are also added inside string literals.
   ```
   In what conditions? Do you mean that they may also be added in some rare cases?



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);

Review Comment:
   Being case sensitive is intentional? The query string is treated as case insensitive. If the language is supposed to be case sensitive, maybe add a note about it in the comments. 



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(@NotNull String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);

Review Comment:
   We are not sure here if the language is sql. If language is specified, add a check that it is either sql or xpath. If we autodetect, then can we check if it is sql by looking at if it starts with `select`? That way, we could have a nice error message saying that we do not recognize the language instead of failing with a syntax error or returning a string with newlines added in random places.



##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);
+        // explain queries
+        if (query.startsWith("explain")) {
+            query = query.substring("explain".length()).trim();
+            if (query.startsWith("measure")) {
+                query = query.substring("measure".length()).trim();
+            }
+        }
+        // union queries
+        while (query.startsWith("(")) {
+            query = query.substring("(".length()).trim();
+        }
+        if (query.startsWith("select")) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Format the query into a more human-readable way, by adding newlines.
+     * Warning: newlines are also added inside e.g. string literals.
+     *
+     * @param query the query (may not be null)
+     * @param language the query language, or null if unknown
+     * @return the formatted query
+     */
+    public static String format(@NotNull String query, String language) {
+        boolean xpath = isXPath(query, language);
+        if (xpath) {
+            return formatXPath(query);
+        } else {
+            return formatSQL(query);
+        }
+    }
+
+    private static String formatXPath(String query) {
+        StringBuilder buff = new StringBuilder(query);
+        for (int i = 0; i < buff.length(); i++) {
+            char c = buff.charAt(i);
+            if (c == '\'' || c == '"') {

Review Comment:
   This could be transformed into a switch.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OAK-10527 Improve readability of the explain query output [jackrabbit-oak]

Posted by "thomasmueller (via GitHub)" <gi...@apache.org>.
thomasmueller commented on code in PR #1187:
URL: https://github.com/apache/jackrabbit-oak/pull/1187#discussion_r1384456882


##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/QueryFormatter.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.jackrabbit.oak.query;
+
+import java.util.Locale;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Formatter for JCR queries in order to make them easier to read. Formatting is
+ * done on a best-effort basis.
+ *
+ * Warning: Care was taken to not add newlines inside string literals and so on,
+ * but there is still no guarantee that the formatted query is semantically
+ * equal to the original one. It is recommended to run queries that are returned
+ * by these methods.
+ */
+public class QueryFormatter {
+
+    /**
+     * Detect whether the query is an XPath query.
+     *
+     * @param query the query
+     * @param language the language, if known, or null
+     * @return true if xpath
+     */
+    public static boolean isXPath(String query, String language) {
+        if (language != null) {
+            return "xpath".equals(language);
+        }
+        query = query.trim().toLowerCase(Locale.ENGLISH);

Review Comment:
   Locale.ENGLISH is safe according to https://stackoverflow.com/questions/10336730/which-locale-should-i-specify-when-i-call-stringtolowercase . We use that in other parts too, so I would keep this.



-- 
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: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org