Skip to content

feat: support non-parent interleaves #272

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

taka-oyama
Copy link
Collaborator

@taka-oyama taka-oyama commented Apr 28, 2025

https://cloud.google.com/spanner/docs/release-notes#April_24_2025

Summary by CodeRabbit

  • New Features
    • Added support for interleaving tables using an alternative method for specifying parent relationships.
  • Bug Fixes
    • Improved SQL generation for interleaved tables to correctly reflect parent relationships.
  • Documentation
    • Updated documentation to clarify the presence of the new property for parent relationships in interleaved definitions.
  • Tests
    • Added tests to ensure correct SQL is generated when using the new interleaving method.

@taka-oyama taka-oyama added the enhancement New feature or request label Apr 28, 2025
@taka-oyama taka-oyama requested a review from a team April 28, 2025 04:39
@taka-oyama taka-oyama self-assigned this Apr 28, 2025
Copy link

coderabbitai bot commented Apr 28, 2025

Walkthrough

The changes introduce a new method, interleaveIn, in the Blueprint class to generalize the logic for interleaving tables, allowing specification of whether the interleave is in a parent table via a boolean parameter. The existing interleaveInParent method is refactored to delegate to this new method. The grammar logic for generating SQL interleave clauses is updated to utilize the new property. The InterleaveDefinition class documentation is updated to reflect the new property, and additional tests are added to verify the new method's behavior.

Changes

Files/Paths Change Summary
src/Schema/Blueprint.php Added interleaveIn method; refactored interleaveInParent to call interleaveIn with $parent = true. Consolidated interleaving logic.
src/Schema/Grammar.php Modified addInterleaveToTable to use the new inParent property and adjust SQL clause construction accordingly.
src/Schema/InterleaveDefinition.php Updated PHPDoc to add @property bool $inParent annotation.
tests/Schema/BlueprintTest.php Added a new test block to test_interleaving to cover the new interleaveIn method and verify SQL output.

Suggested labels

waiting for review


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ec89601 and bac4b62.

📒 Files selected for processing (4)
  • src/Schema/Blueprint.php (1 hunks)
  • src/Schema/Grammar.php (1 hunks)
  • src/Schema/InterleaveDefinition.php (1 hunks)
  • tests/Schema/BlueprintTest.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/Schema/BlueprintTest.php (1)
src/Schema/Blueprint.php (4)
  • Blueprint (30-490)
  • uuid (129-134)
  • string (119-123)
  • interleaveIn (279-287)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Run-PHPUnit
🔇 Additional comments (5)
src/Schema/InterleaveDefinition.php (1)

25-25: Good documentation update!

Adding this property annotation properly documents the new capability to interleave in both parent and non-parent tables, ensuring proper type hinting for the inParent boolean flag.

src/Schema/Grammar.php (1)

441-454: Well-structured refactoring to support the new non-parent interleave feature.

The code now correctly conditionally includes "parent" in the interleave clause based on the inParent flag. The refactoring also improves readability by first assigning the command to a variable before checking for null and breaking down the string concatenation steps.

#!/bin/bash
# Verify that interleave clause construction matches the feature implementation
rg -A 10 "interleave in" --glob="*.php" --glob="!tests/*"
tests/Schema/BlueprintTest.php (1)

375-388: Good test coverage for the new feature!

This new test case properly validates that the interleaveIn method without the parent flag correctly generates SQL without the "parent" keyword. This complements the existing tests for interleaveInParent, ensuring complete coverage of the feature.

src/Schema/Blueprint.php (2)

269-272: Clean implementation of method delegation.

The interleaveInParent method now simply delegates to the more general interleaveIn method with the parent flag set to true. This maintains backward compatibility while supporting the new feature.


274-287:

✅ Verification successful

Great implementation of the new non-parent interleave feature.

The new interleaveIn method generalizes the interleave functionality to support both parent and non-parent interleaves. The implementation:

  1. Takes a table name and an optional boolean flag (defaults to false)
  2. Creates an interleave command with the appropriate parameters
  3. Returns an InterleaveDefinition for fluent method chaining

