|
22 | 22 | import org.apache.doris.analysis.LargeIntLiteral; |
23 | 23 | import org.apache.doris.analysis.TimeStampNsLiteral; |
24 | 24 | import org.apache.doris.catalog.ScalarType; |
| 25 | +import org.apache.doris.nereids.CascadesContext; |
| 26 | +import org.apache.doris.nereids.analyzer.Scope; |
| 27 | +import org.apache.doris.nereids.analyzer.UnboundVariable; |
| 28 | +import org.apache.doris.nereids.analyzer.UnboundVariable.VariableType; |
| 29 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 30 | +import org.apache.doris.nereids.trees.expressions.Variable; |
25 | 31 | import org.apache.doris.nereids.trees.expressions.literal.Literal; |
26 | 32 | import org.apache.doris.nereids.trees.expressions.literal.TimestampTzLiteral; |
27 | 33 | import org.apache.doris.nereids.types.BigIntType; |
|
34 | 40 | import org.apache.doris.nereids.util.MemoTestUtils; |
35 | 41 | import org.apache.doris.qe.ConnectContext; |
36 | 42 |
|
| 43 | +import com.google.common.collect.ImmutableList; |
37 | 44 | import org.junit.jupiter.api.Assertions; |
38 | 45 | import org.junit.jupiter.api.Test; |
39 | 46 |
|
| 47 | +import java.util.List; |
| 48 | + |
40 | 49 | /** Tests for user variable handling in expression analysis. */ |
41 | 50 | public class UserVariableAnalysisTest { |
42 | 51 |
|
@@ -84,4 +93,23 @@ public void testUserVarTimeStampNsTypeAndPrecision() { |
84 | 93 | Assertions.assertEquals(TimeStampNsType.INSTANCE, literal.getDataType()); |
85 | 94 | Assertions.assertEquals("2024-02-29 12:34:56.123456789", literal.getStringValue()); |
86 | 95 | } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testBindUserVariableToRealExpressionAndRecordSqlCacheDependency() { |
| 99 | + ConnectContext ctx = MemoTestUtils.createConnectContext(); |
| 100 | + ctx.setUserVar("v", new IntLiteral(42)); |
| 101 | + CascadesContext cascadesContext = MemoTestUtils.createCascadesContext(ctx, "select @v"); |
| 102 | + ExpressionAnalyzer analyzer = new ExpressionAnalyzer(null, new Scope(ImmutableList.of()), |
| 103 | + cascadesContext, false, false); |
| 104 | + |
| 105 | + Expression analyzed = analyzer.analyze(new UnboundVariable("v", VariableType.USER)); |
| 106 | + |
| 107 | + Assertions.assertInstanceOf(Literal.class, analyzed); |
| 108 | + List<Variable> usedVariables = cascadesContext.getStatementContext().getSqlCacheContext() |
| 109 | + .orElseThrow().getUsedVariables(); |
| 110 | + Assertions.assertEquals(1, usedVariables.size()); |
| 111 | + Assertions.assertEquals("v", usedVariables.get(0).getName()); |
| 112 | + Assertions.assertEquals(VariableType.USER, usedVariables.get(0).getType()); |
| 113 | + Assertions.assertEquals(analyzed, usedVariables.get(0).getRealExpression()); |
| 114 | + } |
87 | 115 | } |
0 commit comments