'Returns a CQL query that can be used to recreate this function.
If `formatted` is set to :const:`True`, extra whitespace will
be added to make the query more readable.'

def as_cql_query(self, formatted=False):

    sep = ('\n                ' if formatted else '    ')
    keyspace = protect_name(self.keyspace_name)
    name = protect_name(self.name)
    selected_cols = ('*' if self.include_all_columns else ',    '.join((protect_name(col.name) for col in self.columns.values())))
    base_table = protect_name(self.base_table_name)
    where_clause = self.where_clause
    part_key = ',    '.join((protect_name(col.name) for col in self.partition_key))
    if (len(self.partition_key) > 1):
        pk = ('((%s)' % part_key)
    else:
        pk = ('(%s' % part_key)
    if self.clustering_key:
        pk += (',    %s' % ',    '.join((protect_name(col.name) for col in self.clustering_key)))
    pk += ')'
    properties = TableMetadataV3._property_string(formatted, self.clustering_key, self.options)
    ret = ('CREATE    MATERIALIZED    VIEW    %(keyspace)s.%(name)s    AS%(sep)sSELECT    %(selected_cols)s%(sep)sFROM    %(keyspace)s.%(base_table)s%(sep)sWHERE    %(where_clause)s%(sep)sPRIMARY    KEY    %(pk)s%(sep)sWITH    %(properties)s' % locals())
    if self.extensions:
        registry = _RegisteredExtensionType._extension_registry
        for k in (six.viewkeys(registry) & self.extensions):
            ext = registry[k]
            cql = ext.after_table_cql(self, k, self.extensions[k])
            if cql:
                ret += ('\n\n%s' % (cql,))
    return ret
