Skip to content

Commit

Permalink
Simulate left join in Presto (#1784)
Browse files Browse the repository at this point in the history
* Simulate left join.

* some typeos in code
  • Loading branch information
lloydtabb authored Jul 17, 2024
1 parent f277be6 commit 20051b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
16 changes: 12 additions & 4 deletions packages/malloy/src/dialect/trino/trino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,17 +621,25 @@ export class PrestoDialect extends TrinoDialect {
if (isArray) {
if (needDistinctKey) {
// return `LEFT JOIN UNNEST(transform(${source}, x -> CAST(ROW(x) as ROW(value) )) WITH ORDINALIITY as words_0(value,__row_id_from_${alias}) ON TRUE`;
return `CROSS JOIN UNNEST(${source}) WITH ORDINALITY as ${alias}(value, __row_id_from_${alias}) `;
return (
'-- Simulate a left join\n' +
`CROSS JOIN UNNEST(COALESCE(${source},ARRAY[NULL])) WITH ORDINALITY as ${alias}(value, __row_id_almost_${alias})\n` +
`CROSS JOIN UNNEST(ARRAY[CASE WHEN ${source} IS NOT NULL THEN __row_id_almost_${alias} END]) as ${alias}_ignore(__row_id_from_${alias})`
);
} else {
// return `CROSS JOIN UNNEST(zip_with(${source},array[],(r,ignore) -> (r, ignore))) as ${alias}(value, ignore)`;
return `CROSS JOIN UNNEST(${source}) as ${alias}(value) `;
return `CROSS JOIN UNNEST(COALESCE(${source}, ARRAY[NULL])) as ${alias}(value) `;
}
} else if (needDistinctKey) {
// return `CROSS JOIN UNNEST(zip_with(${source},array[],(r,ignore) -> (r, ignore))) WITH ORDINALITY as ${alias}_outer(${alias}, ignore,__row_id_from_${alias})`;
return `CROSS JOIN UNNEST(${source}) WITH ORDINALITY as ${alias}_outer(${alias}, __row_id_from_${alias})`;
return (
'-- Simulate a left join\n' +
`CROSS JOIN UNNEST(COALESCE(${source}, ARRAY[NULL])) WITH ORDINALITY as ${alias}_outer(${alias}, __row_id_almost_${alias})\n` +
`CROSS JOIN UNNEST(ARRAY[CASE WHEN ${source} IS NOT NULL THEN __row_id_almost_${alias} END]) as ${alias}_ignore(__row_id_from_${alias})`
);
} else {
// return `CROSS JOIN UNNEST(zip_with(${source},array[],(r,ignore) -> (r, ignore)))as ${alias}_outer(${alias},ignore)`;
return `CROSS JOIN UNNEST(${source}) as ${alias}_outer(${alias})`;
return `CROSS JOIN UNNEST(COALESCE(${source}, ARRAY[NULL])) as ${alias}_outer(${alias})`;
}
}
}
39 changes: 21 additions & 18 deletions test/src/databases/all/nomodel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1055,23 +1055,26 @@ SELECT row_to_json(finalStage) as row FROM __stage0 AS finalStage`);
}
);

test.when(runtime.supportsNesting && runtime.dialect.readsNestedData)(
`can unnest simply from file - ${databaseName}`,
async () => {
await expect(`
test.when(
runtime.supportsNesting &&
runtime.dialect.readsNestedData &&
databaseName !== 'presto'
)(`can unnest simply from file - ${databaseName}`, async () => {
await expect(`
source: ga_sample is ${databaseName}.table('malloytest.ga_sample')
run: ga_sample -> {
aggregate:
h is hits.count()
}
`).malloyResultMatches(runtime, {h: 13233});
}
);
});

test.when(runtime.supportsNesting && runtime.dialect.readsNestedData)(
`can unnest from file - ${databaseName}`,
async () => {
await expect(`
test.when(
runtime.supportsNesting &&
runtime.dialect.readsNestedData &&
databaseName !== 'presto'
)(`can unnest from file - ${databaseName}`, async () => {
await expect(`
source: ga_sample is ${databaseName}.table('malloytest.ga_sample')
run: ga_sample -> {
where: hits.product.productBrand != null
Expand All @@ -1084,22 +1087,22 @@ SELECT row_to_json(finalStage) as row FROM __stage0 AS finalStage`);
p is hits.product.count()
}
`).malloyResultMatches(runtime, {h: 1192, c: 681, p: 1192});
}
);
});

test.when(runtime.supportsNesting && runtime.dialect.readsNestedData)(
`can double unnest - ${databaseName}`,
async () => {
await expect(`
test.when(
runtime.supportsNesting &&
runtime.dialect.readsNestedData &&
databaseName !== 'presto'
)(`can double unnest - ${databaseName}`, async () => {
await expect(`
source: ga_sample is ${databaseName}.table('malloytest.ga_sample')
run: ga_sample -> {
aggregate:
p is floor(hits.product.productPrice.avg())
}
`).malloyResultMatches(runtime, {p: 23001594});
}
);
});

test.when(runtime.supportsNesting)(
'nest null - ${databaseName}',
Expand Down

0 comments on commit 20051b5

Please sign in to comment.