williaster commented on a change in pull request #4909: [Explore] Adding Adhoc 
Filters
URL: 
https://github.com/apache/incubator-superset/pull/4909#discussion_r186486102
 
 

 ##########
 File path: superset/assets/src/explore/AdhocFilter.js
 ##########
 @@ -0,0 +1,92 @@
+import { MULTI_OPERATORS } from './constants';
+
+export const EXPRESSION_TYPES = {
+  SIMPLE: 'SIMPLE',
+  SQL: 'SQL',
+};
+
+export const CLAUSES = {
+  HAVING: 'HAVING',
+  WHERE: 'WHERE',
+};
+
+const OPERATORS_TO_SQL = {
+  '==': '=',
+  '!=': '<>',
+  '>': '>',
+  '<': '<',
+  '>=': '>=',
+  '<=': '<=',
+  in: 'in',
+  'not in': 'not in',
+  like: 'like',
+};
+
+function translateToSql(adhocMetric, { useSimple } = {}) {
+  if (adhocMetric.expressionType === EXPRESSION_TYPES.SIMPLE || useSimple) {
+    const isMulti = MULTI_OPERATORS.indexOf(adhocMetric.operator) >= 0;
+    return `${adhocMetric.subject} ${OPERATORS_TO_SQL[adhocMetric.operator]} 
${isMulti ? '(\'' : ''}${isMulti ? adhocMetric.comparator.join("','") : 
adhocMetric.comparator}${isMulti ? '\')' : ''}`;
+  } else if (adhocMetric.expressionType === EXPRESSION_TYPES.SQL) {
+    return adhocMetric.sqlExpression;
+  }
+  return '';
+}
+
+export default class AdhocFilter {
+  constructor(adhocFilter) {
+    this.expressionType = adhocFilter.expressionType || 
EXPRESSION_TYPES.SIMPLE;
+    if (this.expressionType === EXPRESSION_TYPES.SIMPLE) {
+      this.subject = adhocFilter.subject;
+      this.operator = adhocFilter.operator;
+      this.comparator = adhocFilter.comparator;
+      this.clause = adhocFilter.clause;
+      this.sqlExpression = null;
+    } else if (this.expressionType === EXPRESSION_TYPES.SQL) {
+      this.sqlExpression = adhocFilter.sqlExpression ||
+        translateToSql(adhocFilter, { useSimple: true });
+      this.clause = adhocFilter.clause;
+      this.subject = null;
+      this.operator = null;
+      this.comparator = null;
+    }
+    this.fromFormData = !!adhocFilter.filterOptionName;
+
+    this.filterOptionName = adhocFilter.filterOptionName ||
+      `filter_${Math.random().toString(36).substring(2, 
15)}_${Math.random().toString(36).substring(2, 15)}`;
+  }
+
+  duplicateWith(nextFields) {
+    return new AdhocFilter({
+      ...this,
 
 Review comment:
   not a blocker, but I think in general it's better for readability and 
bugginess to explicitly define which properties are copied (for `...this`, I 
think `...nextFields` is fine)

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to