|
| 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.combinator; |
| 19 | + |
| 20 | +import org.apache.doris.analysis.FunctionCallExpr; |
| 21 | +import org.apache.doris.nereids.glue.translator.ExpressionTranslator; |
| 22 | +import org.apache.doris.nereids.glue.translator.PlanTranslatorContext; |
| 23 | +import org.apache.doris.nereids.rules.expression.rules.ConvertAggStateCast; |
| 24 | +import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE; |
| 25 | +import org.apache.doris.nereids.trees.expressions.AggregateExpression; |
| 26 | +import org.apache.doris.nereids.trees.expressions.Cast; |
| 27 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 28 | +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateParam; |
| 29 | +import org.apache.doris.nereids.trees.expressions.functions.agg.PercentileReservoir; |
| 30 | +import org.apache.doris.nereids.trees.expressions.literal.DoubleLiteral; |
| 31 | +import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; |
| 32 | +import org.apache.doris.nereids.types.AggStateType; |
| 33 | +import org.apache.doris.nereids.types.DoubleType; |
| 34 | +import org.apache.doris.nereids.util.MemoTestUtils; |
| 35 | +import org.apache.doris.nereids.util.MoreFieldsThread; |
| 36 | +import org.apache.doris.nereids.util.PlanChecker; |
| 37 | + |
| 38 | +import com.google.common.collect.ImmutableList; |
| 39 | +import com.google.common.collect.ImmutableSet; |
| 40 | +import org.junit.jupiter.api.Assertions; |
| 41 | +import org.junit.jupiter.api.BeforeEach; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | + |
| 44 | +class StateCombinatorTest { |
| 45 | + @BeforeEach |
| 46 | + void setUp() { |
| 47 | + MemoTestUtils.createConnectContext(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testConstantFoldingPreservesStateLayout() { |
| 52 | + StateCombinator state = StateCombinator.create(new PercentileReservoir( |
| 53 | + new Cast(new VarcharLiteral("7"), DoubleType.INSTANCE), new DoubleLiteral(0.25))); |
| 54 | + AggStateType analyzedType = (AggStateType) state.getDataType(); |
| 55 | + Assertions.assertEquals(ImmutableList.of(true, false), analyzedType.getSubTypeNullables()); |
| 56 | + |
| 57 | + StateCombinator folded = (StateCombinator) MoreFieldsThread.keepFunctionSignature( |
| 58 | + () -> FoldConstantRuleOnFE.evaluateWithoutContext(state)); |
| 59 | + Assertions.assertEquals(new DoubleLiteral(7), folded.child(0)); |
| 60 | + Assertions.assertFalse(folded.child(0).nullable()); |
| 61 | + Assertions.assertEquals(analyzedType, folded.getDataType()); |
| 62 | + |
| 63 | + FunctionCallExpr translated = (FunctionCallExpr) ExpressionTranslator.translate( |
| 64 | + folded, new PlanTranslatorContext()); |
| 65 | + Assertions.assertEquals(analyzedType.toCatalogDataType(), translated.getFn().getReturnType()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testCombineConstantFoldingPreservesInputLayout() { |
| 70 | + PercentileReservoir nested = new PercentileReservoir( |
| 71 | + new Cast(new VarcharLiteral("7"), DoubleType.INSTANCE), new DoubleLiteral(0.25)); |
| 72 | + CombineCombinator combine = new CombineCombinator(nested.children(), nested); |
| 73 | + AggStateType analyzedType = (AggStateType) combine.getDataType(); |
| 74 | + CombineCombinator folded = (CombineCombinator) MoreFieldsThread.keepFunctionSignature( |
| 75 | + () -> FoldConstantRuleOnFE.evaluateWithoutContext(combine)); |
| 76 | + Assertions.assertFalse(folded.child(0).nullable()); |
| 77 | + Assertions.assertEquals(new DoubleLiteral(7), folded.child(0)); |
| 78 | + Assertions.assertEquals(analyzedType, folded.getDataType()); |
| 79 | + Assertions.assertEquals(folded, MoreFieldsThread.keepFunctionSignature( |
| 80 | + () -> folded.withDistinctAndChildren(false, folded.children()))); |
| 81 | + FunctionCallExpr translated = (FunctionCallExpr) ExpressionTranslator.translate( |
| 82 | + new AggregateExpression(folded, AggregateParam.LOCAL_RESULT), new PlanTranslatorContext()); |
| 83 | + Assertions.assertEquals(analyzedType.toCatalogDataType(), translated.getFn().getReturnType()); |
| 84 | + Assertions.assertTrue(translated.getChild(0).isNullable()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testAnalysisCanRecomputeStateType() { |
| 89 | + StateCombinator state = StateCombinator.create(new PercentileReservoir( |
| 90 | + new Cast(new VarcharLiteral("7"), DoubleType.INSTANCE), new DoubleLiteral(0.25))); |
| 91 | + state.getSignature(); |
| 92 | + StateCombinator rebound = MoreFieldsThread.keepFunctionSignature(false, |
| 93 | + () -> state.withChildren(ImmutableList.of(new DoubleLiteral(7), new DoubleLiteral(0.25)))); |
| 94 | + Assertions.assertEquals(ImmutableList.of(false, false), |
| 95 | + ((AggStateType) rebound.getDataType()).getSubTypeNullables()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testStateLayoutsHaveDistinctExpressionIdentity() { |
| 100 | + PercentileReservoir nullable = new PercentileReservoir( |
| 101 | + new Cast(new VarcharLiteral("7"), DoubleType.INSTANCE), new DoubleLiteral(0.25)); |
| 102 | + PercentileReservoir nonnullable = new PercentileReservoir(new DoubleLiteral(7), new DoubleLiteral(0.25)); |
| 103 | + for (boolean combine : ImmutableList.of(false, true)) { |
| 104 | + Expression beforeFold = combine ? new CombineCombinator(nullable.children(), nullable) |
| 105 | + : StateCombinator.create(nullable); |
| 106 | + Expression direct = combine ? new CombineCombinator(nonnullable.children(), nonnullable) |
| 107 | + : StateCombinator.create(nonnullable); |
| 108 | + Expression folded = MoreFieldsThread.keepFunctionSignature( |
| 109 | + () -> FoldConstantRuleOnFE.evaluateWithoutContext(beforeFold)); |
| 110 | + Assertions.assertEquals(direct.children(), folded.children()); |
| 111 | + Assertions.assertNotEquals(direct.getDataType(), folded.getDataType()); |
| 112 | + Assertions.assertNotEquals(direct, folded); |
| 113 | + Assertions.assertEquals(2, ImmutableSet.of(direct, folded).size()); |
| 114 | + Assertions.assertEquals(folded, MoreFieldsThread.keepFunctionSignature( |
| 115 | + () -> folded.withChildren(folded.children()))); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testExplicitStateCastAfterConstantFolding() { |
| 121 | + PlanChecker.from(MemoTestUtils.createConnectContext()) |
| 122 | + .analyze("select cast(percentile_reservoir_state(cast('7' as double), 0.25) " |
| 123 | + + "as agg_state<percentile_reservoir(double not null, double not null)>)") |
| 124 | + .rewrite(); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testExplicitStateCastRebindsCustomSignature() { |
| 129 | + for (String nullable : ImmutableList.of("null", "not null")) { |
| 130 | + PlanChecker.from(MemoTestUtils.createConnectContext()) |
| 131 | + .analyze("select cast(max_state(null) as agg_state<max(int " + nullable + ")>)") |
| 132 | + .rewrite(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testExplicitStateCastUpdatesStateLayout() { |
| 138 | + for (boolean nullable : ImmutableList.of(false, true)) { |
| 139 | + Expression argument = nullable ? new DoubleLiteral(7) |
| 140 | + : new Cast(new VarcharLiteral("7"), DoubleType.INSTANCE); |
| 141 | + StateCombinator state = StateCombinator.create( |
| 142 | + new PercentileReservoir(argument, new DoubleLiteral(0.25))); |
| 143 | + AggStateType target = new AggStateType("percentile_reservoir", |
| 144 | + ImmutableList.of(DoubleType.INSTANCE, DoubleType.INSTANCE), |
| 145 | + ImmutableList.of(nullable, false), true); |
| 146 | + Assertions.assertNotEquals(target, state.getDataType()); |
| 147 | + |
| 148 | + Cast converted = (Cast) MoreFieldsThread.keepFunctionSignature( |
| 149 | + () -> ConvertAggStateCast.convert(new Cast(state, target))); |
| 150 | + Assertions.assertEquals(nullable, converted.child().child(0).nullable()); |
| 151 | + Assertions.assertEquals(target, converted.child().getDataType()); |
| 152 | + FunctionCallExpr translated = (FunctionCallExpr) ExpressionTranslator.translate( |
| 153 | + converted.child(), new PlanTranslatorContext()); |
| 154 | + Assertions.assertEquals(target.toCatalogDataType(), translated.getFn().getReturnType()); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments