Skip to content

feat(plugin-native-sidecar): Pass down session properties in NativeExpressionOptimizer#27304

Merged
pdabre12 merged 1 commit intoprestodb:masterfrom
pdabre12:propagate-session-properties-optimizer
Mar 27, 2026
Merged

feat(plugin-native-sidecar): Pass down session properties in NativeExpressionOptimizer#27304
pdabre12 merged 1 commit intoprestodb:masterfrom
pdabre12:propagate-session-properties-optimizer

Conversation

@pdabre12
Copy link
Copy Markdown
Contributor

@pdabre12 pdabre12 commented Mar 10, 2026

Description

Currently, the NativeExpressionOptimizer does not pass down system session properties, which may result in incorrect query results in a Prestissimo cluster. The optimizer should honor session properties defined at the session level. This change adds support for passing down system session properties in NativeExpressionOptimizer.

Motivation and Context

Fixes: #27313

Impact

No user impact

Test Plan

CI, Unit tests

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.

== NO RELEASE NOTE ==

Summary by Sourcery

Propagate session properties from Presto to the native sidecar expression optimizer so that native optimization can honor query/session configuration.

New Features:

  • Introduce an ExpressionOptimizationRequest wrapper that sends both expressions and session properties from the sidecar to the native optimizer endpoint.
  • Expose system properties from ConnectorSession and FullConnectorSession so they can be included in optimizer requests.
  • Add a utility to build Velox query configs directly from session properties for use outside full SessionRepresentation contexts.

Bug Fixes:

  • Ensure the native optimizer request/response contract validates and requires both expressions and sessionProperties fields, preventing malformed requests.

Enhancements:

  • Refine native server expression-optimization endpoint to accept a structured JSON object and construct QueryConfig using translated session properties plus timezone settings.
  • Refactor PrestoToVeloxQueryConfig session-config handling to support both full sessions and raw system property maps while centralizing query tracing regex construction.
  • Extend tests to verify that session properties affecting JSON casting are respected when expressions are optimized via the native optimizer path.

Summary by Sourcery

Propagate Presto session properties to the native sidecar expression optimizer so native optimization respects session-level configuration.

New Features:

  • Add an ExpressionOptimizationRequest wrapper to send both expressions and session properties from the sidecar to the native optimizer endpoint.
  • Expose system session properties through ConnectorSession and FullConnectorSession for use by native optimization.

Bug Fixes:

  • Ensure native expression optimization uses the same session properties as the main engine, preventing discrepancies in results for session-dependent behavior such as JSON casting.

Enhancements:

  • Extend the native optimizer HTTP endpoint to accept a structured JSON payload with expressions and sessionProperties and build QueryConfig from it.
  • Add utilities to derive Velox query configuration directly from session property maps, including query tracing settings.
  • Improve tests to verify that session properties are correctly propagated through the native optimizer path, including JSON casting behavior.

Tests:

  • Add session property propagation tests in the native sidecar plugin to validate optimizer behavior under different JSON cast configurations.

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

sourcery-ai bot commented Mar 10, 2026

Reviewer's Guide

Propagates Presto session system properties through the native sidecar expression optimizer to the native server, refactors query-config construction to work from raw session properties, and updates the native optimizer HTTP contract and tests accordingly.

Sequence diagram for session properties propagation in native expression optimization

sequenceDiagram
    actor QueryClient
    participant NativeSidecarExpressionInterpreter
    participant PrestoServer
    participant PrestoToVeloxQueryConfig
    participant VeloxQueryCtx

    QueryClient->>NativeSidecarExpressionInterpreter: optimizeBatch(session, expressions, level)
    NativeSidecarExpressionInterpreter->>NativeSidecarExpressionInterpreter: getSidecarRequest(session, level, resolvedExpressions)
    NativeSidecarExpressionInterpreter->>ConnectorSession: getSystemProperties()
    ConnectorSession-->>NativeSidecarExpressionInterpreter: Map<String,String> systemProperties
    NativeSidecarExpressionInterpreter->>NativeSidecarExpressionInterpreter: create ExpressionOptimizationRequest(expressions, systemProperties)
    NativeSidecarExpressionInterpreter->>PrestoServer: HTTP POST /v1/expressions/optimized
    PrestoServer->>PrestoServer: parse JSON body to ExpressionOptimizationRequest
    PrestoServer->>PrestoServer: validate sessionProperties and expressions
    PrestoServer->>PrestoToVeloxQueryConfig: toVeloxConfigsFromSessionProperties(sessionProperties)
    PrestoToVeloxQueryConfig-->>PrestoServer: Map<String,String> veloxConfigs
    PrestoServer->>PrestoServer: add timezone and adjustTimestampToTimezone configs
    PrestoServer->>VeloxQueryCtx: create(QueryConfig(veloxConfigs))
    VeloxQueryCtx-->>PrestoServer: QueryCtx
    PrestoServer->>PrestoServer: optimize expressions using native engine
    PrestoServer-->>NativeSidecarExpressionInterpreter: JSON RowExpressionOptimizationResult list
    NativeSidecarExpressionInterpreter-->>QueryClient: Map<RowExpression, RowExpression> optimizedExpressions
Loading

Class diagram for ExpressionOptimizationRequest and session property propagation

classDiagram
    class ConnectorSession {
        <<interface>>
        +String getQueryId()
        +String getUser()
        +String getSource()
        +String getCatalog()
        +String getSchema()
        +String getRemoteUserAddress()
        +String getUserAgent()
        +String getClientInfo()
        +String getClientTags()
        +String getTimeZoneKey()
        +Locale getLocale()
        +long getStartTime()
        +boolean isClientTransactionSupport()
        +String getTraceToken()
        +Optional getIdentity()
        +Optional getConnectorId()
        +Map~String,String~ getSystemProperties()
    }

    class FullConnectorSession {
        -Session session
        -Identity identity
        +FullConnectorSession(Session session, Identity identity)
        +ConnectorSession forCatalog(String catalog)
        +ConnectorSession forSchema(String schema)
        +ConnectorSession forSessionProperty(String name, String value)
        +Map~String,String~ getSystemProperties()
    }

    class NativeSidecarExpressionInterpreter {
        -NodeManager nodeManager
        -HttpClient httpClient
        -JsonCodec~ExpressionOptimizationRequest~ expressionOptimizationRequestCodec
        -JsonCodec~List~RowExpressionOptimizationResult~~ rowExpressionOptimizationResultJsonCodec
        +NativeSidecarExpressionInterpreter(HttpClient httpClient, NodeManager nodeManager, JsonCodec~List~RowExpressionOptimizationResult~~ rowExpressionOptimizationResultJsonCodec, JsonCodec~ExpressionOptimizationRequest~ expressionOptimizationRequestCodec)
        +Map~RowExpression,RowExpression~ optimizeBatch(ConnectorSession session, Map~RowExpression,RowExpression~ expressions, Level level)
        +List~RowExpressionOptimizationResult~ optimize(ConnectorSession session, List~RowExpression~ expressions, Level level)
        -Request getSidecarRequest(ConnectorSession session, Level level, List~RowExpression~ resolvedExpressions)
    }

    class ExpressionOptimizationRequest {
        -List~RowExpression~ expressions
        -Map~String,String~ sessionProperties
        +ExpressionOptimizationRequest(List~RowExpression~ expressions, Map~String,String~ sessionProperties)
        +List~RowExpression~ getExpressions()
        +Map~String,String~ getSessionProperties()
    }

    class PrestoToVeloxQueryConfig {
        +static unordered_map~String,String~ toVeloxConfigsFromSessionProperties(map~String,String~ sessionProperties)
        +static velox::core::QueryConfig toVeloxConfigs(protocol::SessionRepresentation session)
        +static velox::core::QueryConfig toVeloxConfigs(protocol::SessionRepresentation session, map~String,String~ extraCredentials)
    }

    ConnectorSession <|.. FullConnectorSession
    FullConnectorSession --> Session : wraps
    NativeSidecarExpressionInterpreter ..> ExpressionOptimizationRequest : creates and serializes
    ExpressionOptimizationRequest o--> RowExpression : contains
    ExpressionOptimizationRequest o--> "1" Map~String,String~ : sessionProperties
    PrestoToVeloxQueryConfig ..> ExpressionOptimizationRequest : consumes sessionProperties
    FullConnectorSession ..> PrestoToVeloxQueryConfig : provides systemProperties via getSystemProperties()
