# Path: muninn/function.py
# class Prototype(object):
#     def __init__(self, name, argument_types=(), return_type=None):
#         self.name = name
#         self.argument_types = argument_types
#         self.return_type = return_type
# 
#         self._id = "%s(%s)" % (self.name, ",".join(imap(lambda type: type.name(), self.argument_types)))
#         if self.return_type is not None:
#             self._id += " " + self.return_type.name()
# 
#     @property
#     def id(self):
#         return self._id
# 
#     @property
#     def arity(self):
#         return len(self.argument_types)
# 
#     def _argument_types_equal(self, other):
#         if self.arity != other.arity:
#             return False
# 
#         for type_self, type_other in izip(self.argument_types, other.argument_types):
#             if type_self is not type_other:
#                 return False
#         return True
# 
#     def __eq__(self, other):
#         return self.name == other.name and self._argument_types_equal(other) and self.return_type is other.return_type
# 
#     def __ne__(self, other):
#         return not self == other
# 
#     def __str__(self):
#         return self.id
# 
#     def __repr__(self):
#         return "Prototype(name=%r, argument_types=%r, return_type=%r)" % (self.name, self.argument_types,
#                                                                           self.return_type)
# 
#     def __hash__(self):
#         return hash(self.id)
# 
# Path: muninn/language.py
# def parse_and_analyze(text, namespace_schemas={}, parameters={}):
#     return analyze(parse(text), namespace_schemas, parameters)
# 
# class Literal(AbstractSyntaxTreeNode):
#     def __init__(self, value):
#         self.value = value
# 
#     def __str__(self):
#         return "(%s %s)" % (type(self).__name__, self.value)
# 
# class Name(AbstractSyntaxTreeNode):
#     def __init__(self, value):
#         self.value = value
# 
#     def __str__(self):
#         return "(%s %s)" % (type(self).__name__, self.value)
# 
# Path: muninn/visitor.py
# class Visitor(object):
#     def visit(self, visitable, *args, **kwargs):
#         for type_ in inspect.getmro(type(visitable)):
#             try:
#                 visit_func = getattr(self, "visit_%s" % type_.__name__)
#             except AttributeError:
#                 pass
#             else:
#                 return visit_func(visitable, *args, **kwargs)
# 
#         try:
#             visit_func = getattr(self, "default")
#         except AttributeError:
#             pass
#         else:
#             return visit_func(visitable, *args, **kwargs)


import collections
import inspect
import re
    from collections.abc import MutableMapping
    from collections import MutableMapping
from muninn.exceptions import *
from muninn.function import Prototype
from muninn.language import parse_and_analyze, Literal, Name
from muninn.schema import *
from muninn.visitor import Visitor
            rewriter_func = self._rewriter_table[visitable.prototype]
        except KeyError:
            raise Error("function not supported by backend: %s" % visitable.prototype)

        # sub-query
        prototype = visitable.prototype
        if (prototype.name in ('is_source_of', 'is_derived_from') and prototype.argument_types and
                prototype.argument_types[0].name() == 'boolean'):

            visitor = _WhereExpressionVisitor(self._rewriter_table, self._column_name, self._named_placeholder,
                                              self._root_visitor)
            where_expr, where_parameters, where_namespaces = visitor.visit(visitable.arguments[0])

            if 'core' in where_namespaces:
                where_namespaces.remove('core')

            return rewriter_func(where_expr, where_namespaces)

        # non-sub-query
        else:
            arguments = []
            for type_, argument in zip(visitable.prototype.argument_types, visitable.arguments):
                where = super(_WhereExpressionVisitor, self).visit(argument)
                if isinstance(argument, Literal) and argument.type is UUID and type_ is Boolean:
                    where = ("(uuid = %s)" % where)
                arguments.append(where)
            sql_expr = rewriter_func(*arguments)

            # SQL-improved NULL checking (e.g., field != "blah" also matches NULL)
            if visitable.name in ('==', '!=', '~='):

