This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.2 by this push:
new f4fed523be8 branch-4.2: [fix](fe) Quote dictionary data load
identifiers (#71135)
f4fed523be8 is described below
commit f4fed523be8cf16f3d37797f62873dda37c33f33
Author: Mryange <[email protected]>
AuthorDate: Thu Dec 3 09:31:32 2026 +0800
branch-4.2: [fix](fe) Quote dictionary data load identifiers (#71135)
### What problem does this PR solve?
Issue Number: None
Related PR: #67052
Problem Summary:
Backport #67052 to branch-4.2. Dictionary refresh builds an internal
INSERT from dictionary and source table names. Names containing dots,
spaces, or backticks can be parsed incorrectly when concatenated as raw
SQL.
Quote each identifier component with the Nereids helper. Restore the
previous dictionary status on refresh preparation errors and preserve
scheduled statement-context cleanup on 4.2.
### Release note
Dictionary refresh correctly quotes dictionary and source table names in
generated SQL.
### Testing
- `bash run-fe-ut.sh --run
org.apache.doris.dictionary.DictionaryManagerTest` passed all 8 cases,
including the new parser test.
- Checkstyle passed for both modified Java files.
Pre-submission status: Ready for review. Validation covered the targeted
FE suite. The full FE suite and cluster regression tests were outside
this backport check.
### Check List (For Author)
- Test
- [x] Unit Test
- Behavior changed
- [x] Yes. Dictionary refresh preserves the intended identifier
components in generated SQL.
- Does this need documentation?
- [x] No.
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
Co-authored-by: Doris-Breakwater <[email protected]>
---
.../apache/doris/dictionary/DictionaryManager.java | 63 ++++++++++++----------
.../doris/dictionary/DictionaryManagerTest.java | 60 +++++++++++++++++++++
2 files changed, 96 insertions(+), 27 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/dictionary/DictionaryManager.java
b/fe/fe-core/src/main/java/org/apache/doris/dictionary/DictionaryManager.java
index ea3a72913d5..5fcf973f97e 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/dictionary/DictionaryManager.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/dictionary/DictionaryManager.java
@@ -38,6 +38,7 @@ import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.trees.plans.commands.info.CreateDictionaryInfo;
import
org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoDictionaryCommand;
import
org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
+import org.apache.doris.nereids.util.Utils;
import org.apache.doris.persist.CreateDictionaryPersistInfo;
import org.apache.doris.persist.DictionaryDecreaseVersionInfo;
import org.apache.doris.persist.DictionaryIncreaseVersionInfo;
@@ -455,34 +456,31 @@ public class DictionaryManager extends MasterDaemon
implements Writable {
}
boolean ownsContext = ctx == null;
- if (ownsContext) { // for run with scheduler, not by command.
- // priv check is done in relative(caller) command. so use ADMIN
here is ok.
- ctx = InsertTask.makeConnectContext(UserIdentity.ADMIN,
dictionary.getDbName());
- ctx.setStatementContext(new StatementContext());
- }
-
try {
- // not use rerfresh command's executor to avoid potential problems.
- String insertSql = "insert into " + dictionary.getDbName() + "." +
dictionary.getName()
- + " select * from " + dictionary.getSourceCtlName() + "."
+ dictionary.getSourceDbName()
- + "." + dictionary.getSourceTableName();
- StmtExecutor executor = new StmtExecutor(ctx, insertSql);
- NereidsParser parser = new NereidsParser();
- InsertIntoTableCommand baseCommand = (InsertIntoTableCommand)
parser.parseSingle(insertSql);
- LOG.info("Loading to dictionary {} with query {}. adaptive: {}",
dictionary.getName(), ctx.queryId(),
- adaptiveLoad);
- if (!baseCommand.getLabelName().isPresent()) {
- baseCommand.setLabelName(Optional.of(DICTIONARY_JOB_ID + "_" +
ctx.queryId().toString()));
- }
- if (baseCommand.getJobId() == 0) {
- baseCommand.setJobId(DICTIONARY_JOB_ID);
- }
-
- InsertIntoDictionaryCommand command = new
InsertIntoDictionaryCommand(
- baseCommand, database, dictionary, adaptiveLoad);
-
- // run with sync by status.
try {
+ if (ownsContext) {
+ StatementContext statementContext = new StatementContext();
+ // The calling command has already checked privileges.
+ ctx = InsertTask.makeConnectContext(UserIdentity.ADMIN,
dictionary.getDbName());
+ ctx.setStatementContext(statementContext);
+ }
+
+ String insertSql = buildDataLoadSql(dictionary);
+ StmtExecutor executor = new StmtExecutor(ctx, insertSql);
+ NereidsParser parser = new NereidsParser();
+ InsertIntoTableCommand baseCommand = (InsertIntoTableCommand)
parser.parseSingle(insertSql);
+ LOG.info("Loading to dictionary {} with query {}. adaptive:
{}", dictionary.getName(), ctx.queryId(),
+ adaptiveLoad);
+ if (!baseCommand.getLabelName().isPresent()) {
+ baseCommand.setLabelName(Optional.of(DICTIONARY_JOB_ID +
"_" + ctx.queryId().toString()));
+ }
+ if (baseCommand.getJobId() == 0) {
+ baseCommand.setJobId(DICTIONARY_JOB_ID);
+ }
+
+ InsertIntoDictionaryCommand command = new
InsertIntoDictionaryCommand(
+ baseCommand, database, dictionary, adaptiveLoad);
+
// avoid to generate EmptySetNode making us not able to get
base table version.
ctx.getSessionVariable().setVarOnce(SessionVariable.DISABLE_NEREIDS_RULES,
"OLAP_SCAN_PARTITION_PRUNE,PRUNE_EMPTY_PARTITION");
@@ -583,7 +581,12 @@ public class DictionaryManager extends MasterDaemon
implements Writable {
dictionary.getVersion(),
ctx.getStatementContext().getDictionaryUsedSrcVersion());
} finally {
if (ownsContext) {
- cleanupScheduledContext(ctx);
+ if (ctx == null) {
+ // Context creation can fail after registering the context
with the current thread.
+ ConnectContext.remove();
+ } else {
+ cleanupScheduledContext(ctx);
+ }
}
}
}
@@ -593,6 +596,12 @@ public class DictionaryManager extends MasterDaemon
implements Writable {
ConnectContext.remove();
}
+ static String buildDataLoadSql(Dictionary dictionary) {
+ String targetName =
Utils.qualifiedNameWithBackquote(dictionary.getFullQualifiers());
+ String sourceName =
Utils.qualifiedNameWithBackquote(dictionary.getSourceQualifiedName());
+ return "insert into " + targetName + " select * from " + sourceName;
+ }
+
private boolean commitNowVersion(ConnectContext ctx, Dictionary
dictionary) {
if
(DebugPointUtil.isEnable("DictionaryManager.commitNowVersion.fail")) {
return false;
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/dictionary/DictionaryManagerTest.java
b/fe/fe-core/src/test/java/org/apache/doris/dictionary/DictionaryManagerTest.java
index 8cd79e46c61..6226ad368d4 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/dictionary/DictionaryManagerTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/dictionary/DictionaryManagerTest.java
@@ -17,8 +17,18 @@
package org.apache.doris.dictionary;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
import org.apache.doris.datasource.ExternalScanTaskCacheKey;
+import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.nereids.StatementContext;
+import org.apache.doris.nereids.analyzer.UnboundDictionarySink;
+import org.apache.doris.nereids.analyzer.UnboundRelation;
+import org.apache.doris.nereids.analyzer.UnboundTableSink;
+import org.apache.doris.nereids.parser.NereidsParser;
+import
org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoDictionaryCommand;
+import
org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.persist.CreateDictionaryPersistInfo;
import org.apache.doris.persist.DictionaryDecreaseVersionInfo;
import org.apache.doris.persist.DictionaryIncreaseVersionInfo;
@@ -26,10 +36,13 @@ import org.apache.doris.persist.DropDictionaryPersistInfo;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;
+import com.google.common.collect.ImmutableList;
import org.junit.Assert;
import org.junit.Test;
+import org.mockito.Mockito;
import java.util.Collections;
+import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -134,4 +147,51 @@ public class DictionaryManagerTest {
key, () ->
Collections.singletonList(loadCount.incrementAndGet())));
Assert.assertEquals(2, loadCount.get());
}
+
+ @Test
+ public void testBuildDataLoadSqlQuotesIdentifierParts() {
+ List<String> targetName = ImmutableList.of("target.with` db",
"dict.with` select");
+ List<String> targetFullName = ImmutableList.of("internal",
targetName.get(0), targetName.get(1));
+ List<String> sourceName = ImmutableList.of(
+ "internal` catalog", "source` db", "src.with` select");
+ Dictionary dictionary = Mockito.mock(Dictionary.class);
+ Mockito.when(dictionary.getDbName()).thenReturn(targetName.get(0));
+ Mockito.when(dictionary.getName()).thenReturn(targetName.get(1));
+
Mockito.when(dictionary.getFullQualifiers()).thenReturn(targetFullName);
+
Mockito.when(dictionary.getSourceQualifiedName()).thenReturn(sourceName);
+
Mockito.when(dictionary.getColumnNames()).thenReturn(ImmutableList.of("k",
"v"));
+
+ String insertSql = DictionaryManager.buildDataLoadSql(dictionary);
+
+ Assert.assertEquals("insert into `internal`.`target.with``
db`.`dict.with`` select` "
+ + "select * from `internal`` catalog`.`source`` db`."
+ + "`src.with`` select`", insertSql);
+ ConnectContext context = new ConnectContext();
+ context.setEnv(Env.getCurrentEnv());
+ context.changeDefaultCatalog("external_catalog");
+ context.setThreadLocalInfo();
+ try {
+ InsertIntoTableCommand command = (InsertIntoTableCommand) new
NereidsParser().parseSingle(insertSql);
+ LogicalPlan query = command.getLogicalQuery();
+ Assert.assertTrue(query instanceof UnboundTableSink);
+ Assert.assertEquals(targetFullName, ((UnboundTableSink<?>)
query).getNameParts());
+
+ Database database = Mockito.mock(Database.class);
+ InternalCatalog catalog = Mockito.mock(InternalCatalog.class);
+ Mockito.when(database.getCatalog()).thenReturn(catalog);
+ Mockito.when(catalog.getName()).thenReturn(targetFullName.get(0));
+
Mockito.when(database.getFullName()).thenReturn(targetFullName.get(1));
+ InsertIntoDictionaryCommand dictionaryCommand = new
InsertIntoDictionaryCommand(
+ command, database, dictionary, false);
+ LogicalPlan dictionaryQuery = dictionaryCommand.getLogicalQuery();
+ Assert.assertTrue(dictionaryQuery instanceof
UnboundDictionarySink);
+ Assert.assertEquals(targetFullName, ((UnboundDictionarySink<?>)
dictionaryQuery).getNameParts());
+
+ List<UnboundRelation> sourceRelations =
dictionaryQuery.collectToList(UnboundRelation.class::isInstance);
+ Assert.assertEquals(1, sourceRelations.size());
+ Assert.assertEquals(sourceName,
sourceRelations.get(0).getNameParts());
+ } finally {
+ ConnectContext.remove();
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]