From 62a1cd9b16af589d51b16efa07d72c8a8eb10510 Mon Sep 17 00:00:00 2001 From: Anchal Kejriwal Date: Fri, 8 Mar 2024 15:39:25 +0530 Subject: [PATCH] PHOENIX-7185 Error message in SequenceNotFoundException is misleading --- .../phoenix/exception/SQLExceptionCode.java | 6 +++--- .../phoenix/exception/SQLExceptionInfo.java | 16 ++++++++++++++++ .../apache/phoenix/schema/MetaDataClient.java | 2 +- .../java/org/apache/phoenix/schema/Sequence.java | 6 +++--- .../schema/SequenceNotFoundException.java | 11 +++++------ .../org/apache/phoenix/util/SequenceUtil.java | 4 ++-- .../org/apache/phoenix/end2end/SequenceIT.java | 1 + 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java b/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java index c3596b4f11c..c70f3830324 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java @@ -364,13 +364,13 @@ public SQLException newException(SQLExceptionInfo info) { SEQUENCE_ALREADY_EXIST(1200, "42Z00", "Sequence already exists.", new Factory() { @Override public SQLException newException(SQLExceptionInfo info) { - return new SequenceAlreadyExistsException(info.getSchemaName(), info.getTableName()); + return new SequenceAlreadyExistsException(info.getSchemaName(), info.getSequenceName()); } }), SEQUENCE_UNDEFINED(1201, "42Z01", "Sequence undefined.", new Factory() { @Override public SQLException newException(SQLExceptionInfo info) { - return new SequenceNotFoundException(info.getSchemaName(), info.getTableName()); + return new SequenceNotFoundException(info.getSchemaName(), info.getSequenceName()); } }), START_WITH_MUST_BE_CONSTANT(1202, "42Z02", "Sequence START WITH value must be an integer or long constant."), @@ -392,7 +392,7 @@ public SQLException newException(SQLExceptionInfo info) { AUTO_PARTITION_SEQUENCE_UNDEFINED(1217, "42Z17", "Auto Partition Sequence undefined", new Factory() { @Override public SQLException newException(SQLExceptionInfo info) { - return new SequenceNotFoundException(info.getSchemaName(), info.getTableName()); + return new SequenceNotFoundException(info.getSchemaName(), info.getSequenceName()); } }), CANNOT_UPDATE_PK_ON_DUP_KEY(1218, "42Z18", "Primary key columns may not be udpated in ON DUPLICATE KEY UPDATE clause." ), diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionInfo.java b/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionInfo.java index 80f7297551c..ef43efd032c 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionInfo.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionInfo.java @@ -45,6 +45,7 @@ public class SQLExceptionInfo { public static final String MAX_PHOENIX_COLUMN_SIZE_BYTES = "maxPhoenixColumnSizeBytes"; public static final String PHOENIX_COLUMN_SIZE_BYTES = "phoenixColumnSizeBytes"; public static final String HA_GROUP_INFO = "haGroupInfo"; + public static final String SEQUENCE_NAME = "sequenceName"; private final Throwable rootCause; private final SQLExceptionCode code; // Should always have one. @@ -61,6 +62,7 @@ public class SQLExceptionInfo { private final int phoenixColumnSizeBytes; private final int maxPhoenixColumnSizeBytes; private final String haGroupInfo; + private final String sequenceName; public static class Builder { private Throwable rootCause; @@ -78,6 +80,7 @@ public static class Builder { private int phoenixColumnSizeBytes; private int maxPhoenixColumnSizeBytes; private String haGroupInfo; + private String sequenceName; public Builder(SQLExceptionCode code) { this.code = code; @@ -161,6 +164,11 @@ public SQLExceptionInfo build() { public String toString() { return code.toString(); } + + public Builder setSequenceName(String sequenceName) { + this.sequenceName = sequenceName; + return this; + } } private SQLExceptionInfo(Builder builder) { @@ -179,6 +187,7 @@ private SQLExceptionInfo(Builder builder) { maxPhoenixColumnSizeBytes = builder.maxPhoenixColumnSizeBytes; phoenixColumnSizeBytes = builder.phoenixColumnSizeBytes; haGroupInfo = builder.haGroupInfo; + sequenceName = builder.sequenceName; } @Override @@ -192,6 +201,10 @@ public String toString() { builder.append(" ").append(message); } } + if (sequenceName != null) { + builder.append(" ").append(SEQUENCE_NAME).append("=").append(sequenceName); + return builder.toString(); + } if (functionName != null) { builder.append(" ").append(FUNCTION_NAME).append("=").append(functionName); return builder.toString(); @@ -288,4 +301,7 @@ public int getPhoenixColumnSizeBytes() { public String getHaGroupInfo() { return haGroupInfo; } + public String getSequenceName() { + return sequenceName; + } } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java index ea738bfe55f..20e93dd913c 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java @@ -3415,7 +3415,7 @@ public boolean handleCreateTableMutationCode(MetaDataMutationResult result, Muta throw new ConcurrentTableMutationException(schemaName, tableName); case AUTO_PARTITION_SEQUENCE_NOT_FOUND: throw new SQLExceptionInfo.Builder(SQLExceptionCode.AUTO_PARTITION_SEQUENCE_UNDEFINED) - .setSchemaName(schemaName).setTableName(tableName).build().buildException(); + .setSchemaName(schemaName).setSequenceName(tableName).build().buildException(); case CANNOT_COERCE_AUTO_PARTITION_ID: case UNABLE_TO_CREATE_CHILD_LINK: case PARENT_TABLE_NOT_FOUND: diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/Sequence.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/Sequence.java index a9eff9bc3ad..ddc111eac6a 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/Sequence.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/Sequence.java @@ -345,7 +345,7 @@ public long incrementValue(Result result, ValueOp op, long numToAllocate) throws // } throw new SQLExceptionInfo.Builder(code) .setSchemaName(key.getSchemaName()) - .setTableName(key.getSequenceName()) + .setSequenceName(key.getSequenceName()) .build().buildException(); } // If we found the sequence, we update our cache with the new value @@ -553,7 +553,7 @@ public boolean returnValue(Result result) throws SQLException { // } throw new SQLExceptionInfo.Builder(code) .setSchemaName(key.getSchemaName()) - .setTableName(key.getSequenceName()) + .setSequenceName(key.getSequenceName()) .build().buildException(); } @@ -626,7 +626,7 @@ public long dropSequence(Result result) throws SQLException { // } throw new SQLExceptionInfo.Builder(code) .setSchemaName(key.getSchemaName()) - .setTableName(key.getSequenceName()) + .setSequenceName(key.getSequenceName()) .build().buildException(); } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/SequenceNotFoundException.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/SequenceNotFoundException.java index 29ff87b47d2..8cf2fedacf1 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/SequenceNotFoundException.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/SequenceNotFoundException.java @@ -20,17 +20,16 @@ import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.exception.SQLExceptionInfo; - public class SequenceNotFoundException extends MetaDataEntityNotFoundException { private static final long serialVersionUID = 1L; private static SQLExceptionCode code = SQLExceptionCode.SEQUENCE_UNDEFINED; - public SequenceNotFoundException(String tableName) { - this(null, tableName); + public SequenceNotFoundException(String sequenceName) { + this(null, sequenceName); } - public SequenceNotFoundException(String schemaName, String tableName) { - super(new SQLExceptionInfo.Builder(code).setSchemaName(schemaName).setTableName(tableName).build().toString(), - code.getSQLState(), code.getErrorCode(), schemaName, tableName, null); + public SequenceNotFoundException(String schemaName, String sequenceName) { + super(new SQLExceptionInfo.Builder(code).setSchemaName(schemaName).setSequenceName(sequenceName).build().toString(), + code.getSQLState(), code.getErrorCode(), schemaName, sequenceName, null); } } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/util/SequenceUtil.java b/phoenix-core-client/src/main/java/org/apache/phoenix/util/SequenceUtil.java index b17246cc5ca..52566a52305 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/util/SequenceUtil.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/util/SequenceUtil.java @@ -81,9 +81,9 @@ public static boolean isCycleAllowed(long numToAllocate) { /** * Helper function that returns a {@link SQLException} */ - public static SQLException getException(String schemaName, String tableName, + public static SQLException getException(String schemaName, String sequenceName, SQLExceptionCode code) { - return new SQLExceptionInfo.Builder(code).setSchemaName(schemaName).setTableName(tableName) + return new SQLExceptionInfo.Builder(code).setSchemaName(schemaName).setSequenceName(sequenceName) .build().buildException(); } diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java index 9dc82258ca2..f8618afe1dc 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java @@ -61,6 +61,7 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.apache.phoenix.thirdparty.com.google.common.collect.Lists; +import org.apache.phoenix.schema.SequenceNotFoundException; @Category(ParallelStatsDisabledTest.class)