org.apache.hadoop.hive.ql.exec.Description Java Examples

The following examples show how to use org.apache.hadoop.hive.ql.exec.Description. 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: HiveFunctionRegistry.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private <C,I> void register(Class<? extends I> clazz, ArrayListMultimap<String,Class<? extends I>> methods) {
  Description desc = clazz.getAnnotation(Description.class);
  String[] names;
  if (desc != null) {
    names = desc.name().split(",");
    for (int i=0; i<names.length; i++) {
      names[i] = names[i].trim();
    }
  }else{
    names = new String[]{clazz.getName().replace('.', '_')};
  }

  UDFType type = clazz.getAnnotation(UDFType.class);
  if (type != null && !type.deterministic()) {
    nonDeterministicUDFs.add(clazz);
  }


  for(int i=0; i<names.length;i++) {
    methods.put(names[i].toLowerCase(), clazz);
  }
}
 
Example #2
Source File: UDAFToOrderedList.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected final CommandLine parseOptions(String optionValue) throws UDFArgumentException {
    String[] args = optionValue.split("\\s+");
    Options opts = getOptions();
    opts.addOption("help", false, "Show function help");
    CommandLine cl = CommandLineUtils.parseOptions(args, opts);

    if (cl.hasOption("help")) {
        Description funcDesc = getClass().getAnnotation(Description.class);
        final String cmdLineSyntax;
        if (funcDesc == null) {
            cmdLineSyntax = getClass().getSimpleName();
        } else {
            String funcName = funcDesc.name();
            cmdLineSyntax = funcName == null ? getClass().getSimpleName()
                    : funcDesc.value().replace("_FUNC_", funcDesc.name());
        }
        StringWriter sw = new StringWriter();
        sw.write('\n');
        PrintWriter pw = new PrintWriter(sw);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, cmdLineSyntax, null, opts,
            HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, true);
        pw.flush();
        String helpMsg = sw.toString();
        throw new UDFArgumentException(helpMsg);
    }

    return cl;
}
 
Example #3
Source File: PLSAPredictUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected final CommandLine parseOptions(String optionValue) throws UDFArgumentException {
    String[] args = optionValue.split("\\s+");
    Options opts = getOptions();
    opts.addOption("help", false, "Show function help");
    CommandLine cl = CommandLineUtils.parseOptions(args, opts);

    if (cl.hasOption("help")) {
        Description funcDesc = getClass().getAnnotation(Description.class);
        final String cmdLineSyntax;
        if (funcDesc == null) {
            cmdLineSyntax = getClass().getSimpleName();
        } else {
            String funcName = funcDesc.name();
            cmdLineSyntax = funcName == null ? getClass().getSimpleName()
                    : funcDesc.value().replace("_FUNC_", funcDesc.name());
        }
        StringWriter sw = new StringWriter();
        sw.write('\n');
        PrintWriter pw = new PrintWriter(sw);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, cmdLineSyntax, null, opts,
            HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, true);
        pw.flush();
        String helpMsg = sw.toString();
        throw new UDFArgumentException(helpMsg);
    }

    return cl;
}
 
Example #4
Source File: LDAPredictUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected final CommandLine parseOptions(String optionValue) throws UDFArgumentException {
    String[] args = optionValue.split("\\s+");
    Options opts = getOptions();
    opts.addOption("help", false, "Show function help");
    CommandLine cl = CommandLineUtils.parseOptions(args, opts);

    if (cl.hasOption("help")) {
        Description funcDesc = getClass().getAnnotation(Description.class);
        final String cmdLineSyntax;
        if (funcDesc == null) {
            cmdLineSyntax = getClass().getSimpleName();
        } else {
            String funcName = funcDesc.name();
            cmdLineSyntax = funcName == null ? getClass().getSimpleName()
                    : funcDesc.value().replace("_FUNC_", funcDesc.name());
        }
        StringWriter sw = new StringWriter();
        sw.write('\n');
        PrintWriter pw = new PrintWriter(sw);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, cmdLineSyntax, null, opts,
            HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, true);
        pw.flush();
        String helpMsg = sw.toString();
        throw new UDFArgumentException(helpMsg);
    }

    return cl;
}
 
Example #5
Source File: UDTFWithOptions.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private void showHelp(@Nonnull Options opts, @Nullable String errMsg)
        throws UDFArgumentException {
    Description funcDesc = getClass().getAnnotation(Description.class);
    final String cmdLineSyntax;
    if (funcDesc == null) {
        cmdLineSyntax = getClass().getSimpleName();
    } else {
        String funcName = funcDesc.name();
        cmdLineSyntax = funcName == null ? getClass().getSimpleName()
                : funcDesc.value().replace("_FUNC_", funcDesc.name());
    }
    StringWriter sw = new StringWriter();
    sw.write('\n');
    if (errMsg != null) {
        if (funcDesc != null && funcDesc.name() != null) {
            errMsg = errMsg.replace("_FUNC_", funcDesc.name());
        }
        sw.write(errMsg);
        sw.write("\n\n");
    }
    PrintWriter pw = new PrintWriter(sw);
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, cmdLineSyntax, null, opts,
        HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, true);
    pw.flush();
    String helpMsg = sw.toString();
    throw new UDFArgumentException(helpMsg);
}
 
Example #6
Source File: UDFWithOptions.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private void showHelp(@Nonnull Options opts, @Nullable String errMsg)
        throws UDFArgumentException {
    Description funcDesc = getClass().getAnnotation(Description.class);
    final String cmdLineSyntax;
    if (funcDesc == null) {
        cmdLineSyntax = getClass().getSimpleName();
    } else {
        String funcName = funcDesc.name();
        cmdLineSyntax = funcName == null ? getClass().getSimpleName()
                : funcDesc.value().replace("_FUNC_", funcDesc.name());
    }
    StringWriter sw = new StringWriter();
    sw.write('\n');
    if (errMsg != null) {
        if (funcDesc != null && funcDesc.name() != null) {
            errMsg = errMsg.replace("_FUNC_", funcDesc.name());
        }
        sw.write(errMsg);
        sw.write("\n\n");
    }
    PrintWriter pw = new PrintWriter(sw);
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, cmdLineSyntax, null, opts,
        HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, true);
    pw.flush();
    String helpMsg = sw.toString();
    throw new UDFArgumentException(helpMsg);
}
 
Example #7
Source File: UDAFEvaluatorWithOptions.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected final CommandLine parseOptions(@Nonnull String optionValue)
        throws UDFArgumentException {
    String[] args = optionValue.split("\\s+");
    Options opts = getOptions();
    opts.addOption("help", false, "Show function help");

    final CommandLine cl;
    try {
        cl = CommandLineUtils.parseOptions(args, opts);
    } catch (IllegalArgumentException e) {
        throw new UDFArgumentException(e);
    }

    if (cl.hasOption("help")) {
        Description funcDesc = getClass().getAnnotation(Description.class);
        final String cmdLineSyntax;
        if (funcDesc == null) {
            cmdLineSyntax = getClass().getSimpleName();
        } else {
            String funcName = funcDesc.name();
            cmdLineSyntax = funcName == null ? getClass().getSimpleName()
                    : funcDesc.value().replace("_FUNC_", funcDesc.name());
        }
        StringWriter sw = new StringWriter();
        sw.write('\n');
        PrintWriter pw = new PrintWriter(sw);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(pw, HelpFormatter.DEFAULT_WIDTH, cmdLineSyntax, null, opts,
            HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, true);
        pw.flush();
        String helpMsg = sw.toString();
        throw new UDFArgumentException(helpMsg);
    }

    return cl;
}
 
Example #8
Source File: HiveSqlOperatorTable.java    From marble with Apache License 2.0 4 votes vote down vote up
private void registerUDAF(Reflections reflections) {
  Set<Class> udafClasses = Sets.union(
      reflections.getSubTypesOf(GenericUDAFResolver2.class),
      reflections.getSubTypesOf(UDAF.class));
  for (Class clazz : udafClasses) {
    boolean isWindowFunc = false;
    Description desc = (Description) clazz.getAnnotation(Description.class);
    WindowFunctionDescription windowFunctionDescription = null;
    if (desc == null) {
      windowFunctionDescription =
          (WindowFunctionDescription) clazz
              .getAnnotation(WindowFunctionDescription.class);
      if (windowFunctionDescription != null
          && windowFunctionDescription.description() != null) {
        desc = windowFunctionDescription.description();
        isWindowFunc = true;
      }
    }
    //ignore the operators that need to be excluded
    if (desc == null || isWindowFunc || EXCLUDED_HIVE_UDAF_LIST.contains(
        clazz)) {
      continue;
    }
    String[] names = desc.name().split(",");
    for (int i = 0; i < names.length; i++) {
      String upName = names[i].toUpperCase();
      methodsUDAF.put(upName, clazz);
      SqlAggFunction aggOperatorInSqlStdOperatorTable =
          (SqlAggFunction) getOperatorInSqlStdOperatorTable(
              upName, SqlSyntax.FUNCTION, true);
      HiveSqlAggFunction sqlAggFunction;
      if (aggOperatorInSqlStdOperatorTable == null) {
        sqlAggFunction = new HiveSqlAggFunction(upName, false,
            false, HiveSqlUDAFReturnTypeInference.INSTANCE);

      } else {
        sqlAggFunction = new HiveSqlAggFunction(upName,
            aggOperatorInSqlStdOperatorTable.getNameAsId(),
            aggOperatorInSqlStdOperatorTable.getKind(),
            aggOperatorInSqlStdOperatorTable.getFunctionType(),
            false, false, HiveSqlUDAFReturnTypeInference.INSTANCE);
      }
      register(sqlAggFunction);
    }

  }
}
 
Example #9
Source File: HiveFunctionLoader.java    From tajo with Apache License 2.0 4 votes vote down vote up
static void buildFunctionsFromUDF(Set<Class<? extends UDF>> classes, List<FunctionDesc> list, String jarurl) {
  for (Class<? extends UDF> clazz: classes) {
    String [] names;
    String value = null, extended = null;

    Description desc = clazz.getAnnotation(Description.class);

    // Check @Description annotation (if exists)
    if (desc != null) {
      names = desc.name().split(",");
      for (int i=0; i<names.length; i++) {
        names[i] = names[i].trim();
      }

      value = desc.value();
      extended = desc.extended();
    }
    else {
      names = new String [] {clazz.getName().replace('.','_')};
    }

    // actual function descriptor building
    FunctionDescBuilder builder = new FunctionDescBuilder();

    UDFType type = clazz.getDeclaredAnnotation(UDFType.class);
    if (type != null) {
      builder.setDeterministic(type.deterministic());
    }

    builder.setFunctionType(CatalogProtos.FunctionType.UDF);

    if (value != null) {
      builder.setDescription(value);
    }

    if (extended != null) {
      builder.setExample(extended);
    }

    UDFInvocationDesc udfInvocation = new UDFInvocationDesc(CatalogProtos.UDFtype.HIVE, clazz.getName(), jarurl, true);

    // verify 'evaluate' method and extract return type and parameter types
    for (Method method: clazz.getMethods()) {
      if (method.getName().equals("evaluate")) {
        registerMethod(method, names, udfInvocation, builder, list);
      }
    }
  }
}