Repository: trafodion Updated Branches: refs/heads/master 1ee3de706 -> 877339a11
nonnull pkey, commit #1 Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/5724b2e6 Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/5724b2e6 Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/5724b2e6 Branch: refs/heads/master Commit: 5724b2e6c1b2a955ee4ab4cd6a582d1d74fd1003 Parents: 288ed21 Author: Anoop Sharma <[email protected]> Authored: Mon Jun 4 18:07:45 2018 +0000 Committer: Anoop Sharma <[email protected]> Committed: Mon Jun 4 18:07:45 2018 +0000 ---------------------------------------------------------------------- core/sqf/src/trafconf/tctrace.h | 2 +- core/sql/common/ComAnsiNamePart.cpp | 24 + core/sql/common/ComAnsiNamePart.h | 2 + core/sql/generator/GenPreCode.cpp | 1 + core/sql/optimizer/NATable.cpp | 5 + core/sql/optimizer/NATable.h | 1 + core/sql/parser/ElemDDLConstraintPK.h | 23 +- core/sql/parser/StmtDDLCreate.cpp | 28 +- core/sql/parser/sqlparser.y | 42 +- core/sql/regress/core/EXPECTED056.SB | 6 + core/sql/regress/core/TEST056 | 2 + core/sql/regress/executor/EXPECTED122 | 27 +- core/sql/regress/executor/TEST122 | 5 +- core/sql/regress/privs2/EXPECTED138 | 19 +- core/sql/regress/seabase/EXPECTED020 | 58 ++- core/sql/regress/seabase/EXPECTED031 | 42 +- core/sql/regress/seabase/TEST020 | 4 +- core/sql/sqlcomp/CmpDescribe.cpp | 68 ++- core/sql/sqlcomp/CmpSeabaseDDL.h | 8 +- core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp | 13 +- core/sql/sqlcomp/CmpSeabaseDDLtable.cpp | 668 ++++++++++++++++---------- core/sql/sqlcomp/DefaultConstants.h | 18 +- core/sql/sqlcomp/nadefaults.cpp | 8 +- 23 files changed, 722 insertions(+), 352 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sqf/src/trafconf/tctrace.h ---------------------------------------------------------------------- diff --git a/core/sqf/src/trafconf/tctrace.h b/core/sqf/src/trafconf/tctrace.h index 3e72983..3523c09 100644 --- a/core/sqf/src/trafconf/tctrace.h +++ b/core/sqf/src/trafconf/tctrace.h @@ -80,7 +80,7 @@ private: , const char *key_cmp ); // The number of trace areas held in "traceAreaList" - int numTraceAreas_; + long numTraceAreas_; bool tracingEnabled_; // Save area for retaining prior trace settings if tracing is http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/common/ComAnsiNamePart.cpp ---------------------------------------------------------------------- diff --git a/core/sql/common/ComAnsiNamePart.cpp b/core/sql/common/ComAnsiNamePart.cpp index 1b3b756..ec59e4f 100644 --- a/core/sql/common/ComAnsiNamePart.cpp +++ b/core/sql/common/ComAnsiNamePart.cpp @@ -530,6 +530,30 @@ ComBoolean ComDeriveRandomInternalName( Lng32 nameCharSet, return TRUE; } +// A random name generated by method ComDeriveRandomInternalName has +// the format: [someBytes]_[9-byte timestamp]_[4 uniqueid digits] +// (total 15 bytes suffix) +// [someBytes]_ddddddddd_dddd +// 012345678901234 +// Check if inputName has that format. +ComBoolean ComIsRandomInternalName(const ComString &inputName) +{ + if (inputName.length() <= 15) + return FALSE; + + const char * suffix = &inputName.data()[inputName.length()-15]; + + if (NOT ((suffix[0] == '_') && (suffix[10] == '_'))) + return FALSE; + + // check that timestamp and uniqueid bytes are all numbers + if ((str_atoi(&suffix[1], 9) < 0) || + (str_atoi(&suffix[11], 4) < 0)) + return FALSE; + + return TRUE; +} + // // --------------------------------------------------------------------- // // InternalIdentifierHasDivColNamePrefix() returns TRUE if the internal // // identifier has the DIVISION_ name prefix; otherwise, returns FALSE. http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/common/ComAnsiNamePart.h ---------------------------------------------------------------------- diff --git a/core/sql/common/ComAnsiNamePart.h b/core/sql/common/ComAnsiNamePart.h index 7b64171..76cf7a2 100644 --- a/core/sql/common/ComAnsiNamePart.h +++ b/core/sql/common/ComAnsiNamePart.h @@ -386,4 +386,6 @@ ComBoolean ComDeriveRandomInternalName( Lng32 nameCharSet, ComString &generatedNameInInternalFormat, NAHeap *h = 0 ); +ComBoolean ComIsRandomInternalName(const ComString &inputName); + #endif // COMANSINAMEPART_H http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/generator/GenPreCode.cpp ---------------------------------------------------------------------- diff --git a/core/sql/generator/GenPreCode.cpp b/core/sql/generator/GenPreCode.cpp index 9832c44..12c501c 100644 --- a/core/sql/generator/GenPreCode.cpp +++ b/core/sql/generator/GenPreCode.cpp @@ -2858,6 +2858,7 @@ short DDLExpr::ddlXnsInfo(NABoolean &isDDLxn, NABoolean &xnCanBeStarted) (ddlNode->getOperatorType() == DDL_CREATE_INDEX) || (ddlNode->getOperatorType() == DDL_POPULATE_INDEX) || (ddlNode->getOperatorType() == DDL_ALTER_TABLE_ALTER_COLUMN_DATATYPE) || + (ddlNode->getOperatorType() == DDL_ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY) || (ddlNode->getOperatorType() == DDL_ALTER_TABLE_ALTER_HBASE_OPTIONS) || (ddlNode->getOperatorType() == DDL_ALTER_INDEX_ALTER_HBASE_OPTIONS) || (ddlNode->getOperatorType() == DDL_ALTER_TABLE_RENAME))) http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/optimizer/NATable.cpp ---------------------------------------------------------------------- diff --git a/core/sql/optimizer/NATable.cpp b/core/sql/optimizer/NATable.cpp index 0ad9b3f..3180d57 100644 --- a/core/sql/optimizer/NATable.cpp +++ b/core/sql/optimizer/NATable.cpp @@ -7712,6 +7712,11 @@ NABoolean NATable::hasSaltedColumn(Lng32 * saltColPos) return FALSE; } +const NABoolean NATable::hasSaltedColumn(Lng32 * saltColPos) const +{ + return ((NATable*)this)->hasSaltedColumn(saltColPos); +} + NABoolean NATable::hasDivisioningColumn(Lng32 * divColPos) { for (CollIndex i=0; i<colArray_.entries(); i++ ) http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/optimizer/NATable.h ---------------------------------------------------------------------- diff --git a/core/sql/optimizer/NATable.h b/core/sql/optimizer/NATable.h index 4feece9..9011583 100644 --- a/core/sql/optimizer/NATable.h +++ b/core/sql/optimizer/NATable.h @@ -575,6 +575,7 @@ public: const char *getHiveOriginalViewText() const { return hiveOrigViewText_; } NABoolean hasSaltedColumn(Lng32 * saltColPos = NULL); + const NABoolean hasSaltedColumn(Lng32 * saltColPos = NULL) const; NABoolean hasDivisioningColumn(Lng32 * divColPos = NULL); void setUpdatable( NABoolean value ) http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/parser/ElemDDLConstraintPK.h ---------------------------------------------------------------------- diff --git a/core/sql/parser/ElemDDLConstraintPK.h b/core/sql/parser/ElemDDLConstraintPK.h index d675094..79b2743 100644 --- a/core/sql/parser/ElemDDLConstraintPK.h +++ b/core/sql/parser/ElemDDLConstraintPK.h @@ -63,16 +63,20 @@ class ElemDDLConstraintPK : public ElemDDLConstraintUnique public: // constructors ElemDDLConstraintPK(ElemDDLNode * pColumnRefList = NULL, - ComPkeySerialization ser = COM_SER_NOT_SPECIFIED) + ComPkeySerialization ser = COM_SER_NOT_SPECIFIED, + NABoolean isNullableSpecified = FALSE) : ElemDDLConstraintUnique(ELM_CONSTRAINT_PRIMARY_KEY_ELEM, pColumnRefList), - ser_(ser) + ser_(ser), + isNullableSpecified_(isNullableSpecified) { } ElemDDLConstraintPK(OperatorTypeEnum operatorType, - ComPkeySerialization ser = COM_SER_NOT_SPECIFIED) + ComPkeySerialization ser = COM_SER_NOT_SPECIFIED, + NABoolean isNullableSpecified = FALSE) : ElemDDLConstraintUnique(operatorType, NULL /*column_reference_list*/), - ser_(ser) + ser_(ser), + isNullableSpecified_(isNullableSpecified) { } // virtual destructor @@ -91,6 +95,8 @@ public: NABoolean serialized() { return (ser_ == ComPkeySerialization::COM_SERIALIZED); } + NABoolean isNullableSpecified() { return isNullableSpecified_; } + private: // if set to SERIALIZED, then pkey will be encoded before passint to hbase. // if set to NOT_SERIALIZED, then primary key will not be encoded before @@ -100,6 +106,9 @@ private: // if not specified, then will be determined based on table type. ComPkeySerialization ser_; + // if set, primary key is nullable. Do not make pkey columns non-nullable + // if NOT NULL is not explicitly specified. + NABoolean isNullableSpecified_; }; // class ElemDDLConstraintPK // ----------------------------------------------------------------------- @@ -111,8 +120,10 @@ class ElemDDLConstraintPKColumn : public ElemDDLConstraintPK public: // constructor - ElemDDLConstraintPKColumn(ComColumnOrdering orderingSpec = COM_ASCENDING_ORDER) - : ElemDDLConstraintPK(ELM_CONSTRAINT_PRIMARY_KEY_COLUMN_ELEM), + ElemDDLConstraintPKColumn(ComColumnOrdering orderingSpec = COM_ASCENDING_ORDER, + NABoolean isNullableSpecified = FALSE) + : ElemDDLConstraintPK(ELM_CONSTRAINT_PRIMARY_KEY_COLUMN_ELEM, + COM_SER_NOT_SPECIFIED, isNullableSpecified), columnOrdering_(orderingSpec) { } http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/parser/StmtDDLCreate.cpp ---------------------------------------------------------------------- diff --git a/core/sql/parser/StmtDDLCreate.cpp b/core/sql/parser/StmtDDLCreate.cpp index 460c16b..3656fb4 100644 --- a/core/sql/parser/StmtDDLCreate.cpp +++ b/core/sql/parser/StmtDDLCreate.cpp @@ -4255,9 +4255,12 @@ StmtDDLCreateTable::synthesize() } NABoolean userSpecifiedPKey = FALSE; - if ((CmpCommon::getDefault(MODE_SPECIAL_1) == DF_ON) || + NABoolean nullablePKeySpecified = FALSE; + if ((CmpCommon::getDefault(TRAF_MAKE_PKEY_COLUMNS_NOT_NULL) == DF_ON) || + (CmpCommon::getDefault(MODE_SPECIAL_1) == DF_ON) || (isVolatile()) || - ((isPkeyStoreByKeylist) && pTableDefBody && (pTableDefBody->castToElemDDLLikeCreateTable() == NULL))) + ((isPkeyStoreByKeylist) && pTableDefBody && + (pTableDefBody->castToElemDDLLikeCreateTable() == NULL))) { NABoolean addPrimaryKeyClause = FALSE; @@ -4388,6 +4391,9 @@ StmtDDLCreateTable::synthesize() keyColsList = currElem->castToElemDDLConstraintPK()->getColumnRefList(); + + nullablePKeySpecified = + currElem->castToElemDDLConstraintPK()->isNullableSpecified(); } } } // while @@ -4493,9 +4499,13 @@ StmtDDLCreateTable::synthesize() // loop over all cols and make nullable columns which are part // of pkey specification, not-null-non-droppable. - // Do this only if cqd VOLATILE_TABLE_FIND_SUITABLE_KEY is - // set to OFF. - if (CmpCommon::getDefault(VOLATILE_TABLE_FIND_SUITABLE_KEY) == DF_OFF) + // For volatile tables, do this only if cqd + // VOLATILE_TABLE_FIND_SUITABLE_KEY is set to OFF. + if (((NOT isVolatile()) && + (CmpCommon::getDefault(TRAF_MAKE_PKEY_COLUMNS_NOT_NULL) == DF_ON) && + (NOT nullablePKeySpecified)) || + ((isVolatile()) && + (CmpCommon::getDefault(VOLATILE_TABLE_FIND_SUITABLE_KEY) == DF_OFF))) { currListElem = pTableDefBody; while (currListElem) @@ -4527,7 +4537,13 @@ StmtDDLCreateTable::synthesize() NABoolean makeThisColNNND = FALSE; if (col->isPrimaryKeyConstraintSpecified()) - makeThisColNNND = TRUE; + { + makeThisColNNND = TRUE; + + if (col->getConstraintPK() && + col->getConstraintPK()->isNullableSpecified()) + makeThisColNNND = FALSE; + } else if (keyColsList NEQ NULL) { // See if this col is in pkey or store by clause. http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/parser/sqlparser.y ---------------------------------------------------------------------- diff --git a/core/sql/parser/sqlparser.y b/core/sql/parser/sqlparser.y index e3f534f..e953fa7 100755 --- a/core/sql/parser/sqlparser.y +++ b/core/sql/parser/sqlparser.y @@ -2744,6 +2744,7 @@ static void enableMakeQuotedStringISO88591Mechanism() %type <tokval> left_outer %type <tokval> optional_col_keyword %type <boolean> optional_cast_spec_not_null_spec +%type <boolean> optional_nullable_pkey %type <boolean> optional_encode_key_ordering_spec %type <CollationType> optional_Collation_type %type <SortDirection> optional_sort_direction @@ -10733,6 +10734,18 @@ optional_cast_spec_not_null_spec : empty $$ = TRUE; // NOT NULL phrase specified } +optional_nullable_pkey: empty + { + if (CmpCommon::getDefault(ALLOW_NULLABLE_UNIQUE_KEY_CONSTRAINT) == DF_OFF) + $$ = FALSE; + else + $$ = TRUE; + } + | TOK_NULLABLE + { + $$ = TRUE; + } + /* type item */ cast_specification : TOK_CAST '(' value_expression TOK_AS Set_Cast_Global_False_and_data_type optional_cast_spec_not_null_spec ')' @@ -25736,7 +25749,7 @@ column_constraint : TOK_NOT TOK_NULL NonISO88591LiteralEncountered = FALSE; $$ = new (PARSERHEAP()) ElemDDLConstraintNotNull(TRUE, PARSERHEAP()); } - | TOK_NOT TOK_NULL TOK_ENABLE + | TOK_NOT TOK_NULL TOK_ENABLE { NonISO88591LiteralEncountered = FALSE; $$ = new (PARSERHEAP()) ElemDDLConstraintNotNull(TRUE, PARSERHEAP()); @@ -25808,16 +25821,20 @@ ddl_ordering_spec : TOK_ASC /* type pElemDDLConstraintUnique */ column_unique_specification : unique_constraint_specification - | TOK_PRIMARY TOK_KEY + | TOK_PRIMARY TOK_KEY optional_nullable_pkey { - $$ = new (PARSERHEAP()) ElemDDLConstraintPKColumn(); + $$ = new (PARSERHEAP()) + ElemDDLConstraintPKColumn( + COM_ASCENDING_ORDER, + $3 /*isNullable*/); } - | TOK_PRIMARY TOK_KEY ddl_ordering_spec + | TOK_PRIMARY TOK_KEY optional_nullable_pkey ddl_ordering_spec { $$ = new (PARSERHEAP()) ElemDDLConstraintPKColumn( - $3 /*ddl_ordering_spec*/); + $4 /*ddl_ordering_spec*/, + $3 /*isNullable*/); } /* type pElemDDLConstraintUnique */ @@ -25828,20 +25845,23 @@ unique_constraint_specification : TOK_UNIQUE /* type pElemDDLConstraintUnique */ unique_specification : unique_constraint_specification - | TOK_PRIMARY TOK_KEY + | TOK_PRIMARY TOK_KEY optional_nullable_pkey { $$ = new (PARSERHEAP()) - ElemDDLConstraintPK(NULL, ComPkeySerialization::COM_SER_NOT_SPECIFIED); + ElemDDLConstraintPK(NULL, ComPkeySerialization::COM_SER_NOT_SPECIFIED, + $3); } - | TOK_PRIMARY TOK_KEY TOK_SERIALIZED + | TOK_PRIMARY TOK_KEY optional_nullable_pkey TOK_SERIALIZED { $$ = new (PARSERHEAP()) - ElemDDLConstraintPK(NULL, ComPkeySerialization::COM_SERIALIZED); + ElemDDLConstraintPK(NULL, ComPkeySerialization::COM_SERIALIZED, + $3); } - | TOK_PRIMARY TOK_KEY TOK_NOT TOK_SERIALIZED + | TOK_PRIMARY TOK_KEY optional_nullable_pkey TOK_NOT TOK_SERIALIZED { $$ = new (PARSERHEAP()) - ElemDDLConstraintPK(NULL, ComPkeySerialization::COM_NOT_SERIALIZED); + ElemDDLConstraintPK(NULL, ComPkeySerialization::COM_NOT_SERIALIZED, + $3); } /* type pElemDDLConstraint */ http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/core/EXPECTED056.SB ---------------------------------------------------------------------- diff --git a/core/sql/regress/core/EXPECTED056.SB b/core/sql/regress/core/EXPECTED056.SB index e574132..1e056d6 100755 --- a/core/sql/regress/core/EXPECTED056.SB +++ b/core/sql/regress/core/EXPECTED056.SB @@ -41,12 +41,18 @@ CREATE UNIQUE INDEX T056IN6 ON TRAFODION.SCH.T056T11 --- SQL operation complete. >> +>>cqd traf_alter_add_pkey_as_unique_constraint 'ON'; + +--- SQL operation complete. >>alter table T056t10 add column duck int default 20 +> not null no heading +>constraint duck_PK PRIMARY KEY desc +>; --- SQL operation complete. +>>cqd traf_alter_add_pkey_as_unique_constraint reset; + +--- SQL operation complete. >> >>insert into T056t10 (swallow,barnowl,duck) values (10,1,201); http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/core/TEST056 ---------------------------------------------------------------------- diff --git a/core/sql/regress/core/TEST056 b/core/sql/regress/core/TEST056 index cb414f0..383cef8 100755 --- a/core/sql/regress/core/TEST056 +++ b/core/sql/regress/core/TEST056 @@ -152,10 +152,12 @@ showddl T056t10; --showlabel index T056in5,detail; showddl T056t11; +cqd traf_alter_add_pkey_as_unique_constraint 'ON'; alter table T056t10 add column duck int default 20 not null no heading constraint duck_PK PRIMARY KEY desc ; +cqd traf_alter_add_pkey_as_unique_constraint reset; insert into T056t10 (swallow,barnowl,duck) values (10,1,201); alter table T056t10 add column gosling int default 700 http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/executor/EXPECTED122 ---------------------------------------------------------------------- diff --git a/core/sql/regress/executor/EXPECTED122 b/core/sql/regress/executor/EXPECTED122 index 59d1416..1b9d968 100644 --- a/core/sql/regress/executor/EXPECTED122 +++ b/core/sql/regress/executor/EXPECTED122 @@ -65,7 +65,7 @@ A >>invoke t122t1; -- Definition of Trafodion table TRAFODION.SCH.T122T1 --- Definition current Tue Sep 20 08:28:14 2016 +-- Definition current Mon Apr 30 07:10:10 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -118,7 +118,7 @@ A >>execute s3; -- Definition of Trafodion table TRAFODION.SCH.T122T1 --- Definition current Tue Sep 20 08:28:14 2016 +-- Definition current Mon Apr 30 07:10:10 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -178,7 +178,7 @@ iv >>invoke t122t1; -- Definition of Trafodion table TRAFODION.SCH.T122T1 --- Definition current Tue Sep 20 08:28:43 2016 +-- Definition current Mon Apr 30 07:10:45 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -225,7 +225,7 @@ iv -- Definition of Trafodion table TRAFODION.SCH.T122T1 --- Definition current Tue Sep 20 08:28:44 2016 +-- Definition current Mon Apr 30 07:10:50 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -518,21 +518,19 @@ LC RC OP OPERATOR OPT DESCRIPTION CARD --- 0 row(s) inserted. >> ->>-- see new pk in t122t4_pk >>explain options 'f' +>insert into t122t4_pk values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), (7,17); LC RC OP OPERATOR OPT DESCRIPTION CARD ---- ---- ---- -------------------- -------- -------------------- --------- -5 . 6 root x 7.00E+000 -3 4 5 nested_join 7.00E+000 -. . 4 trafodion_insert T122_PK 1.00E+000 -1 2 3 nested_join 7.00E+000 +3 . 4 root x 7.00E+000 +1 2 3 tuple_flow 7.00E+000 . . 2 trafodion_insert T122T4_PK 1.00E+000 . . 1 tuplelist 7.00E+000 --- SQL operation complete. +>>-- next insert should fail due to duplicate pkey values >>insert into t122t4_pk values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), >>(7,17); *** ERROR[8102] The operation is prevented by a unique constraint. @@ -590,9 +588,14 @@ LC RC OP OPERATOR OPT DESCRIPTION CARD >>alter table t122t3_unique drop constraint t122_uq; --- SQL operation complete. +>> +>>-- next alter should fail, cannot drop primary key >>alter table t122t4_pk drop constraint t122_pk; ---- SQL operation complete. +*** ERROR[1255] Constraint TRAFODION.SCH.T122_PK is the clustering key constraint for table TRAFODION.SCH.T122T4_PK and cannot be dropped. + +--- SQL operation failed with errors. +>> >>alter table t122t4_fk drop constraint t122_fk; --- SQL operation complete. @@ -666,7 +669,9 @@ LC RC OP OPERATOR OPT DESCRIPTION CARD --- SQL operation complete. >>insert into t122t4_pk values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), >>(7,17); ---- 7 row(s) inserted. +*** ERROR[8102] The operation is prevented by a unique constraint. + +--- 0 row(s) inserted. >> >>-- no ri contraint in t122t4_fk >>explain options 'f' http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/executor/TEST122 ---------------------------------------------------------------------- diff --git a/core/sql/regress/executor/TEST122 b/core/sql/regress/executor/TEST122 index f2e0c72..f2a3337 100755 --- a/core/sql/regress/executor/TEST122 +++ b/core/sql/regress/executor/TEST122 @@ -175,9 +175,9 @@ explain options 'f' insert into t122t3_unique values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), (17,7); insert into t122t3_unique values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), (17,7); --- see new pk in t122t4_pk explain options 'f' insert into t122t4_pk values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), (7,17); +-- next insert should fail due to duplicate pkey values insert into t122t4_pk values (11,1), (12,2), (13,3), (14,4), (15,5), (16,6), (7,17); -- see new ri in t122t4_fk @@ -421,7 +421,10 @@ log LOG122; alter table t122t3_col drop column c3; alter table t122t3_ck drop constraint t122_ck; alter table t122t3_unique drop constraint t122_uq; + +-- next alter should fail, cannot drop primary key alter table t122t4_pk drop constraint t122_pk; + alter table t122t4_fk drop constraint t122_fk; drop index t122t3_index; http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/privs2/EXPECTED138 ---------------------------------------------------------------------- diff --git a/core/sql/regress/privs2/EXPECTED138 b/core/sql/regress/privs2/EXPECTED138 index f8dad07..b57b4fa 100644 --- a/core/sql/regress/privs2/EXPECTED138 +++ b/core/sql/regress/privs2/EXPECTED138 @@ -892,20 +892,14 @@ GRANT REFERENCES CREATE TABLE TRAFODION.T138SCH.USER1_T2 ( - C1 INT DEFAULT NULL + C1 INT DEFAULT NULL NOT NULL NOT DROPPABLE , C2 INT DEFAULT NULL + , CONSTRAINT TRAFODION.T138SCH.USER1_PK PRIMARY KEY (C1 ASC) ) ATTRIBUTES ALIGNED FORMAT ; ALTER TABLE TRAFODION.T138SCH.USER1_T2 ADD CONSTRAINT - TRAFODION.T138SCH.USER1_PK UNIQUE - ( - C1 - ) -; - -ALTER TABLE TRAFODION.T138SCH.USER1_T2 ADD CONSTRAINT TRAFODION.T138SCH.USER1_FK FOREIGN KEY ( C2 @@ -973,8 +967,9 @@ GRANT REFERENCES CREATE TABLE TRAFODION.T138SCH.USER1_T2 ( - C1 INT DEFAULT NULL + C1 INT DEFAULT NULL NOT NULL NOT DROPPABLE , C2 INT DEFAULT NULL + , CONSTRAINT TRAFODION.T138SCH.USER1_PK PRIMARY KEY (C1 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -1104,8 +1099,9 @@ GRANT REFERENCES CREATE TABLE TRAFODION.T138SCH.USER1_T2 ( - C1 INT DEFAULT NULL + C1 INT DEFAULT NULL NOT NULL NOT DROPPABLE , C2 INT DEFAULT NULL + , CONSTRAINT TRAFODION.T138SCH.USER1_PK PRIMARY KEY (C1 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -1183,8 +1179,9 @@ GRANT REFERENCES CREATE TABLE TRAFODION.T138SCH.USER1_T2 ( - C1 INT DEFAULT NULL + C1 INT DEFAULT NULL NOT NULL NOT DROPPABLE , C2 INT DEFAULT NULL + , CONSTRAINT TRAFODION.T138SCH.USER1_PK PRIMARY KEY (C1 ASC) ) ATTRIBUTES ALIGNED FORMAT ; http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/seabase/EXPECTED020 ---------------------------------------------------------------------- diff --git a/core/sql/regress/seabase/EXPECTED020 b/core/sql/regress/seabase/EXPECTED020 index 3c1104b..3fc47cf 100644 --- a/core/sql/regress/seabase/EXPECTED020 +++ b/core/sql/regress/seabase/EXPECTED020 @@ -6,7 +6,7 @@ >>invoke test020t1; -- Definition of Trafodion table TRAFODION.SCH.TEST020T1 --- Definition current Thu Jan 19 17:38:24 2017 +-- Definition current Fri May 4 03:32:59 2018 ( C1 INT NO DEFAULT NOT NULL NOT DROPPABLE @@ -50,7 +50,7 @@ C1 C2 >>invoke test020t2; -- Definition of Trafodion table TRAFODION.SCH.TEST020T2 --- Definition current Thu Jan 19 17:38:28 2017 +-- Definition current Fri May 4 03:33:06 2018 ( C CHAR(15) CHARACTER SET ISO88591 COLLATE @@ -123,12 +123,18 @@ G0 --- SQL operation complete. >> ->>-- The following Create Table statement should fail +>>-- The following Create Table statement should fail, cant have nullable pkey +>>cqd traf_make_pkey_columns_not_null 'OFF'; + +--- SQL operation complete. >>create table test020t31 (g0 smallint no default primary key); *** ERROR[1135] Clustering key column G0 must be assigned a NOT NULL NOT DROPPABLE constraint. --- SQL operation failed with errors. +>>cqd traf_make_pkey_columns_not_null reset; + +--- SQL operation complete. >>drop table if exists test020t31; --- SQL operation complete. @@ -163,7 +169,7 @@ G0 >>invoke test020t5; -- Definition of Trafodion table TRAFODION.SCH.TEST020T5 --- Definition current Thu Jan 19 17:38:51 2017 +-- Definition current Fri May 4 03:33:38 2018 ( C INT NO DEFAULT NOT NULL NOT DROPPABLE @@ -264,7 +270,7 @@ ALTER TABLE TRAFODION.SCH.TEST020T5 ADD CONSTRAINT TRAFODION.SCH.TEST020T5C1 >>invoke test020t6; -- Definition of Trafodion table TRAFODION.SCH.TEST020T6 --- Definition current Thu Jan 19 17:40:10 2017 +-- Definition current Fri May 4 03:34:52 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -1775,7 +1781,7 @@ CREATE TABLE TRAFODION.SCH.TEST020T13 , R2 INT NO DEFAULT NOT NULL NOT DROPPABLE , R3 INT NO DEFAULT NOT NULL NOT DROPPABLE , R4 INT DEFAULT NULL - , PRIMARY KEY (R2 ASC, R3 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T13PK PRIMARY KEY (R2 ASC, R3 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -1797,7 +1803,7 @@ CREATE TABLE TRAFODION.SCH.TEST020T13 , R2 INT NO DEFAULT NOT NULL NOT DROPPABLE , R3 INT NO DEFAULT NOT NULL NOT DROPPABLE , R4 INT DEFAULT NULL - , PRIMARY KEY (R2 ASC, R3 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T13PK PRIMARY KEY (R2 ASC, R3 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -1832,7 +1838,7 @@ CREATE TABLE TRAFODION.SCH.TEST020T13 , R2 INT NO DEFAULT NOT NULL NOT DROPPABLE , R3 INT NO DEFAULT NOT NULL NOT DROPPABLE , R4 INT DEFAULT NULL - , PRIMARY KEY (R2 ASC, R3 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T13PK PRIMARY KEY (R2 ASC, R3 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -1885,7 +1891,7 @@ CREATE TABLE TRAFODION.SCH.TEST020T13 , R2 INT NO DEFAULT NOT NULL NOT DROPPABLE , R3 INT NO DEFAULT NOT NULL NOT DROPPABLE , R4 INT DEFAULT NULL - , PRIMARY KEY (R2 ASC, R3 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T13PK PRIMARY KEY (R2 ASC, R3 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -1963,7 +1969,7 @@ CREATE TABLE TRAFODION.SCH.TEST020T13 , R2 INT NO DEFAULT NOT NULL NOT DROPPABLE , R3 INT NO DEFAULT NOT NULL NOT DROPPABLE , R4 INT DEFAULT NULL - , PRIMARY KEY (R2 ASC, R3 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T13PK PRIMARY KEY (R2 ASC, R3 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -2337,7 +2343,7 @@ ALTER TABLE TRAFODION.SCH.TEST020T14 ADD CONSTRAINT CREATE TABLE TRAFODION.SCH.TEST020T15 ( R1 INT NO DEFAULT NOT NULL NOT DROPPABLE - , PRIMARY KEY (R1 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T15PK PRIMARY KEY (R1 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -2358,7 +2364,7 @@ CREATE TABLE TRAFODION.SCH.TEST020T15 CREATE TABLE TRAFODION.SCH.TEST020T15 ( R1 INT NO DEFAULT NOT NULL NOT DROPPABLE - , PRIMARY KEY (R1 ASC) + , CONSTRAINT TRAFODION.SCH.TEST020T15PK PRIMARY KEY (R1 ASC) ) ATTRIBUTES ALIGNED FORMAT ; @@ -3958,8 +3964,8 @@ AAAA ? 2 +> c varchar(10) default to_char(sysdate,'YYYYMMDD'), +> --support sequence as default in next check-in +> --d int not null default testi020_seq.nextval, -+> e int not null, -+> f int, primary key(e)); ++> e int not null, ++> f int, primary key(e)); --- SQL operation complete. >>-- check if the timestamp is inserted with the recent timestamp @@ -3968,10 +3974,10 @@ AAAA ? 2 --- 3 row(s) inserted. >>select sleep(1) from dual; -(EXPR) --------------------- +(EXPR) +----------- - 1 + 1 --- 1 row(s) selected. >>upsert into test020t45(e,f) values(1,4); @@ -3979,7 +3985,7 @@ AAAA ? 2 --- 1 row(s) inserted. >>select count(distinct(a)),count(distinct(b)),count(distinct(c)) from >>test020t45 ; -(EXPR) (EXPR) (EXPR) +(EXPR) (EXPR) (EXPR) -------------------- -------------------- -------------------- 1 3 1 @@ -3997,10 +4003,10 @@ AAAA ? 2 --- 3 row(s) inserted. >>select sleep(1) from dual; -(EXPR) --------------------- +(EXPR) +----------- - 1 + 1 --- 1 row(s) selected. >>upsert into test020t45(e,f) values(1,4); @@ -4008,7 +4014,7 @@ AAAA ? 2 --- 1 row(s) inserted. >>select count(distinct(a)),count(distinct(b)),count(distinct(c)) from >>test020t45 ; -(EXPR) (EXPR) (EXPR) +(EXPR) (EXPR) (EXPR) -------------------- -------------------- -------------------- 2 3 1 @@ -4026,10 +4032,10 @@ AAAA ? 2 --- 3 row(s) inserted. >>select sleep(1) from dual; -(EXPR) --------------------- +(EXPR) +----------- - 1 + 1 --- 1 row(s) selected. >>upsert into test020t45(e,f) values(1,4); @@ -4037,7 +4043,7 @@ AAAA ? 2 --- 1 row(s) inserted. >>select count(distinct(a)),count(distinct(b)),count(distinct(c)) from >>test020t45 ; -(EXPR) (EXPR) (EXPR) +(EXPR) (EXPR) (EXPR) -------------------- -------------------- -------------------- 1 3 1 http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/seabase/EXPECTED031 ---------------------------------------------------------------------- diff --git a/core/sql/regress/seabase/EXPECTED031 b/core/sql/regress/seabase/EXPECTED031 index 6d8d542..d887692 100644 --- a/core/sql/regress/seabase/EXPECTED031 +++ b/core/sql/regress/seabase/EXPECTED031 @@ -8,7 +8,7 @@ --- SQL operation complete. >>alter table t031t1 add constraint ppk primary key(b); -*** ERROR[1043] Constraint TRAFODION.SCH.PPK already exists. +*** ERROR[1256] PRIMARY KEY constraint cannot be added since table TRAFODION.SCH.T031T1 already has a user specified clustering key. --- SQL operation failed with errors. >>alter table t031t1 add constraint ppk2 primary key(b); @@ -38,12 +38,14 @@ --- SQL operation complete. >>alter table t031t1 add constraint ppk primary key(b); -*** ERROR[1043] Constraint TRAFODION.SCH.PPK already exists. +*** ERROR[1256] PRIMARY KEY constraint cannot be added since table TRAFODION.SCH.T031T1 already has a user specified clustering key. --- SQL operation failed with errors. >>alter table t031t1 add constraint ppk2 primary key(b); ---- SQL operation complete. +*** ERROR[1256] PRIMARY KEY constraint cannot be added since table TRAFODION.SCH.T031T1 already has a user specified clustering key. + +--- SQL operation failed with errors. >>alter table t031t1 add constraint ppk unique(b); *** ERROR[1043] Constraint TRAFODION.SCH.PPK already exists. @@ -51,7 +53,9 @@ --- SQL operation failed with errors. >>alter table t031t1 drop constraint ppk; ---- SQL operation complete. +*** ERROR[1255] Constraint TRAFODION.SCH.PPK is the clustering key constraint for table TRAFODION.SCH.T031T1 and cannot be dropped. + +--- SQL operation failed with errors. >> >>drop table if exists t031t1; @@ -79,7 +83,7 @@ --- SQL operation complete. >>alter table t031t1 add constraint ppk primary key(a); -*** ERROR[1254] Duplicate unique constraints are not allowed with same set of columns. +*** ERROR[1256] PRIMARY KEY constraint cannot be added since table TRAFODION.SCH.T031T1 already has a user specified clustering key. --- SQL operation failed with errors. >>alter table t031t1 add constraint ppk primary key(b); @@ -446,7 +450,7 @@ Z Z (EXPR) (EXPR) >>invoke t031t10; -- Definition of Trafodion table TRAFODION.SCH.T031T10 --- Definition current Sun Mar 11 00:49:14 2018 +-- Definition current Mon Apr 30 10:34:42 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -648,7 +652,7 @@ LC RC OP OPERATOR OPT DESCRIPTION CARD >>invoke t031t1; -- Definition of Trafodion table TRAFODION.SCH.T031T1 --- Definition current Sun Mar 11 00:50:44 2018 +-- Definition current Mon Apr 30 10:35:49 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -679,7 +683,7 @@ A B >>invoke t031t1; -- Definition of Trafodion table TRAFODION.SCH.T031T1 --- Definition current Sun Mar 11 00:50:53 2018 +-- Definition current Mon Apr 30 10:35:52 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -730,7 +734,7 @@ A B C >>invoke t031t1; -- Definition of Trafodion table TRAFODION.SCH.T031T1 --- Definition current Sun Mar 11 00:51:05 2018 +-- Definition current Mon Apr 30 10:35:59 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -759,7 +763,7 @@ A B C >>invoke t031t1; -- Definition of Trafodion table TRAFODION.SCH.T031T1 --- Definition current Sun Mar 11 00:51:23 2018 +-- Definition current Mon Apr 30 10:36:09 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -784,7 +788,7 @@ A B C >>invoke t031t1; -- Definition of Trafodion table TRAFODION.SCH.T031T1 --- Definition current Sun Mar 11 00:51:32 2018 +-- Definition current Mon Apr 30 10:36:14 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -804,7 +808,7 @@ A B C >>invoke t031t1; -- Definition of Trafodion table TRAFODION.SCH.T031T1 --- Definition current Sun Mar 11 00:51:37 2018 +-- Definition current Mon Apr 30 10:36:23 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -819,7 +823,7 @@ A B C >>invoke t031v1; -- Definition of Trafodion view TRAFODION.SCH.T031V1 --- Definition current Sun Mar 11 00:51:41 2018 +-- Definition current Mon Apr 30 10:36:27 2018 ( A INT DEFAULT NULL @@ -842,7 +846,7 @@ A B C >>invoke t031v1; -- Definition of Trafodion view TRAFODION.SCH.T031V1 --- Definition current Sun Mar 11 00:51:45 2018 +-- Definition current Mon Apr 30 10:36:32 2018 ( A INT DEFAULT NULL @@ -860,7 +864,7 @@ A B C >>invoke t031v1; -- Definition of Trafodion view TRAFODION.SCH.T031V1 --- Definition current Sun Mar 11 00:51:52 2018 +-- Definition current Mon Apr 30 10:36:37 2018 ( A INT DEFAULT NULL @@ -947,7 +951,7 @@ CREATE INDEX T031T1I1 ON TRAFODION.SCH.T031T1 >>invoke table(index_table t031t1i1); -- Definition of Trafodion table TRAFODION.SCH.T031T1I1 --- Definition current Sun Mar 11 00:52:20 2018 +-- Definition current Mon Apr 30 10:37:01 2018 ( "A@" INT NO DEFAULT @@ -1092,7 +1096,7 @@ CREATE TABLE T031HIVET1 /* Trafodion DDL */ REGISTER /*INTERNAL*/ HIVE TABLE HIVE.HIVE.T031HIVET1; -/* ObjectUID = 596882092547840046 */ +/* ObjectUID = 8006996101335705124 */ CREATE EXTERNAL TABLE T031HIVET1 FOR HIVE.HIVE.T031HIVET1 @@ -1259,8 +1263,8 @@ A A@ SYSKEY ----------- -------------------- - 1 2046478438118704386 - 2 2046478438119028079 + 1 6923318239647142703 + 2 6923318239647458015 --- 2 row(s) selected. >> http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/regress/seabase/TEST020 ---------------------------------------------------------------------- diff --git a/core/sql/regress/seabase/TEST020 b/core/sql/regress/seabase/TEST020 index 47b6aec..e341a55 100755 --- a/core/sql/regress/seabase/TEST020 +++ b/core/sql/regress/seabase/TEST020 @@ -130,8 +130,10 @@ select * from test020t31; -- The following Drop Table statement should execute successfully drop table test020t31; --- The following Create Table statement should fail +-- The following Create Table statement should fail, cant have nullable pkey +cqd traf_make_pkey_columns_not_null 'OFF'; create table test020t31 (g0 smallint no default primary key); +cqd traf_make_pkey_columns_not_null reset; drop table if exists test020t31; -- The following Create Table statement should fail http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/sqlcomp/CmpDescribe.cpp ---------------------------------------------------------------------- diff --git a/core/sql/sqlcomp/CmpDescribe.cpp b/core/sql/sqlcomp/CmpDescribe.cpp index 97f2014..1620627 100644 --- a/core/sql/sqlcomp/CmpDescribe.cpp +++ b/core/sql/sqlcomp/CmpDescribe.cpp @@ -196,6 +196,7 @@ short CmpDescribeSeabaseTable ( char* &outbuf, ULng32 &outbuflen, CollHeap *heap, + const char * pkeyName = NULL, const char * pkeyStr = NULL, NABoolean withPartns = FALSE, NABoolean withoutSalt = FALSE, @@ -892,7 +893,7 @@ short CmpDescribe(const char *query, const RelExpr *queryExpr, rc = CmpDescribeSeabaseTable(d->getDescribedTableName(), (d->getFormat() == Describe::INVOKE_ ? 1 : 2), - outbuf, outbuflen, heap, NULL, TRUE); + outbuf, outbuflen, heap, NULL, NULL, TRUE); goto finally; // we are done } @@ -2462,7 +2463,7 @@ short CmpDescribeHiveTable ( short rc = CmpDescribeSeabaseTable(cn, type, dummyBuf, dummyLen, heap, - NULL, + NULL, NULL, TRUE, FALSE, FALSE, FALSE, FALSE, UINT_MAX, TRUE, @@ -2893,6 +2894,7 @@ short CmpDescribeSeabaseTable ( char* &outbuf, ULng32 &outbuflen, CollHeap *heap, + const char * pkeyName, const char * pkeyStr, NABoolean withPartns, NABoolean withoutSalt, @@ -3301,23 +3303,75 @@ short CmpDescribeSeabaseTable ( if ((type == 3) && (pkeyStr)) { - outputShortLine(*space, " , PRIMARY KEY "); - + if (pkeyName) + { + NAString pkeyPrefix(", CONSTRAINT "); + pkeyPrefix += NAString(pkeyName) + " PRIMARY KEY "; + outputLine(*space, pkeyPrefix.data(), 0); + } + else + { + outputShortLine(*space, " , PRIMARY KEY "); + } + outputLine(*space, pkeyStr, 2); } else { - if ((naTable->getClusteringIndex()) && + if ((naf) && (nonSystemKeyCols > 0) && (NOT isStoreBy)) { + NAString pkeyConstrName; + NAString pkeyConstrObjectName; + if (type == 2) // showddl + { + const AbstractRIConstraintList &uniqueList = naTable->getUniqueConstraints(); + + for (Int32 i = 0; i < uniqueList.entries(); i++) + { + AbstractRIConstraint *ariConstr = uniqueList[i]; + + UniqueConstraint * uniqConstr = (UniqueConstraint*)ariConstr; + if (uniqConstr->isPrimaryKeyConstraint()) + { + pkeyConstrName = + uniqConstr->getConstraintName().getQualifiedNameAsAnsiString(TRUE); + pkeyConstrObjectName = + uniqConstr->getConstraintName().getObjectName(); + break; + } + } // for + } // type 2 + numBTpkeys = naf->getIndexKeyColumns().entries(); if (type == 1) sprintf(buf, " PRIMARY KEY "); else - sprintf(buf, " , PRIMARY KEY "); - + { + // Display primary key name for showddl (type == 2). + // First check to see if pkey name is a generated random name or + // a user specified name. + // If it is a generated random name, then dont display it. + // This is being done for backward compatibility in showddl + // output as well as avoid the need to update multiple + // regressions files. + // Currently we check to see if the name has random generated + // format to determine whether to display it or not. + // If it so happens that a user specified primary key constraint + // has that exact format, then it will not be displayed. + // At some point in future, we can store in metadata if pkey + // name was internally generated or user specified. + if ((type == 2) && + (NOT pkeyConstrObjectName.isNull()) && + (NOT ComIsRandomInternalName(pkeyConstrObjectName))) + sprintf(buf, " , CONSTRAINT %s PRIMARY KEY ", + pkeyConstrName.data()); + else + sprintf(buf, " , PRIMARY KEY "); + } + // if all primary key columns are 'not serialized primary key', // then display that. NABoolean serialized = FALSE; http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/sqlcomp/CmpSeabaseDDL.h ---------------------------------------------------------------------- diff --git a/core/sql/sqlcomp/CmpSeabaseDDL.h b/core/sql/sqlcomp/CmpSeabaseDDL.h index a08a345..e3046ef 100644 --- a/core/sql/sqlcomp/CmpSeabaseDDL.h +++ b/core/sql/sqlcomp/CmpSeabaseDDL.h @@ -1100,6 +1100,12 @@ protected: ExeCliInterface * cilInterface, NABoolean withCreate); + short cloneAndTruncateTable( + const NATable * naTable, // IN: source table + NAString &tempTable, // OUT: temp table + ExpHbaseInterface * ehi, + ExeCliInterface * cliInterface); + short dropSeabaseTable2( ExeCliInterface *cliInterface, StmtDDLDropTable * dropTableNode, @@ -1453,7 +1459,7 @@ protected: short truncateHbaseTable(const NAString &catalogNamePart, const NAString &schemaNamePart, const NAString &objectNamePart, - NATable * naTable, + const NABoolean hasSaltedColumn, ExpHbaseInterface * ehi); void purgedataHbaseTable(DDLExpr * ddlExpr, http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp ---------------------------------------------------------------------- diff --git a/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp b/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp index b97feba..1fe9c82 100644 --- a/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp +++ b/core/sql/sqlcomp/CmpSeabaseDDLcommon.cpp @@ -4093,7 +4093,6 @@ short CmpSeabaseDDL::getUsingViews(ExeCliInterface *cliInterface, getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_OBJECTS, getSystemCatalog(), SEABASE_MD_SCHEMA, SEABASE_VIEWS_USAGE, objectUID); - cliRC = cliInterface->fetchAllRows(usingViewsQueue, buf, 0, FALSE, FALSE, TRUE); if (cliRC < 0) @@ -6298,6 +6297,10 @@ short CmpSeabaseDDL::buildKeyInfoArray( ComTdbVirtTableKeyInfo::DESCENDING_ORDERING); keyInfoArray[index].nonKeyCol = 0; + ElemDDLColDef *colDef = (*colArray)[keyInfoArray[index].tableColNum]; + if (colDef && colDef->getConstraintPK() && colDef->getConstraintPK()->isNullableSpecified()) + allowNullableUniqueConstr = TRUE; + if ((colInfoArray) && (colInfoArray[keyInfoArray[index].tableColNum].nullable != 0) && (NOT allowNullableUniqueConstr)) @@ -8639,7 +8642,7 @@ void CmpSeabaseDDL::updateVersion() short CmpSeabaseDDL::truncateHbaseTable(const NAString &catalogNamePart, const NAString &schemaNamePart, const NAString &objectNamePart, - NATable * naTable, + const NABoolean hasSaltedColumn, ExpHbaseInterface * ehi) { Lng32 retcode = 0; @@ -8652,7 +8655,7 @@ short CmpSeabaseDDL::truncateHbaseTable(const NAString &catalogNamePart, hbaseTable.len = extNameForHbase.length(); // if salted table, preserve splits. - if (naTable->hasSaltedColumn()) + if (hasSaltedColumn) retcode = ehi->truncate(hbaseTable, TRUE, TRUE); else retcode = ehi->truncate(hbaseTable, FALSE, TRUE); @@ -8836,7 +8839,7 @@ void CmpSeabaseDDL::purgedataHbaseTable(DDLExpr * ddlExpr, NABoolean ddlXns = ddlExpr->ddlXns(); if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart, - naTable, ehi)) + naTable->hasSaltedColumn(), ehi)) { deallocEHI(ehi); @@ -8863,7 +8866,7 @@ void CmpSeabaseDDL::purgedataHbaseTable(DDLExpr * ddlExpr, NAString idxName = qn.getObjectName(); retcode = truncateHbaseTable(catName, schName, idxName, - naTable, ehi); + naTable->hasSaltedColumn(), ehi); if (retcode) { deallocEHI(ehi); http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp ---------------------------------------------------------------------- diff --git a/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp b/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp index 234d2d1..f50d018 100644 --- a/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp +++ b/core/sql/sqlcomp/CmpSeabaseDDLtable.cpp @@ -65,6 +65,7 @@ extern short CmpDescribeSeabaseTable ( char* &outbuf, ULng32 &outbuflen, CollHeap *heap, + const char * pkeyName = NULL, const char * pkeyStr = NULL, NABoolean withPartns = FALSE, NABoolean withoutSalt = FALSE, @@ -383,7 +384,7 @@ void CmpSeabaseDDL::createSeabaseTableLike(ExeCliInterface * cliInterface, likeOptions.getIsLikeOptColumnLengthLimit()); else retcode = CmpDescribeSeabaseTable(cn, 3/*createlike*/, buf, buflen, STMTHEAP, - NULL, + NULL, NULL, likeOptions.getIsWithHorizontalPartitions(), likeOptions.getIsWithoutSalt(), likeOptions.getIsWithoutDivision(), @@ -1259,7 +1260,7 @@ short CmpSeabaseDDL::genUniqueName(StmtDDLAddConstraint *addUniqueNode, specifiedConstraint.append("."); specifiedConstraint.append( tableName.getSchemaNamePartAsAnsiString() ); specifiedConstraint.append("."); - + ComString oName = tableName.getObjectNamePartAsAnsiString() ; Lng32 status = ToInternalIdentifier ( oName // in/out - from external- to internal-format , TRUE // in - NABoolean upCaseInternalNameIfRegularIdent @@ -2290,9 +2291,12 @@ short CmpSeabaseDDL::createSeabaseTable2( // VOLATILE_TABLE_FIND_SUITABLE_KEY is ON, then allow it. // If ALLOW_NULLABLE_UNIQUE_KEY_CONSTRAINT is set, then allow it. NABoolean allowNullableUniqueConstr = FALSE; - if (((CmpCommon::getDefault(VOLATILE_TABLE_FIND_SUITABLE_KEY) != DF_OFF) && - (createTableNode->isVolatile())) || - (CmpCommon::getDefault(ALLOW_NULLABLE_UNIQUE_KEY_CONSTRAINT) == DF_ON)) + if ((CmpCommon::getDefault(VOLATILE_TABLE_FIND_SUITABLE_KEY) != DF_OFF) && + (createTableNode->isVolatile())) + allowNullableUniqueConstr = TRUE; + + if ((createTableNode->getIsConstraintPKSpecified()) && + (createTableNode->getAddConstraintPK()->getAlterTableAction()->castToElemDDLConstraintPK()->isNullableSpecified())) allowNullableUniqueConstr = TRUE; int numIterationsToCompleteColumnList = 1; @@ -2834,7 +2838,8 @@ short CmpSeabaseDDL::createSeabaseTable2( } NABoolean ddlXns = createTableNode->ddlXns(); - if (NOT extNameForHbase.isNull()) + if ((NOT extNameForHbase.isNull()) && + (CmpCommon::getDefault(TRAF_NO_HBASE_DROP_CREATE) == DF_OFF)) { HbaseStr hbaseTable; hbaseTable.val = (char*)extNameForHbase.data(); @@ -3086,10 +3091,8 @@ void CmpSeabaseDDL::addConstraints( if (pkConstr) { - StmtDDLAddConstraintUnique *uniqConstr = pkConstr; - NAString uniqueName; - genUniqueName(uniqConstr, uniqueName); + genUniqueName(pkConstr, uniqueName); ComObjectName constrName(uniqueName); constrName.applyDefaults(currCatAnsiName, currSchAnsiName); @@ -3097,8 +3100,8 @@ void CmpSeabaseDDL::addConstraints( const NAString constrSchemaNamePart = constrName.getSchemaNamePartAsAnsiString(TRUE); const NAString constrObjectNamePart = constrName.getObjectNamePartAsAnsiString(TRUE); - ElemDDLConstraintUnique *constraintNode = - ( uniqConstr->getConstraint() )->castToElemDDLConstraintUnique(); + ElemDDLConstraintPK *constraintNode = + ( pkConstr->getConstraint() )->castToElemDDLConstraintPK(); ElemDDLColRefArray &keyColumnArray = constraintNode->getKeyColumnArray(); NAString keyColNameStr; @@ -3117,9 +3120,10 @@ void CmpSeabaseDDL::addConstraints( keyColNameStr += ", "; } - str_sprintf(buf, "alter table \"%s\".\"%s\".\"%s\" add constraint \"%s\".\"%s\".\"%s\" unique (%s)", + str_sprintf(buf, "alter table \"%s\".\"%s\".\"%s\" add constraint \"%s\".\"%s\".\"%s\" primary key %s (%s)", catalogNamePart.data(), schemaNamePart.data(), objectNamePart.data(), constrCatalogNamePart.data(), constrSchemaNamePart.data(), constrObjectNamePart.data(), + (constraintNode->isNullableSpecified() ? " nullable " : ""), keyColNameStr.data()); cliRC = cliInterface.executeImmediate(buf); @@ -3817,7 +3821,7 @@ short CmpSeabaseDDL::dropSeabaseTable2( cliRC = getUsingObject(cliInterface, objUID, usingObjName); if (cliRC < 0) { - deallocEHI(ehi); + deallocEHI(ehi); processReturn(); return -1; @@ -4434,8 +4438,9 @@ short CmpSeabaseDDL::dropSeabaseTable2( NABoolean dropFromMD = TRUE; NABoolean dropFromHbase = (NOT tableName.isExternalHbase()); - if (dropTableNode->getDropBehavior() == COM_NO_CHECK_DROP_BEHAVIOR) + if (CmpCommon::getDefault(TRAF_NO_HBASE_DROP_CREATE) == DF_ON) dropFromHbase = FALSE; + if (dropSeabaseObject(ehi, tabName, currCatName, currSchName, COM_BASE_TABLE_OBJECT, dropTableNode->ddlXns(), @@ -5175,7 +5180,7 @@ short CmpSeabaseDDL::createSeabaseTableLike2( char * buf = NULL; ULng32 buflen = 0; retcode = CmpDescribeSeabaseTable(cn, 3/*createlike*/, buf, buflen, STMTHEAP, - NULL, + NULL, NULL, withPartns, withoutSalt, withoutDivision, withoutRowFormat, FALSE, // include LOB columns (if any) @@ -5226,6 +5231,8 @@ short CmpSeabaseDDL::cloneHbaseTable( const NAString &srcTable, const NAString &clonedTable, ExpHbaseInterface * inEHI) { + Lng32 retcode = 0; + HbaseStr hbaseTable; hbaseTable.val = (char*)srcTable.data(); hbaseTable.len = srcTable.length(); @@ -5242,8 +5249,15 @@ short CmpSeabaseDDL::cloneHbaseTable( } // copy hbaseTable as clonedHbaseTable - if (ehi->copy(hbaseTable, clonedHbaseTable, TRUE)) + if (retcode = ehi->copy(hbaseTable, clonedHbaseTable, TRUE)) { + *CmpCommon::diags() + << DgSqlCode(-8448) + << DgString0((char*)"ExpHbaseInterface::copy()") + << DgString1(getHbaseErrStr(-retcode)) + << DgInt0(-retcode) + << DgString2((char*)GetCliGlobals()->getJniErrorStr()); + if (! inEHI) deallocEHI(ehi); @@ -5321,7 +5335,7 @@ short CmpSeabaseDDL::cloneSeabaseTable( if (truncateHbaseTable(clonedCatNamePart, clonedSchNamePart, clonedObjNamePart, - (NATable*)naTable, inEHI)) + naTable->hasSaltedColumn(), inEHI)) { return -1; } @@ -5355,6 +5369,100 @@ short CmpSeabaseDDL::cloneSeabaseTable( return 0; } +short CmpSeabaseDDL::cloneAndTruncateTable( + const NATable * naTable, // IN: source table + NAString &tempTable, // OUT: temp table + ExpHbaseInterface * ehi, + ExeCliInterface * cliInterface) +{ + Lng32 cliRC = 0; + Lng32 cliRC2 = 0; + NABoolean identityGenAlways = FALSE; + char buf[4000]; + + const NAString &catalogNamePart = naTable->getTableName().getCatalogName(); + const NAString &schemaNamePart = naTable->getTableName().getSchemaName(); + const NAString &objectNamePart = naTable->getTableName().getObjectName(); + + ComUID comUID; + comUID.make_UID(); + Int64 objUID = comUID.get_value(); + + char objUIDbuf[100]; + + tempTable = naTable->getTableName().getQualifiedNameAsAnsiString(); + tempTable += "_"; + tempTable += str_ltoa(objUID, objUIDbuf); + + // identity 'generated always' columns do not permit inserting user specified + // values. Override it since we want to move original values to tgt. + const NAColumnArray &naColArr = naTable->getNAColumnArray(); + for (Int32 c = 0; c < naColArr.entries(); c++) + { + const NAColumn * nac = naColArr[c]; + if (nac->isIdentityColumnAlways()) + { + identityGenAlways = TRUE; + break; + } + } // for + + if (identityGenAlways) + { + cliRC = cliInterface->holdAndSetCQD("override_generated_identity_values", "ON"); + if (cliRC < 0) + { + cliInterface->retrieveSQLDiagnostics(CmpCommon::diags()); + goto label_restore; + } + } + + // clone source naTable to target tempTable + if (cloneSeabaseTable(naTable->getTableName().getQualifiedNameAsAnsiString(), + naTable->objectUid().castToInt64(), + tempTable, + naTable, + ehi, cliInterface, TRUE)) + { + cliRC = -1; + goto label_drop; + } + + // truncate source naTable + if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart, + naTable->hasSaltedColumn(), ehi)) + { + cliRC = -1; + goto label_restore; + } + + cliRC = 0; + goto label_return; + +label_restore: + if (cloneSeabaseTable(tempTable, -1, + naTable->getTableName().getQualifiedNameAsAnsiString(), + naTable, + ehi, cliInterface, FALSE)) + { + cliRC = -1; + goto label_drop; + } + +label_drop: + str_sprintf(buf, "drop table %s", tempTable.data()); + cliRC2 = cliInterface->executeImmediate(buf); + +label_return: + if (identityGenAlways) + cliInterface->restoreCQD("override_generated_identity_values"); + + if (cliRC < 0) + tempTable.clear(); + + return (cliRC < 0 ? -1 : 0); +} + void CmpSeabaseDDL::alterSeabaseTableAddColumn( StmtDDLAlterTableAddColumn * alterAddColNode, NAString &currCatName, NAString &currSchName) @@ -5964,21 +6072,12 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn NAList<NAString> &viewDefnList) { Lng32 cliRC = 0; + Lng32 cliRC2 = 0; const NAFileSet * naf = naTable->getClusteringIndex(); CorrName cn(objectNamePart, STMTHEAP, schemaNamePart, catalogNamePart); - ComUID comUID; - comUID.make_UID(); - Int64 objUID = comUID.get_value(); - - char objUIDbuf[100]; - - NAString tempTable(naTable->getTableName().getQualifiedNameAsAnsiString()); - tempTable += "_"; - tempTable += str_ltoa(objUID, objUIDbuf); - ExpHbaseInterface * ehi = allocEHI(); if (ehi == NULL) return -1; @@ -5987,7 +6086,6 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn (STMTHEAP, 0, NULL, CmpCommon::context()->sqlSession()->getParentQid()); - Int64 tableUID = naTable->objectUid().castToInt64(); const NAColumnArray &naColArr = naTable->getNAColumnArray(); const NAColumn * altNaCol = naColArr.getColumn(altColName); Lng32 altColNum = altNaCol->getPosition(); @@ -5997,47 +6095,14 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn NABoolean xnWasStartedHere = FALSE; - NABoolean identityGenAlways = FALSE; - char buf[4000]; - // identity 'generated always' columns do not permit inserting user specified - // values. Override it since we want to move original values to tgt. - for (Int32 c = 0; c < naColArr.entries(); c++) - { - const NAColumn * nac = naColArr[c]; - if (nac->isIdentityColumnAlways()) - { - identityGenAlways = TRUE; - break; - } - } // for - - if (identityGenAlways) - { - cliRC = cliInterface.holdAndSetCQD("override_generated_identity_values", "ON"); - if (cliRC < 0) - { - cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); - goto label_restore; - } - } - - if (cloneSeabaseTable(naTable->getTableName().getQualifiedNameAsAnsiString(), //cn, - naTable->objectUid().castToInt64(), - tempTable, - naTable, - ehi, &cliInterface, TRUE)) - { - cliRC = -1; - goto label_drop; - } - - if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart, - (NATable*)naTable, ehi)) + // save data by cloning as a temp table and truncate source table + NAString clonedTable; + cliRC = cloneAndTruncateTable(naTable, clonedTable, ehi, &cliInterface); + if (cliRC < 0) { - cliRC = -1; - goto label_restore; + goto label_drop; // diags already populated by called method } if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere)) @@ -6080,6 +6145,7 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn *CmpCommon::diags() << DgSqlCode(-1424) << DgColumnName(altColName); + cliRC = -1; goto label_restore; } @@ -6093,11 +6159,18 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn } } + cliRC = cliInterface.holdAndSetCQD("override_generated_identity_values", "ON"); + if (cliRC < 0) + { + cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + goto label_restore; + } + str_sprintf(buf, "upsert using load into %s(%s) select %s from %s", naTable->getTableName().getQualifiedNameAsAnsiString().data(), tgtCols.data(), tgtCols.data(), - tempTable.data()); + clonedTable.data()); cliRC = cliInterface.executeImmediate(buf); if (cliRC < 0) { @@ -6105,12 +6178,6 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn goto label_restore; } - if (identityGenAlways) - cliInterface.restoreCQD("override_generated_identity_values"); - - if (naTable->hasSecondaryIndexes()) // user indexes - cliInterface.restoreCQD("hide_indexes"); - if ((cliRC = recreateUsingViews(&cliInterface, viewNameList, viewDefnList, ddlXns)) < 0) { @@ -6123,46 +6190,43 @@ short CmpSeabaseDDL::alignedFormatTableDropColumn endXnIfStartedHere(&cliInterface, xnWasStartedHere, 0); - str_sprintf(buf, "drop table %s", tempTable.data()); - cliRC = cliInterface.executeImmediate(buf); - if (cliRC < 0) - { - cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); - goto label_restore; - } + goto label_drop; - - deallocEHI(ehi); - - return 0; - - label_restore: +label_restore: endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1); - if (identityGenAlways) - cliInterface.restoreCQD("override_generated_identity_values"); - - if (naTable->hasSecondaryIndexes()) // user indexes - cliInterface.restoreCQD("hide_indexes"); - ActiveSchemaDB()->getNATableDB()->removeNATable (cn, ComQiScope::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT, FALSE, FALSE); - - if (cloneSeabaseTable(tempTable, -1, - naTable->getTableName().getQualifiedNameAsAnsiString(), - naTable, - ehi, &cliInterface, FALSE)) + + if ((cliRC < 0) && + (NOT clonedTable.isNull()) && + (cloneSeabaseTable(clonedTable, -1, + naTable->getTableName().getQualifiedNameAsAnsiString(), + naTable, + ehi, &cliInterface, FALSE))) { cliRC = -1; goto label_drop; } - label_drop: - str_sprintf(buf, "drop table %s", tempTable.data()); - Lng32 cliRC2 = cliInterface.executeImmediate(buf); +label_drop: + if (NOT clonedTable.isNull()) + { + str_sprintf(buf, "drop table %s", clonedTable.data()); + cliRC2 = cliInterface.executeImmediate(buf); + } + + cliInterface.restoreCQD("override_generated_identity_values"); + cliInterface.restoreCQD("hide_indexes"); + + ActiveSchemaDB()->getNATableDB()->removeNATable + (cn, + ComQiScope::REMOVE_FROM_ALL_USERS, + COM_BASE_TABLE_OBJECT, ddlXns, FALSE); + deallocEHI(ehi); return (cliRC < 0 ? -1 : 0); @@ -6846,34 +6910,24 @@ short CmpSeabaseDDL::alignedFormatTableAlterColumnAttr NAList<NAString> &viewDefnList) { Lng32 cliRC = 0; + Lng32 cliRC2 = 0; const NAFileSet * naf = naTable->getClusteringIndex(); CorrName cn(objectNamePart, STMTHEAP, schemaNamePart, catalogNamePart); - ComUID comUID; - comUID.make_UID(); - Int64 objUID = comUID.get_value(); - - char objUIDbuf[100]; - - NAString tempTable(naTable->getTableName().getQualifiedNameAsAnsiString()); - tempTable += "_"; - tempTable += str_ltoa(objUID, objUIDbuf); - ExpHbaseInterface * ehi = allocEHI(); if (ehi == NULL) return -1; + ExeCliInterface cliInterface (STMTHEAP, 0, NULL, CmpCommon::context()->sqlSession()->getParentQid()); - Int64 tableUID = naTable->objectUid().castToInt64(); const NAColumnArray &naColArr = naTable->getNAColumnArray(); const NAColumn * altNaCol = naColArr.getColumn(altColName); Lng32 altColNum = altNaCol->getPosition(); - char buf[4000]; NAString colFamily; NAString colName; Lng32 datatype, length, precision, scale, dt_start, dt_end, @@ -6889,26 +6943,22 @@ short CmpSeabaseDDL::alignedFormatTableAlterColumnAttr NABoolean xnWasStartedHere = FALSE; - if (cloneSeabaseTable(naTable->getTableName().getQualifiedNameAsAnsiString(), - naTable->objectUid().castToInt64(), - tempTable, - naTable, - ehi, &cliInterface, TRUE)) + char buf[4000]; + + // save data by cloning as a temp table and truncate source table + NAString clonedTable; + cliRC = cloneAndTruncateTable(naTable, clonedTable, ehi, &cliInterface); + if (cliRC < 0) { - cliRC = -1; - goto label_drop; + goto label_drop; // diags already populated by called method } - - if (truncateHbaseTable(catalogNamePart, schemaNamePart, objectNamePart, - (NATable*)naTable, ehi)) + + if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere)) { cliRC = -1; goto label_restore; } - if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere)) - goto label_restore; - if (getColInfo(pColDef, FALSE, // not a metadata, histogram or repository column colFamily, @@ -6945,7 +6995,7 @@ short CmpSeabaseDDL::alignedFormatTableAlterColumnAttr (char*)charset.data(), (Lng32)defaultClass, (quotedDefVal.isNull() ? "" : quotedDefVal.data()), - tableUID, + naTable->objectUid().castToInt64(), altColNum); cliRC = cliInterface.executeImmediate(buf); @@ -6962,7 +7012,7 @@ short CmpSeabaseDDL::alignedFormatTableAlterColumnAttr str_sprintf(buf, "upsert using load into %s select * from %s", naTable->getTableName().getQualifiedNameAsAnsiString().data(), - tempTable.data()); + clonedTable.data()); cliRC = cliInterface.executeImmediate(buf); if (cliRC < 0) { @@ -6991,43 +7041,45 @@ short CmpSeabaseDDL::alignedFormatTableAlterColumnAttr endXnIfStartedHere(&cliInterface, xnWasStartedHere, 0); - str_sprintf(buf, "drop table %s", tempTable.data()); - cliRC = cliInterface.executeImmediate(buf); - if (cliRC < 0) - { - cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); - goto label_restore; - } - - deallocEHI(ehi); - - return 0; + goto label_drop; - label_restore: +label_restore: endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1); ActiveSchemaDB()->getNATableDB()->removeNATable (cn, ComQiScope::REMOVE_FROM_ALL_USERS, COM_BASE_TABLE_OBJECT, FALSE, FALSE); - - if (cloneSeabaseTable(tempTable, -1, - naTable->getTableName().getQualifiedNameAsAnsiString(), - naTable, - ehi, &cliInterface, FALSE)) + + if ((cliRC < 0) && + (NOT clonedTable.isNull()) && + (cloneSeabaseTable(clonedTable, -1, + naTable->getTableName().getQualifiedNameAsAnsiString(), + naTable, + ehi, &cliInterface, FALSE))) { cliRC = -1; goto label_drop; } + +label_drop: + if (NOT clonedTable.isNull()) + { + str_sprintf(buf, "drop table %s", clonedTable.data()); + cliRC2 = cliInterface.executeImmediate(buf); + } - label_drop: - str_sprintf(buf, "drop table %s", tempTable.data()); - Lng32 cliRC2 = cliInterface.executeImmediate(buf); + cliInterface.restoreCQD("override_generated_identity_values"); + cliInterface.restoreCQD("hide_indexes"); + + ActiveSchemaDB()->getNATableDB()->removeNATable + (cn, + ComQiScope::REMOVE_FROM_ALL_USERS, + COM_BASE_TABLE_OBJECT, ddlXns, FALSE); + deallocEHI(ehi); - endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1); - return (cliRC < 0 ? -1 : 0); } @@ -7880,6 +7932,7 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( NAString &currCatName, NAString &currSchName) { Lng32 cliRC = 0; + Lng32 cliRC2 = 0; Lng32 retcode = 0; ExeCliInterface cliInterface(STMTHEAP, 0, NULL, @@ -7912,24 +7965,45 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( return; } + // if table already has a clustering or primary key, return error. + if ((naTable->getClusteringIndex()) && + (NOT naTable->getClusteringIndex()->hasOnlySyskey())) + { + *CmpCommon::diags() + << DgSqlCode(-1256) + << DgString0(extTableName); + + processReturn(); + + return; + } + ElemDDLColRefArray &keyColumnArray = alterAddConstraint->getConstraint()->castToElemDDLConstraintPK()->getKeyColumnArray(); NAList<NAString> keyColList(HEAP, keyColumnArray.entries()); - NAString pkeyStr("("); + NAString pkeyColsStr; + if (alterAddConstraint->getConstraint()->castToElemDDLConstraintPK()->isNullableSpecified()) + pkeyColsStr += " NULLABLE "; + pkeyColsStr += "("; for (Int32 j = 0; j < keyColumnArray.entries(); j++) { const NAString &colName = keyColumnArray[j]->getColumnName(); keyColList.insert(colName); - pkeyStr += colName; + pkeyColsStr += colName; + if (keyColumnArray[j]->getColumnOrdering() == COM_DESCENDING_ORDER) + pkeyColsStr += " DESC"; + else + pkeyColsStr += " ASC"; + if (j < (keyColumnArray.entries() - 1)) - pkeyStr += ", "; + pkeyColsStr += ", "; } - pkeyStr += ")"; + pkeyColsStr += ")"; if (constraintErrorChecks(&cliInterface, - alterAddConstraint->castToStmtDDLAddConstraintUnique(), + alterAddConstraint->castToStmtDDLAddConstraintPK(), naTable, COM_UNIQUE_CONSTRAINT, //TRUE, keyColList)) @@ -7937,117 +8011,148 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( return; } - // if table already has a primary key, return error. - if ((naTable->getClusteringIndex()) && - (NOT naTable->getClusteringIndex()->hasOnlySyskey())) - { - *CmpCommon::diags() - << DgSqlCode(-1256) - << DgString0(extTableName); - - processReturn(); - - return; - } - - // update unique key constraint info + // update unique key constraint info NAString uniqueStr; if (genUniqueName(alterAddConstraint, uniqueStr)) { return; } - // if table doesnt have a user defined primary key, is empty and doesn't have any - // dependent objects (index, views, triggers, RI, etc), then drop it and recreate it with - // this new primary key. - // Do this optimization in mode_special_4 only. - Lng32 len = 0; - Lng32 rowCount = 0; - NABoolean ms4 = FALSE; - if (CmpCommon::getDefault(MODE_SPECIAL_4) == DF_ON) + // find out if this table has dependent objects (views, user indexes, + // unique constraints, referential constraints) + NABoolean dependentObjects = FALSE; + Queue * usingViewsQueue = NULL; + cliRC = getUsingViews(&cliInterface, naTable->objectUid().castToInt64(), + usingViewsQueue); + if (cliRC < 0) { - ms4 = TRUE; - - char query[2000]; - str_sprintf(query, "select [any 1] cast(1 as int not null) from \"%s\".\"%s\".\"%s\" for read committed access", - catalogNamePart.data(), schemaNamePart.data(), objectNamePart.data()); - cliRC = cliInterface.executeImmediate(query, (char*)&rowCount, &len, FALSE); - if (cliRC < 0) - { - cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); - return; - } + processReturn(); + + return; } - - // if the table is not empty, or there are dependent objects/constraints, - // or the table already has a pkey/store by, then create a unique constraint. - NABoolean isStoreBy = FALSE; - Int32 nonSystemKeyCols = 0; - if (naTable->getClusteringIndex()) - { - NAFileSet * naf = naTable->getClusteringIndex(); - for (Lng32 i = 0; i < naf->getIndexKeyColumns().entries(); i++) - { - NAColumn * nac = naf->getIndexKeyColumns()[i]; - - if (NOT nac->isSystemColumn()) - nonSystemKeyCols++; - else if (nac->isSyskeyColumn()) - isStoreBy = TRUE; - } // for - - if (nonSystemKeyCols == 0) - isStoreBy = FALSE; - } // if - if ((rowCount > 0) || // not empty - (NOT ms4) || // not mode_special_4 - (naTable->hasSecondaryIndexes()) || // user indexes - (NOT naTable->getClusteringIndex()->hasSyskey()) || // user defined pkey - (isStoreBy) || // user defined store by + if ((naTable->hasSecondaryIndexes()) || // user indexes (naTable->getUniqueConstraints().entries() > 0) || // unique constraints (naTable->getRefConstraints().entries() > 0) || // ref constraints - (naTable->getCheckConstraints().entries() > 0)) - { - // cannot create clustered primary key constraint. - // create a unique constraint instead. - NAString cliQuery; - cliQuery = "alter table " + extTableName + " add constraint " + uniqueStr - + " unique " + pkeyStr + ";"; - cliRC = cliInterface.executeImmediate((char*)cliQuery.data()); - if (cliRC < 0) + (usingViewsQueue->entries() > 0)) + { + dependentObjects = TRUE; + } + + // if the table has dependent objects return error. + // Users need to drop them before adding primary key + // If cqd is set to create pkey as a unique constraint, then do that. + if (dependentObjects) + { + if (CmpCommon::getDefault(TRAF_ALTER_ADD_PKEY_AS_UNIQUE_CONSTRAINT) == DF_OFF) { - cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + // error + *CmpCommon::diags() << DgSqlCode(-3242) + << DgString0("Cannot alter/add primary key constraint on a table with dependencies. Drop all dependent objects (views, indexes, unique and referential constraints) on the specified table and recreate them after adding the primary key."); } - - if (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)) + else { - // remove NATable for this table - ActiveSchemaDB()->getNATableDB()->removeNATable - (cn, - ComQiScope::REMOVE_FROM_ALL_USERS, - COM_BASE_TABLE_OBJECT, - alterAddConstraint->ddlXns(), FALSE); + // cannot create clustered primary key constraint. + // create a unique constraint instead. + NAString cliQuery; + cliQuery = "alter table " + extTableName + " add constraint " + uniqueStr + + " unique " + pkeyColsStr + ";"; + cliRC = cliInterface.executeImmediate((char*)cliQuery.data()); + if (cliRC < 0) + { + cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + } + + if (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)) + { + // remove NATable for this table + ActiveSchemaDB()->getNATableDB()->removeNATable + (cn, + ComQiScope::REMOVE_FROM_ALL_USERS, + COM_BASE_TABLE_OBJECT, + alterAddConstraint->ddlXns(), FALSE); + } } + + return; + } + + // create a true primary key by drop/create/populate of table. + NABoolean isEmpty = FALSE; + + HbaseStr hbaseTable; + hbaseTable.val = (char*)extNameForHbase.data(); + hbaseTable.len = extNameForHbase.length(); + + retcode = ehi->isEmpty(hbaseTable); + if (retcode < 0) + { + *CmpCommon::diags() + << DgSqlCode(-8448) + << DgString0((char*)"ExpHbaseInterface::isEmpty()") + << DgString1(getHbaseErrStr(-retcode)) + << DgInt0(-retcode) + << DgString2((char*)GetCliGlobals()->getJniErrorStr()); + + deallocEHI(ehi); + + processReturn(); return; } + isEmpty = (retcode == 1); + Int64 tableUID = getObjectUID(&cliInterface, catalogNamePart.data(), schemaNamePart.data(), objectNamePart.data(), COM_BASE_TABLE_OBJECT_LIT); - - // empty table. Drop and recreate it with the new primary key. + + NAString clonedTable; + if (NOT isEmpty) // non-empty table + { + // clone as a temp table and truncate source table + cliRC = cloneAndTruncateTable(naTable, clonedTable, ehi, &cliInterface); + if (cliRC < 0) + { + return; // diags already populated by called method + } + } + + // Drop and recreate it with the new primary key. + NAString pkeyName; + if (NOT alterAddConstraint->getConstraintName().isNull()) + { + pkeyName = alterAddConstraint->getConstraintName(); + } + char * buf = NULL; ULng32 buflen = 0; retcode = CmpDescribeSeabaseTable(cn, 3/*createlike*/, buf, buflen, STMTHEAP, - pkeyStr.data(), TRUE); + pkeyName.data(), pkeyColsStr.data(), TRUE); if (retcode) return; + + NABoolean done = FALSE; + Lng32 curPos = 0; NAString cliQuery; - // drop this table. + + char cqdbuf[200]; + NABoolean xnWasStartedHere = FALSE; + + str_sprintf(cqdbuf, "cqd traf_no_hbase_drop_create 'ON';"); + cliRC = cliInterface.executeImmediate(cqdbuf); + if (cliRC < 0) + { + cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + goto label_return; + } + + if (beginXnIfNotInProgress(&cliInterface, xnWasStartedHere)) + goto label_return; + + // drop this table from metadata. cliQuery = "drop table "; cliQuery += extTableName; cliQuery += " no check;"; @@ -8055,17 +8160,18 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( if (cliRC < 0) { cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); - return; + + goto label_return; } - char cqdbuf[200]; str_sprintf(cqdbuf, "cqd traf_create_table_with_uid '%ld';", tableUID); cliRC = cliInterface.executeImmediate(cqdbuf); if (cliRC < 0) { cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); - return; + + goto label_return; } // and recreate it with the new primary key. @@ -8073,8 +8179,6 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( cliQuery += extTableName; cliQuery += " "; - NABoolean done = FALSE; - Lng32 curPos = 0; while (NOT done) { short len = *(short*)&buf[curPos]; @@ -8092,14 +8196,36 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( if (cliRC < 0) { cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + + goto label_return; } str_sprintf(cqdbuf, "cqd traf_create_table_with_uid '' ;"); cliInterface.executeImmediate(cqdbuf); - if (cliRC < 0) + str_sprintf(cqdbuf, "cqd traf_no_hbase_drop_create 'OFF';"); + cliInterface.executeImmediate(cqdbuf); + + if (NOT isEmpty) // non-empty table { - return; + // remove NATable so current definition could be loaded + ActiveSchemaDB()->getNATableDB()->removeNATable + (cn, + ComQiScope::REMOVE_FROM_ALL_USERS, + COM_BASE_TABLE_OBJECT, + alterAddConstraint->ddlXns(), FALSE); + + // copy tempTable data into newly created table + str_sprintf(buf, "insert with no rollback into %s select * from %s", + naTable->getTableName().getQualifiedNameAsAnsiString().data(), + clonedTable.data()); + cliRC = cliInterface.executeImmediate(buf); + if (cliRC < 0) + { + cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + + goto label_restore; + } } if (updateObjectRedefTime(&cliInterface, @@ -8108,9 +8234,11 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( { processReturn(); - return; + goto label_return; } + endXnIfStartedHere(&cliInterface, xnWasStartedHere, 0); + if (!Get_SqlParser_Flags(INTERNAL_QUERY_FROM_EXEUTIL)) { // remove NATable for this table @@ -8121,6 +8249,60 @@ void CmpSeabaseDDL::alterSeabaseTableAddPKeyConstraint( alterAddConstraint->ddlXns(), FALSE); } + if (NOT clonedTable.isNull()) + { + str_sprintf(buf, "drop table %s", clonedTable.data()); + cliRC = cliInterface.executeImmediate(buf); + if (cliRC < 0) + { + cliInterface.retrieveSQLDiagnostics(CmpCommon::diags()); + goto label_restore; + } + } + + cliRC = 0; + // normal return + +label_restore: + endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1); + + ActiveSchemaDB()->getNATableDB()->removeNATable + (cn, + ComQiScope::REMOVE_FROM_ALL_USERS, + COM_BASE_TABLE_OBJECT, FALSE, FALSE); + + if ((cliRC < 0) && + (NOT clonedTable.isNull()) && + (cloneSeabaseTable(clonedTable, -1, + naTable->getTableName().getQualifiedNameAsAnsiString(), + naTable, + ehi, &cliInterface, FALSE))) + { + cliRC = -1; + goto label_drop; + } + +label_drop: + endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1); + + if ((cliRC < 0) && + (NOT clonedTable.isNull())) + { + str_sprintf(buf, "drop table %s", clonedTable.data()); + cliRC2 = cliInterface.executeImmediate(buf); + } + + deallocEHI(ehi); + +label_return: + endXnIfStartedHere(&cliInterface, xnWasStartedHere, -1); + + str_sprintf(cqdbuf, "cqd traf_create_table_with_uid '' ;"); + cliInterface.executeImmediate(cqdbuf); + + str_sprintf(cqdbuf, "cqd traf_no_hbase_drop_create 'OFF';"); + cliInterface.executeImmediate(cqdbuf); + return; } http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/sqlcomp/DefaultConstants.h ---------------------------------------------------------------------- diff --git a/core/sql/sqlcomp/DefaultConstants.h b/core/sql/sqlcomp/DefaultConstants.h index b2b2bb9..0a9ebed 100644 --- a/core/sql/sqlcomp/DefaultConstants.h +++ b/core/sql/sqlcomp/DefaultConstants.h @@ -3316,7 +3316,23 @@ enum DefaultConstants // Use the earlier implementation of HdfsScan via libhdfs USE_LIBHDFS_SCAN, - + + // if set, make primary key columns non-nullable. ANSI specification. + // Default is ON. + TRAF_MAKE_PKEY_COLUMNS_NOT_NULL, + + // if ON and there are dependent objects on the table, then + // create unique constraint instead of clustered primary key. + // + // Otherwise return error. Users will need to drop dependent objects and + // then recreate them after adding the primary key. + // Default is OFF. + TRAF_ALTER_ADD_PKEY_AS_UNIQUE_CONSTRAINT, + + // if set, do not drop or create hbase objects. + // Internal cqd. Used during pkey alter/add + TRAF_NO_HBASE_DROP_CREATE, + // This enum constant must be the LAST one in the list; it's a count, // not an Attribute (it's not IN DefaultDefaults; it's the SIZE of it)! // Size of byte[] in java when direct byteBuffer can't be used http://git-wip-us.apache.org/repos/asf/trafodion/blob/5724b2e6/core/sql/sqlcomp/nadefaults.cpp ---------------------------------------------------------------------- diff --git a/core/sql/sqlcomp/nadefaults.cpp b/core/sql/sqlcomp/nadefaults.cpp index 7e5cfa1..94eac04 100644 --- a/core/sql/sqlcomp/nadefaults.cpp +++ b/core/sql/sqlcomp/nadefaults.cpp @@ -2854,7 +2854,6 @@ XDDkwd__(SUBQUERY_UNNESTING, "ON"), DDkwd__(TOTAL_RESOURCE_COSTING, "ON"), - DDkwd__(TRAF_ALIGNED_ROW_FORMAT, "ON"), DDkwd__(TRAF_ALLOW_ESP_COLOCATION, "OFF"), @@ -2863,6 +2862,8 @@ XDDkwd__(SUBQUERY_UNNESTING, "ON"), DDkwd__(TRAF_ALLOW_SELF_REF_CONSTR, "ON"), + DDkwd__(TRAF_ALTER_ADD_PKEY_AS_UNIQUE_CONSTRAINT, "OFF"), + DDkwd__(TRAF_ALTER_COL_ATTRS, "ON"), DDkwd__(TRAF_AUTO_CREATE_SCHEMA, "OFF"), @@ -2918,6 +2919,8 @@ XDDkwd__(SUBQUERY_UNNESTING, "ON"), DDkwd__(TRAF_LOAD_USE_FOR_INDEXES, "ON"), DDkwd__(TRAF_LOAD_USE_FOR_STATS, "OFF"), + DDkwd__(TRAF_MAKE_PKEY_COLUMNS_NOT_NULL, "ON"), + // max size in bytes of a char or varchar column. Set to 16M DDui___(TRAF_MAX_CHARACTER_COL_LENGTH, MAX_CHAR_COL_LENGTH_IN_BYTES_STR), DDkwd__(TRAF_MAX_CHARACTER_COL_LENGTH_OVERRIDE, "OFF"), @@ -2928,6 +2931,8 @@ XDDkwd__(SUBQUERY_UNNESTING, "ON"), DDkwd__(TRAF_NO_DTM_XN, "OFF"), + DDkwd__(TRAF_NO_HBASE_DROP_CREATE, "OFF"), + DDint__(TRAF_NUM_HBASE_VERSIONS, "0"), DDint__(TRAF_NUM_OF_SALT_PARTNS, "-1"), @@ -5120,7 +5125,6 @@ enum DefaultConstants NADefaults::validateAndInsert(const char *attrName, val = "OFF"; insert(ALLOW_INCOMPATIBLE_OPERATIONS, val, errOrWarn); - insert(ALLOW_NULLABLE_UNIQUE_KEY_CONSTRAINT, val, errOrWarn); NAString csVal; if (value == "ON")
