mirror of
https://github.com/tencentmusic/supersonic.git
synced 2026-04-28 20:04:27 +08:00
Compare commits
1 Commits
ecea348c44
...
0eb8897d08
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0eb8897d08 |
@@ -170,9 +170,6 @@ public class QueryStructReq extends SemanticQueryReq {
|
|||||||
// 5. Set the limit clause
|
// 5. Set the limit clause
|
||||||
plainSelect.setLimit(buildLimit(queryStructReq));
|
plainSelect.setLimit(buildLimit(queryStructReq));
|
||||||
|
|
||||||
// 6. Set having clause
|
|
||||||
plainSelect.setHaving(buildHavingClause(queryStructReq));
|
|
||||||
|
|
||||||
select.setSelect(plainSelect);
|
select.setSelect(plainSelect);
|
||||||
|
|
||||||
// 6. Set where clause
|
// 6. Set where clause
|
||||||
@@ -242,8 +239,7 @@ public class QueryStructReq extends SemanticQueryReq {
|
|||||||
|
|
||||||
private GroupByElement buildGroupByElement(QueryStructReq queryStructReq) {
|
private GroupByElement buildGroupByElement(QueryStructReq queryStructReq) {
|
||||||
List<String> groups = queryStructReq.getGroups();
|
List<String> groups = queryStructReq.getGroups();
|
||||||
if ((!CollectionUtils.isEmpty(groups) && !queryStructReq.getAggregators().isEmpty())
|
if (!CollectionUtils.isEmpty(groups) && !queryStructReq.getAggregators().isEmpty()) {
|
||||||
|| !queryStructReq.getMetricFilters().isEmpty()) {
|
|
||||||
GroupByElement groupByElement = new GroupByElement();
|
GroupByElement groupByElement = new GroupByElement();
|
||||||
for (String group : groups) {
|
for (String group : groups) {
|
||||||
groupByElement.addGroupByExpression(new Column(group));
|
groupByElement.addGroupByExpression(new Column(group));
|
||||||
@@ -293,23 +289,4 @@ public class QueryStructReq extends SemanticQueryReq {
|
|||||||
}
|
}
|
||||||
return Constants.TABLE_PREFIX + StringUtils.join(modelIds, "_");
|
return Constants.TABLE_PREFIX + StringUtils.join(modelIds, "_");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Expression buildHavingClause(QueryStructReq queryStructReq) {
|
|
||||||
if (queryStructReq.getMetricFilters().isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Filter> filters = queryStructReq.getMetricFilters();
|
|
||||||
SqlFilterUtils sqlFilterUtils = ContextUtils.getBean(SqlFilterUtils.class);
|
|
||||||
String havingClause = sqlFilterUtils.getWhereClause(filters, false);
|
|
||||||
if (StringUtils.isNotBlank(havingClause)) {
|
|
||||||
try {
|
|
||||||
return CCJSqlParserUtil.parseCondExpression(havingClause);
|
|
||||||
} catch (JSQLParserException e) {
|
|
||||||
log.error("Failed to parse having clause", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,18 +34,6 @@ public class SqlVariableParseUtilsTest {
|
|||||||
Assertions.assertEquals(expectedSql, actualSql);
|
Assertions.assertEquals(expectedSql, actualSql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testParseSql_if() {
|
|
||||||
String sql = "select * from t_$interval$ where id = $id$ $if(name)$and name = $name$$endif$";
|
|
||||||
List<SqlVariable> variables = Lists.newArrayList(mockNumSqlVariable(),
|
|
||||||
mockExprSqlVariable(), mockStrSqlVariable());
|
|
||||||
List<Param> params =
|
|
||||||
Lists.newArrayList(mockIdParam(), mockNameParam(), mockIntervalParam());
|
|
||||||
String actualSql = SqlVariableParseUtils.parse(sql, variables, params);
|
|
||||||
String expectedSql = "select * from t_wk where id = 2 and name = 'alice'";
|
|
||||||
Assertions.assertEquals(expectedSql, actualSql);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SqlVariable mockNumSqlVariable() {
|
private SqlVariable mockNumSqlVariable() {
|
||||||
return mockSqlVariable("id", VariableValueType.NUMBER, 1);
|
return mockSqlVariable("id", VariableValueType.NUMBER, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { isFunction } from 'lodash';
|
|||||||
import FullScreen from '@/components/FullScreen';
|
import FullScreen from '@/components/FullScreen';
|
||||||
import SqlEditor from '@/components/SqlEditor';
|
import SqlEditor from '@/components/SqlEditor';
|
||||||
import type { TaskResultItem, TaskResultColumn } from '../data';
|
import type { TaskResultItem, TaskResultColumn } from '../data';
|
||||||
import { executeSql } from '@/pages/SemanticModel/service';
|
import { executeSql, listColumnsBySql } from '@/pages/SemanticModel/service';
|
||||||
|
|
||||||
import SqlParams from './SqlParams';
|
import SqlParams from './SqlParams';
|
||||||
import styles from '../style.less';
|
import styles from '../style.less';
|
||||||
@@ -248,12 +248,19 @@ const SqlDetail: React.FC<IProps> = ({
|
|||||||
id: currentDatabaseItem.key,
|
id: currentDatabaseItem.key,
|
||||||
sqlVariables: sqlParams,
|
sqlVariables: sqlParams,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { code: getColumnCode, data: getColumnData } = await listColumnsBySql({
|
||||||
|
sql: value,
|
||||||
|
databaseId: currentDatabaseItem.key,
|
||||||
|
});
|
||||||
|
|
||||||
setResultTableLoading(false);
|
setResultTableLoading(false);
|
||||||
if (code === 200) {
|
if (code === 200 && getColumnCode === 200) {
|
||||||
const columnData = (data.columns || []).map((item: any) => {
|
const columnData = getColumnData.map((item) => {
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
columnName: item.nameEn,
|
nameEn: item.columnName,
|
||||||
|
type: item.dataType,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
fetchTaskResult(data, columnData);
|
fetchTaskResult(data, columnData);
|
||||||
|
|||||||
@@ -373,6 +373,12 @@ export async function executeSql(params: ExcuteSqlParams) {
|
|||||||
return request.post(`${process.env.API_BASE_URL}database/executeSql`, { data });
|
return request.post(`${process.env.API_BASE_URL}database/executeSql`, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function listColumnsBySql(data: { databaseId: number; sql: string }) {
|
||||||
|
return request.post(`${process.env.API_BASE_URL}database/listColumnsBySql`, {
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function getCatalogs(dbId: number): Promise<any> {
|
export function getCatalogs(dbId: number): Promise<any> {
|
||||||
return request(`${process.env.API_BASE_URL}database/getCatalogs`, {
|
return request(`${process.env.API_BASE_URL}database/getCatalogs`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@@ -403,12 +409,7 @@ export function getTables(databaseId: number, catalog: string, dbName: string):
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getColumns(
|
export function getColumns(databaseId: number, catalog: string, dbName: string, tableName: string): Promise<any> {
|
||||||
databaseId: number,
|
|
||||||
catalog: string,
|
|
||||||
dbName: string,
|
|
||||||
tableName: string,
|
|
||||||
): Promise<any> {
|
|
||||||
return request(`${process.env.API_BASE_URL}database/getColumnsByName`, {
|
return request(`${process.env.API_BASE_URL}database/getColumnsByName`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
params: {
|
params: {
|
||||||
|
|||||||
Reference in New Issue
Block a user