# Path: muninn/function.py
# class Prototype(object):
#     def __init__(self, name, argument_types=(), return_type=None):
#         self.name = name
#         self.argument_types = argument_types
#         self.return_type = return_type
# 
#         self._id = "%s(%s)" % (self.name, ",".join(imap(lambda type: type.name(), self.argument_types)))
#         if self.return_type is not None:
#             self._id += " " + self.return_type.name()
# 
#     @property
#     def id(self):
#         return self._id
# 
#     @property
#     def arity(self):
#         return len(self.argument_types)
# 
#     def _argument_types_equal(self, other):
#         if self.arity != other.arity:
#             return False
# 
#         for type_self, type_other in izip(self.argument_types, other.argument_types):
#             if type_self is not type_other:
#                 return False
#         return True
# 
#     def __eq__(self, other):
#         return self.name == other.name and self._argument_types_equal(other) and self.return_type is other.return_type
# 
#     def __ne__(self, other):
#         return not self == other
# 
#     def __str__(self):
#         return self.id
# 
#     def __repr__(self):
#         return "Prototype(name=%r, argument_types=%r, return_type=%r)" % (self.name, self.argument_types,
#                                                                           self.return_type)
# 
#     def __hash__(self):
#         return hash(self.id)
# 
# Path: muninn/language.py
# def parse_and_analyze(text, namespace_schemas={}, parameters={}):
#     return analyze(parse(text), namespace_schemas, parameters)
# 
# class Literal(AbstractSyntaxTreeNode):
#     def __init__(self, value):
#         self.value = value
# 
#     def __str__(self):
#         return "(%s %s)" % (type(self).__name__, self.value)
# 
# class Name(AbstractSyntaxTreeNode):
#     def __init__(self, value):
#         self.value = value
# 
#     def __str__(self):
#         return "(%s %s)" % (type(self).__name__, self.value)
# 
# Path: muninn/visitor.py
# class Visitor(object):
#     def visit(self, visitable, *args, **kwargs):
#         for type_ in inspect.getmro(type(visitable)):
#             try:
#                 visit_func = getattr(self, "visit_%s" % type_.__name__)
#             except AttributeError:
#                 pass
#             else:
#                 return visit_func(visitable, *args, **kwargs)
# 
#         try:
#             visit_func = getattr(self, "default")
#         except AttributeError:
#             pass
#         else:
#             return visit_func(visitable, *args, **kwargs)


# Path: muninn/database/sql.py
import collections
import inspect
import re
    from collections.abc import MutableMapping
    from collections import MutableMapping
from muninn.exceptions import *
from muninn.function import Prototype
from muninn.language import parse_and_analyze, Literal, Name
from muninn.schema import *
from muninn.visitor import Visitor

            rewriter_func = self._rewriter_table[visitable.prototype]
        except KeyError:
            raise Error("function not supported by backend: %s" % visitable.prototype)

        # sub-query
        prototype = visitable.prototype
        if (prototype.name in ('is_source_of', 'is_derived_from') and prototype.argument_types and
                prototype.argument_types[0].name() == 'boolean'):

            visitor = _WhereExpressionVisitor(self._rewriter_table, self._column_name, self._named_placeholder,
                                              self._root_visitor)
            where_expr, where_parameters, where_namespaces = visitor.visit(visitable.arguments[0])

            if 'core' in where_namespaces:
                where_namespaces.remove('core')

            return rewriter_func(where_expr, where_namespaces)

        # non-sub-query
        else:
            arguments = []
            for type_, argument in zip(visitable.prototype.argument_types, visitable.arguments):
                where = super(_WhereExpressionVisitor, self).visit(argument)
                if isinstance(argument, Literal) and argument.type is UUID and type_ is Boolean:
                    where = ("(uuid = %s)" % where)
                arguments.append(where)
            sql_expr = rewriter_func(*arguments)

            # SQL-improved NULL checking (e.g., field != "blah" also matches NULL)
            if visitable.name in ('==', '!=', '~='):

                if (isinstance(visitable.arguments[0], Name) and isinstance(visitable.arguments[1], Literal)):