Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
prevent adding two dates at parse time
Browse files Browse the repository at this point in the history
attempting to add (not subtract) two dates raises an InvalidOperation parse error because there aren't any sane semantics for doing so
while subtraction gives the duration between the dates, there's hardly any good reason to add them
  • Loading branch information
kroepke committed Nov 22, 2016
1 parent cd69faf commit 02b897b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
Expand Up @@ -77,6 +77,7 @@
import org.graylog.plugins.pipelineprocessor.parser.errors.IncompatibleType;
import org.graylog.plugins.pipelineprocessor.parser.errors.IncompatibleTypes;
import org.graylog.plugins.pipelineprocessor.parser.errors.InvalidFunctionArgument;
import org.graylog.plugins.pipelineprocessor.parser.errors.InvalidOperation;
import org.graylog.plugins.pipelineprocessor.parser.errors.MissingRequiredParam;
import org.graylog.plugins.pipelineprocessor.parser.errors.NonIndexableType;
import org.graylog.plugins.pipelineprocessor.parser.errors.OptionalParametersMustBeNamed;
Expand Down Expand Up @@ -748,16 +749,21 @@ public void exitComparison(RuleLangParser.ComparisonContext ctx) {

@Override
public void exitAddition(RuleLangParser.AdditionContext ctx) {
final BinaryExpression binaryExpr = (BinaryExpression) parseContext.expressions().get(ctx);
final Class leftType = binaryExpr.left().getType();
final Class rightType = binaryExpr.right().getType();
final AdditionExpression addExpression = (AdditionExpression) parseContext.expressions().get(ctx);
final Class leftType = addExpression.left().getType();
final Class rightType = addExpression.right().getType();

// special case for DateTime/Period, which are all compatible
final boolean leftDate = DateTime.class.equals(leftType);
final boolean rightDate = DateTime.class.equals(rightType);
final boolean leftPeriod = Period.class.equals(leftType);
final boolean rightPeriod = Period.class.equals(rightType);
if (leftDate && rightDate || leftDate && rightPeriod || leftPeriod && rightDate || leftPeriod && rightPeriod) {
if (leftDate && rightDate) {
if (addExpression.isPlus()) {
parseContext.addError(new InvalidOperation(ctx, addExpression, "Unable to add two dates"));
}
return;
} else if (leftDate && rightPeriod || leftPeriod && rightDate || leftPeriod && rightPeriod) {
return;
}
// otherwise check generic binary expression
Expand Down
@@ -0,0 +1,32 @@
package org.graylog.plugins.pipelineprocessor.parser.errors;

import com.fasterxml.jackson.annotation.JsonProperty;

import org.antlr.v4.runtime.ParserRuleContext;
import org.graylog.plugins.pipelineprocessor.ast.expressions.Expression;

public class InvalidOperation extends ParseError {
private final Expression expr;

private final String message;

public InvalidOperation(ParserRuleContext ctx, Expression expr, String message) {
super("invalid_operation", ctx);
this.expr = expr;
this.message = message;
}

@JsonProperty("reason")
@Override
public String toString() {
return "Invalid operation: " + message;
}

public Expression getExpression() {
return expr;
}

public String getMessage() {
return message;
}
}
Expand Up @@ -51,6 +51,7 @@
import org.graylog.plugins.pipelineprocessor.parser.errors.IncompatibleIndexType;
import org.graylog.plugins.pipelineprocessor.parser.errors.IncompatibleTypes;
import org.graylog.plugins.pipelineprocessor.parser.errors.InvalidFunctionArgument;
import org.graylog.plugins.pipelineprocessor.parser.errors.InvalidOperation;
import org.graylog.plugins.pipelineprocessor.parser.errors.NonIndexableType;
import org.graylog.plugins.pipelineprocessor.parser.errors.OptionalParametersMustBeNamed;
import org.graylog.plugins.pipelineprocessor.parser.errors.ParseError;
Expand Down Expand Up @@ -388,6 +389,18 @@ public void dateArithmetic() {

}

@Test
public void invalidDateAddition() {
try {
parseRuleWithOptionalCodegen();
fail("Should have thrown parse exception");
} catch (ParseException e) {
assertEquals(1, e.getErrors().size());
assertEquals(InvalidOperation.class, Iterables.getOnlyElement(e.getErrors()).getClass());
}
}


public static class CustomObject {
private final String id;

Expand Down
@@ -0,0 +1,4 @@
rule "cannot add dates"
when
now() + now() == now()
end

0 comments on commit 02b897b

Please sign in to comment.