-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Issue 31437 with keyword query error mysql #34163
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
Changes from 15 commits
8544d67
1653664
2029eaf
068962b
f973677
027ea9f
6c8660a
88a1300
e2e18ea
ab34b8e
6a2640c
2b0c960
52f639f
949ff51
a27ede2
893cea5
7fbb92f
5023a65
c340ae3
791c956
7a9cd89
ff11f5e
aa99360
03cc31d
01f9e2f
30e3647
0e7a6f1
d32b7d4
67df1d8
56147e3
58d2c21
ce53b70
c77d035
30e9acd
d9ea315
b9131f7
5acbb84
761355a
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 |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ | |
| import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext; | ||
| import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; | ||
| import org.apache.shardingsphere.infra.binder.engine.statement.dml.SelectStatementBinder; | ||
| import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; | ||
| import org.apache.shardingsphere.infra.exception.kernel.syntax.UniqueCommonTableExpressionException; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement; | ||
|
|
||
|
|
@@ -46,7 +48,13 @@ public static SubquerySegment bind(final SubquerySegment segment, final SQLState | |
| SQLStatementBinderContext selectBinderContext = | ||
| new SQLStatementBinderContext(binderContext.getMetaData(), binderContext.getCurrentDatabaseName(), binderContext.getHintValueContext(), segment.getSelect()); | ||
| selectBinderContext.getExternalTableBinderContexts().putAll(binderContext.getExternalTableBinderContexts()); | ||
| selectBinderContext.getCommonTableExpressionsSegmentsUniqueAliases().addAll(binderContext.getCommonTableExpressionsSegmentsUniqueAliases()); | ||
| SelectStatement boundSelectStatement = new SelectStatementBinder(outerTableBinderContexts).bind(segment.getSelect(), selectBinderContext); | ||
| return new SubquerySegment(segment.getStartIndex(), segment.getStopIndex(), boundSelectStatement, segment.getText()); | ||
| SubquerySegment result = new SubquerySegment(segment.getStartIndex(), segment.getStopIndex(), boundSelectStatement, segment.getText()); | ||
| selectBinderContext.getCommonTableExpressionsSegmentsUniqueAliases().forEach(each -> { | ||
| ShardingSpherePreconditions.checkNotContains(binderContext.getCommonTableExpressionsSegmentsUniqueAliases(), each, () -> new UniqueCommonTableExpressionException(each.toString())); | ||
|
||
| binderContext.getCommonTableExpressionsSegmentsUniqueAliases().add(each); | ||
| }); | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,9 @@ | |
| import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * SQL statement binder context. | ||
|
|
@@ -47,6 +49,8 @@ public final class SQLStatementBinderContext { | |
|
|
||
| private final SQLStatement sqlStatement; | ||
|
|
||
| private final Set<CaseInsensitiveString> commonTableExpressionsSegmentsUniqueAliases = new HashSet<>(); | ||
|
||
|
|
||
| private final Collection<String> usingColumnNames = new CaseInsensitiveSet<>(); | ||
|
|
||
| private final Collection<ProjectionSegment> joinTableProjectionSegments = new LinkedList<>(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * 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.with; | ||
|
|
||
| import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString; | ||
| import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.type.SimpleTableSegmentBinderContext; | ||
| import org.apache.shardingsphere.infra.binder.engine.segment.dml.with.WithSegmentBinder; | ||
| import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; | ||
| import org.apache.shardingsphere.infra.hint.HintValueContext; | ||
| import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; | ||
| import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; | ||
| import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.complex.CommonTableExpressionSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ShorthandProjectionSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment; | ||
| import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; | ||
| import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.sql.Types; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.LinkedList; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
|
|
||
| public class WithSegmentBinderTest { | ||
Yash-cor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| void assertBind() { | ||
|
|
||
| MySQLSelectStatement mySQLSelectStatement = new MySQLSelectStatement(); | ||
| ProjectionsSegment projectionSegment = new ProjectionsSegment(42, 48); | ||
| ColumnSegment columnSegment = new ColumnSegment(42, 48, new IdentifierValue("user_id")); | ||
| projectionSegment.getProjections().add(new ColumnProjectionSegment(columnSegment)); | ||
| mySQLSelectStatement.setProjections(projectionSegment); | ||
| mySQLSelectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(55, 57, new IdentifierValue("cte")))); | ||
|
|
||
| MySQLSelectStatement subqueryMySQLSelectStatement = new MySQLSelectStatement(); | ||
| ProjectionsSegment subqueryProjectionSegment = new ProjectionsSegment(20, 20); | ||
| ShorthandProjectionSegment shorthandProjectionSegment = new ShorthandProjectionSegment(20, 20); | ||
| subqueryProjectionSegment.getProjections().add(shorthandProjectionSegment); | ||
| subqueryMySQLSelectStatement.setProjections(subqueryProjectionSegment); | ||
| subqueryMySQLSelectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(27, 32, new IdentifierValue("t_user")))); | ||
| SubquerySegment subquerySegment = new SubquerySegment(5, 33, subqueryMySQLSelectStatement, "(SELECT * FROM t_user)"); | ||
| Collection<CommonTableExpressionSegment> commonTableExpressionSegments = new LinkedList<>(); | ||
| commonTableExpressionSegments.add(new CommonTableExpressionSegment(5, 33, new AliasSegment(5, 7, new IdentifierValue("cte")), subquerySegment)); | ||
| WithSegment withSegment = new WithSegment(0, 33, commonTableExpressionSegments); | ||
| mySQLSelectStatement.setWithSegment(withSegment); | ||
|
|
||
| SQLStatementBinderContext binderContext = new SQLStatementBinderContext(createMetaData(), "foo_db", new HintValueContext(), mySQLSelectStatement); | ||
| WithSegment actual = WithSegmentBinder.bind(withSegment, binderContext, binderContext.getExternalTableBinderContexts()); | ||
|
|
||
| assertThat(binderContext.getExternalTableBinderContexts().size(), is(1)); | ||
| assertThat(binderContext.getCommonTableExpressionsSegmentsUniqueAliases().size(), is(1)); | ||
| assertThat(actual.getStartIndex(), is(0)); | ||
| assertTrue(binderContext.getExternalTableBinderContexts().containsKey(new CaseInsensitiveString("cte"))); | ||
| assertTrue(actual.getCommonTableExpressions().iterator().next().getAliasName().isPresent()); | ||
| assertThat(actual.getCommonTableExpressions().iterator().next().getAliasName().get(), is("cte")); | ||
| assertThat(binderContext.getCommonTableExpressionsSegmentsUniqueAliases().iterator().next(), is(new CaseInsensitiveString("cte"))); | ||
|
|
||
| SimpleTableSegmentBinderContext simpleTableSegment = (SimpleTableSegmentBinderContext) binderContext.getExternalTableBinderContexts().get(new CaseInsensitiveString("cte")).iterator().next(); | ||
| ArrayList<ColumnProjectionSegment> columnProjectionSegments = new ArrayList<>(); | ||
| simpleTableSegment.getProjectionSegments().forEach(each -> columnProjectionSegments.add((ColumnProjectionSegment) each)); | ||
|
|
||
| assertThat(columnProjectionSegments.get(0).getColumn().getIdentifier().getValue(), is("user_id")); | ||
| assertThat(columnProjectionSegments.get(1).getColumn().getIdentifier().getValue(), is("name")); | ||
| assertTrue(columnProjectionSegments.get(1).getColumn().getOwner().isPresent()); | ||
| assertThat(columnProjectionSegments.get(1).getColumn().getOwner().get().getIdentifier().getValue(), is("t_user")); | ||
| assertTrue(columnProjectionSegments.get(0).getColumn().getOwner().isPresent()); | ||
| assertThat(columnProjectionSegments.get(0).getColumn().getOwner().get().getIdentifier().getValue(), is("t_user")); | ||
| assertThat(columnProjectionSegments.get(0).getColumn().getIdentifier().getValue(), is("user_id")); | ||
| assertThat(columnProjectionSegments.get(1).getColumn().getColumnBoundInfo().getOriginalSchema().getValue(), is("foo_db")); | ||
| assertThat(columnProjectionSegments.get(0).getColumn().getColumnBoundInfo().getOriginalSchema().getValue(), is("foo_db")); | ||
| assertThat(actual.getCommonTableExpressions().iterator().next().getSubquery().getText(), is("(SELECT * FROM t_user)")); | ||
| } | ||
|
|
||
| private ShardingSphereMetaData createMetaData() { | ||
| ShardingSphereSchema schema = mock(ShardingSphereSchema.class, RETURNS_DEEP_STUBS); | ||
| when(schema.getTable("t_user").getAllColumns()).thenReturn(Arrays.asList( | ||
| new ShardingSphereColumn("user_id", Types.INTEGER, true, false, false, true, false, false), | ||
| new ShardingSphereColumn("name", Types.VARCHAR, false, false, false, true, false, false))); | ||
|
|
||
| ShardingSphereMetaData result = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS); | ||
| when(result.getDatabase("foo_db").getSchema("foo_db")).thenReturn(schema); | ||
| when(result.containsDatabase("foo_db")).thenReturn(true); | ||
| when(result.getDatabase("foo_db").containsSchema("foo_db")).thenReturn(true); | ||
| when(result.getDatabase("foo_db").getSchema("foo_db").containsTable("t_user")).thenReturn(true); | ||
| return result; | ||
| } | ||
|
|
||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.