# Path: muninn/_compat.py
# def dictkeys(d):
#     return list(d.keys())
# 
# def dictvalues(d):
#     return list(d.values())
# 
# def is_python2_unicode(x):
#     return False
# 
# 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/struct.py
# class Struct(object):
#     def __init__(self, data=None):
#         super(Struct, self).__init__()
#         if data is not None:
#             for key in data:
#                 if type(data[key]) == dict:
#                     self[key] = Struct(data[key])
#                 else:
#                     self[key] = data[key]
# 
#     def __getitem__(self, key):
#         try:
#             return getattr(self, key)
#         except AttributeError:
#             raise KeyError(key)
# 
#     def __setitem__(self, key, value):
#         setattr(self, key, value)
# 
#     def __delitem__(self, key):
#         try:
#             return delattr(self, key)
#         except AttributeError:
#             raise KeyError(key)
# 
#     def __contains__(self, key):
#         return hasattr(self, key)
# 
#     def __len__(self):
#         return len(vars(self))
# 
#     def __iter__(self):
#         return iter(vars(self))
# 
#     def __repr__(self):
#         return "Struct(%r)" % vars(self)
# 
#     def update(self, other):
#         '''update a struct, using the same semantics as dict.update()'''
#         for key in other:
#             other_item = other[key]
#             if isinstance(other_item, Struct):
#                 if key not in self:
#                     self[key] = Struct()
#                 else:
#                     if not isinstance(self[key], Struct):
#                         raise Error('Incompatible structs: %s vs %s' % (self, other))
#                 self[key].update(other_item)
#             else:
#                 self[key] = other_item


from muninn._compat import dictkeys, dictvalues, is_python2_unicode
from muninn.exceptions import *
from muninn.function import Prototype
from muninn.schema import *
from muninn.struct import Struct
import re
import functools
import inspect
    import psycopg2
    import psycopg2.extensions
    import psycopg2.extras
    import pg8000
import muninn.config as config
import muninn.database.sql as sql
import muninn.database.ewkb as ewkb
import muninn.geometry as geometry
        def is_derived_from_subquery(where_expr, where_namespaces):
            joins = ''
            for namespace in where_namespaces:
                joins = "%s INNER JOIN %s USING (uuid)" % (joins, self._table_name(namespace))

            return "{core}.uuid in (SELECT {link}.uuid FROM {core} {joins} " \
                "INNER JOIN {link} on {link}.source_uuid = {core}.uuid WHERE {where})".format(
                    core=self._core_table_name, link=self._link_table_name, joins=joins,
                    where=where_expr)

        rewriter_table[Prototype("is_derived_from", (Boolean,), Boolean)] = is_derived_from_subquery

        rewriter_table[Prototype("has_tag", (Text,), Boolean)] = \
            lambda arg0: "EXISTS (SELECT 1 FROM %s WHERE uuid = %s.uuid AND tag = (%s))" % \
            (self._tag_table_name, self._core_table_name, arg0)

        rewriter_table[Prototype("now", (), Timestamp)] = \
            sql.as_is("now() AT TIME ZONE 'UTC'")

        def is_defined_rewriter(arg):
            namespace_name = arg.split('.')
            if len(namespace_name) == 1:
                return 'EXISTS (SELECT 1 FROM %s WHERE uuid = %s.uuid)' % \
                    (self._table_name(arg), self._core_table_name)
            else:
                return "(%s) IS NOT NULL" % arg

        rewriter_table[Prototype("is_defined", (Long,), Boolean)] = is_defined_rewriter
        rewriter_table[Prototype("is_defined", (Integer,), Boolean)] = is_defined_rewriter
        rewriter_table[Prototype("is_defined", (Real,), Boolean)] = is_defined_rewriter

# Path: muninn/_compat.py
# def dictkeys(d):
#     return list(d.keys())
# 
# def dictvalues(d):
#     return list(d.values())
# 
# def is_python2_unicode(x):
#     return False
# 
# 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/struct.py
# class Struct(object):
#     def __init__(self, data=None):
#         super(Struct, self).__init__()
#         if data is not None:
#             for key in data:
#                 if type(data[key]) == dict:
#                     self[key] = Struct(data[key])
#                 else:
#                     self[key] = data[key]
# 
#     def __getitem__(self, key):
#         try:
#             return getattr(self, key)
#         except AttributeError:
#             raise KeyError(key)
# 
#     def __setitem__(self, key, value):
#         setattr(self, key, value)
# 
#     def __delitem__(self, key):
#         try:
#             return delattr(self, key)
#         except AttributeError:
#             raise KeyError(key)
# 
#     def __contains__(self, key):
#         return hasattr(self, key)
# 
#     def __len__(self):
#         return len(vars(self))
# 
#     def __iter__(self):
#         return iter(vars(self))
# 
#     def __repr__(self):
#         return "Struct(%r)" % vars(self)
# 
#     def update(self, other):
#         '''update a struct, using the same semantics as dict.update()'''
#         for key in other:
#             other_item = other[key]
#             if isinstance(other_item, Struct):
#                 if key not in self:
#                     self[key] = Struct()
#                 else:
#                     if not isinstance(self[key], Struct):
#                         raise Error('Incompatible structs: %s vs %s' % (self, other))
#                 self[key].update(other_item)
#             else:
#                 self[key] = other_item


# Path: muninn/database/postgresql.py
from muninn._compat import dictkeys, dictvalues, is_python2_unicode
from muninn.exceptions import *
from muninn.function import Prototype
from muninn.schema import *
from muninn.struct import Struct
import re
import functools
import inspect
    import psycopg2
    import psycopg2.extensions
    import psycopg2.extras
    import pg8000
import muninn.config as config
import muninn.database.sql as sql
import muninn.database.ewkb as ewkb
import muninn.geometry as geometry

        def is_derived_from_subquery(where_expr, where_namespaces):
            joins = ''
            for namespace in where_namespaces:
                joins = "%s INNER JOIN %s USING (uuid)" % (joins, self._table_name(namespace))

            return "{core}.uuid in (SELECT {link}.uuid FROM {core} {joins} " \
                "INNER JOIN {link} on {link}.source_uuid = {core}.uuid WHERE {where})".format(
                    core=self._core_table_name, link=self._link_table_name, joins=joins,
                    where=where_expr)

        rewriter_table[Prototype("is_derived_from", (Boolean,), Boolean)] = is_derived_from_subquery

        rewriter_table[Prototype("has_tag", (Text,), Boolean)] = \
            lambda arg0: "EXISTS (SELECT 1 FROM %s WHERE uuid = %s.uuid AND tag = (%s))" % \
            (self._tag_table_name, self._core_table_name, arg0)

        rewriter_table[Prototype("now", (), Timestamp)] = \
            sql.as_is("now() AT TIME ZONE 'UTC'")

        def is_defined_rewriter(arg):
            namespace_name = arg.split('.')
            if len(namespace_name) == 1:
                return 'EXISTS (SELECT 1 FROM %s WHERE uuid = %s.uuid)' % \
                    (self._table_name(arg), self._core_table_name)
            else:
                return "(%s) IS NOT NULL" % arg

        rewriter_table[Prototype("is_defined", (Long,), Boolean)] = is_defined_rewriter
        rewriter_table[Prototype("is_defined", (Integer,), Boolean)] = is_defined_rewriter
        rewriter_table[Prototype("is_defined", (Real,), Boolean)] = is_defined_rewriter

        rewriter_table[Prototype("is_defined", (Boolean,), Boolean)] = is_defined_rewriter