Skip to content

[Bug]: mo does not support aggregation with non-equality conditions in subqueries with predicates. #23942

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main 3.0-dev

Commit ID

413bff6

Other Environment Information

- Hardware parameters:
- OS type:
- Others:

Actual Behavior

CREATE TABLE ms_t_stk_sis (
SITXDT varchar(24) DEFAULT NULL COMMENT 'Trading date. Format: DDMONYYYY:HH:MI:SS (e.g. 02JAN2025:00:00:00).',
SISTKC varchar(10) DEFAULT NULL COMMENT 'HKEX stock code (e.g. 00001, 00005).',
SISTKN varchar(100) DEFAULT NULL COMMENT 'Stock name / company name in English.',
SIHIGH decimal(16,4) DEFAULT NULL COMMENT 'Highest traded price of the day (HKD).',
SILOW decimal(16,4) DEFAULT NULL COMMENT 'Lowest traded price of the day (HKD).',
SICLSE decimal(16,4) DEFAULT NULL COMMENT 'Closing price of the day (HKD).',
SIVOL bigint DEFAULT NULL COMMENT 'Trading volume in number of shares traded.'
);
INSERT INTO ms_t_stk_sis (SITXDT, SISTKC, SISTKN, SIHIGH, SILOW, SICLSE, SIVOL) VALUES ('02JAN2025:00:00:00', '01870', 'ACME INTL HLDGS', 1.8400, 1.7600, 1.8400, 1395000), ('02JAN2025:00:00:00', '01871', 'CHINA ORIENTED', NULL, NULL, 0.1990, 0), ('02JAN2025:00:00:00', '01872', 'GUAN CHAO HLDGS', 1.2400, 1.0100, 1.0600, 230500);

SELECT s.SISTKC,
(SELECT AVG(s2.SIVOL)
FROM ms_t_stk_sis s2
WHERE s2.SISTKC = s.SISTKC
AND STR_TO_DATE(s2.SITXDT, '%d%b%Y:%H:%i:%s') < STR_TO_DATE(s.SITXDT, '%d%b%Y:%H:%i:%s')
AND STR_TO_DATE(s2.SITXDT, '%d%b%Y:%H:%i:%s') >= STR_TO_DATE(s.SITXDT, '%d%b%Y:%H:%i:%s') - INTERVAL 30 DAY
) AS avg_30d_vol
FROM ms_t_stk_sis s;

Image

Expected Behavior

Image

Steps to Reproduce

as above.

Additional information

No response

Activity

  1. added this to the MO-202603 milestone on Mar 23, 2026
  2. added
    kind/bug
    on Mar 23, 2026
  3. added theissue type on Mar 23, 2026
  4. added
    severity/s0
    on Mar 23, 2026
  5. ck89119 commented on Apr 8, 2026

    @ck89119
    Contributor

    fixed

  6. Ariznawlll commented on Apr 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified on current main (commit 1a29e67, mo-service build commit 1a29e67). The SQL from the issue now runs successfully and returns the expected result: three rows with avg_30d_vol = NULL for SISTKC 01870, 01871, and 01872. I also confirmed the related fix is present: 89f1e23 / PR #24053, which adds support for scalar aggregate subqueries with non-equality correlated predicates. I tried to run go test ./pkg/sql/plan -run TestSubQuery -count=1 as an additional check, but this local environment cannot compile pkg/sql/plan because CGO headers usearch.h and xxhash.h are missing, so that test was blocked by environment setup rather than a case failure. Based on the direct SQL verification, this issue is fixed on current main.

  7. Ariznawlll commented on Apr 30, 2026

    @Ariznawlll
    CollaboratorAuthor
    CREATE DATABASE bug23942_verify_codex_20260429_2235;
    USE bug23942_verify_codex_20260429_2235;
    
    CREATE TABLE ms_t_stk_sis (
      SITXDT varchar(24) DEFAULT NULL,
      SISTKC varchar(10) DEFAULT NULL,
      SISTKN varchar(100) DEFAULT NULL,
      SIHIGH decimal(16,4) DEFAULT NULL,
      SILOW decimal(16,4) DEFAULT NULL,
      SICLSE decimal(16,4) DEFAULT NULL,
      SIVOL bigint DEFAULT NULL
    );
    
    INSERT INTO ms_t_stk_sis (SITXDT, SISTKC, SISTKN, SIHIGH, SILOW, SICLSE, SIVOL) VALUES
      ('02JAN2025:00:00:00', '01870', 'ACME INTL HLDGS', 1.8400, 1.7600, 1.8400, 1395000),
      ('02JAN2025:00:00:00', '01871', 'CHINA ORIENTED', NULL, NULL, 0.1990, 0),
      ('02JAN2025:00:00:00', '01872', 'GUAN CHAO HLDGS', 1.2400, 1.0100, 1.0600, 230500);
    
    SELECT s.SISTKC,
      (SELECT AVG(s2.SIVOL)
       FROM ms_t_stk_sis s2
       WHERE s2.SISTKC = s.SISTKC
         AND STR_TO_DATE(s2.SITXDT, '%d%b%Y:%H:%i:%s') < STR_TO_DATE(s.SITXDT, '%d%b%Y:%H:%i:%s')
         AND STR_TO_DATE(s2.SITXDT, '%d%b%Y:%H:%i:%s') >= STR_TO_DATE(s.SITXDT, '%d%b%Y:%H:%i:%s') - INTERVAL 30 DAY
      ) AS avg_30d_vol
    FROM ms_t_stk_sis s;
    
    DROP DATABASE bug23942_verify_codex_20260429_2235;

    查询返回:

    +--------+-------------+
    | SISTKC | avg_30d_vol |
    +--------+-------------+
    | 01870  |        NULL |
    | 01871  |        NULL |
    | 01872  |        NULL |
    +--------+-------------+
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions