LantaoJin opened a new pull request, #5331:
URL: https://github.com/apache/calcite/pull/5331

   <!--
   Thanks for sending a pull request! Here are some critical tips for you:
   
   1. READ THE GUIDE FIRST: https://calcite.apache.org/develop/#contributing
      *For significant contributions, please discuss on the dev mailing list or 
Jira BEFORE coding.*
   
   2. JIRA IS USUALLY MANDATORY: Ensure you have created an issue on the 
Calcite Jira:
      https://issues.apache.org/jira/projects/CALCITE/issues
      *Check existing issues first to avoid duplicates.*
      *Note: A Jira is NOT required for typos and cosmetic changes (i.e., 
changes that are neither bugs nor features).*
   
   3. TIMING: Strongly recommended to create the Jira BEFORE you start writing 
code (e.g., a day or so before posting a PR).
      This gives others a chance to weigh in on your specification.
   
   4. CRITICAL CONSISTENCY RULE
      The following three items MUST match exactly in wording and meaning:
      (A) The Jira Issue Title
      (B) This Pull Request Title
      (C) Your Git Commit Message
   
      Format: [CALCITE-XXXX] <Description>
      Example: [CALCITE-0000] Add IF NOT EXISTS clause to CREATE TABLE
   
      Guidelines for a good Title:
      - Illustrate using SQL keywords (in ALL-CAPS) rather than Java method 
names.
      - Focus on the specification and user experience, not the implementation.
      - Make it clear whether it is a bug or a feature.
      - Use words like "should" to indicate desired behavior vs. current 
behavior (e.g., "Validator should not close model file").
   
   5. REPRODUCTION: If fixing a bug, please provide a concise SQL example or 
test case to reproduce the issue for a faster review.
   
   6. TESTING: Ensure `./gradlew build` passes and appropriate tests are 
added/updated.
   -->
   
   ## Jira Link
   
   [CALCITE-7857](https://issues.apache.org/jira/browse/CALCITE-7857)
   
   ## Changes Proposed
   <!--
   Please clarify what changes you are proposing. The purpose of this section 
is to outline the changes and how this PR fixes the issue.
   If possible, please consider writing useful notes for better and faster 
reviews in your PR.
   -->
   
   Adds the type `VECTOR(dimension [, elementType])`: a list of a fixed number 
of non-null numbers, such as an embedding. The element type is `REAL` (the 
default) or `TINYINT`. The semantics, the alternatives and the open questions 
are in the Jira case.
   
   ```sql
   SELECT CAST(ARRAY[1, 2.5, -3] AS VECTOR(3));   -- VECTOR(3, REAL)
   SELECT CAST('[1, 2.5, -3]' AS VECTOR(3));      -- same value
   SELECT CAST(? AS VECTOR(768));                 -- bind a java.util.List
   CREATE TABLE docs (id INT, embedding VECTOR(768));
   INSERT INTO docs VALUES (1, ARRAY[...]);       -- implicit cast on INSERT
   ```
   
   Changes:
   
   - **Type.** New `SqlTypeName.VECTOR`, `SqlTypeFamily.VECTOR` and 
`VectorSqlType` (a sibling of `ArraySqlType`, not a subclass), created by the 
new abstract method `RelDataTypeFactory.createVectorType(elementType, 
dimension)`, like `createMeasureType`. Parser support through 
`SqlVectorTypeNameSpec`. Adding an abstract method breaks custom 
`RelDataTypeFactory` implementations; this is noted in `history.md`.
   - **Casts.** A vector casts to and from an `ARRAY` of numbers, a character 
string such as `'[1, 2.5, -3]'`, and a `VECTOR` of the same dimension with 
another element type. A wrong number of elements is a validation error for an 
array constructor or a character literal, and a run-time error otherwise; 
`SAFE_CAST` and `TRY_CAST` return `NULL` instead.
   - **Dynamic parameters.** A parameter gets a `VECTOR` type from `CAST(? AS 
VECTOR(n))` or from its context (`INSERT INTO docs VALUES (?, ?)`). Its value, 
a list of numbers or strings, is checked and converted when the parameter is 
read.
   - **Implicit conversions.** None in expressions. With type coercion, 
`INSERT` and `UPDATE` cast an `ARRAY`, a string or a vector with another 
element type to a `VECTOR` target column.
   - **Comparability.** `VectorSqlType.getComparability()` is `NONE`. The 
validator rejects a vector in comparisons, `IN`, `BETWEEN`, `GREATEST`, 
`LEAST`, `MIN`, `MAX`, `MODE`, `APPROX_COUNT_DISTINCT` and `DECODE`, and as a 
key of `ORDER BY`, `GROUP BY`, `PARTITION BY`, `DISTINCT`, set operations, 
`USING` / `NATURAL` joins and `MATCH_RECOGNIZE`. These clause-level checks 
apply only to `VECTOR`, so other types with comparability `NONE` behave as 
before.
   - **Run time, serialization, SQL generation.** At run time a vector is a 
`java.util.List`, as an `ARRAY` is; JDBC returns it as a `java.sql.Array`. 
`RelJson` writes the component type and dimension. The Calcite dialect writes 
`VECTOR(n, elementType)`, the PostgreSQL dialect writes pgvector's `VECTOR(n)`, 
and other dialects throw. The JDBC adapter does not push down vector 
expressions that the target dialect does not support.
   - **Docs.** `reference.md` documents the type, casts and restrictions.
   
   The Jira case also lists the key of `ARG_MIN`, `ARG_MAX`, `MIN_BY` and 
`MAX_BY` among the places where a vector is rejected. That check comes from the 
fix for [CALCITE-7858](https://issues.apache.org/jira/browse/CALCITE-7858), 
which is a separate pull request, so this PR neither tests nor documents it. 
Whichever of the two PRs merges second adds the two `testVectorNotComparable` 
assertions and the `reference.md` clause.
   
   Tests: `SqlParserTest`, `SqlValidatorTest` (`testVectorType`, 
`testVectorTypeInvalid`, `testVectorNotComparable`), `SqlOperatorTest`, 
`SqlTypeFactoryTest`, `SqlTypeUtilTest`, `RelWriterTest`, 
`RelToSqlConverterTest`, `JdbcAdapterTest`, `JdbcTest`, 
`CalciteRemoteDriverTest`, `ServerTest` and the Quidem scripts 
`core/src/test/resources/sql/vector.iq` and 
`server/src/test/resources/sql/table.iq`.
   
   Follow-ups, out of scope: distance functions for each library and dialect, 
more element types and sparse vectors, SQL generation for other databases with 
a vector type, and comparability checks for vectors nested in `ROW`, `ARRAY`, 
`MAP` and `MULTISET`.


-- 
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]

Reply via email to