[fix](show-load) fix show load with the same column name in Where Clause (#7523)

This commit is contained in:
Henry2SS
2022-01-15 09:54:43 +08:00
committed by GitHub
parent be43316f20
commit 4a3cbf52e3
2 changed files with 49 additions and 2 deletions

View File

@ -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;

View File

@ -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));
}
}