|
18 | 18 | package org.apache.doris.nereids.types; |
19 | 19 |
|
20 | 20 | import org.apache.doris.catalog.Env; |
| 21 | +import org.apache.doris.nereids.exceptions.AnalysisException; |
| 22 | +import org.apache.doris.nereids.hint.DistributeHint; |
| 23 | +import org.apache.doris.nereids.rules.analysis.CheckAfterRewrite; |
| 24 | +import org.apache.doris.nereids.trees.expressions.Alias; |
| 25 | +import org.apache.doris.nereids.trees.expressions.Cast; |
| 26 | +import org.apache.doris.nereids.trees.expressions.EqualTo; |
| 27 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 28 | +import org.apache.doris.nereids.trees.expressions.MarkJoinSlotReference; |
| 29 | +import org.apache.doris.nereids.trees.expressions.Slot; |
21 | 30 | import org.apache.doris.nereids.trees.expressions.literal.VarBinaryLiteral; |
| 31 | +import org.apache.doris.nereids.trees.plans.DistributeType; |
| 32 | +import org.apache.doris.nereids.trees.plans.JoinType; |
| 33 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 34 | +import org.apache.doris.nereids.trees.plans.RelationId; |
| 35 | +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; |
| 36 | +import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation; |
22 | 37 | import org.apache.doris.nereids.util.PlanChecker; |
23 | 38 | import org.apache.doris.qe.GlobalVariable; |
24 | 39 | import org.apache.doris.thrift.TUniqueId; |
25 | 40 | import org.apache.doris.utframe.TestWithFeService; |
26 | 41 |
|
| 42 | +import com.google.common.collect.ImmutableList; |
27 | 43 | import org.junit.jupiter.api.Assertions; |
28 | 44 | import org.junit.jupiter.api.Test; |
29 | 45 |
|
| 46 | +import java.util.List; |
| 47 | +import java.util.Optional; |
| 48 | + |
30 | 49 | public class VarBinarySqlSupportTest extends TestWithFeService { |
31 | 50 | @Test |
32 | 51 | public void testNativeVarbinaryRemainsUnsupported() { |
@@ -142,6 +161,41 @@ private void assertBinaryExpressionsRejected(String[] expressions) { |
142 | 161 | } |
143 | 162 | } |
144 | 163 |
|
| 164 | + @Test |
| 165 | + public void testJoinConditionComparingStringsCastFromBinary() { |
| 166 | + // FE leaves CAST(X'FF' AS STRING) for BE because no FE string literal carries 0xFF, so a nullable IN |
| 167 | + // subquery keeps the cast in a mark join condition, which compares two strings. |
| 168 | + PlanChecker.from(connectContext) |
| 169 | + .analyze("select cast(X'FF' as string) in (select encoded from source_bytes)").rewrite(); |
| 170 | + PlanChecker.from(connectContext) |
| 171 | + .analyze("select cast(X'610062' as string) not in (select encoded from source_bytes)").rewrite(); |
| 172 | + |
| 173 | + // CheckAfterRewrite rejects a join condition only when it compares VARBINARY values. |
| 174 | + Plan left = new LogicalOneRowRelation(new RelationId(1), |
| 175 | + ImmutableList.of(new Alias(new VarBinaryLiteral(new byte[] {(byte) 0xFF}), "l"))); |
| 176 | + Plan right = new LogicalOneRowRelation(new RelationId(2), |
| 177 | + ImmutableList.of(new Alias(new VarBinaryLiteral(new byte[] {(byte) 0xFF}), "r"))); |
| 178 | + Slot l = left.getOutput().get(0); |
| 179 | + Slot r = right.getOutput().get(0); |
| 180 | + Expression strings = new EqualTo(new Cast(l, StringType.INSTANCE), new Cast(r, StringType.INSTANCE)); |
| 181 | + for (boolean mark : new boolean[] {false, true}) { |
| 182 | + PlanChecker.from(connectContext, join(left, right, strings, mark)).applyBottomUp(new CheckAfterRewrite()); |
| 183 | + AnalysisException error = Assertions.assertThrows(AnalysisException.class, |
| 184 | + () -> PlanChecker.from(connectContext, join(left, right, new EqualTo(l, r), mark)) |
| 185 | + .applyBottomUp(new CheckAfterRewrite())); |
| 186 | + Assertions.assertTrue(error.getMessage().contains("varbinary type could not in join equal conditions"), |
| 187 | + error.getMessage()); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private static Plan join(Plan left, Plan right, Expression condition, boolean mark) { |
| 192 | + List<Expression> conditions = ImmutableList.of(condition); |
| 193 | + return new LogicalJoin<>(mark ? JoinType.LEFT_SEMI_JOIN : JoinType.INNER_JOIN, |
| 194 | + mark ? ImmutableList.of() : conditions, ImmutableList.of(), mark ? conditions : ImmutableList.of(), |
| 195 | + new DistributeHint(DistributeType.NONE), |
| 196 | + mark ? Optional.of(new MarkJoinSlotReference("mark")) : Optional.empty(), left, right, null); |
| 197 | + } |
| 198 | + |
145 | 199 | @Test |
146 | 200 | public void testView() throws Exception { |
147 | 201 | Assertions.assertEquals("X'0080FF'", new VarBinaryLiteral("0080FF").toSql()); |
|
0 commit comments