diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 194c48bd7..e37cb2edc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -830,6 +830,8 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 60 needs: [configuration, native-wheel, native-test-ops] + services: + postgres: *postgres-service env: CELL: ref-pandas3 CELL_PINS: pandas==3.0.6 pyarrow==25.0.1 numpy==2.5.3 @@ -872,6 +874,9 @@ jobs: "$RUNNER_TEMP/kit/bin/python" -I -c 'import sdm_client._native as n; assert n.TEST_OPS' echo "SDM_KIT_PYTHON=$RUNNER_TEMP/kit/bin/python" >> "$GITHUB_ENV" - name: The runner's controls and the route tests + env: + SDM_POSTGRES_TEST_DSN: postgresql://postgres:postgres@localhost:5432/sdm_test + SDM_REQUIRE_POSTGRES: '1' run: python -m pytest contract/tests -q -m 'not tooling' -n auto --dist loadfile --timeout=600 --max-worker-restart=0 # Observe the installed native client in each supported dependency cell. - &contract-gate diff --git a/contract/fixtures/maintained/README.md b/contract/fixtures/maintained/README.md index 7dd149825..fb4483949 100644 --- a/contract/fixtures/maintained/README.md +++ b/contract/fixtures/maintained/README.md @@ -7,8 +7,6 @@ These fixtures hold independent expected facts for current correctness tests. Tests need only this checkout; historical clients, archives and recording tools are not dependencies. -- `label-expectations.json` contains only the four label cases' expected context rows and counts. - Write-guard and provider-layout tests exercise these facts without loading full driver result logs. - `snowflake.json` contains the statements and result references for the 14 cases used by the public Python Snowflake tests. `snowflake/` shares identical Arrow IPC streams by their SHA-256 names. Original driver Arrow bytes are preserved, including their types and metadata. @@ -18,13 +16,6 @@ Tests need only this checkout; historical clients, archives and recording tools - `consumers.json` names the current consumers and reviewed byte limits for large retained fixtures. New oversized fixtures require an explicit entry and explanation. -Eight fixtures elsewhere under `contract/` use `sdm-shared-fixture/1`. -Named fields have dictionaries of literal values; ordered rows reference those dictionaries, with `-1` for an absent field. -Nested tables can reference only earlier tables. -This shares repeated inputs and expected results while preserving every case, its order, nulls and JSON types. -The expanded SHA-256 binds the complete original logical JSON document. -The Python and Rust test helpers expand these literals without calling product code to compute expectations. - The original extraction tool and recording-completeness tests were migration-only and have been removed. Git history retains their implementation and original fixtures. Maintain expected values from documented behavior and independently calculated examples; do not regenerate expected answers from the product under test. diff --git a/contract/tests/test_column_name_diagnostics.py b/contract/tests/test_column_name_diagnostics.py index ac91cf81a..b0bd234c8 100644 --- a/contract/tests/test_column_name_diagnostics.py +++ b/contract/tests/test_column_name_diagnostics.py @@ -37,6 +37,27 @@ def _module(name: str, path: Path) -> ModuleType: ROOT / 'contract/support/column_name_scenarios.py', ) +MESSAGES = { + 'task': ( + "[INVALID_REQUEST] task='regression' settles what a single-table " + 'question is asking, which is the only route that has to decide. A ' + 'relational query resolves to its own kind, so state it in the ' + 'query rather than beside it' + ), + 'typo': ( + "[INVALID_REQUEST] '' is not a query a relational model can " + "answer: row 1, column 12: Column 'pricee' does not exist in table " + "'orders'. Please be mindful of case sensitivity and ensure that the " + 'name is spelled correctly.' + ), + 'parts': ( + '[INVALID_REQUEST] 2 forecast steps means 2 calls, one for each, ' + 'because the model has no task that answers them together. That is ' + 'past the 1 this will do without being told to; ask about fewer, or ' + 'raise sdm_client.decompose.MOST_PARTS if the wait is what you want.' + ), +} + def reachable(database: str) -> bool: if database != 'postgres' or os.environ.get('SDM_POSTGRES_TEST_DSN'): @@ -59,25 +80,6 @@ def test_column_names_preserve_actionable_diagnostics(vector) -> None: 'StructuredDataError', 'INVALID_REQUEST', ) - fragments = { - 'task': [ - "task='regression'", - 'single-table', - 'relational query', - 'state it in the query', - ], - 'typo': [ - vector['pql'], - "Column 'pricee' does not exist in table 'orders'", - 'case sensitivity', - 'spelled correctly', - ], - 'parts': [ - '2 forecast steps means 2 calls', - 'past the 1 this will do', - 'ask about fewer', - 'raise sdm_client.decompose.MOST_PARTS', - ], - } - for fragment in fragments[vector['kind']]: - assert fragment in got['message'] + assert got['message'] == MESSAGES[vector['kind']].replace( + '', vector['pql'] + ) diff --git a/contract/tests/test_decimal_semantic_types.py b/contract/tests/test_decimal_semantic_types.py index 31b0541e8..a4294235d 100644 --- a/contract/tests/test_decimal_semantic_types.py +++ b/contract/tests/test_decimal_semantic_types.py @@ -51,15 +51,6 @@ def case() -> tuple[dict[str, Any], str]: return loaded, digest -def column_d(line: dict[str, Any]) -> dict[str, Any]: - r"""``orders.d`` in a line's public schema.""" - [orders] = [ - t for t in line['public']['schema']['tables'] if t['name'] == 'orders' - ] - [d] = [c for c in orders['columns'] if c['name'] == 'd'] - return d - - def test_the_case_is_a_scaled_decimal_past_float64() -> None: r"""The case is what it names: one setup statement adding ``orders.d``, a ``DECIMAL(38, 18)`` stepping by 1e-18 over the shop base's 80 orders, @@ -129,10 +120,43 @@ def test_decimal_column_has_connector_specific_semantic_type( assert 'describe_source' in line['identity']['operations'] assert line['outcome']['result'] == 'success' assert line['outcome']['stage'] == 'from_source' - expected = ( - {'dtype': 'string', 'stype': 'text'} - if database == 'sqlite' - else {'dtype': 'float64', 'stype': 'categorical'} - ) - column = column_d(line) - assert {name: column[name] for name in expected} == expected + sqlite = database == 'sqlite' + + def column(name: str, dtype: str, stype: str) -> dict[str, str]: + return {'dtype': dtype, 'name': name, 'stype': stype} + + assert line['public']['schema'] == { + 'links': [['orders', 'user_id', 'users', 'user_id']], + 'tables': [ + { + 'columns': [ + column('order_id', 'int64', 'ID'), + column('user_id', 'int64', 'ID'), + column('price', 'float64', 'numerical'), + column( + 'ts', + 'string' if sqlite else 'timestamp[us]', + 'timestamp', + ), + column('d', 'string', 'text') + if sqlite + else column('d', 'float64', 'categorical'), + ], + 'duplicate_keys': 0, + 'name': 'orders', + 'primary_key': 'order_id', + 'time_column': 'ts', + }, + { + 'columns': [ + column('user_id', 'int64', 'ID'), + column('age', 'int64', 'numerical'), + column('country', 'string', 'categorical'), + ], + 'duplicate_keys': 0, + 'name': 'users', + 'primary_key': 'user_id', + 'time_column': None, + }, + ], + } diff --git a/contract/tests/test_public_error_messages.py b/contract/tests/test_public_error_messages.py index c363d4752..97ff11959 100644 --- a/contract/tests/test_public_error_messages.py +++ b/contract/tests/test_public_error_messages.py @@ -294,13 +294,13 @@ def test_public_refusal_names_the_invalid_input(case_key: str) -> None: 'INVALID_REQUEST', ) required_words = ['users.d', 'type string', 'constant', 'quotes'] - elif case_key == 'key.uint64-above-int64-max@sqlite': - # The maintained SQLite path now handles this input successfully. + elif case_key in { + 'key.uint64-above-int64-max@sqlite', + 'key.uint64-above-int64-max@postgres', + }: + # SQLite and PostgreSQL read these keys as exact text and succeed. assert _outcome(got['outcome']) == ('success', 'predict', None, None) return - elif case_key == 'key.uint64-above-int64-max@postgres': - expected = ('refusal', 'from_source', 'OverflowError', None) - required_words = ['int'] assert same_outcome(expected, _outcome(got['outcome'])), case_key said = _words(got['message']) assert not said.startswith(GENERIC), ( @@ -349,24 +349,33 @@ def test_public_warnings_name_source_bounds_and_index_requirements( assert len(warnings) == count, (case_key, label, got['warnings']) if not count: return - message = warnings[0][1] - if label == 'context-before-data': - bounds = ( - ('2023-12-03', '2024-01-01') - if case_id == 'shape.star.customers' - else ('2022-12-31', '2023-01-01') - ) - assert all(bound in message for bound in bounds), message - elif label == 'anchor-after-data': - assert '2030-01-01' in message and '2024-07-07' in message - elif label == 'missing-indices': - counts = ('7', '4') if case_id == 'shape.star.customers' else ('3', '2') - assert ( - f'Missing {counts[0]} indices in {counts[1]} tables' in message - ), message - assert 'primary' in message and 'foreign' in message, message - else: - assert message == 'overflow encountered in cast' + star = case_id == 'shape.star.customers' + expected_message = { + 'context-before-data': ( + 'Context anchor timestamp is too early or aggregation time range ' + 'is too large. To form proper input data, we would need data back ' + "to '{}', however, your data only contains data back to '{}'." + ).format( + *( + ('2023-12-03 00:00:00', '2024-01-01 00:00:00') + if star + else ('2022-12-31 00:00:00', '2023-01-01 00:00:00') + ) + ), + 'anchor-after-data': ( + "Anchor timestamp '2030-01-01 00:00:00' is after the latest " + "timestamp '2024-07-07 00:00:00' in the data. Please make sure " + 'this is intended.' + ), + 'missing-indices': ( + 'Missing {} indices in {} tables for optimal database querying. ' + 'For improving runtime, we strongly suggest to create indices for ' + 'primary and foreign keys, e.g., automatically by instantiating ' + 'Kumo Relational via `KumoRelational(graph, optimize=True)`.' + ).format(*((7, 4) if star else (3, 2))), + 'runtime-warning': 'overflow encountered in cast', + }[label] + assert warnings[0][1] == expected_message @pytest.mark.parametrize('name', sorted(INFINITY_SHOWN)) @@ -383,8 +392,14 @@ def test_infinite_timestamp_bounds_have_actionable_warning(name: str) -> None: warning for warning in got['warnings'] if 'time column' in warning[1] ] assert len(guessed) == 1 - assert guessed[0][0] == 'SDMWarning' - assert INFINITY_SHOWN[name] in guessed[0][1] + assert guessed[0] == [ + 'SDMWarning', + 'Some time columns were inferred, or refused, on a guess about what ' + 'a column holds:\n- events.f was not taken as the time column because ' + f'every row that holds a value holds {INFINITY_SHOWN[name]}. If it ' + 'does say when a row became true, name it: ' + "time_columns={'events': 'f'}.", + ] def test_diagnostic_cases_and_connector_selectors_are_complete() -> None: diff --git a/rust/crates/sdm-core/tests/view_words.rs b/rust/crates/sdm-core/tests/view_words.rs index dbd743c77..b2fbd064a 100644 --- a/rust/crates/sdm-core/tests/view_words.rs +++ b/rust/crates/sdm-core/tests/view_words.rs @@ -216,6 +216,93 @@ fn unsafe_text_and_untrusted_calls_are_worded_by_name() { ); } +#[test] +fn every_expression_refusal_reason_keeps_its_wording() { + for (expr, dialect, expected) in [ + ( + "\u{1c}select\u{1c}1", + None, + "it contains the SQL keyword 'select'", + ), + ("x --", None, "it contains '--'"), + ( + "X FROM PAYROLL //", + Some(ExprDialect::Snowflake), + "it contains '//'", + ), + ("x)", None, "it closes a '(' it never opened"), + ("(x", None, "it leaves a '(' unclosed"), + ( + "a$$ + secret(x) + b$$", + None, + "it leaves '$$' unpaired, so which part of it is quoted text cannot be established", + ), + ( + "$$x$$", + Some(ExprDialect::Databricks), + "it contains '$$', which Databricks SQL does not quote with", + ), + ( + "\"MY\\COL\" * 2", + Some(ExprDialect::Snowflake), + "it has a backslash inside a double-quoted name", + ), + ] { + let reason = view_expr::unsafe_reason(expr, dialect, spaces()) + .map(|r| view_words::expr_reason(&r, dialect_words(&r))); + assert_eq!(reason.as_deref(), Some(expected), "{expr:?}"); + } + let wide = ["a", "col", "d", "name", "order_date", "x", "y", "z"]; + for (expr, dialect, columns, expected) in [ + ( + "cat.sch.f(x)", + ExprDialect::Databricks, + &wide[..], + "it calls the qualified function 'f'", + ), + ( + "`f`(x)", + ExprDialect::Databricks, + &wide[..], + "it calls a function by a quoted name", + ), + ( + "a[0](x)", + ExprDialect::Databricks, + &wide[..], + "it calls the result of an expression", + ), + ( + "identifier(x)", + ExprDialect::Databricks, + &["x"][..], + "it names a function through IDENTIFIER()", + ), + ( + "user", + ExprDialect::Databricks, + &["x"][..], + "it reads the session value 'user'", + ), + ( + "`user`", + ExprDialect::Databricks, + &["x"][..], + "it reads the session value `user`", + ), + ( + "\"user\"", + ExprDialect::Snowflake, + &["x"][..], + "it reads the session value \"user\"", + ), + ] { + let reason = view_expr::untrusted_call_reason(expr, dialect, columns, spaces()) + .map(|r| view_words::expr_reason(&r, dialect_words(&r))); + assert_eq!(reason.as_deref(), Some(expected), "{expr:?}"); + } +} + #[test] fn yaml_defaults_have_explicit_values_and_refusals() { for (kind, input, expected) in [ diff --git a/rust/crates/sdm-data/tests/databricks_plans.rs b/rust/crates/sdm-data/tests/databricks_plans.rs index 432bc6e55..39443c2d2 100644 --- a/rust/crates/sdm-data/tests/databricks_plans.rs +++ b/rust/crates/sdm-data/tests/databricks_plans.rs @@ -127,12 +127,30 @@ fn qualified_identifiers_preserve_each_part_and_escape_backticks() { #[test] fn keyed_reads_unpack_the_bound_json_parameter_by_position() { - for kind in ["lookup", "neighbors"] { + let cte = |columns: &str, element: &str| { + format!( + "WITH TMP AS (\n SELECT CAST(pos AS BIGINT) AS `batch`, {columns}\n FROM posexplode(from_json(?, 'array<{element}>'))\n)\n" + ) + }; + let cases = [ + ("lookup", "int64", cte("col AS `key`", "bigint")), + ("lookup", "float64", cte("col AS `key`", "double")), + ("lookup", "utf8", cte("col AS `key`", "string")), + ( + "neighbors", + "int64", + cte( + "col.id AS `key`, col.e AS `end`", + "struct", + ), + ), + ]; + for (kind, key_type, expected) in cases { let mut input = serde_json::json!({ "request":kind,"table":{"source":["main","shop","users"]}, "key":["id"],"columns":["id","at"], "seeds":{"attachment":7,"columns":[ - {"name":"key","nullable":false,"type":{"type":"int64"}}, + {"name":"key","nullable":false,"type":{"type":key_type}}, {"name":"batch","nullable":false,"type":{"type":"int64"}} ]} }); @@ -157,11 +175,10 @@ fn keyed_reads_unpack_the_bound_json_parameter_by_position() { assert_eq!(bound.len(), 1); let statement = bound[0]; assert!( - statement.text.contains("posexplode(from_json(?, 'array<"), - "{}", + statement.text.starts_with(&expected), + "{kind} {key_type}: {}", statement.text ); - assert!(statement.text.contains("key") && statement.text.contains("batch")); let SqlParam::KeySet { keys, .. } = &statement.params[0] else { panic!("key parameter") }; diff --git a/rust/crates/sdm-data/tests/view_expr.rs b/rust/crates/sdm-data/tests/view_expr.rs index 9cf22ec33..b8904ab5d 100644 --- a/rust/crates/sdm-data/tests/view_expr.rs +++ b/rust/crates/sdm-data/tests/view_expr.rs @@ -87,7 +87,20 @@ fn pure_calls_are_allowed_but_user_code_and_session_values_require_trust() { "secret('scope', 'key')", "custom_function(x)", "db.custom_function(x)", + "current_catalog", + "current_database", + "current_date", + "current_path", + "current_role", + "current_schema", + "current_time", + "current_timestamp", "current_user", + "localtime", + "localtimestamp", + "session_user", + "user", + "x + CURRENT_USER", ] { let verdict = view_expr::expression_verdict(expr, dialect, false, &["x"], spaces()).unwrap(); diff --git a/rust/crates/sdm-testkit/tests/snowflake.rs b/rust/crates/sdm-testkit/tests/snowflake.rs index 0b3c63005..f62743d6d 100644 --- a/rust/crates/sdm-testkit/tests/snowflake.rs +++ b/rust/crates/sdm-testkit/tests/snowflake.rs @@ -1194,6 +1194,10 @@ fn assert_bound_payload(sent: &[(String, String, Vec)], payload: &str) - let sql = sent[0].0.split_whitespace().collect::>().join(" "); assert!(sql.contains("f.index as __KUMO_BATCH__")); assert!(sql.contains("FLATTEN(INPUT => PARSE_JSON(?))")); + assert!( + sql.contains("FROM TMP JOIN \"{catalog}\".\"{schema}\"."), + "{sql}" + ); assert!(sql.contains("ON \"user_id\" = TMP.__KUMO_ID__")); sql } @@ -1313,6 +1317,15 @@ fn lookup_preserves_seed_identity_and_duplicate_policy() { sql.contains("COUNT(*) OVER (PARTITION BY TMP.__KUMO_BATCH__) AS \"__kumo_matches__\""), count ); + if !qualify && !count { + assert_eq!( + sql, + "WITH TMP as ( SELECT f.index as __KUMO_BATCH__, f.value::NUMBER as __KUMO_ID__ \ + FROM TABLE(FLATTEN(INPUT => PARSE_JSON(?))) f ) \ + SELECT TMP.__KUMO_BATCH__ as __KUMO_BATCH__, \"user_id\", \"age\", \"country\" \ + FROM TMP JOIN \"{catalog}\".\"{schema}\".\"users\" ON \"user_id\" = TMP.__KUMO_ID__" + ); + } } } diff --git a/rust/testdata/fixture_tables.rs b/rust/testdata/fixture_tables.rs deleted file mode 100644 index ae394324a..000000000 --- a/rust/testdata/fixture_tables.rs +++ /dev/null @@ -1,70 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. -// All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -//! Decode shared literal fixture fields. No product code computes expectations. -use serde_json::{Map, Value}; - -pub fn read(text: &str) -> Value { - let file: Value = serde_json::from_str(text).unwrap(); - if file["format"] != "sdm-shared-fixture/1" { - return file; - } - let tables = file["tables"].as_array().unwrap(); - expand(&file["document"], tables, tables.len()) -} - -fn expand(node: &Value, tables: &[Value], before: usize) -> Value { - match node { - Value::Object(object) if object.contains_key("$fixture_table") => { - assert_eq!(object.len(), 1); - let index = object["$fixture_table"].as_u64().unwrap() as usize; - assert!(index < before, "invalid or cyclic fixture table reference"); - let table = &tables[index]; - let fields = table["fields"].as_array().unwrap(); - let columns = table["values"].as_array().unwrap(); - assert_eq!(fields.len(), columns.len()); - Value::Array( - table["rows"] - .as_array() - .unwrap() - .iter() - .map(|row| { - let cells = row.as_array().unwrap(); - assert_eq!(cells.len(), fields.len()); - let mut record = Map::new(); - for ((field, column), cell) in fields.iter().zip(columns).zip(cells) { - let cell = cell.as_i64().unwrap(); - assert!(cell >= -1); - if cell >= 0 { - let value = &column.as_array().unwrap()[cell as usize]; - assert!( - record - .insert( - field.as_str().unwrap().to_owned(), - expand(value, tables, index) - ) - .is_none() - ); - } - } - Value::Object(record) - }) - .collect(), - ) - } - Value::Object(object) => Value::Object( - object - .iter() - .map(|(key, value)| (key.clone(), expand(value, tables, before))) - .collect(), - ), - Value::Array(items) => Value::Array( - items - .iter() - .map(|value| expand(value, tables, before)) - .collect(), - ), - value => value.clone(), - } -} diff --git a/scripts/check_wire_behavior.py b/scripts/check_wire_behavior.py index 7bafaf1f8..00034f58e 100644 --- a/scripts/check_wire_behavior.py +++ b/scripts/check_wire_behavior.py @@ -209,13 +209,24 @@ def assert_behavior(cases): people = [ [i, 20.0 + i, 'north' if i % 3 else 'south', i % 2] for i in range(12) ] + sections = { + 'model', + 'task', + 'context', + 'predict', + 'schema', + 'output', + 'metadata', + } for name, calls in cases.items(): assert len(calls) == 1, name body = calls[0] - assert body['metadata']['request_id'] == _FIXED_REQUEST_ID if name.startswith('relational/'): + assert set(body) == sections | {'inference'}, name _assert_relational(body, people) continue + assert set(body) == sections, name + assert body['metadata'] == {'request_id': _FIXED_REQUEST_ID} target, kind, dtype = targets[ name.removeprefix('tabular/').split('#')[0] ] @@ -224,11 +235,13 @@ def assert_behavior(cases): 'kind': kind, 'target': {'column_name': target, 'dtype': dtype}, } - assert body['output']['fields'] == ( - ['prediction', 'probabilities'] - if '#default-outputs' in name - else ['prediction'] - ) + assert body['output'] == { + 'fields': ( + ['prediction', 'probabilities'] + if '#default-outputs' in name + else ['prediction'] + ) + } start = 3 if '/pql-' in name else 0 assert ( _rows(body['context']['instance_table'], columns) == people[start:] @@ -274,12 +287,14 @@ def _assert_relational(body, people): 'run_mode': 'fast', 'use_prediction_time': False, } - assert body['metadata']['num_context_examples'] == 84 - assert body['metadata']['num_prediction_examples'] == 3 - assert ( - body['metadata']['source_query'] - == 'PREDICT COUNT(orders.*, 0, 30, days) FOR EACH people.user_id' - ) + assert body['metadata'] == { + 'source_query': ( + 'PREDICT COUNT(orders.*, 0, 30, days) FOR EACH people.user_id' + ), + 'num_context_examples': 84, + 'num_prediction_examples': 3, + 'request_id': _FIXED_REQUEST_ID, + } start = pd.Timestamp('2026-01-01', tz='UTC') seen = set() for half in ('context', 'predict'): diff --git a/tests/test_release_contract.py b/tests/test_release_contract.py index a89b99576..a48a537e6 100644 --- a/tests/test_release_contract.py +++ b/tests/test_release_contract.py @@ -829,6 +829,10 @@ def _current_wire_payloads(): 'history', 'relationship', 'schema_type', + 'client_key', + 'tabular_inference', + 'metadata_key', + 'output_statistic', ], ) def test_wire_assertions_reject_behavior_changes(damage): @@ -859,6 +863,14 @@ def test_wire_assertions_reject_behavior_changes(damage): relational['schema']['related_tables']['orders']['columns']['price'][ 'dtype' ] = 'int64' + elif damage == 'client_key': + relational['client_generated_keys'] = {'orders': '__node_id'} + elif damage == 'tabular_inference': + direct['inference'] = {'run_mode': 'fast'} + elif damage == 'metadata_key': + relational['metadata']['client_rows'] = 3 + elif damage == 'output_statistic': + direct['output']['prediction_statistic'] = 'median' with pytest.raises(AssertionError): _loaded('check_wire_behavior').assert_behavior(cases)