miretskiy commented on code in PR #22689: URL: https://github.com/apache/datafusion/pull/22689#discussion_r3356420342
########## datafusion/sqllogictest/test_files/map/map_transform.slt: ########## @@ -0,0 +1,70 @@ +# 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. + +############# +## map_transform Tests +############# + +statement ok +set datafusion.sql_parser.dialect = databricks; + +# basic: double every value, keys untouched +query ? +SELECT map_transform(map(['a','b','c'], [1,2,3]), (k, v) -> v * 2); +---- +{a: 2, b: 4, c: 6} + +# lambda may reference the key +query ? +SELECT map_transform(map(['a','b','c'], [1,2,3]), (k, v) -> case when k = 'b' then v * 100 else v end); +---- +{a: 1, b: 200, c: 3} + +# lambda can change the value type +query ? +SELECT map_transform(map(['a','b'], [1,2]), (k, v) -> concat(k, cast(v as varchar))); +---- +{a: a1, b: b2} + +# captures from outer columns are visible inside the lambda body +statement ok +CREATE TABLE t (m_key array<string>, m_val array<int>, n int) +AS VALUES + (['a', 'b'], [1, 2], 10), + (['a', 'b'], [3, 4], 100); + +query I? +SELECT n, map_transform(map(m_key, m_val), (k, v) -> v + n) from t order by n; Review Comment: This feels mighty weird to me; trino does not allow this, e.g.: ``` SELECT 10 n, transform(ARRAY[1,2,3], x -> x + 1 + n) INVALID_ARGUMENT: [invalid_argument] USER_ERROR: line 1:51: Column 'n' cannot be resolved ``` But even ignoring trino for a second, this just is "anti-SQL" -- you cannot reference variables that are created in the scame scope (e.g. projection scope) -- and things that do allow it usually do that very explicitly -- for example lateral joins ```PG postgres=# select 1 a, a+1; ERROR: column "a" does not exist LINE 1: select 1 a, a+1; ^ ``` Are we sure we want to do this? ########## datafusion/physical-expr/src/expressions/lambda.rs: ########## @@ -43,6 +43,11 @@ pub struct LambdaExpr { body: Arc<dyn PhysicalExpr>, projected_body: Arc<dyn PhysicalExpr>, projection: Vec<usize>, + /// Number of columns in the outer input schema. Column/LambdaVariable + /// indices below this value reference outer captures; indices at or above + /// reference lambda parameters (whose position in the merged evaluation + /// batch is fixed by the higher-order function, not by the projection). + outer_columns_count: usize, Review Comment: does this fix need to be up-stremed? or it's our own impl? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
