You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by GitBox <gi...@apache.org> on 2020/06/22 15:12:54 UTC

[GitHub] [samza] srinipunuru commented on a change in pull request #1386: [WIP] Adding support for nested rows access via dot path.

srinipunuru commented on a change in pull request #1386:
URL: https://github.com/apache/samza/pull/1386#discussion_r443631316



##########
File path: samza-sql/src/main/java/org/apache/samza/sql/translator/MessageStreamCollector.java
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.samza.sql.translator;
+
+import java.io.Closeable;
+import java.io.Serializable;
+import java.time.Duration;
+import java.util.ArrayDeque;
+import java.util.Collection;
+import java.util.Deque;
+import java.util.function.Function;
+import org.apache.samza.context.Context;
+import org.apache.samza.operators.KV;
+import org.apache.samza.operators.MessageStream;
+import org.apache.samza.operators.OutputStream;
+import org.apache.samza.operators.functions.AsyncFlatMapFunction;
+import org.apache.samza.operators.functions.ClosableFunction;
+import org.apache.samza.operators.functions.FilterFunction;
+import org.apache.samza.operators.functions.FlatMapFunction;
+import org.apache.samza.operators.functions.JoinFunction;
+import org.apache.samza.operators.functions.MapFunction;
+import org.apache.samza.operators.functions.SinkFunction;
+import org.apache.samza.operators.functions.StreamTableJoinFunction;
+import org.apache.samza.operators.windows.Window;
+import org.apache.samza.operators.windows.WindowPane;
+import org.apache.samza.serializers.KVSerde;
+import org.apache.samza.serializers.Serde;
+import org.apache.samza.sql.data.SamzaSqlRelMessage;
+import org.apache.samza.table.Table;
+
+
+/**
+ * Collector of Map and Filter Samza Function, used to collect current call stack and trigger it when applying the join function.
+ *
+ * @TODO This class is a work around here to minimize the amount of code changes, but in an ideal world,
+ * @TODO where we use Calcite planner in conventional way we can combine function when via translation of RelNodes.
+ */
+class MessageStreamCollector implements MessageStream<SamzaSqlRelMessage>, Serializable, Closeable {
+
+  private final Deque<MapFunction<? super SamzaSqlRelMessage, ? extends SamzaSqlRelMessage>> _mapFnCallQueue =
+      new ArrayDeque<>();
+  private final Deque<ClosableFunction> _closingStack = new ArrayDeque<>();
+
+  @Override
+  public <OM> MessageStream<OM> map(MapFunction<? super SamzaSqlRelMessage, ? extends OM> mapFn) {
+    _mapFnCallQueue.offer((MapFunction<? super SamzaSqlRelMessage, ? extends SamzaSqlRelMessage>) mapFn);
+    return (MessageStream<OM>) this;
+  }
+
+  @Override
+  public MessageStream<SamzaSqlRelMessage> filter(FilterFunction<? super SamzaSqlRelMessage> filterFn) {
+    _mapFnCallQueue.offer(new FilterMapAdapter(filterFn));
+    return this;
+  }
+
+   Function<SamzaSqlRelMessage, SamzaSqlRelMessage> getFunction(Context context) {
+    Function<SamzaSqlRelMessage, SamzaSqlRelMessage> tailFn = null;
+    while (!_mapFnCallQueue.isEmpty()) {
+      MapFunction<? super SamzaSqlRelMessage, ? extends SamzaSqlRelMessage> f = _mapFnCallQueue.poll();
+      f.init(context);
+      _closingStack.push(f);
+      Function<SamzaSqlRelMessage, SamzaSqlRelMessage> current = x -> {
+        if (x != null) {
+          return f.apply(x);
+        }
+        return null;
+      };
+      if (tailFn == null) {
+        tailFn = current;
+      } else {
+        tailFn = current.compose(tailFn);
+      }
+    }
+    return tailFn == null ? Function.identity() : tailFn;
+  }
+
+  private static class FilterMapAdapter implements MapFunction<SamzaSqlRelMessage, SamzaSqlRelMessage> {

Review comment:
       Can you add comments here as well on why this adapter is required and what it does?

##########
File path: samza-sql/src/main/java/org/apache/samza/sql/translator/JoinTranslator.java
##########
@@ -141,8 +141,16 @@ void translate(final LogicalJoin join, final TranslatorContext translatorContext
 
     if (tableNode.isRemoteTable()) {
       String remoteTableName = tableNode.getSourceName();
-      StreamTableJoinFunction joinFn = new SamzaSqlRemoteTableJoinFunction(context.getMsgConverter(remoteTableName),
-          context.getTableKeyConverter(remoteTableName), streamNode, tableNode, join.getJoinType(), queryId);
+      MessageStream operatorStack = context.getMessageStream(tableNode.getRelNode().getId());

Review comment:
       Looks like this is where the magic is happening, Can you add some comments describing what we are doing and why we are doing this?

##########
File path: samza-sql/src/main/java/org/apache/samza/sql/translator/MessageStreamCollector.java
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.samza.sql.translator;
+
+import java.io.Closeable;
+import java.io.Serializable;
+import java.time.Duration;
+import java.util.ArrayDeque;
+import java.util.Collection;
+import java.util.Deque;
+import java.util.function.Function;
+import org.apache.samza.context.Context;
+import org.apache.samza.operators.KV;
+import org.apache.samza.operators.MessageStream;
+import org.apache.samza.operators.OutputStream;
+import org.apache.samza.operators.functions.AsyncFlatMapFunction;
+import org.apache.samza.operators.functions.ClosableFunction;
+import org.apache.samza.operators.functions.FilterFunction;
+import org.apache.samza.operators.functions.FlatMapFunction;
+import org.apache.samza.operators.functions.JoinFunction;
+import org.apache.samza.operators.functions.MapFunction;
+import org.apache.samza.operators.functions.SinkFunction;
+import org.apache.samza.operators.functions.StreamTableJoinFunction;
+import org.apache.samza.operators.windows.Window;
+import org.apache.samza.operators.windows.WindowPane;
+import org.apache.samza.serializers.KVSerde;
+import org.apache.samza.serializers.Serde;
+import org.apache.samza.sql.data.SamzaSqlRelMessage;
+import org.apache.samza.table.Table;
+
+
+/**
+ * Collector of Map and Filter Samza Function, used to collect current call stack and trigger it when applying the join function.
+ *
+ * @TODO This class is a work around here to minimize the amount of code changes, but in an ideal world,
+ * @TODO where we use Calcite planner in conventional way we can combine function when via translation of RelNodes.
+ */
+class MessageStreamCollector implements MessageStream<SamzaSqlRelMessage>, Serializable, Closeable {
+
+  private final Deque<MapFunction<? super SamzaSqlRelMessage, ? extends SamzaSqlRelMessage>> _mapFnCallQueue =
+      new ArrayDeque<>();
+  private final Deque<ClosableFunction> _closingStack = new ArrayDeque<>();
+
+  @Override
+  public <OM> MessageStream<OM> map(MapFunction<? super SamzaSqlRelMessage, ? extends OM> mapFn) {
+    _mapFnCallQueue.offer((MapFunction<? super SamzaSqlRelMessage, ? extends SamzaSqlRelMessage>) mapFn);
+    return (MessageStream<OM>) this;
+  }
+
+  @Override
+  public MessageStream<SamzaSqlRelMessage> filter(FilterFunction<? super SamzaSqlRelMessage> filterFn) {
+    _mapFnCallQueue.offer(new FilterMapAdapter(filterFn));
+    return this;
+  }
+
+   Function<SamzaSqlRelMessage, SamzaSqlRelMessage> getFunction(Context context) {
+    Function<SamzaSqlRelMessage, SamzaSqlRelMessage> tailFn = null;
+    while (!_mapFnCallQueue.isEmpty()) {

Review comment:
       Can you add more comments on what we are doing 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:
users@infra.apache.org