|
| 1 | +"""Tests for function types (scalar, aggregate, window).""" |
| 2 | + |
| 3 | +import textwrap |
| 4 | + |
| 5 | +import pytest |
| 6 | +import yaml |
| 7 | + |
| 8 | +from substrait.builders.type import i8 |
| 9 | +from substrait.extension_registry import ExtensionRegistry |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "test_case", |
| 14 | + [ |
| 15 | + # Scalar functions |
| 16 | + pytest.param( |
| 17 | + { |
| 18 | + "yaml_content": textwrap.dedent("""\ |
| 19 | + %YAML 1.2 |
| 20 | + --- |
| 21 | + urn: extension:test:scalar_funcs |
| 22 | + scalar_functions: |
| 23 | + - name: "add" |
| 24 | + description: "Add two numbers" |
| 25 | + impls: |
| 26 | + - args: |
| 27 | + - value: i8 |
| 28 | + - value: i8 |
| 29 | + return: i8 |
| 30 | + """), |
| 31 | + "urn": "extension:test:scalar_funcs", |
| 32 | + "func_name": "add", |
| 33 | + "signature": [i8(nullable=False), i8(nullable=False)], |
| 34 | + "expected_type": "scalar", |
| 35 | + }, |
| 36 | + id="scalar-add", |
| 37 | + ), |
| 38 | + pytest.param( |
| 39 | + { |
| 40 | + "yaml_content": textwrap.dedent("""\ |
| 41 | + %YAML 1.2 |
| 42 | + --- |
| 43 | + urn: extension:test:scalar_funcs |
| 44 | + scalar_functions: |
| 45 | + - name: "test_fn" |
| 46 | + description: "" |
| 47 | + impls: |
| 48 | + - args: |
| 49 | + - value: i8 |
| 50 | + variadic: |
| 51 | + min: 2 |
| 52 | + return: i8 |
| 53 | + """), |
| 54 | + "urn": "extension:test:scalar_funcs", |
| 55 | + "func_name": "test_fn", |
| 56 | + "signature": [i8(nullable=False), i8(nullable=False)], |
| 57 | + "expected_type": "scalar", |
| 58 | + }, |
| 59 | + id="scalar-test_fn", |
| 60 | + ), |
| 61 | + # Aggregate functions |
| 62 | + pytest.param( |
| 63 | + { |
| 64 | + "yaml_content": textwrap.dedent("""\ |
| 65 | + %YAML 1.2 |
| 66 | + --- |
| 67 | + urn: extension:test:agg_funcs |
| 68 | + aggregate_functions: |
| 69 | + - name: "count" |
| 70 | + description: "Count non-null values" |
| 71 | + impls: |
| 72 | + - args: |
| 73 | + - value: i8 |
| 74 | + return: i64 |
| 75 | + """), |
| 76 | + "urn": "extension:test:agg_funcs", |
| 77 | + "func_name": "count", |
| 78 | + "signature": [i8(nullable=False)], |
| 79 | + "expected_type": "aggregate", |
| 80 | + }, |
| 81 | + id="aggregate-count", |
| 82 | + ), |
| 83 | + pytest.param( |
| 84 | + { |
| 85 | + "yaml_content": textwrap.dedent("""\ |
| 86 | + %YAML 1.2 |
| 87 | + --- |
| 88 | + urn: extension:test:agg_funcs |
| 89 | + aggregate_functions: |
| 90 | + - name: "sum" |
| 91 | + description: "Sum values" |
| 92 | + impls: |
| 93 | + - args: |
| 94 | + - value: i8 |
| 95 | + return: i64 |
| 96 | + """), |
| 97 | + "urn": "extension:test:agg_funcs", |
| 98 | + "func_name": "sum", |
| 99 | + "signature": [i8(nullable=False)], |
| 100 | + "expected_type": "aggregate", |
| 101 | + }, |
| 102 | + id="aggregate-sum", |
| 103 | + ), |
| 104 | + # Window functions |
| 105 | + pytest.param( |
| 106 | + { |
| 107 | + "yaml_content": textwrap.dedent("""\ |
| 108 | + %YAML 1.2 |
| 109 | + --- |
| 110 | + urn: extension:test:window_funcs |
| 111 | + window_functions: |
| 112 | + - name: "row_number" |
| 113 | + description: "Assign row numbers" |
| 114 | + impls: |
| 115 | + - args: [] |
| 116 | + return: i64 |
| 117 | + """), |
| 118 | + "urn": "extension:test:window_funcs", |
| 119 | + "func_name": "row_number", |
| 120 | + "signature": [], |
| 121 | + "expected_type": "window", |
| 122 | + }, |
| 123 | + id="window-row_number", |
| 124 | + ), |
| 125 | + pytest.param( |
| 126 | + { |
| 127 | + "yaml_content": textwrap.dedent("""\ |
| 128 | + %YAML 1.2 |
| 129 | + --- |
| 130 | + urn: extension:test:window_funcs |
| 131 | + window_functions: |
| 132 | + - name: "rank" |
| 133 | + description: "Assign ranks" |
| 134 | + impls: |
| 135 | + - args: [] |
| 136 | + return: i64 |
| 137 | + """), |
| 138 | + "urn": "extension:test:window_funcs", |
| 139 | + "func_name": "rank", |
| 140 | + "signature": [], |
| 141 | + "expected_type": "window", |
| 142 | + }, |
| 143 | + id="window-rank", |
| 144 | + ), |
| 145 | + ], |
| 146 | +) |
| 147 | +def test_all_function_types_from_yaml(test_case): |
| 148 | + """Test that all functions in YAML are registered with correct function_type.value.""" |
| 149 | + test_registry = ExtensionRegistry(load_default_extensions=False) |
| 150 | + test_registry.register_extension_dict( |
| 151 | + yaml.safe_load(test_case["yaml_content"]), |
| 152 | + uri=f"https://test.example.com/{test_case['urn'].replace(':', '_')}.yaml", |
| 153 | + ) |
| 154 | + |
| 155 | + result = test_registry.lookup_function( |
| 156 | + urn=test_case["urn"], |
| 157 | + function_name=test_case["func_name"], |
| 158 | + signature=test_case["signature"], |
| 159 | + ) |
| 160 | + assert result is not None, ( |
| 161 | + f"Failed to lookup {test_case['func_name']} in {test_case['urn']}" |
| 162 | + ) |
| 163 | + entry, _ = result |
| 164 | + assert hasattr(entry, "function_type"), ( |
| 165 | + f"Entry for {test_case['func_name']} missing function_type attribute" |
| 166 | + ) |
| 167 | + assert entry.function_type is not None, ( |
| 168 | + f"function_type is None for {test_case['func_name']}" |
| 169 | + ) |
| 170 | + assert isinstance(entry.function_type.value, str), ( |
| 171 | + f"function_type.value is not a string for {test_case['func_name']}" |
| 172 | + ) |
| 173 | + assert entry.function_type.value == test_case["expected_type"], ( |
| 174 | + f"Expected function_type.value '{test_case['expected_type']}' " |
| 175 | + f"for {test_case['func_name']}, got '{entry.function_type.value}'" |
| 176 | + ) |
0 commit comments