Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit cc12da5

Browse files
committed
Merge branch 'master' into jenkins
2 parents 2f6dd56 + 4eb7191 commit cc12da5

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/include/type/value_factory.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,12 @@ class ValueFactory {
214214
std::string str = value.ToString();
215215
int64_t bigint = 0;
216216
try {
217-
bigint = std::stoll(str);
217+
bigint = stoll(str);
218218
} catch (std::out_of_range &e) {
219219
throw Exception(ExceptionType::OUT_OF_RANGE,
220220
"Numeric value out of range.");
221+
} catch(std::invalid_argument &e) {
222+
throw Exception("Invalid input syntax for bigint: \'" + str + "\'");
221223
}
222224
if (bigint > PELOTON_INT64_MAX || bigint < PELOTON_INT64_MIN)
223225
throw Exception(ExceptionType::OUT_OF_RANGE,
@@ -262,11 +264,14 @@ class ValueFactory {
262264
std::string str = value.ToString();
263265
int32_t integer = 0;
264266
try {
265-
integer = std::stoi(str);
267+
integer = stoi(str);
266268
} catch (std::out_of_range &e) {
267269
throw Exception(ExceptionType::OUT_OF_RANGE,
268270
"Numeric value out of range.");
271+
} catch (std::invalid_argument &e) {
272+
throw Exception("Invalid input syntax for integer: \'" + str + "\'");
269273
}
274+
270275
if (integer > PELOTON_INT32_MAX || integer < PELOTON_INT32_MIN)
271276
throw Exception(ExceptionType::OUT_OF_RANGE,
272277
"Numeric value out of range.");
@@ -317,10 +322,12 @@ class ValueFactory {
317322
std::string str = value.ToString();
318323
int16_t smallint = 0;
319324
try {
320-
smallint = std::stoi(str);
325+
smallint = stoi(str);
321326
} catch (std::out_of_range &e) {
322327
throw Exception(ExceptionType::OUT_OF_RANGE,
323328
"Numeric value out of range.");
329+
} catch(std::invalid_argument &e) {
330+
throw Exception("Invalid input syntax for smallint: \'" + str + "\'");
324331
}
325332
if (smallint < PELOTON_INT16_MIN)
326333
throw Exception(ExceptionType::OUT_OF_RANGE,
@@ -375,10 +382,12 @@ class ValueFactory {
375382
std::string str = value.ToString();
376383
int8_t tinyint = 0;
377384
try {
378-
tinyint = std::stoi(str);
385+
tinyint = stoi(str);
379386
} catch (std::out_of_range &e) {
380387
throw Exception(ExceptionType::OUT_OF_RANGE,
381388
"Numeric value out of range.");
389+
} catch(std::invalid_argument &e) {
390+
throw Exception("Invalid input syntax for tinyint: \'" + str + "\'");
382391
}
383392
if (tinyint < PELOTON_INT8_MIN)
384393
throw Exception(ExceptionType::OUT_OF_RANGE,
@@ -413,10 +422,12 @@ class ValueFactory {
413422
std::string str = value.ToString();
414423
double res = 0;
415424
try {
416-
res = std::stod(str);
425+
res = stod(str);
417426
} catch (std::out_of_range &e) {
418427
throw Exception(ExceptionType::OUT_OF_RANGE,
419428
"Numeric value out of range.");
429+
} catch(std::invalid_argument &e) {
430+
throw Exception("Invalid input syntax for decimal: \'" + str + "\'");
420431
}
421432
if (res > PELOTON_DECIMAL_MAX || res < PELOTON_DECIMAL_MIN)
422433
throw Exception(ExceptionType::OUT_OF_RANGE,

test/sql/insert_sql_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,44 @@ TEST_F(InsertSQLTests, UniqueColumn) {
567567
txn_manager.CommitTransaction(txn);
568568
}
569569

570+
TEST_F(InsertSQLTests, BadTypes) {
571+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
572+
auto txn = txn_manager.BeginTransaction();
573+
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
574+
txn_manager.CommitTransaction(txn);
575+
std::string error_message;
576+
std::unique_ptr<optimizer::AbstractOptimizer> optimizer(
577+
new optimizer::Optimizer());
578+
579+
std::string create_table(
580+
"CREATE TABLE foo (id1 int, id2 bigint,"
581+
"id3 smallint, id4 tinyint,"
582+
"id5 decimal);");
583+
TestingSQLUtil::ExecuteSQLQuery(create_table);
584+
// Insert an unconvertible int.
585+
std::string query("INSERT INTO(id) foo VALUES('h');");
586+
txn = txn_manager.BeginTransaction();
587+
EXPECT_THROW(TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn),
588+
peloton::Exception);
589+
query = "INSERT INTO foo(id2) VALUES('h');";
590+
EXPECT_THROW(TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn),
591+
peloton::Exception);
592+
query = "INSERT INTO foo(id3) VALUES('h');";
593+
EXPECT_THROW(TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn),
594+
peloton::Exception);
595+
query = "INSERT INTO foo(id4) VALUES('h');";
596+
EXPECT_THROW(TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn),
597+
peloton::Exception);
598+
query = "INSERT INTO foo(id5) VALUES('h');";
599+
EXPECT_THROW(TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn),
600+
peloton::Exception);
601+
602+
// free the database just created
603+
txn = txn_manager.BeginTransaction();
604+
catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
605+
txn_manager.CommitTransaction(txn);
606+
}
607+
570608
TEST_F(InsertSQLTests, NonExistentTable) {
571609
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
572610
auto txn = txn_manager.BeginTransaction();

0 commit comments

Comments
 (0)