forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue apache#32280/ bug fix: WHERE segment with ON CONFLICT segment i…
…n INSERT throws exception (#2) * add support for WHERE segment with ON CONFLICT segment in INSERT statement of postgres * updated RELEASE-NOTES.md * remove redundant commented code
- Loading branch information
1 parent
27b4042
commit ed4a82e
Showing
13 changed files
with
471 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...he/shardingsphere/infra/binder/context/segment/insert/values/OnConflictUpdateContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
@Getter | ||
@ToString | ||
public final class OnConflictUpdateContext implements WhereAvailable { | ||
|
||
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<>(); | ||
|
||
private final List<ParameterMarkerExpressionSegment> parameterMarkerExpressions; | ||
|
||
private final List<Object> parameters; | ||
|
||
public OnConflictUpdateContext(final Collection<ColumnAssignmentSegment> assignments, final List<Object> params, final int parametersOffset, Optional<WhereSegment> segment) { | ||
List<ExpressionSegment> expressionSegments = assignments.stream().map(ColumnAssignmentSegment::getValue).collect(Collectors.toList()); | ||
segment.ifPresent(whereSegments::add); | ||
for (WhereSegment whereSegment : whereSegments) { | ||
expressionSegments.add(whereSegment.getExpr()); | ||
} | ||
columnSegments = assignments.stream().map(each -> each.getColumns().get(0)).collect(Collectors.toList()); | ||
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()); | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.