All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deephaven.engine.table.impl.select.python.FormulaKernelPythonSingularFunction Maven / Gradle / Ivy

There is a newer version: 0.37.1
Show newest version
/**
 * Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
 */
package io.deephaven.engine.table.impl.select.python;

import io.deephaven.engine.table.impl.select.Formula.FillContext;
import io.deephaven.engine.table.impl.select.FormulaKernelTypedBase;
import io.deephaven.engine.table.impl.select.formula.FormulaKernel;
import io.deephaven.chunk.attributes.Values;
import io.deephaven.chunk.*;
import org.jpy.PyObject;

import java.util.Objects;

/**
 * A python formula kernel which is implemented by iterating over the input chunks and calling the python function N
 * times.
 *
 * @see FormulaKernelPythonChunkedFunction
 */
class FormulaKernelPythonSingularFunction extends FormulaKernelTypedBase implements FormulaKernel {

    private static final String CALL_METHOD = "__call__";

    private final PyObject function;

    FormulaKernelPythonSingularFunction(PyObject function) {
        this.function = Objects.requireNonNull(function, "function");
    }


    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableByteChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final byte output = function
                    .call(byte.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableBooleanChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final boolean output = function
                    .call(boolean.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableCharChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final char output = function
                    .call(char.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableShortChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final short output = function
                    .call(short.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableIntChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final int output = function
                    .call(int.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableLongChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final long output = function
                    .call(long.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableFloatChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final float output = function
                    .call(float.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public void applyFormulaChunk(
            FillContext __context,
            WritableDoubleChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            final double output = function
                    .call(double.class, CALL_METHOD, types, ArgumentsSingular.buildArguments(__sources, i));
            __destination.set(i, output);
        }
    }

    @Override
    public  void applyFormulaChunk(
            FillContext __context,
            WritableObjectChunk __destination,
            Chunk[] __sources) {
        final Class[] types = ArgumentsSingular.buildParamTypes(__sources);
        final int L = __destination.size();
        for (int i = 0; i < L; i++) {
            // this is LESS THAN IDEAL - it would be much better if ObjectChunk would be able to return
            // the non-array type
            final Object output = function
                    .call(Object.class, CALL_METHOD, types, ArgumentsSingular
                            .buildArguments(__sources, i));

            // noinspection unchecked
            __destination.set(i, (T) output);
        }
    }

    @Override
    public FillContext makeFillContext(int __chunkCapacity) {
        return FillContextPython.EMPTY;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy