generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
datacube: add sample data and improve testing (#3144)
- Loading branch information
Showing
14 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...a-cube/src/main/java/org/finos/legend/engine/repl/dataCube/shared/DataCubeSampleData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed 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.finos.legend.engine.repl.dataCube.shared; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.eclipse.collections.api.list.MutableList; | ||
import org.eclipse.collections.impl.factory.Lists; | ||
import org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager; | ||
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.DatabaseConnection; | ||
import org.finos.legend.engine.repl.client.Client; | ||
import org.finos.legend.engine.repl.dataCube.commands.DataCube; | ||
import org.finos.legend.engine.repl.relational.schema.Table; | ||
import org.finos.legend.engine.repl.relational.shared.ConnectionHelper; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
import static org.finos.legend.engine.repl.relational.schema.MetadataReader.getTables; | ||
|
||
public class DataCubeSampleData | ||
{ | ||
public static final DataCubeSampleData SPORT = new DataCubeSampleData("sport", "sample__sport", "org/finos/legend/engine/repl/dataCube/walkthrough/sport-data.csv", Lists.mutable.with("Athlete", "Age", "Country", "Year", "Date", "Sport", "Gold", "Silver", "Bronze")); | ||
public static final DataCubeSampleData TREE = new DataCubeSampleData("tree", "sample__tree", "org/finos/legend/engine/repl/dataCube/walkthrough/tree-data.csv", Lists.mutable.with("city", "country", "year", "tree")); | ||
|
||
public final String name; | ||
public final String tableName; | ||
public final String csvFilePath; | ||
public final MutableList<String> expectedColumns; | ||
|
||
public DataCubeSampleData(String name, String tableName, String csvFilePath, MutableList<String> expectedColumns) | ||
{ | ||
this.name = name; | ||
this.tableName = tableName; | ||
this.csvFilePath = csvFilePath; | ||
this.expectedColumns = expectedColumns; | ||
} | ||
|
||
public void load(Client client) | ||
{ | ||
DatabaseConnection databaseConnection = ConnectionHelper.getDatabaseConnection(client.getModelState().parse(), DataCube.getLocalConnectionPath()); | ||
try ( | ||
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.csvFilePath); | ||
Connection connection = ConnectionHelper.getConnection(databaseConnection, client.getPlanExecutor()); | ||
Statement statement = connection.createStatement()) | ||
{ | ||
MutableList<Table> tables = getTables(connection); | ||
if (tables.anySatisfy(t -> t.name.equals(this.tableName))) | ||
{ | ||
statement.executeUpdate(DatabaseManager.fromString(databaseConnection.type.name()).relationalDatabaseSupport().dropTable(tableName)); | ||
} | ||
Path tempFile = Files.createTempFile("sample-data" + this.name, ".csv"); | ||
FileOutputStream fos = new FileOutputStream(tempFile.toFile()); | ||
IOUtils.copy(Objects.requireNonNull(inputStream, "Can't extract sample data '" + this.name + "' from " + this.csvFilePath), fos); | ||
statement.executeUpdate(DatabaseManager.fromString(databaseConnection.type.name()).relationalDatabaseSupport().load(tableName, tempFile.toString())); | ||
|
||
// post check | ||
tables = getTables(connection); | ||
Table table = tables.detect(t -> t.name.equals(this.tableName)); | ||
if (!Arrays.equals(table.columns.collect(column -> column.name).toArray(), this.expectedColumns.toArray())) | ||
{ | ||
throw new RuntimeException("Sample data '" + this.name + "' does not have the expected columns " + this.expectedColumns.makeString("(", ",", ")") + " (got: " + table.columns.collect(column -> column.name).makeString(",") + ")"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...a-cube/src/main/resources/org/finos/legend/engine/repl/dataCube/walkthrough/tree-data.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
city,country,year,tree | ||
NYC,USA,2011,50 | ||
HAN,VNM,2011,12 | ||
NYC,USA,2000,-51 | ||
PEK,CHN,2012,-10 | ||
SHA,CHN,2012,-16 | ||
SAN,USA,2000,-20 | ||
HAN,VNM,2010,-5 | ||
SAN,USA,2011,12 | ||
SGP,SGP,2000,30 | ||
LDN,UK,2011,30 | ||
SAN,USA,2011,-25 | ||
NYC,USA,2000,100 | ||
NYC,USA,2012,-76 | ||
SAN,USA,2011,-17 | ||
PEK,CHN,2011,77 | ||
SHA,CHN,2000,-11 | ||
SGP,SGP,2000,20 | ||
HAN,VNM,2012,30 | ||
HAN,VNM,2010,19 | ||
LDN,UK,2012,27 | ||
NYC,USA,2012,54 | ||
SHA,CHN,2012,6 | ||
PEK,CHN,2000,60 | ||
SGP,SGP,2011,50 | ||
LDN,UK,2000,16 | ||
SGP,SGP,2012,-10 | ||
HAN,VNM,2012,-11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters