|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.trees.expressions.functions; |
| 19 | + |
| 20 | +import org.apache.doris.nereids.analyzer.UnboundVariable.VariableType; |
| 21 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 22 | +import org.apache.doris.nereids.trees.expressions.Variable; |
| 23 | +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; |
| 24 | +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; |
| 25 | +import org.apache.doris.nereids.types.DataType; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for ExpressionTrait behaviors when children are `Variable`. |
| 34 | + */ |
| 35 | +public class ExpressionTraitTest { |
| 36 | + |
| 37 | + static class DummyFunction extends Expression { |
| 38 | + protected DummyFunction(List<Expression> children) { |
| 39 | + super(children); |
| 40 | + } |
| 41 | + |
| 42 | + protected DummyFunction(Expression... children) { |
| 43 | + super(children); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Expression withChildren(List<Expression> children) { |
| 53 | + return new DummyFunction(children); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected String computeToSql() { |
| 58 | + return "dummy"; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean nullable() { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testVariableWithRealExpression() { |
| 69 | + IntegerLiteral lit = new IntegerLiteral(42); |
| 70 | + Variable var = new Variable("v", VariableType.USER, lit); |
| 71 | + |
| 72 | + DummyFunction func = new DummyFunction(var); |
| 73 | + |
| 74 | + List<Expression> args = func.getArguments(); |
| 75 | + Assertions.assertEquals(1, args.size()); |
| 76 | + Assertions.assertEquals(lit, args.get(0)); |
| 77 | + |
| 78 | + Assertions.assertEquals(lit, func.getArgument(0)); |
| 79 | + |
| 80 | + List<DataType> types = func.getArgumentsTypes(); |
| 81 | + Assertions.assertEquals(1, types.size()); |
| 82 | + Assertions.assertEquals(lit.getDataType(), types.get(0)); |
| 83 | + |
| 84 | + Assertions.assertEquals(lit.getDataType(), func.getArgumentType(0)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testVariableWithoutRealExpression() { |
| 89 | + IntegerLiteral lit = new IntegerLiteral(100); |
| 90 | + // create a Variable but override getRealExpression to simulate a missing real expression |
| 91 | + Variable var = new Variable("v", VariableType.USER, lit) { |
| 92 | + @Override |
| 93 | + public Expression getRealExpression() { |
| 94 | + return null; |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + DummyFunction func = new DummyFunction(var); |
| 99 | + |
| 100 | + List<Expression> args = func.getArguments(); |
| 101 | + Assertions.assertEquals(1, args.size()); |
| 102 | + // when Variable.getRealExpression() returns null, ExpressionTrait should return the Variable itself |
| 103 | + Assertions.assertSame(var, args.get(0)); |
| 104 | + |
| 105 | + Assertions.assertSame(var, func.getArgument(0)); |
| 106 | + |
| 107 | + List<DataType> types = func.getArgumentsTypes(); |
| 108 | + Assertions.assertEquals(1, types.size()); |
| 109 | + // fallback to variable.getDataType() |
| 110 | + Assertions.assertEquals(var.getDataType(), types.get(0)); |
| 111 | + |
| 112 | + Assertions.assertEquals(var.getDataType(), func.getArgumentType(0)); |
| 113 | + } |
| 114 | +} |
0 commit comments