Loading

File-Level Changes

Change Details Files
Propagate ConnectorSession system properties in native expression optimization requests.
  • Add getSystemProperties() default method to ConnectorSession and implement it in FullConnectorSession to expose session-level system properties.
  • Introduce ExpressionOptimizationRequest wrapper carrying both RowExpressions and sessionProperties for the native optimizer payload.
  • Update NativeSidecarExpressionInterpreter to serialize ExpressionOptimizationRequest instead of a bare list of RowExpressions and adjust DI wiring to use the new JSON codec.
presto-spi/src/main/java/com/facebook/presto/spi/ConnectorSession.java
presto-main-base/src/main/java/com/facebook/presto/FullConnectorSession.java
presto-native-sidecar-plugin/src/main/java/com/facebook/presto/sidecar/expressions/ExpressionOptimizationRequest.java
presto-native-sidecar-plugin/src/main/java/com/facebook/presto/sidecar/expressions/NativeSidecarExpressionInterpreter.java
presto-native-sidecar-plugin/src/main/java/com/facebook/presto/sidecar/expressions/NativeExpressionsModule.java
presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/expressions/TestNativeExpressionInterpreter.java
Refactor Presto-to-Velox query config helpers to derive configs from raw session property maps and reuse them for the native optimizer endpoint.
  • Change updateFromSessionConfigs to accept a map of system properties, with an overload that still accepts SessionRepresentation and delegates to the map-based version.
  • Centralize query-tracing regex construction in the map-based updateFromSessionConfigs implementation.
  • Introduce toVeloxConfigsFromSessionProperties helper that builds a Velox config map directly from a sessionProperties map.
presto-native-execution/presto_cpp/main/PrestoToVeloxQueryConfig.cpp
presto-native-execution/presto_cpp/main/PrestoToVeloxQueryConfig.h
Tighten and extend the native server expression-optimization HTTP endpoint to consume structured requests with session properties.
  • Change the optimizer HTTP handler to parse a JSON object containing sessionProperties and expressions instead of a bare JSON array.
  • Validate presence and types of sessionProperties and expressions fields and convert the sessionProperties object into a std::map<string,string>.
  • Build velox::core::QueryConfig using toVeloxConfigsFromSessionProperties, then inject timezone and timestamp-adjustment flags before creating QueryCtx.
presto-native-execution/presto_cpp/main/PrestoServer.cpp
Extend sidecar plugin tests to cover session-property propagation and JSON cast behavior through the native optimizer path.
  • Add a data provider and test in TestNativeSidecarPlugin that toggle FIELD_NAMES_IN_JSON_CAST_ENABLED under native expression optimization and compare results against expected queries.
  • Wire ExpressionOptimizationRequest codec into sidecar expression tests by updating the test module bindings.
presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/TestNativeSidecarPlugin.java
presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/expressions/TestNativeExpressionInterpreter.java

Assessment against linked issues

Issue Objective Addressed Explanation
#27313 Propagate system session properties from the Presto session through the native sidecar expression optimizer into the native server so that the native optimizer honors session-level configuration (e.g., FIELD_NAMES_IN_JSON_CAST_ENABLED).
#27313 Add or update tests to validate that session properties affecting expression semantics (specifically JSON cast behavior controlled by FIELD_NAMES_IN_JSON_CAST_ENABLED) are respected when using the native expression optimizer.

