Skip to content

Commit

Permalink
[server] revert #2078 "server/postgres: optimize SQL query generation…
Browse files Browse the repository at this point in the history
  • Loading branch information
evertedsphere authored and hasura-bot committed Aug 30, 2021
1 parent 8ca110b commit 4887f70
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 335 deletions.
195 changes: 55 additions & 140 deletions server/src-lib/Hasura/Backends/Postgres/Translate/Select.hs

Large diffs are not rendered by default.

70 changes: 8 additions & 62 deletions server/src-lib/Hasura/Backends/Postgres/Translate/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,20 @@ data SourcePrefixes
} deriving (Show, Eq, Generic)
instance Hashable SourcePrefixes

-- | Select portion of rows generated by the query using limit and offset
data SelectSlicing
= SelectSlicing
{ _ssLimit :: !(Maybe Int)
, _ssOffset :: !(Maybe Int64)
} deriving (Show, Eq, Generic)
instance Hashable SelectSlicing

-- | Sorting with -- Note [Optimizing queries using limit/offset])
data SelectSorting
= NoSorting !(Maybe PG.DistinctExpr)
| SortAtBase !PG.OrderByExp !(Maybe PG.DistinctExpr)
-- ^ Apply sorting at base table selection. Sorting only via table columns
| SortAtNode !PG.OrderByExp !(Maybe PG.DistinctExpr)
-- ^ Apply sorting at outer selection. Sorting involves atleast one non-column order by item
deriving (Show, Eq, Generic)
instance Hashable SelectSorting

data SortingAndSlicing
= SortingAndSlicing
{ _sasSorting :: !SelectSorting
, _sasSlicing :: !SelectSlicing
} deriving (Show, Eq, Generic)
instance Hashable SortingAndSlicing

