Skip to content

Commit 48678f3

Browse files
update according to review comments - refactor, comments
1 parent 19b44bc commit 48678f3

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/LogicalPlanBuilder.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,7 @@ protected LogicalPlan plan(ParseTree ctx) {
108108
if (errors.hasNext() == false) {
109109
return p;
110110
} else {
111-
StringBuilder message = new StringBuilder();
112-
int i = 0;
113-
int line = -1;
114-
int charPositionInLine = -1;
115-
116-
while (errors.hasNext()) {
117-
ParsingException e = errors.next();
118-
if (i > 0) {
119-
message.append("; ");
120-
message.append(e.getMessage());
121-
} else {
122-
// line and column numbers are the associated with the first error
123-
line = e.getLineNumber();
124-
charPositionInLine = e.getColumnNumber();
125-
message.append(e.getErrorMessage());
126-
}
127-
i++;
128-
}
129-
throw new ParsingException(line, charPositionInLine, message.toString());
111+
throw ParsingException.combineParsingExceptions(errors);
130112
}
131113
}
132114

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/ParsingException.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.elasticsearch.xpack.esql.EsqlClientException;
1010
import org.elasticsearch.xpack.esql.core.tree.Source;
1111

12+
import java.util.Iterator;
13+
1214
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
1315

1416
public class ParsingException extends EsqlClientException {
@@ -21,6 +23,10 @@ public ParsingException(String message, Exception cause, int line, int charPosit
2123
this.charPositionInLine = charPositionInLine + 1;
2224
}
2325

26+
/**
27+
* To be used only if the exception cannot be associated with a specific position in the query.
28+
* Error message will start with {@code line -1:-1:} instead of using specific location.
29+
*/
2430
public ParsingException(String message, Object... args) {
2531
this(Source.EMPTY, message, args);
2632
}
@@ -37,12 +43,38 @@ public ParsingException(Exception cause, Source source, String message, Object..
3743
this.charPositionInLine = source.source().getColumnNumber();
3844
}
3945

40-
public ParsingException(int line, int charPositionInLine, String message, Object... args) {
46+
private ParsingException(int line, int charPositionInLine, String message, Object... args) {
4147
super(message, args);
4248
this.line = line;
4349
this.charPositionInLine = charPositionInLine;
4450
}
4551

52+
/**
53+
* Combine multiple {@code ParsingException} into one, this is used by {@code LogicalPlanBuilder} to
54+
* consolidate multiple named parameters related {@code ParsingException}.
55+
*/
56+
public static ParsingException combineParsingExceptions(Iterator<ParsingException> parsingExceptions) {
57+
StringBuilder message = new StringBuilder();
58+
int i = 0;
59+
int line = -1;
60+
int charPositionInLine = -1;
61+
62+
while (parsingExceptions.hasNext()) {
63+
ParsingException e = parsingExceptions.next();
64+
if (i > 0) {
65+
message.append("; ");
66+
message.append(e.getMessage());
67+
} else {
68+
// line and column numbers are the associated with the first error
69+
line = e.getLineNumber();
70+
charPositionInLine = e.getColumnNumber();
71+
message.append(e.getErrorMessage());
72+
}
73+
i++;
74+
}
75+
return new ParsingException(line, charPositionInLine, message.toString());
76+
}
77+
4678
public int getLineNumber() {
4779
return line;
4880
}

0 commit comments

Comments
 (0)