Skip to content

prefect.server.database.query_components

AioSqliteQueryComponents

Bases: BaseQueryComponents

Source code in src/prefect/server/database/query_components.py
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
class AioSqliteQueryComponents(BaseQueryComponents):
    # --- Sqlite-specific SqlAlchemy bindings

    def insert(self, obj) -> sqlite.Insert:
        return sqlite.insert(obj)

    def greatest(self, *values):
        return sa.func.max(*values)

    def least(self, *values):
        return sa.func.min(*values)

    # --- Sqlite-specific JSON handling

    @property
    def uses_json_strings(self):
        return True

    def cast_to_json(self, json_obj):
        return sa.func.json(json_obj)

    def build_json_object(self, *args):
        return sa.func.json_object(*args)

    def json_arr_agg(self, json_array):
        return sa.func.json_group_array(json_array)

    # --- Sqlite-optimized subqueries

    def make_timestamp_intervals(
        self,
        start_time: datetime.datetime,
        end_time: datetime.datetime,
        interval: datetime.timedelta,
    ):
        from prefect.server.utilities.database import Timestamp

        # validate inputs
        start_time = pendulum.instance(start_time)
        end_time = pendulum.instance(end_time)
        assert isinstance(interval, datetime.timedelta)

        return (
            sa.text(
                r"""
                -- recursive CTE to mimic the behavior of `generate_series`,
                -- which is only available as a compiled extension
                WITH RECURSIVE intervals(interval_start, interval_end, counter) AS (
                    VALUES(
                        strftime('%Y-%m-%d %H:%M:%f000', :start_time),
                        strftime('%Y-%m-%d %H:%M:%f000', :start_time, :interval),
                        1
                        )

                    UNION ALL

                    SELECT interval_end, strftime('%Y-%m-%d %H:%M:%f000', interval_end, :interval), counter + 1
                    FROM intervals
                    -- subtract interval because recursive where clauses are effectively evaluated on a t-1 lag
                    WHERE
                        interval_start < strftime('%Y-%m-%d %H:%M:%f000', :end_time, :negative_interval)
                        -- don't compute more than 500 intervals
                        AND counter < 500
                )
                SELECT * FROM intervals
                """
            )
            .bindparams(
                start_time=str(start_time),
                end_time=str(end_time),
                interval=f"+{interval.total_seconds()} seconds",
                negative_interval=f"-{interval.total_seconds()} seconds",
            )
            .columns(interval_start=Timestamp(), interval_end=Timestamp())
        )

    def set_state_id_on_inserted_flow_runs_statement(
        self,
        fr_model,
        frs_model,
        inserted_flow_run_ids,
        insert_flow_run_states,
    ):
        """Given a list of flow run ids and associated states, set the state_id
        to the appropriate state for all flow runs"""
        # sqlite requires a correlated subquery to update from another table
        subquery = (
            sa.select(frs_model.id)
            .where(
                frs_model.flow_run_id == fr_model.id,
                frs_model.id.in_([r["id"] for r in insert_flow_run_states]),
            )
            .limit(1)
            .scalar_subquery()
        )
        stmt = (
            sa.update(fr_model)
            .where(
                fr_model.id.in_(inserted_flow_run_ids),
            )
            .values(state_id=subquery)
            # no need to synchronize as these flow runs are entirely new
            .execution_options(synchronize_session=False)
        )
        return stmt

    async def get_flow_run_notifications_from_queue(
        self, session: AsyncSession, limit: int
    ) -> List:
        """
        Sqlalchemy has no support for DELETE RETURNING in sqlite (as of May 2022)
        so instead we issue two queries; one to get queued notifications and a second to delete
        them. This *could* introduce race conditions if multiple queue workers are
        running.
        """

        notification_details_stmt = (
            sa.select(
                orm_models.FlowRunNotificationQueue.id.label("queue_id"),
                orm_models.FlowRunNotificationPolicy.id.label(
                    "flow_run_notification_policy_id"
                ),
                orm_models.FlowRunNotificationPolicy.message_template.label(
                    "flow_run_notification_policy_message_template"
                ),
                orm_models.FlowRunNotificationPolicy.block_document_id,
                orm_models.Flow.id.label("flow_id"),
                orm_models.Flow.name.label("flow_name"),
                orm_models.FlowRun.id.label("flow_run_id"),
                orm_models.FlowRun.name.label("flow_run_name"),
                orm_models.FlowRun.parameters.label("flow_run_parameters"),
                orm_models.FlowRunState.type.label("flow_run_state_type"),
                orm_models.FlowRunState.name.label("flow_run_state_name"),
                orm_models.FlowRunState.timestamp.label("flow_run_state_timestamp"),
                orm_models.FlowRunState.message.label("flow_run_state_message"),
            )
            .select_from(orm_models.FlowRunNotificationQueue)
            .join(
                orm_models.FlowRunNotificationPolicy,
                orm_models.FlowRunNotificationQueue.flow_run_notification_policy_id
                == orm_models.FlowRunNotificationPolicy.id,
            )
            .join(
                orm_models.FlowRunState,
                orm_models.FlowRunNotificationQueue.flow_run_state_id
                == orm_models.FlowRunState.id,
            )
            .join(
                orm_models.FlowRun,
                orm_models.FlowRunState.flow_run_id == orm_models.FlowRun.id,
            )
            .join(
                orm_models.Flow,
                orm_models.FlowRun.flow_id == orm_models.Flow.id,
            )
            .order_by(orm_models.FlowRunNotificationQueue.updated)
            .limit(limit)
        )

        result = await session.execute(notification_details_stmt)
        notifications = result.fetchall()

        # delete the notifications
        delete_stmt = (
            sa.delete(orm_models.FlowRunNotificationQueue)
            .where(
                orm_models.FlowRunNotificationQueue.id.in_(
                    [n.queue_id for n in notifications]
                )
            )
            .execution_options(synchronize_session="fetch")
        )

        await session.execute(delete_stmt)

        return notifications

    async def _handle_filtered_block_document_ids(
        self, session, filtered_block_documents_query
    ):
        """
        On SQLite, including the filtered block document parameters confuses the
        compiler and it passes positional parameters in the wrong order (it is
        unclear why; SQLalchemy manual compilation works great. Switching to
        `named` paramstyle also works but fails elsewhere in the codebase). To
        resolve this, we materialize the filtered id query into a literal set of
        IDs rather than leaving it as a SQL select.
        """
        result = await session.execute(filtered_block_documents_query)
        return result.scalars().all()

    def _get_scheduled_flow_runs_join(
        self,
        work_queue_query,
        limit_per_queue: Optional[int],
        scheduled_before: Optional[datetime.datetime],
    ):
        # precompute for readability
        scheduled_before_clause = (
            orm_models.FlowRun.next_scheduled_start_time <= scheduled_before
            if scheduled_before is not None
            else True
        )

        # select scheduled flow runs, ordered by scheduled start time per queue
        scheduled_flow_runs = (
            sa.select(
                (
                    sa.func.row_number()
                    .over(
                        partition_by=[orm_models.FlowRun.work_queue_name],
                        order_by=orm_models.FlowRun.next_scheduled_start_time,
                    )
                    .label("rank")
                ),
                orm_models.FlowRun,
            )
            .where(
                orm_models.FlowRun.state_type == "SCHEDULED",
                scheduled_before_clause,
            )
            .subquery("scheduled_flow_runs")
        )

        # sqlite short-circuits the `min` comparison on nulls, so we use `999999`
        # as an "unlimited" limit.
        limit = 999999 if limit_per_queue is None else limit_per_queue

        # in the join, only keep flow runs whose rank is less than or equal to the
        # available slots for each queue
        join_criteria = sa.and_(
            self._flow_run_work_queue_join_clause(
                scheduled_flow_runs.c, orm_models.WorkQueue
            ),
            scheduled_flow_runs.c.rank
            <= sa.func.min(
                sa.func.coalesce(work_queue_query.c.available_slots, limit), limit
            ),
        )
        return scheduled_flow_runs, join_criteria

    # -------------------------------------------------------
    # Workers
    # -------------------------------------------------------

    @property
    def _get_scheduled_flow_runs_from_work_pool_template_path(self):
        """
        Template for the query to get scheduled flow runs from a work pool
        """
        return "sqlite/get-runs-from-worker-queues.sql.jinja"

    async def flow_run_graph_v2(
        self,
        session: AsyncSession,
        flow_run_id: UUID,
        since: datetime.datetime,
        max_nodes: int,
        max_artifacts: int,
    ) -> Graph:
        """Returns the query that selects all of the nodes and edges for a flow run
        graph (version 2)."""
        result = await session.execute(
            sa.select(
                sa.func.coalesce(
                    orm_models.FlowRun.start_time,
                    orm_models.FlowRun.expected_start_time,
                ),
                orm_models.FlowRun.end_time,
            ).where(
                orm_models.FlowRun.id == flow_run_id,
            )
        )
        try:
            start_time, end_time = result.one()
        except NoResultFound:
            raise ObjectNotFoundError(f"Flow run {flow_run_id} not found")

        query = sa.text(
            """
            WITH
            edges AS (
                SELECT  CASE
                            WHEN subflow.id IS NOT NULL THEN 'flow-run'
                            ELSE 'task-run'
                        END as kind,
                        COALESCE(subflow.id, task_run.id) as id,
                        COALESCE(flow.name || ' / ' || subflow.name, task_run.name) as label,
                        COALESCE(subflow.state_type, task_run.state_type) as state_type,
                        COALESCE(
                            subflow.start_time,
                            subflow.expected_start_time,
                            task_run.start_time,
                            task_run.expected_start_time
                        ) as start_time,
                        COALESCE(
                            subflow.end_time,
                            task_run.end_time,
                            CASE
                                WHEN task_run.state_type = 'COMPLETED'
                                    THEN task_run.expected_start_time
                                ELSE NULL
                            END
                        ) as end_time,
                        json_extract(argument.value, '$.id') as parent,
                        input.key = '__parents__' as has_encapsulating_task
                FROM    task_run
                        LEFT JOIN json_each(task_run.task_inputs) as input ON true
                        LEFT JOIN json_each(input.value) as argument ON true
                        LEFT JOIN flow_run as subflow
                                ON subflow.parent_task_run_id = task_run.id
                        LEFT JOIN flow
                                ON flow.id = subflow.flow_id
                WHERE   task_run.flow_run_id = :flow_run_id AND
                        task_run.state_type <> 'PENDING' AND
                        COALESCE(
                            subflow.start_time,
                            subflow.expected_start_time,
                            task_run.start_time,
                            task_run.expected_start_time
                        ) IS NOT NULL

                -- the order here is important to speed up building the two sets of
                -- edges in the with_parents and with_children CTEs below
                ORDER BY COALESCE(subflow.id, task_run.id)
            ),
            with_encapsulating AS (
                SELECT  children.id,
                        group_concat(parents.id) as encapsulating_ids
                FROM    edges as children
                        INNER JOIN edges as parents
                                ON parents.id = children.parent
                WHERE children.has_encapsulating_task IS TRUE
                GROUP BY children.id
            ),
            with_parents AS (
                SELECT  children.id,
                        group_concat(parents.id) as parent_ids
                FROM    edges as children
                        INNER JOIN edges as parents
                                ON parents.id = children.parent
                WHERE children.has_encapsulating_task is FALSE OR children.has_encapsulating_task IS NULL
                GROUP BY children.id
            ),
            with_children AS (
                SELECT  parents.id,
                        group_concat(children.id) as child_ids
                FROM    edges as parents
                        INNER JOIN edges as children
                                ON children.parent = parents.id
                WHERE children.has_encapsulating_task IS FALSE OR children.has_encapsulating_task IS NULL
                GROUP BY parents.id
            ),
            nodes AS (
                SELECT  DISTINCT
                        edges.id,
                        edges.kind,
                        edges.id,
                        edges.label,
                        edges.state_type,
                        edges.start_time,
                        edges.end_time,
                        with_parents.parent_ids,
                        with_children.child_ids,
                        with_encapsulating.encapsulating_ids
                FROM    edges
                        LEFT JOIN with_parents
                                ON with_parents.id = edges.id
                        LEFT JOIN with_children
                                ON with_children.id = edges.id
                        LEFT JOIN with_encapsulating
                                ON with_encapsulating.id = edges.id
            )
            SELECT  kind,
                    id,
                    label,
                    state_type,
                    start_time,
                    end_time,
                    parent_ids,
                    child_ids,
                    encapsulating_ids
            FROM    nodes
            WHERE   end_time IS NULL OR end_time >= :since
            ORDER BY start_time, end_time
            LIMIT :max_nodes
            ;
        """
        )

        # SQLite needs this to be a Python datetime object
        since = datetime.datetime(
            since.year,
            since.month,
            since.day,
            since.hour,
            since.minute,
            since.second,
            since.microsecond,
            tzinfo=since.tzinfo,
        )

        query = query.bindparams(
            sa.bindparam("flow_run_id", value=str(flow_run_id)),
            sa.bindparam("since", value=since),
            sa.bindparam("max_nodes", value=max_nodes + 1),
        )

        results = await session.execute(query)

        graph_artifacts = await self._get_flow_run_graph_artifacts(
            session, flow_run_id, max_artifacts
        )
        graph_states = await self._get_flow_run_graph_states(session, flow_run_id)

        nodes: List[Tuple[UUID, Node]] = []
        root_node_ids: List[UUID] = []

        for row in results:
            if not row.parent_ids:
                root_node_ids.append(row.id)

            # With SQLite, some of the values are returned as strings rather than
            # native Python objects, as they would be from PostgreSQL.  These functions
            # help smooth over those differences.

            def edges(
                value: Union[str, Sequence[UUID], Sequence[str], None],
            ) -> List[UUID]:
                if not value:
                    return []
                if isinstance(value, str):
                    return [Edge(id=id) for id in value.split(",")]
                return [Edge(id=id) for id in value]

            def time(
                value: Union[str, datetime.datetime, None],
            ) -> Optional[pendulum.DateTime]:
                if not value:
                    return None
                if isinstance(value, str):
                    return cast(pendulum.DateTime, pendulum.parse(value))
                return pendulum.instance(value)

            nodes.append(
                (
                    row.id,
                    Node(
                        kind=row.kind,
                        id=row.id,
                        label=row.label,
                        state_type=row.state_type,
                        start_time=time(row.start_time),
                        end_time=time(row.end_time),
                        parents=edges(row.parent_ids),
                        children=edges(row.child_ids),
                        encapsulating=edges(row.encapsulating_ids),
                        artifacts=graph_artifacts.get(UUID(row.id), []),
                    ),
                )
            )

            if len(nodes) > max_nodes:
                raise FlowRunGraphTooLarge(
                    f"The graph of flow run {flow_run_id} has more than "
                    f"{max_nodes} nodes."
                )

        return Graph(
            start_time=start_time,
            end_time=end_time,
            root_node_ids=root_node_ids,
            nodes=nodes,
            artifacts=graph_artifacts.get(None, []),
            states=graph_states,
        )

flow_run_graph_v2(session, flow_run_id, since, max_nodes, max_artifacts) async

Returns the query that selects all of the nodes and edges for a flow run graph (version 2).

Source code in src/prefect/server/database/query_components.py
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
async def flow_run_graph_v2(
    self,
    session: AsyncSession,
    flow_run_id: UUID,
    since: datetime.datetime,
    max_nodes: int,
    max_artifacts: int,
) -> Graph:
    """Returns the query that selects all of the nodes and edges for a flow run
    graph (version 2)."""
    result = await session.execute(
        sa.select(
            sa.func.coalesce(
                orm_models.FlowRun.start_time,
                orm_models.FlowRun.expected_start_time,
            ),
            orm_models.FlowRun.end_time,
        ).where(
            orm_models.FlowRun.id == flow_run_id,
        )
    )
    try:
        start_time, end_time = result.one()
    except NoResultFound:
        raise ObjectNotFoundError(f"Flow run {flow_run_id} not found")

    query = sa.text(
        """
        WITH
        edges AS (
            SELECT  CASE
                        WHEN subflow.id IS NOT NULL THEN 'flow-run'
                        ELSE 'task-run'
                    END as kind,
                    COALESCE(subflow.id, task_run.id) as id,
                    COALESCE(flow.name || ' / ' || subflow.name, task_run.name) as label,
                    COALESCE(subflow.state_type, task_run.state_type) as state_type,
                    COALESCE(
                        subflow.start_time,
                        subflow.expected_start_time,
                        task_run.start_time,
                        task_run.expected_start_time
                    ) as start_time,
                    COALESCE(
                        subflow.end_time,
                        task_run.end_time,
                        CASE
                            WHEN task_run.state_type = 'COMPLETED'
                                THEN task_run.expected_start_time
                            ELSE NULL
                        END
                    ) as end_time,
                    json_extract(argument.value, '$.id') as parent,
                    input.key = '__parents__' as has_encapsulating_task
            FROM    task_run
                    LEFT JOIN json_each(task_run.task_inputs) as input ON true
                    LEFT JOIN json_each(input.value) as argument ON true
                    LEFT JOIN flow_run as subflow
                            ON subflow.parent_task_run_id = task_run.id
                    LEFT JOIN flow
                            ON flow.id = subflow.flow_id
            WHERE   task_run.flow_run_id = :flow_run_id AND
                    task_run.state_type <> 'PENDING' AND
                    COALESCE(
                        subflow.start_time,
                        subflow.expected_start_time,
                        task_run.start_time,
                        task_run.expected_start_time
                    ) IS NOT NULL

            -- the order here is important to speed up building the two sets of
            -- edges in the with_parents and with_children CTEs below
            ORDER BY COALESCE(subflow.id, task_run.id)
        ),
        with_encapsulating AS (
            SELECT  children.id,
                    group_concat(parents.id) as encapsulating_ids
            FROM    edges as children
                    INNER JOIN edges as parents
                            ON parents.id = children.parent
            WHERE children.has_encapsulating_task IS TRUE
            GROUP BY children.id
        ),
        with_parents AS (
            SELECT  children.id,
                    group_concat(parents.id) as parent_ids
            FROM    edges as children
                    INNER JOIN edges as parents
                            ON parents.id = children.parent
            WHERE children.has_encapsulating_task is FALSE OR children.has_encapsulating_task IS NULL
            GROUP BY children.id
        ),
        with_children AS (
            SELECT  parents.id,
                    group_concat(children.id) as child_ids
            FROM    edges as parents
                    INNER JOIN edges as children
                            ON children.parent = parents.id
            WHERE children.has_encapsulating_task IS FALSE OR children.has_encapsulating_task IS NULL
            GROUP BY parents.id
        ),
        nodes AS (
            SELECT  DISTINCT
                    edges.id,
                    edges.kind,
                    edges.id,
                    edges.label,
                    edges.state_type,
                    edges.start_time,
                    edges.end_time,
                    with_parents.parent_ids,
                    with_children.child_ids,
                    with_encapsulating.encapsulating_ids
            FROM    edges
                    LEFT JOIN with_parents
                            ON with_parents.id = edges.id
                    LEFT JOIN with_children
                            ON with_children.id = edges.id
                    LEFT JOIN with_encapsulating
                            ON with_encapsulating.id = edges.id
        )
        SELECT  kind,
                id,
                label,
                state_type,
                start_time,
                end_time,
                parent_ids,
                child_ids,
                encapsulating_ids
        FROM    nodes
        WHERE   end_time IS NULL OR end_time >= :since
        ORDER BY start_time, end_time
        LIMIT :max_nodes
        ;
    """
    )

    # SQLite needs this to be a Python datetime object
    since = datetime.datetime(
        since.year,
        since.month,
        since.day,
        since.hour,
        since.minute,
        since.second,
        since.microsecond,
        tzinfo=since.tzinfo,
    )

    query = query.bindparams(
        sa.bindparam("flow_run_id", value=str(flow_run_id)),
        sa.bindparam("since", value=since),
        sa.bindparam("max_nodes", value=max_nodes + 1),
    )

    results = await session.execute(query)

    graph_artifacts = await self._get_flow_run_graph_artifacts(
        session, flow_run_id, max_artifacts
    )
    graph_states = await self._get_flow_run_graph_states(session, flow_run_id)

    nodes: List[Tuple[UUID, Node]] = []
    root_node_ids: List[UUID] = []

    for row in results:
        if not row.parent_ids:
            root_node_ids.append(row.id)

        # With SQLite, some of the values are returned as strings rather than
        # native Python objects, as they would be from PostgreSQL.  These functions
        # help smooth over those differences.

        def edges(
            value: Union[str, Sequence[UUID], Sequence[str], None],
        ) -> List[UUID]:
            if not value:
                return []
            if isinstance(value, str):
                return [Edge(id=id) for id in value.split(",")]
            return [Edge(id=id) for id in value]

        def time(
            value: Union[str, datetime.datetime, None],
        ) -> Optional[pendulum.DateTime]:
            if not value:
                return None
            if isinstance(value, str):
                return cast(pendulum.DateTime, pendulum.parse(value))
            return pendulum.instance(value)

        nodes.append(
            (
                row.id,
                Node(
                    kind=row.kind,
                    id=row.id,
                    label=row.label,
                    state_type=row.state_type,
                    start_time=time(row.start_time),
                    end_time=time(row.end_time),
                    parents=edges(row.parent_ids),
                    children=edges(row.child_ids),
                    encapsulating=edges(row.encapsulating_ids),
                    artifacts=graph_artifacts.get(UUID(row.id), []),
                ),
            )
        )

        if len(nodes) > max_nodes:
            raise FlowRunGraphTooLarge(
                f"The graph of flow run {flow_run_id} has more than "
                f"{max_nodes} nodes."
            )

    return Graph(
        start_time=start_time,
        end_time=end_time,
        root_node_ids=root_node_ids,
        nodes=nodes,
        artifacts=graph_artifacts.get(None, []),
        states=graph_states,
    )

get_flow_run_notifications_from_queue(session, limit) async

Sqlalchemy has no support for DELETE RETURNING in sqlite (as of May 2022) so instead we issue two queries; one to get queued notifications and a second to delete them. This could introduce race conditions if multiple queue workers are running.

Source code in src/prefect/server/database/query_components.py
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
async def get_flow_run_notifications_from_queue(
    self, session: AsyncSession, limit: int
) -> List:
    """
    Sqlalchemy has no support for DELETE RETURNING in sqlite (as of May 2022)
    so instead we issue two queries; one to get queued notifications and a second to delete
    them. This *could* introduce race conditions if multiple queue workers are
    running.
    """

    notification_details_stmt = (
        sa.select(
            orm_models.FlowRunNotificationQueue.id.label("queue_id"),
            orm_models.FlowRunNotificationPolicy.id.label(
                "flow_run_notification_policy_id"
            ),
            orm_models.FlowRunNotificationPolicy.message_template.label(
                "flow_run_notification_policy_message_template"
            ),
            orm_models.FlowRunNotificationPolicy.block_document_id,
            orm_models.Flow.id.label("flow_id"),
            orm_models.Flow.name.label("flow_name"),
            orm_models.FlowRun.id.label("flow_run_id"),
            orm_models.FlowRun.name.label("flow_run_name"),
            orm_models.FlowRun.parameters.label("flow_run_parameters"),
            orm_models.FlowRunState.type.label("flow_run_state_type"),
            orm_models.FlowRunState.name.label("flow_run_state_name"),
            orm_models.FlowRunState.timestamp.label("flow_run_state_timestamp"),
            orm_models.FlowRunState.message.label("flow_run_state_message"),
        )
        .select_from(orm_models.FlowRunNotificationQueue)
        .join(
            orm_models.FlowRunNotificationPolicy,
            orm_models.FlowRunNotificationQueue.flow_run_notification_policy_id
            == orm_models.FlowRunNotificationPolicy.id,
        )
        .join(
            orm_models.FlowRunState,
            orm_models.FlowRunNotificationQueue.flow_run_state_id
            == orm_models.FlowRunState.id,
        )
        .join(
            orm_models.FlowRun,
            orm_models.FlowRunState.flow_run_id == orm_models.FlowRun.id,
        )
        .join(
            orm_models.Flow,
            orm_models.FlowRun.flow_id == orm_models.Flow.id,
        )
        .order_by(orm_models.FlowRunNotificationQueue.updated)
        .limit(limit)
    )

    result = await session.execute(notification_details_stmt)
    notifications = result.fetchall()

    # delete the notifications
    delete_stmt = (
        sa.delete(orm_models.FlowRunNotificationQueue)
        .where(
            orm_models.FlowRunNotificationQueue.id.in_(
                [n.queue_id for n in notifications]
            )
        )
        .execution_options(synchronize_session="fetch")
    )

    await session.execute(delete_stmt)

    return notifications

set_state_id_on_inserted_flow_runs_statement(fr_model, frs_model, inserted_flow_run_ids, insert_flow_run_states)

Given a list of flow run ids and associated states, set the state_id to the appropriate state for all flow runs

Source code in src/prefect/server/database/query_components.py
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
def set_state_id_on_inserted_flow_runs_statement(
    self,
    fr_model,
    frs_model,
    inserted_flow_run_ids,
    insert_flow_run_states,
):
    """Given a list of flow run ids and associated states, set the state_id
    to the appropriate state for all flow runs"""
    # sqlite requires a correlated subquery to update from another table
    subquery = (
        sa.select(frs_model.id)
        .where(
            frs_model.flow_run_id == fr_model.id,
            frs_model.id.in_([r["id"] for r in insert_flow_run_states]),
        )
        .limit(1)
        .scalar_subquery()
    )
    stmt = (
        sa.update(fr_model)
        .where(
            fr_model.id.in_(inserted_flow_run_ids),
        )
        .values(state_id=subquery)
        # no need to synchronize as these flow runs are entirely new
        .execution_options(synchronize_session=False)
    )
    return stmt

AsyncPostgresQueryComponents

Bases: BaseQueryComponents

