Operations¶
Operators on Objects build new Objects. They are lazy: an expression like
a + b records the operation but issues no query. ClickHouse runs the
computation only when you call .data() (or another terminal method). That
lets you chain several steps and have them execute as a single query.
Each operation below uses two source Objects created with aai_id=True, which
gives each its own table so they can be combined.
Arithmetic¶
The standard arithmetic operators work element-wise:
obj_a = await create_object_from_value([10.0, 20.0, 30.0], aai_id=True)
obj_b = await create_object_from_value([2.0, 4.0, 5.0], aai_id=True)
print(f"Created {obj_a}")
print(f"Values in a: {await obj_a.data()}\n") # → [10.0, 20.0, 30.0]
print(f"Created {obj_b}")
print(f"Values in b: {await obj_b.data()}") # → [2.0, 4.0, 5.0]
# All arithmetic operators
print("\n" + "-" * 50)
# Addition
result_add = obj_a + obj_b
print(f"Addition (a + b): {await result_add.data()}") # → [12.0, 24.0, 35.0]
# Subtraction
result_sub = obj_a - obj_b
print(f"Subtraction (a - b): {await result_sub.data()}") # → [8.0, 16.0, 25.0]
# Multiplication
result_mul = obj_a * obj_b
print(f"Multiplication (a * b): {await result_mul.data()}") # → [20.0, 80.0, 150.0]
# Division
result_div = obj_a / obj_b
print(f"Division (a / b): {await result_div.data()}") # → [5.0, 5.0, 6.0]
# Floor Division
result_floordiv = obj_a // obj_b
print(f"Floor Division (a // b): {await result_floordiv.data()}") # → [5.0, 5.0, 6.0]
# Modulo
result_mod = obj_a % obj_b
print(f"Modulo (a % b): {await result_mod.data()}") # → [0.0, 0.0, 0.0]
# Power
result_pow = obj_a**obj_b
print(f"Power (a ** b): {await result_pow.data()}") # → [100.0, 160000.0, 24300000.0]
Comparison¶
Comparisons return a UInt8 column of 0/1 per row:
obj_x = await create_object_from_value([1, 5, 10, 15], aai_id=True)
obj_y = await create_object_from_value([5, 5, 8, 20], aai_id=True)
print(f"Values in x: {await obj_x.data()}") # → [1, 5, 10, 15]
print(f"Values in y: {await obj_y.data()}\n") # → [5, 5, 8, 20]
# Equal
result_eq = obj_x == obj_y
print(f"Equal (x == y): {await result_eq.data()}") # → [0, 1, 0, 0]
# Not Equal
result_ne = obj_x != obj_y
print(f"Not Equal (x != y): {await result_ne.data()}") # → [1, 0, 1, 1]
# Less Than
result_lt = obj_x < obj_y
print(f"Less Than (x < y): {await result_lt.data()}") # → [1, 0, 0, 1]
# Less or Equal
result_le = obj_x <= obj_y
print(f"Less or Equal (x <= y): {await result_le.data()}") # → [1, 1, 0, 1]
# Greater Than
result_gt = obj_x > obj_y
print(f"Greater Than (x > y): {await result_gt.data()}") # → [0, 0, 1, 0]
# Greater or Equal
result_ge = obj_x >= obj_y
print(f"Greater or Equal (x >= y): {await result_ge.data()}") # → [0, 1, 1, 0]
Bitwise¶
Bitwise operators apply per element on integer columns:
obj_m = await create_object_from_value([12, 10, 8], aai_id=True) # Binary: 1100, 1010, 1000
obj_n = await create_object_from_value([10, 12, 4], aai_id=True) # Binary: 1010, 1100, 0100
print(f"Values in m: {await obj_m.data()}") # → [12, 10, 8]
print(f"Values in n: {await obj_n.data()}\n") # → [10, 12, 4]
# Bitwise AND
result_and = obj_m & obj_n
print(f"Bitwise AND (m & n): {await result_and.data()}") # → [8, 8, 0]
# Bitwise OR
result_or = obj_m | obj_n
print(f"Bitwise OR (m | n): {await result_or.data()}") # → [14, 14, 12]
# Bitwise XOR
result_xor = obj_m ^ obj_n
print(f"Bitwise XOR (m ^ n): {await result_xor.data()}") # → [6, 6, 12]
Next¶
Aggregations → — reduce an Object to a single value.
See Also¶
- Object API — operators, aggregations, views
- Examples: Basic Operators — the complete runnable script