Skip to content
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
15 changes: 10 additions & 5 deletions modules/calcite/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ data: {
"REFRESH",
"ANALYZE",
"MAX_CHANGED_PARTITION_ROWS_PERCENT",
"TOTAL"
"TOTAL",
"WAIT",
"NOWAIT"
]

# List of non-reserved keywords to add;
Expand Down Expand Up @@ -110,6 +112,8 @@ data: {
"ANALYZE"
"MAX_CHANGED_PARTITION_ROWS_PERCENT"
"TOTAL"
"WAIT"
"NOWAIT"

# Keywords reserved by Calcite, but not required to be reserved in Ignite.
"ABS"
Expand Down Expand Up @@ -448,10 +452,11 @@ data: {
"SqlSavepoint()",
"SqlRollbackToSavepoint()",
"SqlCommitTransaction()",
"SqlRollbackTransaction()"
"SqlStatisticsAnalyze()"
"SqlStatisticsRefresh()"
"SqlStatisticsDrop()"
"SqlRollbackTransaction()",
"SqlStatisticsAnalyze()",
"SqlStatisticsRefresh()",
"SqlStatisticsDrop()",
"SqlSelectForUpdate()"
]

# List of methods for parsing extensions to "CREATE [OR REPLACE]" calls.
Expand Down
49 changes: 49 additions & 0 deletions modules/calcite/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,52 @@ SqlDrop SqlDropView(Span s, boolean replace) :
return SqlDdlNodes.dropView(s.end(this), ifExists, id);
}
}

/**
* Parses a query optionally followed by FOR UPDATE [OF col [, col ...]] [WAIT n | NOWAIT].
*
* When FOR UPDATE is absent the inner query node is returned unchanged, so this rule
* transparently handles all queries that reach the StatementParser.
*/
SqlNode SqlSelectForUpdate() :
{
final Span s;
SqlNode qry;
SqlNodeList ofList = null;
List<SqlNode> ofCols = null;
SqlIdentifier col;
Long waitSeconds = null;
}
{
qry = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY) { s = span(); }
[
LOOKAHEAD(<FOR> <UPDATE>)
<FOR> <UPDATE>
[
LOOKAHEAD(<OF>)
<OF>
{
ofCols = new ArrayList<SqlNode>();
}
col = CompoundIdentifier() { ofCols.add(col); }
(
<COMMA> col = CompoundIdentifier() { ofCols.add(col); }
)*
{ ofList = new SqlNodeList(ofCols, s.pos()); }
]
[
LOOKAHEAD(<WAIT>)
<WAIT> <UNSIGNED_INTEGER_LITERAL>
{
waitSeconds = Long.parseLong(token.image);
}
|
<NOWAIT>
{
waitSeconds = 0L;
}
]
{ return new IgniteSqlSelectForUpdate(s.end(this), qry, ofList, waitSeconds); }
]
{ return qry; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
import org.apache.ignite.internal.processors.query.calcite.DistributedCalciteConfiguration;
import org.apache.ignite.internal.processors.query.calcite.prepare.ddl.DdlSqlToCommandConverter;
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
import org.apache.ignite.internal.processors.query.calcite.sql.IgniteSqlSelectForUpdate;
import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
import org.apache.ignite.internal.processors.query.calcite.util.AbstractService;
import org.apache.ignite.internal.processors.query.calcite.util.IgniteResource;
import org.apache.ignite.internal.processors.query.calcite.util.TypeUtils;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.T2;
Expand Down Expand Up @@ -93,6 +95,10 @@ public PrepareServiceImpl(GridKernalContext ctx) {
if (!F.isEmpty(disbledRules))
ctx.addRulesFilter(new IgnitePlanner.DisabledRuleFilter(disbledRules));

if (sqlNode instanceof IgniteSqlSelectForUpdate)
throw new IgniteSQLException(IgniteResource.INSTANCE.selectForUpdateNotSupported().str(),
IgniteQueryErrorCode.UNSUPPORTED_OPERATION);

if (SqlKind.DDL.contains(sqlNode.getKind()))
return prepareDdl(sqlNode, ctx);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.ignite.internal.processors.query.calcite.sql;

import java.util.List;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;
import org.jetbrains.annotations.Nullable;

/**
* Parse tree for {@code SELECT ... FOR UPDATE [OF col [, col ...]] [WAIT n | NOWAIT]} statement.
*
* <p>The {@code FOR UPDATE} clause requests pessimistic row-level locks on the rows returned
* by the query. Actual lock acquisition is not yet supported; this node exists to reserve the
* syntax and produce a clear "not yet supported" error at query-preparation time.
*
* <p>Fields:
* <ul>
* <li>{@code query} — the underlying SELECT/WITH/VALUES query</li>
* <li>{@code ofList} — optional list of column references identifying which tables to lock;
* {@code null} means all tables referenced in FROM</li>
* <li>{@code waitSeconds} — {@code null} = use transaction timeout,
* {@code 0} = NOWAIT, positive = WAIT n seconds</li>
* </ul>
*/
public class IgniteSqlSelectForUpdate extends SqlCall {
/** */
private static final SqlOperator OPERATOR =
new SqlSpecialOperator("SELECT FOR UPDATE", SqlKind.OTHER);

/** The wrapped SELECT / WITH / VALUES query. */
private final SqlNode query;

/**
* Columns from the {@code OF} list, or {@code null} when not specified (lock all tables).
* Each element is a {@link org.apache.calcite.sql.SqlIdentifier} referencing a table column.
*/
@Nullable private final SqlNodeList ofList;

/**
* Lock-wait limit: {@code null} = use transaction timeout, {@code 0} = NOWAIT,
* positive = WAIT n seconds.
*/
@Nullable private final Long waitSeconds;

/** */
public IgniteSqlSelectForUpdate(
SqlParserPos pos,
SqlNode query,
@Nullable SqlNodeList ofList,
@Nullable Long waitSeconds
) {
super(pos);

this.query = query;
this.ofList = ofList;
this.waitSeconds = waitSeconds;
}

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

/** {@inheritDoc} */
@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(query, ofList);
}

/** Returns the wrapped query (SELECT / WITH / VALUES). */
public SqlNode query() {
return query;
}

/**
* Returns the {@code OF} column list, or {@code null} when not specified (lock all tables).
*/
@Nullable public SqlNodeList ofList() {
return ofList;
}

/**
* Returns the wait limit: {@code null} = transaction timeout, {@code 0} = NOWAIT,
* positive = WAIT n seconds.
*/
@Nullable public Long waitSeconds() {
return waitSeconds;
}

/** {@inheritDoc} */
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
query.unparse(writer, leftPrec, rightPrec);
writer.keyword("FOR");
writer.keyword("UPDATE");

if (ofList != null) {
writer.keyword("OF");
ofList.unparse(writer, 0, 0);
}

if (waitSeconds != null) {
if (waitSeconds == 0L)
writer.keyword("NOWAIT");
else {
writer.keyword("WAIT");
writer.print(String.valueOf(waitSeconds));
}
}
}
}
Loading
Loading