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

Derived table syntax allows dangling AS #7574

Closed
mrotteveel opened this issue May 7, 2023 · 1 comment
Closed

Derived table syntax allows dangling AS #7574

mrotteveel opened this issue May 7, 2023 · 1 comment

Comments

@mrotteveel
Copy link
Member

mrotteveel commented May 7, 2023

The current syntax for derived tables allows a dangling AS, which should not be allowed. For example:

select *
from (
  select 1 as X from rdb$database
) as

This is the result of the syntax:

%type <selectExprNode> derived_table
derived_table
	: '(' select_expr ')' as_noise correlation_name derived_column_list
		{
			$$ = $2;
			$$->dsqlFlags |= RecordSourceNode::DFLAG_DERIVED;
			if ($5)
				$$->alias = $5->c_str();
			$$->columns = $6;
		}
	;

%type <metaNamePtr> correlation_name
correlation_name
	: /* nothing */				{ $$ = NULL; }
	| symbol_table_alias_name
	;

I think the correct syntax should be:

%type <selectExprNode> derived_table
derived_table
	: '(' select_expr ')' correlation_name derived_column_list
		{
			$$ = $2;
			$$->dsqlFlags |= RecordSourceNode::DFLAG_DERIVED;
			if ($5)
				$$->alias = $5->c_str();
			$$->columns = $6;
		}
	;

%type <metaNamePtr> correlation_name
correlation_name
	: /* nothing */				{ $$ = NULL; }
	| as_noise symbol_table_alias_name
	;
@mrotteveel
Copy link
Member Author

mrotteveel commented May 7, 2023

This syntax also allows you to specify derived_column_list without an alias, which shouldn't be allowed according to the SQL standard. The syntax in the standard is:

<derived table> <correlation or recognition>

<correlation or recognition> ::=
  [ AS ] <correlation name>
  [ <parenthesized derived column list> ]

So, the alias is actually required for a derived table, and you should not be able to specify the derived column list without an alias. I'm not sure if it is worth fixing that as well, as it might break existing queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment