1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-06-04 21:02:13 +03:00

MCOL-477 Change autoincrement on ALTER TABLE

This patch allows the following syntax to change the current
autoincrement value for the table:

ALTER TABLE table_name COMMENT='autoincrement=value';

Where "value" is the new integer to be used.
This commit is contained in:
Andrew Hutchings 2017-01-05 15:03:54 +00:00
parent 30bd422537
commit bb800e4771
9 changed files with 210 additions and 7 deletions

View File

@ -185,6 +185,22 @@ namespace ddlpackage {
AtaTableComment::AtaTableComment(const char *tableComment) :
fTableComment(tableComment)
{
}
AtaTableComment::~AtaTableComment()
{
}
std::ostream& AtaTableComment::put(std::ostream& os) const
{
os << "TableComment: " << fTableComment << endl;
return os;
}
AtaAddColumns::~AtaAddColumns()
{

View File

@ -66,7 +66,7 @@ void ddlerror(struct pass_to_bison* x, char const *s);
char* copy_string(const char *str);
%}
%expect 16
%expect 17
%pure-parser
%lex-param {void * scanner}
%parse-param {struct ddlpackage::pass_to_bison * x}
@ -162,6 +162,7 @@ VARYING WITH ZONE DOUBLE IDB_FLOAT REAL CHARSET IDB_IF EXISTS CHANGE TRUNCATE
%type <str> literal
%type <matchType> opt_match_type
%type <matchType> match_type
%type <ata> alter_table_comment
%type <ata> modify_column
%type <columnType> numeric_type
%type <constraintList> column_qualifier_list
@ -484,10 +485,6 @@ alter_table_statement:
{
$$ = new AlterTableStatement($3, $4);
}
| ALTER TABLE table_name alter_table_actions COMMENT string_literal
{
$$ = new AlterTableStatement($3, $4);
}
;
alter_table_actions:
@ -524,8 +521,18 @@ alter_table_action:
| ata_rename_table
| modify_column
| rename_column
| alter_table_comment
;
alter_table_comment:
COMMENT opt_equal string_literal
{$$ = new AtaTableComment($3);}
|
COMMENT string_literal
{$$ = new AtaTableComment($2);}
;
modify_column:
MODIFY column_name data_type

View File

@ -282,7 +282,8 @@ const std::string AlterActionString[] =
"AtaDropTableConstraint",
"AtaRenameTable",
"AtaModifyColumnType",
"AtaRenameColumn"
"AtaRenameColumn",
"AtaTableComment"
};
/** @brief Datatype Length list
*
@ -340,6 +341,7 @@ enum DDL_SERIAL_TYPE {
DDL_ATA_RENAME_TABLE,
DDL_ATA_RENAME_COLUMN,
DDL_ATA_MODIFY_COLUMN_TYPE,
DDL_ATA_TABLE_COMMENT,
DDL_COLUMN_TYPE,
DDL_COLUMN_DEFAULT_VALUE,
DDL_TABLE_UNIQUE_CONSTRAINT_DEF,
@ -855,6 +857,28 @@ struct AtaRenameTable : public AlterTableAction
QualifiedName *fQualifiedName;
};
/** alter table comment */
struct AtaTableComment : public AlterTableAction
{
/** @brief Deserialize from ByteStream */
virtual int unserialize(messageqcpp::ByteStream& bs);
/** @brief Serialize to ByteStream */
virtual int serialize(messageqcpp::ByteStream& bs);
/** @brief Ctor for deserialization */
AtaTableComment() : fTableComment("")
{}
AtaTableComment(const char *tableComment);
/** @brief Dump to stdout. */
std::ostream& put(std::ostream& os) const;
virtual ~AtaTableComment();
std::string fTableComment;
};
/** @brief alter table modify column */

View File

