GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — datafusion-python/python/tests/test_lambda.py at main · apache/datafusion-python Expand file tree/ Copy pathtest_lambda.pyMore file actions 164 lines (147 loc) · 5.49 KB / Copy pathtest_lambda.pyFile metadata and controls164 lines (147 loc) · 5.49 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. """Tests for lambda expressions and higher-order array functions.""" import pickle import pytest from datafusion import SessionConfig, SessionContext, col, lit from datafusion import functions as f @pytest.fixture def df(): ctx = SessionContext() return ctx.from_pydict({"a": [[1, 2, 3], [4, 5]]}) def _column(df, expr, name): return df.select(expr.alias(name)).collect_column(name).to_pylist() @pytest.mark.parametrize( ("build_expr", "expected"), [ pytest.param( lambda: f.array_transform(col("a"), lambda v: v * 2), [[2, 4, 6], [8, 10]], id="array_transform_callable", ), pytest.param( lambda: f.array_transform( col("a"), f.lambda_(["v"], f.lambda_var("v") * lit(2)) ), [[2, 4, 6], [8, 10]], id="array_transform_explicit_lambda", ), pytest.param( lambda: f.array_transform(col("a"), lambda v: 0), [[0, 0, 0], [0, 0]], id="array_transform_literal_body_is_coerced", ), pytest.param( lambda: f.list_transform(col("a"), lambda v: v + 1), [[2, 3, 4], [5, 6]], id="list_transform_alias", ), pytest.param( lambda: f.array_any_match(col("a"), lambda v: v > 3), [False, True], id="array_any_match_callable", ), pytest.param( lambda: f.array_any_match( col("a"), f.lambda_(["v"], f.lambda_var("v") > lit(2)) ), [True, True], id="array_any_match_explicit_lambda", ), pytest.param( lambda: f.any_match(col("a"), lambda v: v > 4), [False, True], id="any_match_alias", ), pytest.param( lambda: f.list_any_match(col("a"), lambda v: v > 4), [False, True], id="list_any_match_alias", ), pytest.param( lambda: f.array_filter(col("a"), lambda v: v > 2), [[3], [4, 5]], id="array_filter_callable", ), pytest.param( lambda: f.array_filter( col("a"), f.lambda_(["v"], f.lambda_var("v") > lit(2)) ), [[3], [4, 5]], id="array_filter_explicit_lambda", ), pytest.param( lambda: f.list_filter(col("a"), lambda v: v > 2), [[3], [4, 5]], id="list_filter_alias", ), pytest.param( lambda: f.array_first(col("a"), lambda v: v > 2), [3, 4], id="array_first_callable", ), pytest.param( lambda: f.array_first( col("a"), f.lambda_(["v"], f.lambda_var("v") > lit(3)) ), [None, 4], id="array_first_explicit_lambda_no_match_is_null", ), pytest.param( lambda: f.list_first(col("a"), lambda v: v > 4), [None, 5], id="list_first_alias", ), ], ) def test_higher_order_function_results(df, build_expr, expected): assert _column(df, build_expr(), "r") == expected def test_lambda_param_name_appears_in_plan(df): # The user-chosen parameter name should survive into the displayed plan # rather than a synthetic placeholder. expr = f.array_transform(col("a"), lambda value: value * 2) assert "value" in expr.canonical_name() @pytest.mark.parametrize( ("arg", "exc_type", "match"), [ pytest.param(42, TypeError, "expected an Expr or callable", id="non_callable"), pytest.param( lambda: lit(1), ValueError, "at least one parameter", id="zero_arg_callable", ), ], ) def test_to_lambda_rejects_invalid_arg(arg, exc_type, match): with pytest.raises(exc_type, match=match): f.array_transform(col("a"), arg) @pytest.mark.parametrize("dialect", ["DuckDB", "ClickHouse", "Snowflake", "Databricks"]) def test_sql_lambda_keyword_syntax(dialect): # ``lambda x: x * 2`` is the forward-compatible syntax. DuckDB will drop # the arrow form (``x -> ...``) in v2.1; the keyword form is supported by # every dialect in sqlparser-rs that enables lambda functions. ctx = SessionContext(SessionConfig().set("datafusion.sql_parser.dialect", dialect)) result = ctx.sql( "select array_transform([1, 2, 3], lambda x: x * 2) as d" ).collect_column("d") assert result.to_pylist() == [[2, 4, 6]] def test_pickle_lambda_expr_round_trip(df): expr = f.array_transform(col("a"), lambda v: v * 2) decoded = pickle.loads(pickle.dumps(expr)) # noqa: S301 assert decoded.canonical_name() == expr.canonical_name() assert _column(df, decoded, "r") == [[2, 4, 6], [8, 10]] |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |