> data <- tibble(a = 1:3, b = letters[c(1:2, NA)], c = 0.5 + 0:2)
> data
# A tibble: 3 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5
2     2 b       1.5
3     3 <NA>    2.5


Insert
======

> rows_insert(data, tibble(a = 4, b = "z"))
Message: Matching, by = "a"

# A tibble: 4 x 3
      a b         c
  <dbl> <chr> <dbl>
1     1 a       0.5
2     2 b       1.5
3     3 <NA>    2.5
4     4 z      NA  

> rows_insert(data, tibble(a = 3, b = "z"))
Message: Matching, by = "a"

Error: Attempting to insert duplicate rows.


Update
======

> rows_update(data, tibble(a = 2:3, b = "z"))
Message: Matching, by = "a"

# A tibble: 3 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5
2     2 z       1.5
3     3 z       2.5

> rows_update(data, tibble(a = 2:3, b = "z"), by = c("a", "b"))
Error: Attempting to update missing rows.

> rows_update(data, tibble(b = "z", a = 2:3), by = "a")
# A tibble: 3 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5
2     2 z       1.5
3     3 z       2.5


Variants: patch and upsert
==========================

> rows_patch(data, tibble(a = 2:3, b = "z"))
Message: Matching, by = "a"

# A tibble: 3 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5
2     2 b       1.5
3     3 z       2.5

> rows_patch(data, tibble(a = 2:3, b = "z"), by = c("a", "b"))
Error: Attempting to patch missing rows.

> rows_upsert(data, tibble(a = 2:4, b = "z"))
Message: Matching, by = "a"

# A tibble: 4 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5
2     2 z       1.5
3     3 z       2.5
4     4 z      NA  


Delete and truncate
===================

> rows_delete(data, tibble(a = 2:3))
Message: Matching, by = "a"

# A tibble: 1 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5

> rows_delete(data, tibble(a = 2:4))
Message: Matching, by = "a"

Error: Attempting to delete missing rows.

> rows_delete(data, tibble(a = 2:3, b = "b"))
Message: Matching, by = "a"

Message: Ignoring extra columns: b

# A tibble: 1 x 3
      a b         c
  <int> <chr> <dbl>
1     1 a       0.5

> rows_delete(data, tibble(a = 2:3, b = "b"), by = c("a", "b"))
Error: Attempting to delete missing rows.


Errors
======

> rows_insert(data[c(1, 1), ], tibble(a = 3))
Message: Matching, by = "a"

Error: `x` key values are not unique.

> rows_insert(data, tibble(a = c(4, 4)))
Message: Matching, by = "a"

Error: `y` key values are not unique.

> rows_insert(data, tibble(d = 4))
Message: Matching, by = "d"

Error: All columns in `y` must exist in `x`.

> rows_insert(data, tibble(a = 4, b = "z"), by = "e")
Error: All `by` columns must exist in `x`.

