[improvement][headless]Remove unnecessary Database class
Some checks are pending
supersonic CentOS CI / build (21) (push) Waiting to run
supersonic mac CI / build (21) (push) Waiting to run
supersonic ubuntu CI / build (21) (push) Waiting to run
supersonic windows CI / build (21) (push) Waiting to run

This commit is contained in:
jerryjzhang
2024-12-29 19:29:54 +08:00
parent 6486257c9e
commit a2f54d4c80
21 changed files with 71 additions and 135 deletions

View File

@@ -1,8 +1,8 @@
package com.tencent.supersonic.headless.core.executor;
import com.tencent.supersonic.common.util.ContextUtils;
import com.tencent.supersonic.headless.api.pojo.response.DatabaseResp;
import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp;
import com.tencent.supersonic.headless.core.pojo.Database;
import com.tencent.supersonic.headless.core.pojo.QueryStatement;
import com.tencent.supersonic.headless.core.utils.ComponentFactory;
import com.tencent.supersonic.headless.core.utils.SqlUtils;
@@ -38,7 +38,7 @@ public class JdbcExecutor implements QueryExecutor {
SqlUtils sqlUtils = ContextUtils.getBean(SqlUtils.class);
String sql = StringUtils.normalizeSpace(queryStatement.getSql());
log.info("executing SQL: {}", sql);
Database database = queryStatement.getOntology().getDatabase();
DatabaseResp database = queryStatement.getOntology().getDatabase();
SemanticQueryResp queryResultWithColumns = new SemanticQueryResp();
try {
SqlUtils sqlUtil = sqlUtils.init(database);

View File

@@ -1,49 +0,0 @@
package com.tencent.supersonic.headless.core.pojo;
import com.google.common.collect.Lists;
import com.tencent.supersonic.common.pojo.RecordInfo;
import com.tencent.supersonic.common.pojo.enums.EngineType;
import com.tencent.supersonic.common.util.AESEncryptionUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Database extends RecordInfo {
private Long id;
private Long domainId;
private String name;
private String description;
private String version;
private String url;
private String username;
private String password;
private String database;
private String schema;
/** mysql,clickhouse */
private EngineType type;
private List<String> admins = Lists.newArrayList();
private List<String> viewers = Lists.newArrayList();
public String passwordDecrypt() {
return AESEncryptionUtil.aesDecryptECB(password);
}
}

View File

@@ -2,6 +2,7 @@ package com.tencent.supersonic.headless.core.pojo;
import com.alibaba.druid.pool.DruidDataSource;
import com.tencent.supersonic.headless.api.pojo.enums.DataType;
import com.tencent.supersonic.headless.api.pojo.response.DatabaseResp;
import com.tencent.supersonic.headless.core.utils.JdbcDataSourceUtils;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -106,7 +107,7 @@ public class JdbcDataSource {
}
}
public void removeDatasource(Database database) {
public void removeDatasource(DatabaseResp database) {
String key = getDataSourceKey(database);
@@ -128,7 +129,7 @@ public class JdbcDataSource {
}
}
public DruidDataSource getDataSource(Database database) throws RuntimeException {
public DruidDataSource getDataSource(DatabaseResp database) throws RuntimeException {
String name = database.getName();
String jdbcUrl = database.getUrl();
@@ -239,7 +240,7 @@ public class JdbcDataSource {
return druidDataSource;
}
private String getDataSourceKey(Database database) {
private String getDataSourceKey(DatabaseResp database) {
return JdbcDataSourceUtils.getKey(database.getName(), database.getUrl(),
database.getUsername(), database.passwordDecrypt(), "", false);
}

View File

@@ -1,6 +1,7 @@
package com.tencent.supersonic.headless.core.pojo;
import com.tencent.supersonic.common.pojo.enums.EngineType;
import com.tencent.supersonic.headless.api.pojo.response.DatabaseResp;
import com.tencent.supersonic.headless.core.translator.parser.s2sql.DataModel;
import com.tencent.supersonic.headless.core.translator.parser.s2sql.Dimension;
import com.tencent.supersonic.headless.core.translator.parser.s2sql.Materialization;
@@ -18,7 +19,7 @@ public class Ontology {
private Map<String, List<Dimension>> dimensionMap = new HashMap<>();
private List<Materialization> materializationList = new ArrayList<>();
private List<JoinRelation> joinRelations;
private Database database;
private DatabaseResp database;
public List<Dimension> getDimensions() {
return dimensionMap.values().stream().flatMap(Collection::stream)
@@ -27,9 +28,15 @@ public class Ontology {
public EngineType getDatabaseType() {
if (Objects.nonNull(database)) {
return database.getType();
return EngineType.fromString(database.getType().toUpperCase());
}
return null;
}
public String getDatabaseVersion() {
if (Objects.nonNull(database)) {
return database.getVersion();
}
return null;
}
}

View File

@@ -7,7 +7,7 @@ import com.tencent.supersonic.common.pojo.enums.EngineType;
import com.tencent.supersonic.common.util.ContextUtils;
import com.tencent.supersonic.common.util.DateModeUtils;
import com.tencent.supersonic.headless.api.pojo.enums.AggOption;
import com.tencent.supersonic.headless.core.pojo.Database;
import com.tencent.supersonic.headless.api.pojo.response.DatabaseResp;
import com.tencent.supersonic.headless.core.pojo.OntologyQuery;
import com.tencent.supersonic.headless.core.pojo.QueryStatement;
import com.tencent.supersonic.headless.core.pojo.SqlQuery;
@@ -60,8 +60,9 @@ public class MetricRatioParser implements QueryParser {
@Override
public void parse(QueryStatement queryStatement) throws Exception {
Database database = queryStatement.getOntology().getDatabase();
generateRatioSql(queryStatement, database.getType(), database.getVersion());
DatabaseResp database = queryStatement.getOntology().getDatabase();
generateRatioSql(queryStatement, queryStatement.getOntology().getDatabaseType(),
database.getVersion());
}
/** Ratio */

View File

@@ -4,7 +4,6 @@ import com.tencent.supersonic.common.pojo.Aggregator;
import com.tencent.supersonic.common.pojo.ColumnOrder;
import com.tencent.supersonic.common.util.ContextUtils;
import com.tencent.supersonic.headless.api.pojo.enums.AggOption;
import com.tencent.supersonic.headless.core.pojo.Database;
import com.tencent.supersonic.headless.core.pojo.OntologyQuery;
import com.tencent.supersonic.headless.core.pojo.QueryStatement;
import com.tencent.supersonic.headless.core.pojo.SqlQuery;
@@ -37,8 +36,8 @@ public class StructQueryParser implements QueryParser {
sqlGenerateUtils.getSelect(structQuery), dsTable,
sqlGenerateUtils.getGroupBy(structQuery), sqlGenerateUtils.getOrderBy(structQuery),
sqlGenerateUtils.getLimit(structQuery));
Database database = queryStatement.getOntology().getDatabase();
if (!sqlGenerateUtils.isSupportWith(database.getType(), database.getVersion())) {
if (!sqlGenerateUtils.isSupportWith(queryStatement.getOntology().getDatabaseType(),
queryStatement.getOntology().getDatabaseVersion())) {
sqlParam.setSupportWith(false);
sql = String.format("select %s from %s t0 %s %s %s",
sqlGenerateUtils.getSelect(structQuery), dsTable,

View File

@@ -33,7 +33,7 @@ public class SchemaBuilder {
Prepare.CatalogReader catalogReader = new CalciteCatalogReader(rootSchema,
Collections.singletonList(schema.getSchemaKey()), Configuration.typeFactory,
Configuration.config);
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
S2SQLSqlValidatorImpl s2SQLSqlValidator =
new S2SQLSqlValidatorImpl(Configuration.operatorTable, catalogReader,
Configuration.typeFactory, Configuration.getValidatorConfig(engineType));

View File

@@ -3,7 +3,6 @@ package com.tencent.supersonic.headless.core.translator.parser.calcite;
import com.tencent.supersonic.common.calcite.Configuration;
import com.tencent.supersonic.common.pojo.enums.EngineType;
import com.tencent.supersonic.headless.api.pojo.enums.AggOption;
import com.tencent.supersonic.headless.core.pojo.Database;
import com.tencent.supersonic.headless.core.pojo.OntologyQuery;
import com.tencent.supersonic.headless.core.pojo.QueryStatement;
import com.tencent.supersonic.headless.core.translator.parser.calcite.node.DataModelNode;
@@ -19,7 +18,10 @@ import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.validate.SqlValidatorScope;
import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
@Slf4j
public class SqlBuilder {
@@ -43,9 +45,8 @@ public class SqlBuilder {
this.aggOption = ontologyQuery.getAggOption();
buildParseNode();
Database database = queryStatement.getOntology().getDatabase();
optimizeParseNode(database.getType());
return getSql(database.getType());
optimizeParseNode(queryStatement.getOntology().getDatabaseType());
return getSql(queryStatement.getOntology().getDatabaseType());
}
private void buildParseNode() throws Exception {

View File

@@ -152,7 +152,7 @@ public class DataModelNode extends SemanticNode {
public static void mergeQueryFilterDimensionMeasure(Ontology ontology, OntologyQuery queryParam,
Set<String> dimensions, Set<String> measures, SqlValidatorScope scope)
throws Exception {
EngineType engineType = ontology.getDatabase().getType();
EngineType engineType = ontology.getDatabaseType();
if (Objects.nonNull(queryParam.getWhere()) && !queryParam.getWhere().isEmpty()) {
Set<String> filterConditions = new HashSet<>();
FilterNode.getFilterField(parse(queryParam.getWhere(), scope, engineType),

View File

@@ -32,7 +32,7 @@ public class FilterRender extends Renderer {
SqlNode filterNode = null;
List<String> queryMetrics = new ArrayList<>(metricCommand.getMetrics());
List<String> queryDimensions = new ArrayList<>(metricCommand.getDimensions());
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
if (metricCommand.getWhere() != null && !metricCommand.getWhere().isEmpty()) {
filterNode = SemanticNode.parse(metricCommand.getWhere(), scope, engineType);

View File

@@ -50,7 +50,7 @@ public class JoinRender extends Renderer {
public void render(OntologyQuery metricCommand, List<DataModel> dataModels,
SqlValidatorScope scope, S2CalciteSchema schema, boolean nonAgg) throws Exception {
String queryWhere = metricCommand.getWhere();
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
Set<String> whereFields = new HashSet<>();
List<String> fieldWhere = new ArrayList<>();
if (queryWhere != null && !queryWhere.isEmpty()) {
@@ -147,7 +147,7 @@ public class JoinRender extends Renderer {
Set<String> sourceMeasure, SqlValidatorScope scope, S2CalciteSchema schema,
boolean nonAgg) throws Exception {
String alias = Constants.JOIN_TABLE_PREFIX + dataModel.getName();
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
for (String m : reqMetrics) {
if (getMatchMetric(schema, sourceMeasure, m, queryMetrics)) {
MetricNode metricNode = buildMetricNode(m, dataModel, scope, schema, nonAgg, alias);
@@ -182,7 +182,7 @@ public class JoinRender extends Renderer {
Set<String> dimension, SqlValidatorScope scope, S2CalciteSchema schema)
throws Exception {
String alias = Constants.JOIN_TABLE_PREFIX + dataModel.getName();
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
for (String d : reqDimensions) {
if (getMatchDimension(schema, dimension, dataModel, d, queryDimension)) {
if (d.contains(Constants.DIMENSION_IDENTIFY)) {
@@ -262,7 +262,7 @@ public class JoinRender extends Renderer {
private SqlNode buildJoin(SqlNode left, TableView leftTable, TableView tableView,
Map<String, String> before, DataModel dataModel, S2CalciteSchema schema,
SqlValidatorScope scope) throws Exception {
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
SqlNode condition =
getCondition(leftTable, tableView, dataModel, schema, scope, engineType);
SqlLiteral sqlLiteral = SemanticNode.getJoinSqlLiteral("");
@@ -465,7 +465,7 @@ public class JoinRender extends Renderer {
endTime = zipper.getAlias() + "." + endTimeOp.get().getName();
dateTime = partMetric.getAlias() + "." + partTime.get().getName();
}
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
ArrayList<SqlNode> operandList =
new ArrayList<>(Arrays.asList(SemanticNode.parse(endTime, scope, engineType),
SemanticNode.parse(dateTime, scope, engineType)));

View File

@@ -25,7 +25,7 @@ public class OutputRender extends Renderer {
public void render(OntologyQuery metricCommand, List<DataModel> dataModels,
SqlValidatorScope scope, S2CalciteSchema schema, boolean nonAgg) throws Exception {
TableView selectDataSet = super.tableView;
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
for (String dimension : metricCommand.getDimensions()) {
selectDataSet.getMeasure().add(SemanticNode.parse(dimension, scope, engineType));
}

View File

@@ -107,7 +107,7 @@ public class SourceRender extends Renderer {
S2CalciteSchema schema, boolean nonAgg, Map<String, String> extendFields,
TableView dataSet, TableView output, SqlValidatorScope scope) throws Exception {
List<Dimension> dimensionList = schema.getDimensions().get(datasource.getName());
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
boolean isAdd = false;
if (!CollectionUtils.isEmpty(dimensionList)) {
for (Dimension dim : dimensionList) {
@@ -185,7 +185,7 @@ public class SourceRender extends Renderer {
SqlValidatorScope scope, S2CalciteSchema schema, boolean nonAgg) throws Exception {
Iterator<String> iterator = fields.iterator();
List<SqlNode> whereNode = new ArrayList<>();
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
while (iterator.hasNext()) {
String cur = iterator.next();
if (queryDimensions.contains(cur) || queryMetrics.contains(cur)) {
@@ -339,7 +339,7 @@ public class SourceRender extends Renderer {
String queryWhere = ontologyQuery.getWhere();
Set<String> whereFields = new HashSet<>();
List<String> fieldWhere = new ArrayList<>();
EngineType engineType = schema.getOntology().getDatabase().getType();
EngineType engineType = schema.getOntology().getDatabaseType();
if (queryWhere != null && !queryWhere.isEmpty()) {
SqlNode sqlNode = SemanticNode.parse(queryWhere, scope, engineType);
FilterNode.getFilterField(sqlNode, whereFields);

View File

@@ -5,7 +5,7 @@ import javax.sql.DataSource;
import com.alibaba.druid.util.StringUtils;
import com.tencent.supersonic.common.util.MD5Util;
import com.tencent.supersonic.headless.api.pojo.enums.DataType;
import com.tencent.supersonic.headless.core.pojo.Database;
import com.tencent.supersonic.headless.api.pojo.response.DatabaseResp;
import com.tencent.supersonic.headless.core.pojo.JdbcDataSource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -18,14 +18,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import static com.tencent.supersonic.common.pojo.Constants.AT_SYMBOL;
import static com.tencent.supersonic.common.pojo.Constants.COLON;
import static com.tencent.supersonic.common.pojo.Constants.DOUBLE_SLASH;
import static com.tencent.supersonic.common.pojo.Constants.EMPTY;
import static com.tencent.supersonic.common.pojo.Constants.JDBC_PREFIX_FORMATTER;
import static com.tencent.supersonic.common.pojo.Constants.NEW_LINE_CHAR;
import static com.tencent.supersonic.common.pojo.Constants.PATTERN_JDBC_TYPE;
import static com.tencent.supersonic.common.pojo.Constants.SPACE;
import static com.tencent.supersonic.common.pojo.Constants.*;
/** tools functions about jdbc */
@Slf4j
@@ -39,7 +32,7 @@ public class JdbcDataSourceUtils {
this.jdbcDataSource = jdbcDataSource;
}
public static boolean testDatabase(Database database) {
public static boolean testDatabase(DatabaseResp database) {
try {
Class.forName(getDriverClassName(database.getUrl()));
@@ -146,11 +139,11 @@ public class JdbcDataSourceUtils {
return MD5Util.getMD5(sb.toString(), true, 64);
}
public DataSource getDataSource(Database database) throws RuntimeException {
public DataSource getDataSource(DatabaseResp database) throws RuntimeException {
return jdbcDataSource.getDataSource(database);
}
public Connection getConnection(Database database) throws RuntimeException {
public Connection getConnection(DatabaseResp database) throws RuntimeException {
Connection conn = getConnectionWithRetry(database);
if (conn == null) {
try {
@@ -166,7 +159,7 @@ public class JdbcDataSourceUtils {
return conn;
}
private Connection getConnectionWithRetry(Database database) {
private Connection getConnectionWithRetry(DatabaseResp database) {
int rc = 1;
for (;;) {
@@ -193,7 +186,7 @@ public class JdbcDataSourceUtils {
}
}
public void releaseDataSource(Database database) {
public void releaseDataSource(DatabaseResp database) {
jdbcDataSource.removeDatasource(database);
}
}

View File

@@ -3,11 +3,10 @@ package com.tencent.supersonic.headless.core.utils;
import javax.sql.DataSource;
import com.tencent.supersonic.common.pojo.QueryColumn;
import com.tencent.supersonic.common.pojo.enums.EngineType;
import com.tencent.supersonic.common.util.DateUtils;
import com.tencent.supersonic.headless.api.pojo.enums.DataType;
import com.tencent.supersonic.headless.api.pojo.response.DatabaseResp;
import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp;
import com.tencent.supersonic.headless.core.pojo.Database;
import com.tencent.supersonic.headless.core.pojo.JdbcDataSource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -24,11 +23,7 @@ import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static com.tencent.supersonic.common.pojo.Constants.AT_SYMBOL;
@@ -38,7 +33,7 @@ import static com.tencent.supersonic.common.pojo.Constants.AT_SYMBOL;
public class SqlUtils {
@Getter
private Database database;
private DatabaseResp database;
@Autowired
private JdbcDataSource jdbcDataSource;
@@ -57,15 +52,15 @@ public class SqlUtils {
public SqlUtils() {}
public SqlUtils(Database database) {
public SqlUtils(DatabaseResp database) {
this.database = database;
this.dataTypeEnum = DataType.urlOf(database.getUrl());
}
public SqlUtils init(Database database) {
public SqlUtils init(DatabaseResp database) {
return SqlUtilsBuilder.getBuilder()
.withName(database.getId() + AT_SYMBOL + database.getName())
.withType(database.getType().getName()).withJdbcUrl(database.getUrl())
.withType(database.getType()).withJdbcUrl(database.getUrl())
.withUsername(database.getUsername()).withPassword(database.getPassword())
.withJdbcDataSource(this.jdbcDataSource).withResultLimit(this.resultLimit)
.withIsQueryLogEnable(this.isQueryLogEnable).build();
@@ -225,9 +220,8 @@ public class SqlUtils {
}
public SqlUtils build() {
Database database = Database.builder().name(this.name)
.type(EngineType.fromString(this.type.toUpperCase())).url(this.jdbcUrl)
.username(this.username).password(this.password).build();
DatabaseResp database = DatabaseResp.builder().name(this.name).type(this.type)
.url(this.jdbcUrl).username(this.username).password(this.password).build();
SqlUtils sqlUtils = new SqlUtils(database);
sqlUtils.jdbcDataSource = this.jdbcDataSource;