     RETURNS text
 AS $$
     SELECT datasource.name || '_' || entitytype.name || '_' || trend.granularity_to_text($1.granularity)
-        FROM directory.datasource, directory.entitytype
-        WHERE datasource.id = $1.datasource_id AND entitytype.id = $1.entitytype_id;
+    FROM directory.datasource, directory.entitytype
+    WHERE datasource.id = $1.datasource_id AND entitytype.id = $1.entitytype_id;
 $$ LANGUAGE SQL STABLE STRICT;
 
 

$$ LANGUAGE SQL STABLE;


CREATE FUNCTION trend.infer_trendstore_type(trend.trendstore)
    RETURNS trend.storetype
AS $$
    SELECT
        CASE relkind
            WHEN 'r' THEN 'table'::trend.storetype
            WHEN 'v' THEN 'view'::trend.storetype
            ELSE NULL
        END
    FROM pg_class
    WHERE relname = trend.to_base_table_name($1);
$$ LANGUAGE SQL STABLE;


CREATE FUNCTION trend.get_column_names(table_name character varying)
    RETURNS character varying[]
AS $$
    SELECT array_agg(format('%I', a.attname)::character varying) FROM pg_class c
        JOIN pg_attribute a ON a.attrelid = c.oid
    WHERE c.relname = $1 AND a.attnum >= 0 AND NOT a.attisdropped;
$$ LANGUAGE SQL STABLE;


DECLARE
    max_modified timestamp with time zone;
BEGIN
    EXECUTE format(
        'SELECT max(modified) FROM trend.%I WHERE timestamp = $1',
        trend.to_base_table_name($1)
    ) INTO max_modified USING $2;

    RETURN max_modified;
END;