Source code in src/prefect/server/database/query_components.py
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
class AsyncPostgresQueryComponents(BaseQueryComponents):
    # --- Postgres-specific SqlAlchemy bindings

    def insert(self, obj) -> postgresql.Insert:
        return postgresql.insert(obj)

    def greatest(self, *values):
        return sa.func.greatest(*values)

    def least(self, *values):
        return sa.func.least(*values)

    # --- Postgres-specific JSON handling

    @property
    def uses_json_strings(self):
        return False

    def cast_to_json(self, json_obj):
        return json_obj

    def build_json_object(self, *args):
        return sa.func.jsonb_build_object(*args)

    def json_arr_agg(self, json_array):
        return sa.func.jsonb_agg(json_array)

    # --- Postgres-optimized subqueries

    def make_timestamp_intervals(
        self,
        start_time: datetime.datetime,
        end_time: datetime.datetime,
        interval: datetime.timedelta,
    ):
        # validate inputs
        start_time = pendulum.instance(start_time)
        end_time = pendulum.instance(end_time)
        assert isinstance(interval, datetime.timedelta)
        return (
            sa.select(
                sa.literal_column("dt").label("interval_start"),
                (sa.literal_column("dt") + interval).label("interval_end"),
            )
            .select_from(
                sa.func.generate_series(start_time, end_time, interval).alias("dt")
            )
            .where(sa.literal_column("dt") < end_time)
            # grab at most 500 intervals
            .limit(500)
        )

    def set_state_id_on_inserted_flow_runs_statement(
        self,
        fr_model,
        frs_model,
        inserted_flow_run_ids,
        insert_flow_run_states,
    ):
        """Given a list of flow run ids and associated states, set the state_id
        to the appropriate state for all flow runs"""
        # postgres supports `UPDATE ... FROM` syntax
        stmt = (
            sa.update(fr_model)
            .where(
                fr_model.id.in_(inserted_flow_run_ids),
                frs_model.flow_run_id == fr_model.id,
                frs_model.id.in_([r["id"] for r in insert_flow_run_states]),
            )
            .values(state_id=frs_model.id)
            # no need to synchronize as these flow runs are entirely new
            .execution_options(synchronize_session=False)
        )
        return stmt

    async def get_flow_run_notifications_from_queue(
        self, session: AsyncSession, limit: int
    ) -> List:
        # including this as a subquery in the where clause of the
        # `queued_notifications` statement below, leads to errors where the limit
        # is not respected if it is 1. pulling this out into a CTE statement
        # prevents this. see link for more details:
        # https://www.postgresql.org/message-id/16497.1553640836%40sss.pgh.pa.us
        queued_notifications_ids = (
            sa.select(orm_models.FlowRunNotificationQueue.id)
            .select_from(orm_models.FlowRunNotificationQueue)
            .order_by(orm_models.FlowRunNotificationQueue.updated)
            .limit(limit)
            .with_for_update(skip_locked=True)
        ).cte("queued_notifications_ids")

        queued_notifications = (
            sa.delete(orm_models.FlowRunNotificationQueue)
            .returning(
                orm_models.FlowRunNotificationQueue.id,
                orm_models.FlowRunNotificationQueue.flow_run_notification_policy_id,
                orm_models.FlowRunNotificationQueue.flow_run_state_id,
            )
            .where(
                orm_models.FlowRunNotificationQueue.id.in_(
                    sa.select(queued_notifications_ids)
                )
            )
            .cte("queued_notifications")
        )

        notification_details_stmt = (
            sa.select(
                queued_notifications.c.id.label("queue_id"),
                orm_models.FlowRunNotificationPolicy.id.label(
                    "flow_run_notification_policy_id"
                ),
                orm_models.FlowRunNotificationPolicy.message_template.label(
                    "flow_run_notification_policy_message_template"
                ),
                orm_models.FlowRunNotificationPolicy.block_document_id,
                orm_models.Flow.id.label("flow_id"),
                orm_models.Flow.name.label("flow_name"),
                orm_models.FlowRun.id.label("flow_run_id"),
                orm_models.FlowRun.name.label("flow_run_name"),
                orm_models.FlowRun.parameters.label("flow_run_parameters"),
                orm_models.FlowRunState.type.label("flow_run_state_type"),
                orm_models.FlowRunState.name.label("flow_run_state_name"),
                orm_models.FlowRunState.timestamp.label("flow_run_state_timestamp"),
                orm_models.FlowRunState.message.label("flow_run_state_message"),
            )
            .select_from(queued_notifications)
            .join(
                orm_models.FlowRunNotificationPolicy,
                queued_notifications.c.flow_run_notification_policy_id
                == orm_models.FlowRunNotificationPolicy.id,
            )
            .join(
                orm_models.FlowRunState,
                queued_notifications.c.flow_run_state_id == orm_models.FlowRunState.id,
            )
            .join(
                orm_models.FlowRun,
                orm_models.FlowRunState.flow_run_id == orm_models.FlowRun.id,
            )
            .join(
                orm_models.Flow,
                orm_models.FlowRun.flow_id == orm_models.Flow.id,
            )
        )

        result = await session.execute(notification_details_stmt)
        return result.fetchall()

    @property
    def _get_scheduled_flow_runs_from_work_pool_template_path(self):
        """
        Template for the query to get scheduled flow runs from a work pool
        """
        return "postgres/get-runs-from-worker-queues.sql.jinja"

    async def flow_run_graph_v2(
        self,
        session: AsyncSession,
        flow_run_id: UUID,
        since: datetime.datetime,
        max_nodes: int,
        max_artifacts: int,
    ) -> Graph:
        """Returns the query that selects all of the nodes and edges for a flow run
        graph (version 2)."""
        result = await session.execute(
            sa.select(
                sa.func.coalesce(
                    orm_models.FlowRun.start_time,
                    orm_models.FlowRun.expected_start_time,
                ),
                orm_models.FlowRun.end_time,
            ).where(
                orm_models.FlowRun.id == flow_run_id,
            )
        )
        try:
            start_time, end_time = result.one()
        except NoResultFound:
            raise ObjectNotFoundError(f"Flow run {flow_run_id} not found")

        query = sa.text(
            """
            WITH
            edges AS (
                SELECT  CASE
                            WHEN subflow.id IS NOT NULL THEN 'flow-run'
                            ELSE 'task-run'
                        END as kind,
                        COALESCE(subflow.id, task_run.id) as id,
                        COALESCE(flow.name || ' / ' || subflow.name, task_run.name) as label,
                        COALESCE(subflow.state_type, task_run.state_type) as state_type,
                        COALESCE(
                            subflow.start_time,
                            subflow.expected_start_time,
                            task_run.start_time,
                            task_run.expected_start_time
                        ) as start_time,
                        COALESCE(
                            subflow.end_time,
                            task_run.end_time,
                            CASE
                                WHEN task_run.state_type = 'COMPLETED'
                                    THEN task_run.expected_start_time
                                ELSE NULL
                            END
                        ) as end_time,
                        (argument->>'id')::uuid as parent,
                        input.key = '__parents__' as has_encapsulating_task
                FROM    task_run
                        LEFT JOIN jsonb_each(task_run.task_inputs) as input ON true
                        LEFT JOIN jsonb_array_elements(input.value) as argument ON true
                        LEFT JOIN flow_run as subflow
                                ON subflow.parent_task_run_id = task_run.id
                        LEFT JOIN flow
                                ON flow.id = subflow.flow_id
                WHERE   task_run.flow_run_id = :flow_run_id AND
                        task_run.state_type <> 'PENDING' AND
                        COALESCE(
                            subflow.start_time,
                            subflow.expected_start_time,
                            task_run.start_time,
                            task_run.expected_start_time
                        ) IS NOT NULL

                -- the order here is important to speed up building the two sets of
                -- edges in the with_parents and with_children CTEs below
                ORDER BY COALESCE(subflow.id, task_run.id)
            ),
            with_encapsulating AS (
                SELECT  children.id,
                        array_agg(parents.id order by parents.start_time) as encapsulating_ids
                FROM    edges as children
                        INNER JOIN edges as parents
                                ON parents.id = children.parent
                WHERE children.has_encapsulating_task is True
                GROUP BY children.id
            ),
            with_parents AS (
                SELECT  children.id,
                        array_agg(parents.id order by parents.start_time) as parent_ids
                FROM    edges as children
                        INNER JOIN edges as parents
                                ON parents.id = children.parent
                WHERE children.has_encapsulating_task is FALSE OR children.has_encapsulating_task is NULL
                GROUP BY children.id
            ),
            with_children AS (
                SELECT  parents.id,
                        array_agg(children.id order by children.start_time) as child_ids
                FROM    edges as parents
                        INNER JOIN edges as children
                                ON children.parent = parents.id
                WHERE children.has_encapsulating_task is FALSE OR children.has_encapsulating_task is NULL
                GROUP BY parents.id
            ),
            nodes AS (
                SELECT  DISTINCT ON (edges.id)
                        edges.kind,
                        edges.id,
                        edges.label,
                        edges.state_type,
                        edges.start_time,
                        edges.end_time,
                        with_parents.parent_ids,
                        with_children.child_ids,
                        with_encapsulating.encapsulating_ids
                FROM    edges
                        LEFT JOIN with_parents
                                ON with_parents.id = edges.id
                        LEFT JOIN with_children
                                ON with_children.id = edges.id
                        LEFT JOIN with_encapsulating
                                ON with_encapsulating.id = edges.id
            )
            SELECT  kind,
                    id,
                    label,
                    state_type,
                    start_time,
                    end_time,
                    parent_ids,
                    child_ids,
                    encapsulating_ids
            FROM    nodes
            WHERE   end_time IS NULL OR end_time >= :since
            ORDER BY start_time, end_time
            LIMIT :max_nodes
            ;
        """
        )

        query = query.bindparams(
            sa.bindparam("flow_run_id", value=flow_run_id),
            sa.bindparam("since", value=since),
            sa.bindparam("max_nodes", value=max_nodes + 1),
        )

        results = await session.execute(query)

        graph_artifacts = await self._get_flow_run_graph_artifacts(
            session, flow_run_id, max_artifacts
        )
        graph_states = await self._get_flow_run_graph_states(session, flow_run_id)

        nodes: List[Tuple[UUID, Node]] = []
        root_node_ids: List[UUID] = []

        for row in results:
            if not row.parent_ids:
                root_node_ids.append(row.id)

            nodes.append(
                (
                    row.id,
                    Node(
                        kind=row.kind,
                        id=row.id,
                        label=row.label,
                        state_type=row.state_type,
                        start_time=row.start_time,
                        end_time=row.end_time,
                        parents=[Edge(id=id) for id in row.parent_ids or []],
                        children=[Edge(id=id) for id in row.child_ids or []],
                        encapsulating=[
                            Edge(id=id) for id in row.encapsulating_ids or []
                        ],
                        artifacts=graph_artifacts.get(row.id, []),
                    ),
                )
            )

            if len(nodes) > max_nodes:
                raise FlowRunGraphTooLarge(
                    f"The graph of flow run {flow_run_id} has more than "
                    f"{max_nodes} nodes."
                )

        return Graph(
            start_time=start_time,
            end_time=end_time,
            root_node_ids=root_node_ids,
            nodes=nodes,
            artifacts=graph_artifacts.get(None, []),
            states=graph_states,
        )

