Skip to content

Commit

Permalink
SQL: Polish grammar for intervals (#35853)
Browse files Browse the repository at this point in the history
(cherry picked from commit e22a835)
  • Loading branch information
costin committed Nov 26, 2018
1 parent 0891afc commit abd7288
Show file tree
Hide file tree
Showing 7 changed files with 786 additions and 892 deletions.
5 changes: 0 additions & 5 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Expand Up @@ -288,11 +288,6 @@ interval
: INTERVAL sign=(PLUS | MINUS)? (valueNumeric=number | valuePattern=string) leading=intervalField (TO trailing=intervalField)?
;

intervalValue
: number
| string
;

intervalField
: YEAR | YEARS | MONTH | MONTHS | DAY | DAYS | HOUR | HOURS | MINUTE | MINUTES | SECOND | SECONDS
;
Expand Down
Expand Up @@ -544,10 +544,10 @@ public Literal visitIntervalLiteral(IntervalLiteralContext ctx) {
"Invalid interval declaration; trailing unit [{}] specified but the value is with numeric (single unit), "
+ "use the string notation instead", trailing);
}
value = of(interval.valueNumeric, negative, leading);
value = of(interval.valueNumeric, leading);
valueAsText = interval.valueNumeric.getText();
} else {
value = visitIntervalValue(interval.valuePattern, negative, intervalType);
value = of(interval.valuePattern, negative, intervalType);
valueAsText = interval.valuePattern.getText();
}

Expand All @@ -559,8 +559,9 @@ public Literal visitIntervalLiteral(IntervalLiteralContext ctx) {
return new Literal(source(ctx), name, timeInterval, intervalType);
}

private TemporalAmount of(NumberContext valueNumeric, boolean negative, TimeUnit unit) {
private TemporalAmount of(NumberContext valueNumeric, TimeUnit unit) {
// expect numbers for now
// as the number parsing handles the -, there's no need to look at that
Literal value = (Literal) visit(valueNumeric);
Number numeric = (Number) value.fold();

Expand All @@ -571,7 +572,7 @@ private TemporalAmount of(NumberContext valueNumeric, boolean negative, TimeUnit
return Intervals.of(source(valueNumeric), numeric.longValue(), unit);
}

private TemporalAmount visitIntervalValue(StringContext valuePattern, boolean negative, DataType intervalType) {
private TemporalAmount of(StringContext valuePattern, boolean negative, DataType intervalType) {
String valueString = string(valuePattern);
Location loc = source(valuePattern);
TemporalAmount interval = Intervals.parseInterval(loc, valueString, intervalType);
Expand Down
Expand Up @@ -995,18 +995,6 @@ class SqlBaseBaseListener implements SqlBaseListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void exitInterval(SqlBaseParser.IntervalContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterIntervalValue(SqlBaseParser.IntervalValueContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitIntervalValue(SqlBaseParser.IntervalValueContext ctx) { }
/**
* {@inheritDoc}
*
Expand Down
Expand Up @@ -585,13 +585,6 @@ class SqlBaseBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements SqlBa
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitInterval(SqlBaseParser.IntervalContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitIntervalValue(SqlBaseParser.IntervalValueContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
Expand Down
Expand Up @@ -923,16 +923,6 @@ interface SqlBaseListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitInterval(SqlBaseParser.IntervalContext ctx);
/**
* Enter a parse tree produced by {@link SqlBaseParser#intervalValue}.
* @param ctx the parse tree
*/
void enterIntervalValue(SqlBaseParser.IntervalValueContext ctx);
/**
* Exit a parse tree produced by {@link SqlBaseParser#intervalValue}.
* @param ctx the parse tree
*/
void exitIntervalValue(SqlBaseParser.IntervalValueContext ctx);
/**
* Enter a parse tree produced by {@link SqlBaseParser#intervalField}.
* @param ctx the parse tree
Expand Down

0 comments on commit abd7288

Please sign in to comment.