Skip to content

Commit

Permalink
[Refactor](dialect) Add sql dialect converter plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxiangyu committed Dec 25, 2023
1 parent 8bc7ea5 commit f8b62c5
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ under the License.
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>presto-converter</artifactId>
<artifactId>http-dialect-converter</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

### plugin configuration
# Replace the target url, set your sql converter service url here
target_url=http://127.0.0.1:8080/api/v1/sql/convert
target_url=http://127.0.0.1:8080/api/v1/sql/convert
accept_dialects=presto
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ type=DIALECT
description=SQL dialect converter plugin for presto.
version=1.0.0
java.version=1.8.0
classname=org.apache.doris.plugin.dialect.presto.PrestoDialectConverterPlugin
classname=org.apache.doris.plugin.dialect.presto.HttpDialectConverterPlugin
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.plugin.dialect.presto;
package org.apache.doris.plugin.dialect.http;

import com.google.common.base.Preconditions;
import org.apache.doris.nereids.parser.Dialect;
import org.apache.doris.plugin.DialectConverterPlugin;
import org.apache.doris.plugin.Plugin;
Expand All @@ -26,24 +25,27 @@
import org.apache.doris.plugin.PluginInfo;
import org.apache.doris.qe.SessionVariable;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

public class PrestoDialectConverterPlugin extends Plugin implements DialectConverterPlugin {
public class HttpDialectConverterPlugin extends Plugin implements DialectConverterPlugin {

private volatile boolean isInit = false;
private volatile boolean isClosed = false;
private volatile String targetURL = null;
private volatile ImmutableSet<Dialect> acceptDialects = null;

@Override
public void init(PluginInfo info, PluginContext ctx) throws PluginException {
Expand Down Expand Up @@ -83,6 +85,10 @@ private void loadConfig(PluginContext ctx, Map<String, String> pluginInfoPropert
final Map<String, String> properties = props.stringPropertyNames().stream()
.collect(Collectors.toMap(Function.identity(), props::getProperty));
targetURL = properties.get("target_url");
acceptDialects = ImmutableSet.copyOf(Arrays.stream(properties.get("accept_dialects").split(","))
.map(Dialect::getByName)
.filter(dialect -> dialect!= null)
.collect(ImmutableSet.toImmutableSet()));
}

@Override
Expand All @@ -93,12 +99,12 @@ public void close() throws IOException {

@Override
public ImmutableSet<Dialect> acceptDialects() {
return ImmutableSet.of(Dialect.PRESTO);
return acceptDialects;
}

@Override
public @Nullable String convertSql(String originSql, SessionVariable sessionVariable) {
Preconditions.checkNotNull(targetURL);
return SQLDialectUtils.convertSql(targetURL, originSql);
return HttpDialectUtils.convertSql(targetURL, originSql);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.plugin.dialect.presto;
package org.apache.doris.plugin.dialect.http;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -52,8 +52,8 @@
* "code": 0,
* "message": ""
*/
public class SQLDialectUtils {
private static final Logger LOG = LogManager.getLogger(SQLDialectUtils.class);
public class HttpDialectUtils {
private static final Logger LOG = LogManager.getLogger(HttpDialectUtils.class);

public static String convertSql(String targetURL, String originStmt) {
ConvertRequest convertRequest = new ConvertRequest(originStmt, "presto");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.plugin.dialect.presto;
package org.apache.doris.plugin.dialect.http;

import org.junit.After;
import org.junit.Assert;
Expand All @@ -27,7 +27,7 @@
import java.net.ServerSocket;
import java.net.SocketException;

public class SQLDialectUtilsTest {
public class HttpDialectUtilsTest {

private int port;
private SimpleHttpServer server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.plugin.dialect.presto;
package org.apache.doris.plugin.dialect.http;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
Expand Down
2 changes: 1 addition & 1 deletion fe_plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ under the License.
<modules>
<module>auditdemo</module>
<module>auditloader</module>
<module>presto-converter</module>
<module>http-dialect-converter</module>
<module>trino-converter</module>
<module>sparksql-converter</module>
</modules>
Expand Down

0 comments on commit f8b62c5

Please sign in to comment.