amaliujia commented on a change in pull request #12232:
URL: https://github.com/apache/beam/pull/12232#discussion_r460341711



##########
File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/cep/CEPCall.java
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.impl.cep;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexCall;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLiteral;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexNode;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexPatternFieldRef;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlOperator;
+
+/**
+ * A {@code CEPCall} instance represents an operation (node) that contains an 
operator and a list of
+ * operands. It has the similar functionality as Calcite's {@code RexCall}.
+ */
+public class CEPCall extends CEPOperation {
+
+  private final CEPOperator operator;
+  private final List<CEPOperation> operands;
+
+  private CEPCall(CEPOperator operator, List<CEPOperation> operands) {
+    this.operator = operator;
+    this.operands = operands;
+  }
+
+  public CEPOperator getOperator() {
+    return operator;
+  }
+
+  public List<CEPOperation> getOperands() {
+    return operands;
+  }
+
+  public static CEPCall of(RexCall operation) {
+    SqlOperator call = operation.getOperator();
+    CEPOperator myOp = CEPOperator.of(call);
+
+    ArrayList<CEPOperation> operandsList = new ArrayList<>();
+    for (RexNode i : operation.getOperands()) {
+      if (i.getClass() == RexCall.class) {
+        CEPCall callToAdd = CEPCall.of((RexCall) i);
+        operandsList.add(callToAdd);
+      } else if (i.getClass() == RexLiteral.class) {
+        RexLiteral lit = (RexLiteral) i;
+        CEPLiteral litToAdd = CEPLiteral.of(lit);
+        operandsList.add(litToAdd);
+      } else if (i.getClass() == RexPatternFieldRef.class) {
+        RexPatternFieldRef fieldRef = (RexPatternFieldRef) i;
+        CEPFieldRef fieldRefToAdd = CEPFieldRef.of(fieldRef);
+        operandsList.add(fieldRefToAdd);
+      }

Review comment:
       Is it correct to not handle other classes? 
   
   If so can you add an exception in the last `else`.

##########
File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/cep/CEPUtil.java
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.impl.cep;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.schemas.Schema;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelCollation;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelFieldCollation;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexCall;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLiteral;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexNode;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlOperator;
+
+/**
+ * Some utility methods for transforming Calcite's constructs into our own 
Beam constructs (for
+ * serialization purpose).
+ */
+public class CEPUtil {
+
+  private static Quantifier getQuantifier(int start, int end, boolean 
isReluctant) {
+    Quantifier quantToAdd;
+    if (!isReluctant) {
+      if (start == end) {
+        quantToAdd = new Quantifier("{ " + start + " }");
+      } else {
+        if (end == -1) {
+          if (start == 0) {
+            quantToAdd = Quantifier.ASTERISK;
+          } else if (start == 1) {
+            quantToAdd = Quantifier.PLUS;
+          } else {
+            quantToAdd = new Quantifier("{ " + start + " }");
+          }
+        } else {
+          if (start == 0 && end == 1) {
+            quantToAdd = Quantifier.QMARK;
+          } else if (start == -1) {
+            quantToAdd = new Quantifier("{ , " + end + " }");
+          } else {
+            quantToAdd = new Quantifier("{ " + start + " , }");
+          }
+        }
+      }
+    } else {
+      if (start == end) {
+        quantToAdd = new Quantifier("{ " + start + " }?");
+      } else {
+        if (end == -1) {
+          if (start == 0) {
+            quantToAdd = Quantifier.ASTERISK_RELUCTANT;
+          } else if (start == 1) {
+            quantToAdd = Quantifier.PLUS_RELUCTANT;
+          } else {
+            quantToAdd = new Quantifier("{ " + start + " }?");
+          }
+        } else {
+          if (start == 0 && end == 1) {
+            quantToAdd = Quantifier.QMARK_RELUCTANT;
+          } else if (start == -1) {
+            quantToAdd = new Quantifier("{ , " + end + " }?");
+          } else {
+            quantToAdd = new Quantifier("{ " + start + " , }?");
+          }
+        }
+      }
+    }
+
+    return quantToAdd;
+  }
+
+  /** Construct a list of {@code CEPPattern}s from a {@code RexNode}. */
+  public static ArrayList<CEPPattern> getCEPPatternFromPattern(
+      Schema upStreamSchema, RexNode call, Map<String, RexNode> patternDefs) {
+    ArrayList<CEPPattern> patternList = new ArrayList<>();
+    if (call.getClass() == RexLiteral.class) {
+      String p = ((RexLiteral) call).getValueAs(String.class);
+      RexNode pd = patternDefs.get(p);
+      patternList.add(CEPPattern.of(upStreamSchema, p, (RexCall) pd, 
Quantifier.NONE));
+    } else {
+      RexCall patCall = (RexCall) call;
+      SqlOperator operator = patCall.getOperator();
+      List<RexNode> operands = patCall.getOperands();
+
+      // check if if the node has quantifier
+      if (operator.getKind() == SqlKind.PATTERN_QUANTIFIER) {
+        String p = ((RexLiteral) operands.get(0)).getValueAs(String.class);
+        RexNode pd = patternDefs.get(p);
+        int start = ((RexLiteral) operands.get(1)).getValueAs(Integer.class);
+        int end = ((RexLiteral) operands.get(2)).getValueAs(Integer.class);
+        boolean isReluctant = ((RexLiteral) 
operands.get(3)).getValueAs(Boolean.class);
+
+        patternList.add(
+            CEPPattern.of(upStreamSchema, p, (RexCall) pd, 
getQuantifier(start, end, isReluctant)));
+      } else {
+        for (RexNode i : operands) {
+          patternList.addAll(getCEPPatternFromPattern(upStreamSchema, i, 
patternDefs));
+        }
+      }
+    }
+    return patternList;
+  }
+
+  /** Recursively construct a regular expression from a {@code RexNode}. */
+  public static String getRegexFromPattern(RexNode call) {
+    if (call.getClass() == RexLiteral.class) {
+      return ((RexLiteral) call).getValueAs(String.class);
+    } else {
+      RexCall opr = (RexCall) call;
+      SqlOperator operator = opr.getOperator();
+      List<RexNode> operands = opr.getOperands();
+      if (operator.getKind() == SqlKind.PATTERN_QUANTIFIER) {
+        String p = ((RexLiteral) operands.get(0)).getValueAs(String.class);
+        int start = ((RexLiteral) operands.get(1)).getValueAs(Integer.class);
+        int end = ((RexLiteral) operands.get(2)).getValueAs(Integer.class);
+        boolean isReluctant = ((RexLiteral) 
operands.get(3)).getValueAs(Boolean.class);
+        Quantifier quantifier = getQuantifier(start, end, isReluctant);
+        return p + quantifier.toString();
+      }
+      return getRegexFromPattern(opr.getOperands().get(0))
+          + getRegexFromPattern(opr.getOperands().get(1));
+    }
+  }
+
+  /** Transform a list of keys in Calcite to {@code ORDER BY} to {@code 
OrderKey}s. */
+  public static ArrayList<OrderKey> makeOrderKeysFromCollation(RelCollation 
orderKeys) {
+    List<RelFieldCollation> revOrderKeys = orderKeys.getFieldCollations();
+    Collections.reverse(revOrderKeys);

Review comment:
       This reverse seems not useful.

##########
File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/cep/CEPLiteral.java
##########
@@ -0,0 +1,196 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.impl.cep;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.extensions.sql.impl.SqlConversionException;
+import org.apache.beam.sdk.schemas.Schema;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLiteral;
+import org.joda.time.ReadableDateTime;
+
+/**
+ * {@code CEPLiteral} represents a literal node. It corresponds to {@code 
RexLiteral} in Calcite.
+ */
+public class CEPLiteral extends CEPOperation {
+
+  private final Schema.TypeName typeName;
+
+  private CEPLiteral(Schema.TypeName typeName) {
+    this.typeName = typeName;
+  }
+
+  // TODO: deal with other types (byte, short...)
+  public static CEPLiteral of(RexLiteral lit) {
+    switch (lit.getTypeName()) {
+      case INTEGER:
+        return of(lit.getValueAs(Integer.class));
+      case BIGINT:
+        return of(lit.getValueAs(Long.class));
+      case DECIMAL:
+        return of(lit.getValueAs(BigDecimal.class));
+      case FLOAT:
+        return of(lit.getValueAs(Float.class));
+      case DOUBLE:
+        return of(lit.getValueAs(Double.class));
+      case BOOLEAN:
+        return of(lit.getValueAs(Boolean.class));
+      case DATE:
+        return of(lit.getValueAs(ReadableDateTime.class));
+      case CHAR:
+      case VARCHAR:
+        return of(lit.getValueAs(String.class));
+      default:
+        throw new SqlConversionException(
+            "sql literal type not supported: " + lit.getTypeName().toString());

Review comment:
       nit: no need add `literal` here.




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to