Unverified Commit d12da17a authored by Licho's avatar Licho Committed by GitHub

refactor: sql type detection with regrex. (#1811)

* refactor: sql type detection with regrex. * Spotless Apply * fix: add SqlType test * Spotless Apply * refactor: add CTAS sqlTYpe --------- Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent 9b7e8b4e
...@@ -133,7 +133,7 @@ public class Explainer { ...@@ -133,7 +133,7 @@ public class Explainer {
trans.add( trans.add(
new StatementParam( new StatementParam(
watchStatementExplainer.getCreateStatement(tableName), watchStatementExplainer.getCreateStatement(tableName),
SqlType.CTAS)); SqlType.CTASS));
} else { } else {
UDF udf = UDFUtil.toUDF(statement); UDF udf = UDFUtil.toUDF(statement);
if (Asserts.isNotNull(udf)) { if (Asserts.isNotNull(udf)) {
......
...@@ -444,7 +444,8 @@ public class JobManager { ...@@ -444,7 +444,8 @@ public class JobManager {
} else if (useStatementSet && !useGateway) { } else if (useStatementSet && !useGateway) {
List<String> inserts = new ArrayList<>(); List<String> inserts = new ArrayList<>();
for (StatementParam item : jobParam.getTrans()) { for (StatementParam item : jobParam.getTrans()) {
if (item.getType().isInsert() || item.getType().equals(SqlType.CTAS)) { if (item.getType().equals(SqlType.INSERT)
|| item.getType().equals(SqlType.CTASS)) {
inserts.add(item.getValue()); inserts.add(item.getValue());
} }
} }
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
package org.dinky.parser; package org.dinky.parser;
import java.util.regex.Pattern;
/** /**
* SqlType * SqlType
* *
...@@ -26,30 +28,50 @@ package org.dinky.parser; ...@@ -26,30 +28,50 @@ package org.dinky.parser;
* @since 2021/7/3 11:11 * @since 2021/7/3 11:11
*/ */
public enum SqlType { public enum SqlType {
SELECT("SELECT"), SELECT("SELECT", "^SELECT.*"),
CREATE("CREATE"),
DROP("DROP"), CREATE("CREATE", "^CREATE(?!.*AS SELECT).*$"),
ALTER("ALTER"),
INSERT("INSERT"), DROP("DROP", "^DROP.*"),
DESC("DESC"),
DESCRIBE("DESCRIBE"), ALTER("ALTER", "^ALTER.*"),
EXPLAIN("EXPLAIN"),
USE("USE"), INSERT("INSERT", "^INSERT.*"),
SHOW("SHOW"),
LOAD("LOAD"), DESC("DESC", "^DESC.*"),
UNLOAD("UNLOAD"),
SET("SET"), DESCRIBE("DESCRIBE", "^DESCRIBE.*"),
RESET("RESET"),
EXECUTE("EXECUTE"), EXPLAIN("EXPLAIN", "^EXPLAIN.*"),
ADD("ADD"),
WATCH("WATCH"), USE("USE", "^USE.*"),
CTAS("CTAS"),
UNKNOWN("UNKNOWN"); SHOW("SHOW", "^SHOW.*"),
LOAD("LOAD", "^LOAD.*"),
UNLOAD("UNLOAD", "^UNLOAD.*"),
SET("SET", "^SET.*"),
RESET("RESET", "^RESET.*"),
EXECUTE("EXECUTE", "^EXECUTE.*"),
ADD("ADD", "^ADD.*"),
WATCH("WATCH", "^WATCH.*"),
CTASS("CTAS", "^CREATE\\s.*AS\\sSELECT.*$"),
UNKNOWN("UNKNOWN", "^UNKNOWN.*");
private String type; private String type;
private Pattern pattern;
SqlType(String type) { SqlType(String type, String regrex) {
this.type = type; this.type = type;
this.pattern = Pattern.compile(regrex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
} }
public void setType(String type) { public void setType(String type) {
...@@ -60,11 +82,7 @@ public enum SqlType { ...@@ -60,11 +82,7 @@ public enum SqlType {
return type; return type;
} }
public boolean equalsValue(String value) { public boolean match(String statement) {
return type.equalsIgnoreCase(value); return pattern.matcher(statement).matches();
}
public boolean isInsert() {
return type.equals("INSERT");
} }
} }
...@@ -76,7 +76,7 @@ public class Operations { ...@@ -76,7 +76,7 @@ public class Operations {
public static SqlType getOperationType(String sql) { public static SqlType getOperationType(String sql) {
String sqlTrim = sql.replaceAll(SQL_EMPTY_STR, "").trim().toUpperCase(); String sqlTrim = sql.replaceAll(SQL_EMPTY_STR, "").trim().toUpperCase();
return Arrays.stream(SqlType.values()) return Arrays.stream(SqlType.values())
.filter(sqlType -> sqlTrim.startsWith(sqlType.getType())) .filter(sqlType -> sqlType.match(sqlTrim))
.findFirst() .findFirst()
.orElse(SqlType.UNKNOWN); .orElse(SqlType.UNKNOWN);
} }
......
/*
*
* 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.dinky.parser;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
class SqlTypeTest {
@Test
public void match() {
test(
"CREATE TABLE Orders (\n"
+ " order_number BIGINT,\n"
+ " price DECIMAL(32,2),\n"
+ " buyer ROW<first_name STRING, last_name STRING>,\n"
+ " order_time TIMESTAMP(3)\n"
+ ") WITH (\n"
+ " 'connector' = 'datagen',\n"
+ " 'rows-per-second' = '1'\n"
+ ");",
SqlType.CREATE,
true);
test("select * from Orders", SqlType.SELECT, true);
test("DROP...", SqlType.DROP, true);
test("ALTER...", SqlType.ALTER, true);
test("INSERT...", SqlType.INSERT, true);
test("DESC...", SqlType.DESC, true);
test("DESCRIBE...", SqlType.DESCRIBE, true);
test("EXPLAIN...", SqlType.EXPLAIN, true);
test("USE...", SqlType.USE, true);
test("SHOW...", SqlType.SHOW, true);
test("LOAD...", SqlType.LOAD, true);
test("UNLOAD...", SqlType.UNLOAD, true);
test("SET...", SqlType.SET, true);
test("RESET...", SqlType.RESET, true);
test("EXECUTE...", SqlType.EXECUTE, true);
test("ADD...", SqlType.ADD, true);
test("WATCH...", SqlType.WATCH, true);
String sql =
"CREATE TABLE print_OrdersView WITH ('connector' = 'printnet', 'port'='7125', 'hostName' = '172"
+ ".26.16.1', 'sink.parallelism'='1')\n"
+ "AS SELECT * FROM OrdersView; ";
test(sql, SqlType.CTASS, true);
test(sql, SqlType.CREATE, false);
}
private void test(String sql, SqlType sqlType, boolean result) {
Assertions.assertThat(sqlType.match(sql)).isEqualTo(result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment