github-actions[bot] commented on code in PR #62524: URL: https://github.com/apache/doris/pull/62524#discussion_r3098155696
########## fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/UserVariableAnalysisTest.java: ########## @@ -0,0 +1,109 @@ +// 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.rules.analysis; + +import org.apache.doris.analysis.IntLiteral; +import org.apache.doris.analysis.LargeIntLiteral; +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.analyzer.UnboundVariable; +import org.apache.doris.nereids.analyzer.UnboundVariable.VariableType; +import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; +import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Variable; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.types.BigIntType; +import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.LargeIntType; +import org.apache.doris.nereids.util.MemoTestUtils; +import org.apache.doris.qe.ConnectContext; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +/** Tests for user variable handling in expression analysis. */ +public class UserVariableAnalysisTest { + + @Test + public void testUserVarIntegerTypeSmall() { + ConnectContext ctx = MemoTestUtils.createConnectContext(); + // set user var @a = 1 (small int) + ctx.setUserVar("a", new IntLiteral(1L)); + + // get nereids literal via ConnectContext helper + Literal l = ConnectContext.get().getLiteralForUserVar("a"); + Assertions.assertNotNull(l); + Assertions.assertEquals(IntegerType.INSTANCE, l.getDataType()); Review Comment: `new IntLiteral(1L)` is a legacy `TINYINT`, not an `INT` (`IntLiteral.init()` picks the smallest fitting width). With the new `ConnectContext` logic this now becomes a `TinyIntLiteral`, so this assertion is expecting the pre-fix behavior and will fail once FE UTs are run. If the intent is to verify `INT`, construct the legacy literal with `Type.INT`; otherwise assert `TinyIntType`. ########## fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/UserVariableAnalysisTest.java: ########## @@ -0,0 +1,109 @@ +// 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.rules.analysis; + +import org.apache.doris.analysis.IntLiteral; +import org.apache.doris.analysis.LargeIntLiteral; +import org.apache.doris.nereids.CascadesContext; +import org.apache.doris.nereids.analyzer.UnboundVariable; +import org.apache.doris.nereids.analyzer.UnboundVariable.VariableType; +import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; +import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Variable; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.types.BigIntType; +import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.LargeIntType; +import org.apache.doris.nereids.util.MemoTestUtils; +import org.apache.doris.qe.ConnectContext; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +/** Tests for user variable handling in expression analysis. */ +public class UserVariableAnalysisTest { + + @Test + public void testUserVarIntegerTypeSmall() { + ConnectContext ctx = MemoTestUtils.createConnectContext(); + // set user var @a = 1 (small int) + ctx.setUserVar("a", new IntLiteral(1L)); + + // get nereids literal via ConnectContext helper + Literal l = ConnectContext.get().getLiteralForUserVar("a"); + Assertions.assertNotNull(l); + Assertions.assertEquals(IntegerType.INSTANCE, l.getDataType()); + } + + @Test + public void testUserVarIntegerTypeBig() { + ConnectContext ctx = MemoTestUtils.createConnectContext(); + // set user var @b = Long.MAX_VALUE (bigint) + ctx.setUserVar("b", new IntLiteral(Long.MAX_VALUE)); + + Literal l = ConnectContext.get().getLiteralForUserVar("b"); + Assertions.assertNotNull(l); + Assertions.assertEquals(BigIntType.INSTANCE, l.getDataType()); + } + + @Test + public void testUserVarIntegerTypeLarge() { + ConnectContext ctx = MemoTestUtils.createConnectContext(); + // set user var @b = Long.MAX_VALUE (bigint) + ctx.setUserVar("c", new LargeIntLiteral(LargeIntLiteral.LARGE_INT_MAX)); + + Literal l = ConnectContext.get().getLiteralForUserVar("c"); + Assertions.assertNotNull(l); + Assertions.assertEquals(LargeIntType.INSTANCE, l.getDataType()); + } + + @Test + public void testVariableRecordedAndTypeCoercion() { + // create context and cascades context (sql cache enabled by default in test utils) + ConnectContext ctx = MemoTestUtils.createConnectContext(); + ctx.setUserVar("uvar", new IntLiteral(42L)); + + // create a comparison using an unbound user variable: @uvar = 1 + UnboundVariable unboundVar = new UnboundVariable( + "uvar", VariableType.USER); + IntegerLiteral right = new IntegerLiteral(1); + ComparisonPredicate cmp = new EqualTo(unboundVar, right); + + // prepare analyzer with cascades context so sql cache context exists + CascadesContext cascadesContext = MemoTestUtils.createCascadesContext(ctx, "select 1"); + ExpressionAnalyzer analyzer = new ExpressionAnalyzer(null, + new org.apache.doris.nereids.analyzer.Scope(java.util.Collections.emptyList()), + cascadesContext, false, false); + ExpressionRewriteContext rewriteContext = new ExpressionRewriteContext(cascadesContext); + + // analyze should not throw and should handle type coercion + Expression analyzed = analyzer.analyze(cmp, rewriteContext); + Assertions.assertNotNull(analyzed); + + // sql cache context should have recorded the used variable + List<Variable> used = cascadesContext.getStatementContext().getSqlCacheContext().get().getUsedVariables(); Review Comment: This test no longer matches the final implementation. The patch removed SQL-cache recording from `ExpressionAnalyzer.visitUnboundVariable()` and only `ReplaceVariableByLiteral` calls `sqlCacheContext.addUsedVariable(...)`. `ExpressionAnalyzer.analyze(cmp, rewriteContext)` never runs that rewrite, so `usedVariables` stays empty here. To test SQL-cache recording you need to run the analyzer job / `VariableToLiteral` pass, or explicitly rewrite `analyzed` with `ReplaceVariableByLiteral` before asserting. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/ExpressionTrait.java: ########## @@ -46,23 +47,58 @@ default void checkLegalityBeforeTypeCoercion() {} @Developing default void checkLegalityAfterRewrite() {} + /** + * getArguments. + */ default List<Expression> getArguments() { - return children(); + ImmutableList.Builder<Expression> arguments = ImmutableList.builder(); + for (Expression arg : children()) { + if (arg instanceof Variable && ((Variable) arg).getRealExpression() != null) { Review Comment: This only fixes callers that go through `getArguments*()`, but `BindExpression` still binds functions before the later `VariableToLiteral` analyzer pass runs. That means any function that validates raw `child()` nodes still hits the same timing bug. A concrete reachable example is `SET @p = 3; SELECT utc_time(@p)` (also `utc_timestamp(@p)` and `char(@cs, 65)`): `ExpressionAnalyzer.visitUnboundFunction()` immediately calls `TypeCoercionUtils.processBoundFunction()`, and those implementations still check `child(0).isLiteral()` / `instanceof StringLikeLiteral`, so they reject the `Variable` wrapper before this helper is ever consulted. So this fixes helper-based callers like `window_funnel`, but not the general Variable->literal timing issue described in the PR. -- 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]
