Skip to main content
Vane Data / Reference

Tensor functions

Tensor functions construct and inspect TENSOR(dtype, shape) values. Variable-shape tensors store STRUCT(data LIST, shape INTEGER[rank]); fully fixed shapes use a dense ARRAY. tensor() always produces a variable-shape tensor, and tensor_data and tensor_shape require one. See Multimodal data for tensor_type and the storage contract.

Signature

example.py
vane.tensor(data, shape) -> Expression
vane.tensor_data(value) -> Expression
vane.tensor_shape(value) -> Expression
vane.tensor_array(values, dtype) -> pyarrow.ExtensionArray

SQL: tensor(data, shape), tensor_data(value), and tensor_shape(value). tensor_array is Python-only.

Parameters

NameTypeDescriptionDefault
dataValue or ExpressionFlattened row-major elements or a compatible arrayRequired
shapeSequence of nonnegative integers or ExpressionActual per-dimension sizes; dimensions cannot be NULLRequired
valueTENSOR value or ExpressionTensor to inspectRequired
valuesSequence of NumPy arrays or NoneRow values for an Arrow Tensor columnRequired
dtypeVane DuckDBPyTypeComplete variable-shape Tensor type from vane.tensor_type(element_type, shape)Required

None marks a variable dimension only in the tensor_type declaration. Every non-NULL value passed to tensor or tensor_array must have concrete dimensions.

Returns and errors

tensor returns a variable-shape TENSOR whose shape argument determines the rank and the runtime dimensions. tensor_data returns the flattened element LIST and tensor_shape returns INTEGER[rank]; both require a variable-shape tensor and raise for a fixed-shape input. tensor_array returns a validated pyarrow.ExtensionArray carrying the arrow.variable_shape_tensor extension type. NULL inputs return NULL; shape/type mismatches and NULL elements in non-NULL tensors raise.

Example

example.py
import vane


con = vane.connect()
con.sql("SELECT tensor_data(t), tensor_shape(t) FROM (SELECT tensor([1, 2, 3, 4], [2, 2]) AS t)").show()

To build an Arrow column, pass the complete Tensor type, including its shape:

example.py
import numpy as np
import vane


dtype = vane.tensor_type(vane.sqltypes.DOUBLE, [None, 1])
rows = [np.ones((2, 1), dtype=np.float64), np.empty((0, 1), dtype=np.float64), None]
column = vane.tensor_array(rows, dtype)