@ -184,6 +184,11 @@ int AlterTableStatement::unserialize(ByteStream& bytestream)
ata->unserialize(bytestream);
fActions.push_back( ata );
break;
case DDL_ATA_TABLE_COMMENT:
ata = new AtaTableComment();
ata->unserialize(bytestream);
fActions.push_back( ata );
break;
default:
throw("Bad typecode for AlterTableAction");
break;
@ -917,6 +922,33 @@ int AtaDropTableConstraint::serialize(ByteStream& bytestream)
}
///////////////////////////////////////
/// AtaTableComment Serialization
///////////////////////////////////////
/** @brief Construct from Bytestream */
int AtaTableComment::unserialize(ByteStream& bytestream)
{
int ret=1;
// read table constraint
bytestream >> fTableComment;
return ret;
}
/** @brief Serialize to ByteStream */
int AtaTableComment::serialize(ByteStream& bytestream)
{
int ret=1;
bytestream << (quadbyte) DDL_ATA_TABLE_COMMENT;
bytestream << fTableComment;
return ret;
}
///////////////////////////////////////
/// AtaRenameTable Serialization

View File

@ -27,6 +27,8 @@
using namespace std;
#include <boost/shared_ptr.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include "altertableprocessor.h"
@ -435,6 +437,11 @@ AlterTableProcessor::DDLResult AlterTableProcessor::processPackage(ddlpackage::A
renameColumn (alterTableStmt.fSessionID, txnID.id, result, *(dynamic_cast<AtaRenameColumn*> (*action_iterator)), *(alterTableStmt.fTableName), uniqueId);
}
else if (s.find(AlterActionString[11]) != string::npos)
{
// Table Comment
tableComment (alterTableStmt.fSessionID, txnID.id, result, *(dynamic_cast<AtaTableComment*> (*action_iterator)), *(alterTableStmt.fTableName), uniqueId);
}
else
{
throw std::runtime_error("Altertable: Error in the action type");
@ -1790,6 +1797,106 @@ cout << "create table got unknown exception" << endl;
throw std::runtime_error(errorMsg);
}
void AlterTableProcessor::tableComment(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
DDLResult& result, ddlpackage::AtaTableComment& ataTableComment,
ddlpackage::QualifiedName& fTableName, const uint64_t uniqueId)
{
// Currently only process autoincrement values in table comments during alter
SUMMARY_INFO("AlterTableProcessor::tableComment");
uint64_t nextVal;
BRM::OID_t sysOid = 1001;
ByteStream::byte rc = 0;
boost::shared_ptr<messageqcpp::ByteStream> bsIn;
uint16_t dbRoot;
std::string errorMsg;
int pmNum = 1;
rc = fDbrm->getSysCatDBRoot(sysOid, dbRoot);
OamCache * oamcache = OamCache::makeOamCache();
boost::shared_ptr<std::map<int, int> > dbRootPMMap = oamcache->getDBRootToPMMap();
pmNum = (*dbRootPMMap)[dbRoot];
if (rc != 0)
throw std::runtime_error("Error while calling getSysCatDBRoot");
boost::shared_ptr<CalpontSystemCatalog> systemCatalogPtr =
CalpontSystemCatalog::makeCalpontSystemCatalog(sessionID);
boost::algorithm::to_upper(ataTableComment.fTableComment);
boost::regex compat("[[:space:]]*AUTOINCREMENT[[:space:]]*=[[:space:]]*", boost::regex_constants::extended);
boost::match_results<std::string::const_iterator> what;
std::string::const_iterator start, end;
start = ataTableComment.fTableComment.begin();
end = ataTableComment.fTableComment.end();
boost::match_flag_type flags = boost::match_default;
if (boost::regex_search(start, end, what, compat, flags) && what[0].matched)
{
std::string params(&(*(what[0].second)));
char *ep = NULL;
const char *str = params.c_str();
errno = 0;
nextVal = strtoull(str, &ep, 10);
if ((ep == str) || (*ep != '\0') || (errno != 0))
{
throw std::runtime_error(IDBErrorInfo::instance()->errorMsg(ERR_INVALID_START_VALUE));
}
}
else
{
throw std::runtime_error("Invalid table comment");
}
// Get the OID for autoinc (if exists)
CalpontSystemCatalog::TableName tableName;
tableName.schema = fTableName.fSchema;
tableName.table = fTableName.fName;
CalpontSystemCatalog::TableInfo tblInfo = systemCatalogPtr->tableInfo(tableName);
if (tblInfo.tablewithautoincr != 1)
{
throw std::runtime_error("Table does not have an autoincrement column");
}
int32_t oid = systemCatalogPtr->autoColumOid(tableName);
fDbrm->resetAISequence(oid, nextVal);
ByteStream bs;
bs.restart();
bs << (ByteStream::byte) WE_SVR_UPDATE_SYSCOLUMN_AUTOVAL;
bs << uniqueId;
bs << oid;
bs << nextVal;
bs << sessionID;
try
{
fWEClient->write(bs, (uint32_t)pmNum);
while (1)
{
bsIn.reset(new ByteStream());
fWEClient->read(uniqueId, bsIn);
if ( bsIn->length() == 0 ) //read error
{
rc = NETWORK_ERROR;
errorMsg = "Lost connection to Write Engine Server while updating SYSTABLES";
break;
}
else {
*bsIn >> rc;
*bsIn >> errorMsg;
break;
}
}
}
catch (runtime_error& ex) //write error
{
rc = NETWORK_ERROR;
errorMsg = ex.what();
}
catch (...)
{
rc = NETWORK_ERROR;
errorMsg = " Unknown exception caught while updating SYSTABLE.";
}
if (rc != 0)
throw std::runtime_error(errorMsg);
}
void AlterTableProcessor::renameColumn(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
DDLResult& result, ddlpackage::AtaRenameColumn& ataRenameColumn,
ddlpackage::QualifiedName& fTableName, const uint64_t uniqueId)

