Exemplo n.º 1
0
def validate_eval(kv, p):
    """Validate a format with column values

    Args:
      kv (dict): column name as key, and value
      p (str): format to be evaluated

    Returns:
      bool: True on success, otherwise False
    """
    try:
        s = p.format(**kv)
    except KeyError as e:
        raise DbProfilerException.ValidationError(_("Parameter error: ") +
                                                  "`%s' %s" % (p, kv),
                                                  rule=p,
                                                  params=kv)

    try:
        return eval(s)
    except SyntaxError as e:
        raise DbProfilerException.ValidationError(_("Syntax error: ") +
                                                  "`%s'" % s,
                                                  rule=p,
                                                  params=kv)
Exemplo n.º 2
0
    def validate(self, stats):
        """Validate a min/max rule based the column statistics

        Args:
            stats (dict): a table statistics. see Data_Structure.txt
                          for more info.

        Returns:
            True if the expression is true, otherwise False.
        """
        # rule: [ column_name, expression ]
        assert len(self.rule) == 2
        assert 'columns' in stats

        c = None
        for col in stats['columns']:
            if col['column_name'] == self.rule[0]:
                c = col
                break

        if c is None:
            raise DbProfilerException.ValidationError(
                _("Column `%s' not found. Check your validation rule again.") %
                self.rule[0], self.rule)
        assert 'row_count' in stats
        assert ('nulls' in c and 'min' in c and 'max' in c
                and 'cardinality' in c)
        kv = {
            'rows': stats['row_count'],
            'nulls': c['nulls'],
            'min': c['min'],
            'max': c['max'],
            'cardinality': c['cardinality']
        }

        self.statistics[0] += 1

        try:
            s = self.rule[1].format(**kv)
        except KeyError as e:
            self.statistics[1] += 1
            raise DbProfilerException.ValidationError(
                _("Parameter error: ") + "`%s'" % kv, self.rule)

        try:
            if eval(s) is False:
                self.statistics[1] += 1
                return False
        except SyntaxError:
            self.statistics[1] += 1
            raise DbProfilerException.ValidationError(
                _("Syntax error: ") + "`%s'" % s, self.rule)

        return True
Exemplo n.º 3
0
    def validate(self, column_names, record):
        """Validate one record

        Args:
            columns (list): a list of column names associated with each field.
            record (list): a record, consisting of list of fields.

        Returns:
            True on evaluation succeeded, otherwise False.
        """
        # self.rule: [ column name, format ]
        assert len(self.rule) == 2
        assert len(column_names) == len(record)

        self.statistics[0] += 1

        kv = {}
        for k, v in zip(column_names, record):
            # if the value is not a number, it needs to be quoted with "'".
            try:
                float(v)
                kv[k] = v
            except ValueError as e:
                kv[k] = "'" + v + "'"
        try:
            s = self.rule[1].format(**kv)
        except KeyError as e:
            self.statistics[1] += 1
            raise DbProfilerException.ValidationError(
                _("Parameter error: ") + "`%s'" % kv, self.rule)

        try:
            if eval(s) is False:
                self.statistics[1] += 1
                return False
        except SyntaxError:
            self.statistics[1] += 1
            raise DbProfilerException.ValidationError(
                _("Syntax error: ") + "`%s'" % s, self.rule)

        return True
Exemplo n.º 4
0
    def validate(self, dbdriver):
        if dbdriver is None:
            raise DbProfilerException.DriverError(
                _("Database driver not found."))

        try:
            res = dbdriver.q2rs(self.query)
        except DbProfilerException.QueryError as e:
            raise DbProfilerException.ValidationError(_("SQL error: ") +
                                                      "`%s'" % self.query,
                                                      self.label,
                                                      source=e)

        assert res
        assert len(res.column_names) == len(res.resultset[0])
        assert len(res.resultset) == 1

        kv = {}
        for k, v in zip(res.column_names, res.resultset[0]):
            kv[k] = v

        return validate_eval(kv, self.rule[2])