{"record":{"id":"2cb04a6f12ed7ab4","repo":"cube-js/cube","slug":"pre-aggregation-definition-is-malformed","errorCode":null,"errorMessage":"Pre-aggregation definition is malformed","messagePattern":"Pre-aggregation definition is malformed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cubejs-schema-compiler/src/compiler/converters/CubePreAggregationConverter.ts","lineNumber":46,"sourceCode":"  }\n\n  protected convertJS(cubeDefSet: JsSet) {\n    const { preAggregationName, code } = this.preAggregationDefinition;\n    const { cubeDefinition } = cubeDefSet;\n\n    let preAggregationNode: t.ObjectExpression | null = null;\n    const preAggregationAst = parse(`(${code})`);\n\n    if (t.isExpressionStatement(preAggregationAst.program.body[0])) {\n      const [statement] = preAggregationAst.program.body;\n\n      if (t.isObjectExpression(statement.expression)) {\n        preAggregationNode = statement.expression;\n      }\n    }\n\n    if (preAggregationNode === null) {\n      throw new Error('Pre-aggregation definition is malformed');\n    }\n\n    let anchor: t.ObjectExpression | null = null;\n\n    cubeDefinition.properties.forEach((prop) => {\n      if (t.isObjectProperty(prop) && t.isIdentifier(prop.key)) {\n        if (prop.key.name === 'preAggregations' && t.isObjectExpression(prop.value)) {\n          anchor = prop.value;\n\n          prop.value.properties.forEach((p) => {\n            if (t.isObjectProperty(p) && t.isIdentifier(p.key)) {\n              if (p.key.name === preAggregationName) {\n                throw new UserError(`Pre-aggregation '${preAggregationName}' is already defined`);\n              }\n            }\n          });\n        }\n      }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-schema-compiler/src/compiler/converters/CubePreAggregationConverter.ts#L28-L64","documentation":"CubePreAggregationConverter.convertJS parses a pre-aggregation definition snippet with Babel and expects the first statement to be an object expression ({ ... }). If the parsed code is not an object expression (or fails that shape check), a plain Error 'Pre-aggregation definition is malformed' is thrown during AST-based pre-aggregation conversion.","triggerScenarios":"Submitting a preAggregations definition whose code parses to something other than an object literal — e.g. a function call, identifier, array, or empty snippet — via the schema converter API or playground pre-aggregation flows.","commonSituations":"Programmatic pre-aggregation creation where the code string was truncated or built incorrectly, IDE copy/paste dropping the braces, or passing a function-style definition where an object is required.","solutions":["Ensure the pre-aggregation code is a complete object literal, e.g. { measures: [...], timeDimension: ..., granularity: 'day' }","Log/inspect the code string passed to the converter before conversion","Wrap non-expression snippets in parentheses/braces so Babel parses them as ObjectExpression","Catch and report this Error upstream since it is a plain Error, not UserError"],"exampleFix":"// before\nconst code = \"rollups => rollups.main()\";\n\n// after\nconst code = \"{ measures: [orders.count], timeDimension: orders.createdAt, granularity: 'day' }\";","handlingStrategy":"validation","validationCode":"const { parse } = require('@babel/parser');\nconst t = require('@babel/types');\nfunction isObjectLiteralDefinition(code) {\n  const ast = parse(`(${code})`);\n  const [stmt] = ast.program.body;\n  return t.isExpressionStatement(stmt) && t.isObjectExpression(stmt.expression);\n}","typeGuard":null,"tryCatchPattern":"try {\n  converter.convert(astByCubeName);\n} catch (e) {\n  if (e.message === 'Pre-aggregation definition is malformed') {\n    console.error('Pre-aggregation code must be an object literal');\n  }\n}","preventionTips":["Always pass complete '{ ... }' object literals as pre-aggregation code","Validate the generated code string before conversion","Beware plain Errors here (not UserError) — catch generically in tooling"],"tags":["pre-aggregations","ast","validation"],"backgroundTag":"preaggregation-definition-malformed","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}