google / zetasql

ZetaSQL - Analyzer Framework for SQL
Apache License 2.0
2.28k stars 214 forks source link

Java version: How to add aliases for types? #110

Closed tha23rd closed 2 years ago

tha23rd commented 2 years ago

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?

ppaglilla commented 2 years ago

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.

tha23rd commented 2 years ago

Awesome! Thanks so much for the info