hasura / hasura/graphql-engine

server/mssql: fix aggregate queries with nodes field having relationships in selection set

Open
#7,871 0 comments 0 reactions 0 assignees View on GitHub
k/bug
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: v2.0.10

### Environment

OSS

### Reproduction schema

Track tables and add relationships suggested by the console.

### What is the expected behaviour?

The following query should run successfully.
```graphql
query {
author_aggregate{
aggregate{
count
max{
id
}
}
nodes{
id
name
articles{
id
title
content
}
}
}
}
```

### Keywords
mssql aggregate nodes failing

### What is the current behaviour?

The query
```graphql
query {
author_aggregate{
aggregate{
count
max{
id
}
}
nodes{
id
name
articles{
id
title
content
}
}
}
}
```
is failing with following error
```json
{
"errors": [
{
"extensions": {
"internal": {
"tag": "unsuccessful_return_code",
"contents": [
"odbc_SQLExecDirectW",
-1,
"[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The multi-part identifier \"ar_articles1.json\" could not be bound.[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The multi-part identifier \"ar_articles1.json\" could not be bound."
]
},
"path": "$",
"code": "unexpected"
},
"message": "sql server exception"
}
]
}
```

### How to reproduce the issue?

1. Add the following sql server schema
```sql
-- author table
create table author(
id int identity not null primary key,
name text
);
-- insert data
INSERT INTO author (name)
VALUES
('Author 1'),
('Author 2')
;
-- article table
CREATE TABLE article (
id INT IDENTITY NOT NULL PRIMARY KEY,
title TEXT,
content TEXT,
author_id INTEGER REFERENCES author(id),
is_published BIT,
published_on TIMESTAMP
);

INSERT INTO article (title,content,author_id,is_published)
VALUES
(
'Article 1',
'Sample article content 1',
1,
0
),
(
'Article 2',
'Sample article content 2',
1,
1
),
(
'Article 3',
'Sample article content 3',
2,
1
)
;
```
2. Track tables
3. Add relationships suggested by the console

### Screenshots or Screencast

N/A

### Please provide any traces or logs that could help here.

N/A

### Any possible solutions?

Fix the SQL generation for nodes field.

### Can you identify the location in the source code where the problem exists?

https://github.com/hasura/graphql-engine/blob/4f6831d76ee084e1f7d45b14b83e972768c5483e/server/src-lib/Hasura/Backends/MSSQL/FromIr.hs#L179

### If the bug is confirmed, would you be willing to submit a PR?

Yes, of course :)

Contributor guide

Open the contributing guide

Research direction

Start in server/src-lib/Hasura/Backends/MSSQL/FromIr.hs around line 179, then reproduce the query with the author and article SQL Server tables and relationship. Trace the SQL generated for the aggregate nodes selection and ensure the query completes without the ar_articles1.json binding error.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, sql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.