Skip to content

Commit

Permalink
[FLINK-15254][sql cli][module] modules in SQL CLI yaml should preserv…
Browse files Browse the repository at this point in the history
…e order

currently the module map is a hash map in sql cli, which doesn't preserve module loading order from yaml.
fix it by always using a linked hash map

this closes apache#10578.
  • Loading branch information
bowenli86 committed Dec 16, 2019
1 parent 387c8b6 commit 6a0570d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Environment {
private DeploymentEntry deployment;

public Environment() {
this.modules = Collections.emptyMap();
this.modules = new LinkedHashMap<>();
this.catalogs = Collections.emptyMap();
this.tables = Collections.emptyMap();
this.functions = Collections.emptyMap();
Expand All @@ -83,7 +83,7 @@ public Map<String, ModuleEntry> getModules() {
}

public void setModules(List<Map<String, Object>> modules) {
this.modules = new HashMap<>(modules.size());
this.modules = new LinkedHashMap<>(modules.size());

modules.forEach(config -> {
final ModuleEntry entry = ModuleEntry.create(config);
Expand Down Expand Up @@ -235,7 +235,7 @@ public static Environment merge(Environment env1, Environment env2) {
final Environment mergedEnv = new Environment();

// merge modules
final Map<String, ModuleEntry> modules = new HashMap<>(env1.getModules());
final Map<String, ModuleEntry> modules = new LinkedHashMap<>(env1.getModules());
modules.putAll(env2.getModules());
mergedEnv.modules = modules;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -110,6 +111,33 @@ public void testDuplicateModules() {
createModule("module2", "test")));
}

@Test
public void testModuleOrder() {
Environment env1 = new Environment();
Environment env2 = new Environment();
env1.setModules(Arrays.asList(
createModule("b", "test"),
createModule("d", "test")));

env2.setModules(Arrays.asList(
createModule("c", "test"),
createModule("a", "test")));

assertEquals(
Arrays.asList("b", "d"), new ArrayList<>(env1.getModules().keySet())
);

assertEquals(
Arrays.asList("c", "a"), new ArrayList<>(env2.getModules().keySet())
);

Environment env = Environment.merge(env1, env2);

assertEquals(
Arrays.asList("b", "d", "c", "a"), new ArrayList<>(env.getModules().keySet())
);
}

private static Map<String, Object> createCatalog(String name, String type) {
Map<String, Object> prop = new HashMap<>();

Expand Down

0 comments on commit 6a0570d

Please sign in to comment.