{"record":{"id":"c717fa8f17b0539d","repo":"cube-js/cube","slug":"unsupported-measure-type-replacement-for-sourcem","errorCode":null,"errorMessage":"Unsupported measure type replacement for ${sourceMeasure}: ${aggType} => ${newMeasureType}","messagePattern":"Unsupported measure type replacement for (.+?): (.+?) => (.+?)","errorType":"exception","errorClass":"UserError","httpStatus":null,"severity":"error","filePath":"packages/cubejs-schema-compiler/src/adapter/BaseMeasure.ts","lineNumber":43,"sourceCode":"      switch (aggType) {\n        case 'sum':\n        case 'avg':\n        case 'min':\n        case 'max':\n          switch (newMeasureType) {\n            case 'sum':\n            case 'avg':\n            case 'min':\n            case 'max':\n            case 'count_distinct':\n            case 'countDistinct':\n            case 'count_distinct_approx':\n            case 'countDistinctApprox':\n              // Can change from avg/... to count_distinct\n              // Latter does not care what input value is\n              // ok, do nothing\n              break;\n            default:\n              throw new UserError(\n                `Unsupported measure type replacement for ${sourceMeasure}: ${aggType} => ${newMeasureType}`\n              );\n          }\n          break;\n        case 'count_distinct':\n        case 'countDistinct':\n        case 'count_distinct_approx':\n        case 'countDistinctApprox':\n          switch (newMeasureType) {\n            case 'count_distinct':\n            case 'countDistinct':\n            case 'count_distinct_approx':\n            case 'countDistinctApprox':\n              // ok, do nothing\n              break;\n            default:\n              // Can not change from count_distinct to avg/...","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-schema-compiler/src/adapter/BaseMeasure.ts#L25-L61","documentation":"When a rolled-up/patched measure replaces an average-type aggregation (avg, etc.) with count_distinct_approx, Cube allows it only in specific cases; for the avg source branch, any newMeasureType not in the whitelisted set (count/count_distinct family that ignores input value) is rejected with this UserError. Averaging measures depend on their input values, so changing to an incompatible aggregation would silently produce wrong numbers.","triggerScenarios":"Calling query.rollup() or building a rollup query with a measure replacement (aggType from an avg-family measure patched to a newMeasureType like 'avg'->'sum' or 'avg'->'countDistinct' variant not permitted), via the query patch API (preAggregation rollups, rollupMeasure with modified aggregation).","commonSituations":"Defining pre-aggregation rollups where measure types don't match; refactoring data models and reusing a rolled-up measure under a different agg; programmatic query generation replacing measure types.","solutions":["Change the newMeasureType to one supported for the avg source (e.g. count, count_distinct_approx)","Define a separate measure for the desired aggregation instead of patching the avg measure","Remove the measure replacement from the rollup configuration so the original type is preserved","If it must be summed, define the measure as type: 'sum' in the data model rather than patching"],"exampleFix":"// before (rollup patch)\nmeasures: { 'Orders.avgTotal': { aggType: 'avg' } } // patched to newMeasureType 'sum'\n// after — define a dedicated measure in schema\nmeasures: { total: { type: 'sum', sql: 'amount' } }","handlingStrategy":"try-catch","validationCode":"// Ensure rollup measure replacements keep compatible aggregation types\nfunction checkMeasureReplacement(m) {\n  if (m.aggType?.startsWith('avg') && m.newMeasureType &&\n      !['count','count_distinct','count_distinct_approx'].includes(m.newMeasureType)) {\n    throw new Error(`Cannot patch avg measure to ${m.newMeasureType}`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await cubeApi.load(query);\n} catch (e) {\n  if (e.message.startsWith('Unsupported measure type replacement')) {\n    console.error('Rollup measure type mismatch:', e.message);\n    // rebuild query without the patched measure\n  }\n  throw e;\n}","preventionTips":["Only patch avg measures toward count-family aggregations","Define dedicated measures in the schema for each needed aggregation type","Keep rollup definitions in sync with measure definitions","Add schema-level tests for rollup configurations"],"tags":["schema-compiler","measure","rollup","user-error"],"backgroundTag":"unsupported-measure-replacement","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}