This is an automated email from the ASF dual-hosted git repository.
zabetak pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 79301845a8 [CALCITE-7022] Decouple ModelHandler from CalciteConnection
79301845a8 is described below
commit 79301845a895b58c08981c644464162251626dcb
Author: Stamatis Zampetakis <[email protected]>
AuthorDate: Mon May 19 13:54:10 2025 +0200
[CALCITE-7022] Decouple ModelHandler from CalciteConnection
ModelHandler is mainly a parser that can transform YAML/JSON files into
SchemaPlus objects.
Currently, the class requires a CalciteConnection in order to be
instantiated and the latter is a pretty heavyweight object. The usage of the
CalciteConnection makes it rather cumbersome to instantiate and use the parsing
capabilities of the ModelHander independently.
In reality though, the handler does not need much from the
CalciteConnection and we could easily refactor the handler to not depend on the
connection at all.
The motivation for this change is to facilitate the creation of schema
objects from YAML/JSON files.
---
.../main/java/org/apache/calcite/jdbc/Driver.java | 6 ++-
.../org/apache/calcite/model/ModelHandler.java | 35 ++++++++-----
.../org/apache/calcite/model/ModelHandlerTest.java | 59 ++++++++++++++++++++++
core/src/test/resources/hsqldb-scott.json | 28 ++++++++++
4 files changed, 114 insertions(+), 14 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/jdbc/Driver.java
b/core/src/main/java/org/apache/calcite/jdbc/Driver.java
index 3d7ac155d2..5db1f15864 100644
--- a/core/src/main/java/org/apache/calcite/jdbc/Driver.java
+++ b/core/src/main/java/org/apache/calcite/jdbc/Driver.java
@@ -144,7 +144,11 @@ protected Function0<CalcitePrepare> createPrepareFactory()
{
final String model = model(connection);
if (model != null) {
try {
- new ModelHandler(connection, model);
+ ModelHandler h = new ModelHandler(connection.getRootSchema(),
model);
+ String defaultName = h.defaultSchemaName();
+ if (defaultName != null) {
+ connection.setSchema(defaultName);
+ }
} catch (IOException e) {
throw new SQLException(e);
}
diff --git a/core/src/main/java/org/apache/calcite/model/ModelHandler.java
b/core/src/main/java/org/apache/calcite/model/ModelHandler.java
index 65f9fd6597..5842619d16 100644
--- a/core/src/main/java/org/apache/calcite/model/ModelHandler.java
+++ b/core/src/main/java/org/apache/calcite/model/ModelHandler.java
@@ -77,7 +77,8 @@ public class ModelHandler {
.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
private static final ObjectMapper YAML_MAPPER = new YAMLMapper();
- private final CalciteConnection connection;
+ private final SchemaPlus rootSchema;
+ private final @Nullable String defaultSchemaName;
private final Deque<Pair<? extends @Nullable String, SchemaPlus>>
schemaStack =
new ArrayDeque<>();
private final String modelUri;
@@ -85,12 +86,10 @@ public class ModelHandler {
Lattice.@Nullable TileBuilder tileBuilder;
@SuppressWarnings("method.invocation.invalid")
- public ModelHandler(CalciteConnection connection, String uri)
- throws IOException {
+ public ModelHandler(SchemaPlus rootSchema, String uri) throws IOException {
super();
- this.connection = connection;
this.modelUri = uri;
-
+ this.rootSchema = rootSchema;
JsonRoot root;
ObjectMapper mapper;
if (uri.startsWith("inline:")) {
@@ -105,6 +104,19 @@ public ModelHandler(CalciteConnection connection, String
uri)
root = mapper.readValue(new File(uri), JsonRoot.class);
}
visit(root);
+ this.defaultSchemaName = root.defaultSchema;
+ }
+
+ @Deprecated // to be removed before 2.0
+ public ModelHandler(CalciteConnection connection, String uri) throws
IOException {
+ this(connection.getRootSchema(), uri);
+ if (defaultSchemaName != null) {
+ try {
+ connection.setSchema(defaultSchemaName);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
// CHECKSTYLE: IGNORE 1
@@ -196,7 +208,7 @@ public static void addFunctions(SchemaPlus schema,
public void visit(JsonRoot jsonRoot) {
final Pair<@Nullable String, SchemaPlus> pair =
- Pair.of(null, connection.getRootSchema());
+ Pair.of(null, rootSchema);
schemaStack.push(pair);
for (JsonType rootType : jsonRoot.types) {
rootType.accept(this);
@@ -206,13 +218,6 @@ public void visit(JsonRoot jsonRoot) {
}
final Pair<? extends @Nullable String, SchemaPlus> p = schemaStack.pop();
assert p == pair;
- if (jsonRoot.defaultSchema != null) {
- try {
- connection.setSchema(jsonRoot.defaultSchema);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
}
public void visit(JsonMapSchema jsonSchema) {
@@ -473,6 +478,10 @@ private SchemaPlus currentMutableSchema(String
elementType) {
return schema;
}
+ public @Nullable String defaultSchemaName() {
+ return this.defaultSchemaName;
+ }
+
public void visit(final JsonType jsonType) {
try {
final SchemaPlus schema = currentMutableSchema("type");
diff --git a/core/src/test/java/org/apache/calcite/model/ModelHandlerTest.java
b/core/src/test/java/org/apache/calcite/model/ModelHandlerTest.java
new file mode 100644
index 0000000000..48a546b5db
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/model/ModelHandlerTest.java
@@ -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.
+ */
+package org.apache.calcite.model;
+
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.lookup.LikePattern;
+import org.apache.calcite.util.Sources;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Unit test for {@link ModelHandler}.
+ */
+public class ModelHandlerTest {
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7022">[CALCITE-7022]
+ * Decouple ModelHandler from CalciteConnection</a>.
+ * The test ensures/demonstrates that a Schema can be easily parsed/created
from a model
+ * file (JSON/YAML) without necessitating the creation of complex/heavy
objects
+ * (e.g., CalciteConnection). */
+ @Test void testPopulateRootSchemaFromURL() throws IOException {
+ SchemaPlus root = CalciteSchema.createRootSchema(false, false).plus();
+ String mURI =
+
Sources.of(requireNonNull(ModelHandlerTest.class.getResource("/hsqldb-scott.json")))
+ .path();
+ ModelHandler h = new ModelHandler(root, mURI);
+ SchemaPlus scott = root.subSchemas().get("SCOTT");
+ Set<String> tables = scott.tables().getNames(new LikePattern("%"));
+ assertThat(tables, is(ImmutableSet.of("EMP", "DEPT", "BONUS",
"SALGRADE")));
+ assertThat(h.defaultSchemaName(), is("SCOTT"));
+ }
+
+}
diff --git a/core/src/test/resources/hsqldb-scott.json
b/core/src/test/resources/hsqldb-scott.json
new file mode 100644
index 0000000000..abbc5e53a7
--- /dev/null
+++ b/core/src/test/resources/hsqldb-scott.json
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+{
+ "version": "1.0",
+ "defaultSchema": "SCOTT",
+ "schemas": [ {
+ "type": "jdbc",
+ "name": "SCOTT",
+ "jdbcUser": "SA",
+ "jdbcPassword": "",
+ "jdbcUrl": "jdbc:hsqldb:res:scott",
+ "jdbcSchema": "SCOTT"
+ } ]
+}