yupeng9 commented on a change in pull request #6899:
URL: https://github.com/apache/incubator-pinot/pull/6899#discussion_r637704161



##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/PartialUpsertHandler.java
##########
@@ -0,0 +1,103 @@
+/**
+ * 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.pinot.segment.local.upsert;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pinot.segment.local.upsert.merger.CustomMerger;
+import org.apache.pinot.segment.local.upsert.merger.IgnoreMerger;
+import org.apache.pinot.segment.local.upsert.merger.IncrementMerger;
+import org.apache.pinot.segment.local.upsert.merger.OverwriteMerger;
+import org.apache.pinot.segment.local.upsert.merger.PartialUpsertMerger;
+import org.apache.pinot.spi.config.table.UpsertConfig;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+public class PartialUpsertHandler {
+  private HashMap<String, PartialUpsertMerger> _mergers;
+
+  /**
+   * Initializes the partial upsert merger with upsert config. Different 
fields can have different merge strategies.
+   *
+   * @param schema of table
+   * @param globalUpsertStrategy can be derived into fields to merger map.
+   * @param partialUpsertStrategy can be derived into fields to merger map.
+   * @param customMergeStrategy can be derived into fields to merger map.
+   */
+  public void init(Schema schema, UpsertConfig.STRATEGY globalUpsertStrategy,
+      Map<String, UpsertConfig.STRATEGY> partialUpsertStrategy, Map<String, 
String> customMergeStrategy) {
+    _mergers = new HashMap<>();
+
+    // init globalUpsertStrategy
+    if (globalUpsertStrategy == UpsertConfig.STRATEGY.IGNORE) {
+      for (String dimColumn : schema.getDimensionNames()) {
+        if (!schema.getPrimaryKeyColumns().contains(dimColumn)) {
+          _mergers.put(dimColumn, new IgnoreMerger(dimColumn));

Review comment:
       curious, why such merger initialization is needed? Cannot we use some 
static merger since they are inbuilt strategy?

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/PartitionUpsertMetadataManager.java
##########
@@ -142,6 +148,33 @@ public ThreadSafeMutableRoaringBitmap addSegment(String 
segmentName, Iterator<Re
     return validDocIds;
   }
 
+  public RecordLocation findLastRecord(PrimaryKey primaryKey) {
+    RecordLocation currentRecordLocation = 
_primaryKeyToRecordLocationMap.get(primaryKey);
+    return currentRecordLocation;
+  }
+
+  public void handleUpsert(GenericRow row, int docId, PrimaryKey primaryKey, 
long timestamp,
+      MutableSegmentImpl mutableSegmentImpl) {
+    if (mutableSegmentImpl.isPartialUpsertEnabled()) {
+      // get primary key and timestamp for the incoming record.
+      GenericRow previousRow = new GenericRow();
+      // look up the previous full record with pk. Merge record if the 
incoming record is newer than previous record.
+      RecordLocation lastRecord = findLastRecord(primaryKey);

Review comment:
       can lastRecord be null, when it does not exist?

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/TableConfigUtils.java
##########
@@ -348,6 +354,26 @@ public static void validateUpsertConfig(TableConfig 
tableConfig, Schema schema)
     Preconditions.checkState(
         
CollectionUtils.isEmpty(tableConfig.getIndexingConfig().getStarTreeIndexConfigs())
 && !tableConfig
             .getIndexingConfig().isEnableDefaultStarTree(), "The upsert table 
cannot have star-tree index.");
+
+    Preconditions.checkState(validatePartialUpsertStrategies(schema, 
tableConfig.getUpsertConfig()),
+        "The partial upsert strategies is not correct");
+  }
+
+  private static boolean validatePartialUpsertStrategies(Schema schema, 
UpsertConfig upsertConfig) {

Review comment:
       add javadoc on what this validates for

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/merger/IgnoreMerger.java
##########
@@ -0,0 +1,38 @@
+/**
+ * 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.pinot.segment.local.upsert.merger;
+
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+public class IgnoreMerger implements PartialUpsertMerger {

Review comment:
       nit: We can create a static instance of this

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/PartialUpsertHandler.java
##########
@@ -0,0 +1,103 @@
+/**
+ * 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.pinot.segment.local.upsert;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pinot.segment.local.upsert.merger.CustomMerger;
+import org.apache.pinot.segment.local.upsert.merger.IgnoreMerger;
+import org.apache.pinot.segment.local.upsert.merger.IncrementMerger;
+import org.apache.pinot.segment.local.upsert.merger.OverwriteMerger;
+import org.apache.pinot.segment.local.upsert.merger.PartialUpsertMerger;
+import org.apache.pinot.spi.config.table.UpsertConfig;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+public class PartialUpsertHandler {
+  private HashMap<String, PartialUpsertMerger> _mergers;
+
+  /**
+   * Initializes the partial upsert merger with upsert config. Different 
fields can have different merge strategies.
+   *
+   * @param schema of table
+   * @param globalUpsertStrategy can be derived into fields to merger map.
+   * @param partialUpsertStrategy can be derived into fields to merger map.
+   * @param customMergeStrategy can be derived into fields to merger map.
+   */
+  public void init(Schema schema, UpsertConfig.STRATEGY globalUpsertStrategy,
+      Map<String, UpsertConfig.STRATEGY> partialUpsertStrategy, Map<String, 
String> customMergeStrategy) {
+    _mergers = new HashMap<>();
+
+    // init globalUpsertStrategy
+    if (globalUpsertStrategy == UpsertConfig.STRATEGY.IGNORE) {
+      for (String dimColumn : schema.getDimensionNames()) {
+        if (!schema.getPrimaryKeyColumns().contains(dimColumn)) {
+          _mergers.put(dimColumn, new IgnoreMerger(dimColumn));
+        }
+      }
+      for (String metricColumn : schema.getMetricNames()) {
+        if (!schema.getPrimaryKeyColumns().contains(metricColumn)) {
+          _mergers.put(metricColumn, new IgnoreMerger(metricColumn));
+        }
+      }
+    } else {
+      for (String dimColumn : schema.getDimensionNames()) {

Review comment:
       no handling of INCREMENT?

##########
File path: 
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/UpsertConfig.java
##########
@@ -30,16 +32,56 @@
     FULL, PARTIAL, NONE
   }
 
+  public enum STRATEGY {
+    OVERWRITE, IGNORE, INCREMENT
+  }
+
   private final Mode _mode;
+  private final STRATEGY _globalUpsertStrategy;
+  private final Map<String, STRATEGY> _partialUpsertStrategy;
+  private final Map<String, String> _customUpsertStrategy;
 
-  @JsonCreator
   public UpsertConfig(@JsonProperty(value = "mode", required = true) Mode 
mode) {
     Preconditions.checkArgument(mode != null, "Upsert mode must be 
configured");
     Preconditions.checkArgument(mode != Mode.PARTIAL, "Partial upsert mode is 
not supported");

Review comment:
       remove this?

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/merger/PartialUpsertMerger.java
##########
@@ -0,0 +1,33 @@
+/**
+ * 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.pinot.segment.local.upsert.merger;
+
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+public interface PartialUpsertMerger {
+  /**
+   * Handle partial upsert merge for given fieldName.
+   *
+   * @param previousRecord the last derived full record during ingestion.
+   * @param currentRecord the new consumed record.
+   * @return a new row after merge
+   */
+  GenericRow merge(GenericRow previousRecord, GenericRow currentRecord);

Review comment:
       can previousRecord be null?

##########
File path: 
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/upsert/merger/PartialUpsertMerger.java
##########
@@ -0,0 +1,33 @@
+/**
+ * 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.pinot.segment.local.upsert.merger;
+
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+public interface PartialUpsertMerger {

Review comment:
       javadoc




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



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

Reply via email to