<commit_before>/*
 * Copyright 2016 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.spark.operation.dataframe.converter.schema;

import org.apache.commons.lang.StringUtils;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Metadata;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.gchq.gaffer.data.elementdefinition.view.View;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.Converter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.impl.FreqMapConverter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.impl.HyperLogLogPlusConverter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.impl.datasketches.theta.UnionConverter;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Converts a Gaffer {@link Schema} to a Spark {@link StructType}. Only groups that are present in the provided
 * {@link View} are used. Properties that can either automatically be converted into a value that can be used in
 * a Spark Dataframe, or for which a {@link Converter} is provided, will be present in the {@link StructType}.
 * If the same property is present in more than one group, then it must be consistent, i.e. of the same type.
 */
public class SchemaToStructTypeConverter {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchemaToStructTypeConverter.class);
    private static final List<Converter> DEFAULT_CONVERTERS = new ArrayList<>();

    static {
        DEFAULT_CONVERTERS.add(new FreqMapConverter());
        DEFAULT_CONVERTERS.add(new HyperLogLogPlusConverter());
        DEFAULT_CONVERTERS.add(new UnionConverter());
    }

    enum EntityOrEdge {
        ENTITY, EDGE
    }

    public static final String GROUP = "group";
    public static final String VERTEX_COL_NAME = "vertex";
    public static final String SRC_COL_NAME = "src";
    public static final String DST_COL_NAME = "dst";

    private final Schema schema;
    private final View view;
    private final List<Converter> converters = new ArrayList<>();
    private final LinkedHashSet<String> groups = new LinkedHashSet<>();
    private StructType structType;
    private final Map<String, EntityOrEdge> entityOrEdgeByGroup = new HashMap<>();
    private final LinkedHashSet<String> usedProperties = new LinkedHashSet<>();
    private final Map<String, Boolean> propertyNeedsConversion = new HashMap<>();
    private final Map<String, Converter> converterByProperty = new HashMap<>();
    private final Map<String, StructType> structTypeByGroup = new HashMap<>();

    public SchemaToStructTypeConverter(final Schema schema, final View view, final List<Converter> converters) {
        this.schema = schema;
        this.view = view;
        this.converters.addAll(DEFAULT_CONVERTERS);
        if (converters != null) {
            this.converters.addAll(converters);
        }
        // Extract the relevant groups from the view
        addGroups(view);<commit_msg>gh-677 Remove unnecessary view member variable from SchemaToStructTypeConverter<commit_after>/*
 * Copyright 2016 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.spark.operation.dataframe.converter.schema;

import org.apache.commons.lang.StringUtils;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Metadata;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.gchq.gaffer.data.elementdefinition.view.View;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.Converter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.impl.FreqMapConverter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.impl.HyperLogLogPlusConverter;
import uk.gov.gchq.gaffer.spark.operation.dataframe.converter.property.impl.datasketches.theta.UnionConverter;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition;
import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Converts a Gaffer {@link Schema} to a Spark {@link StructType}. Only groups that are present in the provided
 * {@link View} are used. Properties that can either automatically be converted into a value that can be used in
 * a Spark Dataframe, or for which a {@link Converter} is provided, will be present in the {@link StructType}.
 * If the same property is present in more than one group, then it must be consistent, i.e. of the same type.
 */
public class SchemaToStructTypeConverter {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchemaToStructTypeConverter.class);
    private static final List<Converter> DEFAULT_CONVERTERS = new ArrayList<>();

    static {
        DEFAULT_CONVERTERS.add(new FreqMapConverter());
        DEFAULT_CONVERTERS.add(new HyperLogLogPlusConverter());
        DEFAULT_CONVERTERS.add(new UnionConverter());
    }

    enum EntityOrEdge {
        ENTITY, EDGE
    }

    public static final String GROUP = "group";
    public static final String VERTEX_COL_NAME = "vertex";
    public static final String SRC_COL_NAME = "src";
    public static final String DST_COL_NAME = "dst";

    private final Schema schema;
    private final List<Converter> converters = new ArrayList<>();
    private final LinkedHashSet<String> groups = new LinkedHashSet<>();
    private StructType structType;
    private final Map<String, EntityOrEdge> entityOrEdgeByGroup = new HashMap<>();
    private final LinkedHashSet<String> usedProperties = new LinkedHashSet<>();
    private final Map<String, Boolean> propertyNeedsConversion = new HashMap<>();
    private final Map<String, Converter> converterByProperty = new HashMap<>();
    private final Map<String, StructType> structTypeByGroup = new HashMap<>();

    public SchemaToStructTypeConverter(final Schema schema, final View view, final List<Converter> converters) {
        this.schema = schema;
        this.converters.addAll(DEFAULT_CONVERTERS);
        if (converters != null) {
            this.converters.addAll(converters);
        }
        // Extract the relevant groups from the view
        addGroups(view);
{'fraction_non_alphanumeric': 0.09368511054110175, 'fraction_numerical': 0.0028093318675949676, 'mean_word_length': 4.355134074558535, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4236681', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2280, 'n_words': 692}