Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 56d8865

Browse files
chewseleneSelene Chew
andauthored
reorganize query planning and execution (#1011)
* reorganize query planning and execution * rename to query_planning * update mypy.ini Co-authored-by: Selene Chew <[email protected]>
1 parent 1065f2c commit 56d8865

File tree

4 files changed

+67
-60
lines changed

4 files changed

+67
-60
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Copyright 2021-present Kensho Technologies, LLC.

graphql_compiler/schema_transformation/make_query_plan.py renamed to graphql_compiler/query_planning/make_query_plan.py

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from ..ast_manipulation import get_only_query_definition
2222
from ..exceptions import GraphQLValidationError
2323
from ..schema import FilterDirective, OutputDirective
24-
from .split_query import AstType, SubQueryNode
24+
from ..schema_transformation.split_query import AstType, SubQueryNode
2525

2626

2727
@dataclass
@@ -66,47 +66,6 @@ class QueryPlanDescriptor:
6666
output_join_descriptors: List[OutputJoinDescriptor]
6767

6868

69-
def make_query_plan(
70-
root_sub_query_node: SubQueryNode, intermediate_output_names: FrozenSet[str]
71-
) -> QueryPlanDescriptor:
72-
"""Return a QueryPlanDescriptor, whose query ASTs have @filters added.
73-
74-
For each parent of parent and child SubQueryNodes, a new @filter directive will be added
75-
in the child AST. It will be added on the field whose @output directive has the out_name
76-
equal to the child's out name as specified in the QueryConnection. The newly added @filter
77-
will be a 'in_collection' type filter, and the name of the local variable is guaranteed to
78-
be the same as the out_name of the @output on the parent.
79-
80-
ASTs contained in the input node and its children nodes will not be modified.
81-
82-
Args:
83-
root_sub_query_node: representing the base of a query split into pieces
84-
that we want to turn into a query plan.
85-
intermediate_output_names: names of outputs to be removed at the end.
86-
87-
Returns:
88-
QueryPlanDescriptor containing a tree of SubQueryPlans that wrap around each individual
89-
query AST, the set of intermediate output names that are to be removed at the end, and
90-
information on which outputs are to be connect to which in what manner.
91-
"""
92-
output_join_descriptors: List[OutputJoinDescriptor] = []
93-
94-
root_sub_query_plan = SubQueryPlan(
95-
query_ast=root_sub_query_node.query_ast,
96-
schema_id=root_sub_query_node.schema_id,
97-
parent_query_plan=None,
98-
child_query_plans=[],
99-
)
100-
101-
_make_query_plan_recursive(root_sub_query_node, root_sub_query_plan, output_join_descriptors)
102-
103-
return QueryPlanDescriptor(
104-
root_sub_query_plan=root_sub_query_plan,
105-
intermediate_output_names=intermediate_output_names,
106-
output_join_descriptors=output_join_descriptors,
107-
)
108-
109-
11069
def _make_query_plan_recursive(
11170
sub_query_node: SubQueryNode,
11271
sub_query_plan: SubQueryPlan,
@@ -291,6 +250,66 @@ def _get_in_collection_filter_directive(input_filter_name: str) -> DirectiveNode
291250
)
292251

293252

253+
def _get_plan_and_depth_in_dfs_order(query_plan: SubQueryPlan) -> List[Tuple[SubQueryPlan, int]]:
254+
"""Return a list of topologically sorted (query plan, depth) tuples."""
255+
256+
def _get_plan_and_depth_in_dfs_order_helper(query_plan, depth):
257+
plan_and_depth_in_dfs_order = [(query_plan, depth)]
258+
for child_query_plan in query_plan.child_query_plans:
259+
plan_and_depth_in_dfs_order.extend(
260+
_get_plan_and_depth_in_dfs_order_helper(child_query_plan, depth + 1)
261+
)
262+
return plan_and_depth_in_dfs_order
263+
264+
return _get_plan_and_depth_in_dfs_order_helper(query_plan, 0)
265+
266+
267+
######
268+
# Public API
269+
######
270+
271+
272+
def make_query_plan(
273+
root_sub_query_node: SubQueryNode, intermediate_output_names: FrozenSet[str]
274+
) -> QueryPlanDescriptor:
275+
"""Return a QueryPlanDescriptor, whose query ASTs have @filters added.
276+
277+
For each parent of parent and child SubQueryNodes, a new @filter directive will be added
278+
in the child AST. It will be added on the field whose @output directive has the out_name
279+
equal to the child's out name as specified in the QueryConnection. The newly added @filter
280+
will be a 'in_collection' type filter, and the name of the local variable is guaranteed to
281+
be the same as the out_name of the @output on the parent.
282+
283+
ASTs contained in the input node and its children nodes will not be modified.
284+
285+
Args:
286+
root_sub_query_node: representing the base of a query split into pieces
287+
that we want to turn into a query plan.
288+
intermediate_output_names: names of outputs to be removed at the end.
289+
290+
Returns:
291+
QueryPlanDescriptor containing a tree of SubQueryPlans that wrap around each individual
292+
query AST, the set of intermediate output names that are to be removed at the end, and
293+
information on which outputs are to be connect to which in what manner.
294+
"""
295+
output_join_descriptors: List[OutputJoinDescriptor] = []
296+
297+
root_sub_query_plan = SubQueryPlan(
298+
query_ast=root_sub_query_node.query_ast,
299+
schema_id=root_sub_query_node.schema_id,
300+
parent_query_plan=None,
301+
child_query_plans=[],
302+
)
303+
304+
_make_query_plan_recursive(root_sub_query_node, root_sub_query_plan, output_join_descriptors)
305+
306+
return QueryPlanDescriptor(
307+
root_sub_query_plan=root_sub_query_plan,
308+
intermediate_output_names=intermediate_output_names,
309+
output_join_descriptors=output_join_descriptors,
310+
)
311+
312+
294313
def print_query_plan(query_plan_descriptor: QueryPlanDescriptor, indentation_depth: int = 4) -> str:
295314
"""Return a string describing query plan."""
296315
query_plan_strings = [""]
@@ -311,17 +330,3 @@ def print_query_plan(query_plan_descriptor: QueryPlanDescriptor, indentation_dep
311330
query_plan_strings.append(str(query_plan_descriptor.intermediate_output_names) + "\n")
312331

313332
return "".join(query_plan_strings)
314-
315-
316-
def _get_plan_and_depth_in_dfs_order(query_plan: SubQueryPlan) -> List[Tuple[SubQueryPlan, int]]:
317-
"""Return a list of topologically sorted (query plan, depth) tuples."""
318-
319-
def _get_plan_and_depth_in_dfs_order_helper(query_plan, depth):
320-
plan_and_depth_in_dfs_order = [(query_plan, depth)]
321-
for child_query_plan in query_plan.child_query_plans:
322-
plan_and_depth_in_dfs_order.extend(
323-
_get_plan_and_depth_in_dfs_order_helper(child_query_plan, depth + 1)
324-
)
325-
return plan_and_depth_in_dfs_order
326-
327-
return _get_plan_and_depth_in_dfs_order_helper(query_plan, 0)

graphql_compiler/tests/schema_transformation_tests/test_make_query_plan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from graphql import parse, print_ast
66

7-
from ...schema_transformation.make_query_plan import make_query_plan
7+
from ...query_planning.make_query_plan import make_query_plan
88
from ...schema_transformation.split_query import split_query
99
from .example_schema import basic_merged_schema
1010

mypy.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ disallow_untyped_calls = False
160160
[mypy-graphql_compiler.query_pagination.query_parameterizer.*]
161161
disallow_untyped_calls = False
162162

163+
[mypy-graphql_compiler.query_planning.*]
164+
disallow_untyped_calls = False
165+
disallow_untyped_defs = False
166+
163167
[mypy-graphql_compiler.schema_generation.graphql_schema.*]
164168
check_untyped_defs = False
165169
disallow_incomplete_defs = False
@@ -189,9 +193,6 @@ check_untyped_defs = False
189193
[mypy-graphql_compiler.schema_transformation.*]
190194
disallow_untyped_calls = False
191195

192-
[mypy-graphql_compiler.schema_transformation.make_query_plan.*]
193-
disallow_untyped_defs = False
194-
195196
[mypy-graphql_compiler.schema_transformation.split_query.*]
196197
disallow_incomplete_defs = False
197198
disallow_untyped_defs = False

0 commit comments

Comments
 (0)