-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56916][SQL] Simplify ElementAt array codegen under ANSI mode #55941
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions; | ||
|
|
||
| import org.apache.spark.QueryContext; | ||
| import org.apache.spark.sql.errors.QueryExecutionErrors; | ||
|
|
||
| /** | ||
| * Static helpers for array-related expressions invoked from | ||
| * {@code doGenCode} and {@code eval} paths. | ||
| * | ||
| * Currently provides the ANSI-mode index validation for | ||
| * {@link ElementAt} on {@code ArrayType}: a single call replaces ~12 lines | ||
| * of inline length / zero / sign-normalization codegen with a return of | ||
| * the normalized array position (0-based). | ||
| */ | ||
| public final class ArrayUtils { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stack uses per-operation naming ( Consider renaming to |
||
|
|
||
| private ArrayUtils() {} | ||
|
|
||
| /** | ||
| * Validates a 1-based {@code element_at} index against the array length | ||
| * and returns the 0-based position. Throws when the absolute index | ||
| * exceeds the array length (ANSI out-of-bounds) or when {@code index} is | ||
| * zero (always invalid). | ||
| * | ||
| * @param length the array length | ||
| * @param index the 1-based index supplied by the user (positive or negative) | ||
| * @param context the query context attached to the error | ||
| * @return the validated 0-based position | ||
| */ | ||
| public static int elementAtIndexExact(int length, int index, QueryContext context) { | ||
| if (length < Math.abs(index)) { | ||
| throw QueryExecutionErrors.invalidElementAtIndexError(index, length, context); | ||
| } | ||
| if (index == 0) { | ||
| throw QueryExecutionErrors.invalidIndexOfZeroError(context); | ||
| } | ||
| return index > 0 ? index - 1 : length + index; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
a single call replaces ~12 lines ...clause describes the PR's effect rather than the helper's contract — once merged, the original 12-line inline form isn't visible to future readers. PeerCastUtils.javadoesn't include similar line-count claims.