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

ST_DISTANCE() returns NULL for an invalid geometry but is not marked nullable: IS NULL and IS NOT NULL both reject the row, CREATE TABLE ... AS SELECT fails

    XMLWordPrintable

Details

    • Bug
    • Status: In Review (View Workflow)
    • Critical
    • Resolution: Unresolved
    • 10.11, 13.2
    • 10.11
    • GIS

    Description

      Problem

      For an argument that is not a valid geometry, ST_DISTANCE() evaluates to NULL, but when its arguments are constants or NOT NULL columns the item is not nullable, so the optimizer and the result-field code treat it as never NULL. The same query then gives contradictory answers.

      How to repeat

      SELECT ST_DISTANCE(0x41,0x41) AS v,
             ST_DISTANCE(0x41,0x41) IS NULL AS is_null,
             ST_DISTANCE(0x41,0x41) <=> NULL AS null_safe_eq,
             COALESCE(ST_DISTANCE(0x41,0x41), 'x') AS coalesced;
       
      CREATE TABLE tn (id INT, g VARBINARY(10) NOT NULL);
      INSERT INTO tn VALUES (1, 0x41);
      SELECT ST_DISTANCE(g,g) AS d, ST_DISTANCE(g,g) IS NULL AS is_null, ST_DISTANCE(g,g) <=> NULL AS null_safe FROM tn;
      SELECT COUNT(*) FROM tn WHERE ST_DISTANCE(g,g) IS NULL;
      SELECT COUNT(*) FROM tn WHERE ST_DISTANCE(g,g) <=> NULL;
      SELECT COUNT(*) FROM tn WHERE ST_DISTANCE(g,g) IS NOT NULL;
      CREATE TABLE tctas AS SELECT ST_DISTANCE(g,g) AS d FROM tn;
       
      CREATE VIEW vd AS SELECT ST_DISTANCE(0x41,0x41) AS c;
      SELECT c, c IS NULL FROM vd;

      Actual result

      • constants: v is NULL, IS NULL is 0, <=> NULL is 1, COALESCE returns 'x'
      • NOT NULL column: d is NULL, IS NULL is 0; WHERE ... IS NULL returns 0 rows, WHERE ... IS NOT NULL returns 0 rows (the row is in neither partition), WHERE ... <=> NULL returns 1 row
      • CREATE TABLE tctas AS SELECT ST_DISTANCE(g,g) ... fails with ERROR 1048: Column 'd' cannot be null
      • the view returns 0 (the NULL becomes 0) and c IS NULL is 0

      With a nullable column argument all answers are consistent (NULL, IS NULL = 1).

      Analysis

      Item_func_distance (sql/item_geofunc.h) derives from Item_real_func_args_geometry_geometry and has no fix_length_and_dec() that marks the item nullable, so nullability is inherited from the arguments. Item_func_distance::val_real() (sql/item_geofunc.cc) returns NULL (via the mem_error label, which sets null_value) when Geometry::construct() or get_mbr() fails, i.e. for an invalid geometry. With constant or NOT NULL arguments maybe_null() is false, so IS NULL is folded to 0 and a derived column is declared NOT NULL.

      Possible fix: call set_maybe_null() for ST_DISTANCE(). Other GIS functions that return NULL for invalid input may need the same; I did not check them (a view over ST_RELATE(0x41,0x41,0x41) showed the same symptom in my sweep). I did not build or test a patch.

      The code is unchanged on current main (checked against 52391ebe4feb56c8ccbbf33a29ea3acbb7b7cea7). Found by a fuzzer that compares IS NULL with the value of every server function.

      Attachments

        Activity

          People

            rucha174 Rucha Deodhar
            hacker_zheng Wang Zheng
            Rucha Deodhar Rucha Deodhar
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 0d
                0d
                Remaining:
                Remaining Estimate - 0.25d
                0.25d
                Logged:
                Time Spent - Not Specified
                Not Specified

                Git Integration

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