/**
 * Parser and type definitions for table calculation functions
 * Supporting both row-based and pivot-based functions
 */
import type { WarehouseSqlBuilder } from '../types/warehouse';
/**
 * All supported table calculation function types
 */
export declare enum TableCalculationFunctionType {
    ROW = "row",
    INDEX = "index",
    OFFSET = "offset",
    OFFSET_LIST = "offset_list",
    LOOKUP = "lookup",
    LIST = "list",
    PIVOT_COLUMN = "pivot_column",
    PIVOT_INDEX = "pivot_index",
    PIVOT_OFFSET = "pivot_offset",
    PIVOT_OFFSET_LIST = "pivot_offset_list",
    PIVOT_ROW = "pivot_row",
    PIVOT_WHERE = "pivot_where",
    TOTAL = "total",
    ROW_TOTAL = "row_total"
}
/**
 * Row functions - operate across rows in unpivoted data
 */
export declare const ROW_FUNCTIONS: readonly [TableCalculationFunctionType.ROW, TableCalculationFunctionType.INDEX, TableCalculationFunctionType.OFFSET, TableCalculationFunctionType.OFFSET_LIST, TableCalculationFunctionType.LOOKUP, TableCalculationFunctionType.LIST];
/**
 * Pivot functions - operate across columns in pivoted data
 */
export declare const PIVOT_FUNCTIONS: readonly [TableCalculationFunctionType.PIVOT_COLUMN, TableCalculationFunctionType.PIVOT_INDEX, TableCalculationFunctionType.PIVOT_OFFSET, TableCalculationFunctionType.PIVOT_OFFSET_LIST, TableCalculationFunctionType.PIVOT_ROW, TableCalculationFunctionType.PIVOT_WHERE];
/**
 * Aggregate functions - reference column or row totals
 * Pre-processed in MetricQueryBuilder before function compilation.
 */
export declare const AGGREGATE_FUNCTIONS: readonly [TableCalculationFunctionType.TOTAL, TableCalculationFunctionType.ROW_TOTAL];
/**
 * Base type for all function calls
 */
interface BaseFunctionCall {
    type: TableCalculationFunctionType;
    rawSql: string;
}
/**
 * Row function call types
 */
export interface RowFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.ROW;
}
export interface IndexFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.INDEX;
    expression: string;
    rowIndex: number;
}
export interface OffsetFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.OFFSET;
    column: string;
    rowOffset: string;
}
export interface OffsetListFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.OFFSET_LIST;
    column: string;
    rowOffset: number;
    numValues: number;
}
export interface LookupFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.LOOKUP;
    value: string;
    lookupColumn: string;
    resultColumn: string;
}
export interface ListFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.LIST;
    values: string[];
}
/**
 * Pivot function call types
 */
export interface PivotColumnFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.PIVOT_COLUMN;
}
export interface PivotIndexFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.PIVOT_INDEX;
    expression: string;
    pivotIndex: number;
}
export interface PivotOffsetFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.PIVOT_OFFSET;
    expression: string;
    columnOffset: number;
}
export interface PivotOffsetListFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.PIVOT_OFFSET_LIST;
    expression: string;
    columnOffset: number;
    numValues: number;
}
export interface PivotRowFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.PIVOT_ROW;
    expression: string;
}
export interface PivotWhereFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.PIVOT_WHERE;
    selectExpression: string;
    valueExpression: string;
}
/**
 * Aggregate function call types
 */
export interface TotalFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.TOTAL;
    expression: string;
}
export interface RowTotalFunctionCall extends BaseFunctionCall {
    type: TableCalculationFunctionType.ROW_TOTAL;
    expression: string;
}
/**
 * Union type for all function calls
 */
export type TableCalculationFunctionCall = RowFunctionCall | IndexFunctionCall | OffsetFunctionCall | OffsetListFunctionCall | LookupFunctionCall | ListFunctionCall | PivotColumnFunctionCall | PivotIndexFunctionCall | PivotOffsetFunctionCall | PivotOffsetListFunctionCall | PivotRowFunctionCall | PivotWhereFunctionCall | TotalFunctionCall | RowTotalFunctionCall;
/**
 * Parse table calculation SQL to extract function calls
 */
export declare function parseTableCalculationFunctions(sql: string): TableCalculationFunctionCall[];
/**
 * Checks if the given function type is a pivot function.
 * Pivot functions operate across pivot columns (horizontal).
 * @param type - The table calculation function type
 * @returns True if the function is a pivot function
 */
export declare function isPivotFunction(type: TableCalculationFunctionType): boolean;
/**
 * Checks if the given function type is a row function.
 * Row functions operate across rows (vertical).
 * @param type - The table calculation function type
 * @returns True if the function is a row function
 */
export declare function isRowFunction(type: TableCalculationFunctionType): boolean;
/**
 * Checks if any of the parsed functions are pivot functions.
 * @param functions - Array of parsed table calculation functions
 * @returns True if any function is a pivot function
 */
export declare function hasPivotFunctions(functions: TableCalculationFunctionCall[]): boolean;
/**
 * Checks if any of the parsed functions are row functions.
 * @param functions - Array of parsed table calculation functions
 * @returns True if any function is a row function
 */
