vvysotskyi commented on a change in pull request #1886: DRILL-7273: Introduce 
operators for handling metadata
URL: https://github.com/apache/drill/pull/1886#discussion_r344702246
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/physical/impl/metadata/MetadataControllerBatch.java
 ##########
 @@ -0,0 +1,735 @@
+/*
+ * 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.drill.exec.physical.impl.metadata;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.drill.common.expression.SchemaPath;
+import org.apache.drill.common.types.TypeProtos;
+import org.apache.drill.common.types.Types;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.exception.OutOfMemoryException;
+import org.apache.drill.exec.metastore.analyze.AnalyzeColumnUtils;
+import org.apache.drill.exec.metastore.analyze.MetastoreAnalyzeConstants;
+import org.apache.drill.exec.ops.FragmentContext;
+import org.apache.drill.exec.physical.config.MetadataControllerPOP;
+import org.apache.drill.exec.physical.rowSet.DirectRowSet;
+import org.apache.drill.exec.physical.rowSet.RowSetReader;
+import org.apache.drill.exec.planner.common.DrillStatsTable;
+import org.apache.drill.exec.planner.physical.PlannerSettings;
+import org.apache.drill.exec.planner.physical.WriterPrel;
+import org.apache.drill.exec.metastore.analyze.MetadataIdentifierUtils;
+import org.apache.drill.exec.record.AbstractBinaryRecordBatch;
+import org.apache.drill.exec.record.BatchSchema;
+import org.apache.drill.exec.record.RecordBatch;
+import org.apache.drill.exec.record.VectorContainer;
+import org.apache.drill.exec.record.VectorWrapper;
+import org.apache.drill.exec.record.metadata.ColumnMetadata;
+import org.apache.drill.exec.record.metadata.TupleMetadata;
+import org.apache.drill.exec.store.EventBasedRecordWriter.FieldConverter;
+import org.apache.drill.exec.store.StatisticsRecordCollector;
+import org.apache.drill.exec.store.StatisticsRecordWriterImpl;
+import org.apache.drill.exec.store.easy.json.StatisticsCollectorImpl;
+import org.apache.drill.exec.store.parquet.ParquetTableMetadataUtils;
+import org.apache.drill.exec.vector.BitVector;
+import org.apache.drill.exec.vector.VarCharVector;
+import org.apache.drill.exec.vector.accessor.ArrayReader;
+import org.apache.drill.exec.vector.accessor.ObjectReader;
+import org.apache.drill.exec.vector.accessor.ObjectType;
+import org.apache.drill.exec.vector.accessor.TupleReader;
+import org.apache.drill.exec.vector.complex.reader.FieldReader;
+import org.apache.drill.metastore.components.tables.MetastoreTableInfo;
+import org.apache.drill.metastore.components.tables.TableMetadataUnit;
+import org.apache.drill.metastore.components.tables.Tables;
+import org.apache.drill.metastore.expressions.FilterExpression;
+import org.apache.drill.metastore.metadata.BaseMetadata;
+import org.apache.drill.metastore.metadata.BaseTableMetadata;
+import org.apache.drill.metastore.metadata.FileMetadata;
+import org.apache.drill.metastore.metadata.MetadataInfo;
+import org.apache.drill.metastore.metadata.MetadataType;
+import org.apache.drill.metastore.metadata.PartitionMetadata;
+import org.apache.drill.metastore.metadata.RowGroupMetadata;
+import org.apache.drill.metastore.metadata.SegmentMetadata;
+import org.apache.drill.metastore.metadata.TableInfo;
+import org.apache.drill.metastore.operate.Modify;
+import org.apache.drill.metastore.statistics.BaseStatisticsKind;
+import org.apache.drill.metastore.statistics.ColumnStatistics;
+import org.apache.drill.metastore.statistics.ColumnStatisticsKind;
+import org.apache.drill.metastore.statistics.ExactStatisticsConstants;
+import org.apache.drill.metastore.statistics.StatisticsHolder;
+import org.apache.drill.metastore.statistics.StatisticsKind;
+import org.apache.drill.metastore.statistics.TableStatisticsKind;
+import org.apache.drill.shaded.guava.com.google.common.base.Preconditions;
+import 
org.apache.drill.shaded.guava.com.google.common.collect.ArrayListMultimap;
+import org.apache.drill.shaded.guava.com.google.common.collect.Multimap;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * Terminal operator for producing ANALYZE statement. This operator is 
responsible for converting
+ * obtained metadata, fetching absent metadata from the metastore and storing 
resulting metadata into the metastore.
+ * <p>
+ * This operator has two inputs: left input contains metadata and right input 
contains statistics metadata.
+ */
+public class MetadataControllerBatch extends 
AbstractBinaryRecordBatch<MetadataControllerPOP> {
+  private static final Logger logger = 
LoggerFactory.getLogger(MetadataControllerBatch.class);
+
+  private final Tables tables;
+  private final TableInfo tableInfo;
+  private final Map<String, MetadataInfo> metadataToHandle;
+  private final StatisticsRecordCollector statisticsCollector;
+  private final List<TableMetadataUnit> metadataUnits;
+
+  private boolean firstLeft = true;
+  private boolean firstRight = true;
+  private boolean finished = false;
+  private boolean finishedRight = false;
+  private int recordCount = 0;
+
+  protected MetadataControllerBatch(MetadataControllerPOP popConfig,
+      FragmentContext context, RecordBatch left, RecordBatch right) throws 
OutOfMemoryException {
+    super(popConfig, context, false, left, right);
+    this.tables = context.getMetastoreRegistry().get().tables();
+    this.tableInfo = popConfig.getContext().tableInfo();
+    this.metadataToHandle = popConfig.getContext().metadataToHandle() == null
+        ? null
+        : popConfig.getContext().metadataToHandle().stream()
+            .collect(Collectors.toMap(MetadataInfo::identifier, 
Function.identity()));
+    this.metadataUnits = new ArrayList<>();
+    this.statisticsCollector = new StatisticsCollectorImpl();
+  }
+
+  protected boolean setupNewSchema() {
+    container.clear();
+
+    container.addOrGet(MetastoreAnalyzeConstants.OK_FIELD_NAME, 
Types.required(TypeProtos.MinorType.BIT), null);
+    container.addOrGet(MetastoreAnalyzeConstants.SUMMARY_FIELD_NAME, 
Types.required(TypeProtos.MinorType.VARCHAR), null);
+
+    container.buildSchema(BatchSchema.SelectionVectorMode.NONE);
+
+    return true;
+  }
+
+  @Override
+  public IterOutcome innerNext() {
+    IterOutcome outcome;
+    boolean finishedLeft;
+    if (finished) {
+      return IterOutcome.NONE;
+    }
+
+    if (!finishedRight) {
+      outcome = handleRightIncoming();
+      if (outcome != null) {
+        return outcome;
+      }
+    }
+
+    outer:
+    while (true) {
+      outcome = next(0, left);
+      switch (outcome) {
+        case NONE:
+          // all incoming data was processed when returned OK_NEW_SCHEMA
+          finishedLeft = !firstLeft;
+          break outer;
+        case OUT_OF_MEMORY:
+        case NOT_YET:
+        case STOP:
+          return outcome;
+        case OK_NEW_SCHEMA:
+          if (firstLeft) {
+            firstLeft = false;
+            if (!setupNewSchema()) {
+              outcome = IterOutcome.OK;
+            }
+            handleLeftIncoming();
+            return outcome;
+          }
+          //fall through
+        case OK:
+          assert !firstLeft : "First batch should be OK_NEW_SCHEMA";
+          IterOutcome out = handleLeftIncoming();
+          if (out != IterOutcome.OK) {
+            return out;
+          }
+          break;
+        default:
+          throw new UnsupportedOperationException("Unsupported upstream state 
" + outcome);
+      }
+    }
+
+    if (finishedLeft) {
+      IterOutcome out = writeToMetastore();
+      finished = true;
+      return out;
+    }
+    return outcome;
+  }
+
+  private IterOutcome handleRightIncoming() {
+    IterOutcome outcome;
+    outer:
+    while (true) {
+      outcome = next(0, right);
+      switch (outcome) {
+        case NONE:
+          // all incoming data was processed
+          finishedRight = true;
+          break outer;
+        case OUT_OF_MEMORY:
+        case NOT_YET:
+        case STOP:
+          return outcome;
+        case OK_NEW_SCHEMA:
+          firstRight = false;
+          //fall through
+        case OK:
+          assert !firstRight : "First batch should be OK_NEW_SCHEMA";
+          try {
+            appendStatistics(statisticsCollector);
+          } catch (IOException e) {
+            throw new RuntimeException(e);
 
 Review comment:
   Setting failed executor state. Thanks for pointing this.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to