You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "simonvandel (via GitHub)" <gi...@apache.org> on 2023/12/16 21:43:26 UTC

[PR] Add ForeignKey constraint type [arrow-datafusion]

simonvandel opened a new pull request, #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   While toying around with Datafusion, I would like to use foreign key relationship metadata.
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   Extended `Constraint` with `ForeignKey`.
   Added conversion from sqslparser's `TableConstraint`.
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   No, what would be a good place to add tests?
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   
   Yes.
   
   It's now possible to mark columns as foreign keys to other tables.
   
   Adding a new enum variant is a breaking change.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "simonvandel (via GitHub)" <gi...@apache.org>.
simonvandel closed pull request #8566: Add ForeignKey constraint type
URL: https://github.com/apache/arrow-datafusion/pull/8566


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#issuecomment-2059169224

   Hi @simonvandel. Do you still plan on working on this?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#discussion_r1443401501


##########
datafusion/common/src/functional_dependencies.rs:
##########
@@ -97,8 +108,39 @@ impl Constraints {
                         Constraint::Unique(indices)
                     })
                 }
-                TableConstraint::ForeignKey { .. } => {
-                    _plan_err!("Foreign key constraints are not currently supported")
+                TableConstraint::ForeignKey {
+                    foreign_table,
+                    columns,
+                    name: _,
+                    on_delete: _,
+                    on_update: _,
+                    referred_columns,
+                } => {
+                    let get_indices = |c: &[Ident]| {

Review Comment:
   Isn't this code looking up the 'reference columns' in the current table's schema (`df_schema`)?  I think the column references should be to the schema of `foreign_table`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#issuecomment-1859146819

   Thank you @simonvandel  -- the basic idea looks good to me. It seems we have a few CI failures on this PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "simonvandel (via GitHub)" <gi...@apache.org>.
simonvandel commented on code in PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#discussion_r1443687586


##########
datafusion/common/src/functional_dependencies.rs:
##########
@@ -97,8 +108,39 @@ impl Constraints {
                         Constraint::Unique(indices)
                     })
                 }
-                TableConstraint::ForeignKey { .. } => {
-                    _plan_err!("Foreign key constraints are not currently supported")
+                TableConstraint::ForeignKey {
+                    foreign_table,
+                    columns,
+                    name: _,
+                    on_delete: _,
+                    on_update: _,
+                    referred_columns,
+                } => {
+                    let get_indices = |c: &[Ident]| {

Review Comment:
   You were of course right.
   I have added a test in sql_integration.rs now.
   Since we don't have the schema of the referenced table, I had to change the referred_indices to be a referred_columns instead.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "gruuya (via GitHub)" <gi...@apache.org>.
gruuya commented on code in PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#discussion_r1457266257


##########
datafusion/common/src/functional_dependencies.rs:
##########
@@ -97,8 +108,38 @@ impl Constraints {
                         Constraint::Unique(indices)
                     })
                 }
-                TableConstraint::ForeignKey { .. } => {
-                    _plan_err!("Foreign key constraints are not currently supported")
+                TableConstraint::ForeignKey {
+                    foreign_table,
+                    columns,
+                    name: _,
+                    on_delete: _,
+                    on_update: _,
+                    referred_columns,
+                } => {
+                    let column_indices = columns
+                        .iter()
+                        .map(|pk| {
+                            let idx = df_schema
+                                .fields()
+                                .iter()
+                                .position(|item| item.name() == &pk.value)
+                                .ok_or_else(|| {
+                                    DataFusionError::Plan(
+                                        "Column doesn't exist".to_string(),
+                                    )
+                                })?;
+                            Ok(idx)
+                        })

Review Comment:
   Could simplify to 
   ```rust
   .map(|pk| df_schema.index_of_column_by_name(None, pk.value))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#issuecomment-2066408660

   Thanks @simonvandel 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#issuecomment-2053849544

   Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add ForeignKey constraint type [arrow-datafusion]

Posted by "simonvandel (via GitHub)" <gi...@apache.org>.
simonvandel commented on PR #8566:
URL: https://github.com/apache/arrow-datafusion/pull/8566#issuecomment-2059188229

   Hi @andygrove
   I don't have a use case anymore, so I'll just close the PR.
   
   If someone else wants the changes, they can use my changes in a new PR. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org