Possibly linked issues


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

@pdabre12 pdabre12 changed the title feat(native-sidecar-plugin): Pass down session properties in NativeExpressionOptimizer feat(plugin-native-sidecar): Pass down session properties in NativeExpressionOptimizer Mar 10, 2026
@pdabre12 pdabre12 force-pushed the propagate-session-properties-optimizer branch from 7837074 to 9b09f99 Compare March 11, 2026 21:24
@pdabre12 pdabre12 marked this pull request as ready for review March 11, 2026 21:24
@pdabre12 pdabre12 requested review from a team as code owners March 11, 2026 21:24
@prestodb-ci prestodb-ci requested review from a team, Mariamalmesfer and auden-woolfson and removed request for a team March 11, 2026 21:24
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In PrestoServer::getOptimizedExpressions, the JSON sessionProperties map is blindly converted to std::map<std::string, std::string>; consider validating that all values are strings (or coercing them explicitly) to avoid surprising runtime failures if a client sends non-string session property values.
  • You now require sessionProperties in the optimizer request body and construct the QueryCtx before validating the expressions field; if you expect non-upgraded clients or malformed payloads, you might want to validate both keys up front before creating the QueryCtx to fail faster and avoid unnecessary work.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `PrestoServer::getOptimizedExpressions`, the JSON `sessionProperties` map is blindly converted to `std::map<std::string, std::string>`; consider validating that all values are strings (or coercing them explicitly) to avoid surprising runtime failures if a client sends non-string session property values.
- You now require `sessionProperties` in the optimizer request body and construct the `QueryCtx` before validating the `expressions` field; if you expect non-upgraded clients or malformed payloads, you might want to validate both keys up front before creating the `QueryCtx` to fail faster and avoid unnecessary work.

## Individual Comments

### Comment 1
<location path="presto-native-execution/presto_cpp/main/PrestoToVeloxQueryConfig.cpp" line_range="77" />
<code_context>

