Implementation:Apache Druid MeasurePattern
| Knowledge Sources | |
|---|---|
| Domains | Web_Console, Explore_View |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
An immutable value class representing a simplified aggregate measure pattern (count, sum, min, max, or countDistinct) over a column.
Description
The MeasurePattern class models common aggregate patterns found in Druid queries. It can be constructed directly from aggregate type and column name, fitted from an existing SqlExpression via the static `fit` method, or created from a Column object via `fromColumn`. The class supports conversion to and from SqlExpression AST nodes (e.g., COUNT(*), SUM("col"), COUNT(DISTINCT "col")), pretty-printing for display, and immutable changes to the aggregate type or column. The supported aggregate types are: count, sum, min, max, and countDistinct.
Usage
Used in the explore view's measure configuration UI to represent, edit, and convert between user-facing measure patterns and their corresponding SQL aggregate expressions.
Code Reference
Source Location
- Repository: Apache Druid
- File: web-console/src/views/explore-view/models/measure-pattern.ts
- Lines: 1-148
Signature
export type MeasurePatternAggregate = 'count' | 'sum' | 'min' | 'max' | 'countDistinct';
export class MeasurePattern {
static AGGREGATES: MeasurePatternAggregate[];
static fromColumn(column: Column): MeasurePattern;
static fit(expression: SqlExpression): MeasurePattern | undefined;
public readonly aggregate: MeasurePatternAggregate;
public readonly column: string;
constructor(value: MeasurePatternValue);
valueOf(): MeasurePatternValue;
toString(): string;
prettyPrint(): string;
changeAggregate(aggregate: MeasurePatternAggregate): MeasurePattern;
changeColumn(column: string): MeasurePattern;
toExpression(): SqlExpression;
}
Import
import { MeasurePattern } from '../models/measure-pattern';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | MeasurePatternValue |
Yes | Object with aggregate type and optional column name (defaults to '*') |
| expression | SqlExpression |
Yes | A SQL aggregate expression to reverse-engineer into a MeasurePattern (fit) |
| column | Column |
Yes | A druid-query-toolkit Column to derive a default measure pattern from (fromColumn) |
Outputs
| Name | Type | Description |
|---|---|---|
| (return) | MeasurePattern |
A new or modified MeasurePattern instance |
| (return) | SqlExpression |
The SQL aggregate expression corresponding to this pattern (toExpression) |
| (return) | string |
A human-readable description like "Count", "Sum revenue", "Dist. user_id" (prettyPrint) |
Usage Examples
Create from a column and convert to expression
import { MeasurePattern } from '../models/measure-pattern';
const pattern = MeasurePattern.fromColumn({ name: 'revenue', sqlType: 'DOUBLE', nativeType: 'DOUBLE' });
const expr = pattern.toExpression();
// expr represents: SUM("revenue")
Fit a pattern from an existing SQL expression
import { MeasurePattern } from '../models/measure-pattern';
import { SqlFunction } from 'druid-query-toolkit';
const pattern = MeasurePattern.fit(SqlFunction.COUNT_STAR);
// pattern.aggregate === 'count'
// pattern.column === '*'
Change aggregate type
const sumPattern = new MeasurePattern({ aggregate: 'sum', column: 'revenue' });
const maxPattern = sumPattern.changeAggregate('max');
// maxPattern.toExpression() represents: MAX("revenue")