/**
 * Builder for the trailing ` SETTINGS ...` clause appended to ClickHouse
 * read queries.
 *
 * Every read built through this helper carries a per-query memory ceiling
 * (max_memory_usage) and spills oversized GROUP BY / ORDER BY states to
 * disk (max_bytes_before_external_group_by / _sort) instead of letting a
 * single dashboard query take down the server — historically no layer
 * bounded per-query memory at all. Execution-time caps and overflow
 * behavior stay per-call-site: each caller keeps the value it shipped
 * with.
 *
 * All values emitted here MUST be trusted literals (numbers / hardcoded
 * strings) — the clause is appended verbatim to SQL, never parameterized.
 */
// 3 GiB.
export const DEFAULT_MAX_MEMORY_USAGE_IN_BYTES = 3221225472;
// 1.5 GiB — half the memory ceiling so spill kicks in before the cap.
export const DEFAULT_MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY_IN_BYTES = 1610612736;
// 1.5 GiB.
export const DEFAULT_MAX_BYTES_BEFORE_EXTERNAL_SORT_IN_BYTES = 1610612736;
export function getQuerySettings(options) {
    var _a, _b, _c;
    const parts = [];
    if ((options === null || options === void 0 ? void 0 : options.maxExecutionTimeInSeconds) !== undefined) {
        parts.push(`max_execution_time = ${options.maxExecutionTimeInSeconds}`);
    }
    if ((options === null || options === void 0 ? void 0 : options.timeoutOverflowMode) !== undefined) {
        parts.push(`timeout_overflow_mode = '${options.timeoutOverflowMode}'`);
    }
    parts.push(`max_memory_usage = ${(_a = options === null || options === void 0 ? void 0 : options.maxMemoryUsageInBytes) !== null && _a !== void 0 ? _a : DEFAULT_MAX_MEMORY_USAGE_IN_BYTES}`);
    parts.push(`max_bytes_before_external_group_by = ${(_b = options === null || options === void 0 ? void 0 : options.maxBytesBeforeExternalGroupByInBytes) !== null && _b !== void 0 ? _b : DEFAULT_MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY_IN_BYTES}`);
    parts.push(`max_bytes_before_external_sort = ${(_c = options === null || options === void 0 ? void 0 : options.maxBytesBeforeExternalSortInBytes) !== null && _c !== void 0 ? _c : DEFAULT_MAX_BYTES_BEFORE_EXTERNAL_SORT_IN_BYTES}`);
    if (options === null || options === void 0 ? void 0 : options.additionalSettings) {
        for (const [key, value] of Object.entries(options.additionalSettings)) {
            parts.push(typeof value === "string" ? `${key} = '${value}'` : `${key} = ${value}`);
        }
    }
    return ` SETTINGS ${parts.join(", ")}`;
}
export default class QuerySettingsHelper {
    static getQuerySettings(options) {
        return getQuerySettings(options);
    }
}
//# sourceMappingURL=QuerySettingsHelper.js.map