瀏覽代碼

HPCC-15750 Implement getLikelihood for And, OR, NOT

Signed-off-by: Shamser Ahmed <shamser.ahmed@lexisnexis.co.uk>
Shamser Ahmed 9 年之前
父節點
當前提交
c39d288ead
共有 3 個文件被更改,包括 84 次插入2 次删除
  1. 41 0
      ecl/hql/hqlattr.cpp
  2. 6 2
      ecl/hql/hqlfold.cpp
  3. 37 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;

+ 6 - 2
ecl/hql/hqlfold.cpp

@@ -5183,8 +5183,12 @@ IHqlExpression * CExprFolderTransformer::doFoldTransformed(IHqlExpression * unfo
     case no_unlikely:
         {
             IHqlExpression * child = expr->queryChild(0);
-            if (child->queryValue())
-                return LINK(child);
+
+            if (child->getOperator()==no_likely || child->getOperator()==no_unlikely)
+                return createValue(op, expr->getType(), LINK(child->queryChild(0)));
+            else
+                if (child->queryValue())
+                    return LINK(child);
             break;
         }
     }

+ 37 - 0
ecl/regress/likely2.ecl

@@ -0,0 +1,37 @@
+/*##############################################################################
+
+    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 );
+
+output(filtered1,,'tst_likely2.d01');
+output(filtered2,,'tst_likely2.d02');
+output(filtered3,,'tst_likely2.d03');
+