danielcweeks commented on code in PR #18416:
URL: https://github.com/apache/iceberg/pull/18416#discussion_r4668048376


##########
format/spec.md:
##########
@@ -308,6 +310,24 @@ For details on how to serialize a schema to JSON, see 
Appendix C.
 
 [1001]: <https://portal.ogc.org/files/?artifact_id=25355> "OGC Simple feature 
access"
 
+##### Vector
+
+A non-null `vector(E, D)` contains exactly D elements of element type E, and 
every element is non-null. The vector value may be null only when the vector 
field is optional.
+
+E must be one of the following vector element types:
+
+| Element type  | Element values                 | Value type |
+|---------------|--------------------------------|------------|
+| **`int`**     | 32-bit signed integers         | `int`      |
+| **`long`**    | 64-bit signed integers         | `long`     |
+| **`float16`** | 16-bit IEEE 754 floating point | `float`    |
+| **`float`**   | 32-bit IEEE 754 floating point | `float`    |
+| **`double`**  | 64-bit IEEE 754 floating point | `double`   |
+
+Vector element types are valid only inside a `vector`; `float16` is not a 
primitive type and cannot be used as the type of a field. Element values follow 
the rules of the value type, including JSON single-value serialization. Vector 
equality is element-wise, using the equality rules of the value type. 
Floating-point elements must be finite: `NaN`, `+Infinity`, and `-Infinity` are 
invalid.

Review Comment:
   I believe we need one more level of validation for `float16`. Its value type 
is `float`, and that type drives JSON single-value serialization and the Avro 
mapping (line 1904 writes 4-byte floats). So what happens with `[0.1, 70000.0]` 
in a `vector(float16, 2)`? 0.1 isn't exactly representable in binary16, and 
70000 overflows to Infinity. Parquet requires exact preservation and can't hold 
either. "Readers and writers must enforce the vector dimension and element 
requirements" doesn't cover this, because the element requirements only rule 
out null, NaN, and infinity. Some readers will round and others will fail, so 
the same table can return different values depending on the file format.
   
   Something like: "`float16` element values must be finite IEEE 754 binary16 
values. When stored or serialized using the `float` value type, the value must 
be exactly representable as binary16; writers must not produce other values and 
readers must reject them." Or drop `float16` from this PR. New element types 
can be added to the table later without breaking anything.
   
   <!-- lenses: weeks-review, blue-spec-review, russell-review -->



##########
format/spec.md:
##########
@@ -729,6 +749,8 @@ Order id `0` is reserved for the unsorted order.
 
 Sorting floating-point numbers should produce the following behavior: `-NaN` < 
`-Infinity` < `-value` < `-0` < `0` < `value` < `Infinity` < `NaN`. This aligns 
with the implementation of Java floating-point types comparisons.
 
