From 2ebf4b864daa48eb821ed043f58c4638c9d6147f Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 2 Jan 2021 00:21:02 -0500 Subject: [PATCH] ColTypeNumeric: Support date and duration variants If the lexer identifies an expression as an date or a duration, it should be re-cast into integer value. Closes #2101. --- src/columns/ColTypeNumeric.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/columns/ColTypeNumeric.cpp b/src/columns/ColTypeNumeric.cpp index 064892ad3..754643330 100644 --- a/src/columns/ColTypeNumeric.cpp +++ b/src/columns/ColTypeNumeric.cpp @@ -67,10 +67,24 @@ void ColumnTypeNumeric::modify (Task& task, const std::string& value) std::string label = " MODIFICATION "; Context::getContext ().debug (label + _name + " <-- '" + evaluatedValue.get_string () + "' <-- '" + value + '\''); - // If the result is not readily convertible to a numeric value, then this is - // an error. - if (evaluatedValue.type () == Variant::type_string) - throw format ("The value '{1}' is not a valid numeric value.", evaluatedValue.get_string ()); + // Convert the value of the expression to the correct type if needed + switch (evaluatedValue.type ()) + { + // Expected variants - no conversion + case Variant::type_integer: + case Variant::type_real: + break; + // Convertible variants - convert to int + case Variant::type_date: + case Variant::type_duration: + evaluatedValue.cast (Variant::type_integer); + break; + // Non-convertible variants + case Variant::type_string: + throw format ("The value '{1}' is not a valid numeric value.", evaluatedValue.get_string ()); + default: + throw format ("Unexpected variant type: '{1}'", evaluatedValue.type ()); + } task.set (_name, evaluatedValue); }