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

Support SQL non-parsing, transparent transmission mode #25180

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c5779ee
Support netty parameter ChannelOption.SO_BACKLOG configurable (#17812)
galaxylqx May 23, 2022
ef78ed3
Merge branch 'apache:master' into master
galaxylqx May 23, 2022
3cc3842
Merge branch 'apache:master' into master
galaxylqx May 23, 2022
cb215e7
Merge branch 'apache:master' into master
galaxylqx May 24, 2022
29dbd20
Merge branch 'apache:master' into master
galaxylqx May 26, 2022
41da76a
Merge branch 'apache:master' into master
galaxylqx May 27, 2022
c5055ac
Merge branch 'apache:master' into master
galaxylqx May 28, 2022
51629b7
Merge branch 'apache:master' into master
galaxylqx May 29, 2022
2a99db7
Merge branch 'apache:master' into master
galaxylqx May 31, 2022
bf7a883
Merge branch 'apache:master' into master
galaxylqx Jun 1, 2022
ae27e33
Merge branch 'apache:master' into master
galaxylqx Jun 2, 2022
c56702b
Merge branch 'apache:master' into master
galaxylqx Jun 5, 2022
dee7a24
Merge branch 'apache:master' into master
galaxylqx Jun 9, 2022
959a895
Merge branch 'apache:master' into master
galaxylqx Jun 14, 2022
d27cba6
Merge branch 'apache:master' into master
galaxylqx Jun 16, 2022
4012601
Merge branch 'apache:master' into master
galaxylqx Jul 6, 2022
4de4204
Merge branch 'apache:master' into master
galaxylqx Jul 13, 2022
1683f33
Merge branch 'apache:master' into master
galaxylqx Jul 13, 2022
81c3d13
Merge branch 'apache:master' into master
galaxylqx Jul 16, 2022
3aee5c0
Merge branch 'apache:master' into master
galaxylqx Jul 28, 2022
e19e9c8
Merge branch 'apache:master' into master
galaxylqx Nov 2, 2022
f0c03b5
Merge branch 'apache:master' into master
galaxylqx Nov 4, 2022
8f52964
Merge branch 'apache:master' into master
galaxylqx Nov 11, 2022
a9e0485
Merge branch 'apache:master' into master
galaxylqx Nov 15, 2022
c4abb9e
Merge branch 'apache:master' into master
galaxylqx Nov 21, 2022
8034f27
Merge branch 'apache:master' into master
galaxylqx Dec 5, 2022
b6fbfbe
Merge branch 'apache:master' into master
galaxylqx Feb 24, 2023
d4ead85
Merge branch 'apache:master' into master
galaxylqx Feb 27, 2023
48ed778
Merge branch 'apache:master' into master
galaxylqx Feb 28, 2023
0a15200
Merge branch 'apache:master' into master
galaxylqx Mar 1, 2023
06af4ae
Merge branch 'apache:master' into master
galaxylqx Mar 1, 2023
6dce8d6
Merge branch 'apache:master' into master
galaxylqx Mar 1, 2023
a76ec92
Merge branch 'apache:master' into master
galaxylqx Mar 1, 2023
af16526
Merge branch 'apache:master' into master
galaxylqx Mar 3, 2023
08f7e95
Merge branch 'apache:master' into master
galaxylqx Mar 6, 2023
9a74856
Merge branch 'apache:master' into master
galaxylqx Mar 6, 2023
a70689b
Merge branch 'apache:master' into master
galaxylqx Mar 10, 2023
f214f5f
Merge branch 'apache:master' into master
galaxylqx Mar 13, 2023
6ded5b1
Merge branch 'apache:master' into master
galaxylqx Mar 14, 2023
d34f8fb
Merge branch 'apache:master' into master
galaxylqx Mar 14, 2023
1b79231
Merge branch 'apache:master' into master
galaxylqx Mar 15, 2023
50bf89c
Merge branch 'apache:master' into master
galaxylqx Mar 20, 2023
1b5f6b4
Merge branch 'apache:master' into master
galaxylqx Mar 21, 2023
000f341
Merge branch 'apache:master' into master
galaxylqx Mar 27, 2023
d115cd8
Merge branch 'apache:master' into master
galaxylqx Mar 27, 2023
d1eb97d
Merge branch 'apache:master' into master
galaxylqx Apr 14, 2023
69fbdb6
Merge branch 'apache:master' into master
galaxylqx Apr 15, 2023
b12afb3
Support SQL non-parsing, transparent transmission mode
galaxylqx Apr 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,35 @@

package org.apache.shardingsphere.infra.parser;

import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngineFactory;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SimpleSQLStatement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Simple SQL parser engine.
*/
public final class SimpleSQLParserEngine implements SQLParserEngine {

private final SQLStatementParserEngine sqlStatementParserEngine;

public SimpleSQLParserEngine(final String databaseType, final CacheOption sqlStatementCacheOption, final CacheOption parseTreeCacheOption, final boolean isParseComment) {
sqlStatementParserEngine = SQLStatementParserEngineFactory.getSQLStatementParserEngine(
databaseType, sqlStatementCacheOption, parseTreeCacheOption, isParseComment);
}

private boolean isContainShowDatabases(final String sql) {
String regex = "^(\\s*)show(\\s+)databases(\\s*)";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sql);
return matcher.matches();
}

@Override
public SQLStatement parse(final String sql, final boolean useCache) {
return new SimpleSQLStatement();
return isContainShowDatabases(sql) ? sqlStatementParserEngine.parse(sql, useCache) : new SimpleSQLStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void assertRemoveGlobalRuleConfiguration() throws Exception {
Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
when(connection.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
CacheOption cacheOption = new CacheOption(1024, 1024);
SQLParserRuleConfiguration sqlParserRuleConfig = new SQLParserRuleConfiguration(true, cacheOption, cacheOption);
SQLParserRuleConfiguration sqlParserRuleConfig = new SQLParserRuleConfiguration(true, cacheOption, cacheOption, "Standard");
try (
ShardingSphereDataSource actual = new ShardingSphereDataSource(DefaultDatabase.LOGIC_NAME,
null, Collections.singletonMap("ds", new MockedDataSource(connection)), Arrays.asList(mock(ShardingRuleConfiguration.class), sqlParserRuleConfig), new Properties())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public final class SQLParserRuleConfiguration implements GlobalRuleConfiguration
private final CacheOption parseTreeCache;

private final CacheOption sqlStatementCache;

private final String engineType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add default value for engineType. It is better to add a compatible constructor.

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public SQLParserRule(final SQLParserRuleConfiguration ruleConfig) {
sqlCommentParseEnabled = ruleConfig.isSqlCommentParseEnabled();
sqlStatementCache = ruleConfig.getSqlStatementCache();
parseTreeCache = ruleConfig.getParseTreeCache();
engineType = "Standard";
engineType = ruleConfig.getEngineType();
}

/**
Expand All @@ -56,9 +56,9 @@ public SQLParserRule(final SQLParserRuleConfiguration ruleConfig) {
* @return SQL parser engine
*/
public SQLParserEngine getSQLParserEngine(final String databaseType) {
return "Standard".equals(engineType)
? new ShardingSphereSQLParserEngine(databaseType, sqlStatementCache, parseTreeCache, sqlCommentParseEnabled)
: new SimpleSQLParserEngine();
return "Simple".equals(engineType)
? new SimpleSQLParserEngine(databaseType, sqlStatementCache, parseTreeCache, sqlCommentParseEnabled)
: new ShardingSphereSQLParserEngine(databaseType, sqlStatementCache, parseTreeCache, sqlCommentParseEnabled);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class DefaultSQLParserRuleConfigurationBuilder implements DefaultGl

@Override
public SQLParserRuleConfiguration build() {
return new SQLParserRuleConfiguration(false, PARSE_TREE_CACHE_OPTION, SQL_STATEMENT_CACHE_OPTION);
return new SQLParserRuleConfiguration(false, PARSE_TREE_CACHE_OPTION, SQL_STATEMENT_CACHE_OPTION, "Standard");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public final class YamlSQLParserRuleConfiguration implements YamlGlobalRuleConfi

private YamlSQLParserCacheOptionRuleConfiguration parseTreeCache;

private String engineType;

@Override
public Class<SQLParserRuleConfiguration> getRuleConfigurationType() {
return SQLParserRuleConfiguration.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public YamlSQLParserRuleConfiguration swapToYamlConfiguration(final SQLParserRul
result.setSqlCommentParseEnabled(data.isSqlCommentParseEnabled());
result.setParseTreeCache(cacheOptionSwapper.swapToYamlConfiguration(data.getParseTreeCache()));
result.setSqlStatementCache(cacheOptionSwapper.swapToYamlConfiguration(data.getSqlStatementCache()));
result.setEngineType(data.getEngineType());
return result;
}

Expand All @@ -48,7 +49,7 @@ public SQLParserRuleConfiguration swapToObject(final YamlSQLParserRuleConfigurat
CacheOption sqlStatementCacheOption = null == yamlConfig.getSqlStatementCache()
? DefaultSQLParserRuleConfigurationBuilder.SQL_STATEMENT_CACHE_OPTION
: cacheOptionSwapper.swapToObject(yamlConfig.getSqlStatementCache());
return new SQLParserRuleConfiguration(yamlConfig.isSqlCommentParseEnabled(), parseTreeCacheOption, sqlStatementCacheOption);
return new SQLParserRuleConfiguration(yamlConfig.isSqlCommentParseEnabled(), parseTreeCacheOption, sqlStatementCacheOption, yamlConfig.getEngineType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SQLParserRuleTest {

@BeforeEach
void setup() {
sqlParserRule = new SQLParserRule(new SQLParserRuleConfiguration(true, new CacheOption(2, 4), new CacheOption(3, 7)));
sqlParserRule = new SQLParserRule(new SQLParserRuleConfiguration(true, new CacheOption(2, 4), new CacheOption(3, 7), "Standard"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class SQLParserRuleBuilderTest {

@Test
void assertBuild() {
SQLParserRuleConfiguration ruleConfig = new SQLParserRuleConfiguration(true, new CacheOption(2, 5), new CacheOption(4, 7));
SQLParserRuleConfiguration ruleConfig = new SQLParserRuleConfiguration(true, new CacheOption(2, 5),
new CacheOption(4, 7), "Standard");
SQLParserRule actualResult = new SQLParserRuleBuilder().build(ruleConfig, new HashMap<>(), new ConfigurationProperties(new Properties()));
assertThat(actualResult.getConfiguration(), is(ruleConfig));
assertTrue(actualResult.isSqlCommentParseEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class YamlSQLParserRuleConfigurationSwapperTest {
@Test
void assertSwapToYamlConfiguration() {
YamlSQLParserRuleConfiguration actual =
new YamlSQLParserRuleConfigurationSwapper().swapToYamlConfiguration(new SQLParserRuleConfiguration(true, new CacheOption(2, 5), new CacheOption(4, 7)));
new YamlSQLParserRuleConfigurationSwapper().swapToYamlConfiguration(new SQLParserRuleConfiguration(true, new CacheOption(2, 5), new CacheOption(4, 7), "Standard"));
assertTrue(actual.isSqlCommentParseEnabled());
assertThat(actual.getParseTreeCache().getInitialCapacity(), is(2));
assertThat(actual.getParseTreeCache().getMaximumSize(), is(5L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SQLParserRuleConfiguration buildAlteredRuleConfiguration(final SQLParserR
CacheOption sqlStatementCache =
null == sqlStatement.getSqlStatementCache() ? currentRuleConfig.getSqlStatementCache()
: createCacheOption(currentRuleConfig.getSqlStatementCache(), sqlStatement.getSqlStatementCache());
return new SQLParserRuleConfiguration(sqlCommentParseEnabled, parseTreeCache, sqlStatementCache);
return new SQLParserRuleConfiguration(sqlCommentParseEnabled, parseTreeCache, sqlStatementCache, "Standard");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add // TODO with reason to reminder it fix in future

}

private CacheOption createCacheOption(final CacheOption cacheOption, final CacheOptionSegment segment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void assertGetColumnNames() {

private ShardingSphereMetaData mockMetaData() {
SQLParserRule sqlParserRule = mock(SQLParserRule.class);
when(sqlParserRule.getConfiguration()).thenReturn(new SQLParserRuleConfiguration(true, new CacheOption(128, 1024), new CacheOption(2000, 65535)));
when(sqlParserRule.getConfiguration()).thenReturn(new SQLParserRuleConfiguration(true, new CacheOption(128, 1024), new CacheOption(2000, 65535), "Standard"));
return new ShardingSphereMetaData(new LinkedHashMap<>(), new ShardingSphereRuleMetaData(Collections.singleton(sqlParserRule)), new ConfigurationProperties(new Properties()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void init() {

private ShardingSphereRuleMetaData createGlobalRuleMetaData() {
CacheOption cacheOption = new CacheOption(128, 1024L);
return new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption))));
return new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption, "Standard"))));
}

private ShardingSphereResourceMetaData mockResourceMetaData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ private ShardingSphereDatabase createShardingSphereDatabase() {

private ShardingSphereRuleMetaData createShardingSphereRuleMetaData() {
CacheOption cacheOption = new CacheOption(10, 1000);
return new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(true, cacheOption, cacheOption))));
return new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(true, cacheOption, cacheOption, "Standard"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(mock(ShardingSphereDatabase.class));
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData())
.thenReturn(new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(false, new CacheOption(1, 1), new CacheOption(1, 1))))));
.thenReturn(new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(false, new CacheOption(1, 1), new CacheOption(1, 1), "Standard")))));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void assertCreateWithSelectPgNamespaceAndPgClass() {

private static SQLStatement parseSQL(final String sql) {
CacheOption cacheOption = new CacheOption(0, 0);
SQLParserRule sqlParserRule = new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption));
SQLParserRule sqlParserRule = new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption, "Standard"));
return sqlParserRule.getSQLParserEngine("PostgreSQL").parse(sql, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private ContextManager mockContextManager() {
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(mock(ShardingSphereRuleMetaData.class));
CacheOption cacheOption = new CacheOption(1024, 1024);
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class))
.thenReturn(new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption)));
.thenReturn(new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption, "Standard")));
when(result.getMetaDataContexts().getMetaData().getDatabase(connectionSession.getDatabaseName()).getProtocolType()).thenReturn(new MySQLDatabaseType());
ShardingSphereTable table = new ShardingSphereTable();
table.getColumns().put("id", new ShardingSphereColumn("id", Types.BIGINT, true, false, false, false, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package org.apache.shardingsphere.sql.parser.sql.common.statement;

import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;

/**
* Simple SQL statement.
*/
public final class SimpleSQLStatement implements SQLStatement {
public final class SimpleSQLStatement implements MySQLStatement {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rename it as MySQLSimpleSQLStatement and create real SimpleSQLStatement?


@Override
public int getParameterCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
public abstract class SQLRewriterIT {

private final SQLParserRule sqlParserRule = new SQLParserRule(new SQLParserRuleConfiguration(true,
DefaultSQLParserRuleConfigurationBuilder.PARSE_TREE_CACHE_OPTION, DefaultSQLParserRuleConfigurationBuilder.SQL_STATEMENT_CACHE_OPTION));
DefaultSQLParserRuleConfigurationBuilder.PARSE_TREE_CACHE_OPTION, DefaultSQLParserRuleConfigurationBuilder.SQL_STATEMENT_CACHE_OPTION, "Standard"));

private final TimeServiceRule timeServiceRule = new TimeServiceRule(new TimeServiceRuleConfiguration("System", new Properties()));

Expand Down