-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Enable PPL eval string concat on the analytics-engine route via DataFusion CONCAT/CAST #21498
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ae7e2c
[Analytics Framework] Resolve symbolic operators and add SAFE_CAST
ahkcs c8c03d5
[Analytics Engine] Migrate rules and adapter dispatch to fromSqlOperator
ahkcs 876702d
[Analytics Backend / DataFusion] Wire CONCAT/CAST/SAFE_CAST + concat …
ahkcs f98a4da
[QA] Add EvalCommandIT for the analytics-engine REST path
ahkcs dbd2825
[Analytics Framework] Rename fromSqlOperator to fromSqlOperatorWithFa…
ahkcs 8c058a4
[Analytics Framework + Backend] Address @expani review on PR #21498
ahkcs 400ebd9
[Analytics Framework] Resolve symbolic operators by Calcite-operator …
ahkcs eb2121e
[Analytics Framework + Backend] Address @expani follow-up on PR #21498
ahkcs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...-backend-datafusion/src/main/java/org/opensearch/be/datafusion/ConcatFunctionAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.be.datafusion; | ||
|
|
||
| import org.apache.calcite.plan.RelOptCluster; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.opensearch.analytics.spi.FieldStorageInfo; | ||
| import org.opensearch.analytics.spi.ScalarFunctionAdapter; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Adapts {@code ||(a, b, ...)} (Calcite {@code SqlStdOperatorTable.CONCAT}) into a | ||
| * null-propagating form for the DataFusion backend. | ||
| * | ||
| * <p>Calcite's {@code ||} operator follows the SQL standard: if any operand is NULL, the result | ||
| * is NULL. Substrait's default {@code concat} extension is documented with the same semantics, | ||
| * but DataFusion's substrait reader maps it to the DataFusion {@code concat()} function — which | ||
| * deviates from the standard and treats NULL operands as empty strings. To preserve Calcite's | ||
| * semantics on the analytics-engine path, this adapter rewrites | ||
| * | ||
| * <pre>{@code | ||
| * ||(a, b) | ||
| * → | ||
| * CASE WHEN a IS NULL OR b IS NULL THEN NULL ELSE ||(a, b) END | ||
| * }</pre> | ||
| * | ||
| * The inner {@code ||} is left intact and serializes through the same Substrait conversion path, | ||
| * but with the surrounding CASE/IS_NULL the DataFusion {@code concat()} call is short-circuited | ||
| * for any input that contains a NULL — restoring SQL-standard null-propagation without requiring | ||
| * a custom DataFusion UDF. | ||
| * | ||
| * <p>Single-operand calls fall through unchanged (the result equals the operand, so no | ||
| * null-handling rewrite is needed). | ||
| */ | ||
| class ConcatFunctionAdapter implements ScalarFunctionAdapter { | ||
|
|
||
| @Override | ||
| public RexNode adapt(RexCall original, List<FieldStorageInfo> fieldStorage, RelOptCluster cluster) { | ||
| List<RexNode> operands = original.getOperands(); | ||
| if (operands.size() < 2) { | ||
| return original; | ||
| } | ||
| RexBuilder rexBuilder = cluster.getRexBuilder(); | ||
| // Fold operands into a single OR(IS_NULL(o0), IS_NULL(o1), ...) predicate. IS_NULL on a | ||
| // non-null literal reduces to constant-false, so the OR collapses cleanly through the | ||
| // optimizer for cases where some operands are statically non-null. | ||
| RexNode anyNull = rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, operands.get(0)); | ||
| for (int i = 1; i < operands.size(); i++) { | ||
| anyNull = rexBuilder.makeCall( | ||
| SqlStdOperatorTable.OR, | ||
| anyNull, | ||
| rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, operands.get(i)) | ||
| ); | ||
| } | ||
| // Result type stays the same as the original CONCAT — nullable VARCHAR. | ||
| RexNode nullLiteral = rexBuilder.makeNullLiteral(original.getType()); | ||
| return rexBuilder.makeCall(original.getType(), SqlStdOperatorTable.CASE, List.of(anyNull, nullLiteral, original)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.