etr2460 commented on a change in pull request #16701:
URL: https://github.com/apache/superset/pull/16701#discussion_r712661510



##########
File path: 
superset-frontend/src/explore/components/controls/AnnotationLayerControl/AnnotationLayer.jsx
##########
@@ -204,15 +204,30 @@ export default class AnnotationLayer extends 
React.PureComponent {
     return sources;
   }
 
-  isValidFormula(value, annotationType) {
+  isValidFormula(expression, annotationType) {
     if (annotationType === ANNOTATION_TYPES.FORMULA) {
       try {
-        mathjsParse(value).compile().evaluate({ x: 0 });
+        const token = {
+          type: 3,
+          token: 'x',
+          show: 'x',
+          value: 'x',
+        };
+
+        // handle input like "y = x+1" instead of just "x+1"
+        const subexpressions = expression.split('=');
+        if (
+          subexpressions.length > 2 ||
+          (subexpressions[1] && !subexpressions[0].match(/^[a-zA-Z]\w*$/))
+        ) {
+          return false;
+        }
+        mexp.eval(subexpressions[1] ?? subexpressions[0], [token], { x: 0 });

Review comment:
       @villebro @kgabryje: here are some formula examples. most of them are 
constant numbers (.75, 300, etc.) but there are some more complex ones:
   - `2^(x/2000)*100`
   - `x=0.999`
   - `0.0005708176773515392`
   - `.5x+0`
   - `y=7.00`
   - `20.51 *(x < 1577836800000 ) +  20.2*(x < 1580515200000 & x >= 
1577836800000) +  19.9*(x < 1583020800000 & x >= 1580515200000) +  19.3*(x < 
1585699200000 & x >= 1583020800000) +  18.7*(x < 1588291200000 & x >= 
1585699200000) +  18.1*(x < 1590969600000 & x >= 1588291200000) +  18.0*(x < 
1593561600000 & x >= 1590969600000) +  18.0*(x < 1596240000000 & x >= 
1593561600000) +  17.9*(x < 1598918400000 & x >= 1596240000000) +  17.9*(x < 
1601510400000 & x >= 1598918400000) +  17.9*(x < 1604188800000 & x >= 
1601510400000) +  17.9*(x < 1606780800000 & x >= 1604188800000) +  17.9*(x < 
1609459200000 & x >= 1606780800000) +  17.9*(                    x >= 
1609459200000)`
   
   That last one is a real doozy, and probably is the most important one to 
test here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to