Skip to content

Commit 38d197c

Browse files
committed
Make the error messages for decimal span length more meaningful
Signed-off-by: Yuanchun Shen <[email protected]>
1 parent f87241b commit 38d197c

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

ppl/src/main/antlr/OpenSearchPPLLexer.g4

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -509,40 +509,35 @@ ALIGNTIME: 'ALIGNTIME';
509509
// Must precede ID to avoid conflicts with identifier matching
510510
PERCENTILE_SHORTCUT: PERC(INTEGER_LITERAL | DECIMAL_LITERAL) | 'P'(INTEGER_LITERAL | DECIMAL_LITERAL);
511511

512-
SPANLENGTH: [0-9]+ (
513-
'US' |'CS'|'DS'
514-
|'MS'|'MILLISECOND'|'MILLISECONDS'
515-
|'S'|'SEC'|'SECS'|'SECOND'|'SECONDS'
516-
|'MIN'|'MINS'|'MINUTE'|'MINUTES'
517-
|'H'|'HR'|'HRS'|'HOUR'|'HOURS'
518-
|'H'|'HR'|'HRS'|'HOUR'|'HOURS'
519-
|'D'|'DAY'|'DAYS'
520-
|'W'|'WEEK'|'WEEKS'
521-
|'M'|'MON'|'MONTH'|'MONTHS'
522-
|'Q'|'QTR'|'QTRS'|'QUARTER'|'QUARTERS'
523-
|'Y'|'YR'|'YRS'|'YEAR'|'YEARS'
524-
);
512+
fragment DAY_OR_DOUBLE: 'D';
513+
fragment COMMON_TIME_UNIT: 'S'|'SEC'|'SECOND'
514+
|'M'|'MIN'|'MINUTE'
515+
|'H'|'HR'|'HOUR'
516+
|'DAY'|'W'|'WEEK'
517+
|'MON'|'MONTH'
518+
|'Q'|'QTR'|'QUARTER'
519+
|'Y'|'YR'|'YEAR';
520+
fragment PLURAL_UNIT: 'MILLISECONDS'|'SECS'|'SECONDS'|'MINS'|'MINUTES'|'HRS'|'HOURS'
521+
|'DAYS'|'MONTHS'|'QTRS'|'QUARTERS'|'YRS'|'YEARS';
522+
fragment SPANUNIT: COMMON_TIME_UNIT | PLURAL_UNIT
523+
|'US'|'CS'|'DS'
524+
|'MS'|'MILLISECOND';
525+
SPANLENGTH: DEC_DIGIT+ (SPANUNIT | DAY_OR_DOUBLE);
526+
DECIMAL_SPANLENGTH: (DEC_DIGIT+)? '.' DEC_DIGIT+ SPANUNIT;
525527

526528
NUMERIC_ID : DEC_DIGIT+ ID_LITERAL;
527529

528530
// LITERALS AND VALUES
529531
//STRING_LITERAL: DQUOTA_STRING | SQUOTA_STRING | BQUOTA_STRING;
530532
fragment WEEK_SNAP_UNIT: 'W' [0-7];
531-
fragment TIME_SNAP_UNIT: 'S' | 'SEC' | 'SECOND'
532-
| 'M' | 'MIN' | 'MINUTE'
533-
| 'H' | 'HR' | 'HOUR' | 'HOURS'
534-
| 'D' | 'DAY'
535-
| 'W' | 'WEEK' | WEEK_SNAP_UNIT
536-
| 'MON' | 'MONTH'
537-
| 'Q' | 'QTR' | 'QUARTER'
538-
| 'Y' | 'YR' | 'YEAR';
533+
fragment TIME_SNAP_UNIT: COMMON_TIME_UNIT | WEEK_SNAP_UNIT | DAY_OR_DOUBLE;
539534
TIME_SNAP: AT TIME_SNAP_UNIT;
540535
ID: ID_LITERAL;
541536
CLUSTER: CLUSTER_PREFIX_LITERAL;
542537
INTEGER_LITERAL: DEC_DIGIT+;
543538
DECIMAL_LITERAL: (DEC_DIGIT+)? '.' DEC_DIGIT+;
544539
FLOAT_LITERAL: (DEC_DIGIT+)? '.' DEC_DIGIT+ 'F';
545-
DOUBLE_LITERAL: (DEC_DIGIT+)? '.' DEC_DIGIT+ 'D';
540+
DOUBLE_LITERAL: (DEC_DIGIT+)? '.' DEC_DIGIT+ DAY_OR_DOUBLE;
546541

547542
fragment DATE_SUFFIX: ([\-.][*0-9]+)+;
548543
fragment CLUSTER_PREFIX_LITERAL: [*A-Z]+?[*A-Z_\-0-9]* COLON;

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ timechartParameter
299299

300300
spanLiteral
301301
: SPANLENGTH
302+
| DECIMAL_SPANLENGTH
303+
| DOUBLE_LITERAL // 1.5d can also represent decimal span length
302304
| INTEGER_LITERAL
303305
| DECIMAL_LITERAL
304306
;

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,13 @@ public UnresolvedExpression visitSpanLiteral(OpenSearchPPLParser.SpanLiteralCont
816816
return AstDSL.intLiteral(Integer.parseInt(ctx.INTEGER_LITERAL().getText()));
817817
} else if (ctx.DECIMAL_LITERAL() != null) {
818818
return AstDSL.decimalLiteral(new BigDecimal(ctx.DECIMAL_LITERAL().getText()));
819+
} else if (ctx.DECIMAL_SPANLENGTH() != null || ctx.DOUBLE_LITERAL() != null) {
820+
throw new IllegalArgumentException(
821+
StringUtils.format(
822+
"Span length [%s] is invalid: floating-point time intervals are not supported.",
823+
ctx.DECIMAL_SPANLENGTH() != null
824+
? ctx.DECIMAL_SPANLENGTH().getText()
825+
: ctx.DOUBLE_LITERAL().getText()));
819826
} else {
820827
return AstDSL.stringLiteral(ctx.getText());
821828
}

ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,13 +1493,19 @@ public void testReplaceCommandWithMultiplePairs() {
14931493
public void testTimeSpanWithDecimalShouldThrow() {
14941494
Throwable t1 =
14951495
assertThrows(
1496-
SyntaxCheckException.class, () -> plan("source=t | timechart span=1.5d count"));
1497-
assertTrue(t1.getMessage().contains("[1.5d] is not a valid term at this part of the query"));
1496+
IllegalArgumentException.class, () -> plan("source=t | timechart span=1.5d count"));
1497+
assertTrue(
1498+
t1.getMessage()
1499+
.contains(
1500+
"Span length [1.5d] is invalid: floating-point time intervals are not supported."));
14981501

14991502
Throwable t2 =
15001503
assertThrows(
1501-
SyntaxCheckException.class,
1504+
IllegalArgumentException.class,
15021505
() -> plan("source=t | stats count by span(@timestamp, 2.5y)"));
1503-
assertTrue(t2.getMessage().contains("[y] is not a valid term at this part of the query"));
1506+
assertTrue(
1507+
t2.getMessage()
1508+
.contains(
1509+
"Span length [2.5y] is invalid: floating-point time intervals are not supported."));
15041510
}
15051511
}

0 commit comments

Comments
 (0)