-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Currently, columns that have associated expressions are evaluated in the order they reside within the t_config
's m_expressions
member. Where the ordering in the vector is derived from t_view_config
's m_expressions
vector, etc. - in short, arbitrary, presumably the order of the columns from left to right.
I would like to propose that during an update, in the event there is a dependency in one or more of the expressions on another column which itself is also an expression, the column that is the dependency should be evaluated before any of the columns that are dependent on it.
As an example in the following video, taxes are computed in one column, we may want to add another column that is the discount-tax which is a combination of tax to pay, based on the sales amount, state (or city) and month (as some states discount taxes temporarily for a given month) which is then applied to the computed tax calculation.
In this situation, the second expression would always need to be evaluated after the tax column has been computed.
var discount_factor_perc :=
"Sales" >= 10^7 ?
switch
{
case "State" == 'California' and "Monthdate" == '2023/12' : 10; // 10% only during Dec 2023
case "City" == 'Yonkers' : 90; // 90% always - can't refuse
default : 0; // 0%
} : 0;
tax * (1 - discount_factor_perc / 100);
Description: Where Sales are greater than $10mil and, the state is California the discount is 10% if the city is Yonkers then the discount is 90%, otherwise there is no tax discount.
-
Why not just do the discount factor calc in the tax expression? Because we would like to see both values: the tax amount and the discounted tax amount
-
Why not simply copy-n-paste the tax calculation into the discount factor calc? Because of separation of concerns, and also the actual tax computation may not be as trivial as the one shown in the demo, I've already got 99 problems, now remembering to update at least another column when the tax computation changes is just one more.
The solution to this would be to build a DAG covering the expressions and then to topologically order the expressions in m_expressions
.
Expression dependencies and cycle detection for DAG construction can be determined by using the dependent_entity_collector
, more details can be found in Section 16 of the readme: https://www.partow.net/programming/exprtk/readme.html#line_2951