From 9afb378277df28fdc5b31d5f1ed87ac957bc7ee2 Mon Sep 17 00:00:00 2001 From: hao-xu5 Date: Thu, 1 Oct 2026 13:26:41 -0700 Subject: [PATCH] feat: Add LanceFormat table format with version/tag pinning Extends the existing TableFormat abstraction (#5650) with Lance rather than introducing a separate data source, so Lance is addressed the same way Iceberg, Delta and Hudi already are. Closes part of #6899. LanceFormat carries catalog/namespace addressing plus an optional pin to a dataset version or tag. Because SparkSource already drives its reader generically from table_format.format_type.value and table_format.properties, this works with SparkSource with no changes to it: format_type.value is "lance", and the pin is mirrored into properties as lance.version / lance.tag. version is validated as >= 1 so the Python and proto semantics agree. Lance dataset versions start at 1 and the proto treats 0 as unset, so without that guard version=0 would not round-trip, since to_proto/from_proto read 0 as absent. version and tag are mutually exclusive, because a tag already resolves to a version. Only DataFormat_pb2 is regenerated, using grpcio-tools 1.62.3 so the emitted gencode stays at the 4.25.1 level the other checked-in protos use. Regenerating with the pinned grpcio-tools 1.84.0 instead emits gencode that calls ValidateProtobufRuntimeVersion for protobuf 7.35.1, which would break the declared protobuf>=4.24.0 floor for that one module. DataSource_pb2 is deliberately left untouched. It is already stale against DataSource.proto on master, missing ConnectionRef entries, and regenerating it produces ~125 lines of churn unrelated to this change. Signed-off-by: hao-xu5 --- protos/feast/core/DataFormat.proto | 22 +++ .../feast/protos/feast/core/DataFormat_pb2.py | 50 +++--- .../protos/feast/core/DataFormat_pb2.pyi | 63 +++++++- sdk/python/feast/table_format.py | 146 ++++++++++++++++++ sdk/python/tests/unit/test_table_format.py | 116 ++++++++++++++ 5 files changed, 372 insertions(+), 25 deletions(-) diff --git a/protos/feast/core/DataFormat.proto b/protos/feast/core/DataFormat.proto index 98464fa3c2b..f0503416462 100644 --- a/protos/feast/core/DataFormat.proto +++ b/protos/feast/core/DataFormat.proto @@ -75,11 +75,33 @@ message TableFormat { map properties = 4; } + // Defines options for the Lance table format + message LanceFormat { + // Optional catalog name for the Lance table + string catalog = 1; + + // Optional namespace within the catalog + string namespace = 2; + + // Pin reads to a specific Lance dataset version. Zero means unset. + // Mutually exclusive with tag. + uint64 version = 3; + + // Pin reads to a named Lance tag, which resolves to a version. + // Mutually exclusive with version. + string tag = 4; + + // Additional properties for Lance configuration + // Examples: storage options, index parameters, etc. + map properties = 5; + } + // Specifies the table format and format-specific options oneof format { IcebergFormat iceberg_format = 1; DeltaFormat delta_format = 2; HudiFormat hudi_format = 3; + LanceFormat lance_format = 4; } } diff --git a/sdk/python/feast/protos/feast/core/DataFormat_pb2.py b/sdk/python/feast/protos/feast/core/DataFormat_pb2.py index b90958cb325..aa3cc5351ec 100644 --- a/sdk/python/feast/protos/feast/core/DataFormat_pb2.py +++ b/sdk/python/feast/protos/feast/core/DataFormat_pb2.py @@ -14,7 +14,7 @@ -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1b\x66\x65\x61st/core/DataFormat.proto\x12\nfeast.core\"\xa8\x01\n\nFileFormat\x12>\n\x0eparquet_format\x18\x01 \x01(\x0b\x32$.feast.core.FileFormat.ParquetFormatH\x00\x12?\n\x0c\x64\x65lta_format\x18\x02 \x01(\x0b\x32#.feast.core.TableFormat.DeltaFormatB\x02\x18\x01H\x00\x1a\x0f\n\rParquetFormatB\x08\n\x06\x66ormat\"\xf9\x05\n\x0bTableFormat\x12?\n\x0eiceberg_format\x18\x01 \x01(\x0b\x32%.feast.core.TableFormat.IcebergFormatH\x00\x12;\n\x0c\x64\x65lta_format\x18\x02 \x01(\x0b\x32#.feast.core.TableFormat.DeltaFormatH\x00\x12\x39\n\x0bhudi_format\x18\x03 \x01(\x0b\x32\".feast.core.TableFormat.HudiFormatH\x00\x1a\xb1\x01\n\rIcebergFormat\x12\x0f\n\x07\x63\x61talog\x18\x01 \x01(\t\x12\x11\n\tnamespace\x18\x02 \x01(\t\x12I\n\nproperties\x18\x03 \x03(\x0b\x32\x35.feast.core.TableFormat.IcebergFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a\xa6\x01\n\x0b\x44\x65ltaFormat\x12\x1b\n\x13\x63heckpoint_location\x18\x01 \x01(\t\x12G\n\nproperties\x18\x02 \x03(\x0b\x32\x33.feast.core.TableFormat.DeltaFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a\xc9\x01\n\nHudiFormat\x12\x12\n\ntable_type\x18\x01 \x01(\t\x12\x12\n\nrecord_key\x18\x02 \x01(\t\x12\x18\n\x10precombine_field\x18\x03 \x01(\t\x12\x46\n\nproperties\x18\x04 \x03(\x0b\x32\x32.feast.core.TableFormat.HudiFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x42\x08\n\x06\x66ormat\"\xb7\x02\n\x0cStreamFormat\x12:\n\x0b\x61vro_format\x18\x01 \x01(\x0b\x32#.feast.core.StreamFormat.AvroFormatH\x00\x12<\n\x0cproto_format\x18\x02 \x01(\x0b\x32$.feast.core.StreamFormat.ProtoFormatH\x00\x12:\n\x0bjson_format\x18\x03 \x01(\x0b\x32#.feast.core.StreamFormat.JsonFormatH\x00\x1a!\n\x0bProtoFormat\x12\x12\n\nclass_path\x18\x01 \x01(\t\x1a!\n\nAvroFormat\x12\x13\n\x0bschema_json\x18\x01 \x01(\t\x1a!\n\nJsonFormat\x12\x13\n\x0bschema_json\x18\x01 \x01(\tB\x08\n\x06\x66ormatBT\n\x10\x66\x65\x61st.proto.coreB\x0f\x44\x61taFormatProtoZ/github.com/feast-dev/feast/go/protos/feast/coreb\x06proto3') +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1b\x66\x65\x61st/core/DataFormat.proto\x12\nfeast.core\"\xa8\x01\n\nFileFormat\x12>\n\x0eparquet_format\x18\x01 \x01(\x0b\x32$.feast.core.FileFormat.ParquetFormatH\x00\x12?\n\x0c\x64\x65lta_format\x18\x02 \x01(\x0b\x32#.feast.core.TableFormat.DeltaFormatB\x02\x18\x01H\x00\x1a\x0f\n\rParquetFormatB\x08\n\x06\x66ormat\"\x84\x08\n\x0bTableFormat\x12?\n\x0eiceberg_format\x18\x01 \x01(\x0b\x32%.feast.core.TableFormat.IcebergFormatH\x00\x12;\n\x0c\x64\x65lta_format\x18\x02 \x01(\x0b\x32#.feast.core.TableFormat.DeltaFormatH\x00\x12\x39\n\x0bhudi_format\x18\x03 \x01(\x0b\x32\".feast.core.TableFormat.HudiFormatH\x00\x12;\n\x0clance_format\x18\x04 \x01(\x0b\x32#.feast.core.TableFormat.LanceFormatH\x00\x1a\xb1\x01\n\rIcebergFormat\x12\x0f\n\x07\x63\x61talog\x18\x01 \x01(\t\x12\x11\n\tnamespace\x18\x02 \x01(\t\x12I\n\nproperties\x18\x03 \x03(\x0b\x32\x35.feast.core.TableFormat.IcebergFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a\xa6\x01\n\x0b\x44\x65ltaFormat\x12\x1b\n\x13\x63heckpoint_location\x18\x01 \x01(\t\x12G\n\nproperties\x18\x02 \x03(\x0b\x32\x33.feast.core.TableFormat.DeltaFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a\xc9\x01\n\nHudiFormat\x12\x12\n\ntable_type\x18\x01 \x01(\t\x12\x12\n\nrecord_key\x18\x02 \x01(\t\x12\x18\n\x10precombine_field\x18\x03 \x01(\t\x12\x46\n\nproperties\x18\x04 \x03(\x0b\x32\x32.feast.core.TableFormat.HudiFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a\xcb\x01\n\x0bLanceFormat\x12\x0f\n\x07\x63\x61talog\x18\x01 \x01(\t\x12\x11\n\tnamespace\x18\x02 \x01(\t\x12\x0f\n\x07version\x18\x03 \x01(\x04\x12\x0b\n\x03tag\x18\x04 \x01(\t\x12G\n\nproperties\x18\x05 \x03(\x0b\x32\x33.feast.core.TableFormat.LanceFormat.PropertiesEntry\x1a\x31\n\x0fPropertiesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x42\x08\n\x06\x66ormat\"\xb7\x02\n\x0cStreamFormat\x12:\n\x0b\x61vro_format\x18\x01 \x01(\x0b\x32#.feast.core.StreamFormat.AvroFormatH\x00\x12<\n\x0cproto_format\x18\x02 \x01(\x0b\x32$.feast.core.StreamFormat.ProtoFormatH\x00\x12:\n\x0bjson_format\x18\x03 \x01(\x0b\x32#.feast.core.StreamFormat.JsonFormatH\x00\x1a!\n\x0bProtoFormat\x12\x12\n\nclass_path\x18\x01 \x01(\t\x1a!\n\nAvroFormat\x12\x13\n\x0bschema_json\x18\x01 \x01(\t\x1a!\n\nJsonFormat\x12\x13\n\x0bschema_json\x18\x01 \x01(\tB\x08\n\x06\x66ormatBT\n\x10\x66\x65\x61st.proto.coreB\x0f\x44\x61taFormatProtoZ/github.com/feast-dev/feast/go/protos/feast/coreb\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) @@ -30,30 +30,36 @@ _globals['_TABLEFORMAT_DELTAFORMAT_PROPERTIESENTRY']._serialized_options = b'8\001' _globals['_TABLEFORMAT_HUDIFORMAT_PROPERTIESENTRY']._options = None _globals['_TABLEFORMAT_HUDIFORMAT_PROPERTIESENTRY']._serialized_options = b'8\001' + _globals['_TABLEFORMAT_LANCEFORMAT_PROPERTIESENTRY']._options = None + _globals['_TABLEFORMAT_LANCEFORMAT_PROPERTIESENTRY']._serialized_options = b'8\001' _globals['_FILEFORMAT']._serialized_start=44 _globals['_FILEFORMAT']._serialized_end=212 _globals['_FILEFORMAT_PARQUETFORMAT']._serialized_start=187 _globals['_FILEFORMAT_PARQUETFORMAT']._serialized_end=202 _globals['_TABLEFORMAT']._serialized_start=215 - _globals['_TABLEFORMAT']._serialized_end=976 - _globals['_TABLEFORMAT_ICEBERGFORMAT']._serialized_start=416 - _globals['_TABLEFORMAT_ICEBERGFORMAT']._serialized_end=593 - _globals['_TABLEFORMAT_ICEBERGFORMAT_PROPERTIESENTRY']._serialized_start=544 - _globals['_TABLEFORMAT_ICEBERGFORMAT_PROPERTIESENTRY']._serialized_end=593 - _globals['_TABLEFORMAT_DELTAFORMAT']._serialized_start=596 - _globals['_TABLEFORMAT_DELTAFORMAT']._serialized_end=762 - _globals['_TABLEFORMAT_DELTAFORMAT_PROPERTIESENTRY']._serialized_start=544 - _globals['_TABLEFORMAT_DELTAFORMAT_PROPERTIESENTRY']._serialized_end=593 - _globals['_TABLEFORMAT_HUDIFORMAT']._serialized_start=765 - _globals['_TABLEFORMAT_HUDIFORMAT']._serialized_end=966 - _globals['_TABLEFORMAT_HUDIFORMAT_PROPERTIESENTRY']._serialized_start=544 - _globals['_TABLEFORMAT_HUDIFORMAT_PROPERTIESENTRY']._serialized_end=593 - _globals['_STREAMFORMAT']._serialized_start=979 - _globals['_STREAMFORMAT']._serialized_end=1290 - _globals['_STREAMFORMAT_PROTOFORMAT']._serialized_start=1177 - _globals['_STREAMFORMAT_PROTOFORMAT']._serialized_end=1210 - _globals['_STREAMFORMAT_AVROFORMAT']._serialized_start=1212 - _globals['_STREAMFORMAT_AVROFORMAT']._serialized_end=1245 - _globals['_STREAMFORMAT_JSONFORMAT']._serialized_start=1247 - _globals['_STREAMFORMAT_JSONFORMAT']._serialized_end=1280 + _globals['_TABLEFORMAT']._serialized_end=1243 + _globals['_TABLEFORMAT_ICEBERGFORMAT']._serialized_start=477 + _globals['_TABLEFORMAT_ICEBERGFORMAT']._serialized_end=654 + _globals['_TABLEFORMAT_ICEBERGFORMAT_PROPERTIESENTRY']._serialized_start=605 + _globals['_TABLEFORMAT_ICEBERGFORMAT_PROPERTIESENTRY']._serialized_end=654 + _globals['_TABLEFORMAT_DELTAFORMAT']._serialized_start=657 + _globals['_TABLEFORMAT_DELTAFORMAT']._serialized_end=823 + _globals['_TABLEFORMAT_DELTAFORMAT_PROPERTIESENTRY']._serialized_start=605 + _globals['_TABLEFORMAT_DELTAFORMAT_PROPERTIESENTRY']._serialized_end=654 + _globals['_TABLEFORMAT_HUDIFORMAT']._serialized_start=826 + _globals['_TABLEFORMAT_HUDIFORMAT']._serialized_end=1027 + _globals['_TABLEFORMAT_HUDIFORMAT_PROPERTIESENTRY']._serialized_start=605 + _globals['_TABLEFORMAT_HUDIFORMAT_PROPERTIESENTRY']._serialized_end=654 + _globals['_TABLEFORMAT_LANCEFORMAT']._serialized_start=1030 + _globals['_TABLEFORMAT_LANCEFORMAT']._serialized_end=1233 + _globals['_TABLEFORMAT_LANCEFORMAT_PROPERTIESENTRY']._serialized_start=605 + _globals['_TABLEFORMAT_LANCEFORMAT_PROPERTIESENTRY']._serialized_end=654 + _globals['_STREAMFORMAT']._serialized_start=1246 + _globals['_STREAMFORMAT']._serialized_end=1557 + _globals['_STREAMFORMAT_PROTOFORMAT']._serialized_start=1444 + _globals['_STREAMFORMAT_PROTOFORMAT']._serialized_end=1477 + _globals['_STREAMFORMAT_AVROFORMAT']._serialized_start=1479 + _globals['_STREAMFORMAT_AVROFORMAT']._serialized_end=1512 + _globals['_STREAMFORMAT_JSONFORMAT']._serialized_start=1514 + _globals['_STREAMFORMAT_JSONFORMAT']._serialized_end=1547 # @@protoc_insertion_point(module_scope) diff --git a/sdk/python/feast/protos/feast/core/DataFormat_pb2.pyi b/sdk/python/feast/protos/feast/core/DataFormat_pb2.pyi index 193fb82a776..a92c0fbedeb 100644 --- a/sdk/python/feast/protos/feast/core/DataFormat_pb2.pyi +++ b/sdk/python/feast/protos/feast/core/DataFormat_pb2.pyi @@ -191,25 +191,82 @@ class TableFormat(google.protobuf.message.Message): ) -> None: ... def ClearField(self, field_name: typing_extensions.Literal["precombine_field", b"precombine_field", "properties", b"properties", "record_key", b"record_key", "table_type", b"table_type"]) -> None: ... + class LanceFormat(google.protobuf.message.Message): + """Defines options for the Lance table format""" + + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + class PropertiesEntry(google.protobuf.message.Message): + DESCRIPTOR: google.protobuf.descriptor.Descriptor + + KEY_FIELD_NUMBER: builtins.int + VALUE_FIELD_NUMBER: builtins.int + key: builtins.str + value: builtins.str + def __init__( + self, + *, + key: builtins.str = ..., + value: builtins.str = ..., + ) -> None: ... + def ClearField(self, field_name: typing_extensions.Literal["key", b"key", "value", b"value"]) -> None: ... + + CATALOG_FIELD_NUMBER: builtins.int + NAMESPACE_FIELD_NUMBER: builtins.int + VERSION_FIELD_NUMBER: builtins.int + TAG_FIELD_NUMBER: builtins.int + PROPERTIES_FIELD_NUMBER: builtins.int + catalog: builtins.str + """Optional catalog name for the Lance table""" + namespace: builtins.str + """Optional namespace within the catalog""" + version: builtins.int + """Pin reads to a specific Lance dataset version. Zero means unset. + Mutually exclusive with tag. + """ + tag: builtins.str + """Pin reads to a named Lance tag, which resolves to a version. + Mutually exclusive with version. + """ + @property + def properties(self) -> google.protobuf.internal.containers.ScalarMap[builtins.str, builtins.str]: + """Additional properties for Lance configuration + Examples: storage options, index parameters, etc. + """ + def __init__( + self, + *, + catalog: builtins.str = ..., + namespace: builtins.str = ..., + version: builtins.int = ..., + tag: builtins.str = ..., + properties: collections.abc.Mapping[builtins.str, builtins.str] | None = ..., + ) -> None: ... + def ClearField(self, field_name: typing_extensions.Literal["catalog", b"catalog", "namespace", b"namespace", "properties", b"properties", "tag", b"tag", "version", b"version"]) -> None: ... + ICEBERG_FORMAT_FIELD_NUMBER: builtins.int DELTA_FORMAT_FIELD_NUMBER: builtins.int HUDI_FORMAT_FIELD_NUMBER: builtins.int + LANCE_FORMAT_FIELD_NUMBER: builtins.int @property def iceberg_format(self) -> global___TableFormat.IcebergFormat: ... @property def delta_format(self) -> global___TableFormat.DeltaFormat: ... @property def hudi_format(self) -> global___TableFormat.HudiFormat: ... + @property + def lance_format(self) -> global___TableFormat.LanceFormat: ... def __init__( self, *, iceberg_format: global___TableFormat.IcebergFormat | None = ..., delta_format: global___TableFormat.DeltaFormat | None = ..., hudi_format: global___TableFormat.HudiFormat | None = ..., + lance_format: global___TableFormat.LanceFormat | None = ..., ) -> None: ... - def HasField(self, field_name: typing_extensions.Literal["delta_format", b"delta_format", "format", b"format", "hudi_format", b"hudi_format", "iceberg_format", b"iceberg_format"]) -> builtins.bool: ... - def ClearField(self, field_name: typing_extensions.Literal["delta_format", b"delta_format", "format", b"format", "hudi_format", b"hudi_format", "iceberg_format", b"iceberg_format"]) -> None: ... - def WhichOneof(self, oneof_group: typing_extensions.Literal["format", b"format"]) -> typing_extensions.Literal["iceberg_format", "delta_format", "hudi_format"] | None: ... + def HasField(self, field_name: typing_extensions.Literal["delta_format", b"delta_format", "format", b"format", "hudi_format", b"hudi_format", "iceberg_format", b"iceberg_format", "lance_format", b"lance_format"]) -> builtins.bool: ... + def ClearField(self, field_name: typing_extensions.Literal["delta_format", b"delta_format", "format", b"format", "hudi_format", b"hudi_format", "iceberg_format", b"iceberg_format", "lance_format", b"lance_format"]) -> None: ... + def WhichOneof(self, oneof_group: typing_extensions.Literal["format", b"format"]) -> typing_extensions.Literal["iceberg_format", "delta_format", "hudi_format", "lance_format"] | None: ... global___TableFormat = TableFormat diff --git a/sdk/python/feast/table_format.py b/sdk/python/feast/table_format.py index 829d8a6e19e..66fc8ea6501 100644 --- a/sdk/python/feast/table_format.py +++ b/sdk/python/feast/table_format.py @@ -27,6 +27,7 @@ class TableFormatType(Enum): DELTA = "delta" ICEBERG = "iceberg" HUDI = "hudi" + LANCE = "lance" class TableFormat(ABC): @@ -422,6 +423,145 @@ def from_proto(cls, proto: "TableFormatProto") -> "HudiFormat": ) +class LanceFormat(TableFormat): + """ + Lance table format configuration. + + Lance is a columnar format for embedding and multimodal data that keeps vectors + alongside scalar columns in a single dataset, and carries its own dataset + versions, tags, and indexes. This class provides configuration for addressing a + Lance dataset and, optionally, pinning reads to a specific version. + + Args: + catalog (Optional[str]): Name of the catalog managing the Lance table. + namespace (Optional[str]): Namespace within the catalog where the table lives. + version (Optional[int]): Pin reads to a specific Lance dataset version. + Mutually exclusive with ``tag``. + tag (Optional[str]): Pin reads to a named Lance tag, which resolves to a + version. Mutually exclusive with ``version``. + properties (Optional[Dict[str, str]]): Properties for configuring Lance + storage and read behaviour (e.g. storage options, index parameters). + + Attributes: + catalog (str): The catalog name. + namespace (str): The namespace within the catalog. + version (int): The pinned dataset version, if any. + tag (str): The pinned tag, if any. + properties (Dict[str, str]): Lance configuration properties. + + Raises: + ValueError: If both ``version`` and ``tag`` are provided. + + Examples: + Unpinned, reading the latest version: + + >>> lance_format = LanceFormat(catalog="my_catalog", namespace="my_namespace") + + Pinned to an explicit dataset version: + + >>> lance_format = LanceFormat( + ... catalog="my_catalog", + ... namespace="my_namespace", + ... version=3, + ... ) + + Pinned to a tag, which resolves to a version at read time: + + >>> lance_format = LanceFormat( + ... catalog="my_catalog", + ... namespace="my_namespace", + ... tag="candidate", + ... ) + """ + + def __init__( + self, + catalog: Optional[str] = None, + namespace: Optional[str] = None, + version: Optional[int] = None, + tag: Optional[str] = None, + properties: Optional[Dict[str, str]] = None, + ): + if version is not None and tag: + raise ValueError( + "LanceFormat accepts either version or tag, not both. " + "A tag already resolves to a version." + ) + if version is not None and version < 1: + raise ValueError( + f"LanceFormat version must be >= 1, got {version}. " + "Lance dataset versions start at 1; omit version to read latest." + ) + super().__init__(TableFormatType.LANCE, properties) + self.catalog = catalog + self.namespace = namespace + self.version = version + self.tag = tag + + # Mirror addressing and pin information into properties, matching the + # convention used by the other table formats. + if catalog: + self.properties["lance.catalog"] = catalog + if namespace: + self.properties["lance.namespace"] = namespace + if version is not None: + self.properties["lance.version"] = str(version) + if tag: + self.properties["lance.tag"] = tag + + @property + def is_pinned(self) -> bool: + """Whether this format pins reads to a specific version or tag.""" + return self.version is not None or bool(self.tag) + + def to_dict(self) -> Dict: + return { + "format_type": self.format_type.value, + "catalog": self.catalog, + "namespace": self.namespace, + "version": self.version, + "tag": self.tag, + "properties": self.properties, + } + + @classmethod + def from_dict(cls, data: Dict) -> "LanceFormat": + return cls( + catalog=data.get("catalog"), + namespace=data.get("namespace"), + version=data.get("version"), + tag=data.get("tag"), + properties=data.get("properties", {}), + ) + + def to_proto(self) -> "TableFormatProto": + """Convert to protobuf TableFormat message""" + from feast.protos.feast.core.DataFormat_pb2 import ( + TableFormat as TableFormatProto, + ) + + lance_proto = TableFormatProto.LanceFormat( + catalog=self.catalog or "", + namespace=self.namespace or "", + version=self.version or 0, + tag=self.tag or "", + properties=self.properties, + ) + return TableFormatProto(lance_format=lance_proto) + + @classmethod + def from_proto(cls, proto: "TableFormatProto") -> "LanceFormat": + """Create from protobuf TableFormat message""" + lance_proto = proto.lance_format + return cls( + catalog=lance_proto.catalog if lance_proto.catalog else None, + namespace=lance_proto.namespace if lance_proto.namespace else None, + version=lance_proto.version if lance_proto.version else None, + tag=lance_proto.tag if lance_proto.tag else None, + properties=dict(lance_proto.properties), + ) + + def create_table_format(format_type: TableFormatType, **kwargs) -> TableFormat: """ Factory function to create appropriate TableFormat instance based on type. @@ -462,6 +602,8 @@ def create_table_format(format_type: TableFormatType, **kwargs) -> TableFormat: return DeltaFormat(**kwargs) elif format_type == TableFormatType.HUDI: return HudiFormat(**kwargs) + elif format_type == TableFormatType.LANCE: + return LanceFormat(**kwargs) else: raise ValueError(f"Unknown table format type: {format_type}") @@ -505,6 +647,8 @@ def table_format_from_dict(data: Dict) -> TableFormat: return DeltaFormat.from_dict(data) elif format_type == TableFormatType.HUDI.value: return HudiFormat.from_dict(data) + elif format_type == TableFormatType.LANCE.value: + return LanceFormat.from_dict(data) else: raise ValueError(f"Unknown table format type: {format_type}") @@ -560,5 +704,7 @@ def table_format_from_proto(proto: "TableFormatProto") -> TableFormat: return DeltaFormat.from_proto(proto) elif which_format == "hudi_format": return HudiFormat.from_proto(proto) + elif which_format == "lance_format": + return LanceFormat.from_proto(proto) else: raise ValueError(f"Unknown table format in proto: {which_format}") diff --git a/sdk/python/tests/unit/test_table_format.py b/sdk/python/tests/unit/test_table_format.py index 908d491e940..dd08a54dc5b 100644 --- a/sdk/python/tests/unit/test_table_format.py +++ b/sdk/python/tests/unit/test_table_format.py @@ -6,10 +6,12 @@ DeltaFormat, HudiFormat, IcebergFormat, + LanceFormat, TableFormatType, create_table_format, table_format_from_dict, table_format_from_json, + table_format_from_proto, ) @@ -321,3 +323,117 @@ def test_table_format_with_special_characters(self): restored.properties["special.key"] == "value with spaces & symbols!@#$%^&*()" ) + + +class TestLanceFormat: + """Test LanceFormat, including version/tag pinning.""" + + def test_lance_table_format_creation(self): + """Test LanceFormat creation and properties.""" + lance_format = LanceFormat( + catalog="my_catalog", + namespace="my_namespace", + properties={"storage.block_size": "8192"}, + ) + + assert lance_format.format_type == TableFormatType.LANCE + assert lance_format.catalog == "my_catalog" + assert lance_format.namespace == "my_namespace" + assert lance_format.properties["lance.catalog"] == "my_catalog" + assert lance_format.properties["lance.namespace"] == "my_namespace" + assert lance_format.properties["storage.block_size"] == "8192" + + def test_lance_table_format_minimal(self): + """Test LanceFormat with no arguments.""" + lance_format = LanceFormat() + + assert lance_format.format_type == TableFormatType.LANCE + assert lance_format.catalog is None + assert lance_format.namespace is None + assert lance_format.version is None + assert lance_format.tag is None + assert lance_format.is_pinned is False + assert lance_format.properties == {} + + def test_lance_version_pin(self): + """A version pin is recorded on the field and mirrored into properties.""" + lance_format = LanceFormat(catalog="c", version=3) + + assert lance_format.version == 3 + assert lance_format.tag is None + assert lance_format.is_pinned is True + assert lance_format.properties["lance.version"] == "3" + + def test_lance_tag_pin(self): + """A tag pin is recorded on the field and mirrored into properties.""" + lance_format = LanceFormat(catalog="c", tag="candidate") + + assert lance_format.tag == "candidate" + assert lance_format.version is None + assert lance_format.is_pinned is True + assert lance_format.properties["lance.tag"] == "candidate" + + def test_lance_rejects_version_below_one(self): + """Lance versions start at 1, and the proto treats 0 as unset.""" + with pytest.raises(ValueError, match="version must be >= 1"): + LanceFormat(version=0) + + with pytest.raises(ValueError, match="version must be >= 1"): + LanceFormat(version=-1) + + def test_lance_rejects_version_and_tag_together(self): + """A tag already resolves to a version, so both together is ambiguous.""" + with pytest.raises(ValueError, match="either version or tag"): + LanceFormat(version=1, tag="candidate") + + def test_lance_dict_round_trip(self): + """to_dict/from_dict preserves addressing and pin.""" + original = LanceFormat(catalog="c", namespace="n", version=7) + restored = table_format_from_dict(original.to_dict()) + + assert isinstance(restored, LanceFormat) + assert restored.catalog == "c" + assert restored.namespace == "n" + assert restored.version == 7 + assert restored.tag is None + + def test_lance_json_round_trip(self): + """table_format_from_json preserves a tag pin.""" + original = LanceFormat(catalog="c", tag="candidate") + restored = table_format_from_json(json.dumps(original.to_dict())) + + assert isinstance(restored, LanceFormat) + assert restored.tag == "candidate" + assert restored.version is None + + def test_lance_proto_round_trip(self): + """to_proto/from_proto selects the lance_format oneof and keeps the pin.""" + original = LanceFormat(catalog="c", namespace="n", version=3) + proto = original.to_proto() + + assert proto.WhichOneof("format") == "lance_format" + + restored = table_format_from_proto(proto) + assert isinstance(restored, LanceFormat) + assert restored.catalog == "c" + assert restored.namespace == "n" + assert restored.version == 3 + assert restored.tag is None + + def test_lance_proto_round_trip_unpinned(self): + """An unpinned format must not come back pinned to version 0.""" + restored = table_format_from_proto(LanceFormat(catalog="c").to_proto()) + + assert isinstance(restored, LanceFormat) + assert restored.version is None + assert restored.tag is None + assert restored.is_pinned is False + + def test_lance_factory_function(self): + """create_table_format dispatches to LanceFormat.""" + lance_format = create_table_format( + TableFormatType.LANCE, catalog="c", version=2 + ) + + assert isinstance(lance_format, LanceFormat) + assert lance_format.version == 2