Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for BigQuery ANY TYPE data type #1602

Merged
merged 4 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ pub enum DataType {
///
/// [postgresql]: https://www.postgresql.org/docs/current/plpgsql-trigger.html
Trigger,
/// Any data type, used in BigQuery UDF definitions for templated parameters
///
/// [bigquery]: https://cloud.google.com/bigquery/docs/user-defined-functions#templated-sql-udf-parameters
AnyType,
}

impl fmt::Display for DataType {
Expand All @@ -383,7 +387,6 @@ impl fmt::Display for DataType {
DataType::CharacterVarying(size) => {
format_character_string_type(f, "CHARACTER VARYING", size)
}

DataType::CharVarying(size) => format_character_string_type(f, "CHAR VARYING", size),
DataType::Varchar(size) => format_character_string_type(f, "VARCHAR", size),
DataType::Nvarchar(size) => format_character_string_type(f, "NVARCHAR", size),
Expand Down Expand Up @@ -626,6 +629,7 @@ impl fmt::Display for DataType {
}
DataType::Unspecified => Ok(()),
DataType::Trigger => write!(f, "TRIGGER"),
DataType::AnyType => write!(f, "ANY TYPE"),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8382,6 +8382,10 @@ impl<'a> Parser<'a> {
Ok(DataType::Tuple(field_defs))
}
Keyword::TRIGGER => Ok(DataType::Trigger),
Keyword::ANY => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Keyword::ANY => {
Keyword::ANY if self.peek_keyword(Keyword::TYPE) => {

Thinking we can add this guard to ensure that it doesn't conflict with a custom data type that happens to be called ANY?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that is much more in line with the desired / intended behavior. As the current approach would result in a parsing error should we come across an ANY type I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried implementing this change but it seems to break. hmm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is the first time I'm touching the codebase, I needed to understand the general mechanisms. Your change worked in conjunction with let _ = self.parse_keyword(Keyword::TYPE);, which makes intitutive sense after looking at the code in general.

self.expect_keyword(Keyword::TYPE)?;
Ok(DataType::AnyType)
}
_ => {
self.prev_token();
let type_name = self.parse_object_name(false)?;
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,14 @@ fn test_bigquery_create_function() {
"REMOTE WITH CONNECTION us.myconnection ",
"OPTIONS(a = [1, 2])",
),
// Templated
concat!(
"CREATE OR REPLACE TEMPORARY FUNCTION ",
"my_function(param1 ANY TYPE) ",
"AS (",
"(SELECT 1)",
")",
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the functionality itself is the data type, I'm thinking we can use a dedicated test for it? e.g fn test_parse_any_type()? Also can we add a test case to demonstrate the behavior in the other comment what happens in a sql like CREATE TABLE foo (x ANY);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I was in fact doing that but reverted to inlining the test with other UDFs. Not being sure what was the preferred pattern but makes sense!

];
for sql in sqls {
bigquery().verified_stmt(sql);
Expand Down