flow_run_graph_v2(session, flow_run_id, since, max_nodes, max_artifacts) async

Returns the query that selects all of the nodes and edges for a flow run graph (version 2).

Source code in src/prefect/server/database/query_components.py
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
async def flow_run_graph_v2(
    self,
    session: AsyncSession,
    flow_run_id: UUID,
    since: datetime.datetime,
    max_nodes: int,
    max_artifacts: int,
) -> Graph:
    """Returns the query that selects all of the nodes and edges for a flow run
    graph (version 2)."""
    result = await session.execute(
        sa.select(
            sa.func.coalesce(
                orm_models.FlowRun.start_time,
                orm_models.FlowRun.expected_start_time,
            ),
            orm_models.FlowRun.end_time,
        ).where(
            orm_models.FlowRun.id == flow_run_id,
        )
    )
    try:
        start_time, end_time = result.one()
    except NoResultFound:
        raise ObjectNotFoundError(f"Flow run {flow_run_id} not found")

    query = sa.text(
        """
        WITH
        edges AS (
            SELECT  CASE
                        WHEN subflow.id IS NOT NULL THEN 'flow-run'
                        ELSE 'task-run'
                    END as kind,
                    COALESCE(subflow.id, task_run.id) as id,
                    COALESCE(flow.name || ' / ' || subflow.name, task_run.name) as label,
                    COALESCE(subflow.state_type, task_run.state_type) as state_type,
                    COALESCE(
                        subflow.start_time,
                        subflow.expected_start_time,
                        task_run.start_time,
                        task_run.expected_start_time
                    ) as start_time,
                    COALESCE(
                        subflow.end_time,
                        task_run.end_time,
                        CASE
                            WHEN task_run.state_type = 'COMPLETED'
                                THEN task_run.expected_start_time
                            ELSE NULL
                        END
                    ) as end_time,
                    (argument->>'id')::uuid as parent,
                    input.key = '__parents__' as has_encapsulating_task
            FROM    task_run
                    LEFT JOIN jsonb_each(task_run.task_inputs) as input ON true
                    LEFT JOIN jsonb_array_elements(input.value) as argument ON true
                    LEFT JOIN flow_run as subflow
                            ON subflow.parent_task_run_id = task_run.id
                    LEFT JOIN flow
                            ON flow.id = subflow.flow_id
            WHERE   task_run.flow_run_id = :flow_run_id AND
                    task_run.state_type <> 'PENDING' AND
                    COALESCE(
                        subflow.start_time,
                        subflow.expected_start_time,
                        task_run.start_time,
                        task_run.expected_start_time
                    ) IS NOT NULL

            -- the order here is important to speed up building the two sets of
            -- edges in the with_parents and with_children CTEs below
            ORDER BY COALESCE(subflow.id, task_run.id)
        ),
        with_encapsulating AS (
            SELECT  children.id,
                    array_agg(parents.id order by parents.start_time) as encapsulating_ids
            FROM    edges as children
                    INNER JOIN edges as parents
                            ON parents.id = children.parent
            WHERE children.has_encapsulating_task is True
            GROUP BY children.id
        ),
        with_parents AS (
            SELECT  children.id,
                    array_agg(parents.id order by parents.start_time) as parent_ids
            FROM    edges as children
                    INNER JOIN edges as parents
                            ON parents.id = children.parent
            WHERE children.has_encapsulating_task is FALSE OR children.has_encapsulating_task is NULL
            GROUP BY children.id
        ),
        with_children AS (
            SELECT  parents.id,
                    array_agg(children.id order by children.start_time) as child_ids
            FROM    edges as parents
                    INNER JOIN edges as children
                            ON children.parent = parents.id
            WHERE children.has_encapsulating_task is FALSE OR children.has_encapsulating_task is NULL
            GROUP BY parents.id
        ),
        nodes AS (
            SELECT  DISTINCT ON (edges.id)
                    edges.kind,
                    edges.id,
                    edges.label,
                    edges.state_type,
                    edges.start_time,
                    edges.end_time,
                    with_parents.parent_ids,
                    with_children.child_ids,
                    with_encapsulating.encapsulating_ids
            FROM    edges
                    LEFT JOIN with_parents
                            ON with_parents.id = edges.id
                    LEFT JOIN with_children
                            ON with_children.id = edges.id
                    LEFT JOIN with_encapsulating
                            ON with_encapsulating.id = edges.id
        )
        SELECT  kind,
                id,
                label,
                state_type,
                start_time,
                end_time,
                parent_ids,
                child_ids,
                encapsulating_ids
        FROM    nodes
        WHERE   end_time IS NULL OR end_time >= :since
        ORDER BY start_time, end_time
        LIMIT :max_nodes
        ;
    """
    )

    query = query.bindparams(
        sa.bindparam("flow_run_id", value=flow_run_id),
        sa.bindparam("since", value=since),
        sa.bindparam("max_nodes", value=max_nodes + 1),
    )

    results = await session.execute(query)

    graph_artifacts = await self._get_flow_run_graph_artifacts(
        session, flow_run_id, max_artifacts
    )
    graph_states = await self._get_flow_run_graph_states(session, flow_run_id)

    nodes: List[Tuple[UUID, Node]] = []
    root_node_ids: List[UUID] = []

    for row in results:
        if not row.parent_ids:
            root_node_ids.append(row.id)

        nodes.append(
            (
                row.id,
                Node(
                    kind=row.kind,
                    id=row.id,
                    label=row.label,
                    state_type=row.state_type,
                    start_time=row.start_time,
                    end_time=row.end_time,
                    parents=[Edge(id=id) for id in row.parent_ids or []],
                    children=[Edge(id=id) for id in row.child_ids or []],
                    encapsulating=[
                        Edge(id=id) for id in row.encapsulating_ids or []
                    ],
                    artifacts=graph_artifacts.get(row.id, []),
                ),
            )
        )

        if len(nodes) > max_nodes:
            raise FlowRunGraphTooLarge(
                f"The graph of flow run {flow_run_id} has more than "
                f"{max_nodes} nodes."
            )

    return Graph(
        start_time=start_time,
        end_time=end_time,
        root_node_ids=root_node_ids,
        nodes=nodes,
        artifacts=graph_artifacts.get(None, []),
        states=graph_states,
    )

set_state_id_on_inserted_flow_runs_statement(fr_model, frs_model, inserted_flow_run_ids, insert_flow_run_states)

Given a list of flow run ids and associated states, set the state_id to the appropriate state for all flow runs

Source code in src/prefect/server/database/query_components.py
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
def set_state_id_on_inserted_flow_runs_statement(
    self,
    fr_model,
    frs_model,
    inserted_flow_run_ids,
    insert_flow_run_states,
):
    """Given a list of flow run ids and associated states, set the state_id
    to the appropriate state for all flow runs"""
    # postgres supports `UPDATE ... FROM` syntax
    stmt = (
        sa.update(fr_model)
        .where(
            fr_model.id.in_(inserted_flow_run_ids),
            frs_model.flow_run_id == fr_model.id,
            frs_model.id.in_([r["id"] for r in insert_flow_run_states]),
        )
        .values(state_id=frs_model.id)
        # no need to synchronize as these flow runs are entirely new
        .execution_options(synchronize_session=False)
    )
    return stmt

BaseQueryComponents

Bases: ABC

Abstract base class used to inject dialect-specific SQL operations into Prefect.

