Skip to content

feat(isthmus): map execution context variables to/from Calcite#976

Open
nielspardon wants to merge 1 commit into
substrait-io:mainfrom
nielspardon:feat/isthmus-execution-context-variables
Open

feat(isthmus): map execution context variables to/from Calcite#976
nielspardon wants to merge 1 commit into
substrait-io:mainfrom
nielspardon:feat/isthmus-execution-context-variables

Conversation

@nielspardon

Copy link
Copy Markdown
Member

Summary

Adds Isthmus (Substrait ⇄ Apache Calcite) mappings for the three execution context variables introduced in :core by #867: current_timestamp, current_date and current_timezone. Previously any plan containing one of these failed to convert to/from Calcite.

Changes

  • Substrait → Calcite (ExpressionRexConverter): three visit overrides build niladic RexCalls whose return type is forced from the Substrait type so time-zone semantics, precision and required-nullability round-trip.
    • CurrentTimestamp(p)CURRENT_TIMESTAMP typed TIMESTAMP_WITH_LOCAL_TIME_ZONE(p) — Substrait's current_timestamp is a precision_timestamp_tz, whereas Calcite's operator would otherwise infer a timezone-less TIMESTAMP.
    • CurrentDateCURRENT_DATE typed DATE.
    • CurrentTimezone → the new CurrentTimezoneFunction typed non-nullable VARCHAR.
  • Calcite → Substrait: new CallConverters.EXECUTION_CONTEXT_VARIABLE matches these operators by identity and produces the corresponding Substrait expressions. Registered in both CallConverters.defaults() and ConverterProvider.getCallConverters().
  • Session timezone operator: Calcite has no built-in operator for the session timezone, so this adds CurrentTimezoneFunction — a niladic SqlBaseContextVariable (modeled on Calcite's own CURRENT_USER/CURRENT_ROLE). It is registered in SubstraitOperatorTable via a separate scalar-operator table so that:
    • SELECT current_timezone parses from SQL text without parentheses (via Calcite's SqlValidatorImpl.makeNullaryCall), and
    • its generic OTHER_FUNCTION kind does not pollute OVERRIDE_KINDS (which would otherwise filter every standard OTHER_FUNCTION operator out of the operator list).

Tests

  • ExecutionContextVariableConversionTest: bidirectional expression ⇄ Rex round-trips for all three variables.
  • Substrait2SqlTest: full SQL round-trips for current_timestamp, current_date, and current_timezone, including asserting that current_timezone parses and is emitted back as the bare keyword (no Calcite dialect changes required).

Verification

:core:spotlessCheck, :isthmus:spotlessCheck, the full :isthmus:test suite, and compilation of :spark:spark-3.5_2.12 and :examples:substrait-spark all pass.

🤖 Generated with AI

Add Isthmus (Substrait <-> Apache Calcite) mappings for the three
execution context variables introduced in core by substrait-io#867:
current_timestamp, current_date and current_timezone.

Substrait -> Calcite (ExpressionRexConverter): three visit overrides
build niladic RexCalls whose return type is forced from the Substrait
type so time-zone semantics, precision and required-nullability
round-trip. current_timestamp maps to CURRENT_TIMESTAMP typed
TIMESTAMP_WITH_LOCAL_TIME_ZONE(p) (Calcite's operator would otherwise
infer a timezone-less TIMESTAMP), current_date to CURRENT_DATE, and
current_timezone to a new operator (below).

Calcite -> Substrait: a new EXECUTION_CONTEXT_VARIABLE CallConverter
matches these operators by identity and produces the corresponding
Substrait expressions, registered in both CallConverters.defaults() and
ConverterProvider.getCallConverters().

Session timezone: Calcite has no built-in operator for it, so add
CurrentTimezoneFunction, a niladic SqlBaseContextVariable (modeled on
CURRENT_USER/CURRENT_ROLE). It is registered in SubstraitOperatorTable
via a separate scalar-operator table so it parses from SQL text without
parentheses (through SqlValidatorImpl.makeNullaryCall) while avoiding
polluting OVERRIDE_KINDS with its generic OTHER_FUNCTION kind.

Tests: bidirectional expression<->rex round-trips for all three
variables, plus SQL round-trips (including that current_timezone parses
and is emitted back as the bare keyword).
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.

1 participant