{"record":{"id":"d681efe59253f0f2","repo":"cube-js/cube","slug":"approximate-distinct-count-is-not-supported-by-thi","errorCode":null,"errorMessage":"Approximate distinct count is not supported by this DB","messagePattern":"Approximate distinct count is not supported by this DB","errorType":"exception","errorClass":"UserError","httpStatus":null,"severity":"error","filePath":"packages/cubejs-schema-compiler/src/adapter/BaseQuery.js","lineNumber":4014,"sourceCode":"  hllCardinality(_sql) {\n    throw new UserError('Distributed approximate distinct count is not supported by this DB');\n  }\n\n  hllMergeOnly(sql) {\n    return this.hllMerge(sql);\n  }\n\n  hllCardinalityMerge(sql) {\n    return this.hllMerge(sql);\n  }\n\n  castToString(sql) {\n    return `CAST(${sql} as TEXT)`;\n  }\n\n  // eslint-disable-next-line @typescript-eslint/no-unused-vars\n  countDistinctApprox(sql) {\n    throw new UserError('Approximate distinct count is not supported by this DB');\n  }\n\n  primaryKeyCount(cubeName, distinct) {\n    const primaryKeys = this.cubeEvaluator.primaryKeys[cubeName];\n    const primaryKeySql = primaryKeys.length > 1 ?\n      this.concatStringsSql(primaryKeys.map((pk) => this.castToString(this.primaryKeySql(pk, cubeName)))) :\n      this.primaryKeySql(primaryKeys[0], cubeName);\n    return `count(${distinct ? 'distinct ' : ''}${primaryKeySql})`;\n  }\n\n  renderDimensionCase(symbol, cubeName) {\n    const when = symbol.case.when.map(w => ({\n      sql: this.evaluateSql(cubeName, w.sql),\n      label: this.renderDimensionCaseLabel(w.label, cubeName)\n    }));\n    return this.caseWhenStatement(\n      when,\n      symbol.case.else && this.renderDimensionCaseLabel(symbol.case.else.label, cubeName)","sourceCodeStart":3996,"sourceCodeEnd":4032,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-schema-compiler/src/adapter/BaseQuery.js#L3996-L4032","documentation":"countDistinctApprox() is the dialect hook that generates the approximate distinct count SQL (e.g. APPROX_COUNT_DISTINCT, HLL_COUNT.INIT). BaseQuery's default implementation throws this UserError, meaning the DB dialect never opted into approximate counting.","triggerScenarios":"Querying a measure with type countDistinctApprox (or SQL_API features requiring it) against an adapter that does not override countDistinctApprox.","commonSituations":"Copy-pasting a schema with countDistinctApprox measures from a BigQuery/ClickHouse project to a DB like MySQL or plain Postgres without the HLL extension.","solutions":["Change the measure type from countDistinctApprox to countDistinct","Use a data source adapter supporting approximate counts","Extend your adapter and implement countDistinctApprox using a native approximation function or a UDF"],"exampleFix":"// before\nmeasures:\n  - name: unique_users\n    type: countDistinctApprox\n    sql: user_id\n// after\nmeasures:\n  - name: unique_users\n    type: countDistinct\n    sql: user_id","handlingStrategy":"fallback","validationCode":"const approxSupporting = ['postgres','bigquery','clickhouse','snowflake','duckdb','databricks-jdbc'];\nif (!approxSupporting.includes(dataSource)) {\n  throw new Error(`countDistinctApprox unsupported on ${dataSource}; use countDistinct`);\n}","typeGuard":"function supportsApproxCount(adapter) {\n  return typeof adapter.countDistinctApprox === 'function' &&\n    adapter.countDistinctApprox !== BaseQuery.prototype.countDistinctApprox;\n}","tryCatchPattern":"try { return await cube.load(query); } catch (e) {\n  if (/Approximate distinct count is not supported/.test(e.message)) {\n    return await cube.load(swapApproxForExact(query));\n  }\n  throw e;\n}","preventionTips":["Keep measure definitions DB-agnostic; select approx vs exact per data source","Document which drivers support countDistinctApprox for your team"],"tags":["schema-compiler","approximate-distinct-count","unsupported-feature"],"backgroundTag":"approx-distinct-count-unsupported","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}