Skip to content

fix(plugin-native-sidecar): Pass session start time as a header in na…#27545

Draft
pdabre12 wants to merge 1 commit intoprestodb:masterfrom
pdabre12:fix-session-functions
Draft

fix(plugin-native-sidecar): Pass session start time as a header in na…#27545
pdabre12 wants to merge 1 commit intoprestodb:masterfrom
pdabre12:fix-session-functions

Conversation

@pdabre12
Copy link
Copy Markdown
Contributor

@pdabre12 pdabre12 commented Apr 8, 2026

…tive expression optimizer

Description

Motivation and Context

Impact

Test Plan

Contributor checklist

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.
  • If adding new dependencies, verified they have an OpenSSF Scorecard score of 5.0 or higher (or obtained explicit TSC approval for lower scores).

Release Notes

Please follow release notes guidelines and fill in the release notes below.

== RELEASE NOTES ==

General Changes
* ... 
* ... 

Hive Connector Changes
* ... 
* ... 

If release note is NOT required, use:

== NO RELEASE NOTE ==

Summary by Sourcery

Propagate the SQL session start time from the Java sidecar plugin to the native expression optimizer so time-based session functions can be evaluated consistently in the native sidecar.

New Features:

  • Support optimization of time-based session functions (e.g., now(), current_timestamp) in the native expression optimizer using the session start time.

Enhancements:

  • Forward the session start time from the sidecar plugin to the native engine via an HTTP header and native query configuration.
  • Expand native expression optimizer tests to cover session-based time functions and their evaluation semantics.

@prestodb-ci prestodb-ci added the from:IBM PR from IBM label Apr 8, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 8, 2026

Reviewer's Guide

Passes the session start time from the Java sidecar plugin to the native expression optimizer via a new HTTP header, wires it into Velox QueryConfig so session-time-dependent functions like now() and current_timestamp are evaluated consistently, and adds/adjusts tests to validate session function optimization behavior.

Sequence diagram for passing session start time header to native expression optimizer

sequenceDiagram
    actor Client
    participant PrestoCoordinator
    participant NativeSidecarExpressionInterpreter
    participant SidecarHttpClient
    participant PrestoNativeServer
    participant VeloxQueryConfig
    participant ExpressionOptimizer

    Client->>PrestoCoordinator: Submit query
    PrestoCoordinator->>NativeSidecarExpressionInterpreter: Optimize expressions(ConnectorSession, Level, rows)
    NativeSidecarExpressionInterpreter->>NativeSidecarExpressionInterpreter: Read sessionStartTime from SqlFunctionProperties
    NativeSidecarExpressionInterpreter->>SidecarHttpClient: Build Request
    activate NativeSidecarExpressionInterpreter
    NativeSidecarExpressionInterpreter->>SidecarHttpClient: setHeader X-Presto-Session-Start-Time = sessionStartTime
    deactivate NativeSidecarExpressionInterpreter

    SidecarHttpClient->>PrestoNativeServer: POST /v1/expressions
    activate PrestoNativeServer
    PrestoNativeServer->>PrestoNativeServer: Read X-Presto-Session-Start-Time header
    PrestoNativeServer->>VeloxQueryConfig: configs.insert(kSessionStartTime, sessionStartTime)
    VeloxQueryConfig-->>PrestoNativeServer: QueryConfig instance

    PrestoNativeServer->>ExpressionOptimizer: Optimize expressions(QueryConfig, expressions)
    ExpressionOptimizer-->>PrestoNativeServer: Optimized expressions
    deactivate PrestoNativeServer

    PrestoNativeServer-->>SidecarHttpClient: HTTP 200 optimized expressions
    SidecarHttpClient-->>NativeSidecarExpressionInterpreter: Response
    NativeSidecarExpressionInterpreter-->>PrestoCoordinator: Optimized expressions
    PrestoCoordinator-->>Client: Query result
Loading

Updated class diagram for sidecar interpreter and native server configuration

classDiagram
    class NativeSidecarExpressionInterpreter {
        +String PRESTO_TIME_ZONE_HEADER
        +String PRESTO_USER_HEADER
        +String PRESTO_EXPRESSION_OPTIMIZER_LEVEL_HEADER
        +String PRESTO_SESSION_START_TIME_HEADER
        -NodeManager nodeManager
        -HttpClient httpClient
        -Uri sidecarBaseUri
        -Request getSidecarRequest(ConnectorSession session, Level level, List rows)
        +List optimize(ConnectorSession session, Level level, List rows)
    }

    class ConnectorSession {
        +String getUser()
        +SqlFunctionProperties getSqlFunctionProperties()
    }

    class SqlFunctionProperties {
        +TimeZoneKey getTimeZoneKey()
        +long getSessionStartTime()
    }

    class TimeZoneKey {
        +String getId()
    }

    class Request {
        +RequestBuilder newBuilder()
    }

    class RequestBuilder {
        +RequestBuilder setHeader(String name, String value)
        +Request build()
    }

    class PrestoServer {
        +json_array_t getOptimizedExpressions(HttpHeaders httpHeaders, String body)
        -string kTimezoneHeader
        -string kSessionStartTimeHeader
    }

    class HttpHeaders {
        +String getSingleOrEmpty(String name)
    }

    class QueryConfig {
        +static string kSessionTimezone
        +static string kAdjustTimestampToTimezone
        +static string kSessionStartTime
        +QueryConfig(map configs)
    }

    class ExpressionOptimizerService {
        +json_array_t optimize(QueryConfig queryConfig, json_array_t expressions)
    }

    NativeSidecarExpressionInterpreter --> ConnectorSession : uses
    ConnectorSession --> SqlFunctionProperties : uses
    SqlFunctionProperties --> TimeZoneKey : uses
    NativeSidecarExpressionInterpreter --> RequestBuilder : builds
    RequestBuilder --> Request : builds

    PrestoServer --> HttpHeaders : reads
    PrestoServer --> QueryConfig : constructs
    PrestoServer --> ExpressionOptimizerService : calls
    QueryConfig --> "configs" map : contains

    SqlFunctionProperties : +long sessionStartTime
    QueryConfig : +string sessionStartTime
Loading

File-Level Changes

Change Details Files
Propagate session start time from the sidecar plugin to the native expression optimizer through an HTTP header and into Velox query configuration.
  • Define a new X-Presto-Session-Start-Time header constant in the Java native sidecar expression interpreter.
  • Include the session start time from SqlFunctionProperties as a header when building sidecar HTTP requests for expression optimization.
  • Read the X-Presto-Session-Start-Time header in the native Presto server handling expression optimization requests.
  • Inject the session start time value into velox::core::QueryConfig using kSessionStartTime so native evaluation can use it.
presto-native-sidecar-plugin/src/main/java/com/facebook/presto/sidecar/expressions/NativeSidecarExpressionInterpreter.java
presto-native-execution/presto_cpp/main/PrestoServer.cpp
Add tests to validate that session-dependent time functions are optimized correctly using the session start time and clean up obsolete interpreter test.
  • Add a new testSessionFunctions in TestNativeExpressionOptimizer to assert correct optimization of current_timestamp and now() based on session start time and timezone.
  • Construct a custom Session with a controlled start time and timezone to verify equality and ordering semantics for now() and current_timestamp in the optimizer.
  • Remove the disabled testCurrentTimestamp override in TestNativeExpressionInterpreter as current timestamp is now evaluated in the sidecar plugin.
presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/expressions/TestNativeExpressionOptimizer.java
presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/expressions/TestNativeExpressionInterpreter.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

from:IBM PR from IBM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants