This is an automated email from the ASF dual-hosted git repository.
julianhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 2fe48c7cf2 Improve [CALCITE-7424] In Lint, support sort specifications
2fe48c7cf2 is described below
commit 2fe48c7cf25e859b55739250abb5b26820a5a266
Author: Julian Hyde <[email protected]>
AuthorDate: Wed Mar 4 10:54:25 2026 -0800
Improve [CALCITE-7424] In Lint, support sort specifications
When encountering an out-of-order line, the lint warning now says which
line it
should be moved to.
---
.../java/org/apache/calcite/test/LintTest.java | 38 +++++++++++++---------
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/core/src/test/java/org/apache/calcite/test/LintTest.java
b/core/src/test/java/org/apache/calcite/test/LintTest.java
index 089d76edba..ef0db44831 100644
--- a/core/src/test/java/org/apache/calcite/test/LintTest.java
+++ b/core/src/test/java/org/apache/calcite/test/LintTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.calcite.test;
+import org.apache.calcite.runtime.PairList;
import org.apache.calcite.util.Puffin;
import org.apache.calcite.util.Source;
import org.apache.calcite.util.Sources;
@@ -633,7 +634,8 @@ public boolean inJavadoc() {
+ " }\n"
+ "}\n",
"GuavaCharSource{memory}:7:"
- + "Lines must be sorted; ' case b' should be before ' case c'");
+ + "Lines must be sorted; ' case b' should be before ' case c'"
+ + " (move to line 5)");
// Cases after "until" are checked against the same sorted list.
checkSortSpec(
@@ -649,7 +651,8 @@ public boolean inJavadoc() {
+ " }\n"
+ "}\n",
"GuavaCharSource{memory}:9:"
- + "Lines must be sorted; ' case a' should be before ' case x'");
+ + "Lines must be sorted; ' case a' should be before ' case x'"
+ + " (move to line 4)");
// Change '#}' to '##}': consumer stops at the same-indent '}', so
// the second switch's cases are not compared. No violations.
@@ -676,7 +679,8 @@ public boolean inJavadoc() {
+ " }\n"
+ "}\n",
"GuavaCharSource{memory}:4:"
- + "Lines must be sorted; 'a' should be before 'c'");
+ + "Lines must be sorted; 'a' should be before 'c'"
+ + " (move to line 3)");
// Specification spread over multiple lines using '\' continuation.
checkSortSpec(
@@ -690,7 +694,8 @@ public boolean inJavadoc() {
+ " }\n"
+ "}\n",
"GuavaCharSource{memory}:6:"
- + "Lines must be sorted; 'a' should be before 'c'");
+ + "Lines must be sorted; 'a' should be before 'c'"
+ + " (move to line 5)");
}
private void checkSortSpec(String code, String... expectedMessages) {
@@ -781,7 +786,7 @@ private static class SortConsumer
implements Consumer<Puffin.Line<GlobalState, FileState>> {
final Sort sort;
final Comparator<String> comparator = String.CASE_INSENSITIVE_ORDER;
- final List<String> lines = new ArrayList<>();
+ final PairList<String, Integer> lines = PairList.of();
boolean done = false;
SortConsumer(Sort sort) {
@@ -818,19 +823,22 @@ private static class SortConsumer
private void addLine(Puffin.Line<GlobalState, FileState> line,
String thisLine) {
if (!lines.isEmpty()) {
- final String prevLine = lines.get(lines.size() - 1);
+ final String prevLine = lines.left(lines.size() - 1);
if (comparator.compare(prevLine, thisLine) > 0) {
- final String earlierLine =
- Util.filter(lines, s -> comparator.compare(s, thisLine) > 0)
- .iterator().next();
- line.state().message(
- String.format(Locale.ROOT,
- "Lines must be sorted; '%s' should be before '%s'",
- thisLine, earlierLine),
- line);
+ for (int i = 0; i < lines.size(); i++) {
+ if (comparator.compare(lines.left(i), thisLine) > 0) {
+ line.state().message(
+ String.format(Locale.ROOT,
+ "Lines must be sorted; '%s' should be before '%s'"
+ + " (move to line %d)",
+ thisLine, lines.left(i), lines.right(i)),
+ line);
+ break;
+ }
+ }
}
}
- lines.add(thisLine);
+ lines.add(thisLine, line.fnr());
}
}
}