Skip to content

Commit

Permalink
Add DistSQL ALTER SQL_TRANSLATOR RULE
Browse files Browse the repository at this point in the history
  • Loading branch information
Pace2Car committed Nov 21, 2023
1 parent e99e714 commit 81a5a48
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.apache.shardingsphere.infra.util.json.JsonUtils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

/**
* Properties converter.
Expand All @@ -38,8 +38,7 @@ public final class PropertiesConverter {
* @return converted string content
*/
public static String convert(final Properties props) {
Map<Object, Object> sortedProps = new LinkedHashMap<>();
props.keySet().stream().map(Object::toString).sorted().forEach(each -> sortedProps.put(each, props.get(each)));
return sortedProps.isEmpty() ? "" : JsonUtils.toJsonString(sortedProps);
return null == props || props.isEmpty() ? ""
: JsonUtils.toJsonString(props.keySet().stream().map(Object::toString).sorted().collect(Collectors.toMap(each -> each, props::get, (key1, key2) -> key1, LinkedHashMap::new)));
}
}
7 changes: 7 additions & 0 deletions kernel/sql-translator/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@
<artifactId>shardingsphere-sql-translator-distsql-parser</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqltranslator.distsql.handler;
package org.apache.shardingsphere.sqltranslator.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.ral.query.MetaDataRequiredQueryableRALExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.distsql.statement.ShowSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.distsql.statement.queryable.ShowSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;

import java.util.Arrays;
Expand All @@ -40,12 +41,13 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereMetaData
}

private Collection<LocalDataQueryResultRow> buildData(final SQLTranslatorRuleConfiguration ruleConfig) {
return Collections.singleton(new LocalDataQueryResultRow(null != ruleConfig.getType() ? ruleConfig.getType() : "", String.valueOf(ruleConfig.isUseOriginalSQLWhenTranslatingFailed())));
return Collections.singleton(new LocalDataQueryResultRow(null == ruleConfig.getType() ? "" : ruleConfig.getType(),
PropertiesConverter.convert(ruleConfig.getProps()), String.valueOf(ruleConfig.isUseOriginalSQLWhenTranslatingFailed())));
}

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("type", "use_original_sql_when_translating_failed");
return Arrays.asList("type", "props", "use_original_sql_when_translating_failed");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.sqltranslator.distsql.handler.update;

import org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.distsql.statement.updateable.AlterSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;

