Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion meerkat-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/meerkat-browser",
"version": "0.0.134",
"version": "0.0.135",
"dependencies": {
"tslib": "^2.3.0",
"@devrev/meerkat-core": "*",
Expand Down
2 changes: 1 addition & 1 deletion meerkat-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/meerkat-core",
"version": "0.0.134",
"version": "0.0.135",
"dependencies": {
"tslib": "^2.3.0"
},
Expand Down
182 changes: 163 additions & 19 deletions meerkat-core/src/joins/v2/joins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { MeerkatQueryFilter, StructuredJoin, TableSchema } from '../../types/cube-types';
import {
MeerkatQueryFilter,
StructuredJoin,
TableSchema,
} from '../../types/cube-types';
import { GetQueryOutput } from '../../utils/duckdb-ast-parse-serialize';
import { createDirectedGraphV2, generateSqlQueryV2 } from './joins';

Expand Down Expand Up @@ -45,7 +49,10 @@ const sqlMapOf = (schemas: TableSchema[]): { [k: string]: string } =>

describe('joins-v2', () => {
it('emits a plain equi-join when from is scalar', async () => {
const schemas = [scalar('orders', ['id', 'customer_id']), scalar('customers')];
const schemas = [
scalar('orders', ['id', 'customer_id']),
scalar('customers'),
];
const sqlMap = sqlMapOf(schemas);
const paths: StructuredJoin[][] = [
[
Expand Down Expand Up @@ -247,14 +254,33 @@ describe('joins-v2', () => {
const bridgeTables = new Set(['link']);
const paths: StructuredJoin[][] = [
[
{ from: { table: 'issue', column: 'id' }, to: { table: 'link', column: 'source_id' } },
{ from: { table: 'link', column: 'target_id' }, to: { table: 'part', column: 'id' } },
{ from: { table: 'issue', column: 'id' }, to: { table: 'link', column: 'source_id' } },
{ from: { table: 'link', column: 'target_id' }, to: { table: 'user', column: 'id' } },
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'source_id' },
},
{
from: { table: 'link', column: 'target_id' },
to: { table: 'part', column: 'id' },
},
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'source_id' },
},
{
from: { table: 'link', column: 'target_id' },
to: { table: 'user', column: 'id' },
},
],
];
const graph = createDirectedGraphV2(schemas, sqlMap, paths);
const sql = await generateSqlQueryV2(paths, sqlMap, graph, schemas, undefined, bridgeTables);
const sql = await generateSqlQueryV2(
paths,
sqlMap,
graph,
schemas,
undefined,
bridgeTables
);

expect(sql).toContain('issue.id = link.source_id');
expect(sql).toContain('link.target_id = part.id');
Expand All @@ -273,16 +299,35 @@ describe('joins-v2', () => {
const bridgeTables = new Set(['link']);
const paths: StructuredJoin[][] = [
[
{ from: { table: 'issue', column: 'id' }, to: { table: 'link', column: 'source_id' } },
{ from: { table: 'link', column: 'target_id' }, to: { table: 'part', column: 'id' } },
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'source_id' },
},
{
from: { table: 'link', column: 'target_id' },
to: { table: 'part', column: 'id' },
},
],
[
{ from: { table: 'issue', column: 'id' }, to: { table: 'link', column: 'source_id' } },
{ from: { table: 'link', column: 'target_id' }, to: { table: 'user', column: 'id' } },
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'source_id' },
},
{
from: { table: 'link', column: 'target_id' },
to: { table: 'user', column: 'id' },
},
],
];
const graph = createDirectedGraphV2(schemas, sqlMap, paths);
const sql = await generateSqlQueryV2(paths, sqlMap, graph, schemas, undefined, bridgeTables);
const sql = await generateSqlQueryV2(
paths,
sqlMap,
graph,
schemas,
undefined,
bridgeTables
);

expect(sql).toContain('issue.id = link.source_id');
expect(sql).toContain('link.target_id = part.id');
Expand All @@ -299,9 +344,18 @@ describe('joins-v2', () => {
const sqlMap = sqlMapOf(schemas);
const paths: StructuredJoin[][] = [
[
{ from: { table: 'issue', column: 'id' }, to: { table: 'link', column: 'source_id' } },
{ from: { table: 'link', column: 'target_id' }, to: { table: 'part', column: 'id' } },
{ from: { table: 'issue', column: 'id' }, to: { table: 'link', column: 'source_id' } },
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'source_id' },
},
{
from: { table: 'link', column: 'target_id' },
to: { table: 'part', column: 'id' },
},
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'source_id' },
},
],
];
const graph = createDirectedGraphV2(schemas, sqlMap, paths);
Expand Down Expand Up @@ -440,12 +494,101 @@ describe('joins-v2', () => {
);

expect(callCount).toBe(1);
expect(sql).toContain("issue.id = link.source_id AND (link_type_id = 'type_a')");
expect(sql).toContain(
"issue.id = link.source_id AND (link_type_id = 'type_a')"
);
expect(sql).toContain('link.target_id = part.id');
expect(sql).toContain("part.id = link__1.source_id AND (link_type_id = 'type_b')");
expect(sql).toContain(
"part.id = link__1.source_id AND (link_type_id = 'type_b')"
);
expect(sql).toContain('link__1.target_id = user.id');
});

