Skip to content

feat: Implement support for marshalling columns to velox Timestamp type. - #15

Merged
anlowee merged 15 commits into
presto-0.293-clp-connectorfrom
timestamp-marshalling-rebased-2
Jul 11, 2025
Merged

feat: Implement support for marshalling columns to velox Timestamp type.#15
anlowee merged 15 commits into
presto-0.293-clp-connectorfrom
timestamp-marshalling-rebased-2

Conversation

@gibber9809

@gibber9809 gibber9809 commented Jul 10, 2025

Copy link
Copy Markdown

Description

This PR adds support for marshalling timestamps in clp-s to the velox Timestamp type. Since clp-s doesn't have a standard timestamp representation (timestamps can currently be strings, floats, ints, and have different time precisions), we use some heuristics to convert the various timestamp formats to nanosecond precision.

We should be able to eliminate the heuristics in a future PR once we move to a standard nanosecond precision integer (likely delta-encoded) timestamp representation in a future version of the clp-s archive format.

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

  • Xiaochong manually tested an earlier version of this PR
  • Modified connector test to use basic timestamp marshalling
  • Added a new test to exercise timestamp marshalling of various types and precisions

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Added support for handling and loading TIMESTAMP columns, enabling proper recognition and processing of timestamp data.
  • Bug Fixes
    • Updated test cases to validate TIMESTAMP column handling, ensuring correct behaviour for timestamp data in queries.
  • Tests
    • Introduced new test verifying timestamp marshalling with varied timestamp formats.
    • Added example test data covering multiple timestamp representations for improved validation.

@coderabbitai

coderabbitai Bot commented Jul 10, 2025

Copy link
Copy Markdown

Walkthrough

Support for the TIMESTAMP type was added throughout the CLP connector. This includes extending type mappings, updating the column type enumeration, implementing timestamp vector loading with precision detection and conversion, and modifying tests to use actual timestamp types and values. A new test data file with varied timestamp formats was also added. No changes were made to public API signatures.

Changes

Files/Areas Change Summary
velox/connectors/clp/ClpDataSource.cpp Added mapping for TypeKind::TIMESTAMP to search_lib::ColumnType::Timestamp in type handling logic.
velox/connectors/clp/search_lib/ClpCursor.cpp Added handling for ColumnType::Timestamp, mapping it to appropriate literal types during archive loading.
velox/connectors/clp/search_lib/ClpCursor.h Extended ColumnType enum with a new Timestamp value and reordered enumerators.
velox/connectors/clp/search_lib/ClpVectorLoader.cpp, velox/connectors/clp/search_lib/ClpVectorLoader.h Implemented timestamp vector loading, including precision estimation, conversion helpers, and a new templated method for population. Added explicit template instantiations.
velox/connectors/clp/tests/ClpConnectorTest.cpp Changed test column types and expected values from VARCHAR to TIMESTAMP, updated assertions to use timestamp objects, and added a new test for timestamp marshalling.
velox/connectors/clp/tests/examples/test_3.ndjson Added a new test data file containing diverse timestamp formats (ISO string, float seconds, microseconds, nanoseconds).

Sequence Diagram(s)

sequenceDiagram
    participant QueryEngine
    participant ClpDataSource
    participant ClpCursor
    participant ClpVectorLoader

    QueryEngine->>ClpDataSource: Request data (includes TIMESTAMP columns)
    ClpDataSource->>ClpCursor: Map Velox TIMESTAMP to ColumnType::Timestamp
    ClpCursor->>ClpVectorLoader: Request vector loading for ColumnType::Timestamp
    ClpVectorLoader->>ClpVectorLoader: Detect timestamp precision, convert raw values
    ClpVectorLoader-->>ClpCursor: Return populated Timestamp vector
    ClpCursor-->>ClpDataSource: Return loaded data
    ClpDataSource-->>QueryEngine: Return data with TIMESTAMP columns
Loading

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9e47c53 and 801e118.

📒 Files selected for processing (1)
  • velox/connectors/clp/search_lib/ClpVectorLoader.cpp (4 hunks)
