-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[feature](function) Support map arguments for inner_product #67311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c501fa9
[feature](function) Support map arguments for inner_product
BiteTheDDDDt 8f23259
[fix](function) Tighten map inner_product type handling
BiteTheDDDDt 53e6767
[fix](function) Preserve map last-wins semantics in inner_product
BiteTheDDDDt a3809cd
[fix](function) Validate retained map values
BiteTheDDDDt 6916ed7
[fix](function) Optimize constant map inner product
BiteTheDDDDt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
429 changes: 429 additions & 0 deletions
429
be/test/exprs/function/function_map_inner_product_test.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...st/java/org/apache/doris/nereids/trees/expressions/functions/scalar/InnerProductTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // 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. | ||
|
|
||
| package org.apache.doris.nereids.trees.expressions.functions.scalar; | ||
|
|
||
| import org.apache.doris.nereids.exceptions.AnalysisException; | ||
| import org.apache.doris.nereids.parser.NereidsParser; | ||
| import org.apache.doris.nereids.rules.expression.ExpressionRewriteTestHelper; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.types.IntegerType; | ||
| import org.apache.doris.nereids.types.MapType; | ||
| import org.apache.doris.qe.GlobalVariable; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class InnerProductTest { | ||
|
|
||
| private static final NereidsParser PARSER = new NereidsParser(); | ||
|
|
||
| @Test | ||
| public void testInferNullMapKeyType() { | ||
| Expression expression = analyze( | ||
| "inner_product(map(null, cast(2 as float))," | ||
| + " map(cast(null as int), cast(3 as float)))"); | ||
|
|
||
| Assertions.assertTrue(expression instanceof InnerProduct); | ||
| for (Expression argument : ((InnerProduct) expression).getArguments()) { | ||
| Assertions.assertTrue(argument.getDataType().isMapType()); | ||
| Assertions.assertEquals(IntegerType.INSTANCE, | ||
| ((MapType) argument.getDataType()).getKeyType()); | ||
| } | ||
| Assertions.assertDoesNotThrow(expression::checkLegalityAfterRewrite); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRejectMixedMapKeyFamiliesInBothCoercionModes() { | ||
| boolean originalBehavior = GlobalVariable.enableNewTypeCoercionBehavior; | ||
| try { | ||
| for (boolean enableNewBehavior : new boolean[] {true, false}) { | ||
| GlobalVariable.enableNewTypeCoercionBehavior = enableNewBehavior; | ||
| AnalysisException exception = Assertions.assertThrows(AnalysisException.class, | ||
| () -> analyze("inner_product(map(1, cast(2 as float))," | ||
| + " map('1', cast(3 as float)))")); | ||
| Assertions.assertTrue(exception.getMessage().contains("same type family"), | ||
| exception::getMessage); | ||
| } | ||
| } finally { | ||
| GlobalVariable.enableNewTypeCoercionBehavior = originalBehavior; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRejectAllNullMapKeyTypesAfterCoercion() { | ||
| Expression expression = analyze( | ||
| "inner_product(map(null, cast(1 as float))," | ||
| + " map(null, cast(2 as float)))"); | ||
|
|
||
| AnalysisException exception = Assertions.assertThrows( | ||
| AnalysisException.class, expression::checkLegalityAfterRewrite); | ||
| Assertions.assertTrue(exception.getMessage().contains( | ||
| "only supports integer or string map keys"), exception::getMessage); | ||
| } | ||
|
|
||
| private Expression analyze(String sql) { | ||
| return ExpressionRewriteTestHelper.typeCoercion(PARSER.parseExpression(sql)); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
regression-test/data/query_p0/sql_functions/map_functions/test_map_inner_product.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| -- This file is automatically generated. You should know what you did if you want to edit this | ||
| -- !map_inner_product_rows -- | ||
| 1 10.0 | ||
| 2 -4.0 | ||
| 3 0.0 | ||
|
|
||
| -- !map_inner_product_string_keys -- | ||
| 22.0 | ||
|
|
||
| -- !map_inner_product_null_key -- | ||
| 38.0 | ||
|
|
||
| -- !map_inner_product_inferred_null_key -- | ||
| 6.0 | ||
|
|
||
| -- !map_inner_product_disjoint -- | ||
| 0.0 | ||
|
|
||
| -- !map_inner_product_dense_compatibility -- | ||
| 11.0 |
124 changes: 124 additions & 0 deletions
124
regression-test/suites/query_p0/sql_functions/map_functions/test_map_inner_product.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // 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. | ||
|
|
||
| suite("test_map_inner_product", "p0,nonConcurrent") { | ||
| sql "drop table if exists test_map_inner_product" | ||
| sql """ | ||
| create table test_map_inner_product ( | ||
| id int, | ||
| lhs map<int, float>, | ||
| rhs map<int, float> | ||
| ) | ||
| duplicate key(id) | ||
| distributed by hash(id) buckets 1 | ||
| properties("replication_num" = "1") | ||
| """ | ||
| sql """ | ||
| insert into test_map_inner_product values | ||
| (1, map(1, 1.0, 2, 2.0), map(2, 3.0, 1, 4.0)), | ||
| (2, map(1, -2.0, 3, 0.5), map(1, 4.0, 2, 99.0, 3, 8.0)), | ||
| (3, cast(map() as map<int, float>), map(1, 10.0)), | ||
| (4, map(1, cast(null as float)), map(1, 2.0)), | ||
| (5, cast(null as map<int, float>), map(1, 2.0)) | ||
| """ | ||
|
|
||
| order_qt_map_inner_product_rows """ | ||
| select id, inner_product(lhs, rhs) | ||
| from test_map_inner_product | ||
| where id <= 3 | ||
| order by id | ||
| """ | ||
|
|
||
| qt_map_inner_product_string_keys """ | ||
| select inner_product( | ||
| map('a', 2.0, 'b', 3.0), | ||
| map('b', 4.0, 'c', 100.0, 'a', 5.0)) | ||
| """ | ||
|
|
||
| qt_map_inner_product_null_key """ | ||
| select inner_product( | ||
| map(cast(null as int), 2.0, 1, 3.0), | ||
| map(1, 10.0, cast(null as int), 4.0)) | ||
| """ | ||
|
|
||
| qt_map_inner_product_inferred_null_key """ | ||
| select inner_product( | ||
| map(null, cast(2 as float)), | ||
| map(cast(null as int), cast(3 as float))) | ||
| """ | ||
|
|
||
| qt_map_inner_product_disjoint """ | ||
| select inner_product(map(1, 2.0), map(2, 3.0)) | ||
| """ | ||
|
|
||
| qt_map_inner_product_dense_compatibility """ | ||
| select inner_product([1.0, 2.0], [3.0, 4.0]) | ||
| """ | ||
|
|
||
| test { | ||
| sql """ | ||
| select inner_product(lhs, rhs) | ||
| from test_map_inner_product | ||
| where id = 4 | ||
| """ | ||
| exception "First argument for function inner_product cannot have null" | ||
| } | ||
|
|
||
| test { | ||
| sql """ | ||
| select inner_product(lhs, rhs) | ||
| from test_map_inner_product | ||
| where id = 5 | ||
| """ | ||
| exception "First argument for function inner_product cannot be null" | ||
| } | ||
|
|
||
| test { | ||
| sql """ | ||
| select inner_product( | ||
| map(cast('2024-01-01' as date), cast(1 as float)), | ||
| map(cast('2024-01-01' as date), cast(2 as float))) | ||
| """ | ||
| exception "inner_product only supports integer or string map keys" | ||
| } | ||
|
|
||
| test { | ||
| sql """ | ||
| select inner_product( | ||
| map(null, cast(1 as float)), | ||
| map(null, cast(2 as float))) | ||
| """ | ||
| exception "inner_product only supports integer or string map keys" | ||
| } | ||
|
|
||
| def originalTypeCoercionBehavior = sql """ | ||
| show global variables like 'enable_new_type_coercion_behavior' | ||
| """ | ||
| try { | ||
| sql "set global enable_new_type_coercion_behavior = false" | ||
| test { | ||
| sql """ | ||
| select inner_product( | ||
| map(1, cast(2 as float)), | ||
| map('1', cast(3 as float))) | ||
| """ | ||
| exception "inner_product requires map keys from the same type family" | ||
| } | ||
| } finally { | ||
| sql "set global enable_new_type_coercion_behavior = ${originalTypeCoercionBehavior[0][1]}" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.