Skip to content

Repository files navigation

Build Statuscodecov.io

Data Quality package simplifies data validation and logging of errors to the database.

Metadata Schema

FieldSuggested TypeDescription
DateTimestampTime when error was logged
SourceStringData source of the issue
TypeStringType of the issue (enum)
ValueStringValue that contains the problem
URLStringLink to the source record

Error types

TypeDescription
OrphanForeign key does not match the primary key
WrongValue does not match the business rule
MissingValue in the field is empty
DuplicateDuplicated record

Example

Code

library(rdqa)
# Defining the rules - here we don't need the data yetr.smaller<- newConditionRule("small", condition="< big")
r.unique<- newUniqueRule("big")
r.name.required<- newRequiredRule("name")
# Combine the rules in Rules Containerall.rules<- newRulesContainer("test.data.td", r.smaller, r.unique, r.name.required)
# Connect to the db where we want to log the resultsconn<- dbConnect(dbDriver("SQLite"), "demo.db")
# We only need data just before the validationdt<- data.table(id= c(1, 2, 3, 4, 5),
small= c(1, 10, 2, 3, 4),
big= c(10, 1, 20, 20, 40),
name= c("a", "", "c", "d", NA_character_),
key="id")
# This call will validate the data againt all three rules# and log results to the database
validateRules(conn, all.rules, dt)
# Check what we have in the errors tableres<- dbGetQuery(conn, "SELECT * FROM errors")

Expected result

datesourcetyperulerefvalueurl
1486681846test.data.tdConditionField [small] should matrch condition: < big22NA
1486685164test.data.tdUniqueField(s) big should be uqniue44NA
1486685164test.data.tdRequiredField [name] should ont be empty22NA
1486685164test.data.tdRequiredField [name] should ont be empty55NA

Defining rules through Schema object

Schema object allows to define rules in a more readable layout with rules seating inside the data structure:

# Lets say we want to define rules for customer data, here is a sample schema:schema.customers<- Schema(
"customer.data",
schema=list(
list(
name="id",
description="This is an integer primary key for our customer table",
class="integer", required=TRUE,
unique=TRUE
),
list(
name="name",
class="character",
regex="\\w"
),
list(
name="gender",
class="character",
enum= c("male", "female")
)
),
rules=list(
newConditionRule("id", "> 0"),
newConditionRule("name", condition= expression(nchar(name) <12))
)
) # These are our customers:customers<- data.table(
id= c(1L, 2L, NA_integer_, 3L, 4L, -1L),
name= c("John", "Isabellarose", "Anna", "Bob", NA_character_, ""),
gender= c("male", "other", "female", "female", "male", "male"),
key="id"
)
# Validate rules and log problemserrors<- validate(schema.customers, customers)
print(errors)

Errors table for this example will have:

nrefvaluetype
1:-1-1Condition
2:2IsabellaroseCondition
3:NANARequired
4:-1Regex
5:2otherEnum

This specification can be used within ETL or data import procedure to identify records with erros.

It will also raise error if column names or types don't match the schema. You can also stop execution based on the records in errors ouput. e.g.:

assert_that(nrow(errors) ==0)

Once you have schemas set up for your data processes it is quite easy to add monitoring suite using errors table.

About

Data Quality Manager for R

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages