99import org .elasticsearch .xpack .esql .EsqlClientException ;
1010import org .elasticsearch .xpack .esql .core .tree .Source ;
1111
12+ import java .util .Iterator ;
13+
1214import static org .elasticsearch .common .logging .LoggerMessageFormat .format ;
1315
1416public 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