You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/08/01 14:21:27 UTC

[GitHub] [beam] Mark-Zeng commented on a change in pull request #12232: [Beam-9543] Support Match Recognition in Beam SQL

Mark-Zeng commented on a change in pull request #12232:
URL: https://github.com/apache/beam/pull/12232#discussion_r463966552



##########
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:
       will do.

##########
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:
       ok.




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