Skip to content

Commit

Permalink
Joined filtered Sources (#1883)
Browse files Browse the repository at this point in the history
* Joined filtered Sources

* It turns out the postgres and snowflake really didn't support this.
the joined nested table was inaccessible.  Stubbing for now.
  • Loading branch information
lloydtabb authored Sep 7, 2024
1 parent 5196b21 commit 89920bc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/malloy/src/dialect/postgres/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class PostgresDialect extends PostgresBase {
supportsNesting = true;
experimental = false;
readsNestedData = false;
supportsComplexFilteredSources = false;

quoteTablePath(tablePath: string): string {
return tablePath
Expand Down
1 change: 1 addition & 0 deletions packages/malloy/src/dialect/snowflake/snowflake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class SnowflakeDialect extends Dialect {
dontUnionIndex = false;
supportsQualify = false;
supportsPipelinesInViews = false;
supportsComplexFilteredSources = false;

// don't mess with the table pathing.
quoteTablePath(tablePath: string): string {
Expand Down
26 changes: 20 additions & 6 deletions packages/malloy/src/model/malloy_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2775,17 +2775,31 @@ class QueryQuery extends QueryField {
qf.generateExpression(this.rootResult)
);
}
if (ji.children.length === 0 || conditions === undefined) {

if (
ji.children.length === 0 ||
conditions === undefined ||
!this.parent.dialect.supportsComplexFilteredSources
) {
// LTNOTE: need a check here to see the children's where: conditions are local
// to the source and not to any of it's joined children.
// In Presto, we're going to get a SQL error if in this case
// for now. We need to inspect the 'condition' of each of the children
// to see if they reference subchildren and blow up if they do
// or move them to the where clause with a (x.distnct_key is NULL or (condition))
//
// const childrenFiltersAreComplex = somethign(conditions)
// if (conditions && childrenFiltersAreComplex !this.parent.dialect.supportsComplexFilteredSources) {
// throw new Error(
// 'Cannot join a source with a complex filter on a joined source'
// );
// }

if (conditions !== undefined && conditions.length >= 1) {
filters = ` AND (${conditions.join(' AND ')})`;
}
s += ` ${matrixOperation} JOIN ${structSQL} AS ${ji.alias}\n ON ${onCondition}${filters}\n`;
} else {
if (!this.parent.dialect.supportsComplexFilteredSources) {
throw new Error(
'Cannot join a source with a filter on a joined source'
);
}
let select = `SELECT ${ji.alias}.*`;
let joins = '';
for (const childJoin of ji.children) {
Expand Down
27 changes: 27 additions & 0 deletions test/src/databases/all/expr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,33 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
`).malloyResultMatches(runtime, {model_count: 60461, b_count: 355});
}
);

test('joined filtered explores with NO dependencies', async () => {
await expect(`
source: sf is ${databaseName}.table('malloytest.state_facts') extend {
measure: state_count is count()
primary_key: state
}
source: al is sf extend {where: state = 'AL'}
source: a is sf extend {
where: state ~ 'A%'
join_one: al with state
}
source: allx is sf extend {
join_one: a with state
}
// # test.debug
run: allx -> {
aggregate:
allx is state_count
a is a.state_count
al is a.al.state_count
}
`).malloyResultMatches(runtime, {allx: 51, a: 4, al: 1});
});
});

describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
Expand Down

0 comments on commit 89920bc

Please sign in to comment.