Java Code Examples for org.apache.pig.FuncSpec#getClassName()

The following examples show how to use org.apache.pig.FuncSpec#getClassName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PigRelUdfConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a Pig UDF, given its {@link FuncSpec} and a list of relational
 * operands (function arguments). To call this function, the arguments of
 * Pig functions need to be converted into the relational types before.
 *
 * @param builder The relational builder
 * @param pigFunc Pig function description
 * @param operands Relational operands for the function
 * @param returnType Function return data type
 * @return The SQL calls equivalent to the Pig function
 */
static RexNode convertPigFunction(PigRelBuilder builder, FuncSpec pigFunc,
    ImmutableList<RexNode> operands, RelDataType returnType) throws FrontendException {
  // First, check the map for the direct mapping SQL builtin
  final SqlOperator operator = BUILTIN_FUNC.get(pigFunc.getClassName());
  if (operator != null) {
    return builder.call(operator, operands);
  }

  // If no mapping found, build the argument wrapper to convert the relation operands
  // into a Pig tuple so that the Pig function can consume it.
  try {
    // Find the implementation method for the Pig function from
    // the class defining the UDF.
    final Class clazz = Class.forName(pigFunc.getClassName());
    final Method method =
        PIG_UDF_FINDER.findPigUdfImplementationMethod(clazz);

    // Now create the argument wrapper. Depend on the type of the UDF, the
    // relational operands are converted into a Pig Tuple or Pig DataBag
    // with the appropriate wrapper.
    final SqlUserDefinedFunction convertOp =
        Accumulator.class.isAssignableFrom(clazz)
            ? PigRelSqlUdfs.createPigBagUDF(operands)
            : PigRelSqlUdfs.createPigTupleUDF(operands);
    final RexNode rexTuple = builder.call(convertOp, operands);

    // Then convert the Pig function into a @SqlUserDefinedFunction.
    SqlUserDefinedFunction userFuncOp =
        PigRelSqlUdfs.createGeneralPigUdf(clazz.getSimpleName(),
            method, pigFunc, rexTuple.getType(), returnType);

    // Ready to return SqlCall after having SqlUDF and operand
    return builder.call(userFuncOp, ImmutableList.of(rexTuple));
  } catch (ClassNotFoundException e) {
    throw new FrontendException("Cannot find the implementation for Pig UDF class: "
        + pigFunc.getClassName());
  }
}
 
Example 2
Source File: LineageFindRelVisitor.java    From spork with Apache License 2.0 5 votes vote down vote up
private Class instantiateCaster(FuncSpec funcSpec) throws VisitorException {
    if ( funcSpec == null ) {
        return null;
    }

    if( func2casterMap.containsKey(funcSpec) ) {
      return func2casterMap.get(funcSpec);
    }

    LoadCaster caster = null;
    Object obj = PigContext.instantiateFuncFromSpec(funcSpec);
    try {
        if (obj instanceof LoadFunc) {
            caster = ((LoadFunc)obj).getLoadCaster();
        } else if (obj instanceof StreamToPig) {
            caster = ((StreamToPig)obj).getLoadCaster();
        } else {
            throw new VisitorException("Invalid class type " + funcSpec.getClassName(),
                                       2270, PigException.BUG );
        }
    } catch (IOException e) {
        throw new VisitorException("Invalid class type " + funcSpec.getClassName(),
                                     2270, e );
    }

    Class retval = (caster == null) ? null : caster.getClass();
    func2casterMap.put(funcSpec, retval);
    return retval;
}
 
Example 3
Source File: PigContext.java    From spork with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public Class getClassForAlias(String alias) throws IOException{
    String className = null;
    FuncSpec funcSpec = null;
    if (definedFunctions != null) {
        funcSpec = definedFunctions.get(alias);
    }
    if (funcSpec != null) {
        className = funcSpec.getClassName();
    }else{
        className = FuncSpec.getClassNameFromSpec(alias);
    }
    return resolveClassName(className);
}