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

[CALCITE-2395] Support SELECT xxx FROM TABLE FOR UPDATE syntax #763

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[CALCITE-2395] Support SELECT xxx FROM TABLE FOR UPDATE syntax
Initial support for UpdatabilityClause in SQLParser
  • Loading branch information
eolivelli committed Jul 17, 2018
commit 48419b9ed53893e2e9bdd6773896741bc37278b7
48 changes: 47 additions & 1 deletion core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import org.apache.calcite.sql.SqlTimeLiteral;
import org.apache.calcite.sql.SqlTimestampLiteral;
import org.apache.calcite.sql.SqlUnnestOperator;
import org.apache.calcite.sql.SqlUpdate;
import org.apache.calcite.sql.SqlUpdatabilityClause;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlWindow;
import org.apache.calcite.sql.SqlWith;
Expand Down Expand Up @@ -1017,6 +1018,7 @@ SqlSelect SqlSelect() :
final SqlNode having;
final SqlNodeList windowDecls;
final Span s;
final SqlNode updatabilityClause;
}
{
<SELECT>
Expand Down Expand Up @@ -1047,19 +1049,21 @@ SqlSelect SqlSelect() :
groupBy = GroupByOpt()
having = HavingOpt()
windowDecls = WindowOpt()
updatabilityClause = UpdatabilityClauseOpt()
|
E() {
fromClause = null;
where = null;
groupBy = null;
having = null;
windowDecls = null;
updatabilityClause = null;
}
)
{
return new SqlSelect(s.end(this), keywordList,
new SqlNodeList(selectList, Span.of(selectList).pos()),
fromClause, where, groupBy, having, windowDecls, null, null, null);
fromClause, where, groupBy, having, windowDecls, null, null, null, updatabilityClause);
}
}

Expand Down Expand Up @@ -1789,6 +1793,32 @@ SqlNode FromClause() :
}
}

/**
* Parses the optional updatability clause.
*
* <p>SELECT ... FOR UPDATE OF table [, table].
*/
SqlNode UpdatabilityClauseOpt() :
{
SqlNodeList list = null;
}
{
(
<FOR> <UPDATE>
[
<OF>
list = NonParenthesizedSimpleIdentifierList()
]
)
{
return new SqlUpdatabilityClause(list, getPos());
}
|
{
return null;
}
}

/**
* Parses a table reference in a FROM clause, not lateral unless LATERAL
* is explicitly specified.
Expand Down Expand Up @@ -4247,6 +4277,22 @@ SqlNodeList ParenthesizedSimpleIdentifierList() :
}
}

/**
* List of simple identifiers without parentheses.
*/
SqlNodeList NonParenthesizedSimpleIdentifierList() :
{
final Span s;
final List<SqlNode> list = new ArrayList<SqlNode>();
}
{
{ s = span(); }
SimpleIdentifierCommaList(list)
{
return new SqlNodeList(list, s.end(this));
}
}

