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

`DISTINCT` + `char_length(SPACE(AVG(…)))` + `HAVING` returns empty for one insertion order of the same rows

    XMLWordPrintable

Details

    • Unexpected results

    Description

      CREATE TABLE t (
        c_pk BIGINT NOT NULL, c_int BIGINT, c_big BIGINT, c_dec DECIMAL(10,2),
        c_dbl DOUBLE, c_txt VARCHAR(255), c_chr VARCHAR(255), c_date DATE, c_ts DATETIME(6)
      );
       
      -- Even pks first (view2), then odd pks (view23). Expected 2 rows. Actual 0 rows.
      INSERT INTO t VALUES (2, -7, 2, 999.99, 1000.125, 'trailing ', 'o''brien', '1999-12-31', '2024-01-15 12:34:56');
      INSERT INTO t VALUES (4, 2, -1, 12.34, 1.5, 'abc', NULL, '1999-12-31', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (6, 0, 0, -5.50, NULL, 'o''brien', 'trailing ', '1999-12-31', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (8, -7, 2, 999.99, 1000.125, 'trailing ', 'o''brien', '1999-12-31', '2024-01-15 12:34:56');
      INSERT INTO t VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
      INSERT INTO t VALUES (3, 2, -1, NULL, 1000.125, 'o''brien', '', '1999-12-31', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (5, 42, NULL, 0.00, NULL, '', 'trailing ', '2030-06-01', '2024-01-15 12:34:56');
      INSERT INTO t VALUES (7, NULL, 2, -5.50, -1.5, 'abc', NULL, '2024-01-15', '1999-12-31 23:59:59');
       
      SELECT DISTINCT t1.c_int,
             char_length(SPACE(AVG(CAST(t1.c_int AS SIGNED))))
      FROM t t1
      WHERE t1.c_int IS NOT NULL
      GROUP BY t1.c_int, t1.c_ts, t1.c_dec, t1.c_big, t1.c_dbl
      HAVING AVG(t1.c_big) <= t1.c_int;
      -- Expected 2 rows (0,0), (2,2). Actual 0 rows.
       
       
      -- Same rows, odd-then-even INSERT order (control: CORRECT).
      CREATE TABLE t (
        c_pk BIGINT NOT NULL, c_int BIGINT, c_big BIGINT, c_dec DECIMAL(10,2),
        c_dbl DOUBLE, c_txt VARCHAR(255), c_chr VARCHAR(255), c_date DATE, c_ts DATETIME(6)
      );
      INSERT INTO t VALUES (1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
      INSERT INTO t VALUES (3, 2, -1, NULL, 1000.125, 'o''brien', '', '1999-12-31', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (5, 42, NULL, 0.00, NULL, '', 'trailing ', '2030-06-01', '2024-01-15 12:34:56');
      INSERT INTO t VALUES (7, NULL, 2, -5.50, -1.5, 'abc', NULL, '2024-01-15', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (2, -7, 2, 999.99, 1000.125, 'trailing ', 'o''brien', '1999-12-31', '2024-01-15 12:34:56');
      INSERT INTO t VALUES (4, 2, -1, 12.34, 1.5, 'abc', NULL, '1999-12-31', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (6, 0, 0, -5.50, NULL, 'o''brien', 'trailing ', '1999-12-31', '1999-12-31 23:59:59');
      INSERT INTO t VALUES (8, -7, 2, 999.99, 1000.125, 'trailing ', 'o''brien', '1999-12-31', '2024-01-15 12:34:56');
       
      SELECT DISTINCT t1.c_int,
             char_length(SPACE(AVG(CAST(t1.c_int AS SIGNED))))
      FROM t t1
      WHERE t1.c_int IS NOT NULL
      GROUP BY t1.c_int, t1.c_ts, t1.c_dec, t1.c_big, t1.c_dbl
      HAVING AVG(t1.c_big) <= t1.c_int;
      -- 2 rows (0,0), (2,2)  ✓
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              junwenan Junwen An
              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.