This clean implementation follows the package's established patterns and provides a good foundation for the feature.


🌐 Web query:

What are non-parent interleaves in Google Cloud Spanner and when would you use them?

💡 Result:

Non-Parent Interleaves in Google Cloud Spanner

Definition

In Google Cloud Spanner, "non-parent interleaves" is not an official term in the documentation, but it typically refers to table interleaving where referential integrity is not strictly enforced between the parent and child tables. Instead, the physical storage of the child table rows is interleaved (colocated) with the parent table rows to improve data locality and query performance, but Spanner does not enforce foreign key constraints between those tables[1]. Such interleaving contrasts with using foreign keys, which enforce referential integrity but do not guarantee physical row colocating.

How Interleaving Works

  • When you define a table as INTERLEAVE IN PARENT, Cloud Spanner physically stores rows of the child table next to their corresponding parent row.
  • This structure is designed to optimize the retrieval of related data that is frequently accessed together (such as all invoices for a customer), providing faster queries due to data locality[1][4].
  • Interleaved tables require the primary key of the parent to be a prefix of the child table’s primary key[1].

Characteristics of Non-Parent Interleaves

  • Spanner does not enforce referential integrity between the parent and child; that is, you can remove parent rows even if child rows still exist (unless you define cascading deletes), and orphaned child rows may exist[1].
  • Referential integrity (i.e., ensuring a parent row exists for every child row) is only strictly enforced via foreign keys, which do not co-locate data[1][7].
  • The main benefit is physical data locality and improved performance for hierarchical data access, not database-level referential integrity[1][2].

When to Use Non-Parent Interleaves

You would use (non-parent enforced) interleaving in Cloud Spanner in the following cases:

  • Optimizing for Data Locality: When your application frequently reads parent and child records together and you want those reads to be efficient. Interleaving means related rows are stored close together, which can significantly reduce query latency[1][4].
  • Hierarchical Data Needs: If your data naturally forms a hierarchy (e.g., customers → orders → order items), interleaving can optimize queries that retrieve all data for a single hierarchy root (e.g., all orders for one customer)[1][4].
  • Limited Data Volume per Hierarchy: Since Spanner has a maximum split size (currently 4 GB), interleaving is best suited for parent-child groups that won't grow too large under a single root. If you expect a single parent and all its children to approach this limit, interleaving may not be appropriate[4].
  • Not Needing Strict Referential Integrity: When your application can handle the absence of referential integrity at the database level, or you are enforcing it at the application level, interleaved tables can make sense. If you need the database to enforce integrity, use foreign keys instead[1][7].

Summary Table

Feature Interleaved Tables (Non-Parent Enforced) Foreign Keys
Data is physically colocated Yes No
Enforces referential integrity No Yes
Query performance for hierarchy High (due to colocation) Lower (data may be on different splits)
Split size limitation 4 GB per root row tree Not applicable

Example Scenario

Suppose you have a Projects table and a Resources table, where resources belong to projects and you often fetch all resources for a given project. By interleaving Resources in Projects, you can fetch all related data efficiently. However, if you remove a project row and do not explicitly handle cascading deletes, child resource rows could remain orphaned, since referential integrity is not enforced in this model[1][4].

Conclusion

Non-parent interleaves in Cloud Spanner refer to interleaved tables where physical storage is optimized for performance and locality, but referential integrity between tables is not enforced by the database system. Use this model when you prioritize locality and performance over strict referential constraints, and your data fits within split size limits[1][4][7].

Citations:


Approve interleaveIn Implementation

This method cleanly generalizes Spanner table interleaving to support both parent‐enforced and colocation-only (“non-parent enforced”) modes by:

  • Accepting a table name and an optional $parent flag (default false)
  • Building the correct command attributes via createCommand('interleaveInParent', …)
  • Returning an InterleaveDefinition for fluent chaining

The code follows established Blueprint patterns and correctly handles data-locality use cases where referential integrity may be enforced at the database or application level. No further changes required.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant