@Override public String apply(Integer row){ FieldValue value = table.get(row, name); if(Objects.equals(FieldValues.MISSING_VALUE, value)){ throw new MissingValueException(name); } return value.asString(); } };
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); String input = getRequiredArgument(arguments, 0, "input").asString(); String pattern = getRequiredArgument(arguments, 1, "pattern").asString(); Matcher matcher = Pattern.compile(pattern).matcher(input); // "The string is considered to match the pattern if any substring matches the pattern" Boolean result = Boolean.valueOf(matcher.find()); return FieldValueUtil.create(TypeInfos.CATEGORICAL_BOOLEAN, result); } };
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 3); String input = getRequiredArgument(arguments, 0, "input").asString(); String regex = getRequiredArgument(arguments, 1, "pattern").asString(); Pattern pattern = RegExUtil.compile(regex, null); Matcher matcher = pattern.matcher(input); String replacement = getRequiredArgument(arguments, 2, "replacement").asString(); String result = matcher.replaceAll(replacement); return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); } };
private FieldValue evaluate(FieldValue value){ String result = evaluate(value.asString()); return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); } }
@Override public List<String> process(){ TextIndex textIndex = getTextIndex(); FieldValue value = getValue(); Cache<FieldValue, List<String>> termTokenCache = CacheUtil.getValue(textIndex, TextUtil.termTokenCaches, TextUtil.termTokenCacheLoader); List<String> tokens = termTokenCache.getIfPresent(value); if(tokens == null){ String string = value.asString(); tokens = TextUtil.tokenize(textIndex, string); termTokenCache.put(value, tokens); } return tokens; } }
@Override public List<String> process(){ TextIndex textIndex = getTextIndex(); FieldValue value = getValue(); Cache<FieldValue, List<String>> textTokenCache = CacheUtil.getValue(textIndex, TextUtil.textTokenCaches, TextUtil.textTokenCacheLoader); List<String> tokens = textTokenCache.getIfPresent(value); if(tokens == null){ String string = TextUtil.normalize(textIndex, value.asString()); tokens = TextUtil.tokenize(textIndex, string); textTokenCache.put(value, tokens); } return tokens; } }
String string = getRequiredArgument(arguments, 1).asString();
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); ZonedDateTime zonedDateTime = getRequiredArgument(arguments, 0, "input").asZonedDateTime(ZoneId.systemDefault()); Date date = Date.from(zonedDateTime.toInstant()); String pattern = translatePattern(getRequiredArgument(arguments, 1, "pattern").asString()); String result; try { result = String.format(pattern, date); } catch(IllegalFormatException ife){ throw new FunctionException(this, "Invalid \'pattern\' value") .initCause(ife); } return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); }
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 3); String string = getRequiredArgument(arguments, 0, "input").asString(); int position = getRequiredArgument(arguments, 1, "startPos").asInteger(); if(position < 1){ throw new FunctionException(this, "Invalid \'startPos\' value " + position + ". Must be equal or greater than 1"); } // "The first character of a string is located at position 1 (not position 0)" int javaPosition = Math.min(position - 1, string.length()); int length = getRequiredArgument(arguments, 2, "length").asInteger(); if(length < 0){ throw new FunctionException(this, "Invalid \'length\' value " + length); } int javaLength = Math.min(length, (string.length() - javaPosition)); // This expression must never throw a StringIndexOutOfBoundsException String result = string.substring(javaPosition, javaPosition + javaLength); return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); } };
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); Number number = getRequiredArgument(arguments, 0, "input").asNumber(); String pattern = getRequiredArgument(arguments, 1, "pattern").asString(); String result; // According to the java.util.Formatter javadoc, Java formatting is more strict than C's printf formatting. // For example, in Java, if a conversion is incompatible with a flag, an exception will be thrown. In C's printf, inapplicable flags are silently ignored. try { result = String.format(pattern, number); } catch(IllegalFormatException ife){ throw new FunctionException(this, "Invalid \'pattern\' value") .initCause(ife); } return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); } };