#! /bin/sh
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#
# @@@ END COPYRIGHT @@@

# Like the pattern-masking of QACOMP, here we filter out such stuff as
# timestamps and generated identifiers.  Called by the runregr script
# before doing diff.

fil=$1
if [ "$fil" = "" ]; then
  echo "Usage: $0 filename"
  exit 1
fi

# Remove following types of lines:
#   Lines started with "Start Time", "End Time", "Elapsed Time",
#                      "Execution Time", "Table ID: ddddddddd", and
#                      "Hist ID: ddddddddd"
ON_A_VM=
SYSVENDOR="$(cat /sys/class/dmi/id/sys_vendor)"
if [[ ( "${SYSVENDOR/OpenStack/}" != "$SYSVENDOR" ) ||
      ( "${SYSVENDOR/VMware/}"    != "$SYSVENDOR" ) ]]; then
  # Running on an OpenStack or VMware virtual machine
  ON_A_VM=yes
fi
if [[ "$SQ_BUILD_TYPE" = "release" ]]; then
sed "
/Start Time.*/d
" $fil |
sed "/End Time.*/d" |
sed "/Elapsed Time.*/d" |
sed "/Execution Time.*/d" |
sed "/Table ID.*/d" |
sed "/Hist ID.*/d" |
sed "/System load:.*/d" |
sed "/CPU frequency:.*/d" |
awk '
{
# we are looking for a less than 1 ms compile time
#   but we accept time up to 1.4 ms because of other loads
#   and time up to 2.0 ms on a virtual machine
#   when the test is being run
# Acceptable compile time is no more than 00:00:01.001399
#   or on a virtual machine, no more than 00:00:01.001999
# Examples of patterns to classify:
#  1.  "Compile Time 00:00:01.001047" --> FAILED (at least one second)
#  2.  "Compile Time 00:00:00.001470" --> FAILED (more than 1ms)
#  3.  "Compile Time 00:00:00.000870" --> SUCCESS(less than 1ms)
#  4.  "Compile Time 00:00:00.001200" --> SUCCESS(less than 1ms)
#  5.  "Compile Time 00:00:00.001821" --> SUCCESS(less than 1ms)  only on a VM
   if ( $1 == "Compile"  && $2 == "Time" ) { \
     split($3, a, "."); \
     if ( !match(a[1], "00:00:00") ) \
       print "FAIL (at least one second)", $0; \
     else { \
                  print "SUCCESS (less than 1ms)"; \
     }; \
   } else \
     print; \
}
' |
sed "s/====QUERY: .*/Query (see log file)/"
fi

if [[ "$SQ_BUILD_TYPE" = "debug" ]]; then
sed "
/Start Time.*/d
" $fil |
sed "/End Time.*/d" |
sed "/Elapsed Time.*/d" |
sed "/Execution Time.*/d" |
sed "/Table ID.*/d" |
sed "/Hist ID.*/d" |
sed "/Compile Time.*/d" |
sed "/System load:.*/d" |
sed "/CPU frequency:.*/d" |
sed "s/====QUERY: .*/Query (see log file)/"
fi

