From 3b334bad5617035253f6fcb9992a5978a0d8acc7 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Mon, 14 Sep 2020 16:12:20 -0700 Subject: [PATCH] GH-1159: Fix vector-of-interval multiplication/division arithmetic Those operations done between a vector-of-interval and a vector-of-arithmetic-type previously threw a runtime expression error due to an incorrect coercion being used internally. --- src/Expr.cc | 14 ++++++++++++-- .../language.vector-of-interval-arithmetic/out | 4 ++++ .../language/vector-of-interval-arithmetic.zeek | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 testing/btest/Baseline/language.vector-of-interval-arithmetic/out create mode 100644 testing/btest/language/vector-of-interval-arithmetic.zeek diff --git a/src/Expr.cc b/src/Expr.cc index 779219b681..2a08b630d2 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -1414,7 +1414,12 @@ TimesExpr::TimesExpr(ExprPtr arg_op1, ExprPtr arg_op2) if ( bt1 == TYPE_INTERVAL || bt2 == TYPE_INTERVAL ) { if ( IsArithmetic(bt1) || IsArithmetic(bt2) ) - PromoteType(TYPE_INTERVAL, is_vector(op1) || is_vector(op2) ); + { + if ( is_vector(op1) && is_vector(op2) ) + SetType(make_intrusive(base_type(TYPE_INTERVAL))); + else + PromoteType(TYPE_INTERVAL, is_vector(op1) || is_vector(op2) ); + } else ExprError("multiplication with interval requires arithmetic operand"); } @@ -1450,7 +1455,12 @@ DivideExpr::DivideExpr(ExprPtr arg_op1, ExprPtr arg_op2) if ( bt1 == TYPE_INTERVAL || bt2 == TYPE_INTERVAL ) { if ( IsArithmetic(bt1) || IsArithmetic(bt2) ) - PromoteType(TYPE_INTERVAL, is_vector(op1) || is_vector(op2)); + { + if ( is_vector(op1) && is_vector(op2) ) + SetType(make_intrusive(base_type(TYPE_INTERVAL))); + else + PromoteType(TYPE_INTERVAL, is_vector(op1) || is_vector(op2)); + } else if ( bt1 == TYPE_INTERVAL && bt2 == TYPE_INTERVAL ) { if ( is_vector(op1) || is_vector(op2) ) diff --git a/testing/btest/Baseline/language.vector-of-interval-arithmetic/out b/testing/btest/Baseline/language.vector-of-interval-arithmetic/out new file mode 100644 index 0000000000..eaa8d80e90 --- /dev/null +++ b/testing/btest/Baseline/language.vector-of-interval-arithmetic/out @@ -0,0 +1,4 @@ +[4.0 secs 500.0 msecs, 3.0 mins, 4.0 hrs 30.0 mins] +[4.0 secs 500.0 msecs, 3.0 mins, 4.0 hrs 30.0 mins] +[500.0 msecs, 50.0 msecs, 1.0 msec 250.0 usecs] +[2.0 secs, 20.0 secs, 13.0 mins 20.0 secs] diff --git a/testing/btest/language/vector-of-interval-arithmetic.zeek b/testing/btest/language/vector-of-interval-arithmetic.zeek new file mode 100644 index 0000000000..cc11a404b1 --- /dev/null +++ b/testing/btest/language/vector-of-interval-arithmetic.zeek @@ -0,0 +1,10 @@ +# @TEST-EXEC: zeek -b %INPUT >out +# @TEST-EXEC: btest-diff out + +global v1 = vector(1.5, 3.0, 4.5); +global v2 = vector(3 sec, 1 min, 1 hr); + +print v1 * v2; +print v2 * v1; +print v1 / v2; +print v2 / v1;