def lower_folded_coerce_types_into_filter_blocks(folded_ir_blocks): """Lower CoerceType blocks into "INSTANCEOF" Filter blocks. Indended for folded IR blocks.""" new_folded_ir_blocks = [] for block in folded_ir_blocks: new_block = block if isinstance(block, CoerceType): coerce_type_target = block.target_class if len(coerce_type_target) != 1: raise AssertionError( u'Unexpected "coerce_type_target" for MATCH query: ' u'{}'.format(coerce_type_target)) coerce_type_target = funcy.first(coerce_type_target) # INSTANCEOF requires the target class to be passed in as a string, # so we make the target class a string literal. new_predicate = BinaryComposition(u'INSTANCEOF', LocalField('@this'), Literal(coerce_type_target)) new_block = Filter(new_predicate) new_folded_ir_blocks.append(new_block) return new_folded_ir_blocks
def _coerce_block_to_filter_block(coerce_type_block, where_block): """Create an "INSTANCEOF" Filter block from a CoerceType block.""" coerce_type_target = coerce_type_block.target_class if len(coerce_type_target) != 1: raise AssertionError( u'Unexpected "coerce_type_target" for MATCH query: ' u'{}'.format(coerce_type_target)) coerce_type_target = funcy.first(coerce_type_target) # INSTANCEOF requires the target class to be passed in as a string, # so we make the target class a string literal. new_predicate = BinaryComposition(u'INSTANCEOF', LocalField('@this'), Literal(coerce_type_target)) if where_block: # There was already a Filter block -- we'll merge the two predicates together. new_predicate = BinaryComposition(u'&&', new_predicate, where_block.predicate) return Filter(new_predicate)