/*
 * Licensed 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 com.facebook.presto.parquet;

import org.apache.parquet.SemanticVersion;
import org.apache.parquet.Strings;
import org.apache.parquet.VersionParser;
import org.apache.parquet.hadoop.metadata.FileMetaData;

public class ParquetCorruptStatisticsUtils
{
    private static final SemanticVersion PARQUET_1025_FIXED_VERSION = new SemanticVersion(1, 10, 0);

    public ParquetCorruptStatisticsUtils()
    {
    }

    public static boolean shouldIgnoreStatisticsForVarcharBinary(FileMetaData fileMetaData)
    {
        return shouldIgnoreStatisticsForVarcharBinary(fileMetaData.getCreatedBy());
    }

    public static boolean shouldIgnoreStatisticsForVarcharBinary(String createdBy)
    {
        if (Strings.isNullOrEmpty(createdBy)) {
            warnIfNeed("Ignoring statistics because created_by is null or empty! See PARQUET-251 and PARQUET-297");
            return true;
        }
        else {
            try {
                VersionParser.ParsedVersion version = VersionParser.parse(createdBy);
                if (!"parquet-mr".equals(version.application)) {
                    return false;
                }
                else if (Strings.isNullOrEmpty(version.version)) {
                    warnIfNeed("Ignoring statistics because created_by did not contain a semver (see PARQUET-251): " + createdBy);
                    return true;
                }
                else {
                    SemanticVersion semver = SemanticVersion.parse(version.version);

                    if (semver.compareTo(PARQUET_1025_FIXED_VERSION) < 0) {
                        warnIfNeed("Ignoring statistics because this file was created prior to " + PARQUET_1025_FIXED_VERSION + ", see PARQUET-1025.");
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            }
            catch (RuntimeException var4) {
                warnParseErrorOnce(createdBy, var4);
                return true;
            }
            catch (SemanticVersion.SemanticVersionParseException var5) {
                warnParseErrorOnce(createdBy, var5);
                return true;
            }
            catch (VersionParser.VersionParseException var6) {
                warnParseErrorOnce(createdBy, var6);
                return true;
            }
        }
    }

    private static void warnParseErrorOnce(String createdBy, Throwable e)
    {
        // ("Ignoring statistics because created_by could not be parsed (see PARQUET-251): " + createdBy, e);
    }

    private static void warnIfNeed(String message)
    {
        // (message);
    }
}