Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Nov 2, 2024
1 parent de4a560 commit 4522bb8
Show file tree
Hide file tree
Showing 29 changed files with 1,375 additions and 1,151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public class CheckResult {

private static final CheckResult SUCCESS = new CheckResult(true, "");

private boolean success;
private boolean isSuccess;

private String msg;

private CheckResult(boolean success, String msg) {
this.success = success;
private CheckResult(boolean isSuccess, String msg) {
this.isSuccess = isSuccess;
this.msg = msg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static class SingleChoiceOptionBuilder<T> {
private final String key;
private final TypeReference<T> typeReference;

SingleChoiceOptionBuilder(String key, TypeReference typeReference, List<T> optionValues) {
SingleChoiceOptionBuilder(String key, TypeReference<T> typeReference, List<T> optionValues) {
this.optionValues = optionValues;
this.key = key;
this.typeReference = typeReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package com.github.ares.common.exceptions;

import java.io.Serializable;

/** Ares connector error code interface */
public interface AresErrorCode {
public interface AresErrorCode extends Serializable {
/**
* Get error code
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,38 @@
import static com.github.ares.common.exceptions.CommonErrorCode.WRITE_ARES_ROW_ERROR;

public class CommonError {
private static final String KEY_IDENTIFIER = "identifier";
private static final String KEY_OPERATION = "operation";
private static final String KEY_FILE_NAME = "fileName";
private static final String KEY_DATA_TYPE = "dataType";
private static final String KEY_FIELD = "field";

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public static AresRuntimeException fileOperationFailed(
String identifier, String operation, String fileName, Throwable cause) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("operation", operation);
params.put("fileName", fileName);
params.put(KEY_IDENTIFIER, identifier);
params.put(KEY_OPERATION, operation);
params.put(KEY_FILE_NAME, fileName);
return new AresRuntimeException(FILE_OPERATION_FAILED, params, cause);
}

public static AresRuntimeException fileOperationFailed(
String identifier, String operation, String fileName) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("operation", operation);
params.put("fileName", fileName);
params.put(KEY_IDENTIFIER, identifier);
params.put(KEY_OPERATION, operation);
params.put(KEY_FILE_NAME, fileName);
return new AresRuntimeException(FILE_OPERATION_FAILED, params);
}

public static AresRuntimeException fileNotExistFailed(
String identifier, String operation, String fileName) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("operation", operation);
params.put("fileName", fileName);
params.put(KEY_IDENTIFIER, identifier);
params.put(KEY_OPERATION, operation);
params.put(KEY_FILE_NAME, fileName);
return new AresRuntimeException(FILE_NOT_EXISTED, params);
}

Expand All @@ -76,18 +81,18 @@ public static AresRuntimeException writeAresRowFailed(
public static AresRuntimeException unsupportedDataType(
String identifier, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("dataType", dataType);
params.put("field", field);
params.put(KEY_IDENTIFIER, identifier);
params.put(KEY_DATA_TYPE, dataType);
params.put(KEY_FIELD, field);
return new AresRuntimeException(UNSUPPORTED_DATA_TYPE, params);
}

public static AresRuntimeException convertToAresTypeError(
String identifier, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("dataType", dataType);
params.put("field", field);
params.put(KEY_IDENTIFIER, identifier);
params.put(KEY_DATA_TYPE, dataType);
params.put(KEY_FIELD, field);
return new AresRuntimeException(CONVERT_TO_ARES_TYPE_ERROR_SIMPLE, params);
}

Expand All @@ -96,9 +101,9 @@ public static AresRuntimeException convertToAresTypeError(
public static AresRuntimeException convertToConnectorTypeError(
String identifier, String dataType, String field) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put("dataType", dataType);
params.put("field", field);
params.put(KEY_IDENTIFIER, identifier);
params.put(KEY_DATA_TYPE, dataType);
params.put(KEY_FIELD, field);
return new AresRuntimeException(CONVERT_TO_CONNECTOR_TYPE_ERROR_SIMPLE, params);
}

Expand Down Expand Up @@ -137,7 +142,7 @@ public static AresRuntimeException jsonOperationError(String identifier, String
public static AresRuntimeException jsonOperationError(
String identifier, String payload, Throwable cause) {
Map<String, String> params = new HashMap<>();
params.put("identifier", identifier);
params.put(KEY_IDENTIFIER, identifier);
params.put("payload", payload);
AresErrorCode code = JSON_OPERATION_FAILED;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

public class TimeUtils {
private static final Map<Formatter, DateTimeFormatter> FORMATTER_MAP =
new HashMap<Formatter, DateTimeFormatter>();
private static final Map<Formatter, DateTimeFormatter> FORMATTER_MAP = new EnumMap<>(Formatter.class);

static {
FORMATTER_MAP.put(
Expand Down
20 changes: 10 additions & 10 deletions ares-common/src/main/java/com/github/ares/common/utils/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

import java.io.Serializable;

public class Tuple2<T1, T2> implements Serializable {
public class Tuple2<T1 extends Serializable, T2 extends Serializable> implements Serializable {
private static final long serialVersionUID = 1L;

private final T1 _1;
private final T2 _2;
private final T1 value1;
private final T2 value2;

public Tuple2(T1 _1, T2 _2) {
this._1 = _1;
this._2 = _2;
public Tuple2(T1 value1, T2 value2) {
this.value1 = value1;
this.value2 = value2;
}

public static <T1, T2> Tuple2<T1, T2> of(T1 _1, T2 _2) {
return new Tuple2<>(_1, _2);
public static <T1 extends Serializable, T2 extends Serializable> Tuple2<T1, T2> of(T1 value1, T2 value2) {
return new Tuple2<>(value1, value2);
}

public T1 _1() {
return _1;
return value1;
}

public T2 _2() {
return _2;
return value2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ private Sql_scriptContext parse(InputStream in) {
parser.removeErrorListeners();
parser.addErrorListener(parserErrorListener);

Sql_scriptContext sql_scriptContext = parser.sql_script();
Sql_scriptContext sqlScriptContext = parser.sql_script();

if (!lexerErrorListener.getErrors().isEmpty()) {
throw new ParseException(String.join("\n", lexerErrorListener.getErrors()));
}
if (!parserErrorListener.getErrors().isEmpty()) {
throw new ParseException(String.join("\n", parserErrorListener.getErrors()));
}
return sql_scriptContext;
return sqlScriptContext;
} catch (Exception e) {
throw new ParseException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@
import com.github.ares.com.google.inject.name.Names;

public class ParserServiceModule extends AbstractModule {
@Override
protected void configure() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class TableWith extends LogicalOperation implements Serializable

protected Map<String, Object> options = new LinkedHashMap<>();

public TableWith(OperationType plainType) {
protected TableWith(OperationType plainType) {
super(plainType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class LogicalOperation implements Serializable {

private OperationType operationType;

public LogicalOperation(OperationType operationType) {
protected LogicalOperation(OperationType operationType) {
this.operationType = operationType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.github.ares.parser.sqlparser.sparksql;

import com.github.ares.api.common.CriteriaClause;
import com.github.ares.org.antlr.v4.runtime.CharStream;
import com.github.ares.org.antlr.v4.runtime.CharStreams;
import com.github.ares.org.antlr.v4.runtime.CommonTokenStream;
import com.github.ares.parser.antlr4.CaseChangingCharStream;
import com.github.ares.parser.antlr4.CustomErrorListener;
import com.github.ares.parser.antlr4.sparksql.SqlBaseLexer;
import com.github.ares.parser.antlr4.sparksql.SqlBaseParser;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class CommonParser {

public static final String UNSUPPORTED_EXP_MSG = "unsupported syntax: ";
public static final String UNSUPPORTED_EXP_MSG_WITH_PARAM = UNSUPPORTED_EXP_MSG + " %s";

public static final String SQL_SELECT_PREFIX = "SELECT ";

public static SqlBaseParser parseSql(InputStream in) throws IOException {
CharStream s = CharStreams.fromStream(in);
CaseChangingCharStream upper = new CaseChangingCharStream(s, true);

CustomErrorListener lexerErrorListener = new CustomErrorListener();
SqlBaseLexer lexer = new SqlBaseLexer(upper);
lexer.removeErrorListeners();
lexer.addErrorListener(lexerErrorListener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokens);
CustomErrorListener parserErrorListener = new CustomErrorListener();
parser.removeErrorListeners();
parser.addErrorListener(parserErrorListener);
return parser;
}

public static void visitCriteriaClause(CriteriaClause criteriaClause, List<String> items) {
if ("AND".equalsIgnoreCase(criteriaClause.getOperator()) || "OR".equalsIgnoreCase(criteriaClause.getOperator())) {
visitCriteriaClause(criteriaClause.getLeftCriteria(), items);
visitCriteriaClause(criteriaClause.getRightCriteria(), items);
} else {
if ("IN".equalsIgnoreCase(criteriaClause.getOperator())) {
if (criteriaClause.getInItems() != null) {
items.addAll(criteriaClause.getInItems());
}
} else {
items.add(criteriaClause.getRightExpr());
}
}
}
}
Loading

0 comments on commit 4522bb8

Please sign in to comment.