Skip to content

Commit

Permalink
fix(schema-compiler): support string measures in views (#8194)
Browse files Browse the repository at this point in the history
* fix(cubesql): Support date/string measures in SQL API

* update (lint fix)

* fix

fix naming & test measures

* change measure for time test

* test by snapshot

* lint
  • Loading branch information
Nikita-str committed May 3, 2024
1 parent 651b9e0 commit fadfd1f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,13 @@ export class BaseQuery {
return type === 'number' || type === 'string' || type === 'time' || type === 'boolean';
}

/**
TODO: support type qualifiers on min and max
*/
static toMemberDataType(type) {
return this.isCalculatedMeasureType(type) ? type : 'number';
}

aggregateOnGroupedColumn(symbol, evaluateSql, topLevelMerge, measurePath) {
const cumulativeMeasureFilters = (this.safeEvaluateSymbolContext().cumulativeMeasureFilters || {})[measurePath];
if (cumulativeMeasureFilters) {
Expand Down
3 changes: 2 additions & 1 deletion packages/cubejs-schema-compiler/src/compiler/CubeSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { camelize } from 'inflection';
import { UserError } from './UserError';
import { DynamicReference } from './DynamicReference';
import { camelizeCube } from './utils';
import { BaseQuery } from '../adapter';

const FunctionRegex = /function\s+\w+\(([A-Za-z0-9_,]*)|\(([\s\S]*?)\)\s*=>|\(?(\w+)\)?\s*=>/;
const CONTEXT_SYMBOLS = {
Expand Down Expand Up @@ -372,7 +373,7 @@ export class CubeSymbols {
if (type === 'measures') {
memberDefinition = {
sql,
type: 'number',
type: BaseQuery.toMemberDataType(resolvedMember.type),
aggType: resolvedMember.type,
meta: resolvedMember.meta,
title: resolvedMember.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ export class CubeToMetaTransformer {
cubeName, drillMembers, { originalSorting: true }
)) || [];

// TODO support type qualifiers on min and max
const type = BaseQuery.isCalculatedMeasureType(nameToMetric[1].type) ? nameToMetric[1].type : 'number';
const type = BaseQuery.toMemberDataType(nameToMetric[1].type);

return {
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ cube(`Orders`, {
post_aggregate: true,
sql: `${amountRankDateMax}`,
type: `time`,
}
},
countAndTotalAmount: {
type: "string",
sql: `CONCAT(${count}, ' / ', ${totalAmount})`,
},
createdAtMax: {
type: `max`,
sql: `created_at`,
},
createdAtMaxProxy: {
type: "time",
sql: `${createdAtMax}`,
},
},
dimensions: {
id: {
Expand All @@ -72,3 +84,11 @@ cube(`Orders`, {
}
},
});

view(`OrdersView`, {
cubes: [{
joinPath: Orders,
includes: `*`,
excludes: [`toRemove`]
}]
});
2 changes: 1 addition & 1 deletion packages/cubejs-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"smoke:postgres": "jest --verbose -i dist/test/smoke-postgres.test.js",
"smoke:redshift": "jest --verbose -i dist/test/smoke-redshift.test.js",
"smoke:redshift:snapshot": "jest --verbose --updateSnapshot -i dist/test/smoke-redshift.test.js",
"smoke:cubesql": "jest --verbose --forceExit -i dist/test/smoke-cubesql.test.js",
"smoke:cubesql": "TZ=UTC jest --verbose --forceExit -i dist/test/smoke-cubesql.test.js",
"smoke:cubesql:snapshot": "jest --verbose --forceExit --updateSnapshot -i dist/test/smoke-cubesql.test.js",
"smoke:prestodb": "jest --verbose -i dist/test/smoke-prestodb.test.js",
"smoke:prestodb:snapshot": "jest --verbose --forceExit --updateSnapshot -i dist/test/smoke-prestodb.test.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,46 @@ Array [
]
`;

exports[`SQL API Postgres (Data) date/string measures in view: date case 1`] = `
Array [
Object {
"val": "2024-01-01T00:00:00.000",
},
Object {
"val": "2024-01-02T00:00:00.000",
},
Object {
"val": "2024-01-03T00:00:00.000",
},
Object {
"val": "2024-01-04T00:00:00.000",
},
Object {
"val": "2024-01-05T00:00:00.000",
},
]
`;

exports[`SQL API Postgres (Data) date/string measures in view: string case 1`] = `
Array [
Object {
"val": "1 / 100",
},
Object {
"val": "1 / 200",
},
Object {
"val": "1 / 300",
},
Object {
"val": "1 / 500",
},
Object {
"val": "1 / 600",
},
]
`;

exports[`SQL API Postgres (Data) metabase max number: metabase max number 1`] = `
Array [
Object {
Expand Down
10 changes: 10 additions & 0 deletions packages/cubejs-testing/test/smoke-cubesql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,15 @@ limit
`);
expect(res.rows).toMatchSnapshot('power bi post aggregate measure wrap');
});

test('date/string measures in view', async () => {
const queryCtor = (column: string) => `SELECT "${column}" AS val FROM "OrdersView" ORDER BY "id" LIMIT 10`;

const resStr = await connection.query(queryCtor('countAndTotalAmount'));
expect(resStr.rows).toMatchSnapshot('string case');

const resDate = await connection.query(queryCtor('createdAtMaxProxy'));
expect(resDate.rows).toMatchSnapshot('date case');
});
});
});

0 comments on commit fadfd1f

Please sign in to comment.