Skip to content

Commit

Permalink
Support UDT declarations from root of schema model. (apache#7)
Browse files Browse the repository at this point in the history
Related to CALCITE-5346.

Previously we relied on the parser to map unrecognized datatypes to a known type using SqlAlienSystemTypeNameSpec. This worked but made it difficult to change or add new types as necessary. One would have to update at least 3 different parsers (babel, core, server) to make a change.

This change allows for declaring user-defined types at the root of a schema model and allows for easy type alias mapping. These data types are shared by all schema in the model so cast and DDL expressions do not need to scope data type references to a particular sub-schema. 

For example:
```
inline: {
  version: '1.0',
  types: [
    {
      name: 'BOOL',
      type: 'BOOLEAN'
    },
    {
      name: 'BYTES',
      type: 'VARBINARY'
    },
...
  ],
```
Allows for `CAST("true" as BOOL)`
  • Loading branch information
tjbanghart committed Mar 24, 2023
1 parent 0da6571 commit 99d2491
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/model/JsonRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public class JsonRoot {
*/
public final List<JsonSchema> schemas = new ArrayList<>();

/** Types in the root schema. 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,
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/apache/calcite/model/ModelHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ public void visit(JsonRoot jsonRoot) {
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);
}
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/org/apache/calcite/test/UdtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -59,6 +65,12 @@ private CalciteAssert.AssertThat withUdt() {
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>. */
Expand Down

0 comments on commit 99d2491

Please sign in to comment.