Skip to content

Commit

Permalink
Fixed CORE-6084 and CORE-6376.
Browse files Browse the repository at this point in the history
CORE-6084 - CREATE SEQUENCE START WITH has wrong initial value
CORE-6376 - IDENTITY column with explicit START WITH or INCREMENT BY starts with wrong value
  • Loading branch information
asfernandes committed Aug 5, 2020
1 parent 945cec0 commit 23dc0c6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/dsql/DdlNodes.epp
Expand Up @@ -5830,7 +5830,8 @@ bool CreateAlterSequenceNode::executeAlter(thread_db* tdbb, DsqlCompilerScratch*
const SINT64 oldValue = !X.RDB$INITIAL_VALUE.NULL ? X.RDB$INITIAL_VALUE : 0;
const SINT64 newValue = value.specified ? value.value : oldValue;

transaction->getGenIdCache()->put(id, newValue);
transaction->getGenIdCache()->put(id,
newValue - (!X.RDB$GENERATOR_INCREMENT.NULL ? X.RDB$GENERATOR_INCREMENT : 1));

if (newValue != oldValue)
{
Expand Down Expand Up @@ -5916,7 +5917,7 @@ SSHORT CreateAlterSequenceNode::store(thread_db* tdbb, jrd_tra* transaction, con

// The STORE above has caused the DFW item to be posted, so we just adjust the cached
// generator value.
transaction->getGenIdCache()->put(storedId, val);
transaction->getGenIdCache()->put(storedId, val - step);

This comment has been minimized.

Copy link
@dyemanov

dyemanov Aug 6, 2020

Member

Does this mean that if sequence was created with e.g. INCREMENT 10, then GEN_ID(SEQ, 1) will return -9?

This comment has been minimized.

Copy link
@asfernandes

asfernandes Aug 6, 2020

Author Member

With default initial value of 1, it will start with a current value of -9, so first GEN_ID(SEQ, 1) will be -8.

return storedId;
}
Expand Down Expand Up @@ -8137,8 +8138,11 @@ void AlterRelationNode::modifyField(thread_db* tdbb, DsqlCompilerScratch* dsqlSc

if (clause->identityOptions->restart)
{
const SINT64 val = clause->identityOptions->startValue
.orElse(!GEN.RDB$INITIAL_VALUE.NULL ? GEN.RDB$INITIAL_VALUE : 0);
const SINT64 val =
clause->identityOptions->startValue
.orElse(!GEN.RDB$INITIAL_VALUE.NULL ? GEN.RDB$INITIAL_VALUE : 0) -
clause->identityOptions->increment
.orElse(!GEN.RDB$GENERATOR_INCREMENT.NULL ? GEN.RDB$GENERATOR_INCREMENT : 1);

transaction->getGenIdCache()->put(id, val);
}
Expand Down

0 comments on commit 23dc0c6

Please sign in to comment.