<commit_before>/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets 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 io.druid.query.dimension;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.metamx.common.StringUtils;
import io.druid.query.extraction.ExtractionFn;
import io.druid.query.lookup.LookupExtractionFn;
import io.druid.query.lookup.LookupExtractor;
import io.druid.query.lookup.LookupReferencesManager;
import io.druid.query.filter.DimFilterCacheHelper;
import io.druid.segment.DimensionSelector;

import javax.annotation.Nullable;
import java.nio.ByteBuffer;

public class LookupDimensionSpec implements DimensionSpec
{
  private static final byte CACHE_TYPE_ID = 0x4;

  @JsonProperty
  private final String dimension;

  @JsonProperty
  private final String outputName;

  @JsonProperty
  private final LookupExtractor lookup;

  @JsonProperty
  private final boolean retainMissingValue;

  @JsonProperty
  private final String replaceMissingValueWith;

  @JsonProperty
  private final String name;

  @JsonProperty
  private final boolean optimize;

  private final LookupReferencesManager lookupReferencesManager;

  @JsonCreator
  public LookupDimensionSpec(
      @JsonProperty("dimension") String dimension,
      @JsonProperty("outputName") String outputName,
      @JsonProperty("lookup") LookupExtractor lookup,
      @JsonProperty("retainMissingValue") boolean retainMissingValue,
      @JsonProperty("replaceMissingValueWith") String replaceMissingValueWith,
      @JsonProperty("name") String name,
      @JacksonInject LookupReferencesManager lookupReferencesManager,
      @JsonProperty("optimize") Boolean optimize
  )
  {
    this.retainMissingValue = retainMissingValue;
    this.optimize = optimize == null ? true : optimize;
    this.replaceMissingValueWith = Strings.emptyToNull(replaceMissingValueWith);
    this.dimension = Preconditions.checkNotNull(dimension, "dimension can not be Null");
    this.outputName = Preconditions.checkNotNull(outputName, "outputName can not be Null");
    this.lookupReferencesManager = lookupReferencesManager;
    this.name = name;
    this.lookup = lookup;
    Preconditions.checkArgument(
        Strings.isNullOrEmpty(name) ^ (lookup == null),
        "name [%s] and lookup [%s] are mutually exclusive please provide either a name or a lookup", name, lookup
    );

    if (!Strings.isNullOrEmpty(name)) {
      Preconditions.checkNotNull(
          this.lookupReferencesManager,
          "The system is not configured to allow for lookups, please read about configuring a lookup manager in the docs"
      );
    }
  }

  @Override
  @JsonProperty
  public String getDimension()
  {
    return dimension;
  }

  @Override
  @JsonProperty
  public String getOutputName()
  {
    return outputName;
  }

  @JsonProperty
  @Nullable
  public LookupExtractor getLookup()
  {
    return lookup;
  }

  @JsonProperty
  @Nullable
  public String getName()
  {
    return name;
  }

  @Override
  public ExtractionFn getExtractionFn()
  {
    final LookupExtractor lookupExtractor = Strings.isNullOrEmpty(name)
                                            ? this.lookup
                                            : Preconditions.checkNotNull(
                                                this.lookupReferencesManager.get(name).get(),
                                                "can not find lookup with name [%s]",
                                                name
                                            );
    return new LookupExtractionFn(
        lookupExtractor,
        retainMissingValue,
        replaceMissingValueWith,
        lookupExtractor.isOneToOne(),
        optimize
    );

  }

  @Override
  public DimensionSelector decorate(DimensionSelector selector)
  {
    return selector;
  }

