Skip to content

Commit 643d5b2

Browse files
CasperNCasper Neo
authored andcommitted
Fix #7580 by documenting union schema evolution rules (#7585)
Co-authored-by: Casper Neo <[email protected]>
1 parent 5b0b56a commit 643d5b2

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

docs/source/Schemas.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ the world. If this is not practical for you, use explicit field ids, which
552552
should always generate a merge conflict if two people try to allocate the same
553553
id.
554554

555-
### Schema evolution examples
555+
### Schema evolution examples (tables)
556556

557557
Some examples to clarify what happens as you change a schema:
558558

@@ -614,6 +614,41 @@ Occasionally ok. You've renamed fields, which will break all code (and JSON
614614
files!) that use this schema, but as long as the change is obvious, this is not
615615
incompatible with the actual binary buffers, since those only ever address
616616
fields by id/offset.
617+
618+
#### Schema evolution examples (unions)
619+
620+
Suppose we have the following schema:
621+
```
622+
union Foo { A, B }
623+
```
624+
We can add another variant at the end.
625+
```
626+
union Foo { A, B, another_a: A }
627+
```
628+
and this will be okay. Old code will not recognize `another_a`.
629+
However if we add `another_a` anywhere but the end, e.g.
630+
```
631+
union Foo { A, another_a: A, B }
632+
```
633+
this is not okay. When new code writes `another_a`, old code will
634+
misinterpret it as `B` (and vice versa). However you can explicitly
635+
set the union's "discriminant" value like so:
636+
```
637+
union Foo { A = 1, another_a: A = 3, B = 2 }
638+
```
639+
This is okay.
640+
641+
```
642+
union Foo { original_a: A = 1, another_a: A = 3, B = 2 }
643+
```
644+
Renaming fields will break code and any saved human readable representations,
645+
such as json files, but the binary buffers will be the same.
646+
647+
648+
649+
650+
651+
617652
<br>
618653

619654
### Testing whether a field is present in a table

0 commit comments

Comments
 (0)