data SelectSource
= SelectSource
{ _ssPrefix :: !PG.Identifier
, _ssFrom :: !PG.FromItem
, _ssWhere :: !PG.BoolExp
, _ssSortingAndSlicing :: !SortingAndSlicing
{ _ssPrefix :: !PG.Identifier
, _ssFrom :: !PG.FromItem
, _ssDistinct :: !(Maybe PG.DistinctExpr)
, _ssWhere :: !PG.BoolExp
, _ssOrderBy :: !(Maybe PG.OrderByExp)
, _ssLimit :: !(Maybe Int)
, _ssOffset :: !(Maybe Int64)
} deriving (Generic)
instance Hashable SelectSource
deriving instance Show SelectSource
deriving instance Eq SelectSource

noSortingAndSlicing :: SortingAndSlicing
noSortingAndSlicing =
SortingAndSlicing (NoSorting Nothing) noSlicing

noSlicing :: SelectSlicing
noSlicing = SelectSlicing Nothing Nothing

orderByForJsonAgg :: SelectSource -> Maybe PG.OrderByExp
orderByForJsonAgg SelectSource{..} =
case (_sasSorting _ssSortingAndSlicing) of
NoSorting{} -> Nothing
SortAtBase{} -> Nothing
SortAtNode orderBy _ -> Just orderBy

data ApplySortingAndSlicing
= ApplySortingAndSlicing
{ _applyAtBase :: !(Maybe PG.OrderByExp, SelectSlicing, Maybe PG.DistinctExpr)
, _applyAtNode :: !(Maybe PG.OrderByExp, SelectSlicing, Maybe PG.DistinctExpr)
}

applySortingAndSlicing :: SortingAndSlicing -> ApplySortingAndSlicing
applySortingAndSlicing SortingAndSlicing{..} =
case _sasSorting of
NoSorting distinctExp -> applyAtBase Nothing distinctExp
SortAtBase orderByExp distinctExp -> applyAtBase (Just orderByExp) distinctExp
SortAtNode orderByExp distinctExp -> applyAtNode orderByExp distinctExp
where
applyAtBase orderByExp distinctExp =
ApplySortingAndSlicing (orderByExp, _sasSlicing, distinctExp) (Nothing, noSlicing, Nothing)
applyAtNode orderByExp distinctExp =
ApplySortingAndSlicing (Nothing, noSlicing, Nothing) (Just orderByExp, _sasSlicing, distinctExp)

data SelectNode
= SelectNode
{ _snExtractors :: !(HM.HashMap PG.Alias PG.SQLExp)
Expand All @@ -113,7 +59,7 @@ instance Hashable ObjectSelectSource

objectSelectSourceToSelectSource :: ObjectSelectSource -> SelectSource
objectSelectSourceToSelectSource ObjectSelectSource{..} =
SelectSource _ossPrefix _ossFrom _ossWhere noSortingAndSlicing
SelectSource _ossPrefix _ossFrom Nothing _ossWhere Nothing Nothing Nothing

data ObjectRelationSource
= ObjectRelationSource
Expand Down
1 change: 0 additions & 1 deletion server/src-lib/Hasura/RQL/IR/Select.hs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,5 @@ insertFunctionArg argName idx value (FunctionArgsExp positional named) =
-- Lenses

$(makeLenses ''AnnSelectG)
$(makeLenses ''SelectArgsG)
$(makePrisms ''AnnFieldG)
$(makePrisms ''AnnotatedOrderByElement)
29 changes: 0 additions & 29 deletions server/tests-py/queries/explain/limit_orderby_column_query.yaml

This file was deleted.

This file was deleted.

25 changes: 0 additions & 25 deletions server/tests-py/queries/explain/limit_query.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions server/tests-py/queries/explain/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ args:
name TEXT NOT NULL,
age INTEGER
);
CREATE TABLE author(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE article(
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
author_id INTEGER REFERENCES author(id)
);
- type: track_table
args:
Expand All @@ -32,20 +22,3 @@ args:
columns: '*'
filter:
id: X-Hasura-User-Id

- type: track_table
args:
name: author
schema: public

- type: track_table
args:
name: article
schema: public

- type: create_object_relationship
args:
table: article
name: author
using:
foreign_key_constraint_on: author_id
2 changes: 0 additions & 2 deletions server/tests-py/queries/explain/teardown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ args:
cascade: true
sql: |
DROP TABLE "user";
DROP TABLE "article";
DROP TABLE "author";
13 changes: 1 addition & 12 deletions server/tests-py/test_graphql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ def dir(cls):
def test_unauth_role(self, hge_ctx, transport):
check_query_f(hge_ctx, self.dir() + '/unauthorized_role.yaml', transport, False)

@pytest.mark.parametrize("backend", ['postgres', 'mssql'])
@usefixtures('per_class_tests_db_state')
class TestGraphQLExplain:
@classmethod
Expand All @@ -1143,27 +1144,15 @@ def test_simple_query_as_user(self, hge_ctx, backend):
st_code, resp = hge_ctx.v1GraphqlExplain(q, {"x-hasura-role": "random_user"})
assert st_code == 400, resp

@pytest.mark.parametrize("backend", ['postgres', 'mssql'])
def test_simple_query_as_admin_with_user_role(self, hge_ctx, backend):
self.with_admin_secret(hge_ctx, self.dir() + hge_ctx.backend_suffix('/permissions_query') + ".yaml")

@pytest.mark.parametrize("backend", ['postgres', 'mssql'])
def test_simple_query(self, hge_ctx, backend):
self.with_admin_secret(hge_ctx, self.dir() + hge_ctx.backend_suffix('/simple_query') + ".yaml")

@pytest.mark.parametrize("backend", ['postgres', 'mssql'])
def test_permissions_query(self, hge_ctx, backend):
self.with_admin_secret(hge_ctx, self.dir() + hge_ctx.backend_suffix('/permissions_query') + ".yaml")

def test_limit_query(self, hge_ctx):
self.with_admin_secret(hge_ctx, self.dir() + '/limit_query.yaml')

def test_limit_orderby_column_query(self, hge_ctx):
self.with_admin_secret(hge_ctx, self.dir() + '/limit_orderby_column_query.yaml')

def test_limit_orderby_relationship_query(self, hge_ctx):
self.with_admin_secret(hge_ctx, self.dir() + '/limit_orderby_relationship_query.yaml')

def with_admin_secret(self, hge_ctx, f, hdrs=None, req_st=200):
conf = get_conf_f(f)
admin_secret = hge_ctx.hge_key
Expand Down

0 comments on commit 4887f70

Please sign in to comment.