🧰 Additional context used
🧠 Learnings (1)
velox/connectors/clp/search_lib/ClpVectorLoader.cpp (2)
Learnt from: gibber9809
PR: y-scope/velox#15
File: velox/connectors/clp/search_lib/ClpVectorLoader.cpp:22-22
Timestamp: 2025-07-11T15:21:09.634Z
Learning: When analyzing include dependencies, pay special attention to types used in template parameters and enum values like `clp_s::NodeType`, as these may not be immediately obvious in a surface-level review but are essential for compilation.
Learnt from: anlowee
PR: y-scope/velox#1
File: CMakeLists.txt:458-483
Timestamp: 2025-03-10T05:55:18.722Z
Learning: When contributing to the Velox project, prefer adding new code on top of existing scripts rather than restructuring the original code organization, especially for dependency management.
🧬 Code Graph Analysis (1)
velox/connectors/clp/search_lib/ClpVectorLoader.cpp (1)
velox/connectors/clp/search_lib/ClpVectorLoader.h (2)
  • populateTimestampData (54-56)
  • rows (44-48)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build with GCC / Ubuntu release with resolve_dependency
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gibber9809
gibber9809 marked this pull request as ready for review July 10, 2025 20:20

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd this change? Did the file it was generated from change?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was compressed without the --timestamp-key timestamp flag previously. I've updated it so that "timestamp" is now a DateString in the archive instead of a VarString. The test_1.clps archive already seems to have been compressed with --timestamp-key timestamp.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 749d289 and a484bfd.