<#if parser.includeCompoundIdentifier >
/**
* Parses a compound identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ SqlString generateSql() {
SqlParserPos.ZERO);
SqlSelect node =
new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, selectList,
tableName(), null, null, null, null, null, null, null);
tableName(), null, null, null, null, null, null, null, null);
final SqlPrettyWriter writer = new SqlPrettyWriter(jdbcSchema.dialect);
node.unparse(writer, 0, 0);
return writer.toSqlString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public Result visit(Values e) {
new SqlSelect(POS, null,
new SqlNodeList(values2, POS),
new SqlIdentifier("DUAL", POS), null, null,
null, null, null, null, null));
null, null, null, null, null, null));
}
if (list.size() == 1) {
query = list.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ SqlSelect wrapSelect(SqlNode node) {
|| ((SqlCall) node).getOperator() == SqlStdOperatorTable.VALUES)
: node;
return new SqlSelect(POS, SqlNodeList.EMPTY, null, node, null, null, null,
SqlNodeList.EMPTY, null, null, null);
SqlNodeList.EMPTY, null, null, null, null);
}

/** Context for translating a {@link RexNode} expression (within a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
null,
null,
null,
null,
null);
}
}
Expand Down
18 changes: 16 additions & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class SqlSelect extends SqlCall {
SqlNodeList orderBy;
SqlNode offset;
SqlNode fetch;
SqlNode updatabilityClause;
SqlMatchRecognize matchRecognize;

//~ Constructors -----------------------------------------------------------
Expand All @@ -62,7 +63,8 @@ public SqlSelect(SqlParserPos pos,
SqlNodeList windowDecls,
SqlNodeList orderBy,
SqlNode offset,
SqlNode fetch) {
SqlNode fetch,
SqlNode updatabilityClause) {
super(pos);
this.keywordList = Objects.requireNonNull(keywordList != null
? keywordList : new SqlNodeList(pos));
Expand All @@ -76,6 +78,7 @@ public SqlSelect(SqlParserPos pos,
this.orderBy = orderBy;
this.offset = offset;
this.fetch = fetch;
this.updatabilityClause = updatabilityClause;
}

//~ Methods ----------------------------------------------------------------
Expand All @@ -90,7 +93,7 @@ public SqlOperator getOperator() {

@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(keywordList, selectList, from, where,
groupBy, having, windowDecls, orderBy, offset, fetch);
groupBy, having, windowDecls, orderBy, offset, fetch, updatabilityClause);
}

@Override public void setOperand(int i, SqlNode operand) {
Expand Down Expand Up @@ -125,6 +128,9 @@ public SqlOperator getOperator() {
case 9:
fetch = operand;
break;
case 10:
updatabilityClause = operand;
break;
default:
throw new AssertionError(i);
}
Expand Down Expand Up @@ -169,6 +175,14 @@ public void setHaving(SqlNode having) {
this.having = having;
}

public SqlNode getUpdatabilityClause() {
return updatabilityClause;
}

public void setUpdatabilityClause(SqlNode updatabilityClause) {
this.updatabilityClause = updatabilityClause;
}

public final SqlNodeList getSelectList() {
return selectList;
}
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlSelectOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public SqlCall createCall(
(SqlNodeList) operands[6],
(SqlNodeList) operands[7],
operands[8],
operands[9]);
operands[9],
operands[10]);
}

/**
Expand Down Expand Up @@ -104,6 +105,7 @@ public SqlSelect createCall(
SqlNodeList orderBy,
SqlNode offset,
SqlNode fetch,
SqlNode updatabilityClause,
SqlParserPos pos) {
return new SqlSelect(
pos,
Expand All @@ -116,7 +118,8 @@ public SqlSelect createCall(
windowDecls,
orderBy,
offset,
fetch);
fetch,
updatabilityClause);
}

public <R> void acceptCall(
Expand Down Expand Up @@ -240,6 +243,9 @@ public void unparse(
writer.endList(orderFrame);
}
writer.fetchOffset(select.fetch, select.offset);
if (select.updatabilityClause != null) {
select.updatabilityClause.unparse(writer, leftPrec, rightPrec);
}
writer.endList(selectFrame);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.sql;

import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.util.SqlVisitor;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql.validate.SqlValidatorScope;
import org.apache.calcite.util.Litmus;

import java.util.Objects;


/**
* Represents the updatability clause.
* SELECT .. FROM ... FOR UPDATE [OF table,table...]
*/
public class SqlUpdatabilityClause extends SqlNode {

final SqlNodeList tables;

public SqlUpdatabilityClause(SqlNodeList tables, SqlParserPos pos) {
super(pos);
this.tables = Objects.requireNonNull(tables != null
? tables : new SqlNodeList(pos));
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.sep("FOR UPDATE");
if (this.tables.size() > 0) {
writer.sep("OF");
final SqlWriter.Frame tablesFrame =
writer.startList(SqlWriter.FrameTypeEnum.SELECT_LIST);
tables.commaList(writer);
writer.endList(tablesFrame);
}
}

@Override public SqlNode clone(SqlParserPos pos) {
throw new UnsupportedOperationException();
}

@Override public void validate(SqlValidator validator, SqlValidatorScope scope) {
throw new UnsupportedOperationException();
}

@Override public <R> R accept(SqlVisitor<R> visitor) {
throw new UnsupportedOperationException();
}

@Override public boolean equalsDeep(SqlNode node, Litmus litmus) {
throw new UnsupportedOperationException();
}

}

