Skip to content

Commit e7fb2f2

Browse files
committed
fix: Sanity check for silent truncate name problem during pre-aggregation creation
1 parent 438a852 commit e7fb2f2

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

packages/cubejs-query-orchestrator/orchestrator/PreAggregations.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,13 @@ class PreAggregationLoader {
260260

261261
const mostRecentTargetTableName = async () => {
262262
await this.loadCache.reset(this.preAggregation);
263-
return this.targetTableName(
264-
getVersionEntryByContentVersion(
265-
await this.loadCache.getVersionEntries(this.preAggregation)
266-
)
263+
const lastVersion = getVersionEntryByContentVersion(
264+
await this.loadCache.getVersionEntries(this.preAggregation)
267265
);
266+
if (!lastVersion) {
267+
throw new Error(`Pre-aggregation table is not found for ${this.preAggregation.tableName} after it was successfully created. It usually means database silently truncates table names due to max name length.`);
268+
}
269+
return this.targetTableName(lastVersion);
268270
};
269271

270272
if (versionEntry) {

packages/cubejs-query-orchestrator/test/QueryOrchestratorTest.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ class MockDriver {
2222
}
2323

2424
async loadPreAggregationIntoTable(preAggregationTableName) {
25-
this.tables.push(preAggregationTableName);
25+
this.tables.push(preAggregationTableName.substring(0, 100));
26+
}
27+
28+
async dropTable(tableName) {
29+
this.tables = this.tables.filter(t => t !== tableName.split('.')[1]);
2630
}
2731
}
2832

@@ -83,4 +87,30 @@ describe('QueryOrchestrator', () => {
8387
should(result.data[0]).match(/orders_number_and_count20191101_l3kvjcmu_khbemovd/);
8488
should(mockDriver.executedQueries).matchAny(/CREATE INDEX orders_number_and_count_week20191101_l3kvjcmu_khbemovd/);
8589
});
90+
91+
it('silent truncate', async () => {
92+
const query = {
93+
query: "SELECT \"orders__created_at_week\" \"orders__created_at_week\", sum(\"orders__count\") \"orders__count\" FROM (SELECT * FROM stb_pre_aggregations.orders_number_and_count_and_very_very_very_very_very_very_long20191101) as partition_union WHERE (\"orders__created_at_week\" >= ($1::timestamptz::timestamptz AT TIME ZONE 'UTC') AND \"orders__created_at_week\" <= ($2::timestamptz::timestamptz AT TIME ZONE 'UTC')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000",
94+
values: ["2019-11-01T00:00:00Z", "2019-11-30T23:59:59Z"],
95+
cacheKeyQueries: {
96+
renewalThreshold: 21600,
97+
queries: [["SELECT date_trunc('hour', (NOW()::timestamptz AT TIME ZONE 'UTC')) as current_hour", []]]
98+
},
99+
preAggregations: [{
100+
preAggregationsSchema: "stb_pre_aggregations",
101+
tableName: "stb_pre_aggregations.orders_number_and_count_and_very_very_very_very_very_very_long20191101",
102+
loadSql: ["CREATE TABLE stb_pre_aggregations.orders_number_and_count_and_very_very_very_very_very_very_long20191101 AS SELECT\n date_trunc('week', (\"orders\".created_at::timestamptz AT TIME ZONE 'UTC')) \"orders__created_at_week\", count(\"orders\".id) \"orders__count\", sum(\"orders\".number) \"orders__number\"\n FROM\n public.orders AS \"orders\"\n WHERE (\"orders\".created_at >= $1::timestamptz AND \"orders\".created_at <= $2::timestamptz) GROUP BY 1", ["2019-11-01T00:00:00Z", "2019-11-30T23:59:59Z"]],
103+
invalidateKeyQueries: [["SELECT date_trunc('hour', (NOW()::timestamptz AT TIME ZONE 'UTC')) as current_hour", []]],
104+
}],
105+
renewQuery: true
106+
};
107+
let thrown = true;
108+
try {
109+
await queryOrchestrator.fetchQuery(query);
110+
thrown = false;
111+
} catch (e) {
112+
should(e.message).match(/Pre-aggregation table is not found/);
113+
}
114+
should(thrown).equal(true);
115+
});
86116
});

0 commit comments

Comments
 (0)