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 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
64 changes: 63 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.SqlUpdatability;
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 updatability;
}
{
<SELECT>
Expand Down Expand Up @@ -1047,19 +1049,21 @@ SqlSelect SqlSelect() :
groupBy = GroupByOpt()
having = HavingOpt()
windowDecls = WindowOpt()
updatability = UpdatabilityOpt()
|
E() {
fromClause = null;
where = null;
groupBy = null;
having = null;
windowDecls = null;
updatability = 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, updatability);
}
}

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

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

/**
* Parses a table reference in a FROM clause, not lateral unless LATERAL
* is explicitly specified.
Expand Down Expand Up @@ -4230,6 +4260,22 @@ void SimpleIdentifierCommaList(List<SqlNode> list) :
)*
}

/**
* Parses a comma-separated list of expressions.
*/
void SimpleExpressionCommaList(List<SqlNode> list, ExprContext exprContext) :
{
SqlNode expr;
}
{
expr = Expression(exprContext) {list.add(expr);}
(
<COMMA> expr = Expression(exprContext) {
list.add(expr);
}
)*
}

/**
* List of simple identifiers in parentheses. The position extends from the
* open parenthesis to the close parenthesis.
Expand All @@ -4247,6 +4293,22 @@ SqlNodeList ParenthesizedSimpleIdentifierList() :
}
}

/**
* List of expressions without parentheses.
*/
SqlNodeList NonParenthesizedSimpleExpressionList(ExprContext exprContext) :
{
final Span s;
final List<SqlNode> list = new ArrayList<SqlNode>();
}
{
{ s = span(); }
SimpleExpressionCommaList(list, exprContext)
{
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 @@ -58,7 +58,7 @@ public abstract class TableModify extends SingleRel {
* Enumeration of supported modification operations.
*/
public enum Operation {
INSERT, UPDATE, DELETE, MERGE
INSERT, UPDATE, DELETE, MERGE, LOCK
}

//~ Instance fields --------------------------------------------------------
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
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ public enum SqlKind {
* MATCH_RECOGNIZE clause
*/
MATCH_RECOGNIZE,

/**
* UPDATABILITY clause
*/
UPDATABILITY,

// binary operators

/**
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 updatability;
SqlMatchRecognize matchRecognize;

//~ Constructors -----------------------------------------------------------
Expand All @@ -62,7 +63,8 @@ public SqlSelect(SqlParserPos pos,
SqlNodeList windowDecls,
SqlNodeList orderBy,
SqlNode offset,
SqlNode fetch) {
SqlNode fetch,
SqlNode updatability) {
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.updatability = updatability;
}

//~ 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, updatability);
}

@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:
updatability = 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 getUpdatability() {
return updatability;
}

public void setUpdatability(SqlNode updatability) {
this.updatability = updatability;
}

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 updatability,
SqlParserPos pos) {
return new SqlSelect(
pos,
Expand All @@ -116,7 +118,8 @@ public SqlSelect createCall(
windowDecls,
orderBy,
offset,
fetch);
fetch,
updatability);
}

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.updatability != null) {
select.updatability.unparse(writer, leftPrec, rightPrec);
}
writer.endList(selectFrame);
}

Expand Down
77 changes: 77 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlUpdatability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.util.ImmutableNullableList;

import java.util.List;
import java.util.Objects;

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

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("UPDATABILITY", SqlKind.UPDATABILITY) {
@Override public SqlCall createCall(SqlLiteral functionQualifier,
SqlParserPos pos, SqlNode... operands) {
return new SqlUpdatability((SqlNodeList) operands[0], pos);
}
};

final SqlNodeList columnNames;

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

public SqlNodeList getColumnNames() {
return columnNames;
}

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

@Override public SqlOperator getOperator() {
return OPERATOR;
}

@Override public SqlKind getKind() {
return SqlKind.UPDATABILITY;
}

@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(columnNames);
}

}

// End SqlUpdatability.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
Loading