/**
* Alter SQL translator rule statement handler.
*/
public final class AlterSQLTranslatorRuleStatementUpdater implements GlobalRuleRALUpdater<AlterSQLTranslatorRuleStatement, SQLTranslatorRuleConfiguration> {

@Override
public void checkSQLStatement(final SQLTranslatorRuleConfiguration currentRuleConfig, final AlterSQLTranslatorRuleStatement sqlStatement) {
checkProvider(sqlStatement);
}

private void checkProvider(final AlterSQLTranslatorRuleStatement sqlStatement) {
TypedSPILoader.checkService(SQLTranslator.class, sqlStatement.getType(), sqlStatement.getProps());
}

@Override
public SQLTranslatorRuleConfiguration buildAlteredRuleConfiguration(final SQLTranslatorRuleConfiguration currentRuleConfig, final AlterSQLTranslatorRuleStatement sqlStatement) {
return new SQLTranslatorRuleConfiguration(sqlStatement.getType(), sqlStatement.getProps(),
null == sqlStatement.getUseOriginalSQLWhenTranslatingFailed() ? currentRuleConfig.isUseOriginalSQLWhenTranslatingFailed() : sqlStatement.getUseOriginalSQLWhenTranslatingFailed());
}

@Override
public Class<SQLTranslatorRuleConfiguration> getRuleConfigurationClass() {
return SQLTranslatorRuleConfiguration.class;
}

@Override
public Class<AlterSQLTranslatorRuleStatement> getType() {
return AlterSQLTranslatorRuleStatement.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

org.apache.shardingsphere.sqltranslator.distsql.handler.ShowSQLTranslatorRuleExecutor
org.apache.shardingsphere.sqltranslator.distsql.handler.query.ShowSQLTranslatorRuleExecutor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.sqltranslator.distsql.handler.update.AlterSQLTranslatorRuleStatementUpdater
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sqltranslator.distsql.handler;
package org.apache.shardingsphere.sqltranslator.distsql.handler.query;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.distsql.statement.ShowSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.distsql.statement.queryable.ShowSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.sqltranslator.distsql.handler.update;

import org.apache.shardingsphere.infra.props.PropertiesConverter;
import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.distsql.statement.updateable.AlterSQLTranslatorRuleStatement;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AlterSQLTranslatorRuleStatementUpdaterTest {

@Test
void assertExecute() {
AlterSQLTranslatorRuleStatementUpdater updater = new AlterSQLTranslatorRuleStatementUpdater();
SQLTranslatorRuleConfiguration actual = updater.buildAlteredRuleConfiguration(createSQLTranslatorRuleConfiguration(), new AlterSQLTranslatorRuleStatement("JOOQ",
PropertiesBuilder.build(new Property("foo", "fooValue")), null));
assertThat(actual.getType(), is("JOOQ"));
assertTrue(actual.isUseOriginalSQLWhenTranslatingFailed());
assertFalse(actual.getProps().isEmpty());
String props = PropertiesConverter.convert(actual.getProps());
assertTrue(props.contains("\"foo\":\"fooValue\""));
}

private SQLTranslatorRuleConfiguration createSQLTranslatorRuleConfiguration() {
return new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

grammar BaseRule;

import Symbol, Keyword, Literals;

propertiesDefinition
: PROPERTIES LP_ properties? RP_
;

properties
: property (COMMA_ property)*
;

property
: key=STRING_ EQ_ value=literal
;

literal
: STRING_ | (MINUS_)? INT_ | TRUE | FALSE
;
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,39 @@ RULE
SQL_TRANSLATOR
: S Q L UL_ T R A N S L A T O R
;

USE_ORIGINAL_SQL_WHEN_TRANSLATING_FAILED
: U S E UL_ O R I G I N A L UL_ S Q L UL_ W H E N UL_ T R A N S L A T I N G UL_ F A I L E D
;

ALTER
: A L T E R
;

TYPE
: T Y P E
;

JOOQ
: J O O Q
;

NATIVE
: N A T I V E
;

NAME
: N A M E
;

PROPERTIES
: P R O P E R T I E S
;

TRUE
: T R U E
;

FALSE
: F A L S E
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*/

lexer grammar Literals;

import Alphabet, Symbol;

IDENTIFIER_
: [A-Za-z_$0-9]*?[A-Za-z_$]+?[A-Za-z_$0-9]*
| BQ_ ~'`'+ BQ_
;

STRING_
: (DQ_ ('\\'. | '""' | ~('"' | '\\'))* DQ_)
| (SQ_ ('\\'. | '\'\'' | ~('\'' | '\\'))* SQ_)
;

INT_
: [0-9]+
;
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,32 @@

grammar RALStatement;

import Keyword;
import BaseRule;

showSQLTranslatorRule
: SHOW SQL_TRANSLATOR RULE
;

alterSQLTranslatorRule
: ALTER SQL_TRANSLATOR RULE LP_ sqlTranslatorRuleDefinition RP_
;

sqlTranslatorRuleDefinition
: TYPE LP_ NAME EQ_ providerName (COMMA_ propertiesDefinition)? RP_ (COMMA_ useOriginalSQLDefinition)?
;

useOriginalSQLDefinition
: USE_ORIGINAL_SQL_WHEN_TRANSLATING_FAILED EQ_ useOriginalSQL
;

providerName
: STRING_ | buildInProviderTypeName
;

buildInProviderTypeName
: JOOQ | NATIVE
;

useOriginalSQL
: TRUE | FALSE
;
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ grammar SQLTranslatorDistSQLStatement;
import Symbol, RALStatement;

execute
: (showSQLTranslatorRule) SEMI_? EOF
: (showSQLTranslatorRule
| alterSQLTranslatorRule
) SEMI_? EOF
;
Loading

0 comments on commit 81a5a48

Please sign in to comment.