export declare function hasRowFunctions(functions: TableCalculationFunctionCall[]): boolean;
/**
 * Checks if the given function type is an aggregate function (total/row_total).
 */
export declare function isAggregateFunction(type: TableCalculationFunctionType): boolean;
/**
 * Checks if any of the parsed functions are aggregate functions (total/row_total).
 */
export declare function hasAggregateFunctions(functions: TableCalculationFunctionCall[]): boolean;
/**
 * Builds regexes for matching total("field") and row_total("field") in compiled SQL.
 * Used by both extractTotalReferences (detection) and MetricQueryBuilder.replaceTotalReferences (rewriting)
 * to ensure consistent matching logic.
 *
 * @param quoteChar - Warehouse-specific quote char (e.g. `"` for Postgres, `` ` `` for BigQuery).
 *                    When omitted, matches any of `"`, `'`, or `` ` `` (used for detection where
 *                    the warehouse dialect isn't relevant).
 */
export declare function buildTotalFieldRegex(quoteChar?: string): {
    totalRegex: RegExp;
    rowTotalRegex: RegExp;
};
/**
 * Extracts field IDs referenced by total() and row_total() calls in compiled table calculations.
 * Scans compiledSql for patterns like total("field_id") and row_total("field_id").
 *
 * NOTE: This extracts field IDs from compiled SQL (for CTE generation), which is a different
 * concern from parseTableCalculationFunctions() which identifies function *types* in expressions
 * (for compilation). They are intentionally separate parsers.
 */
export declare function extractTotalReferences(compiledTableCalcs: {
    compiledSql: string;
}[]): {
    totalFields: string[];
    rowTotalFields: string[];
};
/**
 * Compiler for table calculation functions to SQL
 */
export declare class TableCalculationFunctionCompiler {
    private warehouseSqlBuilder;
    constructor(warehouseSqlBuilder: WarehouseSqlBuilder);
    /**
     * Compiles all functions in a SQL expression.
     * @param sql - The SQL expression containing pivot functions
     * @param functions - Array of parsed table calculation functions
     * @param orderByClause - Optional ORDER BY clause content for row functions (e.g. '"date" ASC, "revenue" DESC')
     * @returns SQL with functions compiled to their SQL equivalents
     */
    compileFunctions(sql: string, functions: TableCalculationFunctionCall[], orderByClause?: string): string;
    /**
     * Compiles a pivot_offset function call to a guarded SQL window function.
     * Converts pivot_offset(expression, n) to LAG or LEAD with adjacency checks.
     * Walks across pivot columns within the same row (Looker semantics): negative
     * n looks at the pivot column n positions to the left, positive n to the right.
     * Returns NULL if the adjacent pivot column at the offset position is not exactly
     * n columns away (i.e. when the offset would skip over missing pivot columns).
     * @param expression - The SQL expression inside pivot_offset
     * @param columnOffset - The offset value (negative for LAG/previous pivot column, positive for LEAD/next pivot column)
     * @returns Compiled SQL with CASE statement checking for adjacent pivot columns on the same row
     */
    private compilePivotOffset;
    /**
     * Compiles a pivot_column function call to return the current column index.
     * The pivot_column() function returns the index of the current pivot column.
     * @returns SQL expression that retrieves the column_index value
     */
    private compilePivotColumn;
    /**
     * Compiles a pivot_index function call to return a value from a specific pivot column.
     * Uses a window function to find the value from the row with the same row_index
     * but the specified column_index.
     * @param expression - The SQL expression to evaluate
     * @param pivotIndex - The specific pivot column index (0-based)
     * @returns SQL with window function to retrieve value from specified column
     */
    private compilePivotIndex;
    /**
     * Compiles a pivot_where function call to return a value from the first pivot column matching a condition.
     * Evaluates the select expression for each pivot column and returns the value expression
     * from the first column where the condition is true.
     * @param selectExpression - Boolean expression to evaluate for each pivot column
     * @param valueExpression - Expression to return when condition matches
     * @returns SQL with conditional aggregation to find first matching pivot column
     */
    private compilePivotWhere;
    /**
     * Compiles a pivot_offset_list function call to return an array of values from consecutive pivot columns.
     * Uses PostgreSQL array construction syntax with window functions.
     * @param expression - The SQL expression to evaluate
     * @param columnOffset - Starting column offset (negative = previous, positive = next)
     * @param numValues - Number of consecutive values to return
     * @returns SQL array construction with window functions
     */
    private compilePivotOffsetList;
    /**
     * Compiles a pivot_row function call to return all values from the current row across all pivot columns.
     * Uses warehouse-specific array aggregation to collect all values for the expression across pivot columns.
     * @param expression - The SQL expression to evaluate for each pivot column
     * @returns SQL array aggregation for all pivot values in current row
     */
    private compilePivotRow;
    private static buildOrderByWindow;
    private static compileRow;
    private static compileOffset;
    private static compileIndex;
    private compileOffsetList;
    private compileList;
    private static compileLookup;
}
export {};
//# sourceMappingURL=tableCalculationFunctions.d.ts.map