+  // Construct query tracing regex and pass to Velox config.
+  // It replaces the given native_query_trace_task_reg_exp if also set.
+  if (traceFragmentId.has_value() || traceShardId.has_value()) {
+    queryConfigs.emplace(
+        velox::core::QueryConfig::kQueryTraceTaskRegExp,
</code_context>
<issue_to_address>
**issue (bug_risk):** Using emplace for kQueryTraceTaskRegExp prevents overriding an existing config value

Because `emplace` is a no-op when the key already exists, this won’t override a previously set `kQueryTraceTaskRegExp`, which contradicts the comment about replacing `native_query_trace_task_reg_exp`. Consider using `queryConfigs[velox::core::QueryConfig::kQueryTraceTaskRegExp] = ...` or `insert_or_assign` so the session-derived regex reliably overrides any existing value.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@pdabre12
Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Changing the native optimizer HTTP endpoint from accepting a bare JSON array to a JSON object with expressions and sessionProperties will break any existing callers; consider keeping backward compatibility by accepting both formats for a deprecation period (e.g., handle the old array-only body if expressions is missing).
  • The construction of Velox QueryConfig is now split between toVeloxConfigsFromSessionProperties and manual additions in PrestoServer (timezone and adjust-timestamp flags); consider folding the timezone/adjustment handling into a single helper to keep all query-config derivation from session/sessionProperties centralized.
  • The native sidecar now relies on ConnectorSession.getSystemProperties() returning real values, but the SPI default returns an empty map; consider either tightening the type used in NativeSidecarExpressionInterpreter (e.g., requiring FullConnectorSession) or clearly documenting/enforcing that connectors using native optimization must override getSystemProperties to avoid silent misconfiguration.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Changing the native optimizer HTTP endpoint from accepting a bare JSON array to a JSON object with `expressions` and `sessionProperties` will break any existing callers; consider keeping backward compatibility by accepting both formats for a deprecation period (e.g., handle the old array-only body if `expressions` is missing).
- The construction of Velox QueryConfig is now split between `toVeloxConfigsFromSessionProperties` and manual additions in `PrestoServer` (timezone and adjust-timestamp flags); consider folding the timezone/adjustment handling into a single helper to keep all query-config derivation from session/sessionProperties centralized.
- The native sidecar now relies on `ConnectorSession.getSystemProperties()` returning real values, but the SPI default returns an empty map; consider either tightening the type used in `NativeSidecarExpressionInterpreter` (e.g., requiring `FullConnectorSession`) or clearly documenting/enforcing that connectors using native optimization must override `getSystemProperties` to avoid silent misconfiguration.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@pramodsatya pramodsatya left a comment

Choose a reason for hiding this comment

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

Thanks @pdabre12, LGTM overall % one comment.

auto queryConfig = velox::core::QueryConfig{std::move(config)};

json input = json::parse(util::extractMessageBody(body));
VELOX_USER_CHECK(
Copy link
Copy Markdown
Contributor

@pramodsatya pramodsatya Mar 13, 2026

Choose a reason for hiding this comment

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

Could you translate input json to ExpressionOptimizationRequest via presto_protocol ? That way all these checks wont be necessary and protocol can ensure serde happens in the right format.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done.

@pdabre12 pdabre12 requested a review from pramodsatya March 13, 2026 21:13
Copy link
Copy Markdown
Contributor

@pramodsatya pramodsatya left a comment

Choose a reason for hiding this comment

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

Thanks @pdabre12.

}

@DataProvider(name = "fieldNamesInJsonCastEnabled")
private Object[][] fieldNamesInJsonCastEnabled()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: data provider can be reverted and these params can be maintained in a list in the test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

}

@Test(dataProvider = "fieldNamesInJsonCastEnabled")
public void testSessionPropertiesPropagatingThroughOptimizer(String enabled, @Language("SQL") String expected)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Would it be better to move this to TestNativeExpressionOptimizer ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Copy Markdown
Contributor

@aditi-pandit aditi-pandit left a comment

Choose a reason for hiding this comment

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

Thanks @pdabre12 for this code. Just have some nits.

}

@Test(dataProvider = "fieldNamesInJsonCastEnabled")
public void testSessionPropertiesPropagatingThroughOptimizer(String enabled, @Language("SQL") String expected)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit : Rename testSessionProperties

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed and moved to TestNativeExpressionOptimizer, also keeping an e2e test in TestNativeSidecarPlugin.

@pdabre12 pdabre12 force-pushed the propagate-session-properties-optimizer branch from 4a8edba to 4cc691c Compare March 18, 2026 00:14
@pdabre12
Copy link
Copy Markdown
Contributor Author

@aditi-pandit @pramodsatya Can you please have another look? I addressed the comments.

Copy link
Copy Markdown
Contributor

@aditi-pandit aditi-pandit left a comment

Choose a reason for hiding this comment

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

Thanks @pdabre12. Just have a minor comment on the test. Code looks good otherwise.

@pdabre12 pdabre12 force-pushed the propagate-session-properties-optimizer branch from 4cc691c to f5e9ee3 Compare March 26, 2026 23:00
@pdabre12 pdabre12 force-pushed the propagate-session-properties-optimizer branch from f5e9ee3 to 85b234f Compare March 26, 2026 23:02
Copy link
Copy Markdown
Contributor

@aditi-pandit aditi-pandit left a comment

Choose a reason for hiding this comment

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

Thanks @pdabre12

@pdabre12 pdabre12 merged commit 691cb4b into prestodb:master Mar 27, 2026
84 checks passed
@pdabre12 pdabre12 deleted the propagate-session-properties-optimizer branch March 27, 2026 03:14
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.

native(sidecar) : Pass down system session properties in the native expression optimizer

5 participants