Skip to content

Conversation

@kazantsev-maksim
Copy link
Contributor

@kazantsev-maksim kazantsev-maksim commented Jan 2, 2026

Which issue does this PR close?

  • N/A

Rationale for this change

Add new function: https://spark.apache.org/docs/latest/api/sql/index.html#space

What changes are included in this PR?

  • Implementation
  • Unit Tests
  • SLT tests

Are these changes tested?

Yes, tests added as part of this PR.

Are there any user-facing changes?

No, these are new function.

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) spark labels Jan 2, 2026
Kazantsev Maksim added 2 commits January 2, 2026 19:11
if args.args.len() != 1 {
return plan_err!("space expects exactly 1 argument");
}
make_scalar_function(spark_space, vec![])(&args.args)
Copy link
Member

@andygrove andygrove Jan 2, 2026

Choose a reason for hiding this comment

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

I think that space is quite often invoked with a literal argument, e.g., space(2). The current implementation converts the scalar value into an array for each batch and then calls " ".repeat(m as usize) for each row, resulting in the same string being allocated each time. This could be optimized in the scalar case to build the string once rather than per row and then append the same string to the builder repeatedly.

Copy link
Member

Choose a reason for hiding this comment

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

In fact, the return type in this case could be a scalar not an array

if m < 0 {
String::new()
} else {
" ".repeat(m as usize)
Copy link
Member

Choose a reason for hiding this comment

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

This allocates a new string per row, which is discarded after being appended to the array. It would be more efficient for this function to build the string buffer and offsets directly.

# under the License.

query T
SELECT space(1::INT);
Copy link
Member

Choose a reason for hiding this comment

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

Could you add tests that use array inputs. The tests currently only cover scalar inputs.

Kazantsev Maksim added 4 commits January 3, 2026 21:05
@kazantsev-maksim
Copy link
Contributor Author

kazantsev-maksim commented Jan 3, 2026

@andygrove thanks for the review, the benchmark test result in Comet is still not ideal.

space


fn spark_space_array_inner(array: &Int32Array) -> StringArray {
let values = array.values();
let data_capacity = values
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @kazantsev-maksim just thinking aloud if we can iterate values only once?

fn spark_space_array_inner(array: &Int32Array) -> StringArray {
    let mut builder = StringBuilder::new(array.len());
    let mut space_buf = String::new();

    for v in array.iter() {
        match v {
            None => builder.append_null(),
            Some(l) if *l > 0 => {
                let l = *l as usize;
                if space_buf.len() < l {
                    space_buf = " ".repeat(l);
                }
                builder.append_value(&space_buf[..l]);
            }
            Some(_) => builder.append_value(""),
        }
    }

    builder.finish()
}

something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@comphead thanks, performance has improved!

bench

@comphead
Copy link
Contributor

comphead commented Jan 4, 2026

Thanks @kazantsev-maksim and @andygrove for the review

@comphead comphead added this pull request to the merge queue Jan 4, 2026
Merged via the queue into apache:main with commit 7e04974 Jan 4, 2026
31 checks passed
@kazantsev-maksim kazantsev-maksim deleted the spark_space branch January 5, 2026 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants