google/zetasql

Java version: How to add aliases for types?

tha23rd opened this issue · 2 comments

Hi all,

In BQ we have a couple of queries such as:

select CAST(col as int) from table

ZetaSQL complains with:
com.google.zetasql.SqlException: Type not found: int

Is there a way to add type aliases, and if so, how?

Hi @tha23rd!

You can indeed create type aliases. You do so by adding the a new type to your catalog. In the case of the SimpleCatalog, that can be achieved through the SimpleCatalog.addType method.

For example, you can create the INT type alias this way:

SimpleCatalog yourCatalog = new SimpleCatalog("catalog");
yourCatalog.addType(
    "INT",  // INT is the name of our alias
    TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_INT64) // This is an alias for the INT64 type
);

That example can easily be extended to all the type aliases available in BigQuery.

Awesome! Thanks so much for the info