diff --git a/omniscidb/QueryBuilder/QueryBuilder.cpp b/omniscidb/QueryBuilder/QueryBuilder.cpp index 47f5c0e474..7fd84a473c 100644 --- a/omniscidb/QueryBuilder/QueryBuilder.cpp +++ b/omniscidb/QueryBuilder/QueryBuilder.cpp @@ -828,7 +828,7 @@ BuilderExpr BuilderExpr::cast(const Type* new_type) const { return {builder_, expr_->cast(new_type), "", true}; } } else if (expr_->type()->isDate()) { - if (new_type->isDate() || new_type->isTimestamp()) { + if (new_type->isNumber() || new_type->isDate() || new_type->isTimestamp()) { return {builder_, expr_->cast(new_type), "", true}; } } else if (expr_->type()->isTime()) { diff --git a/omniscidb/Tests/QueryBuilderTest.cpp b/omniscidb/Tests/QueryBuilderTest.cpp index 359dd21ea7..80ae4b12eb 100644 --- a/omniscidb/Tests/QueryBuilderTest.cpp +++ b/omniscidb/Tests/QueryBuilderTest.cpp @@ -321,6 +321,21 @@ class QueryBuilderTest : public TestSuite { {"col_vc_10", ctx().varChar(10)}, }); + createTable("test_date", + { + {"col_bi", ctx().int64()}, + {"col_i", ctx().int32()}, + {"col_f", ctx().fp32()}, + {"col_d", ctx().fp64()}, + {"col_dec", ctx().decimal64(10, 2)}, + {"col_b", ctx().boolean()}, + {"col_str", ctx().text()}, + {"col_date", ctx().date32(hdk::ir::TimeUnit::kDay)}, + }); + insertCsvValues("test_date", + "1,1,0.75,0.13444545,50.02,false,some_text,2000-01-01\n" + "2,2,30.82,0.461,7.05,true,a_text,2015-03-18\n"); + createTable("sort", {{"x", ctx().int32()}, {"y", ctx().int32()}, {"z", ctx().int32()}}); insertCsvValues("sort", @@ -348,6 +363,7 @@ class QueryBuilderTest : public TestSuite { dropTable("join1"); dropTable("join2"); dropTable("withNull"); + dropTable("test_date"); } void compare_res_fields(const ExecutionResult& res, @@ -493,6 +509,28 @@ TEST_F(QueryBuilderTest, Arithmetics) { compare_res_data(res, std::vector({0, NULL_BIGINT})); } +TEST_F(QueryBuilderTest, DateToInt) { + QueryBuilder builder(ctx(), schema_mgr_, configPtr()); + + auto tinfo_a = builder.scan("test_date"); + + auto dag = tinfo_a.proj(tinfo_a.ref("col_date").cast("int32")).finalize(); + auto res = runQuery(std::move(dag)); + LOG(ERROR) << "res: " << res.toString(); + LOG(ERROR) << "res: " << toArrow(res)->ToString(); + compare_res_data(res, std::vector({946684800, 1426636800})); + + dag = tinfo_a.proj(tinfo_a.ref("col_date")).finalize(); + res = runQuery(std::move(dag)); + LOG(ERROR) << "res: " << res.toString(); + LOG(ERROR) << "res: " << toArrow(res)->ToString(); + compare_res_data( + res, + std::vector( + {dateTimeParse("2000-01-01", TimeUnit::kDay), + dateTimeParse("2015-03-18", TimeUnit::kDay)})); +} + TEST_F(QueryBuilderTest, Arithmetics2) { QueryBuilder builder(ctx(), schema_mgr_, configPtr());