signature stringlengths 8 3.44k | body stringlengths 0 1.41M | docstring stringlengths 1 122k | id stringlengths 5 17 |
|---|---|---|---|
def __eq__(self, other): | <EOL>return (type(self) == type(other) and<EOL>self.fold_scope_location == other.fold_scope_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:c6:m4 |
def __ne__(self, other): | return not self.__eq__(other)<EOL> | Check another object for non-equality against this one. | f12662:c6:m5 |
def __init__(self, fold_scope_location): | super(FoldCountContextField, self).__init__(fold_scope_location)<EOL>self.fold_scope_location = fold_scope_location<EOL>self.validate()<EOL> | Construct a new FoldCountContextField object for this fold.
Args:
fold_scope_location: FoldScopeLocation specifying the fold whose size is being output.
Returns:
new FoldCountContextField object | f12662:c7: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 != COUNT_META_FIELD_NAME:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(self.fold_scope_location, self))<EOL><DEDENT> | Validate that the FoldCountContextField is correctly representable. | f12662:c7:m1 |
def to_match(self): | self.validate()<EOL>mark_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>return template % template_data<EOL> | Return a unicode object with the MATCH representation of this expression. | f12662:c7:m2 |
def to_gremlin(self): | raise NotImplementedError()<EOL> | Must never be called. | f12662:c7:m3 |
def __init__(self, location): | super(ContextFieldExistence, self).__init__(location)<EOL>self.location = location<EOL>self.validate()<EOL> | Construct a new ContextFieldExistence object for a vertex field from the global context.
Args:
location: Location, specifying where the field was declared. Must point to a vertex.
Returns:
new ContextFieldExistence expression which evaluates to True iff the vertex exists | f12662:c8: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 self.location.field:<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.location))<EOL><DEDENT> | Validate that the ContextFieldExistence is correctly representable. | f12662:c8:m1 |
def to_match(self): | raise AssertionError(u'<STR_LIT>'.format(self))<EOL> | Must not be used -- ContextFieldExistence must be lowered during the IR lowering step. | f12662:c8:m2 |
def to_gremlin(self): | raise AssertionError(u'<STR_LIT>'.format(self))<EOL> | Must not be used -- ContextFieldExistence must be lowered during the IR lowering step. | f12662:c8:m3 |
def __init__(self, operator, inner_expression): | super(UnaryTransformation, self).__init__(operator, inner_expression)<EOL>self.operator = operator<EOL>self.inner_expression = inner_expression<EOL> | Construct a UnaryExpression that modifies the given inner expression. | f12662:c9:m0 |
def validate(self): | _validate_operator_name(self.operator, UnaryTransformation.SUPPORTED_OPERATORS)<EOL>if not isinstance(self.inner_expression, Expression):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.inner_expression).__name__, self.inner_expression))<EOL><DEDENT> | Validate that the UnaryTransformation is correctly representable. | f12662:c9:m1 |
def visit_and_update(self, visitor_fn): | new_inner = self.inner_expression.visit_and_update(visitor_fn)<EOL>if new_inner is not self.inner_expression:<EOL><INDENT>return visitor_fn(UnaryTransformation(self.operator, new_inner))<EOL><DEDENT>else:<EOL><INDENT>return visitor_fn(self)<EOL><DEDENT> | Create an updated version (if needed) of UnaryTransformation via the visitor pattern. | f12662:c9:m2 |
def to_match(self): | self.validate()<EOL>translation_table = {<EOL>u'<STR_LIT:size>': u'<STR_LIT>',<EOL>}<EOL>match_operator = translation_table.get(self.operator)<EOL>if not match_operator:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.operator, self))<EOL><DEDENT>template = u'<STR_LIT>'<EOL>args = {<EOL>'<STR_LIT>': self.inner_expression.to_match(),<EOL>'<STR_LIT>': match_operator,<EOL>}<EOL>return template % args<EOL> | Return a unicode object with the MATCH representation of this UnaryTransformation. | f12662:c9:m3 |
def to_gremlin(self): | translation_table = {<EOL>u'<STR_LIT:size>': u'<STR_LIT>',<EOL>}<EOL>gremlin_operator = translation_table.get(self.operator)<EOL>if not gremlin_operator:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.operator, self))<EOL><DEDENT>template = u'<STR_LIT>'<EOL>args = {<EOL>'<STR_LIT>': self.inner_expression.to_gremlin(),<EOL>'<STR_LIT>': gremlin_operator,<EOL>}<EOL>return template.format(**args)<EOL> | Return a unicode object with the Gremlin representation of this expression. | f12662:c9:m4 |
def __init__(self, operator, left, right): | super(BinaryComposition, self).__init__(operator, left, right)<EOL>self.operator = operator<EOL>self.left = left<EOL>self.right = right<EOL>self.validate()<EOL> | Construct an expression that connects two expressions with an operator.
Args:
operator: unicode, specifying where the field was declared
left: Expression on the left side of the binary operator
right: Expression on the right side of the binary operator
Returns:
new BinaryComposition object | f12662:c10:m0 |
def validate(self): | _validate_operator_name(self.operator, BinaryComposition.SUPPORTED_OPERATORS)<EOL>if not isinstance(self.left, Expression):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.left).__name__, self.left, self))<EOL><DEDENT>if not isinstance(self.right, Expression):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.right).__name__, self.right))<EOL><DEDENT> | Validate that the BinaryComposition is correctly representable. | f12662:c10:m1 |
def visit_and_update(self, visitor_fn): | new_left = self.left.visit_and_update(visitor_fn)<EOL>new_right = self.right.visit_and_update(visitor_fn)<EOL>if new_left is not self.left or new_right is not self.right:<EOL><INDENT>return visitor_fn(BinaryComposition(self.operator, new_left, new_right))<EOL><DEDENT>else:<EOL><INDENT>return visitor_fn(self)<EOL><DEDENT> | Create an updated version (if needed) of BinaryComposition via the visitor pattern. | f12662:c10:m2 |
def to_match(self): | self.validate()<EOL>regular_operator_format = '<STR_LIT>'<EOL>inverted_operator_format = '<STR_LIT>' <EOL>intersects_operator_format = '<STR_LIT>'<EOL>if any((isinstance(self.left, Literal) and self.left.value is None,<EOL>isinstance(self.right, Literal) and self.right.value is None)):<EOL><INDENT>translation_table = {<EOL>u'<STR_LIT:=>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>}<EOL><DEDENT>else:<EOL><INDENT>translation_table = {<EOL>u'<STR_LIT:=>': (u'<STR_LIT:=>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT:>>': (u'<STR_LIT:>>', regular_operator_format),<EOL>u'<STR_LIT:<>': (u'<STR_LIT:<>', regular_operator_format),<EOL>u'<STR_LIT:+>': (u'<STR_LIT:+>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', intersects_operator_format),<EOL>u'<STR_LIT>': (None, None), <EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', regular_operator_format),<EOL>}<EOL><DEDENT>match_operator, format_spec = translation_table.get(self.operator, (None, None))<EOL>if not match_operator:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.operator, self))<EOL><DEDENT>return format_spec % dict(operator=match_operator,<EOL>left=self.left.to_match(),<EOL>right=self.right.to_match())<EOL> | Return a unicode object with the MATCH representation of this BinaryComposition. | f12662:c10:m3 |
def to_gremlin(self): | self.validate()<EOL>immediate_operator_format = u'<STR_LIT>'<EOL>dotted_operator_format = u'<STR_LIT>'<EOL>intersects_operator_format = u'<STR_LIT>'<EOL>translation_table = {<EOL>u'<STR_LIT:=>': (u'<STR_LIT>', immediate_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', immediate_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', immediate_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', immediate_operator_format),<EOL>u'<STR_LIT:>>': (u'<STR_LIT:>>', immediate_operator_format),<EOL>u'<STR_LIT:<>': (u'<STR_LIT:<>', immediate_operator_format),<EOL>u'<STR_LIT:+>': (u'<STR_LIT:+>', immediate_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', immediate_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', immediate_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', dotted_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', intersects_operator_format),<EOL>u'<STR_LIT>': (u'<STR_LIT>', dotted_operator_format),<EOL>}<EOL>gremlin_operator, format_spec = translation_table.get(self.operator, (None, None))<EOL>if not gremlin_operator:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(self.operator, self))<EOL><DEDENT>return format_spec.format(operator=gremlin_operator,<EOL>left=self.left.to_gremlin(),<EOL>right=self.right.to_gremlin())<EOL> | Return a unicode object with the Gremlin representation of this expression. | f12662:c10:m4 |
def __init__(self, predicate, if_true, if_false): | super(TernaryConditional, self).__init__(predicate, if_true, if_false)<EOL>self.predicate = predicate<EOL>self.if_true = if_true<EOL>self.if_false = if_false<EOL>self.validate()<EOL> | Construct an expression that evaluates a predicate and returns one of two results.
Args:
predicate: Expression to evaluate, and based on which to choose the returned value
if_true: Expression to return if the predicate was true
if_false: Expression to return if the predicate was false
Returns:
new TernaryConditional object | f12662:c11: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>if not isinstance(self.if_true, Expression):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.if_true).__name__, self.if_true))<EOL><DEDENT>if not isinstance(self.if_false, Expression):<EOL><INDENT>raise TypeError(u'<STR_LIT>'.format(<EOL>type(self.if_false).__name__, self.if_false))<EOL><DEDENT> | Validate that the TernaryConditional is correctly representable. | f12662:c11:m1 |
def visit_and_update(self, visitor_fn): | new_predicate = self.predicate.visit_and_update(visitor_fn)<EOL>new_if_true = self.if_true.visit_and_update(visitor_fn)<EOL>new_if_false = self.if_false.visit_and_update(visitor_fn)<EOL>if any((new_predicate is not self.predicate,<EOL>new_if_true is not self.if_true,<EOL>new_if_false is not self.if_false)):<EOL><INDENT>return visitor_fn(TernaryConditional(new_predicate, new_if_true, new_if_false))<EOL><DEDENT>else:<EOL><INDENT>return visitor_fn(self)<EOL><DEDENT> | Create an updated version (if needed) of TernaryConditional via the visitor pattern. | f12662:c11:m2 |
def to_match(self): | self.validate()<EOL>def visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if isinstance(expression, TernaryConditional):<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(expression, self))<EOL><DEDENT>return expression<EOL><DEDENT>self.predicate.visit_and_update(visitor_fn)<EOL>format_spec = u'<STR_LIT>'<EOL>predicate_string = self.predicate.to_match()<EOL>if u'<STR_LIT:">' in predicate_string:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(predicate_string, self))<EOL><DEDENT>return format_spec % dict(predicate=predicate_string,<EOL>if_true=self.if_true.to_match(),<EOL>if_false=self.if_false.to_match())<EOL> | Return a unicode object with the MATCH representation of this TernaryConditional. | f12662:c11:m3 |
def to_gremlin(self): | self.validate()<EOL>return u'<STR_LIT>'.format(<EOL>predicate=self.predicate.to_gremlin(),<EOL>if_true=self.if_true.to_gremlin(),<EOL>if_false=self.if_false.to_gremlin())<EOL> | Return a unicode object with the Gremlin representation of this expression. | f12662:c11:m4 |
def _get_vertex_location_name(location): | mark_name, field_name = location.get_location_name()<EOL>if field_name is not None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(location))<EOL><DEDENT>return mark_name<EOL> | Get the location name from a location that is expected to point to a vertex. | f12663:m0 |
def _first_step_to_match(match_step): | parts = []<EOL>if match_step.root_block is not None:<EOL><INDENT>if not isinstance(match_step.root_block, QueryRoot):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(match_step.root_block, match_step))<EOL><DEDENT>match_step.root_block.validate()<EOL>start_class = get_only_element_from_collection(match_step.root_block.start_class)<EOL>parts.append(u'<STR_LIT>' % (start_class,))<EOL><DEDENT>if match_step.coerce_type_block is not None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(match_step))<EOL><DEDENT>if match_step.where_block:<EOL><INDENT>match_step.where_block.validate()<EOL>parts.append(u'<STR_LIT>' % (match_step.where_block.predicate.to_match(),))<EOL><DEDENT>if match_step.as_block is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(match_step))<EOL><DEDENT>else:<EOL><INDENT>match_step.as_block.validate()<EOL>parts.append(u'<STR_LIT>' % (_get_vertex_location_name(match_step.as_block.location),))<EOL><DEDENT>return u'<STR_LIT>' % (u'<STR_LIT:U+002CU+0020>'.join(parts),)<EOL> | Transform the very first MATCH step into a MATCH query string. | f12663:m1 |
def _subsequent_step_to_match(match_step): | if not isinstance(match_step.root_block, (Traverse, Recurse)):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(match_step.root_block, match_step))<EOL><DEDENT>is_recursing = isinstance(match_step.root_block, Recurse)<EOL>match_step.root_block.validate()<EOL>traversal_command = u'<STR_LIT>' % (match_step.root_block.direction,<EOL>match_step.root_block.edge_name)<EOL>parts = []<EOL>if match_step.coerce_type_block:<EOL><INDENT>coerce_type_set = match_step.coerce_type_block.target_class<EOL>if len(coerce_type_set) != <NUM_LIT:1>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(coerce_type_set, match_step))<EOL><DEDENT>coerce_type_target = list(coerce_type_set)[<NUM_LIT:0>]<EOL>parts.append(u'<STR_LIT>' % (coerce_type_target,))<EOL><DEDENT>if is_recursing:<EOL><INDENT>parts.append(u'<STR_LIT>' % (match_step.root_block.depth,))<EOL><DEDENT>if match_step.where_block:<EOL><INDENT>match_step.where_block.validate()<EOL>parts.append(u'<STR_LIT>' % (match_step.where_block.predicate.to_match(),))<EOL><DEDENT>if not is_recursing and match_step.root_block.optional:<EOL><INDENT>parts.append(u'<STR_LIT>')<EOL><DEDENT>if match_step.as_block:<EOL><INDENT>match_step.as_block.validate()<EOL>parts.append(u'<STR_LIT>' % (_get_vertex_location_name(match_step.as_block.location),))<EOL><DEDENT>return u'<STR_LIT>' % (traversal_command, u'<STR_LIT:U+002CU+0020>'.join(parts))<EOL> | Transform any subsequent (non-first) MATCH step into a MATCH query string. | f12663:m2 |
def _represent_match_traversal(match_traversal): | output = []<EOL>output.append(_first_step_to_match(match_traversal[<NUM_LIT:0>]))<EOL>for step in match_traversal[<NUM_LIT:1>:]:<EOL><INDENT>output.append(_subsequent_step_to_match(step))<EOL><DEDENT>return u'<STR_LIT>'.join(output)<EOL> | Emit MATCH query code for an entire MATCH traversal sequence. | f12663:m3 |
def _represent_fold(fold_location, fold_ir_blocks): | start_let_template = u'<STR_LIT>'<EOL>traverse_edge_template = u'<STR_LIT>'<EOL>base_template = start_let_template + traverse_edge_template<EOL>edge_direction, edge_name = fold_location.get_first_folded_edge()<EOL>mark_name, _ = fold_location.get_location_name()<EOL>base_location_name, _ = fold_location.base_location.get_location_name()<EOL>validate_safe_string(mark_name)<EOL>validate_safe_string(base_location_name)<EOL>validate_safe_string(edge_direction)<EOL>validate_safe_string(edge_name)<EOL>template_data = {<EOL>'<STR_LIT>': mark_name,<EOL>'<STR_LIT>': base_location_name,<EOL>'<STR_LIT>': edge_direction,<EOL>'<STR_LIT>': edge_name,<EOL>}<EOL>final_string = base_template % template_data<EOL>for block in fold_ir_blocks:<EOL><INDENT>if isinstance(block, Filter):<EOL><INDENT>final_string += u'<STR_LIT:[>' + block.predicate.to_match() + u'<STR_LIT:]>'<EOL><DEDENT>elif isinstance(block, Traverse):<EOL><INDENT>template_data = {<EOL>'<STR_LIT>': block.direction,<EOL>'<STR_LIT>': block.edge_name,<EOL>}<EOL>final_string += traverse_edge_template % template_data<EOL><DEDENT>elif isinstance(block, MarkLocation):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(type(block), block, fold_ir_blocks))<EOL><DEDENT><DEDENT>final_string += '<STR_LIT>'<EOL>return final_string<EOL> | Emit a LET clause corresponding to the IR blocks for a @fold scope. | f12663:m4 |
def _construct_output_to_match(output_block): | output_block.validate()<EOL>selections = (<EOL>u'<STR_LIT>' % (output_block.fields[key].to_match(), key)<EOL>for key in sorted(output_block.fields.keys()) <EOL>)<EOL>return u'<STR_LIT>' % (u'<STR_LIT:U+002CU+0020>'.join(selections),)<EOL> | Transform a ConstructResult block into a MATCH query string. | f12663:m5 |
def _construct_where_to_match(where_block): | if where_block.predicate == TrueLiteral:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>.format(where_block))<EOL><DEDENT>return u'<STR_LIT>' + where_block.predicate.to_match()<EOL> | Transform a Filter block into a MATCH query string. | f12663:m6 |
def emit_code_from_single_match_query(match_query): | query_data = deque([u'<STR_LIT>'])<EOL>if not match_query.match_traversals:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(match_query.match_traversals, match_query))<EOL><DEDENT>match_traversal_data = [<EOL>_represent_match_traversal(x)<EOL>for x in match_query.match_traversals<EOL>]<EOL>query_data.append(match_traversal_data[<NUM_LIT:0>])<EOL>for traversal_data in match_traversal_data[<NUM_LIT:1>:]:<EOL><INDENT>query_data.append(u'<STR_LIT:U+002CU+0020>')<EOL>query_data.append(traversal_data)<EOL><DEDENT>query_data.appendleft(u'<STR_LIT>') <EOL>query_data.append(u'<STR_LIT>') <EOL>fold_data = sorted([<EOL>_represent_fold(fold_location, fold_ir_blocks)<EOL>for fold_location, fold_ir_blocks in six.iteritems(match_query.folds)<EOL>])<EOL>if fold_data:<EOL><INDENT>query_data.append(u'<STR_LIT>')<EOL>query_data.append(fold_data[<NUM_LIT:0>])<EOL>for fold_clause in fold_data[<NUM_LIT:1>:]:<EOL><INDENT>query_data.append(u'<STR_LIT:U+002CU+0020>')<EOL>query_data.append(fold_clause)<EOL><DEDENT><DEDENT>query_data.appendleft(_construct_output_to_match(match_query.output_block))<EOL>if match_query.where_block is not None:<EOL><INDENT>query_data.append(_construct_where_to_match(match_query.where_block))<EOL><DEDENT>return u'<STR_LIT:U+0020>'.join(query_data)<EOL> | Return a MATCH query string from a list of IR blocks. | f12663:m7 |
def emit_code_from_multiple_match_queries(match_queries): | optional_variable_base_name = '<STR_LIT>'<EOL>union_variable_name = '<STR_LIT>'<EOL>query_data = deque([u'<STR_LIT>', union_variable_name, u'<STR_LIT:)>', u'<STR_LIT>'])<EOL>optional_variables = []<EOL>sub_queries = [emit_code_from_single_match_query(match_query)<EOL>for match_query in match_queries]<EOL>for (i, sub_query) in enumerate(sub_queries):<EOL><INDENT>variable_name = optional_variable_base_name + str(i)<EOL>variable_assignment = variable_name + u'<STR_LIT>'<EOL>sub_query_end = u'<STR_LIT>'<EOL>query_data.append(variable_assignment)<EOL>query_data.append(sub_query)<EOL>query_data.append(sub_query_end)<EOL>optional_variables.append(variable_name)<EOL><DEDENT>query_data.append(union_variable_name)<EOL>query_data.append(u'<STR_LIT>')<EOL>query_data.append(u'<STR_LIT:U+002CU+0020>'.join(optional_variables))<EOL>query_data.append(u'<STR_LIT:)>')<EOL>return u'<STR_LIT:U+0020>'.join(query_data)<EOL> | Return a MATCH query string from a list of MatchQuery namedtuples. | f12663:m8 |
def emit_code_from_ir(compound_match_query, compiler_metadata): | <EOL>match_queries = compound_match_query.match_queries<EOL>if len(match_queries) == <NUM_LIT:1>:<EOL><INDENT>query_string = emit_code_from_single_match_query(match_queries[<NUM_LIT:0>])<EOL><DEDENT>elif len(match_queries) > <NUM_LIT:1>:<EOL><INDENT>query_string = emit_code_from_multiple_match_queries(match_queries)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(match_queries))<EOL><DEDENT>return query_string<EOL> | Return a MATCH query string from a CompoundMatchQuery. | f12663:m9 |
def emit_code_from_ir(ir_blocks, compiler_metadata): | gremlin_steps = (<EOL>block.to_gremlin()<EOL>for block in ir_blocks<EOL>)<EOL>non_empty_steps = (<EOL>step<EOL>for step in gremlin_steps<EOL>if step<EOL>)<EOL>return u'<STR_LIT:.>'.join(non_empty_steps)<EOL> | Return a MATCH query string from a list of IR blocks. | f12664:m0 |
def is_in_fold_innermost_scope(context): | return CONTEXT_FOLD_INNERMOST_SCOPE in context<EOL> | Return True if the current context is within a scope marked @fold. | f12665:m0 |
def unmark_fold_innermost_scope(context): | del context[CONTEXT_FOLD_INNERMOST_SCOPE]<EOL> | Remove the context mark signaling an innermost fold scope. | f12665:m1 |
def set_fold_innermost_scope(context): | context[CONTEXT_FOLD_INNERMOST_SCOPE] = True<EOL> | Set a mark indicating the innermost scope of a fold scope. | f12665:m2 |
def is_in_fold_scope(context): | return CONTEXT_FOLD in context<EOL> | Return True if the current context is within a scope marked @fold. | f12665:m3 |
def get_context_fold_info(context): | return context[CONTEXT_FOLD]<EOL> | Return the fold info stored in the context. | f12665:m4 |
def unmark_context_fold_scope(context): | del context[CONTEXT_FOLD]<EOL> | Return the context mark signaling the presence of a scope marked @fold. | f12665:m5 |
def set_fold_scope_data(context, data): | context[CONTEXT_FOLD] = data<EOL> | Set fold scope data in the context. | f12665:m6 |
def has_fold_count_filter(context): | return CONTEXT_FOLD_HAS_COUNT_FILTER in context<EOL> | Return True if the current context contains a filter on the _x_count field. | f12665:m7 |
def unmark_fold_count_filter(context): | del context[CONTEXT_FOLD_HAS_COUNT_FILTER]<EOL> | Remove the context mark signaling the existence of a fold count filter. | f12665:m8 |
def set_fold_count_filter(context): | context[CONTEXT_FOLD_HAS_COUNT_FILTER] = True<EOL> | Set a mark indicating the presence of a filter on a fold _x_count field. | f12665:m9 |
def is_in_optional_scope(context): | return CONTEXT_OPTIONAL in context<EOL> | Return True if the current context is within a scope marked @optional. | f12665:m10 |
def get_optional_scope_or_none(context): | return context.get(CONTEXT_OPTIONAL, None)<EOL> | Return the optional scope data recorded in the context, or None if no such data. | f12665:m11 |
def set_optional_scope_data(context, data): | context[CONTEXT_OPTIONAL] = data<EOL> | Set optional scope data in the context. | f12665:m12 |
def unmark_optional_scope(context): | del context[CONTEXT_OPTIONAL]<EOL> | Remove the context mark signaling the existence of an optional scope. | f12665:m13 |
def set_output_source_data(context, data): | context[CONTEXT_OUTPUT_SOURCE] = data<EOL> | Set output source data in the context. | f12665:m14 |
def has_encountered_output_source(context): | return CONTEXT_OUTPUT_SOURCE in context<EOL> | Return True if the current context has already encountered an @output_source directive. | f12665:m15 |
def validate_context_for_visiting_vertex_field(parent_location, vertex_field_name, context): | if is_in_fold_innermost_scope(context):<EOL><INDENT>raise GraphQLCompilationError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(COUNT_META_FIELD_NAME, parent_location, vertex_field_name))<EOL><DEDENT> | Ensure that the current context allows for visiting a vertex field. | f12665:m16 |
def scalar_leaf_only(operator): | def decorator(f):<EOL><INDENT>"""<STR_LIT>"""<EOL>@wraps(f)<EOL>def wrapper(filter_operation_info, context, parameters, *args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>current_operator = kwargs['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>current_operator = operator<EOL><DEDENT>if not is_leaf_type(filter_operation_info.field_type):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(current_operator, filter_operation_info))<EOL><DEDENT>return f(filter_operation_info, context, parameters, *args, **kwargs)<EOL><DEDENT>return wrapper<EOL><DEDENT>return decorator<EOL> | Ensure the filter function is only applied to scalar leaf types. | f12666:m0 |
def vertex_field_only(operator): | def decorator(f):<EOL><INDENT>"""<STR_LIT>"""<EOL>@wraps(f)<EOL>def wrapper(filter_operation_info, context, parameters, *args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>current_operator = kwargs['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>current_operator = operator<EOL><DEDENT>if not is_vertex_field_type(filter_operation_info.field_type):<EOL><INDENT>raise GraphQLCompilationError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(current_operator, filter_operation_info.field_name))<EOL><DEDENT>return f(filter_operation_info, context, parameters, *args, **kwargs)<EOL><DEDENT>return wrapper<EOL><DEDENT>return decorator<EOL> | Ensure the filter function is only applied to vertex field types. | f12666:m1 |
def takes_parameters(count): | def decorator(f):<EOL><INDENT>"""<STR_LIT>"""<EOL>@wraps(f)<EOL>def wrapper(filter_operation_info, location, context, parameters, *args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>if len(parameters) != count:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(count, len(parameters), parameters))<EOL><DEDENT>return f(filter_operation_info, location, context, parameters, *args, **kwargs)<EOL><DEDENT>return wrapper<EOL><DEDENT>return decorator<EOL> | Ensure the filter function has "count" parameters specified. | f12666:m2 |
def _represent_argument(directive_location, context, argument, inferred_type): | <EOL>argument_name = argument[<NUM_LIT:1>:]<EOL>validate_safe_string(argument_name)<EOL>if is_variable_argument(argument):<EOL><INDENT>existing_type = context['<STR_LIT>'].get(argument_name, inferred_type)<EOL>if not inferred_type.is_same_type(existing_type):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(argument, existing_type,<EOL>inferred_type))<EOL><DEDENT>context['<STR_LIT>'][argument_name] = inferred_type<EOL>return (expressions.Variable(argument, inferred_type), None)<EOL><DEDENT>elif is_tag_argument(argument):<EOL><INDENT>argument_context = context['<STR_LIT>'].get(argument_name, None)<EOL>if argument_context is None:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(argument))<EOL><DEDENT>location = argument_context['<STR_LIT:location>']<EOL>optional = argument_context['<STR_LIT>']<EOL>tag_inferred_type = argument_context['<STR_LIT:type>']<EOL>if location is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(argument_name))<EOL><DEDENT>if location.field is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(location))<EOL><DEDENT>if not inferred_type.is_same_type(tag_inferred_type):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(tag_inferred_type, inferred_type))<EOL><DEDENT>field_is_local = directive_location.at_vertex() == location.at_vertex()<EOL>non_existence_expression = None<EOL>if optional:<EOL><INDENT>if field_is_local:<EOL><INDENT>non_existence_expression = expressions.FalseLiteral<EOL><DEDENT>else:<EOL><INDENT>non_existence_expression = expressions.BinaryComposition(<EOL>u'<STR_LIT:=>',<EOL>expressions.ContextFieldExistence(location.at_vertex()),<EOL>expressions.FalseLiteral)<EOL><DEDENT><DEDENT>if field_is_local:<EOL><INDENT>representation = expressions.LocalField(argument_name)<EOL><DEDENT>else:<EOL><INDENT>representation = expressions.ContextField(location, tag_inferred_type)<EOL><DEDENT>return (representation, non_existence_expression)<EOL><DEDENT>else:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(argument))<EOL><DEDENT> | Return a two-element tuple that represents the argument to the directive being processed.
Args:
directive_location: Location where the directive is used.
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!
argument: string, the name of the argument to the directive
inferred_type: GraphQL type object specifying the inferred type of the argument
Returns:
(argument_expression, non_existence_expression)
- argument_expression: an Expression object that captures the semantics of the argument
- non_existence_expression: None or Expression object;
If the current block is not optional, this is set to None. Otherwise, it is an
expression that will evaluate to True if the argument is skipped as optional and
therefore not present, and False otherwise. | f12666:m3 |
@scalar_leaf_only(u'<STR_LIT>')<EOL>@takes_parameters(<NUM_LIT:1>)<EOL>def _process_comparison_filter_directive(filter_operation_info, location,<EOL>context, parameters, operator=None): | comparison_operators = {u'<STR_LIT:=>', u'<STR_LIT>', u'<STR_LIT:>>', u'<STR_LIT:<>', u'<STR_LIT>', u'<STR_LIT>'}<EOL>if operator not in comparison_operators:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(comparison_operators, operator))<EOL><DEDENT>filtered_field_type = filter_operation_info.field_type<EOL>filtered_field_name = filter_operation_info.field_name<EOL>argument_inferred_type = strip_non_null_from_type(filtered_field_type)<EOL>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>comparison_expression = expressions.BinaryComposition(<EOL>operator, expressions.LocalField(filtered_field_name), argument_expression)<EOL>final_expression = None<EOL>if non_existence_expression is not None:<EOL><INDENT>final_expression = expressions.BinaryComposition(<EOL>u'<STR_LIT>', non_existence_expression, comparison_expression)<EOL><DEDENT>else:<EOL><INDENT>final_expression = comparison_expression<EOL><DEDENT>return blocks.Filter(final_expression)<EOL> | Return a Filter basic block that performs the given comparison against the property field.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, containing the value to perform the comparison against;
if the parameter is optional and missing, the check will return True
operator: unicode, a comparison operator, like '=', '!=', '>=' etc.
This is a kwarg only to preserve the same positional arguments in the
function signature, to ease validation.
Returns:
a Filter basic block that performs the requested comparison | f12666:m4 |
@vertex_field_only(u'<STR_LIT>')<EOL>@takes_parameters(<NUM_LIT:1>)<EOL>def _process_has_edge_degree_filter_directive(filter_operation_info, location, context, parameters): | if isinstance(filter_operation_info.field_ast, InlineFragment):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(filter_operation_info.field_ast))<EOL><DEDENT>filtered_field_name = filter_operation_info.field_name<EOL>if filtered_field_name is None or not is_vertex_field_name(filtered_field_name):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_name))<EOL><DEDENT>if not is_vertex_field_type(filter_operation_info.field_type):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filter_operation_info))<EOL><DEDENT>argument = parameters[<NUM_LIT:0>]<EOL>if not is_variable_argument(argument):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(argument))<EOL><DEDENT>argument_inferred_type = GraphQLInt<EOL>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, argument, argument_inferred_type)<EOL>if non_existence_expression is not None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(non_existence_expression))<EOL><DEDENT>argument_is_zero = expressions.BinaryComposition(<EOL>u'<STR_LIT:=>', argument_expression, expressions.ZeroLiteral)<EOL>edge_field_is_null = expressions.BinaryComposition(<EOL>u'<STR_LIT:=>', expressions.LocalField(filtered_field_name), expressions.NullLiteral)<EOL>edge_degree_is_zero = expressions.BinaryComposition(<EOL>u'<STR_LIT>', argument_is_zero, edge_field_is_null)<EOL>edge_field_is_not_null = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField(filtered_field_name), expressions.NullLiteral)<EOL>edge_degree = expressions.UnaryTransformation(<EOL>u'<STR_LIT:size>', expressions.LocalField(filtered_field_name))<EOL>edge_degree_matches_argument = expressions.BinaryComposition(<EOL>u'<STR_LIT:=>', edge_degree, argument_expression)<EOL>edge_degree_is_non_zero = expressions.BinaryComposition(<EOL>u'<STR_LIT>', edge_field_is_not_null, edge_degree_matches_argument)<EOL>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', edge_degree_is_zero, edge_degree_is_non_zero)<EOL>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks the degree of the edge to the given vertex field.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, containing the value to check the edge degree against;
if the parameter is optional and missing, the check will return True
Returns:
a Filter basic block that performs the check | f12666:m5 |
@vertex_field_only(u'<STR_LIT>')<EOL>@takes_parameters(<NUM_LIT:1>)<EOL>def _process_name_or_alias_filter_directive(filter_operation_info, location, context, parameters): | filtered_field_type = filter_operation_info.field_type<EOL>if isinstance(filtered_field_type, GraphQLUnionType):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(filtered_field_type))<EOL><DEDENT>current_type_fields = filtered_field_type.fields<EOL>name_field = current_type_fields.get('<STR_LIT:name>', None)<EOL>alias_field = current_type_fields.get('<STR_LIT>', None)<EOL>if not name_field or not alias_field:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type))<EOL><DEDENT>name_field_type = strip_non_null_from_type(name_field.type)<EOL>alias_field_type = strip_non_null_from_type(alias_field.type)<EOL>if not isinstance(name_field_type, GraphQLScalarType):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type))<EOL><DEDENT>if not isinstance(alias_field_type, GraphQLList):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type))<EOL><DEDENT>alias_field_inner_type = strip_non_null_from_type(alias_field_type.of_type)<EOL>if alias_field_inner_type != name_field_type:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type,<EOL>name_field_type,<EOL>alias_field_inner_type))<EOL><DEDENT>argument_inferred_type = name_field_type<EOL>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>check_against_name = expressions.BinaryComposition(<EOL>u'<STR_LIT:=>', expressions.LocalField('<STR_LIT:name>'), argument_expression)<EOL>check_against_alias = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField('<STR_LIT>'), argument_expression)<EOL>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', check_against_name, check_against_alias)<EOL>if non_existence_expression is not None:<EOL><INDENT>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', non_existence_expression, filter_predicate)<EOL><DEDENT>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks for a match against an Entity's name or alias.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, containing the value to check the name or alias against;
if the parameter is optional and missing, the check will return True
Returns:
a Filter basic block that performs the check against the name or alias | f12666:m6 |
@scalar_leaf_only(u'<STR_LIT>')<EOL>@takes_parameters(<NUM_LIT:2>)<EOL>def _process_between_filter_directive(filter_operation_info, location, context, parameters): | filtered_field_type = filter_operation_info.field_type<EOL>filtered_field_name = filter_operation_info.field_name<EOL>argument_inferred_type = strip_non_null_from_type(filtered_field_type)<EOL>arg1_expression, arg1_non_existence = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>arg2_expression, arg2_non_existence = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:1>], argument_inferred_type)<EOL>lower_bound_clause = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField(filtered_field_name), arg1_expression)<EOL>if arg1_non_existence is not None:<EOL><INDENT>lower_bound_clause = expressions.BinaryComposition(<EOL>u'<STR_LIT>', arg1_non_existence, lower_bound_clause)<EOL><DEDENT>upper_bound_clause = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField(filtered_field_name), arg2_expression)<EOL>if arg2_non_existence is not None:<EOL><INDENT>upper_bound_clause = expressions.BinaryComposition(<EOL>u'<STR_LIT>', arg2_non_existence, upper_bound_clause)<EOL><DEDENT>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', lower_bound_clause, upper_bound_clause)<EOL>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks that a field is between two values, inclusive.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 2 elements, specifying the time range in which the data must lie;
if either of the elements is optional and missing,
their side of the check is assumed to be True
Returns:
a Filter basic block that performs the range check | f12666:m7 |
@scalar_leaf_only(u'<STR_LIT>')<EOL>@takes_parameters(<NUM_LIT:1>)<EOL>def _process_in_collection_filter_directive(filter_operation_info, location, context, parameters): | filtered_field_type = filter_operation_info.field_type<EOL>filtered_field_name = filter_operation_info.field_name<EOL>argument_inferred_type = GraphQLList(strip_non_null_from_type(filtered_field_type))<EOL>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', argument_expression, expressions.LocalField(filtered_field_name))<EOL>if non_existence_expression is not None:<EOL><INDENT>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', non_existence_expression, filter_predicate)<EOL><DEDENT>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks for a value's existence in a collection.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the collection existence check | f12666:m8 |
@scalar_leaf_only(u'<STR_LIT>')<EOL>@takes_parameters(<NUM_LIT:1>)<EOL>def _process_has_substring_filter_directive(filter_operation_info, location, context, parameters): | filtered_field_type = filter_operation_info.field_type<EOL>filtered_field_name = filter_operation_info.field_name<EOL>if not strip_non_null_from_type(filtered_field_type).is_same_type(GraphQLString):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type))<EOL><DEDENT>argument_inferred_type = GraphQLString<EOL>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField(filtered_field_name), argument_expression)<EOL>if non_existence_expression is not None:<EOL><INDENT>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', non_existence_expression, filter_predicate)<EOL><DEDENT>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks if the directive arg is a substring of the field.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the substring check | f12666:m9 |
@takes_parameters(<NUM_LIT:1>)<EOL>def _process_contains_filter_directive(filter_operation_info, location, context, parameters): | filtered_field_type = filter_operation_info.field_type<EOL>filtered_field_name = filter_operation_info.field_name<EOL>base_field_type = strip_non_null_from_type(filtered_field_type)<EOL>if not isinstance(base_field_type, GraphQLList):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type))<EOL><DEDENT>argument_inferred_type = strip_non_null_from_type(base_field_type.of_type)<EOL>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField(filtered_field_name), argument_expression)<EOL>if non_existence_expression is not None:<EOL><INDENT>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', non_existence_expression, filter_predicate)<EOL><DEDENT>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks if the directive arg is contained in the field.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the contains check | f12666:m10 |
@takes_parameters(<NUM_LIT:1>)<EOL>def _process_intersects_filter_directive(filter_operation_info, location, context, parameters): | filtered_field_type = filter_operation_info.field_type<EOL>filtered_field_name = filter_operation_info.field_name<EOL>argument_inferred_type = strip_non_null_from_type(filtered_field_type)<EOL>if not isinstance(argument_inferred_type, GraphQLList):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(filtered_field_type))<EOL><DEDENT>argument_expression, non_existence_expression = _represent_argument(<EOL>location, context, parameters[<NUM_LIT:0>], argument_inferred_type)<EOL>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', expressions.LocalField(filtered_field_name), argument_expression)<EOL>if non_existence_expression is not None:<EOL><INDENT>filter_predicate = expressions.BinaryComposition(<EOL>u'<STR_LIT>', non_existence_expression, filter_predicate)<EOL><DEDENT>return blocks.Filter(filter_predicate)<EOL> | Return a Filter basic block that checks if the directive arg and the field intersect.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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!
parameters: list of 1 element, specifying the collection in which the value must exist;
if the collection is optional and missing, the check will return True
Returns:
a Filter basic block that performs the intersects check | f12666:m11 |
def _get_filter_op_name_and_values(directive): | args = get_uniquely_named_objects_by_name(directive.arguments)<EOL>if '<STR_LIT>' not in args:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(directive))<EOL><DEDENT>if not isinstance(args['<STR_LIT:value>'].value, ListValue):<EOL><INDENT>raise GraphQLValidationError(u'<STR_LIT>'.format(directive))<EOL><DEDENT>op_name = args['<STR_LIT>'].value.value<EOL>operator_params = [x.value for x in args['<STR_LIT:value>'].value.values]<EOL>return (op_name, operator_params)<EOL> | Extract the (op_name, operator_params) tuple from a directive object. | f12666:m12 |
def is_filter_with_outer_scope_vertex_field_operator(directive): | if directive.name.value != '<STR_LIT>':<EOL><INDENT>return False<EOL><DEDENT>op_name, _ = _get_filter_op_name_and_values(directive)<EOL>return op_name in OUTER_SCOPE_VERTEX_FIELD_OPERATORS<EOL> | Return True if we have a filter directive whose operator applies to the outer scope. | f12666:m13 |
def process_filter_directive(filter_operation_info, location, context): | op_name, operator_params = _get_filter_op_name_and_values(filter_operation_info.directive)<EOL>non_comparison_filters = {<EOL>u'<STR_LIT>': _process_name_or_alias_filter_directive,<EOL>u'<STR_LIT>': _process_between_filter_directive,<EOL>u'<STR_LIT>': _process_in_collection_filter_directive,<EOL>u'<STR_LIT>': _process_has_substring_filter_directive,<EOL>u'<STR_LIT>': _process_contains_filter_directive,<EOL>u'<STR_LIT>': _process_intersects_filter_directive,<EOL>u'<STR_LIT>': _process_has_edge_degree_filter_directive,<EOL>}<EOL>all_recognized_filters = frozenset(non_comparison_filters.keys()) | COMPARISON_OPERATORS<EOL>if all_recognized_filters != ALL_OPERATORS:<EOL><INDENT>unrecognized_filters = ALL_OPERATORS - all_recognized_filters<EOL>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(unrecognized_filters))<EOL><DEDENT>if op_name in COMPARISON_OPERATORS:<EOL><INDENT>process_func = partial(_process_comparison_filter_directive, operator=op_name)<EOL><DEDENT>else:<EOL><INDENT>process_func = non_comparison_filters.get(op_name, None)<EOL><DEDENT>if process_func is None:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(op_name))<EOL><DEDENT>if (filter_operation_info.field_name is None and<EOL>op_name not in INNER_SCOPE_VERTEX_FIELD_OPERATORS):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(op_name))<EOL><DEDENT>fields = ((filter_operation_info.field_name,) if op_name != '<STR_LIT>'<EOL>else ('<STR_LIT:name>', '<STR_LIT>'))<EOL>context['<STR_LIT>'].record_filter_info(<EOL>location,<EOL>FilterInfo(fields=fields, op_name=op_name, args=tuple(operator_params))<EOL>)<EOL>return process_func(filter_operation_info, location, context, operator_params)<EOL> | Return a Filter basic block that corresponds to the filter operation in the directive.
Args:
filter_operation_info: FilterOperationInfo object, containing the directive and field info
of the field where the filter is to be applied.
location: Location where this filter is used.
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:
a Filter basic block that performs the requested filtering operation | f12666:m14 |
def lower_coerce_type_block_type_data(ir_blocks, type_equivalence_hints): | allowed_key_type_spec = (GraphQLInterfaceType, GraphQLObjectType)<EOL>allowed_value_type_spec = GraphQLUnionType<EOL>for key, value in six.iteritems(type_equivalence_hints):<EOL><INDENT>if (not isinstance(key, allowed_key_type_spec) or<EOL>not isinstance(value, allowed_value_type_spec)):<EOL><INDENT>msg = (u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(key.name, str(type(key)),<EOL>value.name, str(type(value))))<EOL>raise GraphQLCompilationError(msg)<EOL><DEDENT><DEDENT>equivalent_type_names = {<EOL>key.name: {x.name for x in value.types}<EOL>for key, value in six.iteritems(type_equivalence_hints)<EOL>}<EOL>new_ir_blocks = []<EOL>for block in ir_blocks:<EOL><INDENT>new_block = block<EOL>if isinstance(block, CoerceType):<EOL><INDENT>target_class = get_only_element_from_collection(block.target_class)<EOL>if target_class in equivalent_type_names:<EOL><INDENT>new_block = CoerceType(equivalent_type_names[target_class])<EOL><DEDENT><DEDENT>new_ir_blocks.append(new_block)<EOL><DEDENT>return new_ir_blocks<EOL> | Rewrite CoerceType blocks to explicitly state which types are allowed in the coercion. | f12667:m0 |
def lower_coerce_type_blocks(ir_blocks): | new_ir_blocks = []<EOL>for block in ir_blocks:<EOL><INDENT>new_block = block<EOL>if isinstance(block, CoerceType):<EOL><INDENT>predicate = BinaryComposition(<EOL>u'<STR_LIT>', Literal(list(block.target_class)), LocalField('<STR_LIT>'))<EOL>new_block = Filter(predicate)<EOL><DEDENT>new_ir_blocks.append(new_block)<EOL><DEDENT>return new_ir_blocks<EOL> | Lower CoerceType blocks into Filter blocks with a type-check predicate. | f12667:m1 |
def rewrite_filters_in_optional_blocks(ir_blocks): | new_ir_blocks = []<EOL>optional_context_depth = <NUM_LIT:0><EOL>for block in ir_blocks:<EOL><INDENT>new_block = block<EOL>if isinstance(block, CoerceType):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>elif isinstance(block, Traverse) and block.optional:<EOL><INDENT>optional_context_depth += <NUM_LIT:1><EOL><DEDENT>elif isinstance(block, Backtrack) and block.optional:<EOL><INDENT>optional_context_depth -= <NUM_LIT:1><EOL>if optional_context_depth < <NUM_LIT:0>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(ir_blocks))<EOL><DEDENT><DEDENT>elif isinstance(block, Filter) and optional_context_depth > <NUM_LIT:0>:<EOL><INDENT>null_check = BinaryComposition(u'<STR_LIT:=>', LocalField('<STR_LIT>'), NullLiteral)<EOL>new_block = Filter(BinaryComposition(u'<STR_LIT>', null_check, block.predicate))<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT>new_ir_blocks.append(new_block)<EOL><DEDENT>return new_ir_blocks<EOL> | In optional contexts, add a check for null that allows non-existent optional data through.
Optional traversals in Gremlin represent missing optional data by setting the current vertex
to null until the exit from the optional scope. Therefore, filtering and type coercions
(which should have been lowered into filters by this point) must check for null before
applying their filtering predicates. Since missing optional data isn't filtered,
the new filtering predicate should be "(it == null) || existing_predicate".
Args:
ir_blocks: list of IR blocks to lower into Gremlin-compatible form
Returns:
new list of IR blocks with this lowering step applied | f12667:m2 |
def _convert_folded_blocks(folded_ir_blocks): | new_folded_ir_blocks = []<EOL>def folded_context_visitor(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if not isinstance(expression, LocalField):<EOL><INDENT>return expression<EOL><DEDENT>return GremlinFoldedLocalField(expression.field_name)<EOL><DEDENT>for block in folded_ir_blocks:<EOL><INDENT>new_block = block<EOL>if isinstance(block, Filter):<EOL><INDENT>new_predicate = block.predicate.visit_and_update(folded_context_visitor)<EOL>new_block = GremlinFoldedFilter(new_predicate)<EOL><DEDENT>elif isinstance(block, Traverse):<EOL><INDENT>new_block = GremlinFoldedTraverse.from_traverse(block)<EOL><DEDENT>elif isinstance(block, (MarkLocation, Backtrack)):<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(type(block), block, folded_ir_blocks))<EOL><DEDENT>new_folded_ir_blocks.append(new_block)<EOL><DEDENT>return new_folded_ir_blocks<EOL> | Convert Filter/Traverse blocks and LocalField expressions within @fold to Gremlin objects. | f12667:m3 |
def lower_folded_outputs(ir_blocks): | folds, remaining_ir_blocks = extract_folds_from_ir_blocks(ir_blocks)<EOL>if not remaining_ir_blocks:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(folds, remaining_ir_blocks, ir_blocks))<EOL><DEDENT>output_block = remaining_ir_blocks[-<NUM_LIT:1>]<EOL>if not isinstance(output_block, ConstructResult):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(type(output_block), output_block, ir_blocks))<EOL><DEDENT>converted_folds = {<EOL>base_fold_location.get_location_name()[<NUM_LIT:0>]: _convert_folded_blocks(folded_ir_blocks)<EOL>for base_fold_location, folded_ir_blocks in six.iteritems(folds)<EOL>}<EOL>new_output_fields = dict()<EOL>for output_name, output_expression in six.iteritems(output_block.fields):<EOL><INDENT>new_output_expression = output_expression<EOL>if isinstance(output_expression, FoldedContextField):<EOL><INDENT>base_fold_location_name = output_expression.fold_scope_location.get_location_name()[<NUM_LIT:0>]<EOL>folded_ir_blocks = converted_folds[base_fold_location_name]<EOL>new_output_expression = GremlinFoldedContextField(<EOL>output_expression.fold_scope_location, folded_ir_blocks,<EOL>output_expression.field_type)<EOL><DEDENT>new_output_fields[output_name] = new_output_expression<EOL><DEDENT>new_ir_blocks = remaining_ir_blocks[:-<NUM_LIT:1>]<EOL>new_ir_blocks.append(ConstructResult(new_output_fields))<EOL>return new_ir_blocks<EOL> | Lower standard folded output fields into GremlinFoldedContextField objects. | f12667:m4 |
def __init__(self, fold_scope_location, folded_ir_blocks, field_type): | super(GremlinFoldedContextField, self).__init__(<EOL>fold_scope_location, folded_ir_blocks, field_type)<EOL>self.fold_scope_location = fold_scope_location<EOL>self.folded_ir_blocks = folded_ir_blocks<EOL>self.field_type = field_type<EOL>self.validate()<EOL> | Create a new GremlinFoldedContextField. | f12667:c0: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>allowed_block_types = (GremlinFoldedFilter, GremlinFoldedTraverse, Backtrack)<EOL>for block in self.folded_ir_blocks:<EOL><INDENT>if not isinstance(block, allowed_block_types):<EOL><INDENT>raise AssertionError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(type(block), self.folded_ir_blocks, allowed_block_types))<EOL><DEDENT><DEDENT>if not isinstance(self.field_type, GraphQLList):<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(self.field_type))<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> | Validate that the GremlinFoldedContextField is correctly representable. | f12667:c0:m1 |
def to_match(self): | raise NotImplementedError()<EOL> | Must never be called. | f12667:c0:m2 |
def to_gremlin(self): | self.validate()<EOL>edge_direction, edge_name = self.fold_scope_location.get_first_folded_edge()<EOL>validate_safe_string(edge_name)<EOL>inverse_direction_table = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>inverse_direction = inverse_direction_table[edge_direction]<EOL>base_location_name, _ = self.fold_scope_location.base_location.get_location_name()<EOL>validate_safe_string(base_location_name)<EOL>_, field_name = self.fold_scope_location.get_location_name()<EOL>validate_safe_string(field_name)<EOL>if not self.folded_ir_blocks:<EOL><INDENT>template = (<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>)<EOL>filter_and_traverse_data = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>template = (<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>)<EOL>filter_and_traverse_data = u'<STR_LIT:.>'.join(block.to_gremlin()<EOL>for block in self.folded_ir_blocks)<EOL><DEDENT>maybe_format = '<STR_LIT>'<EOL>inner_type = strip_non_null_from_type(self.field_type.of_type)<EOL>if GraphQLDate.is_same_type(inner_type):<EOL><INDENT>maybe_format = '<STR_LIT>' + STANDARD_DATE_FORMAT + '<STR_LIT>'<EOL><DEDENT>elif GraphQLDateTime.is_same_type(inner_type):<EOL><INDENT>maybe_format = '<STR_LIT>' + STANDARD_DATETIME_FORMAT + '<STR_LIT>'<EOL><DEDENT>template_data = {<EOL>'<STR_LIT>': base_location_name,<EOL>'<STR_LIT>': edge_direction,<EOL>'<STR_LIT>': edge_name,<EOL>'<STR_LIT>': field_name,<EOL>'<STR_LIT>': inverse_direction,<EOL>'<STR_LIT>': maybe_format,<EOL>'<STR_LIT>': filter_and_traverse_data,<EOL>}<EOL>return template.format(**template_data)<EOL> | Return a unicode object with the Gremlin representation of this expression. | f12667:c0:m3 |
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. | f12667:c1:m0 |
@classmethod<EOL><INDENT>def from_traverse(cls, traverse_block):<DEDENT> | if isinstance(traverse_block, Traverse):<EOL><INDENT>return cls(traverse_block.direction, traverse_block.edge_name)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(type(traverse_block)))<EOL><DEDENT> | Create a GremlinFoldedTraverse block as a copy of the given Traverse block. | f12667:c2:m0 |
def to_gremlin(self): | self.validate()<EOL>template_data = {<EOL>'<STR_LIT>': self.direction,<EOL>'<STR_LIT>': self.edge_name,<EOL>'<STR_LIT>': '<STR_LIT>' if self.direction == '<STR_LIT>' else '<STR_LIT>'<EOL>}<EOL>return (u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(**template_data))<EOL> | Return a unicode object with the Gremlin representation of this block. | f12667:c2:m1 |
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. | f12667:c3:m0 |
def lower_ir(ir_blocks, query_metadata_table, type_equivalence_hints=None): | sanity_check_ir_blocks_from_frontend(ir_blocks, query_metadata_table)<EOL>ir_blocks = lower_context_field_existence(ir_blocks, query_metadata_table)<EOL>ir_blocks = optimize_boolean_expression_comparisons(ir_blocks)<EOL>if type_equivalence_hints:<EOL><INDENT>ir_blocks = lower_coerce_type_block_type_data(ir_blocks, type_equivalence_hints)<EOL><DEDENT>ir_blocks = lower_coerce_type_blocks(ir_blocks)<EOL>ir_blocks = rewrite_filters_in_optional_blocks(ir_blocks)<EOL>ir_blocks = merge_consecutive_filter_clauses(ir_blocks)<EOL>ir_blocks = lower_folded_outputs(ir_blocks)<EOL>return ir_blocks<EOL> | Lower the IR into an IR form that can be represented in Gremlin queries.
Args:
ir_blocks: list of IR blocks to lower into Gremlin-compatible form
query_metadata_table: QueryMetadataTable object containing all metadata collected during
query processing, including location metadata (e.g. which locations
are folded or optional).
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:
list of IR blocks suitable for outputting as Gremlin | f12668:m0 |
def sanity_check_ir_blocks_from_frontend(ir_blocks, query_metadata_table): | if not ir_blocks:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>_sanity_check_fold_scope_locations_are_unique(ir_blocks)<EOL>_sanity_check_no_nested_folds(ir_blocks)<EOL>_sanity_check_query_root_block(ir_blocks)<EOL>_sanity_check_output_source_follower_blocks(ir_blocks)<EOL>_sanity_check_block_pairwise_constraints(ir_blocks)<EOL>_sanity_check_mark_location_preceding_optional_traverse(ir_blocks)<EOL>_sanity_check_every_location_is_marked(ir_blocks)<EOL>_sanity_check_coerce_type_outside_of_fold(ir_blocks)<EOL>_sanity_check_all_marked_locations_are_registered(ir_blocks, query_metadata_table)<EOL>_sanity_check_registered_locations_parent_locations(query_metadata_table)<EOL> | Assert that IR blocks originating from the frontend do not have nonsensical structure.
Args:
ir_blocks: list of BasicBlocks representing the IR to sanity-check
Raises:
AssertionError, if the IR has unexpected structure. If the IR produced by the front-end
cannot be successfully and correctly used to generate MATCH or Gremlin due to a bug,
this is the method that should catch the problem. | f12669:m0 |
def _sanity_check_registered_locations_parent_locations(query_metadata_table): | for location, location_info in query_metadata_table.registered_locations:<EOL><INDENT>if (location != query_metadata_table.root_location and<EOL>not query_metadata_table.root_location.is_revisited_at(location)):<EOL><INDENT>if location_info.parent_location is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(location, location_info))<EOL><DEDENT><DEDENT>if location_info.parent_location is not None:<EOL><INDENT>query_metadata_table.get_location_info(location_info.parent_location)<EOL><DEDENT><DEDENT> | Assert that all registered locations' parent locations are also registered. | f12669:m1 |
def _sanity_check_all_marked_locations_are_registered(ir_blocks, query_metadata_table): | <EOL>registered_locations = {<EOL>location<EOL>for location, _ in query_metadata_table.registered_locations<EOL>}<EOL>ir_encountered_locations = {<EOL>block.location<EOL>for block in ir_blocks<EOL>if isinstance(block, MarkLocation)<EOL>}<EOL>unregistered_locations = ir_encountered_locations - registered_locations<EOL>unencountered_locations = registered_locations - ir_encountered_locations<EOL>if unregistered_locations:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(unregistered_locations))<EOL><DEDENT>if unencountered_locations:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(unencountered_locations))<EOL><DEDENT> | Assert that all locations in MarkLocation blocks have registered and valid metadata. | f12669:m2 |
def _sanity_check_fold_scope_locations_are_unique(ir_blocks): | observed_locations = dict()<EOL>for block in ir_blocks:<EOL><INDENT>if isinstance(block, Fold):<EOL><INDENT>alternate = observed_locations.get(block.fold_scope_location, None)<EOL>if alternate is not None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(alternate, block, ir_blocks))<EOL><DEDENT>observed_locations[block.fold_scope_location] = block<EOL><DEDENT><DEDENT> | Assert that every FoldScopeLocation that exists on a Fold block is unique. | f12669:m3 |
def _sanity_check_no_nested_folds(ir_blocks): | fold_seen = False<EOL>for block in ir_blocks:<EOL><INDENT>if isinstance(block, Fold):<EOL><INDENT>if fold_seen:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>else:<EOL><INDENT>fold_seen = True<EOL><DEDENT><DEDENT>elif isinstance(block, Unfold):<EOL><INDENT>if not fold_seen:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(ir_blocks))<EOL><DEDENT>else:<EOL><INDENT>fold_seen = False<EOL><DEDENT><DEDENT><DEDENT> | Assert that there are no nested Fold contexts, and that every Fold has a matching Unfold. | f12669:m4 |
def _sanity_check_query_root_block(ir_blocks): | if not isinstance(ir_blocks[<NUM_LIT:0>], QueryRoot):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>for block in ir_blocks[<NUM_LIT:1>:]:<EOL><INDENT>if isinstance(block, QueryRoot):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT><DEDENT> | Assert that QueryRoot is always the first block, and only the first block. | f12669:m5 |
def _sanity_check_construct_result_block(ir_blocks): | if not isinstance(ir_blocks[-<NUM_LIT:1>], ConstructResult):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>for block in ir_blocks[:-<NUM_LIT:1>]:<EOL><INDENT>if isinstance(block, ConstructResult):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(ir_blocks))<EOL><DEDENT><DEDENT> | Assert that ConstructResult is always the last block, and only the last block. | f12669:m6 |
def _sanity_check_output_source_follower_blocks(ir_blocks): | seen_output_source = False<EOL>for block in ir_blocks:<EOL><INDENT>if isinstance(block, OutputSource):<EOL><INDENT>seen_output_source = True<EOL><DEDENT>elif seen_output_source:<EOL><INDENT>if isinstance(block, (Backtrack, Traverse, Recurse)):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(ir_blocks))<EOL><DEDENT><DEDENT><DEDENT> | Ensure there are no Traverse / Backtrack / Recurse blocks after an OutputSource block. | f12669:m7 |
def _sanity_check_block_pairwise_constraints(ir_blocks): | for first_block, second_block in pairwise(ir_blocks):<EOL><INDENT>if isinstance(first_block, MarkLocation) and isinstance(second_block, Filter):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>if isinstance(first_block, MarkLocation) and isinstance(second_block, MarkLocation):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>if isinstance(first_block, Traverse) and first_block.optional:<EOL><INDENT>if not isinstance(second_block, (MarkLocation, CoerceType, Filter)):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT><DEDENT>if isinstance(first_block, Backtrack) and first_block.optional:<EOL><INDENT>if not isinstance(second_block, MarkLocation):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT><DEDENT>if isinstance(second_block, Recurse):<EOL><INDENT>if not (isinstance(first_block, MarkLocation) or isinstance(first_block, Backtrack)):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT><DEDENT><DEDENT> | Assert that adjacent blocks obey all invariants. | f12669:m8 |
def _sanity_check_mark_location_preceding_optional_traverse(ir_blocks): | <EOL>_, new_ir_blocks = extract_folds_from_ir_blocks(ir_blocks)<EOL>for first_block, second_block in pairwise(new_ir_blocks):<EOL><INDENT>if isinstance(second_block, Traverse) and second_block.optional:<EOL><INDENT>if not isinstance(first_block, MarkLocation):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT><DEDENT><DEDENT> | Assert that optional Traverse blocks are preceded by a MarkLocation. | f12669:m9 |
def _sanity_check_every_location_is_marked(ir_blocks): | <EOL>found_start_block = False<EOL>mark_location_blocks_count = <NUM_LIT:0><EOL>start_interval_types = (QueryRoot, Traverse, Recurse, Fold)<EOL>end_interval_types = (Backtrack, ConstructResult, Recurse, Traverse, Unfold)<EOL>for block in ir_blocks:<EOL><INDENT>if isinstance(block, end_interval_types) and found_start_block:<EOL><INDENT>found_start_block = False<EOL>if mark_location_blocks_count != <NUM_LIT:1>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(mark_location_blocks_count, ir_blocks))<EOL><DEDENT><DEDENT>if isinstance(block, MarkLocation):<EOL><INDENT>mark_location_blocks_count += <NUM_LIT:1><EOL><DEDENT>elif isinstance(block, start_interval_types):<EOL><INDENT>found_start_block = True<EOL>mark_location_blocks_count = <NUM_LIT:0><EOL><DEDENT><DEDENT> | Ensure that every new location is marked with a MarkLocation block. | f12669:m10 |
def _sanity_check_coerce_type_outside_of_fold(ir_blocks): | is_in_fold = False<EOL>for first_block, second_block in pairwise(ir_blocks):<EOL><INDENT>if isinstance(first_block, Fold):<EOL><INDENT>is_in_fold = True<EOL><DEDENT>if not is_in_fold and isinstance(first_block, CoerceType):<EOL><INDENT>if not isinstance(second_block, (MarkLocation, Filter)):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT><DEDENT>if isinstance(second_block, Unfold):<EOL><INDENT>is_in_fold = False<EOL><DEDENT><DEDENT> | Ensure that CoerceType not in a @fold are followed by a MarkLocation or Filter block. | f12669:m11 |
def merge_consecutive_filter_clauses(ir_blocks): | if not ir_blocks:<EOL><INDENT>return ir_blocks<EOL><DEDENT>new_ir_blocks = [ir_blocks[<NUM_LIT:0>]]<EOL>for block in ir_blocks[<NUM_LIT:1>:]:<EOL><INDENT>last_block = new_ir_blocks[-<NUM_LIT:1>]<EOL>if isinstance(last_block, Filter) and isinstance(block, Filter):<EOL><INDENT>new_ir_blocks[-<NUM_LIT:1>] = Filter(<EOL>BinaryComposition(u'<STR_LIT>', last_block.predicate, block.predicate))<EOL><DEDENT>else:<EOL><INDENT>new_ir_blocks.append(block)<EOL><DEDENT><DEDENT>return new_ir_blocks<EOL> | Merge consecutive Filter(x), Filter(y) blocks into Filter(x && y) block. | f12670:m0 |
def lower_context_field_existence(ir_blocks, query_metadata_table): | def regular_visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if not isinstance(expression, ContextFieldExistence):<EOL><INDENT>return expression<EOL><DEDENT>location_type = query_metadata_table.get_location_info(expression.location).type<EOL>return BinaryComposition(<EOL>u'<STR_LIT>',<EOL>ContextField(expression.location, location_type),<EOL>NullLiteral)<EOL><DEDENT>def construct_result_visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if not isinstance(expression, ContextFieldExistence):<EOL><INDENT>return expression<EOL><DEDENT>location_type = query_metadata_table.get_location_info(expression.location).type<EOL>return BinaryComposition(<EOL>u'<STR_LIT>',<EOL>OutputContextVertex(expression.location, location_type),<EOL>NullLiteral)<EOL><DEDENT>new_ir_blocks = []<EOL>for block in ir_blocks:<EOL><INDENT>new_block = None<EOL>if isinstance(block, ConstructResult):<EOL><INDENT>new_block = block.visit_and_update_expressions(construct_result_visitor_fn)<EOL><DEDENT>else:<EOL><INDENT>new_block = block.visit_and_update_expressions(regular_visitor_fn)<EOL><DEDENT>new_ir_blocks.append(new_block)<EOL><DEDENT>return new_ir_blocks<EOL> | Lower ContextFieldExistence expressions into lower-level expressions. | f12670:m1 |
def optimize_boolean_expression_comparisons(ir_blocks): | operator_inverses = {<EOL>u'<STR_LIT:=>': u'<STR_LIT>',<EOL>u'<STR_LIT>': u'<STR_LIT:=>',<EOL>}<EOL>def visitor_fn(expression):<EOL><INDENT>"""<STR_LIT>"""<EOL>if not isinstance(expression, BinaryComposition):<EOL><INDENT>return expression<EOL><DEDENT>left_is_binary_composition = isinstance(expression.left, BinaryComposition)<EOL>right_is_binary_composition = isinstance(expression.right, BinaryComposition)<EOL>if not left_is_binary_composition and not right_is_binary_composition:<EOL><INDENT>return expression<EOL><DEDENT>identity_literal = None <EOL>inverse_literal = None <EOL>if expression.operator == u'<STR_LIT:=>':<EOL><INDENT>identity_literal = TrueLiteral<EOL>inverse_literal = FalseLiteral<EOL><DEDENT>elif expression.operator == u'<STR_LIT>':<EOL><INDENT>identity_literal = FalseLiteral<EOL>inverse_literal = TrueLiteral<EOL><DEDENT>else:<EOL><INDENT>return expression<EOL><DEDENT>expression_to_rewrite = None<EOL>if expression.left == identity_literal and right_is_binary_composition:<EOL><INDENT>return expression.right<EOL><DEDENT>elif expression.right == identity_literal and left_is_binary_composition:<EOL><INDENT>return expression.left<EOL><DEDENT>elif expression.left == inverse_literal and right_is_binary_composition:<EOL><INDENT>expression_to_rewrite = expression.right<EOL><DEDENT>elif expression.right == inverse_literal and left_is_binary_composition:<EOL><INDENT>expression_to_rewrite = expression.left<EOL><DEDENT>if expression_to_rewrite is None:<EOL><INDENT>return expression<EOL><DEDENT>elif expression_to_rewrite.operator not in operator_inverses:<EOL><INDENT>return expression<EOL><DEDENT>else:<EOL><INDENT>return BinaryComposition(<EOL>operator_inverses[expression_to_rewrite.operator],<EOL>expression_to_rewrite.left,<EOL>expression_to_rewrite.right)<EOL><DEDENT><DEDENT>new_ir_blocks = []<EOL>for block in ir_blocks:<EOL><INDENT>new_block = block.visit_and_update_expressions(visitor_fn)<EOL>new_ir_blocks.append(new_block)<EOL><DEDENT>return new_ir_blocks<EOL> | Optimize comparisons of a boolean binary comparison expression against a boolean literal.
Rewriting example:
BinaryComposition(
'=',
BinaryComposition('!=', something, NullLiteral)
False)
The above is rewritten into:
BinaryComposition('=', something, NullLiteral)
Args:
ir_blocks: list of basic block objects
Returns:
a new list of basic block objects, with the optimization applied | f12670:m2 |
def extract_folds_from_ir_blocks(ir_blocks): | folds = dict()<EOL>remaining_ir_blocks = []<EOL>current_folded_blocks = []<EOL>in_fold_location = None<EOL>for block in ir_blocks:<EOL><INDENT>if isinstance(block, Fold):<EOL><INDENT>if in_fold_location is not None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(current_folded_blocks, remaining_ir_blocks,<EOL>ir_blocks))<EOL><DEDENT>in_fold_location = block.fold_scope_location<EOL><DEDENT>elif isinstance(block, Unfold):<EOL><INDENT>if in_fold_location is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(current_folded_blocks, remaining_ir_blocks,<EOL>ir_blocks))<EOL><DEDENT>folds[in_fold_location] = current_folded_blocks<EOL>current_folded_blocks = []<EOL>in_fold_location = None<EOL><DEDENT>else:<EOL><INDENT>if in_fold_location is not None:<EOL><INDENT>current_folded_blocks.append(block)<EOL><DEDENT>else:<EOL><INDENT>remaining_ir_blocks.append(block)<EOL><DEDENT><DEDENT><DEDENT>return folds, remaining_ir_blocks<EOL> | Extract all @fold data from the IR blocks, and cut the folded IR blocks out of the IR.
Args:
ir_blocks: list of IR blocks to extract fold data from
Returns:
tuple (folds, remaining_ir_blocks):
- folds: dict of FoldScopeLocation -> list of IR blocks corresponding to that @fold scope.
The list does not contain Fold or Unfold blocks.
- remaining_ir_blocks: list of IR blocks that were not part of a Fold-Unfold section. | f12670:m3 |
def extract_optional_location_root_info(ir_blocks): | complex_optional_roots = []<EOL>location_to_optional_roots = dict()<EOL>in_optional_root_locations = []<EOL>encountered_traverse_within_optional = []<EOL>_, non_folded_ir_blocks = extract_folds_from_ir_blocks(ir_blocks)<EOL>preceding_location = None<EOL>for current_block in non_folded_ir_blocks:<EOL><INDENT>if len(in_optional_root_locations) > <NUM_LIT:0> and isinstance(current_block, (Traverse, Recurse)):<EOL><INDENT>encountered_traverse_within_optional[-<NUM_LIT:1>] = True<EOL><DEDENT>if isinstance(current_block, Traverse) and current_block.optional:<EOL><INDENT>if preceding_location is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>.format(current_block, non_folded_ir_blocks))<EOL><DEDENT>in_optional_root_locations.append(preceding_location)<EOL>encountered_traverse_within_optional.append(False)<EOL><DEDENT>elif isinstance(current_block, EndOptional):<EOL><INDENT>if len(in_optional_root_locations) == <NUM_LIT:0>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(ir_blocks))<EOL><DEDENT>if encountered_traverse_within_optional[-<NUM_LIT:1>]:<EOL><INDENT>complex_optional_roots.append(in_optional_root_locations[-<NUM_LIT:1>])<EOL><DEDENT>in_optional_root_locations.pop()<EOL>encountered_traverse_within_optional.pop()<EOL><DEDENT>elif isinstance(current_block, MarkLocation):<EOL><INDENT>preceding_location = current_block.location<EOL>if len(in_optional_root_locations) != <NUM_LIT:0>:<EOL><INDENT>optional_root_locations_stack = tuple(in_optional_root_locations)<EOL>location_to_optional_roots[current_block.location] = optional_root_locations_stack<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return complex_optional_roots, location_to_optional_roots<EOL> | Construct a mapping from locations within @optional to their correspoding optional Traverse.
Args:
ir_blocks: list of IR blocks to extract optional data from
Returns:
tuple (complex_optional_roots, location_to_optional_roots):
complex_optional_roots: list of @optional locations (location immmediately preceding
an @optional Traverse) that expand vertex fields
location_to_optional_roots: dict mapping from location -> optional_roots where location is
within some number of @optionals and optional_roots is a list
of optional root locations preceding the successive @optional
scopes within which the location resides | f12670:m4 |
def extract_simple_optional_location_info(<EOL>ir_blocks, complex_optional_roots, location_to_optional_roots): | <EOL>location_to_preceding_optional_root_iteritems = six.iteritems({<EOL>location: optional_root_locations_stack[-<NUM_LIT:1>]<EOL>for location, optional_root_locations_stack in six.iteritems(location_to_optional_roots)<EOL>})<EOL>simple_optional_root_to_inner_location = {<EOL>optional_root_location: inner_location<EOL>for inner_location, optional_root_location in location_to_preceding_optional_root_iteritems<EOL>if optional_root_location not in complex_optional_roots<EOL>}<EOL>simple_optional_root_locations = set(simple_optional_root_to_inner_location.keys())<EOL>_, non_folded_ir_blocks = extract_folds_from_ir_blocks(ir_blocks)<EOL>simple_optional_root_info = {}<EOL>preceding_location = None<EOL>for current_block in non_folded_ir_blocks:<EOL><INDENT>if isinstance(current_block, MarkLocation):<EOL><INDENT>preceding_location = current_block.location<EOL><DEDENT>elif isinstance(current_block, Traverse) and current_block.optional:<EOL><INDENT>if preceding_location in simple_optional_root_locations:<EOL><INDENT>inner_location = simple_optional_root_to_inner_location[preceding_location]<EOL>inner_location_name, _ = inner_location.get_location_name()<EOL>simple_optional_info_dict = {<EOL>'<STR_LIT>': inner_location_name,<EOL>'<STR_LIT>': current_block.get_field_name(),<EOL>}<EOL>simple_optional_root_info[preceding_location] = simple_optional_info_dict<EOL><DEDENT><DEDENT><DEDENT>return simple_optional_root_info<EOL> | Construct a map from simple optional locations to their inner location and traversed edge.
Args:
ir_blocks: list of IR blocks to extract optional data from
complex_optional_roots: list of @optional locations (location immmediately preceding
an @optional traverse) that expand vertex fields
location_to_optional_roots: dict mapping from location -> optional_roots where location is
within some number of @optionals and optional_roots is a list
of optional root locations preceding the successive @optional
scopes within which the location resides
Returns:
dict mapping from simple_optional_root_location -> dict containing keys
- 'inner_location_name': Location object correspoding to the unique MarkLocation present
within a simple optional (one that does not expand vertex fields)
scope
- 'edge_field': string representing the optional edge being traversed
where simple_optional_root_to_inner_location is the location preceding the @optional scope | f12670:m5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.