signature stringlengths 8 3.44k | body stringlengths 0 1.41M | docstring stringlengths 1 122k | id stringlengths 5 17 |
|---|---|---|---|
def _validate_fold_has_outputs_or_count_filter(fold_scope_location, fold_has_count_filter, outputs): | <EOL>if fold_has_count_filter:<EOL><INDENT>return True<EOL><DEDENT>for output in six.itervalues(outputs):<EOL><INDENT>if output['<STR_LIT>'] == fold_scope_location:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(fold_scope_location))<EOL> | Ensure the @fold scope has at least one output, or filters on the size of the fold. | f12656:m9 |
def _compile_fragment_ast(schema, current_schema_type, ast, location, context): | query_metadata_table = context['<STR_LIT>']<EOL>coerces_to_type_name = ast.type_condition.name.value<EOL>coerces_to_type_obj = schema.get_type(coerces_to_type_name)<EOL>basic_blocks = []<EOL>is_same_type_as_scope = current_schema_type.is_same_type(coerces_to_type_obj)<EOL>equivalent_union_type = context['<STR_LIT>'].get(coerces_to_type_obj, None)<EOL>is_base_type_of_union = (<EOL>isinstance(current_schema_type, GraphQLUnionType) and<EOL>current_schema_type.is_same_type(equivalent_union_type)<EOL>)<EOL>if not (is_same_type_as_scope or is_base_type_of_union):<EOL><INDENT>query_metadata_table.record_coercion_at_location(location, coerces_to_type_obj)<EOL>basic_blocks.append(blocks.CoerceType({coerces_to_type_name}))<EOL><DEDENT>inner_basic_blocks = _compile_ast_node_to_ir(<EOL>schema, coerces_to_type_obj, ast, location, context)<EOL>basic_blocks.extend(inner_basic_blocks)<EOL>return basic_blocks<EOL> | Return a list of basic blocks corresponding to the inline fragment at this AST node.
Args:
schema: GraphQL schema object, obtained from the graphql library
current_schema_type: GraphQLType, the schema type at the current location
ast: GraphQL AST node, obtained from the graphql library.
location: Location object representing the current location in the query
context: dict, various per-compilation data (e.g. declared tags, whether the current block
is optional, etc.). May be mutated in-place in this function!
Returns:
list of basic blocks, the compiled output of the vertex AST node | f12656:m10 |
def _compile_ast_node_to_ir(schema, current_schema_type, ast, location, context): | basic_blocks = []<EOL>local_unique_directives = get_unique_directives(ast)<EOL>fields = _get_fields(ast)<EOL>vertex_fields, property_fields = fields<EOL>fragment = _get_inline_fragment(ast)<EOL>filter_operations = get_local_filter_directives(<EOL>ast, current_schema_type, vertex_fields)<EOL>fragment_exists = fragment is not None<EOL>fields_exist = vertex_fields or property_fields<EOL>if fragment_exists and fields_exist:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL><DEDENT>if location.field is not None: <EOL><INDENT>if fragment_exists:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(location, fragment))<EOL><DEDENT>if len(property_fields) > <NUM_LIT:0>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(location, property_fields))<EOL><DEDENT><DEDENT>for filter_operation_info in filter_operations:<EOL><INDENT>filter_block = process_filter_directive(filter_operation_info, location, context)<EOL>if isinstance(location, FoldScopeLocation) and location.field == COUNT_META_FIELD_NAME:<EOL><INDENT>set_fold_innermost_scope(context)<EOL>expected_field = expressions.LocalField(COUNT_META_FIELD_NAME)<EOL>replacement_field = expressions.FoldedContextField(location, GraphQLInt)<EOL>visitor_fn = expressions.make_replacement_visitor(expected_field, replacement_field)<EOL>filter_block = filter_block.visit_and_update_expressions(visitor_fn)<EOL>visitor_fn = expressions.make_type_replacement_visitor(<EOL>expressions.ContextField,<EOL>lambda context_field: expressions.GlobalContextField(<EOL>context_field.location, context_field.field_type))<EOL>filter_block = filter_block.visit_and_update_expressions(visitor_fn)<EOL>set_fold_count_filter(context)<EOL>context['<STR_LIT>'].append(filter_block)<EOL><DEDENT>else:<EOL><INDENT>basic_blocks.append(filter_block)<EOL><DEDENT><DEDENT>if location.field is not None:<EOL><INDENT>_compile_property_ast(schema, current_schema_type, ast,<EOL>location, context, local_unique_directives)<EOL><DEDENT>else:<EOL><INDENT>if fragment_exists:<EOL><INDENT>basic_blocks.extend(<EOL>_compile_fragment_ast(schema, current_schema_type, fragment, location, context))<EOL><DEDENT>else:<EOL><INDENT>basic_blocks.extend(<EOL>_compile_vertex_ast(schema, current_schema_type, ast,<EOL>location, context, local_unique_directives, fields))<EOL><DEDENT><DEDENT>return basic_blocks<EOL> | Compile the given GraphQL AST node into a list of basic blocks.
Args:
schema: GraphQL schema object, obtained from the graphql library
current_schema_type: GraphQLType, the schema type at the current location
ast: the current GraphQL AST node, obtained from the graphql library
location: Location object representing the current location in the query
context: dict, various per-compilation data (e.g. declared tags, whether the current block
is optional, etc.). May be mutated in-place in this function!
Returns:
list of basic blocks corresponding to this GraphQL AST node | f12656:m11 |
def _validate_all_tags_are_used(metadata): | tag_names = set([tag_name for tag_name, _ in metadata.tags])<EOL>filter_arg_names = set()<EOL>for location, _ in metadata.registered_locations:<EOL><INDENT>for filter_info in metadata.get_filter_infos(location):<EOL><INDENT>for filter_arg in filter_info.args:<EOL><INDENT>if is_tag_argument(filter_arg):<EOL><INDENT>filter_arg_names.add(get_directive_argument_name(filter_arg))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>unused_tags = tag_names - filter_arg_names<EOL>if unused_tags:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(unused_tags))<EOL><DEDENT> | Ensure all tags are used in some filter. | f12656:m12 |
def _compile_root_ast_to_ir(schema, ast, type_equivalence_hints=None): | if len(ast.selection_set.selections) != <NUM_LIT:1>:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>')<EOL><DEDENT>base_ast = ast.selection_set.selections[<NUM_LIT:0>]<EOL>base_start_type = get_ast_field_name(base_ast) <EOL>current_schema_type = get_field_type_from_schema(schema.get_query_type(), base_start_type)<EOL>location = Location((base_start_type,))<EOL>base_location_info = LocationInfo(<EOL>parent_location=None,<EOL>type=current_schema_type,<EOL>coerced_from_type=None,<EOL>optional_scopes_depth=<NUM_LIT:0>,<EOL>recursive_scopes_depth=<NUM_LIT:0>,<EOL>is_within_fold=False,<EOL>)<EOL>query_metadata_table = QueryMetadataTable(location, base_location_info)<EOL>if not type_equivalence_hints:<EOL><INDENT>type_equivalence_hints = dict()<EOL><DEDENT>context = {<EOL>'<STR_LIT>': query_metadata_table,<EOL>'<STR_LIT>': dict(),<EOL>'<STR_LIT>': [],<EOL>'<STR_LIT>': dict(),<EOL>'<STR_LIT>': dict(),<EOL>'<STR_LIT>': type_equivalence_hints,<EOL>'<STR_LIT>': invert_dict(type_equivalence_hints),<EOL>}<EOL>basic_blocks = [<EOL>blocks.QueryRoot({base_start_type})<EOL>]<EOL>immediate_fragment = _get_inline_fragment(base_ast)<EOL>if immediate_fragment is not None:<EOL><INDENT>msg_args = {<EOL>'<STR_LIT>': immediate_fragment.type_condition.name.value,<EOL>'<STR_LIT>': base_start_type,<EOL>}<EOL>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(**msg_args))<EOL><DEDENT>validate_root_vertex_directives(base_ast)<EOL>new_basic_blocks = _compile_ast_node_to_ir(<EOL>schema, current_schema_type, base_ast, location, context)<EOL>basic_blocks.extend(new_basic_blocks)<EOL>_validate_all_tags_are_used(context['<STR_LIT>'])<EOL>basic_blocks.append(blocks.GlobalOperationsStart())<EOL>basic_blocks.extend(context['<STR_LIT>'])<EOL>outputs_context = context['<STR_LIT>']<EOL>basic_blocks.append(_compile_output_step(outputs_context))<EOL>output_metadata = {<EOL>name: OutputMetadata(type=value['<STR_LIT:type>'], optional=value['<STR_LIT>'])<EOL>for name, value in six.iteritems(outputs_context)<EOL>}<EOL>return IrAndMetadata(<EOL>ir_blocks=basic_blocks,<EOL>input_metadata=context['<STR_LIT>'],<EOL>output_metadata=output_metadata,<EOL>query_metadata_table=context['<STR_LIT>'])<EOL> | Compile a full GraphQL abstract syntax tree (AST) to intermediate representation.
Args:
schema: GraphQL schema object, obtained from the graphql library
ast: the root GraphQL AST node for the query, obtained from the graphql library,
and already validated against the schema for type-correctness
type_equivalence_hints: optional dict of GraphQL type to equivalent GraphQL union
Returns:
IrAndMetadata named tuple, containing fields:
- ir_blocks: a list of IR basic block objects
- input_metadata: a dict of expected input parameters (string) -> inferred GraphQL type
- output_metadata: a dict of output name (string) -> OutputMetadata object
- location_types: a dict of location objects -> GraphQL type objects at that location
- coerced_locations: a set of location objects indicating where type coercions have happened | f12656:m13 |
def _compile_output_step(outputs): | if not outputs:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL><DEDENT>output_fields = {}<EOL>for output_name, output_context in six.iteritems(outputs):<EOL><INDENT>location = output_context['<STR_LIT:location>']<EOL>optional = output_context['<STR_LIT>']<EOL>graphql_type = output_context['<STR_LIT:type>']<EOL>expression = None<EOL>existence_check = None<EOL>if isinstance(location, FoldScopeLocation):<EOL><INDENT>if optional:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(output_context))<EOL><DEDENT>if location.field == COUNT_META_FIELD_NAME:<EOL><INDENT>expression = expressions.FoldCountContextField(location)<EOL><DEDENT>else:<EOL><INDENT>expression = expressions.FoldedContextField(location, graphql_type)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>expression = expressions.OutputContextField(location, graphql_type)<EOL>if optional:<EOL><INDENT>existence_check = expressions.ContextFieldExistence(location.at_vertex())<EOL><DEDENT><DEDENT>if existence_check:<EOL><INDENT>expression = expressions.TernaryConditional(<EOL>existence_check, expression, expressions.NullLiteral)<EOL><DEDENT>output_fields[output_name] = expression<EOL><DEDENT>return blocks.ConstructResult(output_fields)<EOL> | Construct the final ConstructResult basic block that defines the output format of the query.
Args:
outputs: dict, output name (string) -> output data dict, specifying the location
from where to get the data, and whether the data is optional (and therefore
may be missing); missing optional data is replaced with 'null'
Returns:
a ConstructResult basic block that constructs appropriate outputs for the query | f12656:m14 |
def _preprocess_graphql_string(graphql_string): | <EOL>return graphql_string + '<STR_LIT:\n>'<EOL> | Apply any necessary preprocessing to the input GraphQL string, returning the new version. | f12656:m15 |
def _validate_schema_and_ast(schema, ast): | core_graphql_errors = validate(schema, ast)<EOL>unsupported_default_directives = frozenset([<EOL>frozenset([<EOL>'<STR_LIT>',<EOL>frozenset(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']),<EOL>frozenset(['<STR_LIT>'])<EOL>]),<EOL>frozenset([<EOL>'<STR_LIT>',<EOL>frozenset(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']),<EOL>frozenset(['<STR_LIT>'])<EOL>]),<EOL>frozenset([<EOL>'<STR_LIT>',<EOL>frozenset(['<STR_LIT>', '<STR_LIT>']),<EOL>frozenset(['<STR_LIT>'])<EOL>])<EOL>])<EOL>expected_directives = {<EOL>frozenset([<EOL>directive.name,<EOL>frozenset(directive.locations),<EOL>frozenset(six.viewkeys(directive.args))<EOL>])<EOL>for directive in DIRECTIVES<EOL>}<EOL>actual_directives = {<EOL>frozenset([<EOL>directive.name,<EOL>frozenset(directive.locations),<EOL>frozenset(six.viewkeys(directive.args))<EOL>])<EOL>for directive in schema.get_directives()<EOL>}<EOL>missing_directives = expected_directives - actual_directives<EOL>if missing_directives:<EOL><INDENT>missing_message = (u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(missing_directives))<EOL>core_graphql_errors.append(missing_message)<EOL><DEDENT>extra_directives = actual_directives - expected_directives - unsupported_default_directives<EOL>if extra_directives:<EOL><INDENT>extra_message = (u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(extra_directives))<EOL>core_graphql_errors.append(extra_message)<EOL><DEDENT>return core_graphql_errors<EOL> | Validate the supplied graphql schema and ast.
This method wraps around graphql-core's validation to enforce a stricter requirement of the
schema -- all directives supported by the compiler must be declared by the schema, regardless of
whether each directive is used in the query or not.
Args:
schema: GraphQL schema object, created using the GraphQL library
ast: abstract syntax tree representation of a graphql query
Returns:
list containing schema and/or query validation errors | f12656:m16 |
def graphql_to_ir(schema, graphql_string, type_equivalence_hints=None): | graphql_string = _preprocess_graphql_string(graphql_string)<EOL>try:<EOL><INDENT>ast = parse(graphql_string)<EOL><DEDENT>except GraphQLSyntaxError as e:<EOL><INDENT>raise GraphQLParsingError(e)<EOL><DEDENT>validation_errors = _validate_schema_and_ast(schema, ast)<EOL>if validation_errors:<EOL><INDENT>raise GraphQLValidationError(u'<STR_LIT>'.format(validation_errors))<EOL><DEDENT>if len(ast.definitions) != <NUM_LIT:1>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(graphql_string, ast))<EOL><DEDENT>base_ast = ast.definitions[<NUM_LIT:0>]<EOL>return _compile_root_ast_to_ir(schema, base_ast, type_equivalence_hints=type_equivalence_hints)<EOL> | Convert the given GraphQL string into compiler IR, using the given schema object.
Args:
schema: GraphQL schema object, created using the GraphQL library
graphql_string: string containing the GraphQL to compile to compiler IR
type_equivalence_hints: optional dict of GraphQL interface or type -> GraphQL union.
Used as a workaround for GraphQL's lack of support for
inheritance across "types" (i.e. non-interfaces), as well as a
workaround for Gremlin's total lack of inheritance-awareness.
The key-value pairs in the dict specify that the "key" type
is equivalent to the "value" type, i.e. that the GraphQL type or
interface in the key is the most-derived common supertype
of every GraphQL type in the "value" GraphQL union.
Recursive expansion of type equivalence hints is not performed,
and only type-level correctness of this argument is enforced.
See README.md for more details on everything this parameter does.
*****
Be very careful with this option, as bad input here will
lead to incorrect output queries being generated.
*****
Returns:
IrAndMetadata named tuple, containing fields:
- ir_blocks: a list of IR basic block objects
- input_metadata: a dict of expected input parameters (string) -> inferred GraphQL type
- output_metadata: a dict of output name (string) -> OutputMetadata object
- query_metadata_table: a QueryMetadataTable object containing location metadata
Raises flavors of GraphQLError in the following cases:
- if the query is invalid GraphQL (GraphQLParsingError);
- if the query doesn't match the schema (GraphQLValidationError);
- if the query has more than one definition block (GraphQLValidationError);
- if the query has more than one selection in the root object (GraphQLCompilationError);
- if the query does not obey directive usage rules (GraphQLCompilationError);
- if the query provides invalid / disallowed / wrong number of arguments
for a directive (GraphQLCompilationError).
In the case of implementation bugs, could also raise ValueError, TypeError, or AssertionError. | f12656:m17 |
def __eq__(self, other): | <EOL>return self.type.is_same_type(other.type) and self.optional == other.optional<EOL> | Check another OutputMetadata object for equality against this one. | f12656:c0:m0 |
def __ne__(self, other): | return not self.__eq__(other)<EOL> | Check another OutputMetadata object for non-equality against this one. | f12656:c0:m1 |
def workaround_lowering_pass(ir_blocks, query_metadata_table): | new_ir_blocks = []<EOL>for block in ir_blocks:<EOL><INDENT>if isinstance(block, Filter):<EOL><INDENT>new_block = _process_filter_block(query_metadata_table, block)<EOL><DEDENT>else:<EOL><INDENT>new_block = block<EOL><DEDENT>new_ir_blocks.append(new_block)<EOL><DEDENT>return new_ir_blocks<EOL> | Extract locations from TernaryConditionals and rewrite their Filter blocks as necessary. | f12657:m0 |
def _process_filter_block(query_metadata_table, block): | <EOL>base_predicate = block.predicate<EOL>ternary_conditionals = []<EOL>problematic_locations = []<EOL>def find_ternary_conditionals(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if isinstance(expression, TernaryConditional):<EOL><INDENT>ternary_conditionals.append(expression)<EOL><DEDENT>return expression<EOL><DEDENT>def extract_locations_visitor(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if isinstance(expression, (ContextField, ContextFieldExistence)):<EOL><INDENT>location_at_vertex = expression.location.at_vertex()<EOL>if location_at_vertex not in problematic_locations:<EOL><INDENT>problematic_locations.append(location_at_vertex)<EOL><DEDENT><DEDENT>return expression<EOL><DEDENT>return_value = base_predicate.visit_and_update(find_ternary_conditionals)<EOL>if return_value is not base_predicate:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(base_predicate, return_value))<EOL><DEDENT>for ternary in ternary_conditionals:<EOL><INDENT>return_value = ternary.visit_and_update(extract_locations_visitor)<EOL>if return_value is not ternary:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ternary, return_value))<EOL><DEDENT><DEDENT>tautologies = [<EOL>_create_tautological_expression_for_location(query_metadata_table, location)<EOL>for location in problematic_locations<EOL>]<EOL>if not tautologies:<EOL><INDENT>return block<EOL><DEDENT>final_predicate = base_predicate<EOL>for tautology in tautologies:<EOL><INDENT>final_predicate = BinaryComposition(u'<STR_LIT>', final_predicate, tautology)<EOL><DEDENT>return Filter(final_predicate)<EOL> | Rewrite the provided Filter block if necessary. | f12657:m1 |
def _create_tautological_expression_for_location(query_metadata_table, location): | location_type = query_metadata_table.get_location_info(location).type<EOL>location_exists = BinaryComposition(<EOL>u'<STR_LIT>', ContextField(location, location_type), NullLiteral)<EOL>location_does_not_exist = BinaryComposition(<EOL>u'<STR_LIT:=>', ContextField(location, location_type), NullLiteral)<EOL>return BinaryComposition(u'<STR_LIT>', location_exists, location_does_not_exist)<EOL> | For a given location, create a BinaryComposition that always evaluates to 'true'. | f12657:m2 |
def workaround_type_coercions_in_recursions(match_query): | <EOL>new_match_traversals = []<EOL>for current_traversal in match_query.match_traversals:<EOL><INDENT>new_traversal = []<EOL>for match_step in current_traversal:<EOL><INDENT>new_match_step = match_step<EOL>has_coerce_type = match_step.coerce_type_block is not None<EOL>has_recurse_root = isinstance(match_step.root_block, Recurse)<EOL>if has_coerce_type and has_recurse_root:<EOL><INDENT>new_where_block = convert_coerce_type_and_add_to_where_block(<EOL>match_step.coerce_type_block, match_step.where_block)<EOL>new_match_step = match_step._replace(coerce_type_block=None,<EOL>where_block=new_where_block)<EOL><DEDENT>new_traversal.append(new_match_step)<EOL><DEDENT>new_match_traversals.append(new_traversal)<EOL><DEDENT>return match_query._replace(match_traversals=new_match_traversals)<EOL> | Lower CoerceType blocks into Filter blocks within Recurse steps. | f12658:m0 |
def _is_local_filter(filter_block): | <EOL>result = {<EOL>'<STR_LIT>': True<EOL>}<EOL>filter_predicate = filter_block.predicate<EOL>def visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>non_local_expression_types = (ContextField, ContextFieldExistence)<EOL>if isinstance(expression, non_local_expression_types):<EOL><INDENT>result['<STR_LIT>'] = False<EOL><DEDENT>return expression<EOL><DEDENT>filter_predicate.visit_and_update(visitor_fn)<EOL>return result['<STR_LIT>']<EOL> | Return True if the Filter block references no non-local fields, and False otherwise. | f12659:m0 |
def _classify_query_locations(match_query): | preferred_locations = set()<EOL>eligible_locations = set()<EOL>ineligible_locations = set()<EOL>first_match_step = match_query.match_traversals[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>if not isinstance(first_match_step.root_block, QueryRoot):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(first_match_step, match_query))<EOL><DEDENT>if first_match_step.where_block is not None:<EOL><INDENT>if _is_local_filter(first_match_step.where_block):<EOL><INDENT>preferred_locations.add(first_match_step.as_block.location)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(first_match_step, match_query))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>eligible_locations.add(first_match_step.as_block.location)<EOL><DEDENT>for current_traversal in match_query.match_traversals:<EOL><INDENT>for match_step in current_traversal:<EOL><INDENT>current_step_location = match_step.as_block.location<EOL>if isinstance(match_step.root_block, QueryRoot):<EOL><INDENT>already_encountered_location = any((<EOL>current_step_location in preferred_locations,<EOL>current_step_location in eligible_locations,<EOL>current_step_location in ineligible_locations,<EOL>))<EOL>if not already_encountered_location:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(current_step_location, match_step, match_query))<EOL><DEDENT>at_eligible_or_preferred_location = (<EOL>current_step_location in preferred_locations or<EOL>current_step_location in eligible_locations)<EOL>continue<EOL><DEDENT>elif isinstance(match_step.root_block, Recurse):<EOL><INDENT>at_eligible_or_preferred_location = False<EOL><DEDENT>elif isinstance(match_step.root_block, Traverse):<EOL><INDENT>if match_step.root_block.optional:<EOL><INDENT>at_eligible_or_preferred_location = False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>.format(match_step.root_block, match_step, match_query))<EOL><DEDENT>if not at_eligible_or_preferred_location:<EOL><INDENT>ineligible_locations.add(current_step_location)<EOL><DEDENT>elif match_step.where_block is not None:<EOL><INDENT>if _is_local_filter(match_step.where_block):<EOL><INDENT>preferred_locations.add(current_step_location)<EOL><DEDENT>else:<EOL><INDENT>ineligible_locations.add(current_step_location)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>eligible_locations.add(current_step_location)<EOL><DEDENT><DEDENT><DEDENT>return preferred_locations, eligible_locations, ineligible_locations<EOL> | Classify query locations into three groups: preferred, eligible, ineligible.
- Ineligible locations are ones that cannot be the starting point of query execution.
These include locations within recursions, locations that are the target of
an optional traversal, and locations with an associated "where:" clause with non-local filter.
- Preferred locations are ones that are eligible to be the starting point, and also have
an associated "where:" clause that references no non-local fields -- only local fields,
literals, and variables.
- Eligible locations are all locations that do not fall into either of these two categories.
Args:
match_query: MatchQuery object describing the query being analyzed for optimization
Returns:
tuple (preferred, eligible, ineligible) where each element is a set of Location objects.
The three sets are disjoint. | f12659:m1 |
def _calculate_type_bound_at_step(match_step): | current_type_bounds = []<EOL>if isinstance(match_step.root_block, QueryRoot):<EOL><INDENT>current_type_bounds.extend(match_step.root_block.start_class)<EOL><DEDENT>if match_step.coerce_type_block is not None:<EOL><INDENT>current_type_bounds.extend(match_step.coerce_type_block.target_class)<EOL><DEDENT>if current_type_bounds:<EOL><INDENT>return get_only_element_from_collection(current_type_bounds)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT> | Return the GraphQL type bound at the given step, or None if no bound is given. | f12659:m2 |
def _assert_type_bounds_are_not_conflicting(current_type_bound, previous_type_bound,<EOL>location, match_query): | if all((current_type_bound is not None,<EOL>previous_type_bound is not None,<EOL>current_type_bound != previous_type_bound)):<EOL><INDENT>raise AssertionError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(location, previous_type_bound, current_type_bound, match_query))<EOL><DEDENT> | Ensure that the two bounds either are an exact match, or one of them is None. | f12659:m3 |
def _expose_only_preferred_locations(match_query, location_types, coerced_locations,<EOL>preferred_locations, eligible_locations): | preferred_location_types = dict()<EOL>eligible_location_types = dict()<EOL>new_match_traversals = []<EOL>for current_traversal in match_query.match_traversals:<EOL><INDENT>new_traversal = []<EOL>for match_step in current_traversal:<EOL><INDENT>new_step = match_step<EOL>current_step_location = match_step.as_block.location<EOL>if current_step_location in preferred_locations:<EOL><INDENT>current_type_bound = _calculate_type_bound_at_step(match_step)<EOL>previous_type_bound = preferred_location_types.get(current_step_location, None)<EOL>if previous_type_bound is not None:<EOL><INDENT>_assert_type_bounds_are_not_conflicting(<EOL>current_type_bound, previous_type_bound, current_step_location, match_query)<EOL><DEDENT>else:<EOL><INDENT>if current_type_bound is None:<EOL><INDENT>current_type_bound = location_types[current_step_location].name<EOL>new_step = match_step._replace(<EOL>coerce_type_block=CoerceType({current_type_bound}))<EOL><DEDENT>preferred_location_types[current_step_location] = current_type_bound<EOL><DEDENT><DEDENT>elif current_step_location in eligible_locations:<EOL><INDENT>current_type_bound = _calculate_type_bound_at_step(match_step)<EOL>previous_type_bound = eligible_location_types.get(current_step_location, None)<EOL>if current_type_bound is not None:<EOL><INDENT>_assert_type_bounds_are_not_conflicting(<EOL>current_type_bound, previous_type_bound, current_step_location, match_query)<EOL>eligible_location_types[current_step_location] = current_type_bound<EOL>if (current_step_location not in coerced_locations or<EOL>previous_type_bound is not None):<EOL><INDENT>if isinstance(match_step.root_block, QueryRoot):<EOL><INDENT>new_root_block = None<EOL><DEDENT>else:<EOL><INDENT>new_root_block = match_step.root_block<EOL><DEDENT>new_step = match_step._replace(<EOL>root_block=new_root_block, coerce_type_block=None)<EOL><DEDENT>else:<EOL><INDENT>if (isinstance(match_step.root_block, QueryRoot) or<EOL>match_step.coerce_type_block is None):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(match_step, match_query))<EOL><DEDENT>new_where_block = convert_coerce_type_and_add_to_where_block(<EOL>match_step.coerce_type_block, match_step.where_block)<EOL>new_step = match_step._replace(<EOL>coerce_type_block=None, where_block=new_where_block)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT>new_traversal.append(new_step)<EOL><DEDENT>new_match_traversals.append(new_traversal)<EOL><DEDENT>return match_query._replace(match_traversals=new_match_traversals)<EOL> | Return a MATCH query where only preferred locations are valid as query start locations. | f12659:m4 |
def _expose_all_eligible_locations(match_query, location_types, eligible_locations): | eligible_location_types = dict()<EOL>new_match_traversals = []<EOL>for current_traversal in match_query.match_traversals:<EOL><INDENT>new_traversal = []<EOL>for match_step in current_traversal:<EOL><INDENT>new_step = match_step<EOL>current_step_location = match_step.as_block.location<EOL>if current_step_location in eligible_locations:<EOL><INDENT>current_type_bound = _calculate_type_bound_at_step(match_step)<EOL>previous_type_bound = eligible_location_types.get(current_step_location, None)<EOL>if current_type_bound is None:<EOL><INDENT>current_type_bound = location_types[current_step_location].name<EOL>new_coerce_type_block = CoerceType({current_type_bound})<EOL>new_step = match_step._replace(coerce_type_block=new_coerce_type_block)<EOL><DEDENT>else:<EOL><INDENT>_assert_type_bounds_are_not_conflicting(<EOL>current_type_bound, previous_type_bound, current_step_location, match_query)<EOL><DEDENT>eligible_location_types[current_step_location] = current_type_bound<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT>new_traversal.append(new_step)<EOL><DEDENT>new_match_traversals.append(new_traversal)<EOL><DEDENT>return match_query._replace(match_traversals=new_match_traversals)<EOL> | Return a MATCH query where all eligible locations are valid as query start locations. | f12659:m5 |
def expose_ideal_query_execution_start_points(compound_match_query, location_types,<EOL>coerced_locations): | new_queries = []<EOL>for match_query in compound_match_query.match_queries:<EOL><INDENT>location_classification = _classify_query_locations(match_query)<EOL>preferred_locations, eligible_locations, _ = location_classification<EOL>if preferred_locations:<EOL><INDENT>new_query = _expose_only_preferred_locations(<EOL>match_query, location_types, coerced_locations,<EOL>preferred_locations, eligible_locations)<EOL><DEDENT>elif eligible_locations:<EOL><INDENT>new_query = _expose_all_eligible_locations(<EOL>match_query, location_types, eligible_locations)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(match_query))<EOL><DEDENT>new_queries.append(new_query)<EOL><DEDENT>return compound_match_query._replace(match_queries=new_queries)<EOL> | Ensure that OrientDB only considers desirable query start points in query planning. | f12659:m6 |
def __init__(self, *args, **kwargs): | self._print_args = args<EOL>self._print_kwargs = kwargs<EOL> | Construct a new CompilerEntity. | f12660:c0:m0 |
@abstractmethod<EOL><INDENT>def validate(self):<DEDENT> | raise NotImplementedError()<EOL> | Ensure that the CompilerEntity is valid. | f12660:c0:m1 |
def __str__(self): | printed_args = []<EOL>if self._print_args:<EOL><INDENT>printed_args.append('<STR_LIT>')<EOL><DEDENT>if self._print_kwargs:<EOL><INDENT>printed_args.append('<STR_LIT>')<EOL><DEDENT>template = u'<STR_LIT>' + u'<STR_LIT:U+002CU+0020>'.join(printed_args) + u'<STR_LIT:)>'<EOL>return template.format(cls_name=type(self).__name__,<EOL>args=self._print_args,<EOL>kwargs=self._print_kwargs)<EOL> | Return a human-readable unicode representation of this CompilerEntity. | f12660:c0:m2 |
def __repr__(self): | return self.__str__()<EOL> | Return a human-readable str representation of the CompilerEntity object. | f12660:c0:m3 |
def __eq__(self, other): | if type(self) != type(other):<EOL><INDENT>return False<EOL><DEDENT>if len(self._print_args) != len(other._print_args):<EOL><INDENT>return False<EOL><DEDENT>for self_arg, other_arg in six.moves.zip(self._print_args, other._print_args):<EOL><INDENT>if is_type(self_arg):<EOL><INDENT>if not self_arg.is_same_type(other_arg):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if self_arg != other_arg:<EOL><INDENT>return False<EOL><DEDENT><DEDENT><DEDENT>return self._print_kwargs == other._print_kwargs<EOL> | Return True if the CompilerEntity objects are equal, and False otherwise. | f12660:c0:m4 |
def __ne__(self, other): | return not self.__eq__(other)<EOL> | Check another object for non-equality against this one. | f12660:c0:m5 |
@abstractmethod<EOL><INDENT>def to_gremlin(self):<DEDENT> | raise NotImplementedError()<EOL> | Return the Gremlin unicode string representation of this object. | f12660:c0:m6 |
def visit_and_update(self, visitor_fn): | <EOL>return visitor_fn(self)<EOL> | Create an updated version (if needed) of the Expression via the visitor pattern.
Args:
visitor_fn: function that takes an Expression argument, and returns an Expression.
This function is recursively called on all child Expressions that may
exist within this expression. If the visitor_fn does not return the
exact same object that was passed in, this is interpreted as an update
request, and the visit_and_update() method will return a new Expression
with the given update applied. No Expressions are mutated in-place.
Returns:
- If the visitor_fn does not request any updates (by always returning the exact same
object it was called with), this method returns 'self'.
- Otherwise, this method returns a new Expression object that reflects the updates
requested by the visitor_fn. | f12660:c1:m0 |
def visit_and_update_expressions(self, visitor_fn): | <EOL>return self<EOL> | Create an updated version (if needed) of the BasicBlock via the visitor pattern.
Args:
visitor_fn: function that takes an Expression argument, and returns an Expression.
This function is recursively called on all child Expressions that may
exist within this BasicBlock. If the visitor_fn does not return the
exact same object that was passed in, this is interpreted as an update
request, and the visit_and_update() method will return a new BasicBlock
with the given update applied. No Expressions or BasicBlocks are
mutated in-place.
Returns:
- If the visitor_fn does not request any updates (by always returning the exact same
object it was called with), this method returns 'self'.
- Otherwise, this method returns a new BasicBlock object that reflects the updates
requested by the visitor_fn. | f12660:c2:m0 |
def to_gremlin(self): | return u'<STR_LIT>'<EOL> | Return the Gremlin representation of the block, which should almost always be empty.
The effect of MarkerBlocks is applied during optimization and code generation steps. | f12660:c3:m0 |
def __init__(self, start_class): | super(QueryRoot, self).__init__(start_class)<EOL>self.start_class = start_class<EOL>self.validate()<EOL> | Construct a QueryRoot object that starts querying at the specified class name.
Args:
start_class: set of string, class names from which to start the query.
This will generally be a set of length 1, except when using Gremlin
with a non-final class, where we have to include all subclasses
of the start class. This is done using a Gremlin-only IR lowering step.
Returns:
new QueryRoot object | f12661:c0:m0 |
def validate(self): | if not (isinstance(self.start_class, set) and<EOL>all(isinstance(x, six.string_types) for x in self.start_class)):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.start_class).__name__, self.start_class))<EOL><DEDENT>for cls in self.start_class:<EOL><INDENT>validate_safe_string(cls)<EOL><DEDENT> | Ensure that the QueryRoot block is valid. | f12661:c0:m1 |
def to_gremlin(self): | self.validate()<EOL>if len(self.start_class) == <NUM_LIT:1>:<EOL><INDENT>start_class = list(self.start_class)[<NUM_LIT:0>]<EOL>return u'<STR_LIT>'.format('<STR_LIT>', safe_quoted_string(start_class))<EOL><DEDENT>else:<EOL><INDENT>start_classes_list = '<STR_LIT:U+002C>'.join(safe_quoted_string(x) for x in self.start_class)<EOL>return u'<STR_LIT>'.format(start_classes_list)<EOL><DEDENT> | Return a unicode object with the Gremlin representation of this block. | f12661:c0:m2 |
def __init__(self, target_class): | super(CoerceType, self).__init__(target_class)<EOL>self.target_class = target_class<EOL>self.validate()<EOL> | Construct a CoerceType object that filters out any data that is not of the given types.
Args:
target_class: set of string, class names from which to start the query.
This will generally be a set of length 1, except when using Gremlin
with a non-final class, where we have to include all subclasses
of the target class. This is done using a Gremlin-only IR lowering step.
Returns:
new CoerceType object | f12661:c1:m0 |
def validate(self): | if not (isinstance(self.target_class, set) and<EOL>all(isinstance(x, six.string_types) for x in self.target_class)):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.target_class).__name__, self.target_class))<EOL><DEDENT>for cls in self.target_class:<EOL><INDENT>validate_safe_string(cls)<EOL><DEDENT> | Ensure that the CoerceType block is valid. | f12661:c1:m1 |
def to_gremlin(self): | raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL> | Not implemented, should not be used. | f12661:c1:m2 |
def __init__(self, fields): | self.fields = {<EOL>ensure_unicode_string(key): value<EOL>for key, value in six.iteritems(fields)<EOL>}<EOL>super(ConstructResult, self).__init__(self.fields)<EOL>self.validate()<EOL> | Construct a ConstructResult object that maps the given field names to their expressions.
Args:
fields: dict, variable name string -> Expression
see rules for variable names in validate_safe_string().
Returns:
new ConstructResult object | f12661:c2:m0 |
def validate(self): | if not isinstance(self.fields, dict):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.fields).__name__, self.fields))<EOL><DEDENT>for key, value in six.iteritems(self.fields):<EOL><INDENT>validate_safe_string(key)<EOL>if not isinstance(value, Expression):<EOL><INDENT>raise TypeError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(key, value))<EOL><DEDENT><DEDENT> | Ensure that the ConstructResult block is valid. | f12661:c2:m1 |
def visit_and_update_expressions(self, visitor_fn): | new_fields = {}<EOL>for key, value in six.iteritems(self.fields):<EOL><INDENT>new_value = value.visit_and_update(visitor_fn)<EOL>if new_value is not value:<EOL><INDENT>new_fields[key] = new_value<EOL><DEDENT><DEDENT>if new_fields:<EOL><INDENT>return ConstructResult(dict(self.fields, **new_fields))<EOL><DEDENT>else:<EOL><INDENT>return self<EOL><DEDENT> | Create an updated version (if needed) of the ConstructResult via the visitor pattern. | f12661:c2:m2 |
def to_gremlin(self): | self.validate()<EOL>template = (<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL>field_representations = (<EOL>u'<STR_LIT>'.format(name=key, expr=self.fields[key].to_gremlin())<EOL>for key in sorted(self.fields.keys()) <EOL>)<EOL>return template.format(u'<STR_LIT:U+002CU+0020>'.join(field_representations))<EOL> | Return a unicode object with the Gremlin representation of this block. | f12661:c2:m3 |
def __init__(self, predicate): | super(Filter, self).__init__(predicate)<EOL>self.predicate = predicate<EOL>self.validate()<EOL> | Create a new Filter with the specified Expression as a predicate. | f12661:c3:m0 |
def validate(self): | if not isinstance(self.predicate, Expression):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.predicate).__name__, self.predicate))<EOL><DEDENT> | Ensure that the Filter block is valid. | f12661:c3:m1 |
def visit_and_update_expressions(self, visitor_fn): | new_predicate = self.predicate.visit_and_update(visitor_fn)<EOL>if new_predicate is not self.predicate:<EOL><INDENT>return Filter(new_predicate)<EOL><DEDENT>else:<EOL><INDENT>return self<EOL><DEDENT> | Create an updated version (if needed) of the Filter via the visitor pattern. | f12661:c3:m2 |
def to_gremlin(self): | self.validate()<EOL>return u'<STR_LIT>'.format(self.predicate.to_gremlin())<EOL> | Return a unicode object with the Gremlin representation of this block. | f12661:c3:m3 |
def __init__(self, location): | super(MarkLocation, self).__init__(location)<EOL>self.location = location<EOL>self.validate()<EOL> | Create a new MarkLocation at the specified Location.
Args:
location: Location object, must not be at a property field in the query
Returns:
new MarkLocation object | f12661:c4:m0 |
def validate(self): | validate_marked_location(self.location)<EOL> | Ensure that the MarkLocation block is valid. | f12661:c4:m1 |
def to_gremlin(self): | self.validate()<EOL>mark_name, _ = self.location.get_location_name()<EOL>return u'<STR_LIT>'.format(safe_quoted_string(mark_name))<EOL> | Return a unicode object with the Gremlin representation of this block. | f12661:c4:m2 |
def __init__(self, direction, edge_name, optional=False, within_optional_scope=False): | super(Traverse, self).__init__(<EOL>direction, edge_name, optional=optional, within_optional_scope=within_optional_scope)<EOL>self.direction = direction<EOL>self.edge_name = edge_name<EOL>self.optional = optional<EOL>self.within_optional_scope = within_optional_scope<EOL>self.validate()<EOL> | Create a new Traverse block in the given direction and across the given edge.
Args:
direction: string, 'in' or 'out'
edge_name: string obeying variable name rules (see validate_safe_string).
optional: optional bool, specifying whether the traversal to the given location
is optional (i.e. non-filtering) or mandatory (filtering).
Returns:
new Traverse object | f12661:c5:m0 |
def validate(self): | if not isinstance(self.direction, six.string_types):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.direction).__name__, self.direction))<EOL><DEDENT>validate_edge_direction(self.direction)<EOL>validate_safe_string(self.edge_name)<EOL>if not isinstance(self.optional, bool):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.optional).__name__, self.optional))<EOL><DEDENT>if not isinstance(self.within_optional_scope, bool):<EOL><INDENT>raise TypeError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(type(self.within_optional_scope).__name__,<EOL>self.within_optional_scope))<EOL><DEDENT> | Ensure that the Traverse block is valid. | f12661:c5:m1 |
def get_field_name(self): | return u'<STR_LIT>'.format(self.direction, self.edge_name)<EOL> | Return the field name corresponding to the edge being traversed. | f12661:c5:m2 |
def to_gremlin(self): | self.validate()<EOL>if self.optional:<EOL><INDENT>return (u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(<EOL>direction=self.direction,<EOL>edge_name=self.edge_name,<EOL>edge_quoted=safe_quoted_string(self.edge_name)))<EOL><DEDENT>elif self.within_optional_scope:<EOL><INDENT>return (u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(<EOL>direction=self.direction,<EOL>edge_quoted=safe_quoted_string(self.edge_name)))<EOL><DEDENT>else:<EOL><INDENT>return u'<STR_LIT>'.format(<EOL>direction=self.direction,<EOL>edge=safe_quoted_string(self.edge_name))<EOL><DEDENT> | Return a unicode object with the Gremlin representation of this block. | f12661:c5:m3 |
def __init__(self, direction, edge_name, depth, within_optional_scope=False): | super(Recurse, self).__init__(<EOL>direction, edge_name, depth, within_optional_scope=within_optional_scope)<EOL>self.direction = direction<EOL>self.edge_name = edge_name<EOL>self.depth = depth<EOL>self.within_optional_scope = within_optional_scope<EOL>self.validate()<EOL> | Create a new Recurse block which traverses the given edge up to "depth" times.
Args:
direction: string, 'in' or 'out'.
edge_name: string obeying variable name rules (see validate_safe_string).
depth: int, always greater than or equal to 1.
Returns:
new Recurse object | f12661:c6:m0 |
def validate(self): | validate_edge_direction(self.direction)<EOL>validate_safe_string(self.edge_name)<EOL>if not isinstance(self.within_optional_scope, bool):<EOL><INDENT>raise TypeError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(type(self.within_optional_scope).__name__,<EOL>self.within_optional_scope))<EOL><DEDENT>if not isinstance(self.depth, int):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.depth).__name__, self.depth))<EOL><DEDENT>if not (self.depth >= <NUM_LIT:1>):<EOL><INDENT>raise ValueError(u'<STR_LIT>'.format(self.depth))<EOL><DEDENT> | Ensure that the Traverse block is valid. | f12661:c6:m1 |
def to_gremlin(self): | self.validate()<EOL>template = '<STR_LIT>'<EOL>recurse_base = '<STR_LIT>'<EOL>recurse_traversal = '<STR_LIT>'.format(<EOL>direction=self.direction, edge_name=self.edge_name)<EOL>recurse_steps = [<EOL>recurse_base + (recurse_traversal * i)<EOL>for i in six.moves.xrange(self.depth + <NUM_LIT:1>)<EOL>]<EOL>recursion_string = template.format(recurse='<STR_LIT:U+002C>'.join(recurse_steps))<EOL>if self.within_optional_scope:<EOL><INDENT>recurse_template = u'<STR_LIT>'<EOL>return recurse_template.format(recursion_string=recursion_string)<EOL><DEDENT>else:<EOL><INDENT>return recursion_string<EOL><DEDENT> | Return a unicode object with the Gremlin representation of this block. | f12661:c6:m2 |
def __init__(self, location, optional=False): | super(Backtrack, self).__init__(location, optional=optional)<EOL>self.location = location<EOL>self.optional = optional<EOL>self.validate()<EOL> | Create a new Backtrack block, returning to the given location in the query.
Args:
location: Location object, specifying where to backtrack to
optional: optional bool, specifying whether the steps between the current location
and the location to which Backtrack is returning were optional or not
Returns:
new Backtrack object | f12661:c7:m0 |
def validate(self): | validate_marked_location(self.location)<EOL>if not isinstance(self.optional, bool):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.optional).__name__, self.optional))<EOL><DEDENT> | Ensure that the Backtrack block is valid. | f12661:c7:m1 |
def to_gremlin(self): | self.validate()<EOL>if self.optional:<EOL><INDENT>operation = u'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>operation = u'<STR_LIT>'<EOL><DEDENT>mark_name, _ = self.location.get_location_name()<EOL>return u'<STR_LIT>'.format(<EOL>operation=operation,<EOL>mark_name=safe_quoted_string(mark_name))<EOL> | Return a unicode object with the Gremlin representation of this BasicBlock. | f12661:c7:m2 |
def validate(self): | pass<EOL> | Validate the OutputSource block. An OutputSource block is always valid in isolation. | f12661:c8:m0 |
def __init__(self, fold_scope_location): | super(Fold, self).__init__(fold_scope_location)<EOL>self.fold_scope_location = fold_scope_location<EOL>self.validate()<EOL> | Create a new Fold block rooted at the given location. | f12661:c9:m0 |
def validate(self): | if not isinstance(self.fold_scope_location, FoldScopeLocation):<EOL><INDENT>raise TypeError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(type(self.fold_scope_location), self.fold_scope_location))<EOL><DEDENT> | Ensure the Fold block is valid. | f12661:c9:m1 |
def validate(self): | pass<EOL> | Unfold blocks are always valid in isolation. | f12661:c10:m0 |
def validate(self): | pass<EOL> | In isolation, EndOptional blocks are always valid. | f12661:c11:m0 |
def validate(self): | pass<EOL> | In isolation, GlobalOperationsStart blocks are always valid. | f12661:c12:m0 |
def make_replacement_visitor(find_expression, replace_expression): | def visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if expression == find_expression:<EOL><INDENT>return replace_expression<EOL><DEDENT>else:<EOL><INDENT>return expression<EOL><DEDENT><DEDENT>return visitor_fn<EOL> | Return a visitor function that replaces every instance of one expression with another one. | f12662:m0 |
def make_type_replacement_visitor(find_types, replacement_func): | def visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if isinstance(expression, find_types):<EOL><INDENT>return replacement_func(expression)<EOL><DEDENT>else:<EOL><INDENT>return expression<EOL><DEDENT><DEDENT>return visitor_fn<EOL> | Return a visitor function that replaces expressions of a given type with new expressions. | f12662:m1 |
def _validate_operator_name(operator, supported_operators): | if not isinstance(operator, six.text_type):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(operator).__name__, operator))<EOL><DEDENT>if operator not in supported_operators:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(operator))<EOL><DEDENT> | Ensure the named operator is valid and supported. | f12662:m2 |
def __init__(self, value): | super(Literal, self).__init__(value)<EOL>self.value = value<EOL>self.validate()<EOL> | Construct a new Literal object with the given value. | f12662:c0:m0 |
def validate(self): | <EOL>if self.value is None or self.value is True or self.value is False:<EOL><INDENT>return<EOL><DEDENT>if isinstance(self.value, six.string_types):<EOL><INDENT>validate_safe_string(self.value)<EOL>return<EOL><DEDENT>if isinstance(self.value, int):<EOL><INDENT>return<EOL><DEDENT>if isinstance(self.value, list):<EOL><INDENT>if len(self.value) > <NUM_LIT:0>:<EOL><INDENT>for x in self.value:<EOL><INDENT>validate_safe_string(x)<EOL><DEDENT><DEDENT>return<EOL><DEDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(self.value))<EOL> | Validate that the Literal is correctly representable. | f12662:c0:m1 |
def _to_output_code(self): | <EOL>self.validate()<EOL>if self.value is None:<EOL><INDENT>return u'<STR_LIT:null>'<EOL><DEDENT>elif self.value is True:<EOL><INDENT>return u'<STR_LIT:true>'<EOL><DEDENT>elif self.value is False:<EOL><INDENT>return u'<STR_LIT:false>'<EOL><DEDENT>elif isinstance(self.value, six.string_types):<EOL><INDENT>return safe_quoted_string(self.value)<EOL><DEDENT>elif isinstance(self.value, int):<EOL><INDENT>return six.text_type(self.value)<EOL><DEDENT>elif isinstance(self.value, list):<EOL><INDENT>if len(self.value) == <NUM_LIT:0>:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>elif all(isinstance(x, six.string_types) for x in self.value):<EOL><INDENT>list_contents = '<STR_LIT:U+002CU+0020>'.join(safe_quoted_string(x) for x in sorted(self.value))<EOL>return '<STR_LIT:[>' + list_contents + '<STR_LIT:]>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass <EOL><DEDENT>raise AssertionError(u'<STR_LIT>'.format(self))<EOL> | Return a unicode object with the Gremlin/MATCH representation of this Literal. | f12662:c0:m2 |
def __init__(self, variable_name, inferred_type): | variable_name = ensure_unicode_string(variable_name)<EOL>super(Variable, self).__init__(variable_name, inferred_type)<EOL>self.variable_name = variable_name<EOL>self.inferred_type = inferred_type<EOL>self.validate()<EOL> | Construct a new Variable object for the given variable name.
Args:
variable_name: string, should start with '$' and then obey variable naming rules
(see validate_safe_string())
inferred_type: GraphQL type object, specifying the inferred type of the variable
Returns:
new Variable object | f12662:c1:m0 |
def validate(self): | <EOL>if not self.variable_name.startswith(u'<STR_LIT:$>'):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(self.variable_name))<EOL><DEDENT>if self.variable_name in RESERVED_MATCH_KEYWORDS:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.variable_name))<EOL><DEDENT>validate_safe_string(self.variable_name[<NUM_LIT:1>:])<EOL>if not is_graphql_type(self.inferred_type):<EOL><INDENT>raise ValueError(u'<STR_LIT>'.format(self.inferred_type))<EOL><DEDENT>if isinstance(self.inferred_type, GraphQLNonNull):<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(self.inferred_type))<EOL><DEDENT>if isinstance(self.inferred_type, GraphQLList):<EOL><INDENT>inner_type = strip_non_null_from_type(self.inferred_type.of_type)<EOL>if GraphQLDate.is_same_type(inner_type) or GraphQLDateTime.is_same_type(inner_type):<EOL><INDENT>raise GraphQLCompilationError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.inferred_type))<EOL><DEDENT><DEDENT> | Validate that the Variable is correctly representable. | f12662:c1:m1 |
def to_match(self): | self.validate()<EOL>variable_with_no_dollar_sign = self.variable_name[<NUM_LIT:1>:]<EOL>match_variable_name = '<STR_LIT>' % (six.text_type(variable_with_no_dollar_sign),)<EOL>if GraphQLDate.is_same_type(self.inferred_type):<EOL><INDENT>return u'<STR_LIT>' % (match_variable_name, STANDARD_DATE_FORMAT)<EOL><DEDENT>elif GraphQLDateTime.is_same_type(self.inferred_type):<EOL><INDENT>return u'<STR_LIT>' % (match_variable_name, STANDARD_DATETIME_FORMAT)<EOL><DEDENT>else:<EOL><INDENT>return match_variable_name<EOL><DEDENT> | Return a unicode object with the MATCH representation of this Variable. | f12662:c1:m2 |
def to_gremlin(self): | <EOL>if GraphQLDate.is_same_type(self.inferred_type):<EOL><INDENT>return u'<STR_LIT>'.format(STANDARD_DATE_FORMAT, self.variable_name)<EOL><DEDENT>elif GraphQLDateTime.is_same_type(self.inferred_type):<EOL><INDENT>return u'<STR_LIT>'.format(STANDARD_DATETIME_FORMAT, self.variable_name)<EOL><DEDENT>else:<EOL><INDENT>return six.text_type(self.variable_name)<EOL><DEDENT> | Return a unicode object with the Gremlin representation of this expression. | f12662:c1:m3 |
def __eq__(self, other): | <EOL>return (type(self) == type(other) and<EOL>self.variable_name == other.variable_name and<EOL>self.inferred_type.is_same_type(other.inferred_type))<EOL> | Return True if the given object is equal to this one, and False otherwise. | f12662:c1:m4 |
def __ne__(self, other): | return not self.__eq__(other)<EOL> | Check another object for non-equality against this one. | f12662:c1:m5 |
def __init__(self, field_name): | super(LocalField, self).__init__(field_name)<EOL>self.field_name = field_name<EOL>self.validate()<EOL> | Construct a new LocalField object that references a field at the current position. | f12662:c2:m0 |
def get_local_object_gremlin_name(self): | return u'<STR_LIT>'<EOL> | Return the Gremlin name of the local object whose field is being produced. | f12662:c2:m1 |
def validate(self): | validate_safe_string(self.field_name)<EOL> | Validate that the LocalField is correctly representable. | f12662:c2:m2 |
def to_match(self): | self.validate()<EOL>return six.text_type(self.field_name)<EOL> | Return a unicode object with the MATCH representation of this LocalField. | f12662:c2:m3 |
def to_gremlin(self): | self.validate()<EOL>local_object_name = self.get_local_object_gremlin_name()<EOL>if self.field_name == '<STR_LIT>':<EOL><INDENT>return local_object_name<EOL><DEDENT>if '<STR_LIT:@>' in self.field_name:<EOL><INDENT>return u'<STR_LIT>'.format(local_object_name, self.field_name)<EOL><DEDENT>else:<EOL><INDENT>return u'<STR_LIT>'.format(local_object_name, self.field_name)<EOL><DEDENT> | Return a unicode object with the Gremlin representation of this expression. | f12662:c2:m4 |
def __init__(self, location, field_type): | super(GlobalContextField, self).__init__(location, field_type)<EOL>self.location = location<EOL>self.field_type = field_type<EOL>self.validate()<EOL> | Construct a new GlobalContextField object that references a field at a given location.
Args:
location: Location, specifying where the field was declared.
Returns:
new GlobalContextField object | f12662:c3:m0 |
def validate(self): | if not isinstance(self.location, Location):<EOL><INDENT>raise TypeError(u'<STR_LIT>'<EOL>.format(type(self.location).__name__, self.location))<EOL><DEDENT>if self.location.field is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>.format(self.location))<EOL><DEDENT>if not is_graphql_type(self.field_type):<EOL><INDENT>raise ValueError(u'<STR_LIT>'.format(self.field_type))<EOL><DEDENT> | Validate that the GlobalContextField is correctly representable. | f12662:c3:m1 |
def to_match(self): | self.validate()<EOL>mark_name, field_name = self.location.get_location_name()<EOL>validate_safe_string(mark_name)<EOL>validate_safe_string(field_name)<EOL>return u'<STR_LIT>' % (mark_name, field_name)<EOL> | Return a unicode object with the MATCH representation of this GlobalContextField. | f12662:c3:m2 |
def to_gremlin(self): | raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL> | Not implemented, should not be used. | f12662:c3:m3 |
def __init__(self, location, field_type): | super(ContextField, self).__init__(location, field_type)<EOL>self.location = location<EOL>self.field_type = field_type<EOL>self.validate()<EOL> | Construct a new ContextField object that references a field from the global context.
Args:
location: Location, specifying where the field was declared. If the Location points
to a vertex, the field refers to the data captured at the location vertex.
Otherwise, if the Location points to a property, the field refers to
the particular value of that property.
field_type: GraphQLType object, specifying the type of the field being output
Returns:
new ContextField object | f12662:c4:m0 |
def validate(self): | if not isinstance(self.location, Location):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.location).__name__, self.location))<EOL><DEDENT>if not is_graphql_type(self.field_type):<EOL><INDENT>raise ValueError(u'<STR_LIT>'.format(self.field_type))<EOL><DEDENT> | Validate that the ContextField is correctly representable. | f12662:c4:m1 |
def to_match(self): | self.validate()<EOL>mark_name, field_name = self.location.get_location_name()<EOL>validate_safe_string(mark_name)<EOL>if field_name is None:<EOL><INDENT>return u'<STR_LIT>' % (mark_name,)<EOL><DEDENT>else:<EOL><INDENT>validate_safe_string(field_name)<EOL>return u'<STR_LIT>' % (mark_name, field_name)<EOL><DEDENT> | Return a unicode object with the MATCH representation of this ContextField. | f12662:c4:m2 |
def to_gremlin(self): | self.validate()<EOL>mark_name, field_name = self.location.get_location_name()<EOL>if field_name is not None:<EOL><INDENT>validate_safe_string(field_name)<EOL>if '<STR_LIT:@>' in field_name:<EOL><INDENT>template = u'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>template = u'<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>template = u'<STR_LIT>'<EOL><DEDENT>validate_safe_string(mark_name)<EOL>return template.format(mark_name=mark_name, field_name=field_name)<EOL> | Return a unicode object with the Gremlin representation of this expression. | f12662:c4:m3 |
def __init__(self, location, field_type): | super(OutputContextField, self).__init__(location, field_type)<EOL>self.location = location<EOL>self.field_type = field_type<EOL>self.validate()<EOL> | Construct a new OutputContextField object for the field at the given location.
Args:
location: Location, specifying where the field was declared. The Location
must point to a property, and that property's value is output as the result.
field_type: GraphQL type object, specifying the type of the field being output
Returns:
new OutputContextField object | f12662:c5:m0 |
def validate(self): | if not isinstance(self.location, Location):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.location).__name__, self.location))<EOL><DEDENT>if not self.location.field:<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(self.location))<EOL><DEDENT>if not is_graphql_type(self.field_type):<EOL><INDENT>raise ValueError(u'<STR_LIT>'.format(self.field_type))<EOL><DEDENT>stripped_field_type = strip_non_null_from_type(self.field_type)<EOL>if isinstance(stripped_field_type, GraphQLList):<EOL><INDENT>inner_type = strip_non_null_from_type(stripped_field_type.of_type)<EOL>if GraphQLDate.is_same_type(inner_type) or GraphQLDateTime.is_same_type(inner_type):<EOL><INDENT>raise GraphQLCompilationError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.field_type))<EOL><DEDENT><DEDENT> | Validate that the OutputContextField is correctly representable. | f12662:c5:m1 |
def to_match(self): | self.validate()<EOL>mark_name, field_name = self.location.get_location_name()<EOL>validate_safe_string(mark_name)<EOL>validate_safe_string(field_name)<EOL>stripped_field_type = strip_non_null_from_type(self.field_type)<EOL>if GraphQLDate.is_same_type(stripped_field_type):<EOL><INDENT>return u'<STR_LIT>' % (mark_name, field_name, STANDARD_DATE_FORMAT)<EOL><DEDENT>elif GraphQLDateTime.is_same_type(stripped_field_type):<EOL><INDENT>return u'<STR_LIT>' % (mark_name, field_name, STANDARD_DATETIME_FORMAT)<EOL><DEDENT>else:<EOL><INDENT>return u'<STR_LIT>' % (mark_name, field_name)<EOL><DEDENT> | Return a unicode object with the MATCH representation of this expression. | f12662:c5:m2 |
def to_gremlin(self): | self.validate()<EOL>mark_name, field_name = self.location.get_location_name()<EOL>validate_safe_string(mark_name)<EOL>validate_safe_string(field_name)<EOL>if '<STR_LIT:@>' in field_name:<EOL><INDENT>template = u'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>template = u'<STR_LIT>'<EOL><DEDENT>format_value = None<EOL>stripped_field_type = strip_non_null_from_type(self.field_type)<EOL>if GraphQLDate.is_same_type(stripped_field_type):<EOL><INDENT>template += '<STR_LIT>'<EOL>format_value = STANDARD_DATE_FORMAT<EOL><DEDENT>elif GraphQLDateTime.is_same_type(stripped_field_type):<EOL><INDENT>template += '<STR_LIT>'<EOL>format_value = STANDARD_DATETIME_FORMAT<EOL><DEDENT>return template.format(mark_name=mark_name, field_name=field_name,<EOL>format=format_value)<EOL> | Return a unicode object with the Gremlin representation of this expression. | f12662:c5:m3 |
def __eq__(self, other): | <EOL>return (type(self) == type(other) and<EOL>self.location == other.location and<EOL>self.field_type.is_same_type(other.field_type))<EOL> | Return True if the given object is equal to this one, and False otherwise. | f12662:c5:m4 |
def __ne__(self, other): | return not self.__eq__(other)<EOL> | Check another object for non-equality against this one. | f12662:c5:m5 |
def __init__(self, fold_scope_location, field_type): | super(FoldedContextField, self).__init__(fold_scope_location, field_type)<EOL>self.fold_scope_location = fold_scope_location<EOL>self.field_type = field_type<EOL>self.validate()<EOL> | Construct a new FoldedContextField object for this folded field.
Args:
fold_scope_location: FoldScopeLocation specifying the location of
the context field being output.
field_type: GraphQL type object, specifying the type of the field being output.
Since the field is folded, this must be a GraphQLList of some kind.
Returns:
new FoldedContextField object | f12662:c6:m0 |
def validate(self): | if not isinstance(self.fold_scope_location, FoldScopeLocation):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.fold_scope_location), self.fold_scope_location))<EOL><DEDENT>if self.fold_scope_location.field is None:<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>.format(self.fold_scope_location))<EOL><DEDENT>if self.fold_scope_location.field == COUNT_META_FIELD_NAME:<EOL><INDENT>if not GraphQLInt.is_same_type(self.field_type):<EOL><INDENT>raise TypeError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(self.field_type, self.fold_scope_location))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not isinstance(self.field_type, GraphQLList):<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(self.field_type, self.fold_scope_location))<EOL><DEDENT>inner_type = strip_non_null_from_type(self.field_type.of_type)<EOL>if isinstance(inner_type, GraphQLList):<EOL><INDENT>raise GraphQLCompilationError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.fold_scope_location, self.field_type.of_type))<EOL><DEDENT><DEDENT> | Validate that the FoldedContextField is correctly representable. | f12662:c6:m1 |
def to_match(self): | self.validate()<EOL>mark_name, field_name = self.fold_scope_location.get_location_name()<EOL>validate_safe_string(mark_name)<EOL>template = u'<STR_LIT>'<EOL>template_data = {<EOL>'<STR_LIT>': mark_name,<EOL>}<EOL>if field_name == COUNT_META_FIELD_NAME:<EOL><INDENT>template_data['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>inner_type = strip_non_null_from_type(self.field_type.of_type)<EOL>if GraphQLDate.is_same_type(inner_type):<EOL><INDENT>template += '<STR_LIT>' + STANDARD_DATE_FORMAT + '<STR_LIT>'<EOL><DEDENT>elif GraphQLDateTime.is_same_type(inner_type):<EOL><INDENT>template += '<STR_LIT>' + STANDARD_DATETIME_FORMAT + '<STR_LIT>'<EOL><DEDENT>template_data['<STR_LIT>'] = field_name<EOL><DEDENT>return template % template_data<EOL> | Return a unicode object with the MATCH representation of this expression. | f12662:c6:m2 |
def to_gremlin(self): | raise NotImplementedError()<EOL> | Must never be called. | f12662:c6:m3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.