io.deephaven.engine.table.impl.updateby.em.ByteEMOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deephaven-engine-table Show documentation
Show all versions of deephaven-engine-table Show documentation
Engine Table: Implementation and closely-coupled utilities
/*
* ---------------------------------------------------------------------------------------------------------------------
* AUTO-GENERATED CLASS - DO NOT EDIT MANUALLY - for any changes edit CharEMOperator and regenerate
* ---------------------------------------------------------------------------------------------------------------------
*/
package io.deephaven.engine.table.impl.updateby.em;
import io.deephaven.api.updateby.OperationControl;
import io.deephaven.chunk.ByteChunk;
import io.deephaven.chunk.Chunk;
import io.deephaven.chunk.LongChunk;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.engine.rowset.RowSequence;
import io.deephaven.engine.table.impl.MatchPair;
import io.deephaven.engine.table.impl.locations.TableDataException;
import io.deephaven.engine.table.impl.updateby.UpdateByOperator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static io.deephaven.util.QueryConstants.*;
public class ByteEMOperator extends BasePrimitiveEMOperator {
// region extra-fields
final byte nullValue;
// endregion extra-fields
protected class Context extends BasePrimitiveEMOperator.Context {
public ByteChunk extends Values> byteValueChunk;
@SuppressWarnings("unused")
protected Context(final int affectedChunkSize, final int influencerChunkSize) {
super(affectedChunkSize);
}
@Override
public void setValueChunks(@NotNull final Chunk extends Values>[] valueChunks) {
byteValueChunk = valueChunks[0].asByteChunk();
}
@Override
public void accumulateCumulative(@NotNull RowSequence inputKeys,
Chunk extends Values>[] valueChunkArr,
LongChunk extends Values> tsChunk,
int len) {
setValueChunks(valueChunkArr);
// chunk processing
if (timestampColumnName == null) {
// compute with ticks
for (int ii = 0; ii < len; ii++) {
// read the value from the values chunk
final byte input = byteValueChunk.get(ii);
if (input == nullValue) {
handleBadData(this, true, false);
} else {
if (curVal == NULL_DOUBLE) {
curVal = input;
} else {
curVal = aggFunction.apply(curVal, input, opAlpha, opOneMinusAlpha);
}
}
outputValues.set(ii, curVal);
}
} else {
// compute with time
for (int ii = 0; ii < len; ii++) {
// read the value from the values chunk
final byte input = byteValueChunk.get(ii);
final long timestamp = tsChunk.get(ii);
//noinspection ConstantConditions
final boolean isNull = input == nullValue;
final boolean isNullTime = timestamp == NULL_LONG;
if (isNull) {
handleBadData(this, true, false);
} else if (isNullTime) {
// no change to curVal and lastStamp
} else if (curVal == NULL_DOUBLE) {
// We have a valid input value, we can initialize the output value with it.
curVal = input;
lastStamp = timestamp;
} else {
final long dt = timestamp - lastStamp;
if (dt < 0) {
// negative time deltas are not allowed, throw an exception
throw new TableDataException("Timestamp values in UpdateBy operators must not decrease");
}
if (dt != lastDt) {
// Alpha is dynamic based on time, but only recalculated when needed
alpha = Math.exp(-dt / reverseWindowScaleUnits);
oneMinusAlpha = 1.0 - alpha;
lastDt = dt;
}
curVal = aggFunction.apply(curVal, input, alpha, oneMinusAlpha);
lastStamp = timestamp;
}
outputValues.set(ii, curVal);
}
}
// chunk output to column
writeToOutputColumn(inputKeys);
}
@Override
public boolean isValueValid(long atKey) {
return valueSource.getByte(atKey) != nullValue;
}
@Override
public void push(int pos, int count) {
throw new IllegalStateException("EMAOperator#push() is not used");
}
}
/**
* An operator that computes an EMA from a byte column using an exponential decay function.
*
* @param pair the {@link MatchPair} that defines the input/output for this operation
* @param affectingColumns the names of the columns that affect this ema
* @param control defines how to handle {@code null} input values.
* @param timestampColumnName the name of the column containing timestamps for time-based calcuations
* @param windowScaleUnits the smoothing window for the EMA. If no {@code timestampColumnName} is provided, this is measured in ticks, otherwise it is measured in nanoseconds
*/
public ByteEMOperator(
@NotNull final MatchPair pair,
@NotNull final String[] affectingColumns,
@NotNull final OperationControl control,
@Nullable final String timestampColumnName,
final double windowScaleUnits,
@NotNull final EmFunction aggFunction
// region extra-constructor-args
,final byte nullValue
// endregion extra-constructor-args
) {
super(pair, affectingColumns, control, timestampColumnName, windowScaleUnits, aggFunction);
// region constructor
this.nullValue = nullValue;
// endregion constructor
}
@Override
public UpdateByOperator copy() {
return new ByteEMOperator(
pair,
affectingColumns,
control,
timestampColumnName,
reverseWindowScaleUnits,
aggFunction
// region extra-copy-args
, nullValue
// endregion extra-copy-args
);
}
@NotNull
@Override
public UpdateByOperator.Context makeUpdateContext(final int affectedChunkSize, final int influencerChunkSize) {
return new Context(affectedChunkSize, influencerChunkSize);
}
}