Skip to content

Commit

Permalink
[WIP add test] fix(server-core): Handle empty query in getSqlGenerator
Browse files Browse the repository at this point in the history
api-gateway calls getSqlGenerator with empty query and concrete data source to initialize SQL API
But because query is empty `sqlGenerator.dataSource` can be undefined, and it would trigger re-creating query with new data source, which would be `default`
  • Loading branch information
mcheshkov committed Feb 25, 2025
1 parent cd90d56 commit a03d9dd
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/cubejs-server-core/src/core/CompilerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,26 @@ export class CompilerApi {
throw new Error(`Unknown dbType: ${dbType}`);
}

// sqlGenerator.dataSource can return undefined for query without members
// Queries like this are used by api-gateway to initialize SQL API
// At the same time, those queries should use concrete dataSource, so we should be good to go with it
dataSource = compilers.compiler.withQuery(sqlGenerator, () => sqlGenerator.dataSource);
const _dbType = await this.getDbType(dataSource);
if (dataSource !== 'default' && dbType !== _dbType) {
// TODO consider more efficient way than instantiating query
sqlGenerator = await this.createQueryByDataSource(
compilers,
query,
dataSource,
_dbType
);
if (dataSource !== undefined) {
const _dbType = await this.getDbType(dataSource);
if (dataSource !== 'default' && dbType !== _dbType) {
// TODO consider more efficient way than instantiating query
sqlGenerator = await this.createQueryByDataSource(
compilers,
query,
dataSource,
_dbType
);

if (!sqlGenerator) {
throw new Error(`Can't find dialect for '${dataSource}' data source: ${_dbType}`);
if (!sqlGenerator) {
throw new Error(
`Can't find dialect for '${dataSource}' data source: ${_dbType}`
);
}
}
}

Expand Down

0 comments on commit a03d9dd

Please sign in to comment.