diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java index 81f2140b6a4..980d1f28b89 100644 --- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java +++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java @@ -2276,6 +2276,12 @@ protected void registerNamespace( if (namespace == null) { namespaces.put(requireNonNull(ns.getNode()), ns); namespace = ns; + } else { + // due to the identity map, it is possible that the namespace returned by the map is different from the namespace to register + // this can lead to bugs during runtime therefore we throw an exception here + if (ns.getEnclosingNode() != namespace.getEnclosingNode()) { + throw new RuntimeException("Namespace returned by identity hashmap has different enclosing node. Namespace to register: " + ns.getEnclosingNode() + ", Namespace returned by map: " + namespace.getEnclosingNode()); + } } if (usingScope != null) { assert alias != null : "Registering namespace " + ns + ", into scope " + usingScope