jihoonson commented on a change in pull request #9111: Add HashJoinSegment, a 
virtual segment for joins.
URL: https://github.com/apache/druid/pull/9111#discussion_r367608679
 
 

 ##########
 File path: 
processing/src/main/java/org/apache/druid/segment/join/HashJoinEngine.java
 ##########
 @@ -0,0 +1,211 @@
+/*
+ * 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.druid.segment.join;
+
+import org.apache.druid.query.BaseQuery;
+import org.apache.druid.query.dimension.DimensionSpec;
+import org.apache.druid.segment.ColumnSelectorFactory;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.Cursor;
+import org.apache.druid.segment.DimensionSelector;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.joda.time.DateTime;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+public class HashJoinEngine
+{
+  private HashJoinEngine()
+  {
+    // No instantiation.
+  }
+
+  /**
+   * Creates a cursor that represents the join of {@param leftCursor} with 
{@param joinableClause}. The resulting
+   * cursor may generate nulls on the left-hand side (for righty joins; see 
{@link JoinType#isRighty()}) or on
+   * the right-hand side (for lefty joins; see {@link JoinType#isLefty()}). 
Columns that start with the
+   * joinable clause's prefix (see {@link JoinableClause#getPrefix()}) will 
come from the Joinable's column selector
+   * factory, and all other columns will come from the leftCursor's column 
selector factory.
+   *
+   * Ensuing that the joinable clause's prefix does not conflict with any 
columns from "leftCursor" is the
+   * responsibility of the caller.
+   */
+  public static Cursor makeJoinCursor(final Cursor leftCursor, final 
JoinableClause joinableClause)
+  {
+    final ColumnSelectorFactory leftColumnSelectorFactory = 
leftCursor.getColumnSelectorFactory();
+    final JoinMatcher joinMatcher = joinableClause.getJoinable()
+                                                  .makeJoinMatcher(
+                                                      
leftColumnSelectorFactory,
+                                                      
joinableClause.getCondition(),
+                                                      
joinableClause.getJoinType().isRighty()
+                                                  );
+
+    class JoinColumnSelectorFactory implements ColumnSelectorFactory
+    {
+      @Override
+      public DimensionSelector makeDimensionSelector(DimensionSpec 
dimensionSpec)
+      {
+        if (joinableClause.includesColumn(dimensionSpec.getDimension())) {
+          return joinMatcher.getColumnSelectorFactory()
+                            .makeDimensionSelector(
+                                
dimensionSpec.withDimension(joinableClause.unprefix(dimensionSpec.getDimension()))
+                            );
+        } else {
+          final DimensionSelector leftSelector = 
leftColumnSelectorFactory.makeDimensionSelector(dimensionSpec);
+
+          if (!joinableClause.getJoinType().isRighty()) {
+            return leftSelector;
+          } else {
+            return new PossiblyNullDimensionSelector(leftSelector, 
joinMatcher::matchingRemainder);
+          }
+        }
+      }
+
+      @Override
+      public ColumnValueSelector makeColumnValueSelector(String column)
+      {
+        if (joinableClause.includesColumn(column)) {
+          return 
joinMatcher.getColumnSelectorFactory().makeColumnValueSelector(joinableClause.unprefix(column));
+        } else {
+          final ColumnValueSelector<?> leftSelector = 
leftColumnSelectorFactory.makeColumnValueSelector(column);
+
+          if (!joinableClause.getJoinType().isRighty()) {
+            return leftSelector;
+          } else {
+            return new PossiblyNullColumnValueSelector<>(leftSelector, 
joinMatcher::matchingRemainder);
+          }
+        }
+      }
+
+      @Nullable
+      @Override
+      public ColumnCapabilities getColumnCapabilities(String column)
+      {
+        if (joinableClause.includesColumn(column)) {
+          return 
joinMatcher.getColumnSelectorFactory().getColumnCapabilities(joinableClause.unprefix(column));
+        } else {
+          return leftColumnSelectorFactory.getColumnCapabilities(column);
+        }
+      }
+    }
+
+    final JoinColumnSelectorFactory joinColumnSelectorFactory = new 
JoinColumnSelectorFactory();
+
+    class JoinCursor implements Cursor
 
 Review comment:
   Nice!

----------------------------------------------------------------
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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to