View File

@ -136,6 +136,17 @@ namespace ddlpackageprocessor
ddlpackage::AtaRenameColumn& ataRenameColumn,
ddlpackage::QualifiedName& fTableName, const uint64_t uniqueId);
/** @brief change a table autoincrement via a comment
*
* @param result the result of the operation
* @param ataTableComment the AtaTableComment object
* @param fTableName the QualifiedName for the table
*/
EXPORT void tableComment(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
ddlpackage::AtaTableComment& ataTableComment,
ddlpackage::QualifiedName& fTableName, const uint64_t uniqueId);
protected:
void rollBackAlter(const std::string& error, BRM::TxnID txnID, int sessionId, DDLResult& result, uint64_t uniqueId);

View File

@ -1829,7 +1829,7 @@ int ha_calpont_impl_create_(const char *name, TABLE *table_arg, HA_CREATE_INFO *
algorithm::to_upper(tablecomment);
}
//@Bug 2553 Add a parenthesis around option to group them together
string alterpatstr("ALTER[[:space:]]+TABLE[[:space:]]+.*[[:space:]]+((ADD)|(DROP)|(CHANGE)|(ALTER))[[:space:]]+");
string alterpatstr("ALTER[[:space:]]+TABLE[[:space:]]+.*[[:space:]]+(((ADD)|(DROP)|(CHANGE)|(ALTER)|(COMMENT))[[:space:]]+|(COMMENT=))");
string createpatstr("CREATE[[:space:]]+TABLE[[:space:]]");
bool schemaSyncOnly = false;
bool isCreate = true;

View File

@ -46,6 +46,7 @@ enum ServerMessages
WE_SVR_UPDATE_SYSCOLUMN_NEXTVAL,
WE_SVR_UPDATE_SYSCOLUMN_TABLENAME,
WE_SVR_UPDATE_SYSCOLUMN_AUTO,
WE_SVR_UPDATE_SYSCOLUMN_AUTOVAL,
WE_SVR_UPDATE_SYSCOLUMN_COLPOS,
WE_SVR_UPDATE_SYSCOLUMN_RENAMECOLUMN,
WE_SVR_UPDATE_SYSTABLE_TABLENAME,

View File

@ -245,6 +245,11 @@ void DmlReadThread::operator()()
rc = fWeDDLprocessor->updateSyscolumnNextvalCol(ibs, errMsg);
break;
}
case WE_SVR_UPDATE_SYSCOLUMN_AUTOVAL:
{
rc = fWeDDLprocessor->updateSyscolumnNextval(ibs, errMsg);
break;
}
case WE_SVR_UPDATE_SYSCOLUMN_DEFAULTVAL:
{
rc = fWeDDLprocessor->updateSyscolumnSetDefault(ibs, errMsg);