/*
 * 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.planner;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode;
import org.apache.beam.vendor.calcite.v1_26_0.com.google.common.collect.Table;
import org.apache.beam.vendor.calcite.v1_26_0.org.apache.calcite.rel.RelNode;
import org.apache.beam.vendor.calcite.v1_26_0.org.apache.calcite.rel.metadata.MetadataDef;
import org.apache.beam.vendor.calcite.v1_26_0.org.apache.calcite.rel.metadata.MetadataHandler;
import org.apache.beam.vendor.calcite.v1_26_0.org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider;
import org.apache.beam.vendor.calcite.v1_26_0.org.apache.calcite.rel.metadata.RelMetadataProvider;
import org.apache.beam.vendor.calcite.v1_26_0.org.apache.calcite.rel.metadata.RelMetadataQuery;

/**
 * This is the implementation of NodeStatsMetadata. Methods to estimate rate and row count for
 * Calcite's logical nodes be implemented here.
 */
@SuppressWarnings({
  "rawtypes" // TODO(https://issues.apache.org/jira/browse/BEAM-10556)
})
public class RelMdNodeStats implements MetadataHandler<NodeStatsMetadata> {

  public static final RelMetadataProvider SOURCE =
      ReflectiveRelMetadataProvider.reflectiveSource(
          NodeStatsMetadata.METHOD, new RelMdNodeStats());

  @Override
  public MetadataDef<NodeStatsMetadata> getDef() {
    return NodeStatsMetadata.DEF;
  }

  @SuppressWarnings("UnusedDeclaration")
  public NodeStats getNodeStats(RelNode rel, RelMetadataQuery mq) {

    if (rel instanceof BeamRelNode) {
      return this.getBeamNodeStats((BeamRelNode) rel, mq);
    }

    // We can later define custom methods for all different RelNodes to prevent hitting this point.
    // Similar to RelMdRowCount in calcite.

    return NodeStats.UNKNOWN;
  }

  private NodeStats getBeamNodeStats(BeamRelNode rel, RelMetadataQuery mq) {

    // Removing the unknown results.
    // Calcite caches previous results in mq.map. This is done to prevent cyclic calls of this
    // method and also improving the performance. However, we might have returned an unknown result
    // because one of the inputs of the node was unknown (it is a logical node that we have not
    // implemented getNodeStats for it). Later we should not get the Unknown, therefore we need to
    // remove unknown results everyTime that this method is called.
    // Results are also cached in CachingRelMetadataProvider because calcite PlannerImpl#Transform
    // wraps the metadata provider with CachingRelMetadataProvider. However,
    // CachingRelMetadataProvider checks timestamp before returning previous results. Therefore,
    // there wouldn't be a problem in that case.
    Set<Table.Cell<RelNode, List, Object>> cells = mq.map.cellSet();
    List<Table.Cell<RelNode, List, Object>> keys = new ArrayList<>(cells.size());
    for (Table.Cell<RelNode, List, Object> cell : cells) {
      if (cell == null) {
        continue;
      }
      Object rawValue = cell.getValue();
      if (!(rawValue instanceof NodeStats)) {
        continue;
      }
      NodeStats nodeStats = (NodeStats) rawValue;
      if (nodeStats.isUnknown()) {
        keys.add(cell);
      }
    }

    keys.forEach(cell -> mq.map.remove(cell.getRowKey(), cell.getColumnKey()));

    return rel.estimateNodeStats(mq);
  }
}

@@ -868,28 +868,76 @@
 ort 
-java.util.ArrayList;
+static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;%0A
 %0Aimp
@@ -973,19 +973,33 @@
 va.util.
-Set
+stream.Collectors
 ;%0Aimport
@@ -3586,18 +3586,19 @@
 se.%0A    
-Se
+Lis
 t%3CTable.
@@ -3630,15 +3630,22 @@
 t%3E%3E 
-cells =
+keys =%0A       
  mq.
@@ -3661,176 +3661,116 @@
 et()
-;%0A    List%3CTable.Cell%3CRelNode, List, Object%3E%3E keys = new ArrayList%3C%3E(cells.size());%0A    for (Table.Cell%3CRelNode, List, Object%3E cell : cells) %7B%0A      if (cell =
+.stream()%0A            .filter(entry -%3E entry != null)%0A            .filter(entry -%3E entry.getValue() !
 = null)
- %7B
 %0A   
@@ -3778,87 +3778,45 @@
     
-continue;%0A      %7D%0A      Object rawValue = cell.getValue();%0A      if (!(rawValue
+    .filter(entry -%3E entry.getValue()
  ins
@@ -3833,19 +3833,16 @@
 deStats)
-) %7B
 %0A       
@@ -3846,95 +3846,80 @@
     
-continue;%0A      %7D%0A      NodeStats nodeStats = (NodeStats) rawValue;%0A      if (nodeStats
+    .filter(entry -%3E (checkArgumentNotNull((NodeStats) entry.getValue())
 .isU
@@ -3927,18 +3927,17 @@
 known())
- %7B
+)
 %0A       
@@ -3941,37 +3941,42 @@
     
-keys.add(cell);%0A      %7D%0A    %7D
+    .collect(Collectors.toList());
 %0A%0A  

Make it functional