Definitionadvanced
Conditional functional dependency
Also called CFD.
A functional dependency that holds only on the tuples that match a pattern of constants and wildcards. It can also fix the value that a matching tuple must have.
A conditional functional dependency (CFD) is a functional dependency that applies only to the tuples that match a pattern, and that can also state the exact value a matching tuple must have. "Area code 908 means city MH" is a CFD: it holds for the tuples with that area code and says nothing about the others. A plain functional dependency cannot say this. A denial constraint can, since each CFD is a set of denial constraints, but denial constraints also allow order comparisons, which CFDs do not have. CFDs keep the equality-only form of functional dependencies, and that keeps their reasoning closer to Armstrong's axioms.
Definition
Let be a relation schema with attributes . A conditional functional dependency is a pair , where is a functional dependency on and is a pattern tableau: a finite set of pattern tuples over , each entry of which is a constant of the domain of its attribute or the wildcard "". A value matches an entry , written , if is "" or . This extends to tuples position by position.
Set semantics holds. An instance of satisfies if for every pattern and all tuples , which may be the same tuple,
Two readings follow from the formula. With , every tuple that matches must match , so a constant in fixes a value for one tuple alone. With , two tuples that match and agree on must agree on . A plain functional dependency is the CFD with one pattern of wildcards only.
Examples and non-examples
The table Cust has the attributes CC (country code), AC (area code), PN (phone number), NM, STR, CT (city) and ZIP. It is a small table in the style of the customer example of Fan, Geerts, Jia and Kementsietsidis:
| row | CC | AC | PN | NM | STR | CT | ZIP |
|---|---|---|---|---|---|---|---|
| 0 | 01 | 908 | 1111111 | Mike | Tree Ave. | MH | 07974 |
| 1 | 01 | 908 | 1111111 | Rick | Tree Ave. | NYC | 07974 |
| 2 | 01 | 212 | 2222222 | Joe | Elm Str. | NYC | 01202 |
| 3 | 01 | 212 | 2222222 | Jim | Elm Str. | NYC | 02404 |
| 4 | 44 | 131 | 3333333 | Ben | High St. | EDI | EH4 1DT |
- A CFD with three patterns. with the patterns (_, _, _ ‖ _, _, _), (01, 908, _ ‖ _, MH, _) and (01, 212, _ ‖ _, NYC, _). Rows 2 and 3 break the first pattern, the plain functional dependency, since they agree on the phone number but not on the zip code. Row 1 breaks the second pattern alone, since its area code is 908 and its city is not MH. Rows 0 and 1 also break the first pattern on CT. Row 4 matches no constant pattern and has no partner, so it is fine. The violating rows are 0, 1, 2 and 3.
- The value of one tuple. is violated by row 1 alone, in an instance with no other row. The plain dependency holds on that instance, since one tuple never breaks a functional dependency.
- An unsatisfiable tableau. asks every tuple to have and . Only the empty instance satisfies it. Functional dependencies never conflict like this, since a one-tuple instance satisfies every set of them.
- Non-example: an order condition. "In one state, a higher salary has a rate at least as high" compares values with . It is a denial constraint but not a CFD.
- Non-example: a disjunction of values. "The city is MH or NYC" for area code 908 is not a CFD, since a pattern holds one constant or a wildcard. Two patterns with MH and NYC would ask for both.
The violations of one CFD can be found by one scan and one grouping per pattern. This is what the SQL queries and of Bohannon et al. do, the second with a GROUP BY on :
CFD-VIOLATIONS(I, (X -> Y, Tp))- 1
V <- empty - 2
for each pattern tp in Tp do - 3
M <- the rows t of I with t[X] matching tp[X] - 4
add to V each row t of M with t[Y] not matching tp[Y] - 5
group the rows of M by t[X] - 6
add to V the rows of each group that holds two different values of t[Y] - 7
return V
Line L4 finds the violations by one tuple and line L6 those by two tuples. The result is the set of rows that take part in some violating pair, the same set as checking all pairs, which the code tests against. With hashing, the cost is for rows.
Related definitions
- Functional dependency. The CFD with one pattern of wildcards.
- Denial constraint. Each CFD is a set of denial constraints. A pattern with a constant in gives a constraint on one tuple, such as . Agreement on gives constraints on two tuples.
- Consistency. A set of CFDs is consistent if some nonempty instance satisfies it. Bohannon et al. prove that this is NP-complete (Theorem 3.1). The hardness needs attributes with finite domains: without them, consistency is decidable in time (Theorem 3.2).
- Implication. Bohannon et al. give a sound and complete inference system for CFDs (Theorem 3.3) and prove that implication is coNP-complete (Theorem 3.4). For plain functional dependencies, implication takes linear time.
- Repairs. A CFD violation can be repaired by changing values or by deleting tuples, as on the database repair page. Systems such as HoloClean use CFDs as denial constraints.
- Discovery. CFDs can be mined from data, extending the discovery of functional dependencies.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/conditional-functional-dependency/. The authors are listed in prov/CYNKMP.jsonl.
"""Conditional functional dependencies and the detection of their violations.
A row is a tuple of values. A CFD is a triple (X, Y, tableau): X and Y are
lists of attribute positions and each pattern of the tableau is a pair
(px, py) of tuples, one entry per attribute of X and of Y. An entry is a
constant or the wildcard "_". A value v matches an entry e if e is "_" or
e == v. Set semantics: the rows are distinct.
"""
WILD = "_"
def matches(values, pattern):
return all(e == WILD or e == v for v, e in zip(values, pattern))
def project(row, attrs):
return tuple(row[a] for a in attrs)
def pair_violates(s, t, cfd):
"""The definition, for one pair (s, t), which may be the same row twice."""
xs, ys, tableau = cfd
for px, py in tableau:
sx, tx = project(s, xs), project(t, xs)
if sx == tx and matches(sx, px):
sy, ty = project(s, ys), project(t, ys)
if not (sy == ty and matches(sy, py)):
return True
return False
def violations_brute(rows, cfd):
"""The rows that take part in a violation, by checking every pair of rows."""
out = set()
for i, s in enumerate(rows):
for j, t in enumerate(rows):
if pair_violates(s, t, cfd):
out.update((i, j))
return out
def violations(rows, cfd):
"""The same set, found with one scan and one grouping per pattern."""
xs, ys, tableau = cfd
out = set() # @line L1
for px, py in tableau: # @line L2
hit = [i for i, r in enumerate(rows) if matches(project(r, xs), px)] # @line L3
out.update(i for i in hit if not matches(project(rows[i], ys), py)) # @line L4
groups = {}
for i in hit: # @line L5
groups.setdefault(project(rows[i], xs), []).append(i)
for members in groups.values():
if len({project(rows[i], ys) for i in members}) > 1: # @line L6
out.update(members)
return out # @line L7
def satisfies(rows, cfds):
return all(not violations(rows, c) for c in cfds)
def from_fd(xs, ys):
"""A functional dependency X -> Y as the CFD with one all-wildcard pattern."""
return (list(xs), list(ys), [((WILD,) * len(xs), (WILD,) * len(ys))])
test_conditional_functional_dependency.py 79 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/conditional-functional-dependency/. The authors are listed in prov/CYNKMP.jsonl.
import itertools
import random
from conditional_functional_dependency import WILD, from_fd, satisfies, violations, violations_brute
# Attributes: 0 CC, 1 AC, 2 PN, 3 NM, 4 STR, 5 CT, 6 ZIP.
CUST = [
("01", "908", "1111111", "Mike", "Tree Ave.", "MH", "07974"),
("01", "908", "1111111", "Rick", "Tree Ave.", "NYC", "07974"),
("01", "212", "2222222", "Joe", "Elm Str.", "NYC", "01202"),
("01", "212", "2222222", "Jim", "Elm Str.", "NYC", "02404"),
("44", "131", "3333333", "Ben", "High St.", "EDI", "EH4 1DT"),
]
PHI = (
[0, 1, 2],
[4, 5, 6],
[
((WILD, WILD, WILD), (WILD, WILD, WILD)),
(("01", "908", WILD), (WILD, "MH", WILD)),
(("01", "212", WILD), (WILD, "NYC", WILD)),
],
)
AREA_CODE_CITY = ([0, 1], [5], [(("01", "908"), ("MH",)), (("01", "212"), ("NYC",))])
def test_customer_example():
assert violations(CUST, PHI) == {0, 1, 2, 3}
# The plain dependency CC, AC -> CT holds on Rick alone; the pattern does not.
assert violations([CUST[1]], AREA_CODE_CITY) == {0}
assert violations([CUST[1]], from_fd([0, 1], [5])) == set()
assert violations([CUST[0], CUST[2], CUST[4]], AREA_CODE_CITY) == set()
def test_unsatisfiable_tableau():
# ([A] -> [B], {(_, b), (_, c)}): every row breaks one of the two patterns.
cfd = ([0], [1], [((WILD,), ("b",)), ((WILD,), ("c",))])
for row in itertools.product("abc", repeat=2):
assert violations([row], cfd) == {0}
def fd_holds(rows, xs, ys):
return all(
any(s[a] != t[a] for a in xs) or all(s[b] == t[b] for b in ys)
for s, t in itertools.product(rows, repeat=2)
)
def random_cfd(rng, nattrs):
attrs = list(range(nattrs))
rng.shuffle(attrs)
k = rng.randint(1, nattrs - 1)
xs, ys = attrs[:k], attrs[k:][: rng.randint(1, nattrs - k)]
tableau = []
for _ in range(rng.randint(1, 3)):
px = tuple(rng.choice([WILD, WILD, 0, 1]) for _ in xs)
py = tuple(rng.choice([WILD, WILD, 0, 1]) for _ in ys)
tableau.append((px, py))
return xs, ys, tableau
def test_against_brute_force_over_pairs():
rng = random.Random(13)
for _ in range(400):
nattrs = rng.randint(2, 4)
rows = list({tuple(rng.randint(0, 2) for _ in range(nattrs)) for _ in range(rng.randint(0, 12))})
cfd = random_cfd(rng, nattrs)
assert violations(rows, cfd) == violations_brute(rows, cfd)
def test_plain_fd_is_a_cfd():
rng = random.Random(3)
for _ in range(200):
rows = list({tuple(rng.randint(0, 2) for _ in range(3)) for _ in range(rng.randint(0, 8))})
xs = rng.sample(range(3), rng.randint(1, 2))
ys = [b for b in range(3) if b not in xs][:1]
assert satisfies(rows, [from_fd(xs, ys)]) == fd_holds(rows, xs, ys)
References
- Philip Bohannon, Wenfei Fan, Floris Geerts, Xibei Jia and Anastasios Kementsietsidis, Conditional functional dependencies for data cleaning, ICDE 2007, pages 746-755: the definition and Example 3.1, Theorems 3.1 to 3.4 on consistency, inference and implication, and Section 4.1 on detection in SQL
- Wenfei Fan, Floris Geerts, Xibei Jia and Anastasios Kementsietsidis, Conditional functional dependencies for capturing data inconsistencies, ACM TODS 33(2), 2008, the journal version, with the customer example, the static analysis and the SQL detection queries
- Xu Chu, Ihab F. Ilyas and Paolo Papotti, Discovering denial constraints, PVLDB 6(13), 2013, pages 1498-1509, CFDs as a special case of denial constraints
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag CYNKMP Edit this page History Report an error Markdown