Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column of java.lang.Object type created from calling Py func produces unexpected results when used in ternary expression #5710

Open
jmao-denver opened this issue Jul 3, 2024 · 0 comments
Labels
bug Something isn't working core Core development tasks python python-server-side
Milestone

Comments

@jmao-denver
Copy link
Contributor

jmao-denver commented Jul 3, 2024

@nbauernfeind reported this problem to me. This is what I have found after some some digging. I don't see a simple solution for use cases like this. One thing we can improve is to add to the docs the recommendation to always do type casting for Py functions that aren't return type hinted.

import random

from deephaven import empty_table

T = 3
t = empty_table(10).update(
    formulas=["X = random.randint(1, 5)", "Y = X == T ? null : i"])

t1 = empty_table(10).update(
    formulas=["X = random.randint(1, 5)", "Y = X == 3 ? null : i"])

t2 = empty_table(10).update(
    formulas=["X = (int)random.randint(1, 5)", "Y = X == 3 ? null : i"])
image image

Generated formula code -
working case:

   private final java.lang.Byte T;
   private int applyFormulaPerItem(int i, java.lang.Object X) {
        try {
            return eq(X, T) ? NULL_INT : i;
        } catch (java.lang.Exception __e) {
            throw new io.deephaven.engine.table.impl.select.FormulaEvaluationException("In formula: " + __columnName + " = " + "X == 3 ? null : i", __e);
        }
    }

Why: The call to random.randint returns a Byte object and T is also resolved as a Byte object, which makes eq() happy.

failure case:

   private int applyFormulaPerItem(int i, java.lang.Object X) {
        try {
            return eq(X, 3) ? NULL_INT : i;
        } catch (java.lang.Exception __e) {
            throw new io.deephaven.engine.table.impl.select.FormulaEvaluationException("In formula: " + __columnName + " = " + "X == 3 ? null : i", __e);
        }
    }

cause: The call to random.randint returns a Byte object which fails the eq() test because 3 is parsed as an Integer.

   public static boolean eq(Object obj1, Object obj2) {
        // noinspection SimplifiableBooleanExpression
        return obj1 == obj2 || (!(obj1 == null ^ obj2 == null) && obj1.equals(obj2));
    }
@jmao-denver jmao-denver added bug Something isn't working triage labels Jul 3, 2024
@jmao-denver jmao-denver added this to the 3. Triage milestone Jul 3, 2024
@rcaudy rcaudy modified the milestones: 3. Triage, 0.36.0 Jul 3, 2024
@rcaudy rcaudy added core Core development tasks python python-server-side and removed triage labels Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core Core development tasks python python-server-side
Projects
None yet
Development

No branches or pull requests

2 participants