Skip to content

Commit 8a77386

Browse files
Antoine Leblancpaf31
authored andcommitted
server: IR for DB-DB joins
### Description This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch. To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join. Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline. The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order: - type declarations - instance declarations - type aliases - constructor functions - traverse functions hasura/graphql-engine-mono#1580 Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com> GitOrigin-RevId: bbdcb41
1 parent 456caa5 commit 8a77386

43 files changed

Lines changed: 1035 additions & 888 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎server/src-lib/Hasura/Backends/BigQuery/FromIr.hs‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ data Error
3939
= FromTypeUnsupported (Ir.SelectFromG 'BigQuery Expression)
4040
| NoOrderSpecifiedInOrderBy
4141
| MalformedAgg
42-
| FieldTypeUnsupportedForNow (Ir.AnnFieldG 'BigQuery Expression)
43-
| AggTypeUnsupportedForNow (Ir.TableAggregateFieldG 'BigQuery Expression)
44-
| NodesUnsupportedForNow (Ir.TableAggregateFieldG 'BigQuery Expression)
42+
| FieldTypeUnsupportedForNow (Ir.AnnFieldG 'BigQuery (Const Void) Expression)
43+
| AggTypeUnsupportedForNow (Ir.TableAggregateFieldG 'BigQuery (Const Void) Expression)
44+
| NodesUnsupportedForNow (Ir.TableAggregateFieldG 'BigQuery (Const Void) Expression)
4545
| NoProjectionFields
4646
| NoAggregatesMustBeABug
47-
| UnsupportedArraySelect (Ir.ArraySelectG 'BigQuery Expression)
47+
| UnsupportedArraySelect (Ir.ArraySelectG 'BigQuery (Const Void) Expression)
4848
| UnsupportedOpExpG (Ir.OpExpG 'BigQuery Expression)
4949
| UnsupportedSQLExp Expression
5050
| UnsupportedDistinctOn
@@ -110,7 +110,7 @@ runFromIr fromIr = evalStateT (unFromIr fromIr) mempty
110110
-- single object or an array.
111111
mkSQLSelect ::
112112
Rql.JsonAggSelect
113-
-> Ir.AnnSelectG 'BigQuery (Ir.AnnFieldsG 'BigQuery Expression) Expression
113+
-> Ir.AnnSelectG 'BigQuery (Const Void) (Ir.AnnFieldsG 'BigQuery (Const Void) Expression) Expression
114114
-> FromIr BigQuery.Select
115115
mkSQLSelect jsonAggSelect annSimpleSel = do
116116
select <- fromSelectRows annSimpleSel
@@ -123,7 +123,7 @@ mkSQLSelect jsonAggSelect annSimpleSel = do
123123
})
124124

125125
-- | Convert from the IR database query into a select.
126-
fromRootField :: Ir.QueryDB 'BigQuery Expression -> FromIr Select
126+
fromRootField :: Ir.QueryDB 'BigQuery (Const Void) Expression -> FromIr Select
127127
fromRootField =
128128
\case
129129
(Ir.QDBSingleRow s) -> mkSQLSelect Rql.JASSingleObject s
@@ -134,7 +134,7 @@ fromRootField =
134134
--------------------------------------------------------------------------------
135135
-- Top-level exported functions
136136

137-
fromSelectRows :: Ir.AnnSelectG 'BigQuery (Ir.AnnFieldsG 'BigQuery Expression) Expression -> FromIr BigQuery.Select
137+
fromSelectRows :: Ir.AnnSelectG 'BigQuery (Const Void) (Ir.AnnFieldsG 'BigQuery (Const Void) Expression) Expression -> FromIr BigQuery.Select
138138
fromSelectRows annSelectG = do
139139
selectFrom <-
140140
case from of
@@ -186,7 +186,7 @@ fromSelectRows annSelectG = do
186186
else LeaveNumbersAlone
187187

188188
fromSelectAggregate ::
189-
Ir.AnnSelectG 'BigQuery [(Rql.FieldName, Ir.TableAggregateFieldG 'BigQuery Expression)] Expression
189+
Ir.AnnSelectG 'BigQuery (Const Void) [(Rql.FieldName, Ir.TableAggregateFieldG 'BigQuery (Const Void) Expression)] Expression
190190
-> FromIr BigQuery.Select
191191
fromSelectAggregate annSelectG = do
192192
selectFrom <-
@@ -592,7 +592,7 @@ fromTableAggregateFieldG ::
592592
Args
593593
-> Top
594594
-> StringifyNumbers
595-
-> (Rql.FieldName, Ir.TableAggregateFieldG 'BigQuery Expression)
595+
-> (Rql.FieldName, Ir.TableAggregateFieldG 'BigQuery (Const Void) Expression)
596596
-> ReaderT EntityAlias FromIr FieldSource
597597
fromTableAggregateFieldG args permissionBasedTop stringifyNumbers (Rql.FieldName name, field) =
598598
case field of
@@ -616,7 +616,7 @@ fromTableAggregateFieldG args permissionBasedTop stringifyNumbers (Rql.FieldName
616616
{ aliasedThing = BigQuery.ValueExpression (StringValue text)
617617
, aliasedAlias = name
618618
})
619-
Ir.TAFNodes _ (fields :: [(Rql.FieldName, Ir.AnnFieldG 'BigQuery Expression)]) -> do
619+
Ir.TAFNodes _ (fields :: [(Rql.FieldName, Ir.AnnFieldG 'BigQuery (Const Void) Expression)]) -> do
620620
fieldSources <-
621621
traverse
622622
(fromAnnFieldsG (argsExistingJoins args) stringifyNumbers)
@@ -663,7 +663,7 @@ fromAggregateField aggregateField =
663663
fromAnnFieldsG ::
664664
Map TableName EntityAlias
665665
-> StringifyNumbers
666-
-> (Rql.FieldName, Ir.AnnFieldG 'BigQuery Expression)
666+
-> (Rql.FieldName, Ir.AnnFieldG 'BigQuery (Const Void) Expression)
667667
-> ReaderT EntityAlias FromIr FieldSource
668668
fromAnnFieldsG existingJoins stringifyNumbers (Rql.FieldName name, field) =
669669
case field of
@@ -698,6 +698,9 @@ fromAnnFieldsG existingJoins stringifyNumbers (Rql.FieldName name, field) =
698698
{ aliasedThing = BigQuery.ValueExpression (StringValue "null: remote field selected")
699699
, aliasedAlias = name
700700
})
701+
-- TODO: implement this
702+
Ir.AFDBRemote _ -> error "FIXME"
703+
701704

702705
-- | Here is where we project a field as a column expression. If
703706
-- number stringification is on, then we wrap it in a
@@ -840,7 +843,7 @@ fieldSourceJoin =
840843
-- See also 'fromArrayRelationSelectG' for similar example.
841844
fromObjectRelationSelectG ::
842845
Map TableName EntityAlias
843-
-> Ir.ObjectRelationSelectG 'BigQuery Expression
846+
-> Ir.ObjectRelationSelectG 'BigQuery (Const Void) Expression
844847
-> ReaderT EntityAlias FromIr Join
845848
-- We're not using existingJoins at the moment, which was used to
846849
-- avoid re-joining on the same table twice.
@@ -900,13 +903,13 @@ fromObjectRelationSelectG _existingJoins annRelationSelectG = do
900903
, joinExtractPath = Nothing
901904
}
902905
where
903-
Ir.AnnObjectSelectG { _aosFields = fields :: Ir.AnnFieldsG 'BigQuery Expression
906+
Ir.AnnObjectSelectG { _aosFields = fields :: Ir.AnnFieldsG 'BigQuery (Const Void) Expression
904907
, _aosTableFrom = tableFrom :: TableName
905908
, _aosTableFilter = tableFilter :: Ir.AnnBoolExp 'BigQuery Expression
906909
} = annObjectSelectG
907910
Ir.AnnRelationSelectG { aarRelationshipName
908911
, aarColumnMapping = mapping :: HashMap ColumnName ColumnName
909-
, aarAnnSelect = annObjectSelectG :: Ir.AnnObjectSelectG 'BigQuery Expression
912+
, aarAnnSelect = annObjectSelectG :: Ir.AnnObjectSelectG 'BigQuery (Const Void) Expression
910913
} = annRelationSelectG
911914

912915
-- We're not using existingJoins at the moment, which was used to
@@ -920,7 +923,7 @@ _lookupTableFrom existingJoins tableFrom = do
920923
Just entityAlias -> pure (Left entityAlias)
921924
Nothing -> fmap Right (fromQualifiedTable tableFrom)
922925

923-
fromArraySelectG :: Ir.ArraySelectG 'BigQuery Expression -> ReaderT EntityAlias FromIr Join
926+
fromArraySelectG :: Ir.ArraySelectG 'BigQuery (Const Void) Expression -> ReaderT EntityAlias FromIr Join
924927
fromArraySelectG =
925928
\case
926929
Ir.ASSimple arrayRelationSelectG ->
@@ -935,7 +938,7 @@ fromArraySelectG =
935938
--
936939
-- See also 'fromArrayRelationSelectG' for similar example.
937940
fromArrayAggregateSelectG ::
938-
Ir.AnnRelationSelectG 'BigQuery (Ir.AnnAggregateSelectG 'BigQuery Expression)
941+
Ir.AnnRelationSelectG 'BigQuery (Ir.AnnAggregateSelectG 'BigQuery (Const Void) Expression)
939942
-> ReaderT EntityAlias FromIr Join
940943
fromArrayAggregateSelectG annRelationSelectG = do
941944
joinFieldName <- lift (fromRelName aarRelationshipName)
@@ -1040,7 +1043,7 @@ fromArrayAggregateSelectG annRelationSelectG = do
10401043
-- ^ Ordering for the artist table should appear here.
10411044

10421045
fromArrayRelationSelectG ::
1043-
Ir.ArrayRelationSelectG 'BigQuery Expression
1046+
Ir.ArrayRelationSelectG 'BigQuery (Const Void) Expression
10441047
-> ReaderT EntityAlias FromIr Join
10451048
fromArrayRelationSelectG annRelationSelectG = do
10461049
select <- lift (fromSelectRows annSelectG) -- Take the original select.
@@ -1328,7 +1331,7 @@ fromAlias :: From -> EntityAlias
13281331
fromAlias (FromQualifiedTable Aliased {aliasedAlias}) = EntityAlias aliasedAlias
13291332
fromAlias (FromSelect Aliased {aliasedAlias}) = EntityAlias aliasedAlias
13301333

1331-
fieldTextNames :: Ir.AnnFieldsG 'BigQuery Expression -> [Text]
1334+
fieldTextNames :: Ir.AnnFieldsG 'BigQuery (Const Void) Expression -> [Text]
13321335
fieldTextNames = fmap (\(Rql.FieldName name, _) -> name)
13331336

13341337
unEntityAlias :: EntityAlias -> Text

‎server/src-lib/Hasura/Backends/BigQuery/Instances/Execute.hs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bqDBQueryPlan
5050
=> UserInfo
5151
-> SourceName
5252
-> SourceConfig 'BigQuery
53-
-> QueryDB 'BigQuery (UnpreparedValue 'BigQuery)
53+
-> QueryDB 'BigQuery (Const Void) (UnpreparedValue 'BigQuery)
5454
-> m (DBStepInfo 'BigQuery)
5555
bqDBQueryPlan userInfo sourceName sourceConfig qrf = do
5656
select <- planNoPlan userInfo qrf
@@ -113,7 +113,7 @@ bqDBMutationPlan
113113
-> Bool
114114
-> SourceName
115115
-> SourceConfig 'BigQuery
116-
-> MutationDB 'BigQuery (UnpreparedValue 'BigQuery)
116+
-> MutationDB 'BigQuery (Const Void) (UnpreparedValue 'BigQuery)
117117
-> m (DBStepInfo 'BigQuery)
118118
bqDBMutationPlan _userInfo _stringifyNum _sourceName _sourceConfig _mrf =
119119
throw500 "mutations are not supported in BigQuery; this should be unreachable"
@@ -127,7 +127,7 @@ bqDBQueryExplain
127127
-> UserInfo
128128
-> SourceName
129129
-> SourceConfig 'BigQuery
130-
-> QueryDB 'BigQuery (UnpreparedValue 'BigQuery)
130+
-> QueryDB 'BigQuery (Const Void) (UnpreparedValue 'BigQuery)
131131
-> m (AB.AnyBackend DBStepInfo)
132132
bqDBQueryExplain fieldName userInfo sourceName sourceConfig qrf = do
133133
actionsForest <- planToForest userInfo qrf

‎server/src-lib/Hasura/Backends/BigQuery/Instances/Schema.hs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bqBuildTableRelayQueryFields
7777
-> G.Name
7878
-> NESeq (ColumnInfo 'BigQuery)
7979
-> SelPermInfo 'BigQuery
80-
-> m [FieldParser n (QueryRootField UnpreparedValue)]
80+
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
8181
bqBuildTableRelayQueryFields _sourceName _sourceInfo _tableName _tableInfo _gqlName _pkeyColumns _selPerms =
8282
pure []
8383

@@ -91,7 +91,7 @@ bqBuildTableInsertMutationFields
9191
-> InsPermInfo 'BigQuery
9292
-> Maybe (SelPermInfo 'BigQuery)
9393
-> Maybe (UpdPermInfo 'BigQuery)
94-
-> m [FieldParser n (MutationRootField UnpreparedValue)]
94+
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
9595
bqBuildTableInsertMutationFields _sourceName _sourceInfo _tableName _tableInfo _gqlName _insPerms _selPerms _updPerms =
9696
pure []
9797

@@ -104,7 +104,7 @@ bqBuildTableUpdateMutationFields
104104
-> G.Name
105105
-> UpdPermInfo 'BigQuery
106106
-> Maybe (SelPermInfo 'BigQuery)
107-
-> m [FieldParser n (MutationRootField UnpreparedValue)]
107+
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
108108
bqBuildTableUpdateMutationFields _sourceName _sourceInfo _tableName _tableInfo _gqlName _updPerns _selPerms =
109109
pure []
110110

@@ -117,7 +117,7 @@ bqBuildTableDeleteMutationFields
117117
-> G.Name
118118
-> DelPermInfo 'BigQuery
119119
-> Maybe (SelPermInfo 'BigQuery)
120-
-> m [FieldParser n (MutationRootField UnpreparedValue)]
120+
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
121121
bqBuildTableDeleteMutationFields _sourceName _sourceInfo _tableName _tableInfo _gqlName _delPerns _selPerms =
122122
pure []
123123

@@ -129,7 +129,7 @@ bqBuildFunctionQueryFields
129129
-> FunctionInfo 'BigQuery
130130
-> TableName 'BigQuery
131131
-> SelPermInfo 'BigQuery
132-
-> m [FieldParser n (QueryRootField UnpreparedValue)]
132+
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
133133
bqBuildFunctionQueryFields _ _ _ _ _ _ =
134134
pure []
135135

@@ -142,7 +142,7 @@ bqBuildFunctionRelayQueryFields
142142
-> TableName 'BigQuery
143143
-> NESeq (ColumnInfo 'BigQuery)
144144
-> SelPermInfo 'BigQuery
145-
-> m [FieldParser n (QueryRootField UnpreparedValue)]
145+
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
146146
bqBuildFunctionRelayQueryFields _sourceName _sourceInfo _functionName _functionInfo _tableName _pkeyColumns _selPerms =
147147
pure []
148148

@@ -154,7 +154,7 @@ bqBuildFunctionMutationFields
154154
-> FunctionInfo 'BigQuery
155155
-> TableName 'BigQuery
156156
-> SelPermInfo 'BigQuery
157-
-> m [FieldParser n (MutationRootField UnpreparedValue)]
157+
-> m [FieldParser n (MutationRootField UnpreparedValue UnpreparedValue)]
158158
bqBuildFunctionMutationFields _ _ _ _ _ _ =
159159
pure []
160160

‎server/src-lib/Hasura/Backends/BigQuery/Instances/Types.hs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Hasura.Prelude
77
import qualified Language.GraphQL.Draft.Syntax as G
88

99
import Data.Aeson
10-
import Data.Functor.Const
1110

1211
import qualified Hasura.Backends.BigQuery.Source as BigQuery
1312
import qualified Hasura.Backends.BigQuery.Types as BigQuery

‎server/src-lib/Hasura/Backends/BigQuery/Plan.hs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ import qualified Hasura.Base.Error as E
1919
import qualified Hasura.GraphQL.Parser as GraphQL
2020
import qualified Hasura.RQL.Types.Column as RQL
2121

22-
import Hasura.Backends.BigQuery.FromIr as BigQuery
23-
import Hasura.Backends.BigQuery.Types as BigQuery
22+
import Hasura.Backends.BigQuery.FromIr
23+
import Hasura.Backends.BigQuery.Types
2424
import Hasura.RQL.IR
2525
import Hasura.SQL.Backend
2626
import Hasura.SQL.Types
2727
import Hasura.Session
2828

2929

30-
-- --------------------------------------------------------------------------------
31-
-- -- Top-level planner
30+
--------------------------------------------------------------------------------
31+
-- Top-level planner
3232

3333
planToForest ::
3434
MonadError E.QErr m
3535
=> UserInfo
36-
-> QueryDB 'BigQuery (GraphQL.UnpreparedValue 'BigQuery)
36+
-> QueryDB 'BigQuery (Const Void) (GraphQL.UnpreparedValue 'BigQuery)
3737
-> m (Forest DataLoader.PlannedAction)
3838
planToForest userInfo qrf = do
3939
select <- planNoPlan userInfo qrf
@@ -46,13 +46,14 @@ planToForest userInfo qrf = do
4646
planNoPlan ::
4747
MonadError E.QErr m
4848
=> UserInfo
49-
-> QueryDB 'BigQuery (GraphQL.UnpreparedValue 'BigQuery)
49+
-> QueryDB 'BigQuery (Const Void) (GraphQL.UnpreparedValue 'BigQuery)
5050
-> m Select
5151
planNoPlan userInfo queryDB = do
5252
rootField <- traverseQueryDB (prepareValueNoPlan (_uiSession userInfo)) queryDB
53-
runValidate (BigQuery.runFromIr (BigQuery.fromRootField rootField))
53+
runValidate (runFromIr (fromRootField rootField))
5454
`onLeft` (E.throw400 E.NotSupported . (tshow :: NonEmpty Error -> Text))
5555

56+
5657
--------------------------------------------------------------------------------
5758
-- Resolving values
5859

@@ -62,7 +63,7 @@ prepareValueNoPlan ::
6263
(MonadError E.QErr m)
6364
=> SessionVariables
6465
-> GraphQL.UnpreparedValue 'BigQuery
65-
-> m BigQuery.Expression
66+
-> m Expression
6667
prepareValueNoPlan sessionVariables =
6768
\case
6869
GraphQL.UVLiteral x -> pure x

0 commit comments

Comments
 (0)