From 4a3cbf52e383ac0dfe3e1094750ccc69edf40861 Mon Sep 17 00:00:00 2001 From: Henry2SS <45096548+Henry2SS@users.noreply.github.com> Date: Sat, 15 Jan 2022 09:54:43 +0800 Subject: [PATCH] [fix](show-load) fix show load with the same column name in Where Clause (#7523) --- .../apache/doris/analysis/ShowLoadStmt.java | 10 +++++ .../doris/analysis/ShowLoadStmtTest.java | 41 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowLoadStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowLoadStmt.java index afee7145c1..20cabde7a6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowLoadStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowLoadStmt.java @@ -134,6 +134,8 @@ public class ShowLoadStmt extends ShowStmt { throw new AnalysisException("Only allow compound predicate with operator AND"); } + // check whether left.columnName equals to right.columnName + checkPredicateName(cp.getChild(0), cp.getChild(1)); analyzeSubPredicate(cp.getChild(0)); analyzeSubPredicate(cp.getChild(1)); } else { @@ -156,6 +158,14 @@ public class ShowLoadStmt extends ShowStmt { } } + private void checkPredicateName(Expr leftChild, Expr rightChild) throws AnalysisException { + String leftChildColumnName = ((SlotRef) leftChild.getChild(0)).getColumnName(); + String rightChildColumnName = ((SlotRef) rightChild.getChild(0)).getColumnName(); + if (leftChildColumnName.equals(rightChildColumnName)) { + throw new AnalysisException("column names on both sides of operator AND should be diffrent"); + } + } + private void analyzeSubPredicate(Expr subExpr) throws AnalysisException { if (subExpr == null) { return; diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowLoadStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowLoadStmtTest.java index 66d807ade9..2b7e3cd399 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowLoadStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowLoadStmtTest.java @@ -18,9 +18,12 @@ package org.apache.doris.analysis; import org.apache.doris.analysis.BinaryPredicate.Operator; +import org.apache.doris.analysis.CompoundPredicate; +import org.apache.doris.analysis.LikePredicate; import org.apache.doris.catalog.Catalog; import org.apache.doris.catalog.FakeCatalog; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ExceptionChecker; import org.apache.doris.common.UserException; import org.apache.doris.system.SystemInfoService; @@ -110,8 +113,7 @@ public class ShowLoadStmtTest { Assert.assertEquals("SHOW LOAD FROM `testCluster:testDb` WHERE `label` = \'abc\' LIMIT 10", stmt.toString()); StringLiteral stringLiteralLike = new StringLiteral("ab%"); - LikePredicate likePredicate = new LikePredicate(org.apache.doris.analysis.LikePredicate.Operator.LIKE, - slotRef, stringLiteralLike); + LikePredicate likePredicate = new LikePredicate(LikePredicate.Operator.LIKE, slotRef, stringLiteralLike); stmt = new ShowLoadStmt(null, likePredicate, null, new LimitElement(10)); stmt.analyze(analyzer); @@ -122,4 +124,39 @@ public class ShowLoadStmtTest { stmt.analyze(analyzer); Assert.assertEquals("SHOW LOAD FROM `testCluster:testDb` WHERE `state` = \'PENDING\' LIMIT 10", stmt.toString()); } + + @Test + public void testInvalidWhereClause() throws AnalysisException { + //test: WHERE label="abc" AND label LIKE "def"; --> AnalysisException + SlotRef slotRef1 = new SlotRef(null, "label"); + StringLiteral stringLiteral1 = new StringLiteral("abc"); + BinaryPredicate binaryPredicate1 = new BinaryPredicate(BinaryPredicate.Operator.EQ, slotRef1, stringLiteral1); + + SlotRef slotRef2 = new SlotRef(null, "label"); + StringLiteral stringLiteral2 = new StringLiteral("def"); + LikePredicate likePredicate = new LikePredicate(LikePredicate.Operator.LIKE, slotRef2, stringLiteral2); + + CompoundPredicate compoundPredicate1 = new CompoundPredicate(CompoundPredicate.Operator.AND, binaryPredicate1, likePredicate); + ShowLoadStmt stmt1 = new ShowLoadStmt(null, compoundPredicate1, null, null); + + ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "column names on both sides of operator AND should be diffrent", + () -> stmt1.analyze(analyzer)); + + //test: WHERE state="abc" AND state="def"; --> AnalysisException + SlotRef slotRef3 = new SlotRef(null, "state"); + StringLiteral stringLiteral3 = new StringLiteral("abc"); + BinaryPredicate binaryPredicate3 = new BinaryPredicate(BinaryPredicate.Operator.EQ, slotRef3, stringLiteral3); + + SlotRef slotRef4 = new SlotRef(null, "state"); + StringLiteral stringLiteral4 = new StringLiteral("def"); + BinaryPredicate binaryPredicate4 = new BinaryPredicate(BinaryPredicate.Operator.EQ, slotRef4, stringLiteral4); + + CompoundPredicate compoundPredicate2 = new CompoundPredicate(CompoundPredicate.Operator.AND, binaryPredicate3, binaryPredicate4); + ShowLoadStmt stmt2 = new ShowLoadStmt(null, compoundPredicate2, null, null); + + ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "column names on both sides of operator AND should be diffrent", + () -> stmt2.analyze(analyzer)); + + + } }