Welcome to Great Expectations, FileDataAsset!

May 01, 2019

The latest Great Expectations release has first-class support for FileDataAssets, with a new set of file-level expectations. Check them out!

Recently, a colleague posted the following message in one of our internal slack channels:

Imagine (it’s easy if you try) that you have a CSV file, whose header contains 269 columns, separated by the “|” sign. The following lines in this file are comprised of data columns, one data token per header, separated by the “|” sign. Now imagine that one of those data lines contains 272 data values, separated by the “|” sign. […] If you are wondering about why these numbers are so particular, it is because this example did not need to be imagined. It is real and just happened with [redacted to protect the innocent] data set.

It is easy if you try, isn’t it? Is it so much to ask that a CSV conforms to its own spec? Now, of course most of you are immediately thinking about how fun it would be to search for should-be-escaped pipes, how exciting it would be to guess the errors by building column models, or what a hero you would be after doing something even more exotic to coax the meaning from this malformed CSV.

I get it, I go there too. But…are we really asking so much here?

We at Great Expectations (GE) have seen and heard this problem so much that, thanks to the work of @anhollis and others, file-level expectations of just this type are now first-class citizens. The FileDataAsset type is easy to connect to (you guessed it) files. And keeping with the GE spirit of verbosity, you’ll now find these easy-to-understand expectations available right out of the box:

expect_file_to_exist
expect_file_size_to_be_between
expect_file_hash_to_equal
expect_file_to_have_valid_table_header
expect_file_to_be_valid_json
expect_file_line_regex_match_count_to_be_between
expect_file_line_regex_match_count_to_equal



With those expectations in place, we can easily bring more components of our pipeline under test starting with this release of GE.

Let’s replay my colleague’s story using the new FileDataAsset type.

expect_file_to_exist is a pretty minimal place to start, but it could be useful if all we know is that we should get data in a, say, daily drop and we want to alert if the data never shows up (since many pipelines would just happily process all zero files in the staging area).

We believe that testing for simple things that cause silent failures later — for example no data loading because it was never delivered — is an important pattern in Great Expectations. So the most basic config is really simple:

{
  "data_asset_name": "daily_delivery",
  "data_asset_type": "FileDataAsset",
  "meta": {
    "great_expectations.**version**": "0.5.1"
  },
  "expectations": [
   {
     "expectation_type": "expect_file_to_exist",
     "kwargs": {
       "filepath": {
         "$PARAMETER": "filename"
       }
     }
   }
  ]
}



Notice that we’re using filename as an evaluation parameter.*

Clearly though, we have higher expectations than just those: let’s use expect_file_line_regex_match_count_to_equal to ensure we have the right number of values on each line. Again, a common pattern in Great Expectations is to start broad, then refine expectations using annotations that describe data. Our first expectation config might look something like this:

{
  "data_asset_name": "daily_delivery",
  "data_asset_type": "FileDataAsset",
  "meta": {
    "great_expectations.**version**": "0.5.1"
  },
  "expectations": [
    {
      "expectation_type": "expect_file_line_regex_match_count_to_be_between",
      "kwargs": {
       "regex": "\\|",
       "expected_min_count": 268,
       "expected_max_count": 268
      }
    }
  ]
}



Now, the rows with “extra values” will be immediately flagged and available for inspection and quick refinement. Pivoting to a toy dataset, observe the following output:

> asset.expect_file_line_regex_match_count_to_be_between(r'\|', 3, 3, 
  result_format="SUMMARY")
> {'success': False,
  'result': {'element_count': 12,
  'missing_count': 2,
  'missing_percent': 0.16666666666666666,
  'unexpected_count': 2,
  'unexpected_percent': 0.16666666666666666,
  'unexpected_percent_nonmissing': 0.2,
  'partial_unexpected_list': ['B|"C|"|5|4\n', 'B|"why| C"|1|4\n'],
  'partial_unexpected_index_list': [4, 5],
  'partial_unexpected_counts': [{'value': 'B|"C|"|5|4\n', 'count': 1},
  {'value': 'B|"why| C"|1|4\n', 'count': 1}]}}



This provides immediate feedback we can use to refine our pipeline — in this case perhaps by adjusting our treatment of quoted characters — and simultaneously update our expectation:

>  asset.expect*file_line_regex_match_count_to_equal(r'\|(?=([^"\\]*(\\.|"([^"\\]_\\.)_[^"\\]_"))_[^"]\_\$)', 3, meta={"notes": "We    observed quoted pipes in about 15 percent of the first test dataset; for example ‘B|\”C|\”|5|4’"})
   {
     "data_asset_name": "daily_delivery",
     "data_asset_type": "FileDataAsset",
     "meta": {
       "great_expectations.__version__": "0.5.0"
     },
     "expectations": [
       {
         "expectation_type": "expect_file_line_regex_match_count_to_be_between",
         "kwargs": {
         "regex": "\\|",
         "expected_min_count": 3,
         "expected_max_count": 3
       }
       },
       {   
         "expectation_type": "expect_file_line_regex_match_count_to_equal",
         "kwargs": {
           "regex": "\\|(?=([^\"\\\\]*(\\\\.|\"([^\"\\\\]_\\\\.)_[^\"\\\\]_\"))_[^\"]\_\$)",
           "expected_count": 3
         },
         "meta": {
           "notes": "We observed quoted pipes in about 15 percent of the first test dataset; for example \u2018B|\\\u201dC|\\\u201d|5|4\u2019"
         }
       }
     ]
   }



Of course, that’s just the beginning (we just wrote a pretty ugly regex that already threatens to keep me up at night**). Keeping with the spirit of GE, we don’t expect that those expectations will cover all your needs, and we don’t expect that all quality issues will hit on your first round of expectations — but with checks like these in place you can more quickly zero in on problems and help convert tacit knowledge about datasets and data flows into explicit, testable statements.

Further, the FileDataAsset works like all the other core GE classes, making it easy to extend the logic with custom expectations that can still be easily understood, evaluated, and managed alongside other expectations relevant for your core business. For example, much like with the Dataset types, a FileDataAsset provides line-by-line parsing of text files, opening the map_expectation semantics to a wider range of logical locations in your data processing pipelines.

May your expectations always be fulfilled,

The Great Expectations Team

* we could also have omitted the parameter, and created a FileDataAsset in the usual way, with a path parameter.

** For those of you who, like me, hate parsing regexes yourself: we’re looking for | characters that are not in blocks surrounded by quotation marks (and handling escapes). If you decide to include such a regex in your expectations, I’d strongly encourage you to add a nice comment to your expectation’s meta element! And, please note that I gratefully borrowed that regex from this stackoverflow question.

*** Please feel free to ask questions or interact with us through Slack and/or [GitHub issues]


This post was originally posted on medium.

Have something to say about our blog? Shout it from the rooftops!
The Great Expectations Team

Written by The Great Expectations Team
You should star us on Github