Skip to content

Commit

Permalink
Add defaultRowLimit parameter when compiling queries (#2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherswenson authored Jan 16, 2025
1 parent 332e32c commit 59b23ea
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/malloy/src/malloy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ interface CompileQueryOptions {
replaceMaterializedReferences?: boolean;
materializedTablePrefix?: string;
eventStream?: EventStream;
defaultRowLimit?: number;
}

export class Malloy {
Expand Down Expand Up @@ -443,7 +444,10 @@ export class Malloy {
}
const compiledSql = queryModel.compileQuery(
segment,
options,
{
...options,
defaultRowLimit: undefined,
},
false
).sql;
selectStr += parenAlready ? compiledSql : `(${compiledSql})`;
Expand Down
21 changes: 21 additions & 0 deletions packages/malloy/src/model/malloy_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5004,12 +5004,33 @@ export class QueryModel {
};
}

addDefaultRowLimit(query: Query, defaultRowLimit?: number): Query {
if (defaultRowLimit === undefined) return query;
const lastSegment = query.pipeline[query.pipeline.length - 1];
if (lastSegment.type === 'raw') return query;
if (lastSegment.limit !== undefined) return query;
return {
...query,
pipeline: [
...query.pipeline.slice(0, -1),
{
...lastSegment,
limit: defaultRowLimit,
},
],
};
}

compileQuery(
query: Query,
prepareResultOptions?: PrepareResultOptions,
finalize = true
): CompiledQuery {
let newModel: QueryModel | undefined;
query = this.addDefaultRowLimit(
query,
prepareResultOptions?.defaultRowLimit
);
const m = newModel || this;
const ret = m.loadQuery(
query,
Expand Down
1 change: 1 addition & 0 deletions packages/malloy/src/model/malloy_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,7 @@ export interface SearchValueMapResult {
export interface PrepareResultOptions {
replaceMaterializedReferences?: boolean;
materializedTablePrefix?: string;
defaultRowLimit?: number;
}

type UTD =
Expand Down
40 changes: 40 additions & 0 deletions test/src/core/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,44 @@ describe('tags', () => {
});
});

describe('default row limit', () => {
test('default row limit gets added to query', async () => {
const query = runtime.loadQuery(
"run: duckdb.table('malloytest.aircraft') -> { group_by: state }"
);
const result = await query.run({defaultRowLimit: 1});
expect(result.data.rowCount).toBe(1);
});

test('default row limit does not get added to raw query', async () => {
const query = runtime.loadQuery(
`
run: duckdb.sql("""
SELECT 1 as value
UNION ALL
SELECT 2 as value
""")
`
);
const result = await query.run({defaultRowLimit: 1});
expect(result.data.rowCount).toBe(2);
});

test('default row limit does not override existing limit', async () => {
const query = runtime.loadQuery(
"run: duckdb.table('malloytest.aircraft') -> { group_by: state; limit: 2 }"
);
const result = await query.run({defaultRowLimit: 1});
expect(result.data.rowCount).toBe(2);
});

test('no default row limit does not add row limit', async () => {
const query = runtime.loadQuery(
"run: duckdb.table('malloytest.aircraft') -> { group_by: state }"
);
const result = await query.run();
expect(result.data.rowCount).toBe(10); // Weird that there are only 10 states in this table?
});
});

afterAll(async () => await runtime.connection.close());

0 comments on commit 59b23ea

Please sign in to comment.