The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import _ from "underscore";
import type {
AggregateFieldReference,
DimensionReference,
DimensionReferenceWithOptions,
ExpressionReference,
FieldId,
FieldReference,
ReferenceOptions,
TemplateTagReference,
} from "metabase-types/api";
export const isFieldReference = (mbql: any): mbql is FieldReference => {
return Array.isArray(mbql) && mbql.length === 3 && mbql[0] === "field";
};
export const isExpressionReference = (
mbql: any,
): mbql is ExpressionReference => {
return Array.isArray(mbql) && mbql.length >= 2 && mbql[0] === "expression";
};
export const isAggregationReference = (
mbql: any,
): mbql is AggregateFieldReference => {
return Array.isArray(mbql) && mbql[0] === "aggregation";
};
export const isTemplateTagReference = (
mbql: any,
): mbql is TemplateTagReference => {
return Array.isArray(mbql) && mbql[0] === "template-tag";
};
export const createFieldReference = (
columnNameOrFieldId: string | FieldId,
// Unjustified type cast. FIXME
): FieldReference => ["field", columnNameOrFieldId, null] as FieldReference;
export const isValidDimensionReference = (
mbql: any,
): mbql is DimensionReference => {
return [
isFieldReference,
isExpressionReference,
isAggregationReference,
isTemplateTagReference,
].some((predicate) => predicate(mbql));
};
export const isDimensionReferenceWithOptions = (
mbql: unknown,
): mbql is DimensionReferenceWithOptions => {
return (
isFieldReference(mbql) ||
isExpressionReference(mbql) ||
isAggregationReference(mbql)
);
};
export const normalizeReferenceOptions = (
options?: ReferenceOptions | null,
): ReferenceOptions | null => {
if (!options) {
return null;
}
// recursively normalize maps inside options.
options = _.mapObject(options, (val) =>
typeof val === "object" ? normalizeReferenceOptions(val) : val,
);
// remove null/undefined options from map.
options = _.omit(options, (value) => value == null);
return _.isEmpty(options) ? null : options;
};
export const getNormalizedDimensionReference = (
mbql: DimensionReference,
): DimensionReference => {
if (
isFieldReference(mbql) ||
isExpressionReference(mbql) ||
isAggregationReference(mbql)
) {
// Unjustified type cast. FIXME
const normalizedReference = [...mbql] as DimensionReference;
const normalizedOptions = normalizeReferenceOptions(mbql[2]);
normalizedReference[2] = normalizedOptions;
// MBQL normalization drops empty options from expression and aggregation
// references, while field references keep the options position.
if (normalizedOptions == null && !isFieldReference(normalizedReference)) {
// slice does not narrow the tuple type
return normalizedReference.slice(0, 2) as DimensionReference;
}
return normalizedReference;
}
return mbql;
};
const getDimensionReferenceWithoutOptions = (
mbql: DimensionReferenceWithOptions,
optionsKeysToOmit: string[],
): DimensionReferenceWithOptions => {
// Unjustified type cast. FIXME
const newReference = mbql.slice() as DimensionReferenceWithOptions;
const options = newReference[2];
if (!options) {
return newReference;
}
newReference[2] =
options == null ? null : _.omit(options, ...optionsKeysToOmit);
if (_.isEmpty(newReference[2])) {
newReference[2] = null;
}
return newReference;
};
export const BASE_DIMENSION_REFERENCE_OMIT_OPTIONS = [
"temporal-unit",
"binning",
];
export const getDimensionReferenceWithoutTemporalUnitAndBinning = (
mbql: DimensionReferenceWithOptions,
) =>
getDimensionReferenceWithoutOptions(
mbql,
BASE_DIMENSION_REFERENCE_OMIT_OPTIONS,
);
const BASE_TYPE_OPTIONS = ["base-type"];
export const getDimensionReferenceWithoutBaseType = (
mbql: DimensionReferenceWithOptions,
) => getDimensionReferenceWithoutOptions(mbql, BASE_TYPE_OPTIONS);
/**
* Whether this Field clause has a string Field name (as opposed to an integer Field ID). This generally means the
* Field comes from a native query.
*/
export const hasStringFieldName = (mbql: FieldReference) =>
typeof mbql[1] === "string";