Source code in src/prefect/server/database/query_components.py
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
class BaseQueryComponents(ABC):
    """
    Abstract base class used to inject dialect-specific SQL operations into Prefect.
    """

    CONFIGURATION_CACHE = TTLCache(maxsize=100, ttl=ONE_HOUR)

    def _unique_key(self) -> Tuple[Hashable, ...]:
        """
        Returns a key used to determine whether to instantiate a new DB interface.
        """
        return (self.__class__,)

    # --- dialect-specific SqlAlchemy bindings

    @abstractmethod
    def insert(self, obj) -> Union[postgresql.Insert, sqlite.Insert]:
        """dialect-specific insert statement"""

    @abstractmethod
    def greatest(self, *values):
        """dialect-specific SqlAlchemy binding"""

    @abstractmethod
    def least(self, *values):
        """dialect-specific SqlAlchemy binding"""

    # --- dialect-specific JSON handling

    @abstractproperty
    def uses_json_strings(self) -> bool:
        """specifies whether the configured dialect returns JSON as strings"""

    @abstractmethod
    def cast_to_json(self, json_obj):
        """casts to JSON object if necessary"""

    @abstractmethod
    def build_json_object(self, *args):
        """builds a JSON object from sequential key-value pairs"""

    @abstractmethod
    def json_arr_agg(self, json_array):
        """aggregates a JSON array"""

    # --- dialect-optimized subqueries

    @abstractmethod
    def make_timestamp_intervals(
        self,
        start_time: datetime.datetime,
        end_time: datetime.datetime,
        interval: datetime.timedelta,
    ):
        ...

    @abstractmethod
    def set_state_id_on_inserted_flow_runs_statement(
        self,
        fr_model,
        frs_model,
        inserted_flow_run_ids,
        insert_flow_run_states,
    ):
        ...

    @abstractmethod
    async def get_flow_run_notifications_from_queue(
        self, session: AsyncSession, limit: int
    ):
        """Database-specific implementation of reading notifications from the queue and deleting them"""

    async def queue_flow_run_notifications(
        self,
        session: sa.orm.session,
        flow_run: Union[schemas.core.FlowRun, orm_models.FlowRun],
        db: "PrefectDBInterface",
    ):
        """Database-specific implementation of queueing notifications for a flow run"""
        # insert a <policy, state> pair into the notification queue
        stmt = db.insert(orm_models.FlowRunNotificationQueue).from_select(
            [
                orm_models.FlowRunNotificationQueue.flow_run_notification_policy_id,
                orm_models.FlowRunNotificationQueue.flow_run_state_id,
            ],
            # ... by selecting from any notification policy that matches the criteria
            sa.select(
                orm_models.FlowRunNotificationPolicy.id,
                sa.cast(sa.literal(str(flow_run.state_id)), UUIDTypeDecorator),
            )
            .select_from(orm_models.FlowRunNotificationPolicy)
            .where(
                sa.and_(
                    # the policy is active
                    orm_models.FlowRunNotificationPolicy.is_active.is_(True),
                    # the policy state names aren't set or match the current state name
                    sa.or_(
                        orm_models.FlowRunNotificationPolicy.state_names == [],
                        json_has_any_key(
                            orm_models.FlowRunNotificationPolicy.state_names,
                            [flow_run.state_name],
                        ),
                    ),
                    # the policy tags aren't set, or the tags match the flow run tags
                    sa.or_(
                        orm_models.FlowRunNotificationPolicy.tags == [],
                        json_has_any_key(
                            orm_models.FlowRunNotificationPolicy.tags, flow_run.tags
                        ),
                    ),
                )
            ),
            # don't send python defaults as part of the insert statement, because they are
            # evaluated once per statement and create unique constraint violations on each row
            include_defaults=False,
        )
        await session.execute(stmt)

    def get_scheduled_flow_runs_from_work_queues(
        self,
        limit_per_queue: Optional[int] = None,
        work_queue_ids: Optional[List[UUID]] = None,
        scheduled_before: Optional[datetime.datetime] = None,
    ):
        """
        Returns all scheduled runs in work queues, subject to provided parameters.

        This query returns a `(orm_models.FlowRun, orm_models.WorkQueue.id)` pair; calling
        `result.all()` will return both; calling `result.scalars().unique().all()`
        will return only the flow run because it grabs the first result.
        """

        # get any work queues that have a concurrency limit, and compute available
        # slots as their limit less the number of running flows
        concurrency_queues = (
            sa.select(
                orm_models.WorkQueue.id,
                self.greatest(
                    0,
                    orm_models.WorkQueue.concurrency_limit
                    - sa.func.count(orm_models.FlowRun.id),
                ).label("available_slots"),
            )
            .select_from(orm_models.WorkQueue)
            .join(
                orm_models.FlowRun,
                sa.and_(
                    self._flow_run_work_queue_join_clause(
                        orm_models.FlowRun, orm_models.WorkQueue
                    ),
                    orm_models.FlowRun.state_type.in_(
                        ["RUNNING", "PENDING", "CANCELLING"]
                    ),
                ),
                isouter=True,
            )
            .where(orm_models.WorkQueue.concurrency_limit.is_not(None))
            .group_by(orm_models.WorkQueue.id)
            .cte("concurrency_queues")
        )

        # use the available slots information to generate a join
        # for all scheduled runs
        scheduled_flow_runs, join_criteria = self._get_scheduled_flow_runs_join(
            work_queue_query=concurrency_queues,
            limit_per_queue=limit_per_queue,
            scheduled_before=scheduled_before,
        )

        # starting with the work queue table, join the limited queues to get the
        # concurrency information and the scheduled flow runs to load all applicable
        # runs. this will return all the scheduled runs allowed by the parameters
        query = (
            # return a flow run and work queue id
            sa.select(
                sa.orm.aliased(orm_models.FlowRun, scheduled_flow_runs),
                orm_models.WorkQueue.id.label("wq_id"),
            )
            .select_from(orm_models.WorkQueue)
            .join(
                concurrency_queues,
                orm_models.WorkQueue.id == concurrency_queues.c.id,
                isouter=True,
            )
            .join(scheduled_flow_runs, join_criteria)
            .where(
                orm_models.WorkQueue.is_paused.is_(False),
                orm_models.WorkQueue.id.in_(work_queue_ids) if work_queue_ids else True,
            )
            .order_by(
                scheduled_flow_runs.c.next_scheduled_start_time,
                scheduled_flow_runs.c.id,
            )
        )

        return query

    def _get_scheduled_flow_runs_join(
        self,
        work_queue_query,
        limit_per_queue: Optional[int],
        scheduled_before: Optional[datetime.datetime],
    ):
        """Used by self.get_scheduled_flow_runs_from_work_queue, allowing just
        this function to be changed on a per-dialect basis"""

        # precompute for readability
        scheduled_before_clause = (
            orm_models.FlowRun.next_scheduled_start_time <= scheduled_before
            if scheduled_before is not None
            else True
        )

        # get scheduled flow runs with lateral join where the limit is the
        # available slots per queue
        scheduled_flow_runs = (
            sa.select(orm_models.FlowRun)
            .where(
                self._flow_run_work_queue_join_clause(
                    orm_models.FlowRun, orm_models.WorkQueue
                ),
                orm_models.FlowRun.state_type == "SCHEDULED",
                scheduled_before_clause,
            )
            .with_for_update(skip_locked=True)
            # priority given to runs with earlier next_scheduled_start_time
            .order_by(orm_models.FlowRun.next_scheduled_start_time)
            # if null, no limit will be applied
            .limit(sa.func.least(limit_per_queue, work_queue_query.c.available_slots))
            .lateral("scheduled_flow_runs")
        )

        # Perform a cross-join
        join_criteria = sa.literal(True)

        return scheduled_flow_runs, join_criteria

    def _flow_run_work_queue_join_clause(self, flow_run, work_queue):
        """
        On clause for for joining flow runs to work queues

        Used by self.get_scheduled_flow_runs_from_work_queue, allowing just
        this function to be changed on a per-dialect basis
        """
        return sa.and_(flow_run.work_queue_name == work_queue.name)

    # -------------------------------------------------------
    # Workers
    # -------------------------------------------------------

    @abstractproperty
    def _get_scheduled_flow_runs_from_work_pool_template_path(self):
        """
        Template for the query to get scheduled flow runs from a work pool
        """

    async def get_scheduled_flow_runs_from_work_pool(
        self,
        session,
        limit: Optional[int] = None,
        worker_limit: Optional[int] = None,
        queue_limit: Optional[int] = None,
        work_pool_ids: Optional[List[UUID]] = None,
        work_queue_ids: Optional[List[UUID]] = None,
        scheduled_before: Optional[datetime.datetime] = None,
        scheduled_after: Optional[datetime.datetime] = None,
        respect_queue_priorities: bool = False,
    ) -> List[schemas.responses.WorkerFlowRunResponse]:
        template = jinja_env.get_template(
            self._get_scheduled_flow_runs_from_work_pool_template_path
        )

        raw_query = sa.text(
            template.render(
                work_pool_ids=work_pool_ids,
                work_queue_ids=work_queue_ids,
                respect_queue_priorities=respect_queue_priorities,
                scheduled_before=scheduled_before,
                scheduled_after=scheduled_after,
            )
        )

        bindparams = []

        if scheduled_before:
            bindparams.append(
                sa.bindparam("scheduled_before", scheduled_before, type_=Timestamp)
            )

        if scheduled_after:
            bindparams.append(
                sa.bindparam("scheduled_after", scheduled_after, type_=Timestamp)
            )

        # if work pool IDs were provided, bind them
        if work_pool_ids:
            assert all(isinstance(i, UUID) for i in work_pool_ids)
            bindparams.append(
                sa.bindparam(
                    "work_pool_ids",
                    work_pool_ids,
                    expanding=True,
                    type_=UUIDTypeDecorator,
                )
            )

        # if work queue IDs were provided, bind them
        if work_queue_ids:
            assert all(isinstance(i, UUID) for i in work_queue_ids)
            bindparams.append(
                sa.bindparam(
                    "work_queue_ids",
                    work_queue_ids,
                    expanding=True,
                    type_=UUIDTypeDecorator,
                )
            )

        query = raw_query.bindparams(
            *bindparams,
            limit=1000 if limit is None else limit,
            worker_limit=1000 if worker_limit is None else worker_limit,
            queue_limit=1000 if queue_limit is None else queue_limit,
        )

        orm_query = (
            sa.select(
                sa.column("run_work_pool_id"),
                sa.column("run_work_queue_id"),
                orm_models.FlowRun,
            )
            .from_statement(query)
            # indicate that the state relationship isn't being loaded
            .options(sa.orm.noload(orm_models.FlowRun.state))
        )

        result = await session.execute(orm_query)

        return [
            schemas.responses.WorkerFlowRunResponse(
                work_pool_id=r.run_work_pool_id,
                work_queue_id=r.run_work_queue_id,
                flow_run=schemas.core.FlowRun.model_validate(
                    r.FlowRun, from_attributes=True
                ),
            )
            for r in result
        ]

    async def read_block_documents(
        self,
        session: sa.orm.Session,
        block_document_filter: Optional[schemas.filters.BlockDocumentFilter] = None,
        block_type_filter: Optional[schemas.filters.BlockTypeFilter] = None,
        block_schema_filter: Optional[schemas.filters.BlockSchemaFilter] = None,
        include_secrets: bool = False,
        offset: Optional[int] = None,
        limit: Optional[int] = None,
    ):
        # if no filter is provided, one is created that excludes anonymous blocks
        if block_document_filter is None:
            block_document_filter = schemas.filters.BlockDocumentFilter(
                is_anonymous=schemas.filters.BlockDocumentFilterIsAnonymous(eq_=False)
            )

        # --- Query for Parent Block Documents
        # begin by building a query for only those block documents that are selected
        # by the provided filters
        filtered_block_documents_query = sa.select(orm_models.BlockDocument.id).where(
            block_document_filter.as_sql_filter()
        )

        if block_type_filter is not None:
            block_type_exists_clause = sa.select(orm_models.BlockType).where(
                orm_models.BlockType.id == orm_models.BlockDocument.block_type_id,
                block_type_filter.as_sql_filter(),
            )
            filtered_block_documents_query = filtered_block_documents_query.where(
                block_type_exists_clause.exists()
            )

        if block_schema_filter is not None:
            block_schema_exists_clause = sa.select(orm_models.BlockSchema).where(
                orm_models.BlockSchema.id == orm_models.BlockDocument.block_schema_id,
                block_schema_filter.as_sql_filter(),
            )
            filtered_block_documents_query = filtered_block_documents_query.where(
                block_schema_exists_clause.exists()
            )

        if offset is not None:
            filtered_block_documents_query = filtered_block_documents_query.offset(
                offset
            )

        if limit is not None:
            filtered_block_documents_query = filtered_block_documents_query.limit(limit)

        filtered_block_documents_query = filtered_block_documents_query.cte(
            "filtered_block_documents"
        )

        # --- Query for Referenced Block Documents
        # next build a recursive query for (potentially nested) block documents
        # that reference the filtered block documents
        block_document_references_query = (
            sa.select(orm_models.BlockDocumentReference)
            .filter(
                orm_models.BlockDocumentReference.parent_block_document_id.in_(
                    sa.select(filtered_block_documents_query.c.id)
                )
            )
            .cte("block_document_references", recursive=True)
        )
        block_document_references_join = sa.select(
            orm_models.BlockDocumentReference
        ).join(
            block_document_references_query,
            orm_models.BlockDocumentReference.parent_block_document_id
            == block_document_references_query.c.reference_block_document_id,
        )
        recursive_block_document_references_cte = (
            block_document_references_query.union_all(block_document_references_join)
        )

        # --- Final Query for All Block Documents
        # build a query that unions:
        # - the filtered block documents
        # - with any block documents that are discovered as (potentially nested) references
        all_block_documents_query = sa.union_all(
            # first select the parent block
            sa.select(
                orm_models.BlockDocument,
                sa.null().label("reference_name"),
                sa.null().label("reference_parent_block_document_id"),
            )
            .select_from(orm_models.BlockDocument)
            .where(
                orm_models.BlockDocument.id.in_(
                    sa.select(filtered_block_documents_query.c.id)
                )
            ),
            #
            # then select any referenced blocks
            sa.select(
                orm_models.BlockDocument,
                recursive_block_document_references_cte.c.name,
                recursive_block_document_references_cte.c.parent_block_document_id,
            )
            .select_from(orm_models.BlockDocument)
            .join(
                recursive_block_document_references_cte,
                orm_models.BlockDocument.id
                == recursive_block_document_references_cte.c.reference_block_document_id,
            ),
        ).cte("all_block_documents_query")

        # the final union query needs to be `aliased` for proper ORM unpacking
        # and also be sorted
        return (
            sa.select(
                sa.orm.aliased(orm_models.BlockDocument, all_block_documents_query),
                all_block_documents_query.c.reference_name,
                all_block_documents_query.c.reference_parent_block_document_id,
            )
            .select_from(all_block_documents_query)
            .order_by(all_block_documents_query.c.name)
        )

    async def read_configuration_value(
        self, session: sa.orm.Session, key: str
    ) -> Optional[Dict]:
        """
        Read a configuration value by key.

        Configuration values should not be changed at run time, so retrieved
        values are cached in memory.

        The main use of configurations is encrypting blocks, this speeds up nested
        block document queries.
        """
        try:
            return self.CONFIGURATION_CACHE[key]
        except KeyError:
            query = sa.select(orm_models.Configuration).where(
                orm_models.Configuration.key == key
            )
            result = await session.execute(query)
            configuration = result.scalar()
            if configuration is not None:
                self.CONFIGURATION_CACHE[key] = configuration.value
                return configuration.value
            return configuration

    def clear_configuration_value_cache_for_key(self, key: str):
        """Removes a configuration key from the cache."""
        self.CONFIGURATION_CACHE.pop(key, None)

    @abstractmethod
    async def flow_run_graph_v2(
        self,
        session: AsyncSession,
        flow_run_id: UUID,
        since: datetime.datetime,
        max_nodes: int,
        max_artifacts: int,
    ) -> Graph:
        """Returns the query that selects all of the nodes and edges for a flow run graph (version 2)."""
        ...

    async def _get_flow_run_graph_artifacts(
        self,
        session: AsyncSession,
        flow_run_id: UUID,
        max_artifacts: int,
    ):
        """Get the artifacts for a flow run grouped by task run id.

        Does not recurse into subflows.
        Artifacts for the flow run without a task run id are grouped under None.
        """
        query = (
            sa.select(
                orm_models.Artifact,
                orm_models.ArtifactCollection.id.label("latest_in_collection_id"),
            )
            .where(
                orm_models.Artifact.flow_run_id == flow_run_id,
                orm_models.Artifact.type != "result",
            )
            .join(
                orm_models.ArtifactCollection,
                (orm_models.ArtifactCollection.key == orm_models.Artifact.key)
                & (orm_models.ArtifactCollection.latest_id == orm_models.Artifact.id),
                isouter=True,
            )
            .order_by(orm_models.Artifact.created.asc())
            .limit(max_artifacts)
        )

        results = await session.execute(query)

        artifacts_by_task = defaultdict(list)
        for artifact, latest_in_collection_id in results:
            artifacts_by_task[artifact.task_run_id].append(
                GraphArtifact(
                    id=artifact.id,
                    created=artifact.created,
                    key=artifact.key,
                    type=artifact.type,
                    # We're only using the data field for progress artifacts for now
                    data=artifact.data if artifact.type == "progress" else None,
                    is_latest=artifact.key is None
                    or latest_in_collection_id is not None,
                )
            )

        return artifacts_by_task

    async def _get_flow_run_graph_states(
        self,
        session: AsyncSession,
        flow_run_id: UUID,
    ):
        """Get the flow run states for a flow run graph."""
        flow_run_states = await models.flow_run_states.read_flow_run_states(
            session=session, flow_run_id=flow_run_id
        )

        return [
            GraphState(
                id=state.id,
                timestamp=state.timestamp,
                type=state.type,
                name=state.name,
            )
            for state in flow_run_states
        ]

build_json_object(*args) abstractmethod

builds a JSON object from sequential key-value pairs

Source code in src/prefect/server/database/query_components.py
82
83
84
@abstractmethod
def build_json_object(self, *args):
    """builds a JSON object from sequential key-value pairs"""

cast_to_json(json_obj) abstractmethod

casts to JSON object if necessary

Source code in src/prefect/server/database/query_components.py
78
79
80
@abstractmethod
def cast_to_json(self, json_obj):
    """casts to JSON object if necessary"""

clear_configuration_value_cache_for_key(key)

Removes a configuration key from the cache.

Source code in src/prefect/server/database/query_components.py
539
540
541
def clear_configuration_value_cache_for_key(self, key: str):
    """Removes a configuration key from the cache."""
    self.CONFIGURATION_CACHE.pop(key, None)

flow_run_graph_v2(session, flow_run_id, since, max_nodes, max_artifacts) abstractmethod async

Returns the query that selects all of the nodes and edges for a flow run graph (version 2).

Source code in src/prefect/server/database/query_components.py
543
544
545
546
547
548
549
550
551
552
553
@abstractmethod
async def flow_run_graph_v2(
    self,
    session: AsyncSession,
    flow_run_id: UUID,
    since: datetime.datetime,
    max_nodes: int,
    max_artifacts: int,
) -> Graph:
    """Returns the query that selects all of the nodes and edges for a flow run graph (version 2)."""
    ...

