This is an automated email from the ASF dual-hosted git repository.
jhyde 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 5d93ed4c89 [CALCITE-5621] Allow user-defined type declarations (UDTs)
in the root of the JSON catalog
5d93ed4c89 is described below
commit 5d93ed4c89f048d30f83e6046159920044f4a8fe
Author: TJ Banghart <[email protected]>
AuthorDate: Wed Mar 29 12:58:24 2023 -0700
[CALCITE-5621] Allow user-defined type declarations (UDTs) in the root of
the JSON catalog
Such types are considered global, and can be seen in all
schemas.
Close apache/calcite#3135
---
core/src/main/java/org/apache/calcite/model/JsonRoot.java | 11 ++++++++++-
core/src/main/java/org/apache/calcite/model/JsonType.java | 3 ++-
.../src/main/java/org/apache/calcite/model/ModelHandler.java | 3 +++
core/src/test/java/org/apache/calcite/test/UdtTest.java | 12 ++++++++++++
site/_docs/model.md | 9 +++++++--
5 files changed, 34 insertions(+), 4 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/model/JsonRoot.java
b/core/src/main/java/org/apache/calcite/model/JsonRoot.java
index f35790c6a4..5d6115cebe 100644
--- a/core/src/main/java/org/apache/calcite/model/JsonRoot.java
+++ b/core/src/main/java/org/apache/calcite/model/JsonRoot.java
@@ -49,10 +49,11 @@ import static java.util.Objects.requireNonNull;
* {@link JsonTile} (in collection {@link JsonLattice#tiles tiles})
* {@link JsonMeasure} (in collection {@link JsonTile#measures
measures})
* {@link JsonMaterialization} (in collection {@link
JsonSchema#materializations materializations})
+ * {@link JsonType} (in collection {@link JsonRoot#types types})
* </pre>
* <!-- CHECKSTYLE: ON -->
*
- * <p>See the <a href="http://calcite.apache.org/docs/model.html">JSON
+ * <p>See the <a href="https://calcite.apache.org/docs/model.html">JSON
* model reference</a>.
*/
public class JsonRoot {
@@ -73,6 +74,14 @@ public class JsonRoot {
*/
public final List<JsonSchema> schemas = new ArrayList<>();
+ /** List of types in the root schema.
+ *
+ * <p>Such types global, that is, shared by all schemas in the model.
+ *
+ * <p>The list may be empty.
+ */
+ public final List<JsonType> types = new ArrayList<>();
+
@JsonCreator
public JsonRoot(
@JsonProperty(value = "version", required = true) String version,
diff --git a/core/src/main/java/org/apache/calcite/model/JsonType.java
b/core/src/main/java/org/apache/calcite/model/JsonType.java
index c7475a7686..bc3f7fd30e 100644
--- a/core/src/main/java/org/apache/calcite/model/JsonType.java
+++ b/core/src/main/java/org/apache/calcite/model/JsonType.java
@@ -29,7 +29,8 @@ import static java.util.Objects.requireNonNull;
/**
* Type schema element.
*
- * <p>Occurs within {@link JsonMapSchema#tables}.
+ * <p>Occurs within {@link JsonMapSchema#types},
+ * {@link JsonRoot#types}.
*
* @see JsonRoot Description of schema elements
*/
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 e464ab1dae..128869afe8 100644
--- a/core/src/main/java/org/apache/calcite/model/ModelHandler.java
+++ b/core/src/main/java/org/apache/calcite/model/ModelHandler.java
@@ -196,6 +196,9 @@ public class ModelHandler {
final Pair<@Nullable String, SchemaPlus> pair =
Pair.of(null, connection.getRootSchema());
schemaStack.push(pair);
+ for (JsonType rootType : jsonRoot.types) {
+ rootType.accept(this);
+ }
for (JsonSchema schema : jsonRoot.schemas) {
schema.accept(this);
}
diff --git a/core/src/test/java/org/apache/calcite/test/UdtTest.java
b/core/src/test/java/org/apache/calcite/test/UdtTest.java
index e3786d8b8c..970a40a0b7 100644
--- a/core/src/test/java/org/apache/calcite/test/UdtTest.java
+++ b/core/src/test/java/org/apache/calcite/test/UdtTest.java
@@ -25,6 +25,12 @@ class UdtTest {
private CalciteAssert.AssertThat withUdt() {
final String model = "{\n"
+ " version: '1.0',\n"
+ + " types: [\n"
+ + " {\n"
+ + " name: 'foo',\n"
+ + " type: 'BIGINT'\n"
+ + " }"
+ + " ],\n"
+ " schemas: [\n"
+ " {\n"
+ " name: 'adhoc',\n"
@@ -59,6 +65,12 @@ class UdtTest {
withUdt().query(sql).returns("LD=1\n");
}
+ @Test void testRootUdt() {
+ final String sql = "select CAST(\"id\" AS foo) as ld "
+ + "from (VALUES ROW(1, 'SameName')) AS \"t\" (\"id\", \"desc\")";
+ withUdt().query(sql).returns("LD=1\n");
+ }
+
/** Test case for
* <a
href="https://issues.apache.org/jira/browse/CALCITE-3045">[CALCITE-3045]
* NullPointerException when casting null literal to composite user defined
type</a>. */
diff --git a/site/_docs/model.md b/site/_docs/model.md
index bc83a256e2..567313d973 100644
--- a/site/_docs/model.md
+++ b/site/_docs/model.md
@@ -36,7 +36,8 @@ Models can also be built programmatically using the `Schema`
SPI.
{
version: '1.0',
defaultSchema: 'mongo',
- schemas: [ Schema... ]
+ schemas: [ Schema... ],
+ types: [ Type... ]
}
{% endhighlight %}
@@ -46,6 +47,8 @@ version: 1.0
defaultSchema: mongo
schemas:
- [Schema...]
+types:
+- [Type...]
{% endhighlight %}
`version` (required string) must have value `1.0`.
@@ -56,6 +59,8 @@ become the default schema for connections to Calcite that use
this model.
`schemas` (optional list of <a href="#schema">Schema</a> elements).
+`types` (optional list of <a href="#type">Type</a> elements shared by all
schemas).
+
### Schema
Occurs within `root.schemas`.
@@ -496,7 +501,7 @@ if found, creates an aggregate function.
### Type
-Occurs within `root.schemas.types`.
+Occurs within `root.types` and `root.schemas.types`.
#### JSON
{% highlight json %}