Assertive functions: unique key, non-NA key, etc.

Open
#3,161 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
20/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

No source file, test, or entry point is named. Start with the linked discussion and the wrapper examples in the issue, then determine whether the project has agreed on a focused subset of the proposed assertions. Done would require a settled API and scope before implementation can begin.

Written by the indexing model from the issue text.

Description

feature request

This is related to the discussion of Some assertive functions.

DESCRIPTION

I believe one of the common use cases is using data.table as an in-memory relational database, whose syntax happens to be much more succinct, flexible and expressive than SQL and performance is extremely efficient.

The relational database has developed for years. The schema ties different tables together by using concepts like primary key, foreign keys, field types, constraints, etc. Of course, implementing all such things would certainly be over-kill and not necessary.

However, in my daily routine, I often find myself in a situation that I need to assert my data.table object has certain simple yet important properties. The most common assertations for me are:

  • The key of the table is unique and contains no NA - similar to the Primary Key;
  • The key of the table is unique and allows NA - similar to the Unique Key;
  • A field only contains pre-defined values - similar to the Foreign Key Constraint;
  • All the non-ASCII columns are encoded in UTF-8, if not, converted to it - usually before writing into an RDS file;
  • All the timezone of the date-time columns are all Asia/Shanghai - the timezone of China - this point is not that important, though.
SUGGESTION

For the first three points, the interface seems fits the is.data.table() function. For example, is.data.table(x, key = c("nocheck", "primary", "unique", "exist"), foreign_constraint = list(field = c(xxx))).

For the last two points, the interface may be setutf8(), settz().

Your ideas?

Thanks.


Some wrappers I use for myself
# the key columns should contain no NA. Otherwise, the NA in the position i will match to the NA in the key 
# usually we don't want that
no_na_in_keys <- function (x) {
    stopifnot(data.table::is.data.table(x), data.table::haskey(x))
    all(purrr::map_lgl(key(x), ~!anyNA(x[[.]]))) # Of course I will not use purrr when I file the PR :D
}

# although we can set `mult = 'first'` to avoid the duplicate return values, we'd like to claim it's a data.table
# contains no duplicate keys in advance
is_unique_dt <- function (x) {
    data.table::is.data.table(x) && data.table::haskey(x) && 
        identical(anyDuplicated(x, by = key(x)), 0L)
}

# is the data.table has a primary key?
is_pk_dt <- function (x) {
    is_unique_dt(x) && no_na_in_keys(x)
}
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.