Use objects
This guide will show you how to work with objects in your query strings.
When performing complex analyses, objects are an invaluable tool. Objects can contain related data and provide an easy way to access data from one source to make your program more logical or streamlined.
The Deephaven Query Language natively supports objects in Python and Groovy, allowing users to pass in objects, object's fields, and object's methods into query strings.
Power of objects in code
Objects are designed to hold information or values. In the following example, operators are used with objects to assign values.
Here, we have two objects and each object holds two values and a custom method. When we call that object, those specific values and methods are utilized without having to pass extra parameters.
class SimpleObj {
public int a, b
SimpleObj(int a, int b){
this.a = a
this.b = b
}
int getA() {
return this.a
}
int getB() {
return this.b
}
int compute() {
return getA() + getB()
}
}
class OtherObj {
public int a, b
OtherObj(int a, int b){
this.a = a
this.b = b
}
int getA() {
return this.a
}
int getB() {
return this.b
}
int compute() {
return 2 * getA() + 2 * getB()
}
}
obj1 = new SimpleObj(1, 2)
obj2 = new OtherObj(3, 4)
result = emptyTable(5).update("X = obj1.getA()", "Y = obj1.compute()", "M = obj2.getA()", "N = obj2.compute()")
- result
Poorly written code
If we didn't use objects, our code could get confusing and cumbersome.
In the following example, we do a similar operation as above, but without the power of objects. Notice that the compute methods require us to track the parameters, and pass them in every time we need to perform these operations.
compute1 = { int valueA, int valueB -> (valueA + valueB) }
compute2 = { int valueA, int valueB -> (2 * valueA + 2 * valueB) }
a1 = 1
b1 = 2
a2 = 3
b2 = 4
result = emptyTable(5).update("X = a1", "Y = compute1(a1, b1)", "M = a2", "N = compute2(a2, b2)")
- result
Use the power of objects in the Deephaven Query Language to make your queries more powerful and concise.