Casting

This guide covers casting in Deephaven. Data type conversion, or casting for short, is the process of changing a value from one data type to another.

In tables, casting is essential to ensure that columns are in the correct format for subsequent operations. It's also vital for optimizing performance and managing memory effectively.

For more information on why casting is important in Python queries, see:

Type casts

A type cast is an expression that converts a value from one data type to another. In Python, type casts use the syntax:

a = 3
b = float(a)

In Deephaven, type casts in query strings ensure that columns convert the data type of a column to another. They use Java syntax.

Python variables

The following query creates a column using a Python variable. The column is automatically converted to the byte type. A type cast is used to convert the variable to a Java primitive int:

from deephaven import empty_table

a = 1

source = empty_table(1).update(
    [
        "BytePi = a",
        "IntPi = (int)a",
    ]
)
source_meta = source.meta_table

Type casting Python variables in query strings has limitations. Unlike Python, where basic data types have arbitrary precision, Java primitives have fixed precision. You cannot cast a value to a type with smaller precision. For example, the following query fails with an error:

from deephaven import empty_table

a = 3.14

source = empty_table(1).update(
    [
        "DoublePi = a",
        "FloatPi = (float)a",
    ]
)
source_meta = source.meta_table
Value: table update operation failed. : Incompatible types; java.lang.Double cannot be converted to float

The Python float type corresponds most closely to the Java double type in terms of precision. When a Python float is used in a query string, Deephaven treats it as a boxed java.lang.Double object until it's written to the table. Since the Deephaven engine lacks complete type information about variable a, it cannot safely downcast it from a double to the lower-precision float type.

Python functions

Python functions called in query strings should generally use type hints rather than type casts. However, type casts still work and can be used when needed.

The following example uses a type cast to convert the returned value of a Python function to a java.lang.String:

Note

The shorthand notation (String) is used below. The full notation is (java.lang.String).

from deephaven import empty_table
from random import choice


def random_symbol():
    return choice(["AAPL", "GOOG", "MSFT"])


source = empty_table(10).update("Symbol = (String)random_symbol()")

Python classes

Unlike Python functions, Python classes used in query strings cannot leverage type hints to specify return types. Instead, you must use explicit type casts to ensure columns have the appropriate data types. The example below demonstrates using type casts with Python class methods and attributes in query strings:

from deephaven import empty_table
import numpy as np


class MyClass:
    my_value = 3

    def __init__(self, x, y):
        self.x = x
        self.y = y

    @classmethod
    def change_value(cls, new_value) -> np.intc:
        MyClass.my_value = new_value
        return new_value

    @staticmethod
    def multiply_modulo(x, y, modulo):
        if modulo == 0:
            return x * y
        return (x % modulo) * (y % modulo)

    def plus_modulo(self, modulo):
        if modulo == 0:
            return self.x + self.y
        return (self.x % modulo) + (self.y % modulo)


my_class = MyClass(15, 6)

source = empty_table(1).update(
    [
        "Q = MyClass.change_value(4)",
        "X = (int)MyClass.change_value(5)",
        "Y = (int)MyClass.multiply_modulo(11, 16, X)",
        "Z = (int)my_class.plus_modulo(X)",
    ]
)
source_meta = source.meta_table