-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Fix on conflict
error and add support for other on conflict
queries in Postgres
#34227
base: master
Are you sure you want to change the base?
Changes from 7 commits
e0dda25
5ec751c
f170f5e
ce175f9
b455574
30283fc
008ba36
27b4042
ed4a82e
6a72dfa
c985ba1
8201efd
426e9bf
af0a37e
fee7feb
107044b
03a00c3
8d42636
6d16aff
d0c8879
8449442
cc421be
5e225c4
b51d95b
41c96cd
033c582
7c34c47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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.shardingsphere.infra.binder.context.segment.insert.values; | ||
|
||
import com.google.common.base.Preconditions; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.apache.shardingsphere.infra.binder.context.type.WhereAvailable; | ||
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor; | ||
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add javadoc for this class. |
||
@Getter | ||
@ToString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing this out. I added the annotation because, while it has not been explicitly used anywhere currently, I observed a similar annotation unused applied to the |
||
public final class OnConflictUpdateContext implements WhereAvailable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove WhereAvailable here, it's only for SQLStatement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for reviewing and pointing this out too. Adding it here was redundant and an oversight on my part as I missed removing it during cleanup. I will address it in the correction commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @strongduanmu |
||
|
||
private final int parameterCount; | ||
|
||
private final List<ExpressionSegment> valueExpressions; | ||
|
||
private final Collection<WhereSegment> whereSegments = new LinkedList<>(); | ||
|
||
private final Collection<ColumnSegment> columnSegments; | ||
|
||
private final Collection<BinaryOperationExpression> joinConditions = new LinkedList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a join condition in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, It's not part of a particular segment. join can be handled outside the conflict segment in Insert statement . I'll address this in a correction commit. |
||
|
||
private final List<ParameterMarkerExpressionSegment> parameterMarkerExpressions; | ||
|
||
private final List<Object> parameters; | ||
|
||
public OnConflictUpdateContext(final Collection<ColumnAssignmentSegment> assignments, final List<Object> params, final int parametersOffset, final Optional<WhereSegment> segment) { | ||
List<ExpressionSegment> expressionSegments = assignments.stream().map(ColumnAssignmentSegment::getValue).collect(Collectors.toList()); | ||
segment.ifPresent(whereSegments::add); | ||
for (WhereSegment whereSegment : whereSegments) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename whereSegment to each |
||
expressionSegments.add(whereSegment.getExpr()); | ||
} | ||
columnSegments = assignments.stream().map(each -> each.getColumns().get(0)).collect(Collectors.toList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why only call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Postgres ON CONFLICT clause only updates the specified conflicting column, and each assignment logically corresponds to a single column in this context. |
||
ColumnExtractor.extractColumnSegments(columnSegments, whereSegments); | ||
ExpressionExtractor.extractJoinConditions(joinConditions, whereSegments); | ||
valueExpressions = getValueExpressions(expressionSegments); | ||
parameterMarkerExpressions = ExpressionExtractor.getParameterMarkerExpressions(expressionSegments); | ||
parameterCount = parameterMarkerExpressions.size(); | ||
parameters = getParameters(params, parametersOffset); | ||
} | ||
|
||
private List<ExpressionSegment> getValueExpressions(final Collection<ExpressionSegment> assignments) { | ||
List<ExpressionSegment> result = new ArrayList<>(assignments.size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only for cast Collection to List? If so, please modify the origianl assignments to ArrayList. |
||
result.addAll(assignments); | ||
return result; | ||
} | ||
|
||
private List<Object> getParameters(final List<Object> params, final int paramsOffset) { | ||
if (params.isEmpty() || 0 == parameterCount) { | ||
return Collections.emptyList(); | ||
} | ||
List<Object> result = new ArrayList<>(parameterCount); | ||
result.addAll(params.subList(paramsOffset, paramsOffset + parameterCount)); | ||
return result; | ||
} | ||
|
||
/** | ||
* Get value. | ||
* | ||
* @param index index | ||
* @return value | ||
*/ | ||
public Object getValue(final int index) { | ||
ExpressionSegment valueExpression = valueExpressions.get(index); | ||
if (valueExpression instanceof ParameterMarkerExpressionSegment) { | ||
return parameters.get(getParameterIndex((ParameterMarkerExpressionSegment) valueExpression)); | ||
} | ||
if (valueExpression instanceof FunctionSegment) { | ||
return valueExpression; | ||
} | ||
return ((LiteralExpressionSegment) valueExpression).getLiterals(); | ||
} | ||
|
||
private int getParameterIndex(final ParameterMarkerExpressionSegment paramMarkerExpression) { | ||
int result = parameterMarkerExpressions.indexOf(paramMarkerExpression); | ||
Preconditions.checkArgument(result >= 0, "Can not get parameter index."); | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement; | ||
import org.apache.shardingsphere.sql.parser.statement.postgresql.dml.PostgreSQLInsertStatement; | ||
|
||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
@@ -63,6 +64,12 @@ private InsertStatement copy(final InsertStatement sqlStatement) { | |
result.getValues().addAll(sqlStatement.getValues()); | ||
sqlStatement.getSetAssignment().ifPresent(result::setSetAssignment); | ||
sqlStatement.getOnDuplicateKeyColumns().ifPresent(result::setOnDuplicateKeyColumns); | ||
sqlStatement.getOnConflictKeyColumns().ifPresent(segment -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not use PostgreSQLInsertStatement in SQLBinder. If you need call setOnConflictKeyColumnsSegment method, please add it to InsertStatement, and implement setOnConflictKeyColumnsSegment in PostgreSQLInsertStatement. |
||
if (result instanceof PostgreSQLInsertStatement) { | ||
((PostgreSQLInsertStatement) result).setOnConflictKeyColumnsSegment(segment); | ||
} | ||
}); | ||
sqlStatement.getWithSegment().ifPresent(result::setWithSegment); | ||
sqlStatement.getOutputSegment().ifPresent(result::setOutputSegment); | ||
sqlStatement.getMultiTableInsertType().ifPresent(result::setMultiTableInsertType); | ||
sqlStatement.getMultiTableInsertIntoSegment().ifPresent(result::setMultiTableInsertIntoSegment); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you modify this change log to
Fix on conflict error and add support for other on conflict queries in Postgres #34227
?