Skip to content

Commit

Permalink
Merge pull request #60 from rayokota/upgrade-calcite-1.25.0
Browse files Browse the repository at this point in the history
Upgrade to Calcite 1.25.0
  • Loading branch information
rayokota authored Sep 4, 2020
2 parents efdb77c + dc41bc3 commit 308ccd0
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 34 deletions.
225 changes: 196 additions & 29 deletions kareldb-core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,27 @@ JAVACODE SqlParseException convertException(Throwable ex)
tokenImage = pex.tokenImage;
if (pex.currentToken != null) {
final Token token = pex.currentToken.next;
// Checks token.image.equals("1") to avoid recursive call.
// The SqlAbstractParserImpl#MetadataImpl constructor uses constant "1" to
// throw intentionally to collect the expected tokens.
if (!token.image.equals("1")
&& getMetadata().isKeyword(token.image)
&& SqlParserUtil.allowsIdentifier(tokenImage, expectedTokenSequences)) {
// If the next token is a keyword, reformat the error message as:

// Incorrect syntax near the keyword '{keyword}' at line {line_number},
// column {column_number}.
final String expecting = ex.getMessage()
.substring(ex.getMessage().indexOf("Was expecting"));
final String errorMsg = String.format("Incorrect syntax near the keyword '%s' "
+ "at line %d, column %d.\n%s",
token.image,
token.beginLine,
token.beginColumn,
expecting);
// Replace the ParseException with explicit error message.
ex = new ParseException(errorMsg);
}
pos = new SqlParserPos(
token.beginLine,
token.beginColumn,
Expand Down Expand Up @@ -1807,7 +1828,15 @@ SqlNode SelectItem() :
e = SelectExpression()
[
[ <AS> ]
id = SimpleIdentifier() {
(
id = SimpleIdentifier()
|
// Mute the warning about ambiguity between alias and continued
// string literal.
LOOKAHEAD(1)
id = SimpleIdentifierFromStringLiteral()
)
{
e = SqlStdOperatorTable.AS.createCall(span().end(e), e, id);
}
]
Expand Down Expand Up @@ -3741,7 +3770,7 @@ SqlNode AtomicRowExpression() :
}
{
(
e = Literal()
e = LiteralOrIntervalExpression()
|
e = DynamicParam()
|
Expand Down Expand Up @@ -3977,11 +4006,29 @@ SqlDrop SqlDrop() :
* Usually returns an SqlLiteral, but a continued string literal
* is an SqlCall expression, which concatenates 2 or more string
* literals; the validator reduces this.
*
* <p>If the context allows both literals and expressions,
* use {@link #LiteralOrIntervalExpression}, which requires less
* lookahead.
*/
SqlNode Literal() :
{
SqlNode e;
}
{
(
e = NonIntervalLiteral()
|
e = IntervalLiteral()
)
{ return e; }
}

/** Parses a literal that is not an interval literal. */
SqlNode NonIntervalLiteral() :
{
final SqlNode e;
}
{
(
e = NumericLiteral()
Expand All @@ -3991,8 +4038,6 @@ SqlNode Literal() :
e = SpecialLiteral()
|
e = DateTimeLiteral()
|
e = IntervalLiteral()
<#-- additional literal parser methods are included here -->
<#list parser.literalParserMethods as method>
|
Expand All @@ -4002,8 +4047,25 @@ SqlNode Literal() :
{
return e;
}
}


/** Parses a literal or an interval expression.
*
* <p>We include them in the same production because it is difficult to
* distinguish interval literals from interval expression (both of which
* start with the {@code INTERVAL} keyword); this way, we can use less
* LOOKAHEAD. */
SqlNode LiteralOrIntervalExpression() :
{
final SqlNode e;
}
{
(
e = IntervalLiteralOrExpression()
|
e = NonIntervalLiteral()
)
{ return e; }
}

/** Parses a unsigned numeric literal */
Expand Down Expand Up @@ -4095,6 +4157,12 @@ SqlNode StringLiteral() :
}
}
(
// The grammar is ambiguous when a continued literals and a character
// string alias are both possible. For example, in
// SELECT x'01'\n'ab'
// we prefer that 'ab' continues the literal, and is not an alias.
// The following LOOKAHEAD mutes the warning about ambiguity.
LOOKAHEAD(1)
<QUOTED_STRING>
{
try {
Expand Down Expand Up @@ -4144,6 +4212,12 @@ SqlNode StringLiteral() :
nfrags++;
}
(
// The grammar is ambiguous when a continued literals and a character
// string alias are both possible. For example, in
// SELECT 'taxi'\n'cab'
// we prefer that 'cab' continues the literal, and is not an alias.
// The following LOOKAHEAD mutes the warning about ambiguity.
LOOKAHEAD(1)
<QUOTED_STRING>
{
p = SqlParserUtil.parseString(token.image);
Expand Down Expand Up @@ -4374,6 +4448,53 @@ SqlLiteral IntervalLiteral() :
}
}

/** Parses an interval literal (e.g. {@code INTERVAL '2:3' HOUR TO MINUTE})
* or an interval expression (e.g. {@code INTERVAL emp.empno MINUTE}
* or {@code INTERVAL 3 MONTHS}). */
SqlNode IntervalLiteralOrExpression() :
{
final String p;
final SqlIntervalQualifier intervalQualifier;
int sign = 1;
final Span s;
SqlNode e;
}
{
<INTERVAL> { s = span(); }
[
<MINUS> { sign = -1; }
|
<PLUS> { sign = 1; }
]
(
// literal (with quoted string)
<QUOTED_STRING> { p = token.image; }
intervalQualifier = IntervalQualifier() {
return SqlParserUtil.parseIntervalLiteral(s.end(intervalQualifier),
sign, p, intervalQualifier);
}
|
// To keep parsing simple, any expressions besides numeric literal and
// identifiers must be enclosed in parentheses.
(
<LPAREN>
e = Expression(ExprContext.ACCEPT_SUB_QUERY)
<RPAREN>
|
e = UnsignedNumericLiteral()
|
e = CompoundIdentifier()
)
intervalQualifier = IntervalQualifierStart() {
if (sign == -1) {
e = SqlStdOperatorTable.UNARY_MINUS.createCall(e.getParserPosition(), e);
}
return SqlStdOperatorTable.INTERVAL.createCall(s.end(this), e,
intervalQualifier);
}
)
}

TimeUnit Year() :
{
}
Expand Down Expand Up @@ -4430,34 +4551,36 @@ TimeUnit Second() :

SqlIntervalQualifier IntervalQualifier() :
{
final Span s;
final TimeUnit start;
TimeUnit end = null;
int startPrec = RelDataType.PRECISION_NOT_SPECIFIED;
int secondFracPrec = RelDataType.PRECISION_NOT_SPECIFIED;
}
{
(
start = Year() [ <LPAREN> startPrec = UnsignedIntLiteral() <RPAREN> ]
start = Year() { s = span(); } startPrec = PrecisionOpt()
[
LOOKAHEAD(2) <TO> end = Month()
]
|
start = Month() [ <LPAREN> startPrec = UnsignedIntLiteral() <RPAREN> ]
start = Month() { s = span(); } startPrec = PrecisionOpt()
|
start = Day() [ <LPAREN> startPrec = UnsignedIntLiteral() <RPAREN> ]
[ LOOKAHEAD(2) <TO>
start = Day() { s = span(); } startPrec = PrecisionOpt()
[
LOOKAHEAD(2) <TO>
(
end = Hour()
|
end = Minute()
|
end = Second()
[ <LPAREN> secondFracPrec = UnsignedIntLiteral() <RPAREN> ]
end = Second() secondFracPrec = PrecisionOpt()
)
]
|
start = Hour() [ <LPAREN> startPrec = UnsignedIntLiteral() <RPAREN> ]
[ LOOKAHEAD(2) <TO>
start = Hour() { s = span(); } startPrec = PrecisionOpt()
[
LOOKAHEAD(2) <TO>
(
end = Minute()
|
Expand All @@ -4466,26 +4589,54 @@ SqlIntervalQualifier IntervalQualifier() :
)
]
|
start = Minute() [ <LPAREN> startPrec = UnsignedIntLiteral() <RPAREN> ]
[ LOOKAHEAD(2) <TO>
(
end = Second()
[ <LPAREN> secondFracPrec = UnsignedIntLiteral() <RPAREN> ]
)
start = Minute() { s = span(); } startPrec = PrecisionOpt()
[
LOOKAHEAD(2) <TO> end = Second()
[ <LPAREN> secondFracPrec = UnsignedIntLiteral() <RPAREN> ]
]
|
start = Second()
start = Second() { s = span(); }
[
<LPAREN> startPrec = UnsignedIntLiteral()
[ <COMMA> secondFracPrec = UnsignedIntLiteral() ]
<RPAREN>
]
)
{
return new SqlIntervalQualifier(start, startPrec, end, secondFracPrec,
s.end(this));
}
}

/** Interval qualifier without 'TO unit'. */
SqlIntervalQualifier IntervalQualifierStart() :
{
final Span s;
final TimeUnit start;
int startPrec = RelDataType.PRECISION_NOT_SPECIFIED;
int secondFracPrec = RelDataType.PRECISION_NOT_SPECIFIED;
}
{
(
(
start = Year()
| start = Month()
| start = Day()
| start = Hour()
| start = Minute()
)
{ s = span(); }
startPrec = PrecisionOpt()
|
start = Second() { s = span(); }
[ <LPAREN> startPrec = UnsignedIntLiteral()
[ <COMMA> secondFracPrec = UnsignedIntLiteral() ]
<RPAREN>
]
)
{
return new SqlIntervalQualifier(start,
startPrec,
end,
secondFracPrec,
getPos());
return new SqlIntervalQualifier(start, startPrec, null, secondFracPrec,
s.end(this));
}
}

Expand Down Expand Up @@ -4656,6 +4807,23 @@ SqlIdentifier SimpleIdentifier() :
}
}

/**
* Parses a character literal as an SqlIdentifier.
* Only valid for column aliases in certain dialects.
*/
SqlIdentifier SimpleIdentifierFromStringLiteral() :
{
}
{
<QUOTED_STRING> {
if (!this.conformance.allowCharLiteralAlias()) {
throw SqlUtil.newContextException(getPos(), RESOURCE.charLiteralAliasNotValid());
}
final String s = SqlParserUtil.parseString(token.image);
return new SqlIdentifier(s, getPos());
}
}

/**
* Parses a comma-separated list of simple identifiers.
*/
Expand Down Expand Up @@ -5201,7 +5369,6 @@ int PrecisionOpt() :
int precision = -1;
}
{
LOOKAHEAD(2)
<LPAREN>
precision = UnsignedIntLiteral()
<RPAREN>
Expand Down Expand Up @@ -6055,22 +6222,22 @@ SqlCall GroupByWindowingCall():
(
<TUMBLE>
{
s = span();
op = SqlStdOperatorTable.TUMBLE_OLD;
}
|
<HOP>
{
s = span();
op = SqlStdOperatorTable.HOP_OLD;
}
|
<SESSION>
{
s = span();
op = SqlStdOperatorTable.SESSION_OLD;
}
)
{
s = span();
}
args = UnquantifiedFunctionParameterList(ExprContext.ACCEPT_SUB_QUERY) {
return op.createCall(s.end(this), args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.calcite.adapter.enumerable.EnumerableConvention;
import org.apache.calcite.plan.Convention;
import org.apache.calcite.plan.RelTrait;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.convert.ConverterRule;
Expand Down Expand Up @@ -45,9 +46,15 @@ public class EnumerableTableModifyExtensionRule extends ConverterRule {
* @param relBuilderFactory Builder for relational expressions
*/
public EnumerableTableModifyExtensionRule(RelBuilderFactory relBuilderFactory) {
super(LogicalTableModify.class, (Predicate<RelNode>) r -> true,
Convention.NONE, EnumerableConvention.INSTANCE, relBuilderFactory,
"EnumerableTableModificationExtensionRule");
super(Config.EMPTY
.withRelBuilderFactory(relBuilderFactory)
.as(Config.class)
.withConversion(
LogicalTableModify.class,
(Predicate<RelNode>) r -> true,
Convention.NONE,
EnumerableConvention.INSTANCE,
"EnumerableTableModificationExtensionRule"));
}

@Override
Expand Down
Loading

0 comments on commit 308ccd0

Please sign in to comment.