Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 修复 聚合时字段别名 不被bigquery 允许使用 #2192

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package datart.data.provider.jdbc.adapters;

import datart.core.data.provider.Column;
import datart.core.data.provider.ForeignKey;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @Author lihuamin
* @create 2023/6/8
*/

@Slf4j
public class BigQueryDataProviderAdapter extends JdbcDataProviderAdapter {
@Override
public Set<String> readAllDatabases() throws SQLException {
Set<String> databases = new HashSet<>();
try (Connection conn = getConn()) {
DatabaseMetaData metaData = conn.getMetaData();
boolean isCatalog = isReadFromCatalog(conn);
ResultSet rs ;
if (!isCatalog) {
rs = metaData.getCatalogs();
} else {
rs = metaData.getSchemas();
log.info("Database 'catalogs' is true, but get databases with 'schemas'");
}

// String currDatabase = readCurrDatabase(conn, !isCatalog);
// if (StringUtils.isNotBlank(currDatabase)) {
// return Collections.singleton(currDatabase);
// }

while (rs.next()) {
String database = rs.getString(1);
databases.add(database);
}
return databases;
}
}


@Override
public Set<String> readAllTables(String database) throws SQLException {
try (Connection conn = getConn()) {
Set<String> tables = new HashSet<>();
DatabaseMetaData metadata = conn.getMetaData();
String catalog = conn.getCatalog();
// String catalog = null;
String schema;
boolean readFromCatalog = isReadFromCatalog(conn);
if (!readFromCatalog) {
catalog = database;
schema = conn.getSchema();
} else {
schema = database;
}
try (ResultSet rs = metadata.getTables(catalog, schema, "%", new String[]{"TABLE", "VIEW"})) {
while (rs.next()) {
String tableName = rs.getString(3);
tables.add(tableName);
}
}
return tables;
}
}

@Override
public Set<Column> readTableColumn(String database, String table) throws SQLException {
try (Connection conn = getConn()) {
Set<Column> columnSet = new HashSet<>();
DatabaseMetaData metadata = conn.getMetaData();
Map<String, List<ForeignKey>> importedKeys = getImportedKeys(metadata, database, table);
final String catalog = conn.getCatalog();
try (ResultSet columns = metadata.getColumns(catalog, database, table, "%")) {
while (columns.next()) {
Column column = readTableColumn(columns);
column.setForeignKeys(importedKeys.get(column.columnKey()));
columnSet.add(column);
}
}
return columnSet;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ ACCESS:
BIG_QUERY:
db-type: BIG_QUERY
name: BIG_QUERY
literal-quote: "`"
identifier-quote: "'"
literal-end-quote: "`"
identifier-end-quote: "'"
adapter-class: "datart.data.provider.jdbc.adapters.BigQueryDataProviderAdapter"
driver-class: com.simba.googlebigquery.jdbc.Driver
url-prefix: jdbc:bigquery://

Expand Down
8 changes: 4 additions & 4 deletions frontend/src/__tests__/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ describe('Test getQueryData', () => {
viewId: 'e83684bc8e92431dbfb164195abe443e',
aggregators: [
{
alias: 'COUNT(education)',
alias: 'COUNT_education',
column: ['education'],
sqlOperator: 'COUNT',
},
{ alias: 'SUM(chartAge)', column: ['chartAge'], sqlOperator: 'SUM' },
{ alias: 'SUM_chartAge', column: ['chartAge'], sqlOperator: 'SUM' },
],
groups: [{ alias: 'age', column: ['age'] }],
filters: [
Expand Down Expand Up @@ -163,12 +163,12 @@ describe('Test getQueryData', () => {
viewId: '6236737d64954c8bac6cbaf478e17ff2',
aggregators: [
{
alias: 'SUM(viewComputerField_age)',
alias: 'SUM_viewComputerField_age',
column: ['viewComputerField_age'],
sqlOperator: 'SUM',
},
{
alias: 'SUM(root.salary)',
alias: 'SUM_root.salary',
column: ['root', 'salary'],
sqlOperator: 'SUM',
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/models/ChartDataRequestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class ChartDataRequestBuilder {
return c.colName;
}
if (c.aggregate) {
return `${c.aggregate}(${c.colName})`;
return `${c.aggregate}_${c.colName}`;
}
return c.colName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ describe('ChartDataRequestBuild Test', () => {
const requestParams = builder.build();

expect(requestParams.aggregators).toEqual([
{ alias: 'AVG(amount)', column: ['amount'], sqlOperator: 'AVG' },
{ alias: 'SUM(sub-amount)', column: ['sub-amount'], sqlOperator: 'SUM' },
{ alias: 'COUNT(total)', column: ['total'], sqlOperator: 'COUNT' },
{ alias: 'AVG_amount', column: ['amount'], sqlOperator: 'AVG' },
{ alias: 'SUM_sub-amount', column: ['sub-amount'], sqlOperator: 'SUM' },
{ alias: 'COUNT_total', column: ['total'], sqlOperator: 'COUNT' },
{ alias: 'sex', column: ['sex'], sqlOperator: undefined },
{ alias: 'age', column: ['age'], sqlOperator: undefined },
]);
Expand Down Expand Up @@ -258,17 +258,17 @@ describe('ChartDataRequestBuild Test', () => {

expect(requestParams.aggregators).toEqual([
{
alias: 'AVG(dad.amount)',
alias: 'AVG_dad.amount',
column: ['dad', 'amount'],
sqlOperator: 'AVG',
},
{
alias: 'SUM(sub-amount)',
alias: 'SUM_sub-amount',
column: ['sub-amount'],
sqlOperator: 'SUM',
},
{
alias: 'COUNT(total)',
alias: 'COUNT_total',
column: ['total'],
sqlOperator: 'COUNT',
},
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('ChartDataRequestBuild Test', () => {
const requestParams = builder.build();

expect(requestParams.aggregators).toEqual([
{ alias: 'AVG(amount)', column: ['amount'], sqlOperator: 'AVG' },
{ alias: 'AVG_amount', column: ['amount'], sqlOperator: 'AVG' },
]);
});

Expand Down Expand Up @@ -408,7 +408,7 @@ describe('ChartDataRequestBuild Test', () => {

expect(requestParams.aggregators).toEqual([
{
alias: 'AVG(dad.amount)',
alias: 'AVG_dad.amount',
column: ['dad', 'amount'],
sqlOperator: 'AVG',
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/models/__tests__/ChartDataSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('ChartDataSet Tests', () => {
);
expect(
dataset.getFieldKey({ colName: 'columnName', aggregate: 'AVG' } as any),
).toEqual('AVG(COLUMNNAME)');
).toEqual('AVG_COLUMNNAME');
expect(dataset.getFieldIndex({ colName: 'column1' } as any)).toEqual(0);
expect(
dataset.getFieldIndex({ colName: 'column1', aggregate: 'AVG' } as any),
Expand Down
Loading