// End SqlUpdatabilityClause.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public MysqlSqlDialect(Context context) {
final SqlNode operand = ((SqlBasicCall) aggCall).operand(0);
final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
final SqlNode unionOperand = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY,
SqlNodeList.of(nullLiteral), null, null, null, null, SqlNodeList.EMPTY, null, null, null);
SqlNodeList.of(nullLiteral), null, null, null, null, SqlNodeList.EMPTY, null,
null, null, null);
// For MySQL, generate
// CASE COUNT(*)
// WHEN 0 THEN NULL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ protected SqlNode performUnconditionalRewrites(
new SqlNodeList(SqlParserPos.ZERO);
selectList.add(SqlIdentifier.star(SqlParserPos.ZERO));
return new SqlSelect(node.getParserPosition(), null, selectList, node,
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null);
}

case ORDER_BY: {
Expand Down Expand Up @@ -1244,7 +1244,7 @@ protected SqlNode performUnconditionalRewrites(
}
return new SqlSelect(SqlParserPos.ZERO, null, selectList, orderBy.query,
null, null, null, null, orderList, orderBy.offset,
orderBy.fetch);
orderBy.fetch, null);
}

case EXPLICIT_TABLE: {
Expand All @@ -1253,7 +1253,7 @@ protected SqlNode performUnconditionalRewrites(
final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
selectList.add(SqlIdentifier.star(SqlParserPos.ZERO));
return new SqlSelect(SqlParserPos.ZERO, null, selectList, call.operand(0),
null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null);
}

case DELETE: {
Expand Down Expand Up @@ -1347,7 +1347,7 @@ private void rewriteMerge(SqlMerge call) {
call.getCondition());
SqlSelect select =
new SqlSelect(SqlParserPos.ZERO, null, selectList, outerJoin, null,
null, null, null, null, null, null);
null, null, null, null, null, null, null);
call.setSourceSelect(select);

// Source for the insert call is a select of the source table
Expand All @@ -1365,7 +1365,7 @@ private void rewriteMerge(SqlMerge call) {
final SqlNode insertSource = SqlNode.clone(sourceTableRef);
select =
new SqlSelect(SqlParserPos.ZERO, null, selectList, insertSource, null,
null, null, null, null, null, null);
null, null, null, null, null, null, null);
insertCall.setSource(select);
}
}
Expand Down Expand Up @@ -1430,7 +1430,7 @@ private SqlNode rewriteUpdateToMerge(
}
source =
new SqlSelect(SqlParserPos.ZERO, null, selectList, source, null, null,
null, null, null, null, null);
null, null, null, null, null, null);
source = SqlValidatorUtil.addAlias(source, UPDATE_SRC_ALIAS);
SqlMerge mergeCall =
new SqlMerge(updateCall.getParserPosition(), target, condition, source,
Expand Down Expand Up @@ -1485,7 +1485,7 @@ protected SqlSelect createSourceSelectForUpdate(SqlUpdate call) {
call.getAlias().getSimple());
}
return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable,
call.getCondition(), null, null, null, null, null, null);
call.getCondition(), null, null, null, null, null, null, null);
}

/**
Expand All @@ -1506,7 +1506,7 @@ protected SqlSelect createSourceSelectForDelete(SqlDelete call) {
call.getAlias().getSimple());
}
return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable,
call.getCondition(), null, null, null, null, null, null);
call.getCondition(), null, null, null, null, null, null, null);
}

/**
Expand Down
Loading