get_flow_run_notifications_from_queue(session, limit) abstractmethod async

Database-specific implementation of reading notifications from the queue and deleting them

Source code in src/prefect/server/database/query_components.py
111
112
113
114
115
@abstractmethod
async def get_flow_run_notifications_from_queue(
    self, session: AsyncSession, limit: int
):
    """Database-specific implementation of reading notifications from the queue and deleting them"""

get_scheduled_flow_runs_from_work_queues(limit_per_queue=None, work_queue_ids=None, scheduled_before=None)

Returns all scheduled runs in work queues, subject to provided parameters.

This query returns a (orm_models.FlowRun, orm_models.WorkQueue.id) pair; calling result.all() will return both; calling result.scalars().unique().all() will return only the flow run because it grabs the first result.

Source code in src/prefect/server/database/query_components.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def get_scheduled_flow_runs_from_work_queues(
    self,
    limit_per_queue: Optional[int] = None,
    work_queue_ids: Optional[List[UUID]] = None,
    scheduled_before: Optional[datetime.datetime] = None,
):
    """
    Returns all scheduled runs in work queues, subject to provided parameters.

    This query returns a `(orm_models.FlowRun, orm_models.WorkQueue.id)` pair; calling
    `result.all()` will return both; calling `result.scalars().unique().all()`
    will return only the flow run because it grabs the first result.
    """

    # get any work queues that have a concurrency limit, and compute available
    # slots as their limit less the number of running flows
    concurrency_queues = (
        sa.select(
            orm_models.WorkQueue.id,
            self.greatest(
                0,
                orm_models.WorkQueue.concurrency_limit
                - sa.func.count(orm_models.FlowRun.id),
            ).label("available_slots"),
        )
        .select_from(orm_models.WorkQueue)
        .join(
            orm_models.FlowRun,
            sa.and_(
                self._flow_run_work_queue_join_clause(
                    orm_models.FlowRun, orm_models.WorkQueue
                ),
                orm_models.FlowRun.state_type.in_(
                    ["RUNNING", "PENDING", "CANCELLING"]
                ),
            ),
            isouter=True,
        )
        .where(orm_models.WorkQueue.concurrency_limit.is_not(None))
        .group_by(orm_models.WorkQueue.id)
        .cte("concurrency_queues")
    )

    # use the available slots information to generate a join
    # for all scheduled runs
    scheduled_flow_runs, join_criteria = self._get_scheduled_flow_runs_join(
        work_queue_query=concurrency_queues,
        limit_per_queue=limit_per_queue,
        scheduled_before=scheduled_before,
    )

    # starting with the work queue table, join the limited queues to get the
    # concurrency information and the scheduled flow runs to load all applicable
    # runs. this will return all the scheduled runs allowed by the parameters
    query = (
        # return a flow run and work queue id
        sa.select(
            sa.orm.aliased(orm_models.FlowRun, scheduled_flow_runs),
            orm_models.WorkQueue.id.label("wq_id"),
        )
        .select_from(orm_models.WorkQueue)
        .join(
            concurrency_queues,
            orm_models.WorkQueue.id == concurrency_queues.c.id,
            isouter=True,
        )
        .join(scheduled_flow_runs, join_criteria)
        .where(
            orm_models.WorkQueue.is_paused.is_(False),
            orm_models.WorkQueue.id.in_(work_queue_ids) if work_queue_ids else True,
        )
        .order_by(
            scheduled_flow_runs.c.next_scheduled_start_time,
            scheduled_flow_runs.c.id,
        )
    )

    return query

greatest(*values) abstractmethod

dialect-specific SqlAlchemy binding

Source code in src/prefect/server/database/query_components.py
64
65
66
@abstractmethod
def greatest(self, *values):
    """dialect-specific SqlAlchemy binding"""

insert(obj) abstractmethod

dialect-specific insert statement

Source code in src/prefect/server/database/query_components.py
60
61
62
@abstractmethod
def insert(self, obj) -> Union[postgresql.Insert, sqlite.Insert]:
    """dialect-specific insert statement"""

json_arr_agg(json_array) abstractmethod

aggregates a JSON array

Source code in src/prefect/server/database/query_components.py
86
87
88
@abstractmethod
def json_arr_agg(self, json_array):
    """aggregates a JSON array"""

least(*values) abstractmethod

dialect-specific SqlAlchemy binding

Source code in src/prefect/server/database/query_components.py
68
69
70
@abstractmethod
def least(self, *values):
    """dialect-specific SqlAlchemy binding"""

queue_flow_run_notifications(session, flow_run, db) async

Database-specific implementation of queueing notifications for a flow run

Source code in src/prefect/server/database/query_components.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
async def queue_flow_run_notifications(
    self,
    session: sa.orm.session,
    flow_run: Union[schemas.core.FlowRun, orm_models.FlowRun],
    db: "PrefectDBInterface",
):
    """Database-specific implementation of queueing notifications for a flow run"""
    # insert a <policy, state> pair into the notification queue
    stmt = db.insert(orm_models.FlowRunNotificationQueue).from_select(
        [
            orm_models.FlowRunNotificationQueue.flow_run_notification_policy_id,
            orm_models.FlowRunNotificationQueue.flow_run_state_id,
        ],
        # ... by selecting from any notification policy that matches the criteria
        sa.select(
            orm_models.FlowRunNotificationPolicy.id,
            sa.cast(sa.literal(str(flow_run.state_id)), UUIDTypeDecorator),
        )
        .select_from(orm_models.FlowRunNotificationPolicy)
        .where(
            sa.and_(
                # the policy is active
                orm_models.FlowRunNotificationPolicy.is_active.is_(True),
                # the policy state names aren't set or match the current state name
                sa.or_(
                    orm_models.FlowRunNotificationPolicy.state_names == [],
                    json_has_any_key(
                        orm_models.FlowRunNotificationPolicy.state_names,
                        [flow_run.state_name],
                    ),
                ),
                # the policy tags aren't set, or the tags match the flow run tags
                sa.or_(
                    orm_models.FlowRunNotificationPolicy.tags == [],
                    json_has_any_key(
                        orm_models.FlowRunNotificationPolicy.tags, flow_run.tags
                    ),
                ),
            )
        ),
        # don't send python defaults as part of the insert statement, because they are
        # evaluated once per statement and create unique constraint violations on each row
        include_defaults=False,
    )
    await session.execute(stmt)

read_configuration_value(session, key) async

Read a configuration value by key.

Configuration values should not be changed at run time, so retrieved values are cached in memory.

The main use of configurations is encrypting blocks, this speeds up nested block document queries.

Source code in src/prefect/server/database/query_components.py
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
async def read_configuration_value(
    self, session: sa.orm.Session, key: str
) -> Optional[Dict]:
    """
    Read a configuration value by key.

    Configuration values should not be changed at run time, so retrieved
    values are cached in memory.

    The main use of configurations is encrypting blocks, this speeds up nested
    block document queries.
    """
    try:
        return self.CONFIGURATION_CACHE[key]
    except KeyError:
        query = sa.select(orm_models.Configuration).where(
            orm_models.Configuration.key == key
        )
        result = await session.execute(query)
        configuration = result.scalar()
        if configuration is not None:
            self.CONFIGURATION_CACHE[key] = configuration.value
            return configuration.value
        return configuration

uses_json_strings()

specifies whether the configured dialect returns JSON as strings

Source code in src/prefect/server/database/query_components.py
74
75
76
@abstractproperty
def uses_json_strings(self) -> bool:
    """specifies whether the configured dialect returns JSON as strings"""