From a9b5ce98152e5228ca07d8d59625ed16b7c3e62e Mon Sep 17 00:00:00 2001 From: ShaoxunLi <79825592+ShaoxunLi@users.noreply.github.com> Date: Tue, 15 Oct 2024 10:03:06 +0800 Subject: [PATCH] [BugFix] fix issue when using placeholder in column position in prepare stmt (#51812) Signed-off-by: ShaoxunLi <446032208@qq.com> --- .../com/starrocks/analysis/Parameter.java | 19 +++++++++++++++++++ .../starrocks/analysis/PreparedStmtTest.java | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/fe/fe-core/src/main/java/com/starrocks/analysis/Parameter.java b/fe/fe-core/src/main/java/com/starrocks/analysis/Parameter.java index 9b2b8a64db36b..8d446b2a0ce5d 100644 --- a/fe/fe-core/src/main/java/com/starrocks/analysis/Parameter.java +++ b/fe/fe-core/src/main/java/com/starrocks/analysis/Parameter.java @@ -83,4 +83,23 @@ public Type getOriginType() { protected String toSqlImpl() { return "?"; } + + @Override + public int hashCode() { + return slotId; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + + if (obj.getClass() != this.getClass()) { + return false; + } + + Parameter parameter = (Parameter) obj; + return this.slotId == parameter.slotId; + } } diff --git a/fe/fe-core/src/test/java/com/starrocks/analysis/PreparedStmtTest.java b/fe/fe-core/src/test/java/com/starrocks/analysis/PreparedStmtTest.java index aaa4607a38fdc..cb26562131a6d 100644 --- a/fe/fe-core/src/test/java/com/starrocks/analysis/PreparedStmtTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/analysis/PreparedStmtTest.java @@ -30,6 +30,8 @@ import org.junit.BeforeClass; import org.junit.Test; +import java.util.HashSet; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -97,6 +99,22 @@ public void testPrepareEnable() { Assert.assertFalse(executor.isForwardToLeader()); } + @Test + public void testPrepareWithSelectConst() throws Exception { + String sql = "PREPARE stmt1 FROM select ?, ?, ?;"; + PrepareStmt stmt = (PrepareStmt) UtFrameUtils.parseStmtWithNewParser(sql, ctx); + Assert.assertEquals(3, stmt.getParameters().size()); + + HashSet idSet = new HashSet(); + for (Expr expr : stmt.getParameters()) { + Assert.assertEquals(true, idSet.add(expr.hashCode())); + } + + Assert.assertEquals(false, stmt.getParameters().get(0).equals(stmt.getParameters().get(1))); + Assert.assertEquals(false, stmt.getParameters().get(1).equals(stmt.getParameters().get(2))); + Assert.assertEquals(false, stmt.getParameters().get(0).equals(stmt.getParameters().get(2))); + } + @Test public void testPrepareStatementParser() { String sql = "PREPARE stmt1 FROM insert into demo.prepare_stmt values (?, ?, ?, ?);";