|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | +# ruff: noqa: D103 |
| 3 | + |
| 4 | +"""The ``simple_writable`` fixture's created tables (protocol 3.3 CTAS contract). |
| 5 | +
|
| 6 | +Mirrors what ``vgi/test/sql/integration/simple_writable/ctas*.test`` need from the |
| 7 | +fixture (``docs/writable_tx_fixtures.md``, "simple_writable: created tables"): |
| 8 | +``catalog_table_create`` reports ``created`` (false for IF NOT EXISTS on an |
| 9 | +existing table, which stays unchanged), created tables scan and insert, |
| 10 | +``catalog_table_drop`` removes them, and the write functions are table_only. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import sys |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +import pyarrow as pa |
| 19 | +import pytest |
| 20 | +from vgi_rpc.utils import serialize_record_batch_bytes |
| 21 | + |
| 22 | +from vgi.arguments import Arguments |
| 23 | +from vgi.catalog import FUNCTION_VISIBILITY_TABLE_ONLY, OnConflict, SchemaObjectType, SerializedSchema |
| 24 | +from vgi.client.client import Client |
| 25 | + |
| 26 | +_WORKER = f"{sys.executable} -m vgi._test_fixtures.simple_writable" |
| 27 | +_COLUMNS = pa.schema( |
| 28 | + [ |
| 29 | + ("i", pa.int32()), |
| 30 | + ("b", pa.int64()), |
| 31 | + ("v", pa.string()), |
| 32 | + ("d", pa.float64()), |
| 33 | + ("f", pa.bool_()), |
| 34 | + ] |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def attached() -> Any: |
| 40 | + with Client(_WORKER, pool=None) as client: |
| 41 | + result = client.catalog_attach( |
| 42 | + name="simple_writable", options={}, data_version_spec=None, implementation_version=None |
| 43 | + ) |
| 44 | + with Client(_WORKER, pool=None, attach_opaque_data=result.attach_opaque_data) as client: |
| 45 | + yield client, result.attach_opaque_data |
| 46 | + |
| 47 | + |
| 48 | +def _create(client: Client, attach: Any, name: str, on_conflict: OnConflict, columns: pa.Schema = _COLUMNS) -> bool: |
| 49 | + return client.table_create( |
| 50 | + attach_opaque_data=attach, |
| 51 | + schema_path=["main"], |
| 52 | + name=name, |
| 53 | + columns=SerializedSchema(columns.serialize().to_pybytes()), |
| 54 | + on_conflict=on_conflict, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _insert(client: Client, name: str, rows: list[dict[str, Any]], schema: pa.Schema = _COLUMNS) -> list[Any]: |
| 59 | + options = serialize_record_batch_bytes(pa.RecordBatch.from_pylist([{"result_mode": "rows"}])) |
| 60 | + out = client.table_in_out_function( |
| 61 | + function_name="simple_writable_insert", |
| 62 | + schema_path=["main"], |
| 63 | + input=iter([pa.RecordBatch.from_pylist(rows, schema=schema)]), |
| 64 | + arguments=Arguments(positional=[pa.scalar(name)], named={"write_options": pa.scalar(options)}), |
| 65 | + ) |
| 66 | + return [row for b in out for row in b.to_pylist()] |
| 67 | + |
| 68 | + |
| 69 | +def _scan(client: Client, name: str) -> list[dict[str, Any]]: |
| 70 | + out = client.table_function( |
| 71 | + function_name="simple_writable_scan", schema_path=["main"], arguments=Arguments(positional=[pa.scalar(name)]) |
| 72 | + ) |
| 73 | + return [row for b in out for row in b.to_pylist()] |
| 74 | + |
| 75 | + |
| 76 | +def test_created_flag_and_if_not_exists(attached: Any) -> None: |
| 77 | + client, attach = attached |
| 78 | + version = client.catalog_version(attach_opaque_data=attach) |
| 79 | + assert _create(client, attach, "made", OnConflict.ERROR) is True |
| 80 | + assert client.catalog_version(attach_opaque_data=attach) > version |
| 81 | + row = {"i": 1, "b": 2, "v": "x", "d": 1.5, "f": True} |
| 82 | + assert _insert(client, "made", [row]) == [row] |
| 83 | + assert _scan(client, "made") == [row] |
| 84 | + # IF NOT EXISTS on an existing table: not created, left exactly as it was. |
| 85 | + assert _create(client, attach, "made", OnConflict.IGNORE, pa.schema([("other", pa.int64())])) is False |
| 86 | + assert _scan(client, "made") == [row] |
| 87 | + assert _create(client, attach, "items", OnConflict.IGNORE) is False |
| 88 | + with pytest.raises(Exception, match="already exists"): |
| 89 | + _create(client, attach, "made", OnConflict.ERROR) |
| 90 | + with pytest.raises(Exception, match="already exists"): |
| 91 | + _create(client, attach, "items", OnConflict.ERROR) |
| 92 | + # REPLACE drops the old table and its rows first. |
| 93 | + assert _create(client, attach, "made", OnConflict.REPLACE, pa.schema([("only", pa.int64())])) is True |
| 94 | + assert _scan(client, "made") == [] |
| 95 | + |
| 96 | + |
| 97 | +def test_created_table_is_listed_insert_only_without_rowid(attached: Any) -> None: |
| 98 | + client, attach = attached |
| 99 | + assert _create(client, attach, "Listed", OnConflict.ERROR) is True |
| 100 | + info = client.table_get(attach_opaque_data=attach, schema_path=["main"], name="listed") |
| 101 | + assert info is not None |
| 102 | + assert info.write_result_modes == {"insert": "rows"} |
| 103 | + columns = pa.ipc.read_schema(pa.py_buffer(info.columns)) |
| 104 | + assert columns.names == _COLUMNS.names |
| 105 | + tables = client.schema_contents( |
| 106 | + attach_opaque_data=attach, transaction_opaque_data=None, path=["main"], type=SchemaObjectType.TABLE |
| 107 | + ) |
| 108 | + assert "listed" in {t.name for t in tables} |
| 109 | + |
| 110 | + |
| 111 | +def test_drop_created_table(attached: Any) -> None: |
| 112 | + client, attach = attached |
| 113 | + _create(client, attach, "gone", OnConflict.ERROR) |
| 114 | + client.table_drop(attach_opaque_data=attach, schema_path=["main"], name="gone") |
| 115 | + assert client.table_get(attach_opaque_data=attach, schema_path=["main"], name="gone") is None |
| 116 | + client.table_drop(attach_opaque_data=attach, schema_path=["main"], name="gone", ignore_not_found=True) |
| 117 | + with pytest.raises(Exception, match="does not exist"): |
| 118 | + client.table_drop(attach_opaque_data=attach, schema_path=["main"], name="gone") |
| 119 | + with pytest.raises(Exception, match="cannot be dropped"): |
| 120 | + client.table_drop(attach_opaque_data=attach, schema_path=["main"], name="items") |
| 121 | + |
| 122 | + |
| 123 | +def test_unsupported_column_type_creates_nothing(attached: Any) -> None: |
| 124 | + client, attach = attached |
| 125 | + with pytest.raises(Exception, match="unsupported Arrow type"): |
| 126 | + _create(client, attach, "bad", OnConflict.ERROR, pa.schema([("t", pa.date32())])) |
| 127 | + assert client.table_get(attach_opaque_data=attach, schema_path=["main"], name="bad") is None |
| 128 | + |
| 129 | + |
| 130 | +def test_write_functions_are_table_only(attached: Any) -> None: |
| 131 | + client, attach = attached |
| 132 | + infos = client.schema_contents( |
| 133 | + attach_opaque_data=attach, transaction_opaque_data=None, path=["main"], type=SchemaObjectType.TABLE_FUNCTION |
| 134 | + ) |
| 135 | + visibility = {info.name: info.visibility for info in infos} |
| 136 | + for name in ( |
| 137 | + "simple_writable_insert", |
| 138 | + "simple_writable_update", |
| 139 | + "simple_writable_delete", |
| 140 | + "simple_writable_broken_returning_insert", |
| 141 | + ): |
| 142 | + assert visibility[name] == FUNCTION_VISIBILITY_TABLE_ONLY |
| 143 | + assert visibility["simple_writable_scan"] in (None, "public") |
0 commit comments