code
stringlengths
67
466k
docstring
stringlengths
1
13.2k
private void writeObject(ObjectOutputStream out) throws IOException { super.write(out); out.writeUTF(mapredInputFormat.getClass().getName()); out.writeUTF(keyClass.getName()); out.writeUTF(valueClass.getName()); jobConf.write(out); }
--------------------------------------------------------------------------------------------
@Override public void accept(Visitor<PlanNode> visitor) { if (visitor.preVisit(this)) { this.input1.getSource().accept(visitor); this.input2.getSource().accept(visitor); for (Channel broadcastInput : getBroadcastInputs()) { broadcastInput.getSource().accept(visitor); } visitor.postVisit(this); } }
--------------------------------------------------------------------------------------------
@PublicEvolving public static <T> DynamicEventTimeSessionWindows<T> withDynamicGap(SessionWindowTimeGapExtractor<T> sessionWindowTimeGapExtractor) { return new DynamicEventTimeSessionWindows<>(sessionWindowTimeGapExtractor); }
Creates a new {@code SessionWindows} {@link WindowAssigner} that assigns elements to sessions based on the element timestamp. @param sessionWindowTimeGapExtractor The extractor to use to extract the time gap from the input elements @return The policy.
@Override public void snapshotState(StateSnapshotContext context) throws Exception { super.snapshotState(context); checkState(checkpointedState != null, "The operator state has not been properly initialized."); int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask(); checkpointedState.clear(); List<TimestampedFileInputSplit> readerState = reader.getReaderState(); try { for (TimestampedFileInputSplit split : readerState) { // create a new partition for each entry. checkpointedState.add(split); } } catch (Exception e) { checkpointedState.clear(); throw new Exception("Could not add timestamped file input splits to to operator " + "state backend of operator " + getOperatorName() + '.', e); } if (LOG.isDebugEnabled()) { LOG.debug("{} (taskIdx={}) checkpointed {} splits: {}.", getClass().getSimpleName(), subtaskIdx, readerState.size(), readerState); } }
--------------------- Checkpointing --------------------------
public static void checkParameter(boolean condition, @Nullable Object errorMessage) { if (!condition) { throw new ProgramParametrizationException(String.valueOf(errorMessage)); } }
Checks the given boolean condition, and throws an {@code ProgramParametrizationException} if the condition is not met (evaluates to {@code false}). The exception will have the given error message. @param condition The condition to check @param errorMessage The message for the {@code ProgramParametrizationException} that is thrown if the check fails. @throws ProgramParametrizationException Thrown, if the condition is violated. @see Preconditions#checkNotNull(Object, String)
public void setParallelism(int parallelism) { Preconditions.checkArgument( parallelism > 0 || parallelism == ExecutionConfig.PARALLELISM_DEFAULT, "The parallelism must be at least one, or ExecutionConfig.PARALLELISM_DEFAULT (use system default)."); this.parallelism = parallelism; }
Sets the parallelism of this {@code StreamTransformation}. @param parallelism The new parallelism to set on this {@code StreamTransformation}.
public void setMaxParallelism(int maxParallelism) { Preconditions.checkArgument(maxParallelism > 0 && maxParallelism <= StreamGraphGenerator.UPPER_BOUND_MAX_PARALLELISM, "Maximum parallelism must be between 1 and " + StreamGraphGenerator.UPPER_BOUND_MAX_PARALLELISM + ". Found: " + maxParallelism); this.maxParallelism = maxParallelism; }
Sets the maximum parallelism for this stream transformation. @param maxParallelism Maximum parallelism for this stream transformation.
public void setUidHash(String uidHash) { Preconditions.checkNotNull(uidHash); Preconditions.checkArgument(uidHash.matches("^[0-9A-Fa-f]{32}$"), "Node hash must be a 32 character String that describes a hex code. Found: " + uidHash); this.userProvidedNodeHash = uidHash; }
Sets an user provided hash for this operator. This will be used AS IS the create the JobVertexID. <p>The user provided hash is an alternative to the generated hashes, that is considered when identifying an operator through the default hash mechanics fails (e.g. because of changes between Flink versions). <p><strong>Important</strong>: this should be used as a workaround or for trouble shooting. The provided hash needs to be unique per transformation and job. Otherwise, job submission will fail. Furthermore, you cannot assign user-specified hash to intermediate nodes in an operator chain and trying so will let your job fail. <p>A use case for this is in migration between Flink versions or changing the jobs in a way that changes the automatically generated hashes. In this case, providing the previous hashes directly through this method (e.g. obtained from old logs) can help to reestablish a lost mapping from states to their target operator. @param uidHash The user provided hash for this operator. This will become the JobVertexID, which is shown in the logs and web ui.
public TypeInformation<T> getOutputType() { if (outputType instanceof MissingTypeInfo) { MissingTypeInfo typeInfo = (MissingTypeInfo) this.outputType; throw new InvalidTypesException( "The return type of function '" + typeInfo.getFunctionName() + "' could not be determined automatically, due to type erasure. " + "You can give type information hints by using the returns(...) " + "method on the result of the transformation call, or by letting " + "your function implement the 'ResultTypeQueryable' " + "interface.", typeInfo.getTypeException()); } typeUsed = true; return this.outputType; }
Returns the output type of this {@code StreamTransformation} as a {@link TypeInformation}. Once this is used once the output type cannot be changed anymore using {@link #setOutputType}. @return The output type of this {@code StreamTransformation}
public static String getUserCodeClassLoaderInfo(ClassLoader loader) { if (loader instanceof URLClassLoader) { URLClassLoader cl = (URLClassLoader) loader; try { StringBuilder bld = new StringBuilder(); if (cl == ClassLoader.getSystemClassLoader()) { bld.append("System ClassLoader: "); } else { bld.append("URL ClassLoader:"); } for (URL url : cl.getURLs()) { bld.append("\n "); if (url == null) { bld.append("(null)"); } else if ("file".equals(url.getProtocol())) { String filePath = url.getPath(); File fileFile = new File(filePath); bld.append("file: '").append(filePath).append('\''); if (fileFile.exists()) { if (fileFile.isDirectory()) { bld.append(" (directory)"); } else { JarFile jar = null; try { jar = new JarFile(filePath); bld.append(" (valid JAR)"); } catch (Exception e) { bld.append(" (invalid JAR: ").append(e.getMessage()).append(')'); } finally { if (jar != null) { jar.close(); } } } } else { bld.append(" (missing)"); } } else { bld.append("url: ").append(url); } } return bld.toString(); } catch (Throwable t) { return "Cannot access classloader info due to an exception.\n" + ExceptionUtils.stringifyException(t); } } else { return "No user code ClassLoader"; } }
Gets information about URL class loaders. The returned info string contains all URLs of the class loader. For file URLs, it contains in addition whether the referenced file exists, is a valid JAR file, or is a directory. <p>NOTE: This method makes a best effort to provide information about the classloader, and never throws an exception.</p> @param loader The classloader to get the info string for. @return The classloader information string.
public static boolean validateClassLoadable(ClassNotFoundException cnfe, ClassLoader cl) { try { String className = cnfe.getMessage(); Class.forName(className, false, cl); return true; } catch (ClassNotFoundException e) { return false; } catch (Exception e) { return false; } }
Checks, whether the class that was not found in the given exception, can be resolved through the given class loader. @param cnfe The ClassNotFoundException that defines the name of the class. @param cl The class loader to use for the class resolution. @return True, if the class can be resolved with the given class loader, false if not.
private Mapping extractProjectsAndMapping( Aggregate aggregate, RelNode input, RelBuilder relBuilder) { // Compute which input fields are used. final ImmutableBitSet.Builder inputFieldsUsed = getInputFieldUsed(aggregate, input); final List<RexNode> projects = new ArrayList<>(); final Mapping mapping = Mappings.create(MappingType.INVERSE_SURJECTION, aggregate.getInput().getRowType().getFieldCount(), inputFieldsUsed.cardinality()); int j = 0; for (int i : inputFieldsUsed.build()) { projects.add(relBuilder.field(i)); mapping.set(i, j++); } if (input instanceof Project) { // this will not create trivial projects relBuilder.project(projects); } else { relBuilder.project(projects, Collections.emptyList(), true); } return mapping; }
Extract projects from the Aggregate and return the index mapping between the new projects and it's input.
private ImmutableBitSet.Builder getInputFieldUsed(Aggregate aggregate, RelNode input) { // 1. group fields are always used final ImmutableBitSet.Builder inputFieldsUsed = aggregate.getGroupSet().rebuild(); // 2. agg functions for (AggregateCall aggCall : aggregate.getAggCallList()) { for (int i : aggCall.getArgList()) { inputFieldsUsed.set(i); } if (aggCall.filterArg >= 0) { inputFieldsUsed.set(aggCall.filterArg); } } // 3. window time field if the aggregate is a group window aggregate. if (aggregate instanceof LogicalWindowAggregate) { inputFieldsUsed.set(getWindowTimeFieldIndex((LogicalWindowAggregate) aggregate, input)); } return inputFieldsUsed; }
Compute which input fields are used by the aggregate.
public static long calculateCutoffMB(Configuration config, long containerMemoryMB) { Preconditions.checkArgument(containerMemoryMB > 0); // (1) check cutoff ratio final float memoryCutoffRatio = config.getFloat( ResourceManagerOptions.CONTAINERIZED_HEAP_CUTOFF_RATIO); if (memoryCutoffRatio >= 1 || memoryCutoffRatio <= 0) { throw new IllegalArgumentException("The configuration value '" + ResourceManagerOptions.CONTAINERIZED_HEAP_CUTOFF_RATIO.key() + "' must be between 0 and 1. Value given=" + memoryCutoffRatio); } // (2) check min cutoff value final int minCutoff = config.getInteger( ResourceManagerOptions.CONTAINERIZED_HEAP_CUTOFF_MIN); if (minCutoff >= containerMemoryMB) { throw new IllegalArgumentException("The configuration value '" + ResourceManagerOptions.CONTAINERIZED_HEAP_CUTOFF_MIN.key() + "'='" + minCutoff + "' is larger than the total container memory " + containerMemoryMB); } // (3) check between heap and off-heap long cutoff = (long) (containerMemoryMB * memoryCutoffRatio); if (cutoff < minCutoff) { cutoff = minCutoff; } return cutoff; }
Calcuate cutoff memory size used by container, it will throw an {@link IllegalArgumentException} if the config is invalid or return the cutoff value if valid. @param config The Flink configuration. @param containerMemoryMB The size of the complete container, in megabytes. @return cutoff memory size used by container.
public static ContaineredTaskManagerParameters create( Configuration config, long containerMemoryMB, int numSlots) { // (1) try to compute how much memory used by container final long cutoffMB = calculateCutoffMB(config, containerMemoryMB); // (2) split the remaining Java memory between heap and off-heap final long heapSizeMB = TaskManagerServices.calculateHeapSizeMB(containerMemoryMB - cutoffMB, config); // use the cut-off memory for off-heap (that was its intention) final long offHeapSizeMB = containerMemoryMB - heapSizeMB; // (3) obtain the additional environment variables from the configuration final HashMap<String, String> envVars = new HashMap<>(); final String prefix = ResourceManagerOptions.CONTAINERIZED_TASK_MANAGER_ENV_PREFIX; for (String key : config.keySet()) { if (key.startsWith(prefix) && key.length() > prefix.length()) { // remove prefix String envVarKey = key.substring(prefix.length()); envVars.put(envVarKey, config.getString(key, null)); } } // done return new ContaineredTaskManagerParameters( containerMemoryMB, heapSizeMB, offHeapSizeMB, numSlots, envVars); }
Computes the parameters to be used to start a TaskManager Java process. @param config The Flink configuration. @param containerMemoryMB The size of the complete container, in megabytes. @return The parameters to start the TaskManager processes with.
@Internal public static Set<Annotation> readSingleForwardAnnotations(Class<?> udfClass) { ForwardedFields forwardedFields = udfClass.getAnnotation(ForwardedFields.class); NonForwardedFields nonForwardedFields = udfClass.getAnnotation(NonForwardedFields.class); ReadFields readSet = udfClass.getAnnotation(ReadFields.class); Set<Annotation> annotations = new HashSet<Annotation>(); if (forwardedFields != null) { annotations.add(forwardedFields); } if (nonForwardedFields != null) { if (!annotations.isEmpty()) { throw new InvalidProgramException("Either " + ForwardedFields.class.getSimpleName() + " or " + NonForwardedFields.class.getSimpleName() + " can be annotated to a function, not both."); } annotations.add(nonForwardedFields); } if (readSet != null) { annotations.add(readSet); } return !annotations.isEmpty() ? annotations : null; }
Reads the annotations of a user defined function with one input and returns semantic properties according to the forwarded fields annotated. @param udfClass The user defined function, represented by its class. @return The DualInputSemanticProperties containing the forwarded fields.
@Internal public static Set<Annotation> readDualForwardAnnotations(Class<?> udfClass) { // get readSet annotation from stub ForwardedFieldsFirst forwardedFields1 = udfClass.getAnnotation(ForwardedFieldsFirst.class); ForwardedFieldsSecond forwardedFields2 = udfClass.getAnnotation(ForwardedFieldsSecond.class); // get readSet annotation from stub NonForwardedFieldsFirst nonForwardedFields1 = udfClass.getAnnotation(NonForwardedFieldsFirst.class); NonForwardedFieldsSecond nonForwardedFields2 = udfClass.getAnnotation(NonForwardedFieldsSecond.class); ReadFieldsFirst readSet1 = udfClass.getAnnotation(ReadFieldsFirst.class); ReadFieldsSecond readSet2 = udfClass.getAnnotation(ReadFieldsSecond.class); Set<Annotation> annotations = new HashSet<Annotation>(); if (nonForwardedFields1 != null && forwardedFields1 != null) { throw new InvalidProgramException("Either " + ForwardedFieldsFirst.class.getSimpleName() + " or " + NonForwardedFieldsFirst.class.getSimpleName() + " can be annotated to a function, not both."); } else if (forwardedFields1 != null) { annotations.add(forwardedFields1); } else if (nonForwardedFields1 != null) { annotations.add(nonForwardedFields1); } if (forwardedFields2 != null && nonForwardedFields2 != null) { throw new InvalidProgramException("Either " + ForwardedFieldsSecond.class.getSimpleName() + " or " + NonForwardedFieldsSecond.class.getSimpleName() + " can be annotated to a function, not both."); } else if (forwardedFields2 != null) { annotations.add(forwardedFields2); } else if (nonForwardedFields2 != null) { annotations.add(nonForwardedFields2); } if (readSet1 != null) { annotations.add(readSet1); } if (readSet2 != null) { annotations.add(readSet2); } return !annotations.isEmpty() ? annotations : null; }
Reads the annotations of a user defined function with two inputs and returns semantic properties according to the forwarded fields annotated. @param udfClass The user defined function, represented by its class. @return The DualInputSemanticProperties containing the forwarded fields.
public void shutdown() throws Exception { Throwable firstException = null; try { scheduledExecutorService.shutdownNow(); } catch (Throwable t) { firstException = t; } libraryCacheManager.shutdown(); stackTraceSampleCoordinator.shutDown(); backPressureStatsTracker.shutDown(); if (firstException != null) { ExceptionUtils.rethrowException(firstException, "Error while shutting down JobManager services"); } }
Shutdown the {@link JobMaster} services. <p>This method makes sure all services are closed or shut down, even when an exception occurred in the shutdown of one component. The first encountered exception is thrown, with successive exceptions added as suppressed exceptions. @throws Exception The first Exception encountered during shutdown.
public static JobManagerSharedServices fromConfiguration( Configuration config, BlobServer blobServer) throws Exception { checkNotNull(config); checkNotNull(blobServer); final String classLoaderResolveOrder = config.getString(CoreOptions.CLASSLOADER_RESOLVE_ORDER); final String[] alwaysParentFirstLoaderPatterns = CoreOptions.getParentFirstLoaderPatterns(config); final BlobLibraryCacheManager libraryCacheManager = new BlobLibraryCacheManager( blobServer, FlinkUserCodeClassLoaders.ResolveOrder.fromString(classLoaderResolveOrder), alwaysParentFirstLoaderPatterns); final FiniteDuration timeout; try { timeout = AkkaUtils.getTimeout(config); } catch (NumberFormatException e) { throw new IllegalConfigurationException(AkkaUtils.formatDurationParsingErrorMessage()); } final ScheduledExecutorService futureExecutor = Executors.newScheduledThreadPool( Hardware.getNumberCPUCores(), new ExecutorThreadFactory("jobmanager-future")); final StackTraceSampleCoordinator stackTraceSampleCoordinator = new StackTraceSampleCoordinator(futureExecutor, timeout.toMillis()); final int cleanUpInterval = config.getInteger(WebOptions.BACKPRESSURE_CLEANUP_INTERVAL); final BackPressureStatsTrackerImpl backPressureStatsTracker = new BackPressureStatsTrackerImpl( stackTraceSampleCoordinator, cleanUpInterval, config.getInteger(WebOptions.BACKPRESSURE_NUM_SAMPLES), config.getInteger(WebOptions.BACKPRESSURE_REFRESH_INTERVAL), Time.milliseconds(config.getInteger(WebOptions.BACKPRESSURE_DELAY))); futureExecutor.scheduleWithFixedDelay( backPressureStatsTracker::cleanUpOperatorStatsCache, cleanUpInterval, cleanUpInterval, TimeUnit.MILLISECONDS); return new JobManagerSharedServices( futureExecutor, libraryCacheManager, RestartStrategyFactory.createRestartStrategyFactory(config), stackTraceSampleCoordinator, backPressureStatsTracker, blobServer); }
------------------------------------------------------------------------
public static BinaryString fromAddress( MemorySegment[] segments, int offset, int numBytes) { return new BinaryString(segments, offset, numBytes); }
Creates an BinaryString from given address (base and offset) and length.
public static BinaryString blankString(int length) { byte[] spaces = new byte[length]; Arrays.fill(spaces, (byte) ' '); return fromBytes(spaces); }
Creates an BinaryString that contains `length` spaces.
public int numChars() { ensureMaterialized(); if (inFirstSegment()) { int len = 0; for (int i = 0; i < sizeInBytes; i += numBytesForFirstByte(getByteOneSegment(i))) { len++; } return len; } else { return numCharsSlow(); } }
Returns the number of code points in it.
@Override public int compareTo(BinaryString other) { if (javaObject != null && other.javaObject != null) { return javaObject.compareTo(other.javaObject); } ensureMaterialized(); other.ensureMaterialized(); if (segments.length == 1 && other.segments.length == 1) { int len = Math.min(sizeInBytes, other.sizeInBytes); MemorySegment seg1 = segments[0]; MemorySegment seg2 = other.segments[0]; for (int i = 0; i < len; i++) { int res = (seg1.get(offset + i) & 0xFF) - (seg2.get(other.offset + i) & 0xFF); if (res != 0) { return res; } } return sizeInBytes - other.sizeInBytes; } // if there are multi segments. return compareMultiSegments(other); }
UTF-8 supports bytes comparison.
private int compareMultiSegments(BinaryString other) { if (sizeInBytes == 0 || other.sizeInBytes == 0) { return sizeInBytes - other.sizeInBytes; } int len = Math.min(sizeInBytes, other.sizeInBytes); MemorySegment seg1 = segments[0]; MemorySegment seg2 = other.segments[0]; int segmentSize = segments[0].size(); int otherSegmentSize = other.segments[0].size(); int sizeOfFirst1 = segmentSize - offset; int sizeOfFirst2 = otherSegmentSize - other.offset; int varSegIndex1 = 1; int varSegIndex2 = 1; // find the first segment of this string. while (sizeOfFirst1 <= 0) { sizeOfFirst1 += segmentSize; seg1 = segments[varSegIndex1++]; } while (sizeOfFirst2 <= 0) { sizeOfFirst2 += otherSegmentSize; seg2 = other.segments[varSegIndex2++]; } int offset1 = segmentSize - sizeOfFirst1; int offset2 = otherSegmentSize - sizeOfFirst2; int needCompare = Math.min(Math.min(sizeOfFirst1, sizeOfFirst2), len); while (needCompare > 0) { // compare in one segment. for (int i = 0; i < needCompare; i++) { int res = (seg1.get(offset1 + i) & 0xFF) - (seg2.get(offset2 + i) & 0xFF); if (res != 0) { return res; } } if (needCompare == len) { break; } len -= needCompare; // next segment if (sizeOfFirst1 < sizeOfFirst2) { //I am smaller seg1 = segments[varSegIndex1++]; offset1 = 0; offset2 += needCompare; sizeOfFirst1 = segmentSize; sizeOfFirst2 -= needCompare; } else if (sizeOfFirst1 > sizeOfFirst2) { //other is smaller seg2 = other.segments[varSegIndex2++]; offset2 = 0; offset1 += needCompare; sizeOfFirst2 = otherSegmentSize; sizeOfFirst1 -= needCompare; } else { // same, should go ahead both. seg1 = segments[varSegIndex1++]; seg2 = other.segments[varSegIndex2++]; offset1 = 0; offset2 = 0; sizeOfFirst1 = segmentSize; sizeOfFirst2 = otherSegmentSize; } needCompare = Math.min(Math.min(sizeOfFirst1, sizeOfFirst2), len); } checkArgument(needCompare == len); return sizeInBytes - other.sizeInBytes; }
Find the boundaries of segments, and then compare MemorySegment.
public BinaryString substring(final int start, final int until) { ensureMaterialized(); if (until <= start || start >= sizeInBytes) { return EMPTY_UTF8; } if (inFirstSegment()) { MemorySegment segment = segments[0]; int i = 0; int c = 0; while (i < sizeInBytes && c < start) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } int j = i; while (i < sizeInBytes && c < until) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; segment.get(offset + j, bytes, 0, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } } else { return substringSlow(start, until); } }
Returns a substring of this. @param start the position of first code point @param until the position after last code point, exclusive.
public static BinaryString concat(Iterable<BinaryString> inputs) { // Compute the total length of the result. int totalLength = 0; for (BinaryString input : inputs) { if (input != null) { input.ensureMaterialized(); totalLength += input.getSizeInBytes(); } } // Allocate a new byte array, and copy the inputs one by one into it. final byte[] result = new byte[totalLength]; int offset = 0; for (BinaryString input : inputs) { if (input != null) { int len = input.sizeInBytes; SegmentsUtil.copyToBytes(input.segments, input.offset, result, offset, len); offset += len; } } return fromBytes(result); }
Concatenates input strings together into a single string.
public static BinaryString concatWs(BinaryString separator, BinaryString... inputs) { return concatWs(separator, Arrays.asList(inputs)); }
Concatenates input strings together into a single string using the separator. A null input is skipped. For example, concat(",", "a", null, "c") would yield "a,c".
public static BinaryString concatWs(BinaryString separator, Iterable<BinaryString> inputs) { if (null == separator || EMPTY_UTF8.equals(separator)) { return concat(inputs); } separator.ensureMaterialized(); int numInputBytes = 0; // total number of bytes from the inputs int numInputs = 0; // number of non-null inputs for (BinaryString input : inputs) { if (input != null) { input.ensureMaterialized(); numInputBytes += input.sizeInBytes; numInputs++; } } if (numInputs == 0) { // Return an empty string if there is no input, or all the inputs are null. return EMPTY_UTF8; } // Allocate a new byte array, and copy the inputs one by one into it. // The size of the new array is the size of all inputs, plus the separators. final byte[] result = new byte[numInputBytes + (numInputs - 1) * separator.sizeInBytes]; int offset = 0; int j = 0; for (BinaryString input : inputs) { if (input != null) { int len = input.sizeInBytes; SegmentsUtil.copyToBytes(input.segments, input.offset, result, offset, len); offset += len; j++; // Add separator if this is not the last input. if (j < numInputs) { SegmentsUtil.copyToBytes(separator.segments, separator.offset, result, offset, separator.sizeInBytes); offset += separator.sizeInBytes; } } } return fromBytes(result); }
Concatenates input strings together into a single string using the separator. A null input is skipped. For example, concat(",", "a", null, "c") would yield "a,c".
public boolean contains(final BinaryString substring) { ensureMaterialized(); substring.ensureMaterialized(); if (substring.sizeInBytes == 0) { return true; } int find = SegmentsUtil.find( segments, offset, sizeInBytes, substring.segments, substring.offset, substring.sizeInBytes); return find != -1; }
Returns whether this contains `substring` or not. Same to like '%substring%'.
public boolean endsWith(final BinaryString suffix) { ensureMaterialized(); suffix.ensureMaterialized(); return matchAt(suffix, sizeInBytes - suffix.sizeInBytes); }
Same to like '%suffix'.
public BinaryString trim(BinaryString trimStr) { if (trimStr == null) { return null; } return trimLeft(trimStr).trimRight(trimStr); }
Walk each character of current string from both ends, remove the character if it is in trim string. Return the new substring which both ends trim characters have been removed. @param trimStr the trim string @return A subString which both ends trim characters have been removed.
public BinaryString trimLeft(BinaryString trimStr) { ensureMaterialized(); if (trimStr == null) { return null; } trimStr.ensureMaterialized(); if (trimStr.isSpaceString()) { return trimLeft(); } if (inFirstSegment()) { int searchIdx = 0; while (searchIdx < this.sizeInBytes) { int charBytes = numBytesForFirstByte(getByteOneSegment(searchIdx)); BinaryString currentChar = copyBinaryStringInOneSeg(searchIdx, searchIdx + charBytes - 1); // try to find the matching for the character in the trimString characters. if (trimStr.contains(currentChar)) { searchIdx += charBytes; } else { break; } } // empty string if (searchIdx >= sizeInBytes) { return EMPTY_UTF8; } else { return copyBinaryStringInOneSeg(searchIdx, sizeInBytes - 1); } } else { return trimLeftSlow(trimStr); } }
Walk each character of current string from left end, remove the character if it is in trim string. Stops at the first character which is not in trim string. Return the new substring. @param trimStr the trim string @return A subString which removes all of the character from the left side that is in trim string.
public BinaryString trimRight(BinaryString trimStr) { ensureMaterialized(); if (trimStr == null) { return null; } trimStr.ensureMaterialized(); if (trimStr.isSpaceString()) { return trimRight(); } if (inFirstSegment()) { int charIdx = 0; int byteIdx = 0; // each element in charLens is length of character in the source string int[] charLens = new int[sizeInBytes]; // each element in charStartPos is start position of first byte in the source string int[] charStartPos = new int[sizeInBytes]; while (byteIdx < sizeInBytes) { charStartPos[charIdx] = byteIdx; charLens[charIdx] = numBytesForFirstByte(getByteOneSegment(byteIdx)); byteIdx += charLens[charIdx]; charIdx++; } // searchIdx points to the first character which is not in trim string from the right // end. int searchIdx = sizeInBytes - 1; charIdx -= 1; while (charIdx >= 0) { BinaryString currentChar = copyBinaryStringInOneSeg( charStartPos[charIdx], charStartPos[charIdx] + charLens[charIdx] - 1); if (trimStr.contains(currentChar)) { searchIdx -= charLens[charIdx]; } else { break; } charIdx--; } if (searchIdx < 0) { // empty string return EMPTY_UTF8; } else { return copyBinaryStringInOneSeg(0, searchIdx); } } else { return trimRightSlow(trimStr); } }
Walk each character of current string from right end, remove the character if it is in trim string. Stops at the first character which is not in trim string. Return the new substring. @param trimStr the trim string @return A subString which removes all of the character from the right side that is in trim string.
public BinaryString keyValue(byte split1, byte split2, BinaryString keyName) { ensureMaterialized(); if (keyName == null || keyName.getSizeInBytes() == 0) { return null; } if (inFirstSegment() && keyName.inFirstSegment()) { // position in byte int byteIdx = 0; // position of last split1 int lastSplit1Idx = -1; while (byteIdx < sizeInBytes) { // If find next split1 in str, process current kv if (segments[0].get(offset + byteIdx) == split1) { int currentKeyIdx = lastSplit1Idx + 1; // If key of current kv is keyName, return the value directly BinaryString value = findValueOfKey(split2, keyName, currentKeyIdx, byteIdx); if (value != null) { return value; } lastSplit1Idx = byteIdx; } byteIdx++; } // process the string which is not ends with split1 int currentKeyIdx = lastSplit1Idx + 1; return findValueOfKey(split2, keyName, currentKeyIdx, sizeInBytes); } else { return keyValueSlow(split1, split2, keyName); } }
Parse target string as key-value string and return the value matches key name. If accept any null arguments, return null. example: keyvalue('k1=v1;k2=v2', ';', '=', 'k2') = 'v2' keyvalue('k1:v1,k2:v2', ',', ':', 'k3') = NULL @param split1 separator between key-value tuple. @param split2 separator between key and value. @param keyName name of the key whose value you want return. @return target value.
public int indexOf(BinaryString subStr, int start) { ensureMaterialized(); subStr.ensureMaterialized(); if (subStr.sizeInBytes == 0) { return 0; } if (inFirstSegment()) { // position in byte int byteIdx = 0; // position is char int charIdx = 0; while (byteIdx < sizeInBytes && charIdx < start) { byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } do { if (byteIdx + subStr.sizeInBytes > sizeInBytes) { return -1; } if (SegmentsUtil.equals(segments, offset + byteIdx, subStr.segments, subStr.offset, subStr.sizeInBytes)) { return charIdx; } byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } while (byteIdx < sizeInBytes); return -1; } else { return indexOfSlow(subStr, start); } }
Returns the position of the first occurence of substr in current string starting from given position. @param subStr subStr to be searched @param start start position @return the position of the first occurence of substring. Return -1 if not found.
public BinaryString reverse() { ensureMaterialized(); if (inFirstSegment()) { byte[] result = new byte[this.sizeInBytes]; // position in byte int byteIdx = 0; while (byteIdx < sizeInBytes) { int charBytes = numBytesForFirstByte(getByteOneSegment(byteIdx)); segments[0].get( offset + byteIdx, result, result.length - byteIdx - charBytes, charBytes); byteIdx += charBytes; } return BinaryString.fromBytes(result); } else { return reverseSlow(); } }
Reverse each character in current string. @return a new string which character order is reverse to current string.
private SegmentAndOffset firstSegmentAndOffset(int segSize) { int segIndex = offset / segSize; return new SegmentAndOffset(segIndex, offset % segSize); }
TODO upper/lower is slow?..
public Long toLong() { ensureMaterialized(); if (sizeInBytes == 0) { return null; } int size = segments[0].size(); SegmentAndOffset segmentAndOffset = startSegmentAndOffset(size); int totalOffset = 0; byte b = segmentAndOffset.value(); final boolean negative = b == '-'; if (negative || b == '+') { segmentAndOffset.nextByte(size); totalOffset++; if (sizeInBytes == 1) { return null; } } long result = 0; final byte separator = '.'; final int radix = 10; final long stopValue = Long.MIN_VALUE / radix; while (totalOffset < this.sizeInBytes) { b = segmentAndOffset.value(); totalOffset++; segmentAndOffset.nextByte(size); if (b == separator) { // We allow decimals and will return a truncated integral in that case. // Therefore we won't throw an exception here (checking the fractional // part happens below.) break; } int digit; if (b >= '0' && b <= '9') { digit = b - '0'; } else { return null; } // We are going to process the new digit and accumulate the result. However, before // doing this, if the result is already smaller than the // stopValue(Long.MIN_VALUE / radix), then result * 10 will definitely be smaller // than minValue, and we can stop. if (result < stopValue) { return null; } result = result * radix - digit; // Since the previous result is less than or equal to // stopValue(Long.MIN_VALUE / radix), we can just use `result > 0` to check overflow. // If result overflows, we should stop. if (result > 0) { return null; } } // This is the case when we've encountered a decimal separator. The fractional // part will not change the number, but we will verify that the fractional part // is well formed. while (totalOffset < sizeInBytes) { byte currentByte = segmentAndOffset.value(); if (currentByte < '0' || currentByte > '9') { return null; } totalOffset++; segmentAndOffset.nextByte(size); } if (!negative) { result = -result; if (result < 0) { return null; } } return result; }
Parses this BinaryString to Long. <p>Note that, in this method we accumulate the result in negative format, and convert it to positive format at the end, if this string is not started with '-'. This is because min value is bigger than max value in digits, e.g. Long.MAX_VALUE is '9223372036854775807' and Long.MIN_VALUE is '-9223372036854775808'. <p>This code is mostly copied from LazyLong.parseLong in Hive. @return Long value if the parsing was successful else null.
public Decimal toDecimal(int precision, int scale) { ensureMaterialized(); if (precision > Decimal.MAX_LONG_DIGITS || this.sizeInBytes > Decimal.MAX_LONG_DIGITS) { return toDecimalSlow(precision, scale); } // Data in Decimal is stored by one long value if `precision` <= Decimal.MAX_LONG_DIGITS. // In this case we can directly extract the value from memory segment. int size = getSegments()[0].size(); SegmentAndOffset segmentAndOffset = startSegmentAndOffset(size); int totalOffset = 0; // Remove white spaces at the beginning byte b = 0; while (totalOffset < this.sizeInBytes) { b = segmentAndOffset.value(); if (b != ' ' && b != '\n' && b != '\t') { break; } totalOffset++; segmentAndOffset.nextByte(size); } if (totalOffset == this.sizeInBytes) { // all whitespaces return null; } // ======= Significand part begin ======= final boolean negative = b == '-'; if (negative || b == '+') { segmentAndOffset.nextByte(size); totalOffset++; if (totalOffset == this.sizeInBytes) { // only contains prefix plus/minus return null; } } long significand = 0; int exp = 0; int significandLen = 0, pointPos = -1; while (totalOffset < this.sizeInBytes) { b = segmentAndOffset.value(); totalOffset++; segmentAndOffset.nextByte(size); if (b >= '0' && b <= '9') { // No need to worry about overflow, because this.sizeInBytes <= Decimal.MAX_LONG_DIGITS significand = significand * 10 + (b - '0'); significandLen++; } else if (b == '.') { if (pointPos >= 0) { // More than one decimal point return null; } pointPos = significandLen; } else { break; } } if (pointPos < 0) { pointPos = significandLen; } if (negative) { significand = -significand; } // ======= Significand part end ======= // ======= Exponential part begin ======= if ((b == 'e' || b == 'E') && totalOffset < this.sizeInBytes) { b = segmentAndOffset.value(); final boolean expNegative = b == '-'; if (expNegative || b == '+') { segmentAndOffset.nextByte(size); totalOffset++; if (totalOffset == this.sizeInBytes) { return null; } } int expDigits = 0; // As `precision` <= 18, value absolute range is limited to 10^-18 ~ 10^18. // The worst case is <18-digits>E-36 final int expStopValue = 40; while (totalOffset < this.sizeInBytes) { b = segmentAndOffset.value(); totalOffset++; segmentAndOffset.nextByte(size); if (b >= '0' && b <= '9') { // No need to worry about larger exponents, // because they will produce overflow or underflow if (expDigits < expStopValue) { expDigits = expDigits * 10 + (b - '0'); } } else { break; } } if (expNegative) { expDigits = -expDigits; } exp += expDigits; } exp -= significandLen - pointPos; // ======= Exponential part end ======= // Check for invalid character at the end while (totalOffset < this.sizeInBytes) { b = segmentAndOffset.value(); totalOffset++; segmentAndOffset.nextByte(size); // White spaces are allowed at the end if (b != ' ' && b != '\n' && b != '\t') { return null; } } // Round exp to scale int change = exp + scale; if (significandLen + change > precision) { // Overflow return null; } if (change >= 0) { significand *= Decimal.POW10[change]; } else { int k = negative ? -5 : 5; significand = (significand + k * Decimal.POW10[-change - 1]) / Decimal.POW10[-change]; } return Decimal.fromLong(significand, precision, scale); }
Parses this BinaryString to Decimal. @return Decimal value if the parsing was successful, or null if overflow @throws NumberFormatException if the parsing failed.
public BinaryString toUpperCase() { if (javaObject != null) { return toUpperCaseSlow(); } if (sizeInBytes == 0) { return EMPTY_UTF8; } int size = segments[0].size(); SegmentAndOffset segmentAndOffset = startSegmentAndOffset(size); byte[] bytes = new byte[sizeInBytes]; bytes[0] = (byte) Character.toTitleCase(segmentAndOffset.value()); for (int i = 0; i < sizeInBytes; i++) { byte b = segmentAndOffset.value(); if (numBytesForFirstByte(b) != 1) { // fallback return toUpperCaseSlow(); } int upper = Character.toUpperCase((int) b); if (upper > 127) { // fallback return toUpperCaseSlow(); } bytes[i] = (byte) upper; segmentAndOffset.nextByte(size); } return fromBytes(bytes); }
Returns the upper case of this string.
public BinaryString toLowerCase() { if (javaObject != null) { return toLowerCaseSlow(); } if (sizeInBytes == 0) { return EMPTY_UTF8; } int size = segments[0].size(); SegmentAndOffset segmentAndOffset = startSegmentAndOffset(size); byte[] bytes = new byte[sizeInBytes]; bytes[0] = (byte) Character.toTitleCase(segmentAndOffset.value()); for (int i = 0; i < sizeInBytes; i++) { byte b = segmentAndOffset.value(); if (numBytesForFirstByte(b) != 1) { // fallback return toLowerCaseSlow(); } int lower = Character.toLowerCase((int) b); if (lower > 127) { // fallback return toLowerCaseSlow(); } bytes[i] = (byte) lower; segmentAndOffset.nextByte(size); } return fromBytes(bytes); }
Returns the lower case of this string.
public BinaryString[] splitByWholeSeparatorPreserveAllTokens(BinaryString separator) { ensureMaterialized(); final int len = sizeInBytes; if (len == 0) { return EMPTY_STRING_ARRAY; } if (separator == null || EMPTY_UTF8.equals(separator)) { // Split on whitespace. return splitByWholeSeparatorPreserveAllTokens(fromString(" ")); } separator.ensureMaterialized(); final int separatorLength = separator.sizeInBytes; final ArrayList<BinaryString> substrings = new ArrayList<>(); int beg = 0; int end = 0; while (end < len) { end = SegmentsUtil.find( segments, offset + beg, sizeInBytes - beg, separator.segments, separator.offset, separator.sizeInBytes) - offset; if (end > -1) { if (end > beg) { // The following is OK, because String.substring( beg, end ) excludes // the character at the position 'end'. substrings.add(BinaryString.fromAddress(segments, offset + beg, end - beg)); // Set the starting point for the next search. // The following is equivalent to beg = end + (separatorLength - 1) + 1, // which is the right calculation: beg = end + separatorLength; } else { // We found a consecutive occurrence of the separator. substrings.add(EMPTY_UTF8); beg = end + separatorLength; } } else { // String.substring( beg ) goes from 'beg' to the end of the String. substrings.add(BinaryString.fromAddress(segments, offset + beg, sizeInBytes - beg)); end = len; } } return substrings.toArray(new BinaryString[0]); }
<p>Splits the provided text into an array, separator string specified. </p> <p>The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens.</p> <p>A {@code null} separator splits on whitespace.</p> <pre> "".splitByWholeSeparatorPreserveAllTokens(*) = [] "ab de fg".splitByWholeSeparatorPreserveAllTokens(null) = ["ab", "de", "fg"] "ab de fg".splitByWholeSeparatorPreserveAllTokens(null) = ["ab", "", "", "de", "fg"] "ab:cd:ef".splitByWholeSeparatorPreserveAllTokens(":") = ["ab", "cd", "ef"] "ab-!-cd-!-ef".splitByWholeSeparatorPreserveAllTokens("-!-") = ["ab", "cd", "ef"] </pre> <p>Note: return BinaryStrings is reuse MemorySegments from this.</p> @param separator String containing the String to be used as a delimiter, {@code null} splits on whitespace @return an array of parsed Strings, {@code null} if null String was input @since 2.4
public BinaryString hash(MessageDigest md) { String str = EncodingUtils.hex(md.digest(getBytes())); return fromString(str); }
Calculate the hash value of a given string use {@link MessageDigest}.
public Boolean toBooleanSQL() { if (TRUE_STRINGS.contains(toLowerCase())) { return true; } else if (FALSE_STRINGS.contains(toLowerCase())) { return false; } else { return null; } }
Decide boolean representation of a string.
static <IN, OUT> FlatSelectBuilder<IN, OUT> fromFlatSelect(final PatternFlatSelectFunction<IN, OUT> function) { return new FlatSelectBuilder<>(function); }
Starts constructing a {@link PatternProcessFunction} from a {@link PatternFlatSelectFunction} that emitted elements through {@link org.apache.flink.util.Collector}.
static <IN, OUT> SelectBuilder<IN, OUT> fromSelect(final PatternSelectFunction<IN, OUT> function) { return new SelectBuilder<>(function); }
Starts constructing a {@link PatternProcessFunction} from a {@link PatternSelectFunction} that emitted elements through return value.
public SingleOutputStreamOperator<T> setParallelism(int parallelism) { Preconditions.checkArgument(canBeParallel() || parallelism == 1, "The parallelism of non parallel operator must be 1."); transformation.setParallelism(parallelism); return this; }
Sets the parallelism for this operator. @param parallelism The parallelism for this operator. @return The operator with set parallelism.
@PublicEvolving public SingleOutputStreamOperator<T> setMaxParallelism(int maxParallelism) { Preconditions.checkArgument(maxParallelism > 0, "The maximum parallelism must be greater than 0."); Preconditions.checkArgument(canBeParallel() || maxParallelism == 1, "The maximum parallelism of non parallel operator must be 1."); transformation.setMaxParallelism(maxParallelism); return this; }
Sets the maximum parallelism of this operator. <p>The maximum parallelism specifies the upper bound for dynamic scaling. It also defines the number of key groups used for partitioned state. @param maxParallelism Maximum parallelism @return The operator with set maximum parallelism
private SingleOutputStreamOperator<T> setResources(ResourceSpec resources) { Preconditions.checkNotNull(resources, "The resources must be not null."); Preconditions.checkArgument(resources.isValid(), "The values in resources must be not less than 0."); transformation.setResources(resources, resources); return this; }
Sets the resources for this operator, the minimum and preferred resources are the same by default. @param resources The resources for this operator. @return The operator with set minimum and preferred resources.
@PublicEvolving public SingleOutputStreamOperator<T> forceNonParallel() { transformation.setParallelism(1); transformation.setMaxParallelism(1); nonParallel = true; return this; }
Sets the parallelism and maximum parallelism of this operator to one. And mark this operator cannot set a non-1 degree of parallelism. @return The operator with only one parallelism.
public SingleOutputStreamOperator<T> returns(Class<T> typeClass) { requireNonNull(typeClass, "type class must not be null."); try { return returns(TypeInformation.of(typeClass)); } catch (InvalidTypesException e) { throw new InvalidTypesException("Cannot infer the type information from the class alone." + "This is most likely because the class represents a generic type. In that case," + "please use the 'returns(TypeHint)' method instead."); } }
Adds a type information hint about the return type of this operator. This method can be used in cases where Flink cannot determine automatically what the produced type of a function is. That can be the case if the function uses generic type variables in the return type that cannot be inferred from the input type. <p>Classes can be used as type hints for non-generic types (classes without generic parameters), but not for generic types like for example Tuples. For those generic types, please use the {@link #returns(TypeHint)} method. @param typeClass The class of the returned data type. @return This operator with the type information corresponding to the given type class.
public SingleOutputStreamOperator<T> returns(TypeHint<T> typeHint) { requireNonNull(typeHint, "TypeHint must not be null"); try { return returns(TypeInformation.of(typeHint)); } catch (InvalidTypesException e) { throw new InvalidTypesException("Cannot infer the type information from the type hint. " + "Make sure that the TypeHint does not use any generic type variables."); } }
Adds a type information hint about the return type of this operator. This method can be used in cases where Flink cannot determine automatically what the produced type of a function is. That can be the case if the function uses generic type variables in the return type that cannot be inferred from the input type. <p>Use this method the following way: <pre>{@code DataStream<Tuple2<String, Double>> result = stream.flatMap(new FunctionWithNonInferrableReturnType()) .returns(new TypeHint<Tuple2<String, Double>>(){}); }</pre> @param typeHint The type hint for the returned data type. @return This operator with the type information corresponding to the given type hint.
public SingleOutputStreamOperator<T> returns(TypeInformation<T> typeInfo) { requireNonNull(typeInfo, "TypeInformation must not be null"); transformation.setOutputType(typeInfo); return this; }
Adds a type information hint about the return type of this operator. This method can be used in cases where Flink cannot determine automatically what the produced type of a function is. That can be the case if the function uses generic type variables in the return type that cannot be inferred from the input type. <p>In most cases, the methods {@link #returns(Class)} and {@link #returns(TypeHint)} are preferable. @param typeInfo type information as a return type hint @return This operator with a given return type hint.
public <X> DataStream<X> getSideOutput(OutputTag<X> sideOutputTag) { if (wasSplitApplied) { throw new UnsupportedOperationException("getSideOutput() and split() may not be called on the same DataStream. " + "As a work-around, please add a no-op map function before the split() call."); } sideOutputTag = clean(requireNonNull(sideOutputTag)); // make a defensive copy sideOutputTag = new OutputTag<X>(sideOutputTag.getId(), sideOutputTag.getTypeInfo()); TypeInformation<?> type = requestedSideOutputs.get(sideOutputTag); if (type != null && !type.equals(sideOutputTag.getTypeInfo())) { throw new UnsupportedOperationException("A side output with a matching id was " + "already requested with a different type. This is not allowed, side output " + "ids need to be unique."); } requestedSideOutputs.put(sideOutputTag, sideOutputTag.getTypeInfo()); SideOutputTransformation<X> sideOutputTransformation = new SideOutputTransformation<>(this.getTransformation(), sideOutputTag); return new DataStream<>(this.getExecutionEnvironment(), sideOutputTransformation); }
Gets the {@link DataStream} that contains the elements that are emitted from an operation into the side output with the given {@link OutputTag}. @see org.apache.flink.streaming.api.functions.ProcessFunction.Context#output(OutputTag, Object)
private GenericRecord convertRowToAvroRecord(Schema schema, Row row) { final List<Schema.Field> fields = schema.getFields(); final int length = fields.size(); final GenericRecord record = new GenericData.Record(schema); for (int i = 0; i < length; i++) { final Schema.Field field = fields.get(i); record.put(i, convertFlinkType(field.schema(), row.getField(i))); } return record; }
--------------------------------------------------------------------------------------------
private ObjectNode convertRow(ObjectNode reuse, RowTypeInfo info, Row row) { if (reuse == null) { reuse = mapper.createObjectNode(); } final String[] fieldNames = info.getFieldNames(); final TypeInformation<?>[] fieldTypes = info.getFieldTypes(); // validate the row if (row.getArity() != fieldNames.length) { throw new IllegalStateException(String.format( "Number of elements in the row '%s' is different from number of field names: %d", row, fieldNames.length)); } for (int i = 0; i < fieldNames.length; i++) { final String name = fieldNames[i]; final JsonNode fieldConverted = convert(reuse, reuse.get(name), fieldTypes[i], row.getField(i)); reuse.set(name, fieldConverted); } return reuse; }
--------------------------------------------------------------------------------------------
int getNumOccupiedMemorySegments() { // either the number of memory segments, or one for spilling final int numPartitionBuffers = this.partitionBuffers != null ? this.partitionBuffers.length : this.buildSideWriteBuffer.getNumOccupiedMemorySegments(); return numPartitionBuffers + bucketArea.buckets.length + bucketArea.numOverflowSegments; }
Gets the number of memory segments used by this partition, which includes build side memory buffers and overflow memory segments. @return The number of occupied memory segments.
final int insertIntoBuildBuffer(BinaryRow record) throws IOException { this.buildSideRecordCounter++; if (isInMemory()) { final long pointer = this.buildSideWriteBuffer.getCurrentPointer(); int skip = this.buildSideSerializer.serializeToPages(record, this.buildSideWriteBuffer); if (isInMemory()) { long ret = pointer + skip; if (ret > Integer.MAX_VALUE) { throw new RuntimeException("Too more data in this partition: " + ret); } return (int) ret; } else { return -1; } } else { this.buildSideSerializer.serializeToPages(record, this.buildSideWriteBuffer); return -1; } }
Inserts the given object into the current buffer. This method returns a pointer that can be used to address the written record in this partition, if it is in-memory. The returned pointers have no expressiveness in the case where the partition is spilled. @param record The object to be written to the partition. @return A pointer to the object in the partition, or <code>-1</code>, if the partition is spilled. @throws IOException Thrown, when this is a spilled partition and the write failed.
int finalizeBuildPhase(IOManager ioAccess, FileIOChannel.Enumerator probeChannelEnumerator) throws IOException { this.finalBufferLimit = this.buildSideWriteBuffer.getCurrentPositionInSegment(); this.partitionBuffers = this.buildSideWriteBuffer.close(); if (!isInMemory()) { // close the channel. this.buildSideChannel.close(); this.probeSideBuffer = FileChannelUtil.createOutputView( ioAccess, probeChannelEnumerator.next(), compressionEnable, compressionCodecFactory, compressionBlockSize, memorySegmentSize); return 1; } else { return 0; } }
After build phase. @return build spill return buffer, if have spilled, it returns the current write buffer, because it was used all the time in build phase, so it can only be returned at this time.
public static void main(String[] args) throws Exception { final ParameterTool params = ParameterTool.fromArgs(args); // set up the execution environment final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // get input data DataStream<String> text; if (params.has("input")) { // read the text file from given input path text = env.readTextFile(params.get("input")); } else { System.out.println("Executing WindowWordCount example with default input data set."); System.out.println("Use --input to specify file input."); // get default test text data text = env.fromElements(WordCountData.WORDS); } // make parameters available in the web interface env.getConfig().setGlobalJobParameters(params); final int windowSize = params.getInt("window", 10); final int slideSize = params.getInt("slide", 5); DataStream<Tuple2<String, Integer>> counts = // split up the lines in pairs (2-tuples) containing: (word,1) text.flatMap(new WordCount.Tokenizer()) // create windows of windowSize records slided every slideSize records .keyBy(0) .countWindow(windowSize, slideSize) // group by the tuple field "0" and sum up tuple field "1" .sum(1); // emit result if (params.has("output")) { counts.writeAsText(params.get("output")); } else { System.out.println("Printing result to stdout. Use --output to specify output path."); counts.print(); } // execute program env.execute("WindowWordCount"); }
*************************************************************************
public void reportError(Throwable t) { // set the exception, if it is the first (and the exception is non null) if (t != null && exception.compareAndSet(null, t) && toInterrupt != null) { toInterrupt.interrupt(); } }
Sets the exception and interrupts the target thread, if no other exception has occurred so far. <p>The exception is only set (and the interruption is only triggered), if no other exception was set before. @param t The exception that occurred
public void checkAndThrowException() throws Exception { Throwable t = exception.get(); if (t != null) { if (t instanceof Exception) { throw (Exception) t; } else if (t instanceof Error) { throw (Error) t; } else { throw new Exception(t); } } }
Checks whether an exception has been set via {@link #reportError(Throwable)}. If yes, that exception if re-thrown by this method. @throws Exception This method re-throws the exception, if set.
private void emitWindowResult(W window) throws Exception { BaseRow aggResult = windowFunction.getWindowAggregationResult(window); if (sendRetraction) { previousState.setCurrentNamespace(window); BaseRow previousAggResult = previousState.value(); // has emitted result for the window if (previousAggResult != null) { // current agg is not equal to the previous emitted, should emit retract if (!equaliser.equalsWithoutHeader(aggResult, previousAggResult)) { reuseOutput.replace((BaseRow) getCurrentKey(), previousAggResult); BaseRowUtil.setRetract(reuseOutput); // send retraction collector.collect(reuseOutput); // send accumulate reuseOutput.replace((BaseRow) getCurrentKey(), aggResult); BaseRowUtil.setAccumulate(reuseOutput); collector.collect(reuseOutput); // update previousState previousState.update(aggResult); } // if the previous agg equals to the current agg, no need to send retract and accumulate } // the first fire for the window, only send accumulate else { // send accumulate reuseOutput.replace((BaseRow) getCurrentKey(), aggResult); BaseRowUtil.setAccumulate(reuseOutput); collector.collect(reuseOutput); // update previousState previousState.update(aggResult); } } else { reuseOutput.replace((BaseRow) getCurrentKey(), aggResult); // no need to set header collector.collect(reuseOutput); } }
Emits the window result of the given window.
private void registerCleanupTimer(W window) { long cleanupTime = cleanupTime(window); if (cleanupTime == Long.MAX_VALUE) { // don't set a GC timer for "end of time" return; } if (windowAssigner.isEventTime()) { triggerContext.registerEventTimeTimer(cleanupTime); } else { triggerContext.registerProcessingTimeTimer(cleanupTime); } }
Registers a timer to cleanup the content of the window. @param window the window whose state to discard
private long cleanupTime(W window) { if (windowAssigner.isEventTime()) { long cleanupTime = Math.max(0, window.maxTimestamp() + allowedLateness); return cleanupTime >= window.maxTimestamp() ? cleanupTime : Long.MAX_VALUE; } else { return Math.max(0, window.maxTimestamp()); } }
Returns the cleanup time for a window, which is {@code window.maxTimestamp + allowedLateness}. In case this leads to a value greated than {@link Long#MAX_VALUE} then a cleanup time of {@link Long#MAX_VALUE} is returned. @param window the window whose cleanup time we are computing.
public void onMatch(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); if (!AggregateUtil.containsAccurateDistinctCall(aggregate.getAggCallList())) { return; } // Check unsupported aggregate which contains both approximate distinct call and // accurate distinct call. if (AggregateUtil.containsApproximateDistinctCall(aggregate.getAggCallList())) { throw new TableException( "There are both Distinct AggCall and Approximate Distinct AggCall in one sql statement, " + "it is not supported yet.\nPlease choose one of them."); } // If this aggregate is a non-simple aggregate(e.g. CUBE, ROLLUP) // and contains distinct calls, it should be transformed to simple aggregate first // by DecomposeGroupingSetsRule. Then this rule expands it's distinct aggregates. if (aggregate.getGroupSets().size() > 1) { return; } // Find all of the agg expressions. We use a LinkedHashSet to ensure determinism. int nonDistinctAggCallCount = 0; // find all aggregate calls without distinct int filterCount = 0; int unsupportedNonDistinctAggCallCount = 0; final Set<Pair<List<Integer>, Integer>> argLists = new LinkedHashSet<>(); for (AggregateCall aggCall : aggregate.getAggCallList()) { if (aggCall.filterArg >= 0) { ++filterCount; } if (!aggCall.isDistinct()) { ++nonDistinctAggCallCount; final SqlKind aggCallKind = aggCall.getAggregation().getKind(); // We only support COUNT/SUM/MIN/MAX for the "single" count distinct optimization switch (aggCallKind) { case COUNT: case SUM: case SUM0: case MIN: case MAX: break; default: ++unsupportedNonDistinctAggCallCount; } } else { argLists.add(Pair.of(aggCall.getArgList(), aggCall.filterArg)); } } final int distinctAggCallCount = aggregate.getAggCallList().size() - nonDistinctAggCallCount; Preconditions.checkState(argLists.size() > 0, "containsDistinctCall lied"); // If all of the agg expressions are distinct and have the same // arguments then we can use a more efficient form. if (nonDistinctAggCallCount == 0 && argLists.size() == 1 && aggregate.getGroupType() == Group.SIMPLE) { final Pair<List<Integer>, Integer> pair = com.google.common.collect.Iterables.getOnlyElement(argLists); final RelBuilder relBuilder = call.builder(); convertMonopole(relBuilder, aggregate, pair.left, pair.right); call.transformTo(relBuilder.build()); return; } if (useGroupingSets) { rewriteUsingGroupingSets(call, aggregate); return; } // If only one distinct aggregate and one or more non-distinct aggregates, // we can generate multi-phase aggregates if (distinctAggCallCount == 1 // one distinct aggregate && filterCount == 0 // no filter && unsupportedNonDistinctAggCallCount == 0 // sum/min/max/count in non-distinct aggregate && nonDistinctAggCallCount > 0) { // one or more non-distinct aggregates final RelBuilder relBuilder = call.builder(); convertSingletonDistinct(relBuilder, aggregate, argLists); call.transformTo(relBuilder.build()); return; } // Create a list of the expressions which will yield the final result. // Initially, the expressions point to the input field. final List<RelDataTypeField> aggFields = aggregate.getRowType().getFieldList(); final List<RexInputRef> refs = new ArrayList<>(); final List<String> fieldNames = aggregate.getRowType().getFieldNames(); final ImmutableBitSet groupSet = aggregate.getGroupSet(); final int groupAndIndicatorCount = aggregate.getGroupCount() + aggregate.getIndicatorCount(); for (int i : Util.range(groupAndIndicatorCount)) { refs.add(RexInputRef.of(i, aggFields)); } // Aggregate the original relation, including any non-distinct aggregates. final List<AggregateCall> newAggCallList = new ArrayList<>(); int i = -1; for (AggregateCall aggCall : aggregate.getAggCallList()) { ++i; if (aggCall.isDistinct()) { refs.add(null); continue; } refs.add( new RexInputRef( groupAndIndicatorCount + newAggCallList.size(), aggFields.get(groupAndIndicatorCount + i).getType())); newAggCallList.add(aggCall); } // In the case where there are no non-distinct aggregates (regardless of // whether there are group bys), there's no need to generate the // extra aggregate and join. final RelBuilder relBuilder = call.builder(); relBuilder.push(aggregate.getInput()); int n = 0; if (!newAggCallList.isEmpty()) { final RelBuilder.GroupKey groupKey = relBuilder.groupKey(groupSet, aggregate.getGroupSets()); relBuilder.aggregate(groupKey, newAggCallList); ++n; } // For each set of operands, find and rewrite all calls which have that // set of operands. for (Pair<List<Integer>, Integer> argList : argLists) { doRewrite(relBuilder, aggregate, n++, argList.left, argList.right, refs); } relBuilder.project(refs, fieldNames); call.transformTo(relBuilder.build()); }
~ Methods ----------------------------------------------------------------
private RelBuilder convertSingletonDistinct(RelBuilder relBuilder, Aggregate aggregate, Set<Pair<List<Integer>, Integer>> argLists) { // In this case, we are assuming that there is a single distinct function. // So make sure that argLists is of size one. Preconditions.checkArgument(argLists.size() == 1); // For example, // SELECT deptno, COUNT(*), SUM(bonus), MIN(DISTINCT sal) // FROM emp // GROUP BY deptno // // becomes // // SELECT deptno, SUM(cnt), SUM(bonus), MIN(sal) // FROM ( // SELECT deptno, COUNT(*) as cnt, SUM(bonus), sal // FROM EMP // GROUP BY deptno, sal) // Aggregate B // GROUP BY deptno // Aggregate A relBuilder.push(aggregate.getInput()); final List<AggregateCall> originalAggCalls = aggregate.getAggCallList(); final ImmutableBitSet originalGroupSet = aggregate.getGroupSet(); // Add the distinct aggregate column(s) to the group-by columns, // if not already a part of the group-by final SortedSet<Integer> bottomGroupSet = new TreeSet<>(); bottomGroupSet.addAll(aggregate.getGroupSet().asList()); for (AggregateCall aggCall : originalAggCalls) { if (aggCall.isDistinct()) { bottomGroupSet.addAll(aggCall.getArgList()); break; // since we only have single distinct call } } // Generate the intermediate aggregate B, the one on the bottom that converts // a distinct call to group by call. // Bottom aggregate is the same as the original aggregate, except that // the bottom aggregate has converted the DISTINCT aggregate to a group by clause. final List<AggregateCall> bottomAggregateCalls = new ArrayList<>(); for (AggregateCall aggCall : originalAggCalls) { // Project the column corresponding to the distinct aggregate. Project // as-is all the non-distinct aggregates if (!aggCall.isDistinct()) { final AggregateCall newCall = AggregateCall.create(aggCall.getAggregation(), false, aggCall.isApproximate(), aggCall.getArgList(), -1, ImmutableBitSet.of(bottomGroupSet).cardinality(), relBuilder.peek(), null, aggCall.name); bottomAggregateCalls.add(newCall); } } // Generate the aggregate B (see the reference example above) relBuilder.push( aggregate.copy( aggregate.getTraitSet(), relBuilder.build(), false, ImmutableBitSet.of(bottomGroupSet), null, bottomAggregateCalls)); // Add aggregate A (see the reference example above), the top aggregate // to handle the rest of the aggregation that the bottom aggregate hasn't handled final List<AggregateCall> topAggregateCalls = com.google.common.collect.Lists.newArrayList(); // Use the remapped arguments for the (non)distinct aggregate calls int nonDistinctAggCallProcessedSoFar = 0; for (AggregateCall aggCall : originalAggCalls) { final AggregateCall newCall; if (aggCall.isDistinct()) { List<Integer> newArgList = new ArrayList<>(); for (int arg : aggCall.getArgList()) { newArgList.add(bottomGroupSet.headSet(arg).size()); } newCall = AggregateCall.create(aggCall.getAggregation(), false, aggCall.isApproximate(), newArgList, -1, originalGroupSet.cardinality(), relBuilder.peek(), aggCall.getType(), aggCall.name); } else { // If aggregate B had a COUNT aggregate call the corresponding aggregate at // aggregate A must be SUM. For other aggregates, it remains the same. final List<Integer> newArgs = com.google.common.collect.Lists.newArrayList( bottomGroupSet.size() + nonDistinctAggCallProcessedSoFar); if (aggCall.getAggregation().getKind() == SqlKind.COUNT) { newCall = AggregateCall.create(new SqlSumEmptyIsZeroAggFunction(), false, aggCall.isApproximate(), newArgs, -1, originalGroupSet.cardinality(), relBuilder.peek(), aggCall.getType(), aggCall.getName()); } else { newCall = AggregateCall.create(aggCall.getAggregation(), false, aggCall.isApproximate(), newArgs, -1, originalGroupSet.cardinality(), relBuilder.peek(), aggCall.getType(), aggCall.name); } nonDistinctAggCallProcessedSoFar++; } topAggregateCalls.add(newCall); } // Populate the group-by keys with the remapped arguments for aggregate A // The top groupset is basically an identity (first X fields of aggregate B's // output), minus the distinct aggCall's input. final Set<Integer> topGroupSet = new HashSet<>(); int groupSetToAdd = 0; for (int bottomGroup : bottomGroupSet) { if (originalGroupSet.get(bottomGroup)) { topGroupSet.add(groupSetToAdd); } groupSetToAdd++; } relBuilder.push( aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), aggregate.indicator, ImmutableBitSet.of(topGroupSet), null, topAggregateCalls)); return relBuilder; }
Converts an aggregate with one distinct aggregate and one or more non-distinct aggregates to multi-phase aggregates (see reference example below). @param relBuilder Contains the input relational expression @param aggregate Original aggregate @param argLists Arguments and filters to the distinct aggregate function
private RelBuilder convertMonopole(RelBuilder relBuilder, Aggregate aggregate, List<Integer> argList, int filterArg) { // For example, // SELECT deptno, COUNT(DISTINCT sal), SUM(DISTINCT sal) // FROM emp // GROUP BY deptno // // becomes // // SELECT deptno, COUNT(distinct_sal), SUM(distinct_sal) // FROM ( // SELECT DISTINCT deptno, sal AS distinct_sal // FROM EMP GROUP BY deptno) // GROUP BY deptno // Project the columns of the GROUP BY plus the arguments // to the agg function. final Map<Integer, Integer> sourceOf = new HashMap<>(); createSelectDistinct(relBuilder, aggregate, argList, filterArg, sourceOf); // Create an aggregate on top, with the new aggregate list. final List<AggregateCall> newAggCalls = com.google.common.collect.Lists.newArrayList(aggregate.getAggCallList()); rewriteAggCalls(newAggCalls, argList, sourceOf); final int cardinality = aggregate.getGroupSet().cardinality(); relBuilder.push( aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), aggregate.indicator, ImmutableBitSet.range(cardinality), null, newAggCalls)); return relBuilder; }
Converts an aggregate relational expression that contains just one distinct aggregate function (or perhaps several over the same arguments) and no non-distinct aggregate functions.
private void doRewrite(RelBuilder relBuilder, Aggregate aggregate, int n, List<Integer> argList, int filterArg, List<RexInputRef> refs) { final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder(); final List<RelDataTypeField> leftFields; if (n == 0) { leftFields = null; } else { leftFields = relBuilder.peek().getRowType().getFieldList(); } // Aggregate( // child, // {COUNT(DISTINCT 1), SUM(DISTINCT 1), SUM(2)}) // // becomes // // Aggregate( // Join( // child, // Aggregate(child, < all columns > {}), // INNER, // <f2 = f5>)) // // E.g. // SELECT deptno, SUM(DISTINCT sal), COUNT(DISTINCT gender), MAX(age) // FROM Emps // GROUP BY deptno // // becomes // // SELECT e.deptno, adsal.sum_sal, adgender.count_gender, e.max_age // FROM ( // SELECT deptno, MAX(age) as max_age // FROM Emps GROUP BY deptno) AS e // JOIN ( // SELECT deptno, COUNT(gender) AS count_gender FROM ( // SELECT DISTINCT deptno, gender FROM Emps) AS dgender // GROUP BY deptno) AS adgender // ON e.deptno = adgender.deptno // JOIN ( // SELECT deptno, SUM(sal) AS sum_sal FROM ( // SELECT DISTINCT deptno, sal FROM Emps) AS dsal // GROUP BY deptno) AS adsal // ON e.deptno = adsal.deptno // GROUP BY e.deptno // // Note that if a query contains no non-distinct aggregates, then the // very first join/group by is omitted. In the example above, if // MAX(age) is removed, then the sub-select of "e" is not needed, and // instead the two other group by's are joined to one another. // Project the columns of the GROUP BY plus the arguments // to the agg function. final Map<Integer, Integer> sourceOf = new HashMap<>(); createSelectDistinct(relBuilder, aggregate, argList, filterArg, sourceOf); // Now compute the aggregate functions on top of the distinct dataset. // Each distinct agg becomes a non-distinct call to the corresponding // field from the right; for example, // "COUNT(DISTINCT e.sal)" // becomes // "COUNT(distinct_e.sal)". final List<AggregateCall> aggCallList = new ArrayList<>(); final List<AggregateCall> aggCalls = aggregate.getAggCallList(); final int groupAndIndicatorCount = aggregate.getGroupCount() + aggregate.getIndicatorCount(); int i = groupAndIndicatorCount - 1; for (AggregateCall aggCall : aggCalls) { ++i; // Ignore agg calls which are not distinct or have the wrong set // arguments. If we're rewriting aggs whose args are {sal}, we will // rewrite COUNT(DISTINCT sal) and SUM(DISTINCT sal) but ignore // COUNT(DISTINCT gender) or SUM(sal). if (!aggCall.isDistinct()) { continue; } if (!aggCall.getArgList().equals(argList)) { continue; } // Re-map arguments. final int argCount = aggCall.getArgList().size(); final List<Integer> newArgs = new ArrayList<>(argCount); for (int j = 0; j < argCount; j++) { final Integer arg = aggCall.getArgList().get(j); newArgs.add(sourceOf.get(arg)); } final int newFilterArg = aggCall.filterArg >= 0 ? sourceOf.get(aggCall.filterArg) : -1; final AggregateCall newAggCall = AggregateCall.create(aggCall.getAggregation(), false, aggCall.isApproximate(), newArgs, newFilterArg, aggCall.getType(), aggCall.getName()); assert refs.get(i) == null; if (n == 0) { refs.set(i, new RexInputRef(groupAndIndicatorCount + aggCallList.size(), newAggCall.getType())); } else { refs.set(i, new RexInputRef(leftFields.size() + groupAndIndicatorCount + aggCallList.size(), newAggCall.getType())); } aggCallList.add(newAggCall); } final Map<Integer, Integer> map = new HashMap<>(); for (Integer key : aggregate.getGroupSet()) { map.put(key, map.size()); } final ImmutableBitSet newGroupSet = aggregate.getGroupSet().permute(map); assert newGroupSet .equals(ImmutableBitSet.range(aggregate.getGroupSet().cardinality())); com.google.common.collect.ImmutableList<ImmutableBitSet> newGroupingSets = null; if (aggregate.indicator) { newGroupingSets = ImmutableBitSet.ORDERING.immutableSortedCopy( ImmutableBitSet.permute(aggregate.getGroupSets(), map)); } relBuilder.push( aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), aggregate.indicator, newGroupSet, newGroupingSets, aggCallList)); // If there's no left child yet, no need to create the join if (n == 0) { return; } // Create the join condition. It is of the form // 'left.f0 = right.f0 and left.f1 = right.f1 and ...' // where {f0, f1, ...} are the GROUP BY fields. final List<RelDataTypeField> distinctFields = relBuilder.peek().getRowType().getFieldList(); final List<RexNode> conditions = com.google.common.collect.Lists.newArrayList(); for (i = 0; i < groupAndIndicatorCount; ++i) { // null values form its own group // use "is not distinct from" so that the join condition // allows null values to match. conditions.add( rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM, RexInputRef.of(i, leftFields), new RexInputRef(leftFields.size() + i, distinctFields.get(i).getType()))); } // Join in the new 'select distinct' relation. relBuilder.join(JoinRelType.INNER, conditions); }
Converts all distinct aggregate calls to a given set of arguments. <p>This method is called several times, one for each set of arguments. Each time it is called, it generates a JOIN to a new SELECT DISTINCT relational expression, and modifies the set of top-level calls. @param aggregate Original aggregate @param n Ordinal of this in a join. {@code relBuilder} contains the input relational expression (either the original aggregate, the output from the previous call to this method. {@code n} is 0 if we're converting the first distinct aggregate in a query with no non-distinct aggregates) @param argList Arguments to the distinct aggregate function @param filterArg Argument that filters input to aggregate function, or -1 @param refs Array of expressions which will be the projected by the result of this rule. Those relating to this arg list will be modified @return Relational expression
private RelBuilder createSelectDistinct(RelBuilder relBuilder, Aggregate aggregate, List<Integer> argList, int filterArg, Map<Integer, Integer> sourceOf) { relBuilder.push(aggregate.getInput()); final List<Pair<RexNode, String>> projects = new ArrayList<>(); final List<RelDataTypeField> childFields = relBuilder.peek().getRowType().getFieldList(); for (int i : aggregate.getGroupSet()) { sourceOf.put(i, projects.size()); projects.add(RexInputRef.of2(i, childFields)); } if (filterArg >= 0) { sourceOf.put(filterArg, projects.size()); projects.add(RexInputRef.of2(filterArg, childFields)); } for (Integer arg : argList) { if (filterArg >= 0) { // Implement // agg(DISTINCT arg) FILTER $f // by generating // SELECT DISTINCT ... CASE WHEN $f THEN arg ELSE NULL END AS arg // and then applying // agg(arg) // as usual. // // It works except for (rare) agg functions that need to see null // values. final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder(); final RexInputRef filterRef = RexInputRef.of(filterArg, childFields); final Pair<RexNode, String> argRef = RexInputRef.of2(arg, childFields); RexNode condition = rexBuilder.makeCall(SqlStdOperatorTable.CASE, filterRef, argRef.left, rexBuilder.ensureType(argRef.left.getType(), rexBuilder.makeCast(argRef.left.getType(), rexBuilder.constantNull()), true)); sourceOf.put(arg, projects.size()); projects.add(Pair.of(condition, "i$" + argRef.right)); continue; } if (sourceOf.get(arg) != null) { continue; } sourceOf.put(arg, projects.size()); projects.add(RexInputRef.of2(arg, childFields)); } relBuilder.project(Pair.left(projects), Pair.right(projects)); // Get the distinct values of the GROUP BY fields and the arguments // to the agg functions. relBuilder.push( aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), false, ImmutableBitSet.range(projects.size()), null, com.google.common.collect.ImmutableList.<AggregateCall>of())); return relBuilder; }
Given an {@link org.apache.calcite.rel.core.Aggregate} and the ordinals of the arguments to a particular call to an aggregate function, creates a 'select distinct' relational expression which projects the group columns and those arguments but nothing else. <p>For example, given <blockquote> <pre>select f0, count(distinct f1), count(distinct f2) from t group by f0</pre> </blockquote> <p>and the argument list <blockquote>{2}</blockquote> <p>returns <blockquote> <pre>select distinct f0, f2 from t</pre> </blockquote> <p>The <code>sourceOf</code> map is populated with the source of each column; in this case sourceOf.get(0) = 0, and sourceOf.get(1) = 2. @param relBuilder Relational expression builder @param aggregate Aggregate relational expression @param argList Ordinals of columns to make distinct @param filterArg Ordinal of column to filter on, or -1 @param sourceOf Out parameter, is populated with a map of where each output field came from @return Aggregate relational expression which projects the required columns
@SuppressWarnings("unchecked") public static <T> NFAFactory<T> compileFactory( final Pattern<T, ?> pattern, boolean timeoutHandling) { if (pattern == null) { // return a factory for empty NFAs return new NFAFactoryImpl<>(0, Collections.<State<T>>emptyList(), timeoutHandling); } else { final NFAFactoryCompiler<T> nfaFactoryCompiler = new NFAFactoryCompiler<>(pattern); nfaFactoryCompiler.compileFactory(); return new NFAFactoryImpl<>(nfaFactoryCompiler.getWindowTime(), nfaFactoryCompiler.getStates(), timeoutHandling); } }
Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create multiple NFAs. @param pattern Definition of sequence pattern @param timeoutHandling True if the NFA shall return timed out event patterns @param <T> Type of the input events @return Factory for NFAs corresponding to the given pattern
public static boolean canProduceEmptyMatches(final Pattern<?, ?> pattern) { NFAFactoryCompiler<?> compiler = new NFAFactoryCompiler<>(checkNotNull(pattern)); compiler.compileFactory(); State<?> startState = compiler.getStates().stream().filter(State::isStart).findFirst().orElseThrow( () -> new IllegalStateException("Compiler produced no start state. It is a bug. File a jira.")); Set<State<?>> visitedStates = new HashSet<>(); final Stack<State<?>> statesToCheck = new Stack<>(); statesToCheck.push(startState); while (!statesToCheck.isEmpty()) { final State<?> currentState = statesToCheck.pop(); if (visitedStates.contains(currentState)) { continue; } else { visitedStates.add(currentState); } for (StateTransition<?> transition : currentState.getStateTransitions()) { if (transition.getAction() == StateTransitionAction.PROCEED) { if (transition.getTargetState().isFinal()) { return true; } else { statesToCheck.push(transition.getTargetState()); } } } } return false; }
Verifies if the provided pattern can possibly generate empty match. Example of patterns that can possibly generate empty matches are: A*, A?, A* B? etc. @param pattern pattern to check @return true if empty match could potentially match the pattern, false otherwise
public void inputWatermark(Watermark watermark, int channelIndex) { // ignore the input watermark if its input channel, or all input channels are idle (i.e. overall the valve is idle). if (lastOutputStreamStatus.isActive() && channelStatuses[channelIndex].streamStatus.isActive()) { long watermarkMillis = watermark.getTimestamp(); // if the input watermark's value is less than the last received watermark for its input channel, ignore it also. if (watermarkMillis > channelStatuses[channelIndex].watermark) { channelStatuses[channelIndex].watermark = watermarkMillis; // previously unaligned input channels are now aligned if its watermark has caught up if (!channelStatuses[channelIndex].isWatermarkAligned && watermarkMillis >= lastOutputWatermark) { channelStatuses[channelIndex].isWatermarkAligned = true; } // now, attempt to find a new min watermark across all aligned channels findAndOutputNewMinWatermarkAcrossAlignedChannels(); } } }
Feed a {@link Watermark} into the valve. If the input triggers the valve to output a new Watermark, {@link ValveOutputHandler#handleWatermark(Watermark)} will be called to process the new Watermark. @param watermark the watermark to feed to the valve @param channelIndex the index of the channel that the fed watermark belongs to (index starting from 0)
public void inputStreamStatus(StreamStatus streamStatus, int channelIndex) { // only account for stream status inputs that will result in a status change for the input channel if (streamStatus.isIdle() && channelStatuses[channelIndex].streamStatus.isActive()) { // handle active -> idle toggle for the input channel channelStatuses[channelIndex].streamStatus = StreamStatus.IDLE; // the channel is now idle, therefore not aligned channelStatuses[channelIndex].isWatermarkAligned = false; // if all input channels of the valve are now idle, we need to output an idle stream // status from the valve (this also marks the valve as idle) if (!InputChannelStatus.hasActiveChannels(channelStatuses)) { // now that all input channels are idle and no channels will continue to advance its watermark, // we should "flush" all watermarks across all channels; effectively, this means emitting // the max watermark across all channels as the new watermark. Also, since we already try to advance // the min watermark as channels individually become IDLE, here we only need to perform the flush // if the watermark of the last active channel that just became idle is the current min watermark. if (channelStatuses[channelIndex].watermark == lastOutputWatermark) { findAndOutputMaxWatermarkAcrossAllChannels(); } lastOutputStreamStatus = StreamStatus.IDLE; outputHandler.handleStreamStatus(lastOutputStreamStatus); } else if (channelStatuses[channelIndex].watermark == lastOutputWatermark) { // if the watermark of the channel that just became idle equals the last output // watermark (the previous overall min watermark), we may be able to find a new // min watermark from the remaining aligned channels findAndOutputNewMinWatermarkAcrossAlignedChannels(); } } else if (streamStatus.isActive() && channelStatuses[channelIndex].streamStatus.isIdle()) { // handle idle -> active toggle for the input channel channelStatuses[channelIndex].streamStatus = StreamStatus.ACTIVE; // if the last watermark of the input channel, before it was marked idle, is still larger than // the overall last output watermark of the valve, then we can set the channel to be aligned already. if (channelStatuses[channelIndex].watermark >= lastOutputWatermark) { channelStatuses[channelIndex].isWatermarkAligned = true; } // if the valve was previously marked to be idle, mark it as active and output an active stream // status because at least one of the input channels is now active if (lastOutputStreamStatus.isIdle()) { lastOutputStreamStatus = StreamStatus.ACTIVE; outputHandler.handleStreamStatus(lastOutputStreamStatus); } } }
Feed a {@link StreamStatus} into the valve. This may trigger the valve to output either a new Stream Status, for which {@link ValveOutputHandler#handleStreamStatus(StreamStatus)} will be called, or a new Watermark, for which {@link ValveOutputHandler#handleWatermark(Watermark)} will be called. @param streamStatus the stream status to feed to the valve @param channelIndex the index of the channel that the fed stream status belongs to (index starting from 0)
private <T> void addEntry(StreamElementQueueEntry<T> streamElementQueueEntry) { assert(lock.isHeldByCurrentThread()); queue.addLast(streamElementQueueEntry); streamElementQueueEntry.onComplete( (StreamElementQueueEntry<T> value) -> { try { onCompleteHandler(value); } catch (InterruptedException e) { // we got interrupted. This indicates a shutdown of the executor LOG.debug("AsyncBufferEntry could not be properly completed because the " + "executor thread has been interrupted.", e); } catch (Throwable t) { operatorActions.failOperator(new Exception("Could not complete the " + "stream element queue entry: " + value + '.', t)); } }, executor); }
Add the given {@link StreamElementQueueEntry} to the queue. Additionally, this method registers a onComplete callback which is triggered once the given queue entry is completed. @param streamElementQueueEntry to be inserted @param <T> Type of the stream element queue entry's result
private void onCompleteHandler(StreamElementQueueEntry<?> streamElementQueueEntry) throws InterruptedException { lock.lockInterruptibly(); try { if (!queue.isEmpty() && queue.peek().isDone()) { LOG.debug("Signal ordered stream element queue has completed head element."); headIsCompleted.signalAll(); } } finally { lock.unlock(); } }
Check if the completed {@link StreamElementQueueEntry} is the current head. If this is the case, then notify the consumer thread about a new consumable entry. @param streamElementQueueEntry which has been completed @throws InterruptedException if the current thread is interrupted
@Deprecated @PublicEvolving public DataSink<T> sortLocalOutput(int field, Order order) { // get flat keys Keys.ExpressionKeys<T> ek = new Keys.ExpressionKeys<>(field, this.type); int[] flatKeys = ek.computeLogicalKeyPositions(); if (!Keys.ExpressionKeys.isSortKey(field, this.type)) { throw new InvalidProgramException("Selected sort key is not a sortable type"); } if (this.sortKeyPositions == null) { // set sorting info this.sortKeyPositions = flatKeys; this.sortOrders = new Order[flatKeys.length]; Arrays.fill(this.sortOrders, order); } else { // append sorting info to exising info int oldLength = this.sortKeyPositions.length; int newLength = oldLength + flatKeys.length; this.sortKeyPositions = Arrays.copyOf(this.sortKeyPositions, newLength); this.sortOrders = Arrays.copyOf(this.sortOrders, newLength); for (int i = 0; i < flatKeys.length; i++) { this.sortKeyPositions[oldLength + i] = flatKeys[i]; this.sortOrders[oldLength + i] = order; } } return this; }
Sorts each local partition of a {@link org.apache.flink.api.java.tuple.Tuple} data set on the specified field in the specified {@link Order} before it is emitted by the output format. <p><b>Note: Only tuple data sets can be sorted using integer field indices.</b> <p>The tuple data set can be sorted on multiple fields in different orders by chaining {@link #sortLocalOutput(int, Order)} calls. @param field The Tuple field on which the data set is locally sorted. @param order The Order in which the specified Tuple field is locally sorted. @return This data sink operator with specified output order. @see org.apache.flink.api.java.tuple.Tuple @see Order @deprecated Use {@link DataSet#sortPartition(int, Order)} instead
@Deprecated @PublicEvolving public DataSink<T> sortLocalOutput(String fieldExpression, Order order) { int numFields; int[] fields; Order[] orders; // compute flat field positions for (nested) sorting fields Keys.ExpressionKeys<T> ek = new Keys.ExpressionKeys<>(fieldExpression, this.type); fields = ek.computeLogicalKeyPositions(); if (!Keys.ExpressionKeys.isSortKey(fieldExpression, this.type)) { throw new InvalidProgramException("Selected sort key is not a sortable type"); } numFields = fields.length; orders = new Order[numFields]; Arrays.fill(orders, order); if (this.sortKeyPositions == null) { // set sorting info this.sortKeyPositions = fields; this.sortOrders = orders; } else { // append sorting info to existing info int oldLength = this.sortKeyPositions.length; int newLength = oldLength + numFields; this.sortKeyPositions = Arrays.copyOf(this.sortKeyPositions, newLength); this.sortOrders = Arrays.copyOf(this.sortOrders, newLength); for (int i = 0; i < numFields; i++) { this.sortKeyPositions[oldLength + i] = fields[i]; this.sortOrders[oldLength + i] = orders[i]; } } return this; }
Sorts each local partition of a data set on the field(s) specified by the field expression in the specified {@link Order} before it is emitted by the output format. <p><b>Note: Non-composite types can only be sorted on the full element which is specified by a wildcard expression ("*" or "_").</b> <p>Data sets of composite types (Tuple or Pojo) can be sorted on multiple fields in different orders by chaining {@link #sortLocalOutput(String, Order)} calls. @param fieldExpression The field expression for the field(s) on which the data set is locally sorted. @param order The Order in which the specified field(s) are locally sorted. @return This data sink operator with specified output order. @see Order @deprecated Use {@link DataSet#sortPartition(String, Order)} instead
protected GenericDataSinkBase<T> translateToDataFlow(Operator<T> input) { // select the name (or create a default one) String name = this.name != null ? this.name : this.format.toString(); GenericDataSinkBase<T> sink = new GenericDataSinkBase<>(this.format, new UnaryOperatorInformation<>(this.type, new NothingTypeInfo()), name); // set input sink.setInput(input); // set parameters if (this.parameters != null) { sink.getParameters().addAll(this.parameters); } // set parallelism if (this.parallelism > 0) { // use specified parallelism sink.setParallelism(this.parallelism); } else { // if no parallelism has been specified, use parallelism of input operator to enable chaining sink.setParallelism(input.getParallelism()); } if (this.sortKeyPositions != null) { // configure output sorting Ordering ordering = new Ordering(); for (int i = 0; i < this.sortKeyPositions.length; i++) { ordering.appendOrdering(this.sortKeyPositions[i], null, this.sortOrders[i]); } sink.setLocalOrder(ordering); } return sink; }
--------------------------------------------------------------------------------------------
public DataSink<T> setParallelism(int parallelism) { Preconditions.checkArgument(parallelism > 0 || parallelism == ExecutionConfig.PARALLELISM_DEFAULT, "The parallelism of an operator must be at least 1."); this.parallelism = parallelism; return this; }
Sets the parallelism for this data sink. The degree must be 1 or more. @param parallelism The parallelism for this data sink. A value equal to {@link ExecutionConfig#PARALLELISM_DEFAULT} will use the system default. @return This data sink with set parallelism.
private DataSink<T> setResources(ResourceSpec minResources, ResourceSpec preferredResources) { Preconditions.checkNotNull(minResources, "The min resources must be not null."); Preconditions.checkNotNull(preferredResources, "The preferred resources must be not null."); Preconditions.checkArgument(minResources.isValid() && preferredResources.isValid() && minResources.lessThanOrEqual(preferredResources), "The values in resources must be not less than 0 and the preferred resources must be greater than the min resources."); this.minResources = minResources; this.preferredResources = preferredResources; return this; }
Sets the minimum and preferred resources for this data sink. and the lower and upper resource limits will be considered in resource resize feature for future plan. @param minResources The minimum resources for this data sink. @param preferredResources The preferred resources for this data sink. @return The data sink with set minimum and preferred resources.
private DataSink<T> setResources(ResourceSpec resources) { Preconditions.checkNotNull(resources, "The resources must be not null."); Preconditions.checkArgument(resources.isValid(), "The values in resources must be not less than 0."); this.minResources = resources; this.preferredResources = resources; return this; }
Sets the resources for this data sink, and the minimum and preferred resources are the same by default. @param resources The resources for this data sink. @return The data sink with set minimum and preferred resources.
void restorePartitionBuffers(IOManager ioManager, List<MemorySegment> availableMemory) throws IOException { final BulkBlockChannelReader reader = ioManager.createBulkBlockChannelReader(this.initialBuildSideChannel, availableMemory, this.initialPartitionBuffersCount); reader.close(); final List<MemorySegment> partitionBuffersFromDisk = reader.getFullSegments(); this.partitionBuffers = (MemorySegment[]) partitionBuffersFromDisk.toArray(new MemorySegment[partitionBuffersFromDisk.size()]); this.overflowSegments = new MemorySegment[2]; this.numOverflowSegments = 0; this.nextOverflowBucket = 0; this.isRestored = true; }
This method is called every time a multi-match hash map is opened again for a new probe input. @param ioManager @param availableMemory @throws IOException
@PublicEvolving public <KS, OUT> SingleOutputStreamOperator<OUT> process(final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function) { TypeInformation<OUT> outTypeInfo = TypeExtractor.getBinaryOperatorReturnType( function, KeyedBroadcastProcessFunction.class, 1, 2, 3, TypeExtractor.NO_INDEX, getType1(), getType2(), Utils.getCallLocationName(), true); return process(function, outTypeInfo); }
Assumes as inputs a {@link BroadcastStream} and a {@link KeyedStream} and applies the given {@link KeyedBroadcastProcessFunction} on them, thereby creating a transformed output stream. @param function The {@link KeyedBroadcastProcessFunction} that is called for each element in the stream. @param <KS> The type of the keys in the keyed stream. @param <OUT> The type of the output elements. @return The transformed {@link DataStream}.
@PublicEvolving public <KS, OUT> SingleOutputStreamOperator<OUT> process( final KeyedBroadcastProcessFunction<KS, IN1, IN2, OUT> function, final TypeInformation<OUT> outTypeInfo) { Preconditions.checkNotNull(function); Preconditions.checkArgument(inputStream1 instanceof KeyedStream, "A KeyedBroadcastProcessFunction can only be used on a keyed stream."); TwoInputStreamOperator<IN1, IN2, OUT> operator = new CoBroadcastWithKeyedOperator<>(clean(function), broadcastStateDescriptors); return transform("Co-Process-Broadcast-Keyed", outTypeInfo, operator); }
Assumes as inputs a {@link BroadcastStream} and a {@link KeyedStream} and applies the given {@link KeyedBroadcastProcessFunction} on them, thereby creating a transformed output stream. @param function The {@link KeyedBroadcastProcessFunction} that is called for each element in the stream. @param outTypeInfo The type of the output elements. @param <KS> The type of the keys in the keyed stream. @param <OUT> The type of the output elements. @return The transformed {@link DataStream}.
@PublicEvolving public <OUT> SingleOutputStreamOperator<OUT> process( final BroadcastProcessFunction<IN1, IN2, OUT> function, final TypeInformation<OUT> outTypeInfo) { Preconditions.checkNotNull(function); Preconditions.checkArgument(!(inputStream1 instanceof KeyedStream), "A BroadcastProcessFunction can only be used on a non-keyed stream."); TwoInputStreamOperator<IN1, IN2, OUT> operator = new CoBroadcastWithNonKeyedOperator<>(clean(function), broadcastStateDescriptors); return transform("Co-Process-Broadcast", outTypeInfo, operator); }
Assumes as inputs a {@link BroadcastStream} and a non-keyed {@link DataStream} and applies the given {@link BroadcastProcessFunction} on them, thereby creating a transformed output stream. @param function The {@link BroadcastProcessFunction} that is called for each element in the stream. @param outTypeInfo The type of the output elements. @param <OUT> The type of the output elements. @return The transformed {@link DataStream}.
public static List<Expression> createAliasList(List<Expression> aliases, TableOperation child) { TableSchema childSchema = child.getTableSchema(); if (aliases.size() > childSchema.getFieldCount()) { throw new ValidationException("Aliasing more fields than we actually have."); } List<ValueLiteralExpression> fieldAliases = aliases.stream() .map(f -> f.accept(aliasLiteralValidator)) .collect(Collectors.toList()); String[] childNames = childSchema.getFieldNames(); return IntStream.range(0, childNames.length) .mapToObj(idx -> { UnresolvedReferenceExpression oldField = new UnresolvedReferenceExpression(childNames[idx]); if (idx < fieldAliases.size()) { ValueLiteralExpression alias = fieldAliases.get(idx); return new CallExpression(BuiltInFunctionDefinitions.AS, Arrays.asList(oldField, alias)); } else { return oldField; } }).collect(Collectors.toList()); }
Creates a list of valid alias expressions. Resulting expression might still contain {@link UnresolvedReferenceExpression}. @param aliases aliases to validate @param child relational operation on top of which to apply the aliases @return validated list of aliases
@Override public BinaryRow baseRowToBinary(BaseRow row) { if (row instanceof BinaryRow) { return (BinaryRow) row; } BinaryRow binaryRow = new BinaryRow(types.length); BinaryRowWriter writer = new BinaryRowWriter(binaryRow); writer.writeHeader(row.getHeader()); for (int i = 0; i < types.length; i++) { if (row.isNullAt(i)) { writer.setNullAt(i); } else { BinaryWriter.write(writer, i, TypeGetterSetters.get(row, i, types[i]), types[i]); } } writer.complete(); return binaryRow; }
Convert base row to binary row. TODO modify it to code gen, and reuse BinaryRow&BinaryRowWriter.
public Option add(String name) throws RequiredParametersException { if (!this.data.containsKey(name)) { Option option = new Option(name); this.data.put(name, option); return option; } else { throw new RequiredParametersException("Option with key " + name + " already exists."); } }
Add a parameter based on its name. @param name - the name of the parameter @return - an {@link Option} object representing the parameter @throws RequiredParametersException if an option with the same name is already defined
public void add(Option option) throws RequiredParametersException { if (!this.data.containsKey(option.getName())) { this.data.put(option.getName(), option); } else { throw new RequiredParametersException("Option with key " + option.getName() + " already exists."); } }
Add a parameter encapsulated in an {@link Option} object. @param option - the parameter @throws RequiredParametersException if an option with the same name is already defined
public ParameterTool applyTo(ParameterTool parameterTool) throws RequiredParametersException { List<String> missingArguments = new LinkedList<>(); HashMap<String, String> newParameters = new HashMap<>(parameterTool.toMap()); for (Option o : data.values()) { if (newParameters.containsKey(o.getName())) { if (Objects.equals(newParameters.get(o.getName()), ParameterTool.NO_VALUE_KEY)) { // the parameter has been passed, but no value, check if there is a default value checkAndApplyDefaultValue(o, newParameters); } else { // a value has been passed in the parameterTool, now check if it adheres to all constraints checkAmbiguousValues(o, newParameters); checkIsCastableToDefinedType(o, newParameters); checkChoices(o, newParameters); } } else { // check if there is a default name or a value passed for a possibly defined alternative name. if (hasNoDefaultValueAndNoValuePassedOnAlternativeName(o, newParameters)) { missingArguments.add(o.getName()); } } } if (!missingArguments.isEmpty()) { throw new RequiredParametersException(this.missingArgumentsText(missingArguments), missingArguments); } return ParameterTool.fromMap(newParameters); }
Check for all required parameters defined: - has a value been passed - if not, does the parameter have an associated default value - does the type of the parameter match the one defined in RequiredParameters - does the value provided in the parameterTool adhere to the choices defined in the option. <p>If any check fails, a RequiredParametersException is thrown @param parameterTool - parameters supplied by the user. @return the updated ParameterTool containing all the required parameters @throws RequiredParametersException if any of the specified checks fail
private void checkAndApplyDefaultValue(Option o, Map<String, String> data) throws RequiredParametersException { if (hasNoDefaultValueAndNoValuePassedOnAlternativeName(o, data)) { throw new RequiredParametersException("No default value for undefined parameter " + o.getName()); } }
else throw an exception
private void checkIsCastableToDefinedType(Option o, Map<String, String> data) throws RequiredParametersException { if (o.hasType() && !o.isCastableToDefinedType(data.get(o.getName()))) { throw new RequiredParametersException("Value for parameter " + o.getName() + " cannot be cast to type " + o.getType()); } }
is castable to the type of the option (if any is defined)
private void checkChoices(Option o, Map<String, String> data) throws RequiredParametersException { if (o.getChoices().size() > 0 && !o.getChoices().contains(data.get(o.getName()))) { throw new RequiredParametersException("Value " + data.get(o.getName()) + " is not in the list of valid choices for key " + o.getName()); } }
adheres to the list of given choices for the param in the options (if any are defined)
private boolean hasNoDefaultValueAndNoValuePassedOnAlternativeName(Option o, Map<String, String> data) throws RequiredParametersException { if (o.hasAlt() && data.containsKey(o.getAlt())) { data.put(o.getName(), data.get(o.getAlt())); } else { if (o.hasDefaultValue()) { data.put(o.getName(), o.getDefaultValue()); if (o.hasAlt()) { data.put(o.getAlt(), o.getDefaultValue()); } } else { return true; } } return false; }
else return true to indicate parameter is 'really' missing
private void checkAmbiguousValues(Option o, Map<String, String> data) throws RequiredParametersException{ if (data.containsKey(o.getAlt()) && !Objects.equals(data.get(o.getAlt()), ParameterTool.NO_VALUE_KEY)) { throw new RequiredParametersException("Value passed for parameter " + o.getName() + " is ambiguous. Value passed for short and long name."); } }
check if it also contains a value for the shortName in option (if any is defined)
public String getHelp() { StringBuilder sb = new StringBuilder(data.size() * HELP_TEXT_LENGTH_PER_PARAM); sb.append("Required Parameters:"); sb.append(HELP_TEXT_LINE_DELIMITER); for (Option o : data.values()) { sb.append(this.helpText(o)); } sb.append(HELP_TEXT_LINE_DELIMITER); return sb.toString(); }
Build a help text for the defined parameters. <p>The format of the help text will be: Required Parameters: \t -:shortName:, --:name: \t :helpText: \t default: :defaultValue: \t choices: :choices: \n @return a formatted help String.
private String helpText(Option option) { StringBuilder sb = new StringBuilder(HELP_TEXT_LENGTH_PER_PARAM); sb.append(HELP_TEXT_PARAM_DELIMITER); // if there is a short name, add it. if (option.hasAlt()) { sb.append("-"); sb.append(option.getAlt()); sb.append(", "); } // add the name sb.append("--"); sb.append(option.getName()); sb.append(HELP_TEXT_PARAM_DELIMITER); // if there is a help text, add it if (option.getHelpText() != null) { sb.append(option.getHelpText()); sb.append(HELP_TEXT_PARAM_DELIMITER); } // if there is a default value, add it. if (option.hasDefaultValue()) { sb.append("default: "); sb.append(option.getDefaultValue()); sb.append(HELP_TEXT_PARAM_DELIMITER); } // if there is a list of choices add it. if (!option.getChoices().isEmpty()) { sb.append("choices: "); for (String choice : option.getChoices()) { sb.append(choice); sb.append(" "); } } sb.append(HELP_TEXT_LINE_DELIMITER); return sb.toString(); }
for the given option create a line for the help text. The line looks like: \t -:shortName:, --:name: \t :helpText: \t default: :defaultValue: \t choices: :choices:
public JaccardIndex<K, VV, EV> setGroupSize(int groupSize) { Preconditions.checkArgument(groupSize > 0, "Group size must be greater than zero"); this.groupSize = groupSize; return this; }
Override the default group size for the quadratic expansion of neighbor pairs. Small groups generate more data whereas large groups distribute computation less evenly among tasks. <p>The default value should be near-optimal for all use cases. @param groupSize the group size for the quadratic expansion of neighbor pairs @return this
public JaccardIndex<K, VV, EV> setMinimumScore(int numerator, int denominator) { Preconditions.checkArgument(numerator >= 0, "Minimum score numerator must be non-negative"); Preconditions.checkArgument(denominator > 0, "Minimum score denominator must be greater than zero"); Preconditions.checkArgument(numerator <= denominator, "Minimum score fraction must be less than or equal to one"); this.unboundedScores = false; this.minimumScoreNumerator = numerator; this.minimumScoreDenominator = denominator; return this; }
Filter out Jaccard Index scores less than the given minimum fraction. @param numerator numerator of the minimum score @param denominator denominator of the minimum score @return this @see #setMaximumScore(int, int)