  @Override
  public byte[] getCacheKey()
  {
    byte[] dimensionBytes = StringUtils.toUtf8(dimension);
    byte[] dimExtractionFnBytes = Strings.isNullOrEmpty(name)
                                  ? getLookup().getCacheKey()
                                  : StringUtils.toUtf8(name);
    byte[] outputNameBytes = StringUtils.toUtf8(outputName);
    byte[] replaceWithBytes = StringUtils.toUtf8(Strings.nullToEmpty(replaceMissingValueWith));


    return ByteBuffer.allocate(6
                               + dimensionBytes.length
                               + outputNameBytes.length
                               + dimExtractionFnBytes.length
                               + replaceWithBytes.length)
                     .put(CACHE_TYPE_ID)
                     .put(dimensionBytes)
                     .put(DimFilterCacheHelper.STRING_SEPARATOR)
                     .put(outputNameBytes)
                     .put(DimFilterCacheHelper.STRING_SEPARATOR)
                     .put(dimExtractionFnBytes)
                     .put(DimFilterCacheHelper.STRING_SEPARATOR)<commit_msg>Improve NPE message in LookupDimensionSpec when lookup does not exist. (#3065)<commit_after>/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets 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 io.druid.query.dimension;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.metamx.common.StringUtils;
import io.druid.query.extraction.ExtractionFn;
import io.druid.query.filter.DimFilterCacheHelper;
import io.druid.query.lookup.LookupExtractionFn;
import io.druid.query.lookup.LookupExtractor;
import io.druid.query.lookup.LookupReferencesManager;
import io.druid.segment.DimensionSelector;

import javax.annotation.Nullable;
import java.nio.ByteBuffer;

public class LookupDimensionSpec implements DimensionSpec
{
  private static final byte CACHE_TYPE_ID = 0x4;

  @JsonProperty
  private final String dimension;

  @JsonProperty
  private final String outputName;

  @JsonProperty
  private final LookupExtractor lookup;

  @JsonProperty
  private final boolean retainMissingValue;

  @JsonProperty
  private final String replaceMissingValueWith;

  @JsonProperty
  private final String name;

  @JsonProperty
  private final boolean optimize;

  private final LookupReferencesManager lookupReferencesManager;

  @JsonCreator
  public LookupDimensionSpec(
      @JsonProperty("dimension") String dimension,
      @JsonProperty("outputName") String outputName,
      @JsonProperty("lookup") LookupExtractor lookup,
      @JsonProperty("retainMissingValue") boolean retainMissingValue,
      @JsonProperty("replaceMissingValueWith") String replaceMissingValueWith,
      @JsonProperty("name") String name,
      @JacksonInject LookupReferencesManager lookupReferencesManager,
      @JsonProperty("optimize") Boolean optimize
  )
  {
    this.retainMissingValue = retainMissingValue;
    this.optimize = optimize == null ? true : optimize;
    this.replaceMissingValueWith = Strings.emptyToNull(replaceMissingValueWith);
    this.dimension = Preconditions.checkNotNull(dimension, "dimension can not be Null");
    this.outputName = Preconditions.checkNotNull(outputName, "outputName can not be Null");
    this.lookupReferencesManager = lookupReferencesManager;
    this.name = name;
    this.lookup = lookup;
    Preconditions.checkArgument(
        Strings.isNullOrEmpty(name) ^ (lookup == null),
        "name [%s] and lookup [%s] are mutually exclusive please provide either a name or a lookup", name, lookup
    );

    if (!Strings.isNullOrEmpty(name)) {
      Preconditions.checkNotNull(
          this.lookupReferencesManager,
          "The system is not configured to allow for lookups, please read about configuring a lookup manager in the docs"
      );
    }
  }

  @Override
  @JsonProperty
  public String getDimension()
  {
    return dimension;
  }

  @Override
  @JsonProperty
  public String getOutputName()
  {
    return outputName;
  }

  @JsonProperty
  @Nullable
  public LookupExtractor getLookup()
  {
    return lookup;
  }

  @JsonProperty
  @Nullable
  public String getName()
  {
    return name;
  }

  @Override
  public ExtractionFn getExtractionFn()
  {
    final LookupExtractor lookupExtractor = Strings.isNullOrEmpty(name)
                                            ? this.lookup
                                            : Preconditions.checkNotNull(
                                                lookupReferencesManager.get(name),
                                                "Lookup [%s] not found",
                                                name
                                            ).get();

    return new LookupExtractionFn(
        lookupExtractor,
        retainMissingValue,
        replaceMissingValueWith,
        lookupExtractor.isOneToOne(),
        optimize
    );
  }

  @Override
  public DimensionSelector decorate(DimensionSelector selector)
  {
    return selector;
  }

  @Override
  public byte[] getCacheKey()
  {
    byte[] dimensionBytes = StringUtils.toUtf8(dimension);
    byte[] dimExtractionFnBytes = Strings.isNullOrEmpty(name)
                                  ? getLookup().getCacheKey()
                                  : StringUtils.toUtf8(name);
    byte[] outputNameBytes = StringUtils.toUtf8(outputName);
    byte[] replaceWithBytes = StringUtils.toUtf8(Strings.nullToEmpty(replaceMissingValueWith));


    return ByteBuffer.allocate(6
                               + dimensionBytes.length
                               + outputNameBytes.length
                               + dimExtractionFnBytes.length
                               + replaceWithBytes.length)
                     .put(CACHE_TYPE_ID)
                     .put(dimensionBytes)
                     .put(DimFilterCacheHelper.STRING_SEPARATOR)
                     .put(outputNameBytes)
                     .put(DimFilterCacheHelper.STRING_SEPARATOR)
                     .put(dimExtractionFnBytes)
                     .put(DimFilterCacheHelper.STRING_SEPARATOR)
{'fraction_non_alphanumeric': 0.07323774362496781, 'fraction_numerical': 0.002232334506739933, 'mean_word_length': 2.719029374201788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '18949', 'n_tokens_mistral': 3028, 'n_tokens_neox': 2904, 'n_words': 777}