All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.druid.segment.virtual.SingleStringInputCachingExpressionColumnValueSelector Maven / Gradle / Ivy

There is a newer version: 30.0.1
Show newest version
/*
 * 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.druid.segment.virtual;

import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.math.expr.Expr;
import org.apache.druid.math.expr.ExprEval;
import org.apache.druid.math.expr.ExpressionType;
import org.apache.druid.math.expr.InputBindings;
import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
import org.apache.druid.segment.DimensionDictionarySelector;
import org.apache.druid.segment.DimensionSelector;
import org.apache.druid.segment.RowIdSupplier;
import org.apache.druid.segment.data.IndexedInts;

import javax.annotation.Nullable;

/**
 * Like {@link ExpressionColumnValueSelector}, but caches results for the first CACHE_SIZE dictionary IDs of
 * a string column. Must only be used on selectors with dictionaries.
 */
public class SingleStringInputCachingExpressionColumnValueSelector extends BaseExpressionColumnValueSelector
{
  private static final int CACHE_SIZE = 1000;

  private final DimensionSelector selector;
  private final Expr expression;
  private final Expr.ObjectBinding bindings;
  @Nullable
  private final ExprEval[] arrayEvalCache;
  @Nullable
  private final LruEvalCache lruEvalCache;

  public SingleStringInputCachingExpressionColumnValueSelector(
      final DimensionSelector selector,
      final Expr expression,
      @Nullable final RowIdSupplier rowIdSupplier
  )
  {
    super(rowIdSupplier);

    // Verify expression has just one binding.
    if (expression.analyzeInputs().getRequiredBindings().size() != 1) {
      throw new ISE("Expected expression with just one binding");
    }

    this.selector = Preconditions.checkNotNull(selector, "selector");
    this.expression = Preconditions.checkNotNull(expression, "expression");

    final Supplier inputSupplier = ExpressionSelectors.supplierFromDimensionSelector(selector, false, false);
    this.bindings = InputBindings.forInputSupplier(ExpressionType.STRING, inputSupplier);

    if (selector.getValueCardinality() == DimensionDictionarySelector.CARDINALITY_UNKNOWN) {
      throw new ISE("Selector must have a dictionary");
    } else if (selector.getValueCardinality() <= CACHE_SIZE) {
      arrayEvalCache = new ExprEval[selector.getValueCardinality()];
      lruEvalCache = null;
    } else {
      arrayEvalCache = null;
      lruEvalCache = new LruEvalCache(expression, bindings);
    }
  }

  @Override
  public void inspectRuntimeShape(final RuntimeShapeInspector inspector)
  {
    super.inspectRuntimeShape(inspector);
    inspector.visit("selector", selector);
    inspector.visit("expression", expression);
  }

  @Override
  protected ExprEval eval()
  {
    final IndexedInts row = selector.getRow();

    if (row.size() == 1) {
      final int id = row.get(0);

      if (arrayEvalCache != null) {
        if (arrayEvalCache[id] == null) {
          arrayEvalCache[id] = expression.eval(bindings);
        }
        return arrayEvalCache[id];
      } else {
        assert lruEvalCache != null;
        return lruEvalCache.compute(id);
      }
    }

    return expression.eval(bindings);
  }

  @Override
  public boolean isNull()
  {
    // It is possible for an expression to have a non-null String value but it can return null when parsed
    // as a primitive long/float/double.
    // ExprEval.isNumericNull checks whether the parsed primitive value is null or not.
    return eval().isNumericNull();
  }

  public static class LruEvalCache
  {
    private final Expr expression;
    private final Expr.ObjectBinding bindings;
    private final Int2ObjectLinkedOpenHashMap m = new Int2ObjectLinkedOpenHashMap<>(CACHE_SIZE);

    public LruEvalCache(final Expr expression, final Expr.ObjectBinding bindings)
    {
      this.expression = expression;
      this.bindings = bindings;
    }

    public ExprEval compute(final int id)
    {
      ExprEval value = m.getAndMoveToFirst(id);

      if (value == null) {
        value = expression.eval(bindings);
        m.putAndMoveToFirst(id, value);

        if (m.size() > CACHE_SIZE) {
          m.removeLast();
        }
      }

      return value;
    }
  }
}