+Vector values of the same `vector(E, D)` type must be sorted lexicographically 
using the [comparison rules](expressions-spec.md#comparisons) of E's value type.

Review Comment:
   Do we actually need to define an ordering for vectors? `identity` excludes 
`vector` and no other transform accepts it. Sort orders can't reference vector 
fields, and vectors are excluded from `lower_bound`/`upper_bound`. Nothing in 
Sorting can use this ordering. It also sets up a cycle: expressions-spec.md:169 
points here for ordering, and this sentence points back to expressions-spec 
`#comparisons`. The only thing it enables is `<`/`>` on vectors, and 
lexicographic order over embeddings isn't meaningful. It's easy to add ordering 
later if a use case comes up, but hard to take it out once it's in the spec.
   
   I'd drop this sentence and keep vectors equality-only for now. If we do need 
ordering, it should be defined once, next to the type in the Vector section, 
not here.
   
   <!-- lenses: weeks-review, blue-spec-review, russell-review -->



##########
format/spec.md:
##########
@@ -1878,6 +1901,7 @@ Maps with non-string keys must use an array 
representation with the `map` logica
 |**`binary`**|`bytes`||
 |**`struct`**|`record`||
 |**`list`**|`array`||
+|**`vector(E, D)`**|`array`|When writing, array elements must use the Avro 
mapping for E's value type and must not be null. The array must not have an 
`element-id` property. Readers and writers must enforce the vector dimension 
and element requirements.|

Review Comment:
   nit: why "When writing"? Parquet gets an explicit read rule, but Avro 
doesn't, so this reads as if readers may be lenient in some unstated way. I'd 
drop "When writing, " so it reads "Array elements must use the Avro mapping for 
E's value type and must not be null."
   
   <!-- lenses: russell-review -->



##########
format/spec.md:
##########
@@ -1934,12 +1960,25 @@ Lists must use the [3-level 
representation](https://github.com/apache/parquet-fo
 | **`binary`**       | `binary`                                                
                                                                                
     |                                             |                            
                                    |
 | **`struct`**       | `group`                                                 
                                                                                
     |                                             |                            
                                    |
 | **`list`**         | `3-level list`                                          
                                                                                
     | `LIST`                                      | See Parquet docs for 
3-level representation.                   |
+| **`vector(E, D)`** | `3-level list`                                          
                                                                                
     | `FINITE_VECTOR(D)`                          | The outer group carries 
the field ID of the vector. When writing, the `element` field must use the 
Parquet type for E listed in the vector element table below. Writing and 
reading an element must preserve its value exactly. The ConvertedType must be 
`LIST`. The `list` and `element` fields must not be assigned field IDs. |
 | **`map`**          | `3-level map`                                           
                                                                                
     | `MAP`                                       | See Parquet docs for 
3-level representation.                   |
 | **`variant`**      | `group` with `metadata` and `value` fields. `metadata` 
and `value` must not be assigned field IDs and the fields are accessed through 
names. | `VARIANT`                                   | See Parquet docs for 
[Variant 
encoding](https://github.com/apache/parquet-format/blob/master/VariantEncoding.md)
 and [Variant shredding 
encoding](https://github.com/apache/parquet-format/blob/master/VariantShredding.md).
 |
 | **`geometry`**     | `binary`                                                
                                                                                
     | `GEOMETRY`                                  | WKB format, see [Appendix 
G](#appendix-g-geospatial-notes).                             |
 | **`geography`**    | `binary`                                                
                                                                                
     | `GEOGRAPHY`                                 | WKB format, see [Appendix 
G](#appendix-g-geospatial-notes).                             |
 | **`file`**         | `group` with the `file` sub-fields. Sub-fields must be 
assigned field IDs.                                                             
      | `FILE`                                      | See Parquet docs for the 
[`FILE` 
type](https://github.com/apache/parquet-format/blob/master/LogicalTypes.md#file)
 and [File Type](#file-type). |
 
+Vector elements must be written to Parquet using the following types:
+
+| Element type  | Parquet physical type     | Logical type |
+|---------------|---------------------------|--------------|
+| **`int`**     | `int32`                   |              |
+| **`long`**    | `int64`                   |              |
+| **`float16`** | `fixed_len_byte_array[2]` | `FLOAT16`    |
+| **`float`**   | `float`                   |              |
+| **`double`**  | `double`                  |              |
+
+When reading `vector(E, D)`, the `element` field must use the Parquet mapping 
for E. The `FINITE_VECTOR` `num_elements` parameter must equal D.

Review Comment:
   nit: the Parquet mapping is now stated three times. `num_elements` = D is at 
line 1940 and again here, and the element mapping is split across the row note 
(write), the element table, and this sentence (read). Now that the element 
table pins the types, "Writing and reading an element must preserve its value 
exactly" in the vector row doesn't add anything either. I'd keep the line 1940 
sentence and the element table, drop this sentence, and change the row note to 
say "The `element` field must use the Parquet type for E listed in the vector 
element table below."
   
   <!-- lenses: weeks-review, blue-spec-review, russell-review -->



##########
format/expressions-spec.md:
##########
@@ -166,6 +166,7 @@ Primitive types are compared using signed comparison, 
except for the following t
 * `uuid` uses unsigned byte-wise comparison of the UUID bytes
 * `decimal` uses signed comparison independent of scale; this is equivalent to 
comparison of unscaled values because type alignment produces values with the 
same scale
 * `float` and `double` use IEEE 754 order for all non-NaN values; see below 
for NaN comparison rules
+* `vector` values use the equality rules defined in [Vector](spec.md#vector) 
and the ordering defined in [Sorting](spec.md#sorting) in the table spec

Review Comment:
   nit: right now this bullet sends readers to the table spec's Sorting 
section, and Sorting sends them back here. An engine implementing comparisons 
on vectors shouldn't have to go back and forth between the two documents. If 
ordering is dropped, this can just be:
   
   ```suggestion
   * `vector` values support only equality comparison; vectors are equal if all 
elements are equal using the rules of the element's value type
   ```
   
   <!-- lenses: blue-spec-review, russell-review -->



##########
format/spec.md:
##########
@@ -308,6 +310,24 @@ For details on how to serialize a schema to JSON, see 
Appendix C.
 
 [1001]: <https://portal.ogc.org/files/?artifact_id=25355> "OGC Simple feature 
access"
 
+##### Vector
+
+A non-null `vector(E, D)` contains exactly D elements of element type E, and 
every element is non-null. The vector value may be null only when the vector 
field is optional.
+
+E must be one of the following vector element types:
+
+| Element type  | Element values                 | Value type |
+|---------------|--------------------------------|------------|
+| **`int`**     | 32-bit signed integers         | `int`      |
+| **`long`**    | 64-bit signed integers         | `long`     |
+| **`float16`** | 16-bit IEEE 754 floating point | `float`    |
+| **`float`**   | 32-bit IEEE 754 floating point | `float`    |
+| **`double`**  | 64-bit IEEE 754 floating point | `double`   |
+
+Vector element types are valid only inside a `vector`; `float16` is not a 
primitive type and cannot be used as the type of a field. Element values follow 
the rules of the value type, including JSON single-value serialization. Vector 
equality is element-wise, using the equality rules of the value type. 
Floating-point elements must be finite: `NaN`, `+Infinity`, and `-Infinity` are 
invalid.
+
+Type promotion is not allowed for `vector(E, D)`. The element type E, 
dimension D, and vector field ID must not change. A `vector` must not be 
promoted to a non-vector type. A non-vector type other than `unknown` must not 
be promoted to `vector`. A `list` is not a `vector(E, D)`, even if every list 
instance contains exactly D elements that satisfy the vector element 
requirements.

Review Comment:
   I don't think we need this paragraph. It says "no promotion" four different 
ways. The type promotion table in Schema Evolution is an allow-list: `vector` 
isn't in it, so it can't be promoted, and `unknown` -> any type already covers 
the one allowed path. Restating it here gives us two places to keep in sync. 
"vector field ID must not change" also suggests field IDs can change for other 
types, and they can't.
   
   Can we remove it? If we want it to be explicit, a short note in Schema 
Evolution would work: "`vector(E, D)` cannot be promoted; E and D are part of 
the type."
   
   <!-- lenses: blue-spec-review, weeks-review, russell-review -->



##########
format/spec.md:
##########
@@ -729,6 +749,8 @@ Order id `0` is reserved for the unsorted order.
 
 Sorting floating-point numbers should produce the following behavior: `-NaN` < 
`-Infinity` < `-value` < `-0` < `0` < `value` < `Infinity` < `NaN`. This aligns 
with the implementation of Java floating-point types comparisons.
 
+Vector values of the same `vector(E, D)` type must be sorted lexicographically 
using the [comparison rules](expressions-spec.md#comparisons) of E's value type.

Review Comment:
   This doesn't sound right.  Lexicographic sorting is based on characters, not 
numeric values.  Based on how I'm reading this a vector(int, 2) would sort as
   ```
   SORT ASC
   (12,1)
   (2,1)
   ```
   
   Do we mean to say, "must be sorted numerically using . . . "



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to