danny0405 commented on a change in pull request #1449: [CALCITE-3302] Implement 
CLASSIFIER and LAST function for MATCH_RECOG…
URL: https://github.com/apache/calcite/pull/1449#discussion_r323083873
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
 ##########
 @@ -2925,6 +2934,83 @@ private Expression normalize(SqlTypeName typeName, 
Expression e) {
       }
     }
   }
+
+  /** Implements CLASSIFIER MR Function */
+  private static class ClassifierImplementor implements MatchImplementor {
+    @Override public Expression implement(RexToLixTranslator translator, 
RexCall call,
+                                          ParameterExpression row, 
ParameterExpression rows,
+                                          ParameterExpression symbols, 
ParameterExpression i) {
+      return Types.castIfNecessary(String.class,
+          Expressions.call(
+              symbols,
+              Types.lookupMethod(List.class, "get", int.class), i));
+    }
+  }
+
+  /** Implements LAST MR Function */
+  private static class LastImplementor implements MatchImplementor {
+    @Override public Expression implement(RexToLixTranslator translator, 
RexCall call,
+                                          ParameterExpression row, 
ParameterExpression rows,
+                                          ParameterExpression symbols, 
ParameterExpression i) {
+      final RexNode node = call.getOperands().get(0);
+
+      final String alpha = ((RexPatternFieldRef) 
call.getOperands().get(0)).getAlpha();
+
+      final BinaryExpression lastIndex = Expressions.subtract(
+          Expressions.call(rows, Types.lookupMethod(List.class, "size")),
+          Expressions.constant(1)
+      );
+
+      // Just take the last one, if exists
+      if ("*".equals(alpha)) {
+        ((EnumerableMatch.PassedRowsInputGetter) 
translator.inputGetter).setIndex(i);
+        // Important, unbox the node / expression to avoid NullAs.NOT_POSSIBLE
+        final RexPatternFieldRef ref = (RexPatternFieldRef) node;
+        final RexPatternFieldRef newRef = new RexPatternFieldRef(
+            ref.getAlpha(),
+            ref.getIndex(),
+            translator.typeFactory.createTypeWithNullability(ref.getType(), 
true)
+        );
+        final Expression expression = translator.translate(newRef, 
NullAs.NULL);
+        ((EnumerableMatch.PassedRowsInputGetter) 
translator.inputGetter).setIndex(null);
+        return expression;
+      } else {
+        // Alpha != "*" so we have to search for a specific one to find and 
use that, if found
+        ((EnumerableMatch.PassedRowsInputGetter) 
translator.inputGetter).setIndex(
+            Expressions.call(
+                Types.lookupMethod(MatchUtils.class, "lastWithSymbol", 
String.class,
+                    List.class, List.class, int.class),
+                Expressions.constant(alpha),
+                rows,
+                symbols,
+                i));
+
+        // Important, unbox the node / expression to avoid NullAs.NOT_POSSIBLE
+        final RexPatternFieldRef ref = (RexPatternFieldRef) node;
+        final RexPatternFieldRef newRef = new RexPatternFieldRef(
+            ref.getAlpha(),
+            ref.getIndex(),
+            translator.typeFactory.createTypeWithNullability(ref.getType(), 
true)
+        );
+        final Expression expression = translator.translate(newRef, 
NullAs.NULL);
+        ((EnumerableMatch.PassedRowsInputGetter) 
translator.inputGetter).setIndex(null);
+        return expression;
+      }
+    }
+  }
+
+  /** Implements PREV MR Function */
+  private static class PrevImplementor implements CallImplementor {
+    @Override public Expression implement(RexToLixTranslator translator, 
RexCall call,
+                                          NullAs nullAs) {
 
 Review comment:
   Fix the indent.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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

Reply via email to