<commit_before>/*
 * 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.calcite.sql.type;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Rules that determine whether a type is assignable from another type.
 */
public class SqlTypeAssignmentRule implements SqlTypeMappingRule {
  //~ Static fields/initializers ---------------------------------------------

  private static final SqlTypeAssignmentRule INSTANCE;

  //~ Instance fields --------------------------------------------------------

  private final Map<SqlTypeName, ImmutableSet<SqlTypeName>> map;

  //~ Constructors -----------------------------------------------------------

  /**
   * Creates a {@code SqlTypeAssignmentRules} with specified type mappings {@code map}.
   *
   * <p>Make this constructor private intentionally, use {@link #instance()}.
   *
   * @param map The type mapping, for each map entry, the values types can be assigned to
   *            the key type
   */
  private SqlTypeAssignmentRule(
      Map<SqlTypeName, ImmutableSet<SqlTypeName>> map) {
    this.map = ImmutableMap.copyOf(map);
  }

  static {
    final SqlTypeMappingRules.Builder rules = SqlTypeMappingRules.builder();

    final Set<SqlTypeName> rule = new HashSet<>();

    // IntervalYearMonth is assignable from...
    for (SqlTypeName interval : SqlTypeName.YEAR_INTERVAL_TYPES) {
      rules.add(interval, SqlTypeName.YEAR_INTERVAL_TYPES);
    }
    for (SqlTypeName interval : SqlTypeName.DAY_INTERVAL_TYPES) {
      rules.add(interval, SqlTypeName.DAY_INTERVAL_TYPES);
    }
    for (SqlTypeName interval : SqlTypeName.DAY_INTERVAL_TYPES) {
      final Set<SqlTypeName> dayIntervalTypes = SqlTypeName.DAY_INTERVAL_TYPES;
      rules.add(interval, dayIntervalTypes);
    }

    // MULTISET is assignable from...
    rules.add(SqlTypeName.MULTISET, EnumSet.of(SqlTypeName.MULTISET));

    // TINYINT is assignable from...
    rules.add(SqlTypeName.TINYINT, EnumSet.of(SqlTypeName.TINYINT));

    // SMALLINT is assignable from...
    rule.clear();
    rule.add(SqlTypeName.TINYINT);
    rule.add(SqlTypeName.SMALLINT);
    rules.add(SqlTypeName.SMALLINT, rule);

    // INTEGER is assignable from...
    rule.clear();<commit_msg>[CALCITE-3882] Remove duplicated code from SqlTypeAssignmentRule (<NAME>ang)<commit_after>/*
 * 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.calcite.sql.type;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Rules that determine whether a type is assignable from another type.
 */
public class SqlTypeAssignmentRule implements SqlTypeMappingRule {
  //~ Static fields/initializers ---------------------------------------------

  private static final SqlTypeAssignmentRule INSTANCE;

  //~ Instance fields --------------------------------------------------------

  private final Map<SqlTypeName, ImmutableSet<SqlTypeName>> map;

  //~ Constructors -----------------------------------------------------------

  /**
   * Creates a {@code SqlTypeAssignmentRules} with specified type mappings {@code map}.
   *
   * <p>Make this constructor private intentionally, use {@link #instance()}.
   *
   * @param map The type mapping, for each map entry, the values types can be assigned to
   *            the key type
   */
  private SqlTypeAssignmentRule(
      Map<SqlTypeName, ImmutableSet<SqlTypeName>> map) {
    this.map = ImmutableMap.copyOf(map);
  }

  static {
    final SqlTypeMappingRules.Builder rules = SqlTypeMappingRules.builder();

    final Set<SqlTypeName> rule = new HashSet<>();

    // IntervalYearMonth is assignable from...
    for (SqlTypeName interval : SqlTypeName.YEAR_INTERVAL_TYPES) {
      rules.add(interval, SqlTypeName.YEAR_INTERVAL_TYPES);
    }
    for (SqlTypeName interval : SqlTypeName.DAY_INTERVAL_TYPES) {
      rules.add(interval, SqlTypeName.DAY_INTERVAL_TYPES);
    }

    // MULTISET is assignable from...
    rules.add(SqlTypeName.MULTISET, EnumSet.of(SqlTypeName.MULTISET));

    // TINYINT is assignable from...
    rules.add(SqlTypeName.TINYINT, EnumSet.of(SqlTypeName.TINYINT));

    // SMALLINT is assignable from...
    rule.clear();
    rule.add(SqlTypeName.TINYINT);
    rule.add(SqlTypeName.SMALLINT);
    rules.add(SqlTypeName.SMALLINT, rule);

    // INTEGER is assignable from...
    rule.clear();
{'fraction_non_alphanumeric': 0.14066622691292877, 'fraction_numerical': 0.001978891820580475, 'mean_word_length': 2.9666448659254416, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6649319', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1653, 'n_words': 523}