it('rewrites condition members to the aliased bridge table (link__1)', async () => {
// Reproduces: Issue → link (Child of) → Enhancement and Issue → link
// (Dependency of) → Ticket. The second link join must filter on
// link__1.link_type_id, not the first link's link_type_id.
const schemas = [
scalar('issue', ['id']),
scalar('link', ['id', 'source_id', 'target_id', 'link_type_id']),
scalar('enhancement', ['id', 'name']),
scalar('ticket', ['id', 'severity']),
];
const sqlMap = sqlMapOf(schemas);
const bridgeTables = new Set(['link']);
const paths: StructuredJoin[][] = [
[
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'target_id' },
condition: {
member: 'link.link_type_id',
operator: 'equals',
values: ['custom_link_type/20'],
},
},
{
from: { table: 'link', column: 'source_id' },
to: { table: 'enhancement', column: 'id' },
},
],
[
{
from: { table: 'issue', column: 'id' },
to: { table: 'link', column: 'target_id' },
condition: {
member: 'link.link_type_id',
operator: 'equals',
values: ['custom_link_type/default-20'],
},
},
{
from: { table: 'link', column: 'source_id' },
to: { table: 'ticket', column: 'id' },
},
],
];
const graph = createDirectedGraphV2(schemas, sqlMap, paths);

let serializedAstQuery = '';
const mockGetQueryOutput: GetQueryOutput = async (query) => {
serializedAstQuery = query;
return [
{
result:
"SELECT (link.link_type_id = 'custom_link_type/20') AS __meerkat_batch_expr_0__, (link__1.link_type_id = 'custom_link_type/default-20') AS __meerkat_batch_expr_1__;",
},
];
};

const sql = await generateSqlQueryV2(
paths,
sqlMap,
graph,
schemas,
mockGetQueryOutput,
bridgeTables
);

// Condition AST must reference the aliased instance — this is what
// production DuckDB serialization emits as link__1.link_type_id.
expect(serializedAstQuery).toContain(
'"column_names":["link","link_type_id"]'
);
expect(serializedAstQuery).toContain(
'"column_names":["link__1","link_type_id"]'
);
expect(sql).toContain(
"issue.id = link.target_id AND (link.link_type_id = 'custom_link_type/20')"
);
expect(sql).toContain(
"issue.id = link__1.target_id AND (link__1.link_type_id = 'custom_link_type/default-20')"
);
expect(sql).not.toContain(
"issue.id = link__1.target_id AND (link.link_type_id = 'custom_link_type/default-20')"
);
});

it('handles notSet condition (IS NULL)', async () => {
const schemas = [
scalar('issue', ['id']),
Expand Down Expand Up @@ -526,7 +669,8 @@ describe('joins-v2', () => {
);

expect(sql).toContain('issue.id = link.source_id AND');
expect(sql).toContain("(link_type_id = 'type_a') OR (link_type_id = 'type_b')");
expect(sql).toContain(
"(link_type_id = 'type_a') OR (link_type_id = 'type_b')"
);
});

});
37 changes: 33 additions & 4 deletions meerkat-core/src/joins/v2/joins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cubeFilterToDuckdbAST } from '../../cube-filter-transformer/factory';
import { traverseMeerkatQueryFilter } from '../../filter-params/filter-params-ast';
import { getUsedTableSchema } from '../../get-used-table-schema/get-used-table-schema';
import { memberKeyToSafeKey } from '../../member-formatters/member-key-to-safe-key';
import { splitIntoDataSourceAndFields } from '../../member-formatters/split-into-data-source-and-fields';
import {
MeerkatQueryFilter,
Query,
Expand Down Expand Up @@ -199,6 +200,21 @@ const inferBridgeTables = (
return bridges;
};

const rewriteConditionTableAlias = (
condition: MeerkatQueryFilter,
originalTable: string,
aliasedTable: string
): MeerkatQueryFilter => {
const rewritten: MeerkatQueryFilter = JSON.parse(JSON.stringify(condition));
traverseMeerkatQueryFilter([rewritten], (filter) => {
const [table, fields] = splitIntoDataSourceAndFields(filter.member);
if (table === originalTable) {
filter.member = `${aliasedTable}.${fields}`;
}
});
return rewritten;
};

const aliasBridgeTables = (
paths: StructuredJoin[][],
bridgeTables: Set<string>
Expand All @@ -225,10 +241,23 @@ const aliasBridgeTables = (
continue;
}

const alias = `${edge.to.table}__${count}`;
result.push({ ...edge, to: { ...edge.to, table: alias } });

if (i + 1 < path.length && path[i + 1].from.table === edge.to.table) {
const originalTo = edge.to.table;
const alias = `${originalTo}__${count}`;
result.push({
...edge,
to: { ...edge.to, table: alias },
...(edge.condition
? {
condition: rewriteConditionTableAlias(
edge.condition,
originalTo,
alias
),
}
: {}),
});

if (i + 1 < path.length && path[i + 1].from.table === originalTo) {
nextFromAlias = alias;
}
}
Expand Down
2 changes: 1 addition & 1 deletion meerkat-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/meerkat-node",
"version": "0.0.134",
"version": "0.0.135",
"dependencies": {
"@devrev/meerkat-core": "*",
"@swc/helpers": "~0.5.0",
Expand Down
Loading