Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

3.2 KB
Raw Blame
import z from "zod";
import { OrderByState } from "../../../interfaces/orderBy";
import {
  findUiColumnMapping,
  UiColumnMappings,
} from "../../../tableDefinitions/types";
import { InvalidRequestError } from "../../../errors/InvalidRequestError";
import { logger } from "../../logger";
import type { OrderByDirection, OrderByEntry } from "./event-query-builder";

type OrderByStateNotNull = Exclude<OrderByState, null>;

export function orderByToClickhouseSql(
  orderBy: OrderByState | OrderByState[] = [],
  tableColumns: UiColumnMappings,
  usedInAggregation = false,
): string {
  if (
    !orderBy ||
    (Array.isArray(orderBy) && orderBy.filter(Boolean).length === 0)
  ) {
    return "";
  }

  if (!Array.isArray(orderBy)) {
    orderBy = [orderBy];
  }
  // Initialize an array to hold order by clauses
  const orderByClauses: string[] = [];

  // Loop through each orderBy entry
  for (const ob of orderBy.filter((o): o is OrderByStateNotNull =>
    Boolean(o),
  )) {
    // Get column definition to map column to internal name, e.g. "t.id"
    const col = findUiColumnMapping(tableColumns, ob.column);

    if (!col) {
      logger.warn(`Invalid order by column: ${ob.column}`);
      throw new InvalidRequestError("Invalid order by column: " + ob.column);
    }

    // Assert that ob.order is either "asc" or "desc"
    const orderByOrder = z.enum(["ASC", "DESC"]);
    const order = orderByOrder.safeParse(ob.order);
    if (!order.success) {
      logger.warn(`Invalid order: ${ob.order}. Expected "ASC" or "DESC"`);
      throw new Error("Invalid order: " + ob.order);
    }

    const column = `${col.queryPrefix ? col.queryPrefix + "." : ""}${col.clickhouseSelect}`;

    // Append the order by clause to the array
    orderByClauses.push(
      `${usedInAggregation ? `anyLast(${column})` : column} ${order.data}`,
    );
  }

  // Join all order by clauses with a comma and return
  return `ORDER BY ${orderByClauses.join(", ")}`;
}

/**
 * Convert OrderByState to structured OrderByEntry array for use with BaseEventsQueryBuilder.
 * Returns empty array if no valid orderBy is provided.
 */
export function orderByToEntries(
  orderBy: OrderByState | OrderByState[] = [],
  tableColumns: UiColumnMappings,
): OrderByEntry[] {
  if (
    !orderBy ||
    (Array.isArray(orderBy) && orderBy.filter(Boolean).length === 0)
  ) {
    return [];
  }

  if (!Array.isArray(orderBy)) {
    orderBy = [orderBy];
  }

  const entries: OrderByEntry[] = [];

  for (const ob of orderBy.filter((o): o is OrderByStateNotNull =>
    Boolean(o),
  )) {
    const col = findUiColumnMapping(tableColumns, ob.column);

    if (!col) {
      logger.warn(`Invalid order by column: ${ob.column}`);
      throw new InvalidRequestError("Invalid order by column: " + ob.column);
    }

    const orderByOrder = z.enum(["ASC", "DESC"]);
    const order = orderByOrder.safeParse(ob.order);
    if (!order.success) {
      logger.warn(`Invalid order: ${ob.order}. Expected "ASC" or "DESC"`);
      throw new Error("Invalid order: " + ob.order);
    }

    const column = `${col.queryPrefix ? col.queryPrefix + "." : ""}${col.clickhouseSelect}`;

    entries.push({
      column,
      direction: order.data as OrderByDirection,
    });
  }

  return entries;
}