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

org.apache.hadoop.hive.ql.optimizer.ppr.PartExprEvalUtils Maven / Gradle / Ivy

Go to download

Hive is a data warehouse infrastructure built on top of Hadoop see http://wiki.apache.org/hadoop/Hive

The 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.hadoop.hive.ql.optimizer.ppr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator;
import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluatorFactory;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

public class PartExprEvalUtils {
  /**
   * Evaluate expression with partition columns
   *
   * @param expr
   * @param partSpec
   * @param rowObjectInspector
   * @return value returned by the expression
   * @throws HiveException
   */
  static synchronized public Object evalExprWithPart(ExprNodeDesc expr, LinkedHashMap partSpec,
      StructObjectInspector rowObjectInspector) throws HiveException {
    Object[] rowWithPart = new Object[2];
    // Create the row object
    ArrayList partNames = new ArrayList();
    ArrayList partValues = new ArrayList();
    ArrayList partObjectInspectors = new ArrayList();
    for (Map.Entry entry : partSpec.entrySet()) {
      partNames.add(entry.getKey());
      partValues.add(entry.getValue());
      partObjectInspectors
          .add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
    }
    StructObjectInspector partObjectInspector = ObjectInspectorFactory
        .getStandardStructObjectInspector(partNames, partObjectInspectors);

    rowWithPart[1] = partValues;
    ArrayList ois = new ArrayList(
        2);
    ois.add(rowObjectInspector);
    ois.add(partObjectInspector);
    StructObjectInspector rowWithPartObjectInspector = ObjectInspectorFactory
        .getUnionStructObjectInspector(ois);

    ExprNodeEvaluator evaluator = ExprNodeEvaluatorFactory
        .get(expr);
    ObjectInspector evaluateResultOI = evaluator
        .initialize(rowWithPartObjectInspector);
    Object evaluateResultO = evaluator.evaluate(rowWithPart);

    return ((PrimitiveObjectInspector) evaluateResultOI)
        .getPrimitiveJavaObject(evaluateResultO);
  }

  static synchronized public Map prepareExpr(
      ExprNodeDesc expr, List partNames,
      StructObjectInspector rowObjectInspector) throws HiveException {

    // Create the row object
    List partObjectInspectors = new ArrayList();
    for (int i = 0; i < partNames.size(); i++) {
      partObjectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
    }
    StructObjectInspector partObjectInspector = ObjectInspectorFactory
        .getStandardStructObjectInspector(partNames, partObjectInspectors);

    List ois = new ArrayList(2);
    ois.add(rowObjectInspector);
    ois.add(partObjectInspector);
    StructObjectInspector rowWithPartObjectInspector =
      ObjectInspectorFactory.getUnionStructObjectInspector(ois);

    ExprNodeEvaluator evaluator = ExprNodeEvaluatorFactory.get(expr);
    ObjectInspector evaluateResultOI = evaluator.initialize(rowWithPartObjectInspector);

    Map result =
      new HashMap();
    result.put((PrimitiveObjectInspector)evaluateResultOI,  evaluator);
    return result;
  }

  static synchronized public Object evaluateExprOnPart(
      Map pair, Object[] rowWithPart)
      throws HiveException {
    assert(pair.size() > 0);
    // only get the 1st entry from the map
    Map.Entry entry = pair.entrySet().iterator().next();
    PrimitiveObjectInspector evaluateResultOI = entry.getKey();
    ExprNodeEvaluator evaluator = entry.getValue();

    Object evaluateResultO = evaluator.evaluate(rowWithPart);

    return evaluateResultOI.getPrimitiveJavaObject(evaluateResultO);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy