Skip to content

Commit

Permalink
fix SQL generation if more than one aggregate order_by items present, f…
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshkky committed Oct 3, 2019
1 parent 5893cf4 commit ced467e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 45 deletions.
8 changes: 6 additions & 2 deletions server/src-lib/Hasura/RQL/DML/Select/Internal.hs
Expand Up @@ -165,8 +165,9 @@ mkBaseTableColAls :: Iden -> PGCol -> Iden
mkBaseTableColAls pfx pgCol =
pfx <> Iden ".pg." <> toIden pgCol

ordByFldName :: FieldName
ordByFldName = FieldName "order_by"
mkOrderByFieldName :: RelName -> FieldName
mkOrderByFieldName relName =
FieldName $ relNameToTxt relName <> "." <> "order_by"

-- posttgres ignores anything beyond 63 chars for an iden
-- in this case, we'll need to use json_build_object function
Expand Down Expand Up @@ -293,6 +294,7 @@ processAnnOrderByCol pfx parAls arrRelCtx strfyNum = \case
-- "pfx.or.relname"."pfx.ob.or.relname.rest" AS "pfx.ob.or.relname.rest"
AOCObj (RelInfo rn _ colMapping relTab _) relFltr rest ->
let relPfx = mkObjRelTableAls pfx rn
ordByFldName = mkOrderByFieldName rn
((nesAls, nesCol), ordByNode) =
processAnnOrderByCol relPfx ordByFldName emptyArrRelCtx strfyNum rest
(objNodeM, arrNodeM) = case ordByNode of
Expand Down Expand Up @@ -403,6 +405,7 @@ mkArrNodeInfo pfx parAls (ArrRelCtx arrFlds obRels) = \case
similarFlds = getSimilarAggFlds rn tabArgs $ delete aggFld
similarFldNames = map fst similarFlds
similarOrdByFound = rn `elem` obRels && tabArgs == noTableArgs
ordByFldName = mkOrderByFieldName rn
extraOrdByFlds = bool [] [ordByFldName] similarOrdByFound
sortedFlds = sort $ fld : (similarFldNames <> extraOrdByFlds)
alias = S.Alias $ mkUniqArrRelAls parAls sortedFlds
Expand All @@ -411,6 +414,7 @@ mkArrNodeInfo pfx parAls (ArrRelCtx arrFlds obRels) = \case
subQueryRequired similarFlds similarOrdByFound
ANIAggOrdBy rn ->
let similarFlds = map fst $ getSimilarAggFlds rn noTableArgs id
ordByFldName = mkOrderByFieldName rn
sortedFlds = sort $ ordByFldName:similarFlds
alias = S.Alias $ mkUniqArrRelAls parAls sortedFlds
prefix = mkArrRelTableAls pfx parAls sortedFlds
Expand Down
@@ -0,0 +1,32 @@
description: Fetch Albums order by their tracks and tags aggregate fields
url: /v1/graphql
status: 200
response:
data:
Album:
- album_id: 1
- album_id: 2
query:
query: |
query {
Album(
order_by: [
{
Tracks_aggregate: {
max: {
track_id: asc
}
}
},
{
Tags_aggregate:{
max: {
id:asc
}
}
}
]
){
album_id
}
}
33 changes: 31 additions & 2 deletions server/tests-py/queries/graphql_query/order_by/setup.yaml
Expand Up @@ -20,7 +20,7 @@ args:
args:
sql: |
create table author(
id serial primary key,
id serial primary key,
name text unique,
contact_id INTEGER REFERENCES contact(id)
);
Expand Down Expand Up @@ -86,7 +86,7 @@ args:
insert into author (name, contact_id)
values
('Author 1', 2),
('Author 2', 1)
('Author 2', 1)
- type: run_sql
args:
Expand Down Expand Up @@ -177,6 +177,35 @@ args:
( 6, 'Good One', 2, 346208, 6732987) ,
( 7, 'Mistress', 2, 420985, 7521946)
# Create Tag table
- type: run_sql
args:
sql: |
CREATE table "Tag" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
album_id INTEGER NOT NULL REFERENCES "Album"(album_id)
);
INSERT INTO "Tag" (name, album_id)
VALUES
( 'Rock', 1),
( 'Folk', 1),
( 'Hip Hop', 2);
- type: track_table
args:
name: Tag
schema: public

- type: create_array_relationship
args:
table: Album
name: Tags
using:
foreign_key_constraint_on:
table: Tag
column: album_id

# Create employee table
- type: run_sql
args:
Expand Down
49 changes: 8 additions & 41 deletions server/tests-py/queries/graphql_query/order_by/teardown.yaml
@@ -1,46 +1,13 @@
type: bulk
args:
#Drop relationship first
- type: drop_relationship
args:
relationship: articles
table:
schema: public
name: author

- type: run_sql
args:
sql: |
drop table article
- type: run_sql
args:
sql: |
drop table author
- type: run_sql
args:
sql: |
drop table contact
- type: drop_relationship
args:
relationship: Tracks
table:
schema: public
name: Album

- type: run_sql
args:
sql: |
drop table "Track"
- type: run_sql
args:
sql: |
drop table "Album"
- type: run_sql
args:
sql: |
drop table employee
DROP TABLE article;
DROP TABLE author;
DROP TABLE contact;
DROP TABLE "Track";
DROP TABLE "Tag";
DROP TABLE "Album";
DROP TABLE employee;
cascade: true
3 changes: 3 additions & 0 deletions server/tests-py/test_graphql_queries.py
Expand Up @@ -417,6 +417,9 @@ def test_employee_distinct_department_order_by_salary_asc(self, hge_ctx, transpo
def test_employee_distinct_fail(self, hge_ctx, transport):
check_query_f(hge_ctx, self.dir() + '/employee_distinct_fail.yaml', transport)

def test_album_order_by_tracks_tags(self, hge_ctx, transport):
check_query_f(hge_ctx, self.dir() + '/album_order_by_tracks_tags.yaml', transport)

@classmethod
def dir(cls):
return 'queries/graphql_query/order_by'
Expand Down

0 comments on commit ced467e

Please sign in to comment.