📒 Files selected for processing (6)
  • velox/connectors/clp/ClpDataSource.cpp (1 hunks)
  • velox/connectors/clp/search_lib/ClpCursor.cpp (1 hunks)
  • velox/connectors/clp/search_lib/ClpCursor.h (1 hunks)
  • velox/connectors/clp/search_lib/ClpVectorLoader.cpp (4 hunks)
  • velox/connectors/clp/search_lib/ClpVectorLoader.h (2 hunks)
  • velox/connectors/clp/tests/ClpConnectorTest.cpp (10 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
velox/connectors/clp/tests/ClpConnectorTest.cpp (1)
velox/serializers/tests/PrestoSerializerTest.cpp (1)
  • makeRowVector (804-821)
velox/connectors/clp/search_lib/ClpVectorLoader.cpp (1)
velox/connectors/clp/search_lib/ClpVectorLoader.h (2)
  • populateTimestampData (54-56)
  • rows (44-48)
🔇 Additional comments (5)
velox/connectors/clp/search_lib/ClpCursor.h (1)

52-60: LGTM! Clean enum extension for timestamp support.

The Timestamp enumerator is correctly added to the ColumnType enum with proper placement and formatting.

velox/connectors/clp/ClpDataSource.cpp (1)

96-98: LGTM! Correct type mapping for timestamp support.

The mapping from TypeKind::TIMESTAMP to ColumnType::Timestamp is properly implemented and follows the established pattern.

velox/connectors/clp/search_lib/ClpVectorLoader.h (1)

19-21: LGTM! Well-structured additions for timestamp vector loading.

The new includes are appropriate for timestamp support, and the templated populateTimestampData method follows established patterns in the class design.

Also applies to: 23-24, 53-56

velox/connectors/clp/tests/ClpConnectorTest.cpp (2)

39-41: LGTM! Clear constants for test timestamp values.

The constants provide a clear reference for the test timestamp "2025-04-30T08:50:05Z" used across multiple test cases.


174-174: LGTM! Comprehensive test updates for timestamp support.

All relevant test cases have been properly updated to use TIMESTAMP() type instead of VARCHAR() and the expected results correctly use Velox Timestamp objects. The test updates are consistent across all affected test methods.

Also applies to: 182-182, 199-211, 221-221, 234-234, 247-259, 269-269, 281-281, 297-309

Comment thread velox/connectors/clp/search_lib/ClpCursor.cpp
Comment on lines +282 to +294
if (nullptr != dynamic_cast<clp_s::Int64ColumnReader*>(columnReader_)) {
populateTimestampData<clp_s::NodeType::Integer>(rows, timestampVector);
} else if (
nullptr !=
dynamic_cast<clp_s::DateStringColumnReader*>(columnReader_)) {
populateTimestampData<clp_s::NodeType::DateString>(
rows, timestampVector);
} else if (
nullptr != dynamic_cast<clp_s::FloatColumnReader*>(columnReader_)) {
populateTimestampData<clp_s::NodeType::Float>(rows, timestampVector);
} else {
populateTimestampData<clp_s::NodeType::Unknown>(rows, timestampVector);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider optimizing type detection for better performance.

The current implementation uses multiple dynamic_cast operations in sequence, which could impact performance. Consider storing the column reader type information to avoid runtime type checking.

As a performance optimization, you could:

  1. Store the actual node type in the ClpVectorLoader constructor when the column reader is created
  2. Use a switch statement based on the stored type instead of dynamic casting
  3. This would eliminate the runtime overhead of type checking for each batch of rows
🤖 Prompt for AI Agents
In velox/connectors/clp/search_lib/ClpVectorLoader.cpp around lines 282 to 294,
the code uses multiple dynamic_cast checks to determine the column reader type,
which impacts performance. To fix this, capture and store the column reader's
node type once in the ClpVectorLoader constructor when the column reader is
created. Then replace the dynamic_cast sequence with a switch statement on the
stored node type in this function, calling populateTimestampData accordingly.
This removes repeated runtime type checks and improves efficiency.

Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp Outdated
Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp Outdated
default:
break;
}
if (columnReader_ == nullptr || false == supportedNodeType) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Simplify boolean comparison.

Use !supportedNodeType instead of false == supportedNodeType for better readability.

-  if (columnReader_ == nullptr || false == supportedNodeType) {
+  if (columnReader_ == nullptr || !supportedNodeType) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (columnReader_ == nullptr || false == supportedNodeType) {
if (columnReader_ == nullptr || !supportedNodeType) {
🤖 Prompt for AI Agents
In velox/connectors/clp/search_lib/ClpVectorLoader.cpp at line 164, simplify the
boolean comparison by replacing 'false == supportedNodeType' with
'!supportedNodeType' to improve code readability.

#include <utility>

#include "clp_s/ColumnReader.hpp"
#include "clp_s/SchemaTree.hpp"

@coderabbitai coderabbitai Bot Jul 10, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Remove unused include.

The include "clp_s/SchemaTree.hpp" appears to be unused in this file.

-#include "clp_s/SchemaTree.hpp"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#include "clp_s/SchemaTree.hpp"
#include <cmath>
#include <cstdint>
#include "velox/type/Timestamp.h"
🤖 Prompt for AI Agents
In velox/connectors/clp/search_lib/ClpVectorLoader.cpp at line 22, the include
directive for "clp_s/SchemaTree.hpp" is unused. Remove this include statement to
clean up the code and avoid unnecessary dependencies.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used for clp_s::NodeType

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for the definition of clp_s::NodeType to determine which header file defines it
echo "Searching for clp_s::NodeType definition..."
rg -A 3 -B 3 "enum.*NodeType" --type cpp --type hpp
echo ""
echo "Searching for NodeType in SchemaTree.hpp specifically..."
rg -A 10 -B 5 "NodeType" --files-with-matches | grep -i schema
echo ""
echo "Searching for files containing NodeType..."
rg -l "NodeType" --type cpp --type hpp

Length of output: 601


🏁 Script executed:

#!/bin/bash
# Search more broadly for NodeType definition and usage
echo "Searching for NodeType definition..."
rg -A 5 -B 5 "enum.*NodeType" --type cpp --type c
echo ""
echo "Searching for NodeType in header files..."
rg -A 5 -B 5 "enum.*NodeType" --type h
echo ""
echo "Searching for all files containing NodeType..."
rg -l "NodeType" --type cpp --type c --type h
echo ""
echo "Searching specifically for clp_s::NodeType..."
rg -A 5 -B 5 "clp_s::NodeType" --type cpp --type c --type h
echo ""
echo "Searching for include statements with SchemaTree..."
rg "#include.*SchemaTree" --type cpp --type c --type h

Length of output: 8722


@gibber9809 You're absolutely right, and I apologize for the incorrect assessment. Looking at the code more carefully, I can see that clp_s::NodeType is used extensively throughout the file:

  • As a template parameter: template <clp_s::NodeType Type>
  • In case statements: clp_s::NodeType::Float, clp_s::NodeType::Integer, clp_s::NodeType::DateString, clp_s::NodeType::Unknown
  • In function calls: populateTimestampData<clp_s::NodeType::Integer>(rows, timestampVector)
  • In explicit template instantiations at the end of the file

The #include "clp_s/SchemaTree.hpp" is clearly needed to provide the definition of the clp_s::NodeType enum. Thank you for the correction!


🧬 Code Graph Analysis Results

Relevant snippets from velox/connectors/clp/search_lib/ClpVectorLoader.h (lines 38-56):

ClpVectorLoader(
      clp_s::BaseColumnReader* columnReader,
      ColumnType nodeType,
      std::shared_ptr<std::vector<uint64_t>> filteredRowIndices);

void loadInternal(
      RowSet rows,
      ValueHook* hook,
      vector_size_t resultSize,
      VectorPtr* result) override;

template <typename T, typename VectorPtr>
void populateData(RowSet rows, VectorPtr vector);

template <clp_s::NodeType Type>
void populateTimestampData(
      RowSet rows,
      FlatVector<facebook::velox::Timestamp>* vector);
  • The constructor takes a pointer to clp_s::BaseColumnReader, a ColumnType (which corresponds to the node type), and a shared pointer to a vector of filtered row indices.
  • loadInternal is the main method to load data into a Velox vector, dispatching based on the node type.
  • populateData is a templated method to populate data for generic types.
  • populateTimestampData is a templated method specialized by clp_s::NodeType to populate timestamp data.

Relevant details from ClpVectorLoader.cpp (full file provided):

  • The file includes usage of clp_s::NodeType enum as a template parameter in populateTimestampData.
  • The loadInternal method uses nodeType_ (of type ColumnType) to decide how to load data.
  • For ColumnType::Timestamp, it checks the dynamic type of columnReader_ against clp_s::Int64ColumnReader, clp_s::DateStringColumnReader, and clp_s::FloatColumnReader and calls populateTimestampData with the corresponding clp_s::NodeType template argument.
  • The populateTimestampData template method is specialized for clp_s::NodeType::Float, Integer, DateString, and Unknown.
  • The file also defines helper functions to convert timestamps from various precisions to Velox Timestamp objects.
  • Explicit template instantiations for populateTimestampData with clp_s::NodeType values are provided at the end of the file.

These snippets and details show how clp_s::NodeType is used as a template parameter to specialize timestamp data loading behavior in ClpVectorLoader. This is directly relevant to understanding the usage of clp_s::NodeType in this file.


✏️ Learnings added
Learnt from: gibber9809
PR: y-scope/velox#15
File: velox/connectors/clp/search_lib/ClpVectorLoader.cpp:22-22
Timestamp: 2025-07-11T15:21:09.634Z
Learning: When analyzing include dependencies, pay special attention to types used in template parameters and enum values like `clp_s::NodeType`, as these may not be immediately obvious in a surface-level review but are essential for compilation.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (6)
velox/connectors/clp/search_lib/ClpCursor.cpp (1)

216-220: Add missing break statement to prevent fall-through.

The new ColumnType::Timestamp case is missing a break statement, which will cause execution to fall through to the default case.

        case ColumnType::Timestamp:
          columnDescriptor->set_matching_types(
              LiteralType::EpochDateT | LiteralType::IntegerT |
              LiteralType::FloatT);
+         break;
velox/connectors/clp/search_lib/ClpVectorLoader.cpp (5)

53-56: Fix incorrect nanoseconds constant.

The kEpochNanoseconds1971 constant has the wrong value. It should be 31536000000000000 (17 digits) instead of 31536000000000 (14 digits, same as microseconds).

-  constexpr int64_t kEpochNanoseconds1971{31536000000000};
+  constexpr int64_t kEpochNanoseconds1971{31536000000000000};

87-87: Use std::floor for consistency.

For consistency with other standard library functions, use std::floor instead of floor.

-  double seconds{floor(timestamp)};
+  double seconds{std::floor(timestamp)};

22-22: Remove unused include.

The include "clp_s/SchemaTree.hpp" appears to be unused in this file.

-#include "clp_s/SchemaTree.hpp"

164-164: Simplify boolean comparison.

Use !supportedNodeType instead of false == supportedNodeType for better readability.

-  if (columnReader_ == nullptr || false == supportedNodeType) {
+  if (columnReader_ == nullptr || !supportedNodeType) {

282-294: Consider optimizing type detection for better performance.

The current implementation uses multiple dynamic_cast operations in sequence, which could impact performance. Consider storing the column reader type information to avoid runtime type checking.

As a performance optimization, you could:

  1. Store the actual node type in the ClpVectorLoader constructor when the column reader is created
  2. Use a switch statement based on the stored type instead of dynamic casting
  3. This would eliminate the runtime overhead of type checking for each batch of rows
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a484bfd and 6c0c555.

📒 Files selected for processing (3)
  • velox/connectors/clp/search_lib/ClpCursor.cpp (1 hunks)
  • velox/connectors/clp/search_lib/ClpVectorLoader.cpp (4 hunks)
  • velox/connectors/clp/tests/ClpConnectorTest.cpp (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build with GCC / Ubuntu release with resolve_dependency
🔇 Additional comments (1)
velox/connectors/clp/tests/ClpConnectorTest.cpp (1)

29-42: Test changes properly validate timestamp functionality.

The test updates correctly change column types from VARCHAR to TIMESTAMP and use proper Timestamp objects with the defined constants. The epoch seconds value (1746003005) accurately represents "2025-04-30T08:50:05Z".

Also applies to: 173-175, 220-221, 233-234, 268-269, 280-281, 297-298

Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp
@gibber9809
gibber9809 requested a review from kirkrodrigues July 11, 2025 00:35
Comment thread velox/connectors/clp/search_lib/ClpCursor.h Outdated
@gibber9809
gibber9809 requested a review from anlowee July 11, 2025 15:33
Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp Outdated
Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp Outdated
Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp
Comment thread velox/connectors/clp/search_lib/ClpVectorLoader.cpp
Comment thread velox/connectors/clp/tests/ClpConnectorTest.cpp
@gibber9809
gibber9809 requested a review from anlowee July 11, 2025 18:21
anlowee
anlowee previously approved these changes Jul 11, 2025
@y-scope y-scope deleted a comment from gibber9809 Jul 11, 2025
Comment on lines +93 to +114
auto convertToVeloxTimestamp(int64_t timestamp) -> Timestamp {
switch (estimatePrecision(timestamp)) {
case TimestampPrecision::Nanoseconds:
break;
case TimestampPrecision::Microseconds:
timestamp *= Timestamp::kNanosecondsInMicrosecond;
break;
case TimestampPrecision::Milliseconds:
timestamp *= Timestamp::kNanosecondsInMillisecond;
break;
case TimestampPrecision::Seconds:
timestamp *= Timestamp::kNanosInSecond;
break;
}
int64_t seconds{timestamp / Timestamp::kNanosInSecond};
int64_t nanoseconds{timestamp - seconds * Timestamp::kNanosInSecond};
if (nanoseconds < 0) {
seconds -= 1;
nanoseconds += Timestamp::kNanosInSecond;
}
return Timestamp(seconds, static_cast<uint64_t>(nanoseconds));
}

@anlowee anlowee Jul 11, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto convertToVeloxTimestamp(int64_t timestamp) -> Timestamp {
switch (estimatePrecision(timestamp)) {
case TimestampPrecision::Nanoseconds:
break;
case TimestampPrecision::Microseconds:
timestamp *= Timestamp::kNanosecondsInMicrosecond;
break;
case TimestampPrecision::Milliseconds:
timestamp *= Timestamp::kNanosecondsInMillisecond;
break;
case TimestampPrecision::Seconds:
timestamp *= Timestamp::kNanosInSecond;
break;
}
int64_t seconds{timestamp / Timestamp::kNanosInSecond};
int64_t nanoseconds{timestamp - seconds * Timestamp::kNanosInSecond};
if (nanoseconds < 0) {
seconds -= 1;
nanoseconds += Timestamp::kNanosInSecond;
}
return Timestamp(seconds, static_cast<uint64_t>(nanoseconds));
}
auto convertToVeloxTimestamp(int64_t timestamp) -> Timestamp {
int64_t seconds{timestamp};
int64_t precisionDifference{Timestamp::kNanosInSecond};
switch (estimatePrecision(timestamp)) {
case TimestampPrecision::Nanoseconds:
break;
case TimestampPrecision::Microseconds:
precisionDifference = Timestamp::kNanosInSecond / Timestamp::kNanosecondsInMicrosecond;
break;
case TimestampPrecision::Milliseconds:
precisionDifference = Timestamp::kNanosInSecond / Timestamp::kNanosecondsInMillisecond;
break;
case TimestampPrecision::Seconds:
precisionDifference = Timestamp::kNanosInSecond / Timestamp::kNanosInSecond;
break;
}
seconds /= precisionDifference;
int64_t nanoseconds = timestamp % Timestamp::kNanosInSecond;
if (nanoseconds < 0) {
seconds -= 1;
nanoseconds += Timestamp::kNanosInSecond;
}
return Timestamp(seconds, static_cast<uint64_t>(nanoseconds));
}

A possible way to avoid overflow permenantly

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe consider this way

@gibber9809 gibber9809 Jul 11, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would need to do

int64_t nanoseconds{timestamp % precisionDifference  * (Timestamp::kNanosInSecond / precisionDifference)};

but yeah, this should work.

Should we update the implementation then, or is it fine to leave it?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good if we can update it in this PR, this would be the final comment for this PR

@anlowee
anlowee merged commit fbb5aa7 into presto-0.293-clp-connector Jul 11, 2025
@anlowee
anlowee deleted the timestamp-marshalling-rebased-2 branch July 11, 2025 21:38
anlowee pushed a commit that referenced this pull request Jul 23, 2025
…ger-overflow (facebookincubator#13831)

Summary:
Pull Request resolved: facebookincubator#13831

This avoids the following errors:

```
fbcode/third-party-buck/platform010/build/libgcc/include/c++/trunk/bits/std_abs.h:56:41: runtime error: negation of -9223372036854775808 cannot be represented in type 'long'; cast to an unsigned type to negate this value to itself
    #0 0x000000346ce5 in std::abs(long) fbcode/third-party-buck/platform010/build/libgcc/include/c++/trunk/bits/std_abs.h:56
    #1 0x000000345879 in std::shared_ptr<facebook::velox::BiasVector<facebook::velox::test::EvalTypeHelper<long>::Type>> facebook::velox::test::VectorMaker::biasVector<long>(std::vector<std::optional<long>, std::allocator<std::optional<long>>> const&) fbcode/velox/vector/tests/utils/VectorMaker-inl.h:58
    #2 0x000000344d34 in facebook::velox::test::BiasVectorErrorTest::errorTest(std::vector<std::optional<long>, std::allocator<std::optional<long>>>) fbcode/velox/vector/tests/BiasVectorTest.cpp:39
    #3 0x00000033ec99 in facebook::velox::test::BiasVectorErrorTest_checkRangeTooLargeError_Test::TestBody() fbcode/velox/vector/tests/BiasVectorTest.cpp:44
    #4 0x7fe0a2342c46 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) fbsource/src/gtest.cc:2727
    #5 0x7fe0a234275d in testing::Test::Run() fbsource/src/gtest.cc:2744
    #6 0x7fe0a2345fb3 in testing::TestInfo::Run() fbsource/src/gtest.cc:2890
    #7 0x7fe0a234c8eb in testing::TestSuite::Run() fbsource/src/gtest.cc:3068
    #8 0x7fe0a237b52b in testing::internal::UnitTestImpl::RunAllTests() fbsource/src/gtest.cc:6059
    #9 0x7fe0a237a0a2 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) fbsource/src/gtest.cc:2727
    #10 0x7fe0a23797f5 in testing::UnitTest::Run() fbsource/src/gtest.cc:5599
    #11 0x7fe0a2239800 in RUN_ALL_TESTS() fbsource/gtest/gtest.h:2334
    #12 0x7fe0a223952c in main fbcode/common/gtest/LightMain.cpp:20
    #13 0x7fe09ec2c656 in __libc_start_call_main /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #14 0x7fe09ec2c717 in __libc_start_main@GLIBC_2.2.5 /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/csu/../csu/libc-start.c:409:3
    #15 0x00000033d8b0 in _start /home/engshare/third-party2/glibc/2.34/src/glibc-2.34/csu/../sysdeps/x86_64/start.S:116

UndefinedBehaviorSanitizer: signed-integer-overflow fbcode/third-party-buck/platform010/build/libgcc/include/c++/trunk/bits/std_abs.h:56:41
```
Avoid overflow by using the expression (static_cast<uint64_t>(1) + ~static_cast<uint64_t>(min)) to calculate the absolute value of min without using std::abs

Reviewed By: dmm-fb, peterenescu

Differential Revision: D76901449

fbshipit-source-id: 7eb3bd0f83e42f44cdf34ea1759f3aa9e1042dae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants