Refine search
@Description( name = "xpath_long", value = "_FUNC_(xml, xpath) - Returns a long value that matches the xpath expression", extended = "Example:\n" + " > SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)') FROM src LIMIT 1;\n" + " 3") public class UDFXPathLong extends UDF { private final UDFXPathUtil xpath = new UDFXPathUtil(); public long evaluate(String xml, String path) { return xpath.evalNumber(xml, path).longValue(); } }
/** * UDFRowSequence. */ @Description(name = "row_sequence", value = "_FUNC_() - Returns a generated row sequence number starting from 1") @UDFType(deterministic = false, stateful = true) public class UDFRowSequence extends UDF { private LongWritable result = new LongWritable(); public UDFRowSequence() { result.set(0); } public LongWritable evaluate() { result.set(result.get() + 1); return result; } }
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) { 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); }
names = desc.name().split(","); for (int i=0; i<names.length; i++) { names[i] = names[i].trim(); value = desc.value(); extended = desc.extended(); builder.setDeterministic(type.deterministic());
private static String getName(GenericUDF hiveUDF) { String udfName = null; if (hiveUDF instanceof GenericUDFBridge) { udfName = ((GenericUDFBridge) hiveUDF).getUdfName(); } else { Class<? extends GenericUDF> udfClass = hiveUDF.getClass(); Annotation udfAnnotation = udfClass.getAnnotation(Description.class); if (udfAnnotation != null && udfAnnotation instanceof Description) { Description udfDescription = (Description) udfAnnotation; udfName = udfDescription.name(); if (udfName != null) { String[] aliases = udfName.split(","); if (aliases.length > 0) { udfName = aliases[0]; } } } if (udfName == null || udfName.isEmpty()) { udfName = hiveUDF.getClass().getName(); int indx = udfName.lastIndexOf("."); if (indx >= 0) { indx += 1; udfName = udfName.substring(indx); } } } return udfName; }
outStream.writeBytes(desc.value().replace("_FUNC_", funcName)); if (descFunc.isExtended()) { Set<String> synonyms = FunctionRegistry.getFunctionSynonyms(funcName); outStream.writeBytes("\nSynonyms: " + join(synonyms, ", ")); if (desc.extended().length() > 0) { outStream.writeBytes("\n" + desc.extended().replace("_FUNC_", funcName));
private <I> void register(Class<? extends I> clazz, ArrayListMultimap<String, Class<? extends I>> methods) { Description desc = clazz.getAnnotation(Description.class); Stream<String> namesStream; if (desc != null) { namesStream = Stream.of(desc.name().split(",")) .map(String::trim); } else { namesStream = Stream.of(clazz) .map(Class::getName) .map(name -> name.replace('.', '_')); } // Checks specified array of function names whether they should be replaced // using FUNCTION_REPLACE_MAP map. namesStream.map(String::toLowerCase) .map(functionName -> FUNCTION_REPLACE_MAP.getOrDefault(functionName, functionName)) .forEach(name -> methods.put(name, clazz)); UDFType type = clazz.getAnnotation(UDFType.class); if (type != null && !type.deterministic()) { nonDeterministicUDFs.add(clazz); } }
@Description( name = "lag", value = "LAG (scalar_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause); " + "The LAG function is used to access data from a previous row.", extended = "Example:\n " + "select p1.p_mfgr, p1.p_name, p1.p_size,\n" + " p1.p_size - lag(p1.p_size,1,p1.p_size) over( distribute by p1.p_mfgr sort by p1.p_name) as deltaSz\n" + " from part p1 join part p2 on p1.p_partkey = p2.p_partkey") @UDFType(impliesOrder = true) public class GenericUDFLag extends GenericUDFLeadLag { @Override protected String _getFnName() { return "lag"; } @Override protected int getIndex(int amt) { return pItr.getIndex() - 1 - amt; } @Override protected Object getRow(int amt) throws HiveException { return pItr.lag(amt + 1); } }
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; }
private static String getName(GenericUDF hiveUDF) { String udfName = null; if (hiveUDF instanceof GenericUDFBridge) { udfName = ((GenericUDFBridge) hiveUDF).getUdfName(); } else { Class<? extends GenericUDF> udfClass = hiveUDF.getClass(); Annotation udfAnnotation = udfClass.getAnnotation(Description.class); if (udfAnnotation != null && udfAnnotation instanceof Description) { Description udfDescription = (Description) udfAnnotation; udfName = udfDescription.name(); if (udfName != null) { String[] aliases = udfName.split(","); if (aliases.length > 0) udfName = aliases[0]; } } if (udfName == null || udfName.isEmpty()) { udfName = hiveUDF.getClass().getName(); int indx = udfName.lastIndexOf("."); if (indx >= 0) { indx += 1; udfName = udfName.substring(indx); } } } return udfName; }
outStream.writeBytes(desc.value().replace("_FUNC_", funcName)); if (descFunc.isExtended()) { Set<String> synonyms = FunctionRegistry.getFunctionSynonyms(funcName); outStream.writeBytes("\nSynonyms: " + join(synonyms, ", ")); if (desc.extended().length() > 0) { outStream.writeBytes("\n" + desc.extended().replace("_FUNC_", funcName));
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); } }
@Description( name = "xpath_number,xpath_double", value = "_FUNC_(xml, xpath) - Returns a double value that matches the xpath expression", extended = "Example:\n" + " > SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)') FROM src LIMIT 1;\n" + " 3.0") public class UDFXPathDouble extends UDF { private final UDFXPathUtil xpath = new UDFXPathUtil(); public double evaluate(String xml, String path) { return xpath.evalNumber(xml, path).doubleValue(); } }
@Description( name = "lead", value = "LEAD (scalar_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause); " @UDFType(impliesOrder = true) public class GenericUDFLead extends GenericUDFLeadLag {
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; }
@Override public Void visitCall(RexCall call) { if(AnnotationUtils.getAnnotation(GenericUDFOPNotNull.class, Description.class).name().equals(call.getOperator().getName())) { if(call.getOperands().get(0) instanceof RexInputRef && !types.get(((RexInputRef)call.getOperands().get(0)).getIndex()).getType().isNullable()) { // No need to add not null filter for a constant. throw new Util.FoundOne(call); } } return super.visitCall(call); }
outStream.writeBytes(desc.value().replace("_FUNC_", funcName)); if (descFunc.isExtended()) { Set<String> synonyms = FunctionRegistry.getFunctionSynonyms(funcName); outStream.writeBytes("\nSynonyms: " + join(synonyms, ", ")); if (desc.extended().length() > 0) { outStream.writeBytes("\n" + desc.extended().replace("_FUNC_", funcName));
@Description( name = "xpath_float", value = "_FUNC_(xml, xpath) - Returns a float value that matches the xpath expression", extended = "Example:\n" + " > SELECT _FUNC_('<a><b>1</b><b>2</b></a>','sum(a/b)') FROM src LIMIT 1;\n" + " 3.0") public class UDFXPathFloat extends UDF { private final UDFXPathUtil xpath = new UDFXPathUtil(); public float evaluate(String xml, String path) { return xpath.evalNumber(xml, path).floatValue(); } }
/** * UDFUUID. * */ @Description(name = "uuid", value = "_FUNC_() - Returns a universally unique identifier (UUID) string.", extended = "The value is returned as a canonical UUID 36-character string.\n" + "Example:\n" + " > SELECT _FUNC_();\n" + " '0baf1f52-53df-487f-8292-99a03716b688'\n" + " > SELECT _FUNC_();\n" + " '36718a53-84f5-45d6-8796-4f79983ad49d'") @UDFType(deterministic = false) public class UDFUUID extends UDF { private final Text result = new Text(); /** * Returns a universally unique identifier (UUID) string (36 characters). * * @return Text */ public Text evaluate() { result.set(UUID.randomUUID().toString()); return result; } }
@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; }