Assertive functions: unique key, non-NA key, etc.
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·