@@ -61,3 +61,56 @@ def test_create_namespace_if_already_existing(catalog: RestCatalog) -> None:
6161 catalog .create_namespace_if_not_exists (TEST_NAMESPACE_IDENTIFIER )
6262
6363 assert catalog .namespace_exists (TEST_NAMESPACE_IDENTIFIER )
64+
65+
66+ @pytest .mark .integration
67+ @pytest .mark .parametrize ("catalog" , [pytest .lazy_fixture ("session_catalog" )])
68+ def test_create_table_invalid_sort_order (catalog : RestCatalog ) -> None :
69+ from pyiceberg .schema import Schema
70+ from pyiceberg .types import LongType , NestedField
71+ from pyiceberg .catalog .rest import Endpoints
72+
73+ namespace = "default"
74+ table_name = "test_invalid_sort_order"
75+
76+ # Ensure namespace exists
77+ catalog .create_namespace_if_not_exists (namespace )
78+
79+ # Define a simple schema
80+ schema = Schema (NestedField (1 , "x" , LongType ()))
81+
82+ # Create the payload manually to bypass client-side validation
83+ # We want a sort order that references a non-existent field ID (e.g., 9999)
84+ payload = {
85+ "name" : table_name ,
86+ "schema" : schema .model_dump (by_alias = True ),
87+ "write-order" : {
88+ "order-id" : 1 ,
89+ "fields" : [
90+ {
91+ "transform" : "identity" ,
92+ "source-id" : 9999 , # Non-existent field
93+ "direction" : "asc" ,
94+ "null-order" : "nulls-first" ,
95+ }
96+ ],
97+ },
98+ "stage-create" : False ,
99+ }
100+
101+ # Send the request using the underlying session
102+ # Endpoints.create_table is "namespaces/{namespace}/tables"
103+ url = catalog .url (Endpoints .create_table , namespace = namespace )
104+
105+ response = catalog ._session .post (url , json = payload )
106+
107+ assert response .status_code == 400
108+ response_json = response .json ()
109+ response_json ["error" ].pop ("stack" , None )
110+ assert response_json == {
111+ "error" : {
112+ "message" : "Cannot find source column for sort field: identity(9999) ASC NULLS FIRST" ,
113+ "type" : "ValidationException" ,
114+ "code" : 400 ,
115+ }
116+ }
0 commit comments