sunxiaojian commented on code in PR #8980:
URL: https://github.com/apache/gravitino/pull/8980#discussion_r2630335180


##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/cache/ScanPlanCacheKey.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.gravitino.iceberg.service.cache;
+
+import com.google.common.base.Objects;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.rest.requests.PlanTableScanRequest;
+
+/** Cache key for Iceberg table scan plans. */
+public class ScanPlanCacheKey {
+  private final TableIdentifier tableIdentifier;
+  private final Long snapshotId;
+  private final Long startSnapshotId;
+  private final Long endSnapshotId;
+  private final Expression filter; // Store the filter object instead of string
+  private final String selectStr;
+  private final String statsFieldsStr;
+  private final boolean caseSensitive;
+  private final boolean useSnapshotSchema;
+
+  private ScanPlanCacheKey(
+      TableIdentifier tableIdentifier,
+      Long snapshotId,
+      Long startSnapshotId,
+      Long endSnapshotId,
+      Expression filter,
+      String select,
+      String statsFields,
+      boolean caseSensitive,
+      boolean useSnapshotSchema) {
+    this.tableIdentifier = tableIdentifier;
+    this.snapshotId = snapshotId;
+    this.startSnapshotId = startSnapshotId;
+    this.endSnapshotId = endSnapshotId;
+    this.filter = filter;
+    this.selectStr = select;
+    this.statsFieldsStr = statsFields;
+    this.caseSensitive = caseSensitive;
+    this.useSnapshotSchema = useSnapshotSchema;
+  }
+
+  /**
+   * Creates a cache key from table identifier, table, and scan request.
+   *
+   * @param tableIdentifier the table identifier
+   * @param table the Iceberg table
+   * @param scanRequest the scan request containing filters and projections
+   * @return a new cache key
+   */
+  public static ScanPlanCacheKey create(
+      TableIdentifier tableIdentifier, Table table, PlanTableScanRequest 
scanRequest) {
+
+    // Use current snapshot if not specified
+    Long snapshotId = scanRequest.snapshotId();
+    if (snapshotId == null && table.currentSnapshot() != null) {
+      snapshotId = table.currentSnapshot().snapshotId();
+    }
+
+    // Include startSnapshotId and endSnapshotId in the key
+    Long startSnapshotId = scanRequest.startSnapshotId();
+    Long endSnapshotId = scanRequest.endSnapshotId();
+
+    // Store the filter expression object directly instead of its string 
representation.
+    // This approach relies on Expression's equals() and hashCode() 
implementations,
+    // which are designed to correctly handle semantic equality of expressions.
+    Expression filter = scanRequest.filter();
+
+    // Sort select and statsFields to make key order-independent

Review Comment:
   Yes, I think it's necessary. If not sorted, lists with the same elements in 
different orders will not be equal whether they are used with 
`Objects.equals()` or `Objects.hash()`



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to