# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():


from collections import defaultdict
from copy import deepcopy
from datetime import datetime, timedelta
from sqlalchemy import Table, and_, asc, func
from sqlalchemy.sql import select
from plenario.database import redshift_base as redshift_base, redshift_session as r_session
    if not valid_columns:
        raise ValueError('Your query returns no results. You have specified '
                         'filters which are likely contradictory (for example '
                         'filtering on a sensor which doesn\'t have the feature '
                         'you are aggregating for)')

    redshift_base.metadata.reflect()
    obs_table = redshift_base.metadata.tables[network.name + '__' + feature.name]

    # Generate the necessary select statements and datetime delimiters
    selects = _generate_aggregate_selects(obs_table,
                                          valid_columns,
                                          agg_fn,
                                          agg_unit)

    # Generate the base query
    query = select(selects).where(and_(
        obs_table.c.datetime >= start_dt,
        obs_table.c.datetime < end_dt,
        obs_table.c.node_id == node.id
    ))

    # If sensors are specified, filter on them
    if target_sensors:
        query = query.where(obs_table.c.sensor.in_(target_sensors))

    # Format the query
    query = query.group_by('time_bucket').order_by(asc('time_bucket'))

    # Execute and format the result

# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():


# Path: plenario/sensor_network/api/sensor_aggregate_functions.py
from collections import defaultdict
from copy import deepcopy
from datetime import datetime, timedelta
from sqlalchemy import Table, and_, asc, func
from sqlalchemy.sql import select
from plenario.database import redshift_base as redshift_base, redshift_session as r_session

    if not valid_columns:
        raise ValueError('Your query returns no results. You have specified '
                         'filters which are likely contradictory (for example '
                         'filtering on a sensor which doesn\'t have the feature '
                         'you are aggregating for)')

    redshift_base.metadata.reflect()
    obs_table = redshift_base.metadata.tables[network.name + '__' + feature.name]

    # Generate the necessary select statements and datetime delimiters
    selects = _generate_aggregate_selects(obs_table,
                                          valid_columns,
                                          agg_fn,
                                          agg_unit)

    # Generate the base query
    query = select(selects).where(and_(
        obs_table.c.datetime >= start_dt,
        obs_table.c.datetime < end_dt,
        obs_table.c.node_id == node.id
    ))

    # If sensors are specified, filter on them
    if target_sensors:
        query = query.where(obs_table.c.sensor.in_(target_sensors))

    # Format the query
    query = query.group_by('time_bucket').order_by(asc('time_bucket'))

    # Execute and format the result

    results = r_session.execute(query).fetchall()