cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: allow foreign keys between indexes with columns in different orders

Open dt opened this issue 10 years ago • 8 comments

currently the referencing and referenced indexes are assumed to be in the same order -- that is, the value appearing in the i'th column of one must appear (or not appear) in the i'th column of the other.

Since we we're encoding the index key for each index anyway from the datums, we should be able to remove that constraint. We just need to reorder the values DTuple before encoding the index key, using a int-int mapping stored in the fk ref.

Jira issue: CRDB-6160

dt avatar Aug 10 '16 19:08 dt

@dt if you please give a more concrete explanation, I think I could try to fix it.

yznming avatar Sep 03 '16 14:09 yznming

@yznming Foreign key relationships are between indexes, essentially saying any value in the referencing index must correspond to a value in the referenced index (which has to be unique). Right now it is assumed that the values are in the same order in both indexes. For example, if you have a table foo with columns x and y, and an index on [x, y], then another table bar with columns a and b that correspond to x and y in foo, in order to have a foreign key from bar to foo, there needs to be an index on [a, b].

However we should be able to use an index [b, a] too, if we keep track of which column in one index corresponds to which column in the other.

Basically we'd start with a->x, b->y, then look at foo's [x, y] index, then bar's [b, a] index and come up with a mapping of the element offsets like 0->1, 1->0 stored in the FKref on the indexes, then use that to re-order the values in the dtuple in baseFKHelper.check.

dt avatar Sep 07 '16 15:09 dt

@dt do you mean this case

CREATE TABLE orders (
  id INT PRIMARY KEY,
  product STRING NOT NULL,
  quantity INT,
  customer INT NOT NULL,
  CONSTRAINT id_customer_unique UNIQUE (id, customer)
);

CREATE TABLE reviews (
  id INT PRIMARY KEY,
  product STRING NOT NULL,
  customer INT NOT NULL,
  "order" INT NOT NULL,
  body STRING,
  CONSTRAINT order_customer_fk FOREIGN KEY (customer, "order") REFERENCES orders (customer, id)
);

yznming avatar Sep 12 '16 09:09 yznming

@yznming I think what @dt meant might be having the key in both tables with same columns ignoring their order.So in your example orders table can have [product,customer] as key and reviews table has [product,customer] as foreign key referencing the key in orders table.So the key in reviews table can also be [customer,product] i.e order does not matter and which might be implemented by this change.

nexthack avatar Nov 13 '16 19:11 nexthack

is this issue still relevant or has this been fixed ? I was trying to find refs to baseFKHelper.check and FKref but could not find any.

sum12 avatar Jan 29 '18 17:01 sum12

@RaduBerinde Once the optimizer can handle FK cascades in all cases, would this then be easy to implement?

thoszhang avatar Apr 30 '20 21:04 thoszhang

Yeah, the new path doesn't make any assumptions about columns. We'll still need to check that a unique index exists for those columns in whatever order (on the parent side). And we'd probably stop creating any child-side index automatically.

RaduBerinde avatar May 02 '20 00:05 RaduBerinde

This is finished now.

jordanlewis avatar Jun 11 '22 20:06 jordanlewis