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

Add DATE TO DATE and TIMESTAMP TO DATE casting functions. #3220

Merged
merged 9 commits into from
Apr 8, 2024
Merged
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
49 changes: 43 additions & 6 deletions src/function/vector_cast_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ static void resolveNestedVector(std::shared_ptr<ValueVector> inputVector, ValueV
}

// non-nested types
scalar_func_exec_t func = CastFunction::bindCastFunction<CastChildFunctionExecutor>("CAST",
inputType->getLogicalTypeID(), resultType->getLogicalTypeID())
->execFunc;
std::vector<std::shared_ptr<ValueVector>> childParams{inputVector};
dataPtr->numOfEntries = numOfEntries;
func(childParams, *resultVector, (void*)dataPtr);
if (inputType->getLogicalTypeID() != resultType->getLogicalTypeID()) {
mxwli marked this conversation as resolved.
Show resolved Hide resolved
scalar_func_exec_t func = CastFunction::bindCastFunction<CastChildFunctionExecutor>("CAST",
inputType->getLogicalTypeID(), resultType->getLogicalTypeID())
->execFunc;
std::vector<std::shared_ptr<ValueVector>> childParams{inputVector};
dataPtr->numOfEntries = numOfEntries;
func(childParams, *resultVector, (void*)dataPtr);
} else {
for (auto i = 0u; i < numOfEntries; i++) {
resultVector->copyFromVectorData(i, inputVector.get(), i);
}
}
}

static void nestedTypesCastExecFunction(const std::vector<std::shared_ptr<ValueVector>>& params,
Expand Down Expand Up @@ -586,6 +592,34 @@ static std::unique_ptr<ScalarFunction> bindCastBetweenNested(const std::string&
}
}

template<typename EXECUTOR = UnaryFunctionExecutor, typename DST_TYPE>
static std::unique_ptr<ScalarFunction> bindCastToDateFunction(const std::string& functionName,
LogicalTypeID sourceTypeID, LogicalTypeID dstTypeID) {
scalar_func_exec_t func;
switch (sourceTypeID) {
case LogicalTypeID::TIMESTAMP_MS:
func = ScalarFunction::UnaryExecFunction<timestamp_ms_t, DST_TYPE, CastToDate, EXECUTOR>;
break;
case LogicalTypeID::TIMESTAMP_NS:
func = ScalarFunction::UnaryExecFunction<timestamp_ns_t, DST_TYPE, CastToDate, EXECUTOR>;
break;
case LogicalTypeID::TIMESTAMP_SEC:
func = ScalarFunction::UnaryExecFunction<timestamp_sec_t, DST_TYPE, CastToDate, EXECUTOR>;
break;
case LogicalTypeID::TIMESTAMP_TZ:
case LogicalTypeID::TIMESTAMP:
func = ScalarFunction::UnaryExecFunction<timestamp_t, DST_TYPE, CastToDate, EXECUTOR>;
break;
// LCOV_EXCL_START
default:
mxwli marked this conversation as resolved.
Show resolved Hide resolved
throw ConversionException{stringFormat("Unsupported casting function from {} to {}.",
LogicalTypeUtils::toString(sourceTypeID), LogicalTypeUtils::toString(dstTypeID))};
// LCOV_EXCL_END
}
return std::make_unique<ScalarFunction>(functionName, std::vector<LogicalTypeID>{sourceTypeID},
LogicalTypeID::DATE, func);
}

template<typename EXECUTOR = UnaryFunctionExecutor, typename DST_TYPE>
static std::unique_ptr<ScalarFunction> bindCastToTimestampFunction(const std::string& functionName,
LogicalTypeID sourceTypeID, LogicalTypeID dstTypeID) {
Expand Down Expand Up @@ -683,6 +717,9 @@ std::unique_ptr<ScalarFunction> CastFunction::bindCastFunction(const std::string
return bindCastToNumericFunction<uint8_t, CastToUInt8, EXECUTOR>(functionName, sourceTypeID,
targetTypeID);
}
case LogicalTypeID::DATE: {
return bindCastToDateFunction<EXECUTOR, date_t>(functionName, sourceTypeID, targetTypeID);
}
case LogicalTypeID::TIMESTAMP_NS: {
return bindCastToTimestampFunction<EXECUTOR, timestamp_ns_t>(functionName, sourceTypeID,
targetTypeID);
Expand Down
28 changes: 28 additions & 0 deletions src/include/function/cast/functions/cast_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ inline void CastDateToTimestamp::operation(common::date_t& input, common::timest
result.value /= common::Interval::MICROS_PER_SEC;
}

struct CastToDate {
template<typename T>
static inline void operation(T& input, common::date_t& result);
};

template<>
inline void CastToDate::operation(common::timestamp_t& input, common::date_t& result) {
result = common::Timestamp::getDate(input);
}

template<>
inline void CastToDate::operation(common::timestamp_ns_t& input, common::date_t& result) {
auto tmp = common::Timestamp::fromEpochNanoSeconds(input.value);
operation<common::timestamp_t>(tmp, result);
}

template<>
inline void CastToDate::operation(common::timestamp_ms_t& input, common::date_t& result) {
auto tmp = common::Timestamp::fromEpochMilliSeconds(input.value);
operation<common::timestamp_t>(tmp, result);
}

template<>
inline void CastToDate::operation(common::timestamp_sec_t& input, common::date_t& result) {
auto tmp = common::Timestamp::fromEpochSeconds(input.value);
operation<common::timestamp_t>(tmp, result);
}

struct CastToDouble {
template<typename T>
static inline void operation(T& input, double& result) {
Expand Down
34 changes: 34 additions & 0 deletions test/test_files/common/todatecast.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-GROUP ToDateCast
-DATASET CSV empty

--

-CASE ToDateCastTest

-STATEMENT RETURN CAST(date('2024-04-05'), 'date')
---- 1
2024-04-05

-STATEMENT RETURN CAST(timestamp('2024-04-05 23:59:59.999'), 'date')
---- 1
2024-04-05

-STATEMENT RETURN CAST(CAST('2024-04-05', 'TIMESTAMP_MS'), 'date')
mxwli marked this conversation as resolved.
Show resolved Hide resolved
---- 1
2024-04-05

-STATEMENT RETURN CAST(CAST('2024-04-05', 'TIMESTAMP_NS'), 'date')
---- 1
2024-04-05

-STATEMENT RETURN CAST(CAST('2024-04-05 01:25:30', 'TIMESTAMP_SEC'), 'date')
---- 1
2024-04-05

-STATEMENT CREATE NODE TABLE todatecast(id serial, col1 struct(a date, b int32), primary key(id))
---- ok
-STATEMENT CREATE (:todatecast {col1: {a: date('2023-04-05'), b: 1}})
---- ok
-STATEMENT MATCH (t:todatecast) RETURN t.col1
---- 1
{a: 2023-04-05, b: 1}