Skip to content

Commit

Permalink
Use unique table name for TestKuduIntegrationDecimalColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen2112 committed Mar 23, 2021
1 parent 9a3683d commit d4e6a29
Showing 1 changed file with 38 additions and 52 deletions.
Expand Up @@ -16,6 +16,8 @@
import io.trino.testing.AbstractTestQueryFramework;
import io.trino.testing.MaterializedResult;
import io.trino.testing.QueryRunner;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TrinoSqlExecutor;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -65,70 +67,54 @@ public void testCreateTableWithDecimalColumn()
@Test
public void testDecimalColumn()
{
assertUpdate("CREATE TABLE IF NOT EXISTS test_decimal (" +
"id INT WITH (primary_key=true), " +
"col_decimal decimal(10, 6)" +
") WITH (" +
" partition_by_hash_columns = ARRAY['id'], " +
" partition_by_hash_buckets = 2" +
")");

assertUpdate("INSERT INTO test_decimal VALUES (0, 0.0), (2, 2.2), (1, 1.1)", 3);
assertQuery("SELECT * FROM test_decimal WHERE col_decimal = 1.1", "VALUES (1, 1.1)");
assertUpdate("DELETE FROM test_decimal WHERE col_decimal = 1.1", 1);

assertUpdate("DROP TABLE test_decimal");
try (TestTable testTable = new TestTable(
new TrinoSqlExecutor(getQueryRunner()),
"test_decimal",
"(id INT WITH (primary_key=true), col_decimal decimal(10, 6)) " +
"WITH (partition_by_hash_columns = ARRAY['id'], partition_by_hash_buckets = 2)")) {
assertUpdate(format("INSERT INTO %s VALUES (0, 0.0), (2, 2.2), (1, 1.1)", testTable.getName()), 3);
assertQuery(format("SELECT * FROM %s WHERE col_decimal = 1.1", testTable.getName()), "VALUES (1, 1.1)");
assertUpdate(format("DELETE FROM %s WHERE col_decimal = 1.1", testTable.getName()), 1);
assertQueryReturnsEmptyResult(format("SELECT * FROM %s WHERE col_decimal = 1.1", testTable.getName()));
}
}

@Test
public void testDeleteByPrimaryKeyDecimalColumn()
{
assertUpdate("CREATE TABLE IF NOT EXISTS test_decimal (" +
"decimal_id decimal(18, 3) WITH (primary_key=true), " +
"col_decimal decimal(18, 3)" +
") WITH (" +
" partition_by_hash_columns = ARRAY['decimal_id'], " +
" partition_by_hash_buckets = 2" +
")");

assertUpdate("INSERT INTO test_decimal VALUES (1.1, 1.1), (2.2, 2.2)", 2);
assertUpdate("DELETE FROM test_decimal WHERE decimal_id = 2.2", 1);
assertQuery("SELECT * FROM test_decimal", "VALUES (1.1, 1.1)");

assertUpdate("DROP TABLE test_decimal");
try (TestTable testTable = new TestTable(
new TrinoSqlExecutor(getQueryRunner()),
"test_decimal",
"(decimal_id decimal(18, 3) WITH (primary_key=true), col_decimal decimal(18, 3)) " +
"WITH (partition_by_hash_columns = ARRAY['decimal_id'], partition_by_hash_buckets = 2)")) {
assertUpdate(format("INSERT INTO %s VALUES (1.1, 1.1), (2.2, 2.2)", testTable.getName()), 2);
assertUpdate(format("DELETE FROM %s WHERE decimal_id = 2.2", testTable.getName()), 1);
assertQuery(format("SELECT * FROM %s", testTable.getName()), "VALUES (1.1, 1.1)");
}
}

private void doTestCreateTableWithDecimalColumn(TestDecimal decimal)
{
String tableName = decimal.getTableName();
String dropTable = format("DROP TABLE IF EXISTS %s", tableName);
String createTable = format(
"CREATE TABLE %s (\n" +
" id INT WITH (primary_key=true),\n" +
" dec DECIMAL(%s, %s)\n" +
") WITH (\n" +
" partition_by_hash_columns = ARRAY['id'],\n" +
" partition_by_hash_buckets = 2\n" +
")",
tableName,
String tableDefinition = format(
"(id INT WITH (primary_key=true), dec DECIMAL(%s, %s)) " +
"WITH (partition_by_hash_columns = ARRAY['id'], partition_by_hash_buckets = 2)",
decimal.precision,
decimal.scale);

assertUpdate(dropTable);
assertUpdate(createTable);

String fullPrecisionValue = "1234567890.1234567890123456789012345678";
int maxScale = decimal.precision - 10;
int valuePrecision = decimal.precision - maxScale + Math.min(maxScale, decimal.scale);
String insertValue = fullPrecisionValue.substring(0, valuePrecision + 1);
assertUpdate(format("INSERT INTO %s VALUES(1, DECIMAL '%s')", tableName, insertValue), 1);

MaterializedResult result = computeActual(format("SELECT id, CAST((dec - (DECIMAL '%s')) as DOUBLE) FROM %s", insertValue, tableName));
assertEquals(result.getRowCount(), 1);
Object obj = result.getMaterializedRows().get(0).getField(1);
assertTrue(obj instanceof Double);
Double actual = (Double) obj;
assertEquals(0, actual, 0.3 * Math.pow(0.1, decimal.scale), "p=" + decimal.precision + ",s=" + decimal.scale + " => " + actual + ",insert = " + insertValue);
try (TestTable testTable = new TestTable(new TrinoSqlExecutor(getQueryRunner()), decimal.getTableName(), tableDefinition)) {
String fullPrecisionValue = "1234567890.1234567890123456789012345678";
int maxScale = decimal.precision - 10;
int valuePrecision = decimal.precision - maxScale + Math.min(maxScale, decimal.scale);
String insertValue = fullPrecisionValue.substring(0, valuePrecision + 1);
assertUpdate(format("INSERT INTO %s VALUES(1, DECIMAL '%s')", testTable.getName(), insertValue), 1);

MaterializedResult result = computeActual(format("SELECT id, CAST((dec - (DECIMAL '%s')) as DOUBLE) FROM %s", insertValue, testTable.getName()));
assertEquals(result.getRowCount(), 1);
Object obj = result.getMaterializedRows().get(0).getField(1);
assertTrue(obj instanceof Double);
Double actual = (Double) obj;
assertEquals(0, actual, 0.3 * Math.pow(0.1, decimal.scale), "p=" + decimal.precision + ",s=" + decimal.scale + " => " + actual + ",insert = " + insertValue);
}
}

static class TestDecimal
Expand Down

0 comments on commit d4e6a29

Please sign in to comment.