Skip to content

[SPARK-53546][SQL][TESTS] Fix InMemoryDataSource to return default value or null for new fields - #52299

Closed
szehon-ho wants to merge 3 commits into
apache:masterfrom
szehon-ho:in_memory_schema_evolution
Closed

szehon-ho wants to merge 3 commits into
apache:masterfrom
szehon-ho:in_memory_schema_evolution

Conversation

@szehon-ho

@szehon-ho szehon-ho commented Sep 10, 2025 •

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

InMemoryTable does not handle schema evolution properly. In particular, it always uses the current schema to read the in-memory rows. For newly-added columns that don't exist in the row, the InternalRow sometimes return some junk value.

This fixes InMemoryDataSource to properly handle schema evolution. Like modern table format, it persists the 'writeSchema' used to write the BufferedRows (on the BufferedRows object here), and then at read time resolves it with the 'readSchema'.

Why are the changes needed?

While writing tests for MERGE INTO schema evolution, realized that in these cases InMemoryTable is returning junk values, unlike a real V2 data source.

Does this PR introduce any user-facing change?

No

How was this patch tested?

Add a new test in AlterTableTests

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions Bot added the SQL label Sep 10, 2025

@szehon-ho szehon-ho Sep 11, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this method is used by alterTable and regular write, so we have a flag newData=false to preserve the original 'writeSchema' for the BufferedRows in the alterTable case instead of using the new schema. In the write case, we mark the new rows with the new schema.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I end up, making another method for alterTable to not pollute existing logic with if-else. That version preserves the 'writeSchema' on the BufferedRows.

@szehon-ho
szehon-ho force-pushed the in_memory_schema_evolution branch from 6f66699 to 616f350 Compare September 11, 2025 00:41

@szehon-ho szehon-ho Sep 12, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note: Array[Column] is not serializable, so hence passing StructType to these BatchWriterFactory and BatchWriters

@szehon-ho
szehon-ho force-pushed the in_memory_schema_evolution branch from 5357f86 to a1dc29c Compare September 12, 2025 22:24
@cloud-fan

Copy link
Copy Markdown
Contributor

thanks, merging to master!

Comment on lines +354 to +355
def alterTableWithData(data: Array[BufferedRows],
newSchema: StructType): InMemoryBaseTable = {

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.

style nit:

Suggested change
def alterTableWithData(data: Array[BufferedRows],
newSchema: StructType): InMemoryBaseTable = {
def alterTableWithData(
data: Array[BufferedRows],
newSchema: StructType): InMemoryBaseTable = {

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.

You can fix them in your next PR.

@cloud-fan cloud-fan closed this in fbdad29 Sep 15, 2025
cloud-fan pushed a commit that referenced this pull request Sep 17, 2025
… style for InMemoryBaseTable

### What changes were proposed in this pull request?
Fix nested array struct schema evolution case for InMemoryDataSource, also style fixes.

### Why are the changes needed?
Address style review comment (#52299 (comment)) and a small bug fix for #52299 (test-only)

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Ran affected unit test

### Was this patch authored or co-authored using generative AI tooling?
No

Closes #52359 from szehon-ho/in_memory_schema_evolution_fix.

Authored-by: Szehon Ho <szehon.apache@gmail.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
huangxiaopingRD pushed a commit to huangxiaopingRD/spark that referenced this pull request Nov 25, 2025
…lue or null for new fields

### What changes were proposed in this pull request?
InMemoryTable does not handle schema evolution properly.  In particular, it always uses the current schema to read the in-memory rows.  For newly-added columns that don't exist in the row, the InternalRow sometimes return some junk value.

This fixes InMemoryDataSource to properly handle schema evolution.  Like modern table format, it persists the 'writeSchema' used to write the BufferedRows (on the BufferedRows object here), and then at read time resolves it with the 'readSchema'.

### Why are the changes needed?
While writing tests for MERGE INTO schema evolution, realized that in these cases InMemoryTable is returning junk values, unlike a real V2 data source.

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Add a new test in AlterTableTests

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#52299 from szehon-ho/in_memory_schema_evolution.

Authored-by: Szehon Ho <szehon.apache@gmail.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
huangxiaopingRD pushed a commit to huangxiaopingRD/spark that referenced this pull request Nov 25, 2025
… style for InMemoryBaseTable

### What changes were proposed in this pull request?
Fix nested array struct schema evolution case for InMemoryDataSource, also style fixes.

### Why are the changes needed?
Address style review comment (apache#52299 (comment)) and a small bug fix for apache#52299 (test-only)

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Ran affected unit test

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#52359 from szehon-ho/in_memory_schema_evolution_fix.

Authored-by: Szehon Ho <szehon.apache@gmail.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants