Repository: Azure/pykusto

Issue:
iff doesn't support sub expressions
The following syntax is not working:
.extend(timeDelta=iff(col.day-col.day1 == 0, timedelta(DAYS_BACK_DIFF), col.day-col.day1))

Discussion:

Patch:
diff --git a/pykusto/functions.py b/pykusto/functions.py
index 8450ef8..210958f 100644
--- a/pykusto/functions.py
+++ b/pykusto/functions.py
@@ -120,6 +120,8 @@ def ceiling(expr: NumberType) -> NumberExpression:
 
 def cos(expr: NumberType) -> NumberExpression:
     return expr.cos()
+
+
 #
 # def cot(self): return
 
@@ -257,12 +259,12 @@ def hourofday(expr: DatetimeType) -> NumberExpression:
     return expr.hourofday()
 
 
-def iif(predicate: BooleanType, if_true: ExpressionType, if_false: ExpressionType) -> BaseExpression:
-    return BaseExpression(KQL('iif({}, {}, {})'.format(predicate, if_true, if_false)))
+def iff(predicate: BooleanType, if_true: ExpressionType, if_false: ExpressionType) -> BaseExpression:
+    return BaseExpression(KQL('iff({}, {}, {})'.format(predicate, _subexpr_to_kql(if_true), _subexpr_to_kql(if_false))))
 
 
-def iff(predicate: BooleanType, if_true: ExpressionType, if_false: ExpressionType) -> BaseExpression:
-    return BaseExpression(KQL('iff({}, {}, {})'.format(predicate, if_true, if_false)))
+def iif(predicate: BooleanType, if_true: ExpressionType, if_false: ExpressionType) -> BaseExpression:
+    return iff(predicate, if_true, if_false)
 
 
 #
diff --git a/setup.py b/setup.py
index 605de64..b31e771 100644
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
 
 setup(
     name='pykusto',
-    version='0.0.4',
+    version='0.0.5',
     packages=find_packages(exclude=['test']),
     url='https://github.com/Azure/pykusto',
     license='MIT License',


Test patch:
diff --git a/test/test_functions.py b/test/test_functions.py
index 247142d..55fc998 100644
--- a/test/test_functions.py
+++ b/test/test_functions.py
@@ -7,7 +7,7 @@ from pykusto.query import Query
 from test.test_base import TestBase
 
 
-# TODO dcount_hll, iif
+# TODO dcount_hll
 
 class TestFunction(TestBase):
     def test_acos(self):
@@ -629,3 +629,16 @@ class TestFunction(TestBase):
             " | summarize active_days = dcount(bin(timestamp, time(1.0:0:0.0)))",
             Query().summarize(active_days=f.dcount(f.bin(col.timestamp, datetime.timedelta(1)))).render()
         )
+
+    def test_iff(self):
+        self.assertEqual(
+            " | project foo = (iff(foo > (ago(time(2.0:0:0.0))), time(3.0:0:0.0), time(4.0:0:0.0)))",
+            Query().project(foo=f.iff(col.foo > f.ago(datetime.timedelta(2)), datetime.timedelta(3), datetime.timedelta(4))).render()
+        )
+
+    def test_iif(self):
+        # iif is just an alias to iff
+        self.assertEqual(
+            " | project foo = (iff(foo > (ago(time(2.0:0:0.0))), time(3.0:0:0.0), time(4.0:0:0.0)))",
+            Query().project(foo=f.iif(col.foo > f.ago(datetime.timedelta(2)), datetime.timedelta(3), datetime.timedelta(4))).render()
+        )