Skip to content

[Bug]: Incorrect JSON grouping and array ordering in FOR JSON AUTO #4616

Description

@Teletele-Lin

What happened?

FOR JSON AUTO: only compares with the last element (the previous row) and appends sequentially.

There are two logical bugs in the checkForDuplicateRows function within forjson.c. These bugs violate the core streaming semantics of FOR JSON AUTO and scramble the output order.

TEST CASES:

DROP TABLE Departments, Employees;

-- 1. Create Departments table
CREATE TABLE Departments (
    DeptID INT PRIMARY KEY,
    DeptName NVARCHAR(50),
    Location NVARCHAR(50)
);

-- 2. Create Employees table
CREATE TABLE Employees (
    EmpID INT PRIMARY KEY,
    FullName NVARCHAR(50),
    JobTitle NVARCHAR(50),
    Salary DECIMAL(10, 2),
    DeptID INT, -- Foreign Key
    Email NVARCHAR(100)
);

-- 3. Insert test data
INSERT INTO Departments (DeptID, DeptName, Location)
VALUES 
(1, N'R&D Department', N'Beijing'),
(2, N'Marketing Department', N'Shanghai');

INSERT INTO Employees (EmpID, FullName, JobTitle, Salary, DeptID, Email)
VALUES 
(101, N'Zhang San', N'Senior Engineer', 25000.00, 1, 'zhangsan@demo.com'),
(102, N'Li Si', N'Test Engineer', 15000.00, 2, NULL), -- Email is NULL
(103, N'Wang Wu', N'Marketing Manager', 18000.00, 1, 'wangwu@demo.com');

SELECT d.DeptName, d.Location, e.FullName, e.JobTitle 
FROM Departments d 
JOIN Employees e ON d.DeptID = e.DeptID;

SELECT d.DeptName, d.Location, e.FullName, e.JobTitle 
FROM Departments d 
JOIN Employees e ON d.DeptID = e.DeptID 
FOR JSON AUTO; -- without order by

IN SQL Server:

DeptName	Location	FullName	JobTitle
R&D Department	Beijing	Zhang San	Senior Engineer
Marketing Department	Shanghai	Li Si	Test Engineer
R&D Department	Beijing	Wang Wu	Marketing Manager
[
  {
    "DeptName": "R&D Department",
    "Location": "Beijing",
    "e": [
      {
        "FullName": "Zhang San",
        "JobTitle": "Senior Engineer"
      }
    ]
  },
  {
    "DeptName": "Marketing Department",
    "Location": "Shanghai",
    "e": [
      {
        "FullName": "Li Si",
        "JobTitle": "Test Engineer"
      }
    ]
  },
  {
    "DeptName": "R&D Department",
    "Location": "Beijing",
    "e": [
      {
        "FullName": "Wang Wu",
        "JobTitle": "Marketing Manager"
      }
    ]
  }
]

IN BBF:

NOTE: ORDER and Incorrect JSON grouping

R&D Department	Beijing	Zhang San	Senior Engineer
Marketing Department	Shanghai	Li Si	Test Engineer
R&D Department	Beijing	Wang Wu	Marketing Manager
[
  {
    "DeptName": "Marketing Department",
    "Location": "Shanghai",
    "e": [
      {
        "FullName": "Li Si",
        "JobTitle": "Test Engineer"
      }
    ]
  },
  {
    "DeptName": "R&D Department",
    "Location": "Beijing",
    "e": [
      {
        "FullName": "Zhang San",
        "JobTitle": "Senior Engineer"
      },
      {
        "FullName": "Wang Wu",
        "JobTitle": "Marketing Manager"
      }
    ]
  }
]

Version

BABEL_5_X_DEV (Default)

Extension

None

Which flavor of Linux are you using when you see the bug?

No response

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions