Skip to content

Commit b4ef763

Browse files
feat: Function Column Aliases without an Alias Name func(x) (a, b, c)
- fixes #821 - fixes #171 Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent 0179cc0 commit b4ef763

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,23 +2530,52 @@ AllTableColumns AllTableColumns():
25302530
}
25312531

25322532
Alias Alias():
2533-
{ String name = null;
2533+
{ String name = "";
25342534
Token token = null;
25352535
boolean useAs = false;
25362536
Alias alias;
25372537
String colname;
25382538
ColDataType colDataType = null;
25392539
}
25402540
{
2541-
[<K_AS> { useAs = true; } ]
2542-
( name=RelObjectNameWithoutStart() | token=<S_CHAR_LITERAL> { name=token.image; } )
2543-
{ alias = new Alias(name,useAs); }
2541+
(
2542+
LOOKAHEAD(3) (
2543+
// Aliases with Columns:
2544+
// SELECT fun(x) AS (a,b,c)
2545+
// SELECT fun(x) AS T(a,b,c)
2546+
<K_AS>
2547+
[ LOOKAHEAD(2) name=RelObjectNameWithoutStart() ]
2548+
{ alias = new Alias(name, true ); }
2549+
2550+
"(" { List<Alias.AliasColumn> list = new ArrayList<Alias.AliasColumn>(); }
2551+
colname = RelObjectName() [ colDataType = ColDataType() ] { list.add(new Alias.AliasColumn(colname, colDataType)); }
2552+
(
2553+
"," { colDataType=null; } colname = RelObjectName() [ colDataType = ColDataType()] { list.add(new Alias.AliasColumn(colname, colDataType)); }
2554+
)*
2555+
")" { alias.setAliasColumns(list); }
2556+
2557+
)
2558+
|
2559+
(
2560+
// Aliases without Columns:
2561+
// SELECT fun(x) AS T
2562+
// SELECT fun(x) T
2563+
2564+
[<K_AS> { useAs = true; } ]
2565+
( name=RelObjectNameWithoutStart() | token=<S_CHAR_LITERAL> { name=token.image; } )
2566+
{ alias = new Alias(name,useAs); }
25442567

2545-
[ LOOKAHEAD(2) "(" { List<Alias.AliasColumn> list = new ArrayList<Alias.AliasColumn>(); }
2546-
colname = RelObjectName() [ colDataType = ColDataType() ] { list.add(new Alias.AliasColumn(colname, colDataType)); }
2547-
("," { colDataType=null; } colname = RelObjectName() [ colDataType = ColDataType()] { list.add(new Alias.AliasColumn(colname, colDataType)); } )*
2548-
")" { alias.setAliasColumns(list); } ]
2568+
/* Should not be allowed/needed
2569+
We can bring this back only if people complain
25492570

2571+
[ LOOKAHEAD(2) "(" { List<Alias.AliasColumn> list = new ArrayList<Alias.AliasColumn>(); }
2572+
colname = RelObjectName() [ colDataType = ColDataType() ] { list.add(new Alias.AliasColumn(colname, colDataType)); }
2573+
("," { colDataType=null; } colname = RelObjectName() [ colDataType = ColDataType()] { list.add(new Alias.AliasColumn(colname, colDataType)); } )*
2574+
")" { alias.setAliasColumns(list); } ]
2575+
2576+
*/
2577+
)
2578+
)
25502579
{ return alias; }
25512580
}
25522581

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.sf.jsqlparser.expression;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.test.TestUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
class AliasTest {
8+
9+
@Test
10+
void testUDTF() throws JSQLParserException {
11+
String sqlStr = "select udtf_1(words) as (a1, a2) from tab";
12+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
13+
}
14+
}

src/test/resources/net/sf/jsqlparser/statement/select/oracle-tests/analytic_query02.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ select time_id, product
1919

2020

2121

22-
--@FAILURE: Encountered unexpected token: "(" "(" recorded first on Aug 3, 2021, 7:20:07 AM
22+
--@FAILURE: Encountered unexpected token: "(" "(" recorded first on Aug 3, 2021, 7:20:07 AM
23+
--@FAILURE: Encountered unexpected token: "by" "BY" recorded first on Apr 6, 2024, 7:38:53 AM

src/test/resources/net/sf/jsqlparser/statement/select/oracle-tests/pivot01.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ select * from pivot_table
1515

1616

1717
--@SUCCESSFULLY_PARSED_AND_DEPARSED first on Aug 3, 2021, 7:20:08 AM
18-
--@FAILURE: select*from pivot_tableunpivot(yearly_total for order_mode in(store as 'direct',internet as 'online'))order by year,order_mode recorded first on Jul 12, 2023, 12:58:42 PM
18+
--@FAILURE: select*from pivot_tableunpivot(yearly_total for order_mode in(store as 'direct',internet as 'online'))order by year,order_mode recorded first on Jul 12, 2023, 12:58:42 PM
19+
--@FAILURE: Encountered unexpected token: "\'direct\'" <S_CHAR_LITERAL> recorded first on Apr 6, 2024, 7:50:18 AM

0 commit comments

Comments
 (0)