瀏覽代碼

Merge pull request #8830 from shamser/issue15750

HPCC-15750 Implement getLikelihood for And, OR, NOT, optimize nested likely

Reviewed-by: Gavin Halliday <ghalliday@hpccsystems.com>
Gavin Halliday 9 年之前
父節點
當前提交
cb3513cce4
共有 3 個文件被更改,包括 88 次插入1 次删除
  1. 41 0
      ecl/hql/hqlattr.cpp
  2. 7 1
      ecl/hql/hqlfold.cpp
  3. 40 0
      ecl/regress/likely2.ecl

+ 41 - 0
ecl/hql/hqlattr.cpp

@@ -3805,6 +3805,47 @@ IHqlExpression * queryLikelihoodExpr(IHqlExpression * expr)
         else
             likelihoodExpr.set(queryConstantLikelihoodFalse());
         break;
+    case no_and:
+        {
+            double p1 = queryLikelihood(expr->queryChild(0));
+            if (isKnownLikelihood(p1))
+            {
+                double p2 = queryLikelihood(expr->queryChild(1));
+                if (isKnownLikelihood(p2))
+                {
+                    likelihoodExpr.set(createConstant(createRealValue(p1*p2,8)));
+                    break;
+                }
+            }
+            likelihoodExpr.set(queryConstantLikelihoodUnknown());
+            break;
+        }
+    case no_or:
+        {
+            double p1 = queryLikelihood(expr->queryChild(0));
+            if (isKnownLikelihood(p1))
+            {
+                double p2 = queryLikelihood(expr->queryChild(1));
+                if (isKnownLikelihood(p2))
+                {
+                    likelihoodExpr.set(createConstant(createRealValue(p1+p2-p1*p2,8)));
+                    break;
+                }
+            }
+            likelihoodExpr.set(queryConstantLikelihoodUnknown());
+            break;
+        }
+    case no_not:
+        {
+            double p1 = queryLikelihood(expr->queryChild(0));
+            if (isKnownLikelihood(p1))
+            {
+                likelihoodExpr.set(createConstant(createRealValue(1.0-p1,8)));
+                break;
+            }
+            likelihoodExpr.set(queryConstantLikelihoodUnknown());
+            break;
+        }
     default:
         likelihoodExpr.set(queryConstantLikelihoodUnknown());
         break;

+ 7 - 1
ecl/hql/hqlfold.cpp

@@ -5201,8 +5201,14 @@ IHqlExpression * CExprFolderTransformer::doFoldTransformed(IHqlExpression * unfo
     case no_unlikely:
         {
             IHqlExpression * child = expr->queryChild(0);
-            if (child->queryValue())
+            switch(child->getOperator())
+            {
+            case no_likely:
+            case no_unlikely:
+                return replaceChild(expr, 0, child->queryChild(0));
+            case no_constant:
                 return LINK(child);
+            }
             break;
         }
     }

+ 40 - 0
ecl/regress/likely2.ecl

@@ -0,0 +1,40 @@
+/*##############################################################################
+
+    HPCC SYSTEMS software Copyright (C) 2016 HPCC Systems®.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+############################################################################## */
+
+
+person_layout := RECORD
+    unsigned8 person_id,
+    string1 per_sex,
+    string40 per_last_name,
+    unsigned8 xpos
+END;
+person := dataset('person', person_layout, thor);
+
+isLessXpos1000 := LIKELY(person.xpos < 1000, 0.1);
+isIdLess4 := LIKELY(person.person_id < 4,0.2);
+filtered0 := person( LIKELY(isLessXpos1000, 0.2) );
+filtered1 := person( isLessXpos1000 AND isIdLess4 );
+filtered2 := person( isLessXpos1000 OR isIdLess4 );
+filtered3 := person( NOT isLessXpos1000 );
+filtered4 := person( LIKELY(TRUE) );
+
+output(filtered0,,'tst_likely2.d00');
+output(filtered1,,'tst_likely2.d01');
+output(filtered2,,'tst_likely2.d02');
+output(filtered3,,'tst_likely2.d03');
+output(filtered4,,'tst_likely2.d04');
+