content: /*
   * 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.asterix.external.writer.printer.parquet;
  
  import org.apache.asterix.om.lazy.AbstractLazyVisitablePointable;
  import org.apache.asterix.om.lazy.AbstractListLazyVisitablePointable;
  import org.apache.asterix.om.lazy.FlatLazyVisitablePointable;
  import org.apache.asterix.om.lazy.ILazyVisitablePointableVisitor;
  import org.apache.asterix.om.lazy.RecordLazyVisitablePointable;
  import org.apache.asterix.om.lazy.TypedRecordLazyVisitablePointable;
  import org.apache.asterix.om.types.ARecordType;
  import org.apache.asterix.om.types.ATypeTag;
  import org.apache.asterix.om.types.IAType;
  import org.apache.hyracks.api.exceptions.HyracksDataException;
  import org.apache.hyracks.data.std.api.IValueReference;
  
  // This class is used to check the schema of a record against a schema that has been inferred so far.
  // By checking, we can determine if the record is equivalent to the schema, if the record is growing, or if there is a conflict.
  public class SchemaCheckerLazyVisitor implements ISchemaChecker,
          ILazyVisitablePointableVisitor<ISchemaChecker.SchemaComparisonType, ParquetSchemaTree.SchemaNode> {
      private final FieldNamesDictionary fieldNamesDictionary;
      private final RecordLazyVisitablePointable record;
  
      public SchemaCheckerLazyVisitor(IAType typeInfo) {
          this.fieldNamesDictionary = new FieldNamesDictionary();
          if (typeInfo.getTypeTag() == ATypeTag.OBJECT) {
              this.record = new TypedRecordLazyVisitablePointable((ARecordType) typeInfo);
          } else if (typeInfo.getTypeTag() == ATypeTag.ANY) {
              this.record = new RecordLazyVisitablePointable(true);
          } else {
              throw new RuntimeException("Type Unsupported for parquet printing");
          }
      }
  
      @Override
      public ISchemaChecker.SchemaComparisonType visit(RecordLazyVisitablePointable pointable,
              ParquetSchemaTree.SchemaNode schemaNode) throws HyracksDataException {
          if (schemaNode.getType() == null) {
              return SchemaComparisonType.GROWING;
          }
  
          if (!(schemaNode.getType() instanceof ParquetSchemaTree.RecordType recordType)) {
              return ISchemaChecker.SchemaComparisonType.CONFLICTING;
          }
  
          ISchemaChecker.SchemaComparisonType schemaComparisonType = ISchemaChecker.SchemaComparisonType.EQUIVALENT;
          int nonMissingChildren = 0;
          for (int i = 0; i < pointable.getNumberOfChildren(); i++) {
              pointable.nextChild();
              AbstractLazyVisitablePointable child = pointable.getChildVisitablePointable();
              if (child.getTypeTag() == ATypeTag.MISSING) {
                  continue;
              }
              nonMissingChildren++;
              String childColumnName = fieldNamesDictionary.getOrCreateFieldNameIndex(pointable.getFieldName());
              ParquetSchemaTree.SchemaNode childType = recordType.getChildren().get(childColumnName);
              if (childType == null) {
                  return SchemaComparisonType.CONFLICTING;
              }
              schemaComparisonType = ISchemaChecker.max(schemaComparisonType, child.accept(this, childType));
          }
          if (nonMissingChildren != recordType.getChildren().size()) {
              return SchemaComparisonType.CONFLICTING;
          }
          return schemaComparisonType;
      }
  
      @Override
      public ISchemaChecker.SchemaComparisonType visit(AbstractListLazyVisitablePointable pointable,
              ParquetSchemaTree.SchemaNode schemaNode) throws HyracksDataException {
          if (schemaNode.getType() == null) {
              return SchemaComparisonType.GROWING;
          }
          if (!(schemaNode.getType() instanceof ParquetSchemaTree.ListType)) {
              return ISchemaChecker.SchemaComparisonType.CONFLICTING;
          }
  
          ParquetSchemaTree.ListType listType = (ParquetSchemaTree.ListType) schemaNode.getType();
          ISchemaChecker.SchemaComparisonType schemaComparisonType = ISchemaChecker.SchemaComparisonType.EQUIVALENT;
  
          for (int i = 0; i < pointable.getNumberOfChildren(); i++) {
              pointable.nextChild();
              AbstractLazyVisitablePointable child = pointable.getChildVisitablePointable();
              if (child.getTypeTag() == ATypeTag.MISSING) {
                  throw new HyracksDataException("Missing values are not allowed in lists for parquet printing.");
              }
              if (listType.isEmpty()) {
                  schemaComparisonType =
                          ISchemaChecker.max(schemaComparisonType, ISchemaChecker.SchemaComparisonType.GROWING);
                  continue;
              }
              schemaComparisonType = ISchemaChecker.max(schemaComparisonType, child.accept(this, listType.getChild()));
          }
          return schemaComparisonType;
      }
  
      @Override
      public ISchemaChecker.SchemaComparisonType visit(FlatLazyVisitablePointable currentValue,
              ParquetSchemaTree.SchemaNode schemaNode) throws HyracksDataException {
          if (schemaNode.getType() == null) {
              return SchemaComparisonType.GROWING;
          }
          // SchemaNode.getTypeTag can never be MISSING here
          if (currentValue.getTypeTag() == ATypeTag.NULL) {
              return SchemaComparisonType.EQUIVALENT;
          }
          if (!(schemaNode.getType() instanceof ParquetSchemaTree.FlatType inferredType)) {
              return ISchemaChecker.SchemaComparisonType.CONFLICTING;
          }
  
          if (inferredType.getTypeTag() == currentValue.getTypeTag()) {
              return ISchemaChecker.SchemaComparisonType.EQUIVALENT;
          }
          if (!inferredType.isCompatibleWith(currentValue.getTypeTag())) {
              return ISchemaChecker.SchemaComparisonType.CONFLICTING;
          }
          if (inferredType.isStrictChildOf(currentValue.getTypeTag())) {
              return ISchemaChecker.SchemaComparisonType.GROWING;
          }
          if (inferredType.isStrictParentOf(currentValue.getTypeTag())) {
              return ISchemaChecker.SchemaComparisonType.EQUIVALENT;
          }
  
          return SchemaComparisonType.CONFLICTING;
      }
  
      @Override
      public SchemaComparisonType checkSchema(ParquetSchemaTree.SchemaNode schemaNode, IValueReference valueReference)
              throws HyracksDataException {
          record.set(valueReference);
          return record.accept(this, schemaNode);
      }
  }
  