This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 318bd3f9de5 [Cherry-Pick][improvement](stmt) Add fuzzy matching of 
label in show transaction (#30725)
318bd3f9de5 is described below

commit 318bd3f9de520cbd3be1b989b33aa49c2f780446
Author: wudi <676366...@qq.com>
AuthorDate: Thu Feb 1 23:04:06 2024 +0800

    [Cherry-Pick][improvement](stmt) Add fuzzy matching of label in show 
transaction (#30725)
    
    * Add fuzzy matching of label in show transaction
    
    * fix
---
 .../apache/doris/analysis/ShowTransactionStmt.java | 15 ++++++--
 .../java/org/apache/doris/qe/ShowExecutor.java     |  3 ++
 .../doris/transaction/DatabaseTransactionMgr.java  | 22 ++++++++++++
 .../doris/transaction/GlobalTransactionMgr.java    |  5 +++
 .../query_p0/show/test_show_transaction.groovy     | 42 ++++++++++++++++++++++
 5 files changed, 85 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
index b88922298b2..f34c0e5e336 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
@@ -42,6 +42,7 @@ public class ShowTransactionStmt extends ShowStmt {
     private long txnId = -1;
     private String label = "";
     private TransactionStatus status = TransactionStatus.UNKNOWN;
+    private boolean labelMatch = false;
 
     public ShowTransactionStmt(String dbName, Expr whereClause) {
         this.dbName = dbName;
@@ -64,6 +65,10 @@ public class ShowTransactionStmt extends ShowStmt {
         return status;
     }
 
+    public boolean labelMatch() {
+        return labelMatch;
+    }
+
     @Override
     public void analyze(Analyzer analyzer) throws AnalysisException, 
UserException {
         super.analyze(analyzer);
@@ -95,7 +100,7 @@ public class ShowTransactionStmt extends ShowStmt {
                     valid = false;
                     break CHECK;
                 }
-            } else {
+            } else if (!(whereClause instanceof LikePredicate)) {
                 valid = false;
                 break CHECK;
             }
@@ -123,10 +128,16 @@ public class ShowTransactionStmt extends ShowStmt {
             } else {
                 valid = false;
             }
+
+            if (whereClause instanceof LikePredicate && 
leftKey.equalsIgnoreCase("label")) {
+                //Only supports label like matching
+                labelMatch = true;
+                label = label.replaceAll("%", ".*");
+            }
         }
 
         if (!valid) {
-            throw new AnalysisException("Where clause should looks like one of 
them: id = 123 or label = 'label' "
+            throw new AnalysisException("Where clause should looks like one of 
them: id = 123 or label =/like 'label' "
                     + "or status = 
'prepare/precommitted/committed/visible/aborted'");
         }
     }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
index dbe0a27f931..c6cb9dcb055 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
@@ -2250,6 +2250,9 @@ public class ShowExecutor {
         if (status != TransactionStatus.UNKNOWN) {
             resultSet = new ShowResultSet(showStmt.getMetaData(),
                     transactionMgr.getDbTransInfoByStatus(db.getId(), status));
+        } else if (showStmt.labelMatch() && !showStmt.getLabel().isEmpty()) {
+            resultSet =  new ShowResultSet(showStmt.getMetaData(),
+                transactionMgr.getDbTransInfoByLabelMatch(db.getId(), 
showStmt.getLabel()));
         } else {
             Long txnId = showStmt.getTxnId();
             String label = showStmt.getLabel();
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
 
b/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
index e780bc02472..e772b28ade4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
@@ -297,6 +297,28 @@ public class DatabaseTransactionMgr {
         return infos;
     }
 
+    public List<List<String>> getTxnStateInfoList(String labelRegex) {
+        List<List<String>> infos = Lists.newArrayList();
+        List<TransactionState> transactionStateCollection = 
Lists.newArrayList();
+        readLock();
+        try {
+            
transactionStateCollection.addAll(idToFinalStatusTransactionState.values());
+            
transactionStateCollection.addAll(idToRunningTransactionState.values());
+            // get transaction order by txn id desc
+            transactionStateCollection.stream()
+                    .filter(transactionState -> 
(transactionState.getLabel().matches(labelRegex)))
+                    .sorted(TransactionState.TXN_ID_COMPARATOR)
+                    .forEach(t -> {
+                        List<String> info = Lists.newArrayList();
+                        getTxnStateInfo(t, info);
+                        infos.add(info);
+                    });
+        } finally {
+            readUnlock();
+        }
+        return infos;
+    }
+
     private void getTxnStateInfo(TransactionState txnState, List<String> info) 
{
         info.add(String.valueOf(txnState.getTransactionId()));
         info.add(txnState.getLabel());
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
 
b/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
index 4832d66bedb..30f1c706c50 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
@@ -565,6 +565,11 @@ public class GlobalTransactionMgr implements Writable {
         return dbTransactionMgr.getTxnStateInfoList(status);
     }
 
+    public List<List<String>> getDbTransInfoByLabelMatch(long dbId, String 
label) throws AnalysisException {
+        DatabaseTransactionMgr dbTransactionMgr = 
getDatabaseTransactionMgr(dbId);
+        return dbTransactionMgr.getTxnStateInfoList(label);
+    }
+
     public long getTxnNumByStatus(TransactionStatus status) {
         long counter = 0;
         for (DatabaseTransactionMgr dbMgr : 
dbIdToDatabaseTransactionMgrs.values()) {
diff --git a/regression-test/suites/query_p0/show/test_show_transaction.groovy 
b/regression-test/suites/query_p0/show/test_show_transaction.groovy
new file mode 100644
index 00000000000..fa7f0ddbdc9
--- /dev/null
+++ b/regression-test/suites/query_p0/show/test_show_transaction.groovy
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("test_show_transaction", "p0") {
+    // define a sql table
+    def testTable = "test_show_transaction"
+
+    sql "DROP TABLE IF EXISTS ${testTable}"
+    sql """
+            CREATE TABLE IF NOT EXISTS ${testTable} (
+              `k1` INT NULL COMMENT "",
+              `k2` STRING NOT NULL COMMENT ""
+            ) ENGINE=OLAP
+            DUPLICATE KEY(`k1`)
+            DISTRIBUTED BY HASH(`k1`) BUCKETS 1
+            PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1",
+            "storage_format" = "V2"
+            );
+            """
+
+    def uuid = UUID.randomUUID().toString().replaceAll("-", "");
+    sql """ INSERT INTO ${testTable} WITH LABEL 
label_test_show_transaction_${uuid} VALUES(100, 'doris')  """
+    def res = sql_return_maparray """ show transaction where label = 
'label_test_show_transaction_${uuid}'  """
+    print("show transaction result : " + res)
+    def reslike = sql_return_maparray """ show transaction where label like 
'label_test_show_transaction_${uuid}%'  """
+    assertTrue(res.equals(reslike))
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to