Skip to content

Commit

Permalink
[enhancement](sequence col) add session variable to skip sequence col…
Browse files Browse the repository at this point in the history
…umn check while INSERT INTO (#41655) (#41732)

cherry-pick #41655
  • Loading branch information
zhannngchen authored Oct 12, 2024
1 parent fe93764 commit 9a9eaa7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ private void analyzeTargetTable(Analyzer analyzer) throws AnalysisException {
}

if (!haveInputSeqCol && !isPartialUpdate && !isFromDeleteOrUpdateStmt
&& !analyzer.getContext().getSessionVariable().isEnableUniqueKeyPartialUpdate()) {
&& !analyzer.getContext().getSessionVariable().isEnableUniqueKeyPartialUpdate()
&& analyzer.getContext().getSessionVariable().isRequireSequenceInInsert()) {
if (!seqColInTable.isPresent() || seqColInTable.get().getDefaultValue() == null
|| !seqColInTable.get().getDefaultValue().equals(DefaultValue.CURRENT_TIMESTAMP)) {
throw new AnalysisException("Table " + olapTable.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public List<Rule> buildRules() {
}
}

if (!haveInputSeqCol && !isPartialUpdate) {
if (!haveInputSeqCol && !isPartialUpdate && (!sink.isFromNativeInsertStmt()
|| ConnectContext.get().getSessionVariable().isRequireSequenceInInsert())) {
if (!seqColInTable.isPresent() || seqColInTable.get().getDefaultValue() == null
|| !seqColInTable.get().getDefaultValue()
.equals(DefaultValue.CURRENT_TIMESTAMP)) {
Expand Down
17 changes: 17 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ public class SessionVariable implements Serializable, Writable {
public static final String ENABLE_MATCH_WITHOUT_INVERTED_INDEX = "enable_match_without_inverted_index";
public static final String ENABLE_FALLBACK_ON_MISSING_INVERTED_INDEX = "enable_fallback_on_missing_inverted_index";

public static final String REQUIRE_SEQUENCE_IN_INSERT = "require_sequence_in_insert";

/**
* If set false, user couldn't submit analyze SQL and FE won't allocate any related resources.
*/
Expand Down Expand Up @@ -1501,6 +1503,13 @@ public void setEnableEsShardScroll(boolean enableESShardScroll) {
this.enableESShardScroll = enableESShardScroll;
}

@VariableMgr.VarAttr(name = REQUIRE_SEQUENCE_IN_INSERT, needForward = true, description = {
"该变量用于控制,使用了sequence列的unique key表,insert into操作是否要求必须提供每一行的sequence列的值",
"This variable controls whether the INSERT INTO operation on unique key tables with a sequence"
+ " column requires a sequence column to be provided for each row"
})
public boolean requireSequenceInInsert = true;

public boolean isEnableESShardScroll() {
return enableESShardScroll;
}
Expand Down Expand Up @@ -2593,6 +2602,14 @@ public void setEnableUniqueKeyPartialUpdate(boolean enableUniqueKeyPartialUpdate
this.enableUniqueKeyPartialUpdate = enableUniqueKeyPartialUpdate;
}

public void setRequireSequenceInInsert(boolean value) {
this.requireSequenceInInsert = value;
}

public boolean isRequireSequenceInInsert() {
return this.requireSequenceInInsert;
}

/**
* Serialize to thrift object.
* Used for rest api.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@

-- !all --
1 10 15 16 17 0 4 15
15 8 19 20 21 0 7 3
15 8 19 20 21 0 9 3
2 5 14 13 14 0 5 12
3 6 11 14 15 0 6 13

-- !all_clone_table --
1 10 15 16 17 0 2 \N
15 8 19 20 21 0 2 \N
2 5 14 13 14 0 2 \N
3 6 11 14 15 0 2 \N

-- !1 --
1 1 1 1 1 0 2 1
2 2 2 2 2 0 2 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ suite("test_unique_table_sequence") {
exception "Table ${tableName} has sequence column, need to specify the sequence column"
}

// with `require_sequence_in_insert=false`, previous insert operation should success
sql "SET require_sequence_in_insert=false"

sql "INSERT INTO ${tableName} values(15, 8, 19, 20, 21)"

sql "INSERT INTO ${tableName} (k1, v1, v2, v3, v4) values(15, 8, 19, 20, 21)"

sql "SET require_sequence_in_insert=true"

// correct way of insert into with seq col
sql "INSERT INTO ${tableName} (k1, v1, v2, v3, v4, __DORIS_SEQUENCE_COL__) values(15, 8, 19, 20, 21, 3)"

Expand All @@ -134,7 +143,31 @@ suite("test_unique_table_sequence") {

order_qt_all "SELECT * from ${tableName}"

sql "SET show_hidden_columns=false"

def tableNameClone = tableName + "_clone"
sql "DROP TABLE IF EXISTS ${tableNameClone}"
sql "create table ${tableNameClone} like ${tableName}"

// test insert into select *
test {
sql "INSERT INTO ${tableNameClone} select * from ${tableName}"
exception "Table ${tableNameClone} has sequence column, need to specify the sequence column"
}

// with `require_sequence_in_insert=true`, previous insert operation should success
sql "SET require_sequence_in_insert=false"

sql "INSERT INTO ${tableNameClone} select * from ${tableName}"

sql "SET require_sequence_in_insert=true"

sql "SET show_hidden_columns=true"

order_qt_all_clone_table "SELECT * from ${tableNameClone}"

sql "DROP TABLE ${tableName}"
sql "DROP TABLE ${tableNameClone}"

sql "DROP TABLE IF EXISTS ${tableName}"
sql """
Expand Down

0 comments on commit 9a9eaa7

Please sign in to comment.