Changesets · Branches · Tags · Compare
| Changeset | edcc3dc20a6eed3a7c42ce2ff3c58ff6e18f7dfbad4acfc695fd9287c4c158f0 |
|---|---|
| Author | Lei Xu <lei@lancedb.com> · 2024-09-14 11:33 -07:00 |
| Committer | GitHub <noreply@github.com> · 2024-09-14 11:33 -07:00 |
| Parents | 91e3fec7522d |
2 files changed
modified python/python/lancedb/table.py +5 −0
| @@ -94,6 +94,8 @@ | ||
| 94 | 94 | schema = data[0].__class__.to_arrow_schema() |
| 95 | 95 | data = [model_to_dict(d) for d in data] |
| 96 | 96 | return pa.Table.from_pylist(data, schema=schema) |
| 97 | + elif isinstance(data[0], pa.RecordBatch): | |
| 98 | + return pa.Table.from_batches(data, schema=schema) | |
| 97 | 99 | else: |
| 98 | 100 | return pa.Table.from_pylist(data) |
| 99 | 101 | elif isinstance(data, dict): |
| @@ -173,6 +175,9 @@ | ||
| 173 | 175 | on_bad_vectors=on_bad_vectors, |
| 174 | 176 | fill_value=fill_value, |
| 175 | 177 | ) |
| 178 | + else: | |
| 179 | + if schema is not None: | |
| 180 | + data = pa.Table.from_pylist([], schema) | |
| 176 | 181 | if schema is None: |
| 177 | 182 | if data is None: |
| 178 | 183 | raise ValueError("Either data or schema must be provided") |
modified python/python/tests/test_remote_db.py +54 −12
| @@ -1,15 +1,7 @@ | ||
| 1 | -# Copyright 2023 LanceDB Developers | |
| 2 | -# | |
| 3 | -# Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | -# you may not use this file except in compliance with the License. | |
| 5 | -# You may obtain a copy of the License at | |
| 6 | -# http://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | -# | |
| 8 | -# Unless required by applicable law or agreed to in writing, software | |
| 9 | -# distributed under the License is distributed on an "AS IS" BASIS, | |
| 10 | -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 11 | -# See the License for the specific language governing permissions and | |
| 12 | -# limitations under the License. | |
| 1 | +# SPDX-License-Identifier: Apache-2.0 | |
| 2 | +# SPDX-FileCopyrightText: Copyright The LanceDB Authors | |
| 3 | + | |
| 4 | +from unittest.mock import MagicMock | |
| 13 | 5 | |
| 14 | 6 | import lancedb |
| 15 | 7 | import pyarrow as pa |
| @@ -39,3 +31,53 @@ | ||
| 39 | 31 | table = conn["test"] |
| 40 | 32 | table.schema = pa.schema([pa.field("vector", pa.list_(pa.float32(), 2))]) |
| 41 | 33 | table.search([1.0, 2.0]).to_pandas() |
| 34 | + | |
| 35 | + | |
| 36 | +def test_create_empty_table(): | |
| 37 | + client = MagicMock() | |
| 38 | + conn = lancedb.connect("db://client-will-be-injected", api_key="fake") | |
| 39 | + | |
| 40 | + conn._client = client | |
| 41 | + | |
| 42 | + schema = pa.schema([pa.field("vector", pa.list_(pa.float32(), 2))]) | |
| 43 | + | |
| 44 | + client.post.return_value = {"status": "ok"} | |
| 45 | + table = conn.create_table("test", schema=schema) | |
| 46 | + assert table.name == "test" | |
| 47 | + assert client.post.call_args[0][0] == "/v1/table/test/create/" | |
| 48 | + | |
| 49 | + json_schema = { | |
| 50 | + "fields": [ | |
| 51 | + { | |
| 52 | + "name": "vector", | |
| 53 | + "nullable": True, | |
| 54 | + "type": { | |
| 55 | + "type": "fixed_size_list", | |
| 56 | + "fields": [ | |
| 57 | + {"name": "item", "nullable": True, "type": {"type": "float"}} | |
| 58 | + ], | |
| 59 | + "length": 2, | |
| 60 | + }, | |
| 61 | + }, | |
| 62 | + ] | |
| 63 | + } | |
| 64 | + client.post.return_value = {"schema": json_schema} | |
| 65 | + assert table.schema == schema | |
| 66 | + assert client.post.call_args[0][0] == "/v1/table/test/describe/" | |
| 67 | + | |
| 68 | + client.post.return_value = 0 | |
| 69 | + assert table.count_rows(None) == 0 | |
| 70 | + | |
| 71 | + | |
| 72 | +def test_create_table_with_recordbatches(): | |
| 73 | + client = MagicMock() | |
| 74 | + conn = lancedb.connect("db://client-will-be-injected", api_key="fake") | |
| 75 | + | |
| 76 | + conn._client = client | |
| 77 | + | |
| 78 | + batch = pa.RecordBatch.from_arrays([pa.array([[1.0, 2.0], [3.0, 4.0]])], ["vector"]) | |
| 79 | + | |
| 80 | + client.post.return_value = {"status": "ok"} | |
| 81 | + table = conn.create_table("test", [batch], schema=batch.schema) | |
| 82 | + assert table.name == "test" | |
| 83 | + assert client.post.call_args[0][0] == "/v1/table/test/create/" |