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

Make SQL parser more BQ compatible #43

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
61 changes: 54 additions & 7 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -2192,12 +2192,20 @@ SqlNode TableRef3(ExprContext exprContext, boolean lateral) :
{
(
LOOKAHEAD(2)
tableName = CompoundTableIdentifier()
( tableRef = TableHints(tableName) | { tableRef = tableName; } )
[ tableRef = ExtendTable(tableRef) ]
tableRef = Over(tableRef)
[ tableRef = Snapshot(tableRef) ]
[ tableRef = MatchRecognize(tableRef) ]
tableName = CompoundTableIdentifier() { s = span(); }
(
// Table call syntax like FROM a.b() instead of FROM TABLE(a.b())
// Three tokens needed to disambiguate EXTEND syntax from CALCITE-493.
// Example: "FROM EventLog(lastGCTime TIME)".
LOOKAHEAD(3)
tableRef = ImplicitTableFunctionCallArgs(tableName)
|
( tableRef = TableHints(tableName) | { tableRef = tableName; } )
[ tableRef = ExtendTable(tableRef) ]
tableRef = Over(tableRef)
[ tableRef = Snapshot(tableRef) ]
[ tableRef = MatchRecognize(tableRef) ]
)
|
LOOKAHEAD(2)
[ <LATERAL> { lateral = true; } ]
Expand Down Expand Up @@ -2387,6 +2395,38 @@ void AddCompoundIdentifierType(List<SqlNode> list, List<SqlNode> extendList) :
}
}

SqlNode ImplicitTableFunctionCallArgs(SqlIdentifier name) :
{
final List<SqlNode> tableFuncArgs = new ArrayList<SqlNode>();
final SqlNode call;
final Span s;
}
{
// Table call syntax like FROM a.b() instead of FROM TABLE(a.b())
// We've already parsed the name, so we don't use NamedRoutineCall.
{ s = span(); }
<LPAREN>
[
AddArg0(tableFuncArgs, ExprContext.ACCEPT_CURSOR)
(
<COMMA> {
// a comma-list can't appear where only a query is expected
checkNonQueryExpression(ExprContext.ACCEPT_CURSOR);
}
AddArg(tableFuncArgs, ExprContext.ACCEPT_CURSOR)
)*
]
<RPAREN>
{
final SqlParserPos pos = s.end(this);
call = createCall(name, pos,
SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, null,
tableFuncArgs);
return SqlStdOperatorTable.COLLECTION_TABLE.createCall(pos,
call);
}
}

SqlNode TableFunctionCall() :
{
final Span s;
Expand Down Expand Up @@ -6671,6 +6711,12 @@ List<SqlNode> JsonNameAndValue() :
}
(
<VALUE>
|
<COMMA> {
if (kvMode) {
throw SqlUtil.newContextException(getPos(), RESOURCE.illegalComma());
}
}
|
<COLON> {
if (kvMode) {
Expand Down Expand Up @@ -6707,7 +6753,8 @@ SqlCall JsonObjectFunctionCall() :
}
{
<JSON_OBJECT> { span = span(); }
<LPAREN> [
<LPAREN>
[
LOOKAHEAD(2)
list = JsonNameAndValue() {
nvArgs.addAll(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public interface CalciteResource {
@BaseMessage("Illegal identifier '':''. Was expecting ''VALUE''")
ExInst<CalciteException> illegalColon();

@BaseMessage("Illegal identifier '',''. Was expecting ''VALUE''")
ExInst<CalciteException> illegalComma();

@BaseMessage("TABLESAMPLE percentage must be between 0 and 100, inclusive")
@Property(name = "SQLSTATE", value = "2202H")
ExInst<CalciteException> invalidSampleSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3348,10 +3348,9 @@ void checkPeriodPredicate(Checker checker) {
.ok(expected);
}

/** Even in SQL Server conformance mode, we do not yet support
* 'function(args)' as an abbreviation for 'table(function(args)'. */
/** We now support 'function(args)' as an abbreviation for 'table(function(args)'. */
tanclary marked this conversation as resolved.
Show resolved Hide resolved
@Test void testOuterApplyFunctionFails() {
final String sql = "select * from dept outer apply ramp(deptno^)^)";
final String sql = "select * from dept outer apply ramp(deptno)^)^";
sql(sql)
.withConformance(SqlConformanceEnum.SQL_SERVER_2008)
.fails("(?s).*Encountered \"\\)\" at .*");
Expand Down Expand Up @@ -4524,13 +4523,20 @@ void checkPeriodPredicate(Checker checker) {
sql(sql).ok(expected);
}

@Test void testTableFunction() {
@Test void testTableFunctionWithTableWrapper() {
final String sql = "select * from table(score(table orders))";
final String expected = "SELECT *\n"
+ "FROM TABLE(`SCORE`((TABLE `ORDERS`)))";
sql(sql).ok(expected);
}

@Test void testTableFunctionWithoutTableWrapper() {
final String sql = "select * from score(table orders)";
final String expected = "SELECT *\n"
+ "FROM TABLE(`SCORE`((TABLE `ORDERS`)))";
sql(sql).ok(expected);
}

@Test void testTableFunctionWithPartitionKey() {
// test one partition key for input table
final String sql = "select * from table(topn(table orders partition by productid, 3))";
Expand Down Expand Up @@ -8679,6 +8685,10 @@ private static Consumer<List<? extends Throwable>> checkWarnings(
.ok("JSON_OBJECT(KEY 'foo' VALUE "
+ "JSON_OBJECT(KEY 'foo' VALUE 'bar' NULL ON NULL) "
+ "FORMAT JSON NULL ON NULL)");
expr("json_object('foo', 'bar')")
.ok("JSON_OBJECT(KEY 'foo' VALUE 'bar' NULL ON NULL)");
expr("json_object('foo', 'bar', 'baz', 'qux')")
.ok("JSON_OBJECT(KEY 'foo' VALUE 'bar', KEY 'baz' VALUE 'qux' NULL ON NULL)");

if (!Bug.TODO_FIXED) {
return;
Expand Down
Loading