-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] (nereids)implement showSyncJobCommand in nereids
- Loading branch information
1 parent
42a7734
commit a811c3d
Showing
6 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowSyncJobCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// 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. | ||
|
||
package org.apache.doris.nereids.trees.plans.commands; | ||
|
||
import org.apache.doris.catalog.Column; | ||
import org.apache.doris.catalog.DatabaseIf; | ||
import org.apache.doris.catalog.Env; | ||
import org.apache.doris.catalog.ScalarType; | ||
import org.apache.doris.common.AnalysisException; | ||
import org.apache.doris.common.ErrorCode; | ||
import org.apache.doris.common.ErrorReport; | ||
import org.apache.doris.common.util.ListComparator; | ||
import org.apache.doris.datasource.InternalCatalog; | ||
import org.apache.doris.mysql.privilege.PrivPredicate; | ||
import org.apache.doris.nereids.trees.plans.PlanType; | ||
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; | ||
import org.apache.doris.qe.ConnectContext; | ||
import org.apache.doris.qe.ShowResultSet; | ||
import org.apache.doris.qe.ShowResultSetMetaData; | ||
import org.apache.doris.qe.StmtExecutor; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Represents the command for SHOW SYNC JOB. | ||
*/ | ||
public class ShowSyncJobCommand extends ShowCommand { | ||
public static final ImmutableList<String> TITLE_NAMES = new ImmutableList.Builder<String>() | ||
.add("JobId").add("JobName").add("Type").add("State").add("Channel").add("Status") | ||
.add("JobConfig").add("CreateTime").add("LastStartTime").add("LastStopTime").add("FinishTime").add("Msg") | ||
.build(); | ||
|
||
private String databaseName; | ||
|
||
public ShowSyncJobCommand(String databaseName) { | ||
super(PlanType.SHOW_SYNC_JOB_COMMAND); | ||
this.databaseName = databaseName; | ||
} | ||
|
||
private void validate(ConnectContext ctx) throws AnalysisException { | ||
if (Strings.isNullOrEmpty(databaseName)) { | ||
databaseName = ctx.getDatabase(); | ||
if (Strings.isNullOrEmpty(databaseName)) { | ||
ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR); | ||
} | ||
} | ||
if (!Env.getCurrentEnv().getAccessManager() | ||
.checkDbPriv(ConnectContext.get(), InternalCatalog.INTERNAL_CATALOG_NAME, | ||
databaseName, PrivPredicate.SHOW)) { | ||
ErrorReport.reportAnalysisException(ErrorCode.ERR_DB_ACCESS_DENIED_ERROR, | ||
PrivPredicate.SHOW.getPrivs().toString(), databaseName); | ||
} | ||
} | ||
|
||
@Override | ||
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { | ||
validate(ctx); | ||
Env env = Env.getCurrentEnv(); | ||
DatabaseIf db = Env.getCurrentInternalCatalog().getDbOrAnalysisException(databaseName); | ||
|
||
List<List<Comparable>> syncInfos = env.getSyncJobManager().getSyncJobsInfoByDbId(db.getId()); | ||
Collections.sort(syncInfos, new ListComparator<List<Comparable>>(0)); | ||
|
||
List<List<String>> rows = Lists.newArrayList(); | ||
for (List<Comparable> syncInfo : syncInfos) { | ||
List<String> row = new ArrayList<String>(syncInfo.size()); | ||
|
||
for (Comparable element : syncInfo) { | ||
row.add(element.toString()); | ||
} | ||
rows.add(row); | ||
} | ||
return new ShowResultSet(this.getMetaData(), rows); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { | ||
return visitor.visitShowSyncJobCommand(this, context); | ||
} | ||
|
||
public ShowResultSetMetaData getMetaData() { | ||
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder(); | ||
for (String title : TITLE_NAMES) { | ||
builder.addColumn(new Column(title, ScalarType.createVarchar(30))); | ||
} | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
regression-test/suites/nereids_p0/show/test_show_sync_job_command.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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_sync_job_command", "query,sync_job") { | ||
try { | ||
// Create the necessary database and tables | ||
sql """CREATE DATABASE IF NOT EXISTS test_db;""" | ||
|
||
// Create a UNIQUE KEY table with batch delete enabled | ||
sql """CREATE TABLE IF NOT EXISTS test_db.test_tbl1 ( | ||
id INT, | ||
name STRING | ||
) UNIQUE KEY(id) | ||
DISTRIBUTED BY HASH(id) BUCKETS 1 | ||
PROPERTIES("replication_num" = "1");""" | ||
|
||
// Create the sync job | ||
sql """CREATE SYNC test_db.job1 | ||
( | ||
FROM mysql_db1.tbl1 INTO test_tbl1 | ||
) | ||
FROM BINLOG | ||
( | ||
"type" = "canal", | ||
"canal.server.ip" = "127.0.0.1", | ||
"canal.server.port" = "11111", | ||
"canal.destination" = "example", | ||
"canal.username" = "", | ||
"canal.password" = "" | ||
);""" | ||
|
||
// Verify the job is created | ||
checkNereidsExecute("SHOW SYNC JOB FROM test_db") | ||
|
||
} catch (Exception e) { | ||
// Log any exceptions that occur during testing | ||
log.error("Failed to execute CREATE SYNC JOB command", e) | ||
throw e | ||
} finally { | ||
// Cleanup | ||
try_sql("STOP SYNC JOB IF EXISTS job1;") | ||
try_sql("DROP TABLE IF EXISTS test_db.test_tbl1;") | ||
try_sql("DROP DATABASE IF EXISTS test_db;") | ||
} | ||
} |