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

[WIP] Prototype rewriting UNION ALL to disjunction #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions parser/grammar/sql-spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ void numeric_primary():
| character_value_expression()
}

void numeric_value_function() #FunctionCall:
void numeric_value_function() #BuiltinFunctionCall(1):
{}
{
position_expression()
Expand Down Expand Up @@ -1371,7 +1371,7 @@ void string_value_function():
| binary_value_function()
}

void character_value_function() #FunctionCall:
void character_value_function() #BuiltinFunctionCall:
{}
{
character_substring_function()
Expand Down Expand Up @@ -1595,7 +1595,7 @@ void time_zone_specifier():
| "TIME" "ZONE" interval_primary()
}

void datetime_value_function() #FunctionCall:
void datetime_value_function() #BuiltinFunctionCall:
{}
{
current_date_value_function()
Expand Down Expand Up @@ -1668,7 +1668,7 @@ void interval_primary():
}


void interval_value_function() #FunctionCall:
void interval_value_function() #BuiltinFunctionCall:
{}
{
interval_absolute_value_function()
Expand Down
5 changes: 5 additions & 0 deletions parser/src/main/java/com/facebook/coresql/parser/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public String toString(String prefix)
return super.toString(prefix) + " (" + getLocation().toString() + ")" +
(NumChildren() == 0 ? " (" + beginToken.image + ")" : "");
}

public String GetSqlString()
{
return Unparser.unparseClean(this);
}
}
12 changes: 9 additions & 3 deletions parser/src/main/java/com/facebook/coresql/parser/Unparser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
package com.facebook.coresql.parser;

import static com.facebook.coresql.parser.SqlParserConstants.EOF;
import static com.facebook.coresql.parser.SqlParserConstants.tokenImage;

public class Unparser
extends com.facebook.coresql.parser.SqlParserDefaultVisitor
{
protected StringBuilder stringBuilder = new StringBuilder();
private Token lastToken = new Token();

public static String unparse(AstNode node, Unparser unparser)
public static String unparseClean(AstNode node, Unparser unparser)
{
unparser.stringBuilder.setLength(0);
unparser.lastToken.next = node.beginToken;
Expand All @@ -30,9 +31,9 @@ public static String unparse(AstNode node, Unparser unparser)
return unparser.stringBuilder.toString();
}

public static String unparse(AstNode node)
public static String unparseClean(AstNode node)
{
return unparse(node, new Unparser());
return unparseClean(node, new Unparser());
}

private void printSpecialTokens(Token t)
Expand Down Expand Up @@ -62,6 +63,11 @@ public final void printToken(String s)
stringBuilder.append(" " + s + " ");
}

public final void printKeyword(int keyword)
{
printToken(tokenImage[keyword].substring(1, tokenImage[keyword].length() - 1));
}

private void printToken(Token t)
{
while (lastToken != t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.IOException;

import static com.facebook.coresql.parser.ParserHelper.parseStatement;
import static com.facebook.coresql.parser.Unparser.unparse;
import static com.facebook.coresql.parser.Unparser.unparseClean;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

Expand Down Expand Up @@ -66,7 +66,7 @@ public void parseUnparseTest()
for (String sql : TEST_SQL_TESTSTRINGS) {
AstNode ast = parse(sql);
assertNotNull(ast);
assertEquals(sql.trim(), unparse(ast).trim());
assertEquals(sql.trim(), unparseClean(ast).trim());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.Optional;

import static com.facebook.coresql.parser.ParserHelper.parseStatement;
import static com.facebook.coresql.parser.Unparser.unparse;
import static com.facebook.coresql.parser.Unparser.unparseClean;
import static com.facebook.coresql.parser.sqllogictest.java.SqlLogicTest.CoreSqlParsingError.PARSING_ERROR;
import static com.facebook.coresql.parser.sqllogictest.java.SqlLogicTest.CoreSqlParsingError.UNPARSED_DOES_NOT_MATCH_ORIGINAL_ERROR;
import static com.facebook.coresql.parser.sqllogictest.java.SqlLogicTest.CoreSqlParsingError.UNPARSING_ERROR;
Expand Down Expand Up @@ -132,7 +132,7 @@ private Optional<CoreSqlParsingError> checkStatementForParserError(String statem
}
String unparsed;
try {
unparsed = unparse(ast.get());
unparsed = unparseClean(ast.get());
}
catch (Exception e) {
return Optional.of(UNPARSING_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Optional<RewriteResult> rewrite()
if (patternMatchedNodes.isEmpty()) {
return Optional.empty();
}
String rewrittenSql = Unparser.unparse(root, this);
String rewrittenSql = Unparser.unparseClean(root, this);
return Optional.of(new RewriteResult(REWRITE_NAME, rewrittenSql));
}

Expand Down
Loading