Browse Source

Merge pull request #4617 from ghalliday/issue9640

HPCC-9640 Optimize NORMALIZE(ds, 0) and NORMALIZE(ds, 1)

Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 12 years ago
parent
commit
368d6130e1
3 changed files with 72 additions and 1 deletions
  1. 4 1
      ecl/hql/hqlfold.cpp
  2. 29 0
      ecl/hql/hqlopt.cpp
  3. 39 0
      ecl/regress/issue9640.ecl

+ 4 - 1
ecl/hql/hqlfold.cpp

@@ -3793,7 +3793,6 @@ IHqlExpression * NullFolderMixin::foldNullDataset(IHqlExpression * expr)
     case no_transformebcdic:
     case no_transformascii:
     case no_rollupgroup:
-    case no_normalize:
     case no_normalizegroup:
     case no_parse:
     case no_newparse:
@@ -3804,6 +3803,10 @@ IHqlExpression * NullFolderMixin::foldNullDataset(IHqlExpression * expr)
         if (isNull(child))
             return replaceWithNull(expr);
         break;
+    case no_normalize:
+        if (isNull(child) || matchesConstantValue(expr->queryChild(1), 0))
+            return replaceWithNull(expr);
+        break;
     case no_allnodes:
     case no_thisnode:
         if (isNull(child) && expr->isDataset())

+ 29 - 0
ecl/hql/hqlopt.cpp

@@ -2488,6 +2488,35 @@ IHqlExpression * CTreeOptimizer::doCreateTransformed(IHqlExpression * transforme
             }
             break;
         }
+    case no_normalize:
+        //Convert NORMALIZE(ds, 0, t(LEFT, COUNTER)) to empty dataset
+        if (matchesConstantValue(transformed->queryChild(1), 0))
+            return replaceWithNull(transformed);
+        //Convert NORMALIZE(ds, 1, t(LEFT, COUNTER)) to PROJECT(ds, t(LEFT, 1));
+        if (matchesConstantValue(transformed->queryChild(1), 1))
+        {
+            IHqlExpression * counter = queryAttributeChild(transformed, _countProject_Atom, 0);
+
+            HqlExprArray args;
+            unwindChildren(args, transformed, 0, 1);
+
+            IHqlExpression * transform = transformed->queryChild(2);
+            if (counter)
+            {
+                OwnedHqlExpr one = createConstant(counter->queryType()->castFrom(false, I64C(1)));
+                //Remove the annotations from the transform, otherwise it may say t(LEFT,COUNTER) which is confusing.
+                args.append(*replaceExpression(transform->queryBody(), counter, one));
+            }
+            else
+                args.append(*LINK(transform));
+
+            DBGLOG("Optimizer: Convert %s(,1) into PROJECT", queryNode0Text(transformed));
+            unwindChildren(args, transformed, 3);
+            //This is not a count project.. so remove the attribute.
+            removeProperty(args, _countProject_Atom);
+            return createDataset(no_hqlproject, args);
+        }
+        break;
     }
 
     bool shared = childrenAreShared(transformed);

+ 39 - 0
ecl/regress/issue9640.ecl

@@ -0,0 +1,39 @@
+/*##############################################################################
+
+    HPCC SYSTEMS software Copyright (C) 2012 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.
+############################################################################## */
+
+namesRecord :=
+            RECORD
+string20        surname;
+string10        forename;
+integer2        age := 25;
+            END;
+
+namesTable := NOFOLD(dataset([
+        {'Hawthorn','Gavin',31},
+        {'Hawthorn','Mia',30},
+        {'Smithe','Pru',10},
+        {'X','Z'}], namesRecord));
+
+namesRecord t(namesRecord l, unsigned n) := TRANSFORM
+    SELF.age := l.age + n;
+    SELF := l;
+END;
+
+
+OUTPUT(NORMALIZE(namesTable, 0, t(LEFT, COUNTER)));
+
+OUTPUT(NORMALIZE(namesTable, 1, t(LEFT, COUNTER)));