Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-41639

VEC_DISTANCE_COSINE returns 0 for small-magnitude non-zero orthogonal and opposite vectors

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Minor
    • Resolution: Unresolved
    • 12.3.2
    • None
    • Vector search
    • None
    • Windows x64
    • Fix incorrect cosine distance results for small-magnitude non-zero vectors.

    Description

      Description

      VEC_DISTANCE_COSINE returns incorrect results for small-magnitude, non-zero vectors.
      For non-zero finite vectors, cosine distance should be invariant under positive scaling. In particular, two orthogonal vectors should have a cosine distance of 1, and two antiparallel vectors should have a cosine distance of 2.
      On MariaDB Server 12.3.2, VEC_DISTANCE_COSINE returns 0 for both cases when the vector components are 1e-23. The same calculations return the expected values when the components are 1e-22.
      These inputs are non-zero and representable as vector elements. The Euclidean-distance control also returns a non-zero result for the small vectors.

      How to repeat

      SELECT VERSION(), @@version_compile_os, @@version_compile_machine;

      SELECT
      VEC_DISTANCE_EUCLIDEAN(
      VEC_FromText('[1e-23,0]'),
      VEC_FromText('[0,1e-23]')
      ) AS small_euclidean,
      VEC_DISTANCE_COSINE(
      VEC_FromText('[1e-23,0]'),
      VEC_FromText('[0,1e-23]')
      ) AS small_orthogonal,
      VEC_DISTANCE_COSINE(
      VEC_FromText('[1e-23,0]'),
      VEC_FromText('[-1e-23,0]')
      ) AS small_opposite;

      SELECT
      VEC_DISTANCE_COSINE(
      VEC_FromText('[1e-22,0]'),
      VEC_FromText('[0,1e-22]')
      ) AS control_orthogonal,
      VEC_DISTANCE_COSINE(
      VEC_FromText('[1e-22,0]'),
      VEC_FromText('[-1e-22,0]')
      ) AS control_opposite;

      Actual result

      small_euclidean: approximately 1.4142135621184782e-23
      small_orthogonal: 0.0
      small_opposite: 0.0

      control_orthogonal: 1.0
      control_opposite: 2.0

      Expected result

      small_euclidean should be non-zero.
      small_orthogonal should be approximately 1.0.
      small_opposite should be approximately 2.0.

      Attachments

        Activity

          People

            Unassigned Unassigned
            123 zhang hanshuo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.