Skip to content

Commit

Permalink
Fix issue where count_approx was not symmetric
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherswenson committed Sep 19, 2024
1 parent 5c56368 commit f257c8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/malloy/src/dialect/duckdb/dialect_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const count_approx: DefinitionBlueprint = {
takes: {'value': {dimension: 'any'}},
returns: {measure: 'number'},
impl: {function: 'APPROX_COUNT_DISTINCT'},
isSymmetric: true,
};

const string_agg: OverloadedDefinitionBlueprint = {
Expand Down
1 change: 1 addition & 0 deletions packages/malloy/src/dialect/trino/dialect_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const count_approx: DefinitionBlueprint = {
takes: {'value': {dimension: 'any'}},
returns: {measure: 'number'},
impl: {function: 'APPROX_DISTINCT'},
isSymmetric: true,
};

const max_by: DefinitionBlueprint = {
Expand Down
27 changes: 18 additions & 9 deletions test/src/databases/all/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,22 +1088,31 @@ expressionModels.forEach((x, databaseName) => {
});

describe('count_approx', () => {
test.when(runtime.dialect.supportsCountApprox)(
'works generally',
async () => {
await expect(`
const testIfSupported = test.when(runtime.dialect.supportsCountApprox);
testIfSupported('works generally', async () => {
await expect(`
// be accurate within 30%
// # test.debug
run: ${databaseName}.table('malloytest.state_facts') -> {
aggregate: passes is abs(count_approx(state)-count(state))/count(state) < 0.3
aggregate: also_passes is abs(count_approx(airport_count)-count(airport_count))/count(airport_count) < 0.3
}
`).malloyResultMatches(runtime, {
'passes': true,
'also_passes': true,
});
}
);
'passes': true,
'also_passes': true,
});
});
testIfSupported('works with fanout', async () => {
await expect(`
source: state_facts is ${databaseName}.table('malloytest.state_facts')
source: state_facts_fanout is ${databaseName}.table('malloytest.state_facts') extend {
join_cross: state_facts on true
}
run: state_facts_fanout -> {
aggregate: x is state_facts.state.count_approx() > 0
}
`).malloyResultMatches(runtime, {x: true});
});
});
describe('last_value', () => {
it(`works - ${databaseName}`, async () => {
Expand Down

0 comments on commit f257c8c

Please sign in to comment.