You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@superset.apache.org by GitBox <gi...@apache.org> on 2018/04/12 21:26:06 UTC
[GitHub] michellethomas commented on a change in pull request #4736:
[Explore] Adding custom expressions to adhoc metrics
michellethomas commented on a change in pull request #4736: [Explore] Adding custom expressions to adhoc metrics
URL: https://github.com/apache/incubator-superset/pull/4736#discussion_r181226690
##########
File path: superset/assets/javascripts/explore/AdhocMetric.js
##########
@@ -1,7 +1,45 @@
+import { sqlaAutoGeneratedMetricRegex } from './constants';
+
+export const EXPRESSION_TYPES = {
+ SIMPLE: 'SIMPLE',
+ SQL: 'SQL',
+};
+
+function inferSqlExpressionColumn(adhocMetric) {
+ if (adhocMetric.sqlExpression && sqlaAutoGeneratedMetricRegex.test(adhocMetric.sqlExpression)) {
+ const indexFirstCloseParen = adhocMetric.sqlExpression.indexOf(')');
+ const indexPairedOpenParen =
+ adhocMetric.sqlExpression.substring(0, indexFirstCloseParen).lastIndexOf('(');
+ if (indexFirstCloseParen > 0 && indexPairedOpenParen > 0) {
+ return adhocMetric.sqlExpression.substring(indexPairedOpenParen + 1, indexFirstCloseParen);
+ }
+ }
+ return null;
+}
+
+function inferSqlExpressionAggregate(adhocMetric) {
+ if (adhocMetric.sqlExpression && sqlaAutoGeneratedMetricRegex.test(adhocMetric.sqlExpression)) {
+ const indexFirstOpenParen = adhocMetric.sqlExpression.indexOf('(');
+ if (indexFirstOpenParen > 0) {
+ return adhocMetric.sqlExpression.substring(0, indexFirstOpenParen);
+ }
+ }
+ return null;
+}
+
export default class AdhocMetric {
constructor(adhocMetric) {
- this.column = adhocMetric.column;
- this.aggregate = adhocMetric.aggregate;
+ this.expressionType = adhocMetric.expressionType || EXPRESSION_TYPES.SIMPLE;
+ if (this.expressionType === EXPRESSION_TYPES.SIMPLE) {
+ const inferredColumn = inferSqlExpressionColumn(adhocMetric);
+ this.column = adhocMetric.column || (inferredColumn && { column_name: inferredColumn });
Review comment:
Just to make sure I understand, `inferSqlExpressionColumn` is necessary if adhocMetric was a sql expression typed previously when EXPRESSION_TYPES was SQL correct? There's a lot going on in this section, maybe we could add a few comments to clarify?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services