You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "yyy1000 (via GitHub)" <gi...@apache.org> on 2024/05/07 22:27:00 UTC

[PR] enable user defined display_name [datafusion]

yyy1000 opened a new pull request, #10417:
URL: https://github.com/apache/datafusion/pull/10417

   ## 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 #10376 .
   
   ## 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.  
   -->
   
   ## 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?
   
   <!--
   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)?
   -->
   
   ## 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.
   -->
   


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

Posted by "yyy1000 (via GitHub)" <gi...@apache.org>.
yyy1000 commented on code in PR #10417:
URL: https://github.com/apache/datafusion/pull/10417#discussion_r1593359897


##########
datafusion/functions/src/core/getfield.rs:
##########
@@ -50,10 +50,31 @@ impl ScalarUDFImpl for GetFieldFunc {
     fn as_any(&self) -> &dyn Any {
         self
     }
+
     fn name(&self) -> &str {
         "get_field"
     }
 
+    fn display_name(&self, args: &[Expr]) -> Result<String> {
+        if args.len() != 2 {
+            return exec_err!(
+                "get_field function requires 2 arguments, got {}",
+                args.len()
+            );
+        }
+
+        let name = match &args[1] {
+            Expr::Literal(name) => name,
+            _ => {
+                return exec_err!(
+                    "get_field function requires the argument field_name to be a string"
+                );
+            }
+        };
+
+        Ok(format!("{}[{}]", args[0].display_name()?, name))
+    }
+

Review Comment:
   Yeah, this example has better view. 🙌



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

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

   It's good now. @jayzhan211 You can review it when you are available. :)
   In this example https://github.com/apache/datafusion/blob/96487ea0cbb7901a1e4aa18fdf6deb8961319fea/datafusion/core/tests/user_defined/user_defined_aggregates.rs#L165-L176, after `apply_function_rewrites`, the logical plan would not create an alias because the `display_name` of `get_field` is the same as `sq.first[value]`
   
   In this PR:
   "| initial_logical_plan                                       | Projection: (sq.first)[value], (sq.first)[time]                                                                                                                                                                         |",
       "|                                                            |   SubqueryAlias: sq                                                                                                                                                                                                     |",
       "|                                                            |     Projection: first(t.value,t.time) AS first                                                                                                                                                                          |",
       "|                                                            |       Aggregate: groupBy=[[]], aggr=[[first(t.value, t.time)]]                                                                                                                                                          |",
       "|                                                            |         TableScan: t                                                                                                                                                                                                    |",
       "| logical_plan after apply_function_rewrites                 | Projection: get_field(sq.first, Utf8(\"value\")), get_field(sq.first, Utf8(\"time\"))                                                                                                                                       |",
       "|                                                            |   SubqueryAlias: sq                                                                                                                                                                                                     |",
       "|                                                            |     Projection: first(t.value,t.time) AS first                                                                                                                                                                          |",
       "|                                                            |       Aggregate: groupBy=[[]], aggr=[[first(t.value, t.time)]]                                                                                                                                                          |",
       "|                                                            |         TableScan: t                                                                                                                                                                                                    |",
   
   
   Before this PR:
   "| initial_logical_plan                                       | Projection: (sq.first)[value], (sq.first)[time]                                                                                                                                                                         |",
       "|                                                            |   SubqueryAlias: sq                                                                                                                                                                                                     |",
       "|                                                            |     Projection: first(t.value,t.time) AS first                                                                                                                                                                          |",
       "|                                                            |       Aggregate: groupBy=[[]], aggr=[[first(t.value, t.time)]]                                                                                                                                                          |",
       "|                                                            |         TableScan: t                                                                                                                                                                                                    |",
       "| logical_plan after apply_function_rewrites                 | Projection: get_field(sq.first, Utf8(\"value\")) AS sq.first[value], get_field(sq.first, Utf8(\"time\")) AS sq.first[time]                                                                                                  |",
       "|                                                            |   SubqueryAlias: sq                                                                                                                                                                                                     |",
       "|                                                            |     Projection: first(t.value,t.time) AS first                                                                                                                                                                          |",
       "|                                                            |       Aggregate: groupBy=[[]], aggr=[[first(t.value, t.time)]]                                                                                                                                                          |",
       "|                                                            |         TableScan: t                                                                                                                                                                                                    |",
       "| logical_plan after inline_table_scan                       | SAME TEXT AS ABOVE


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

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

   Thanks @yyy1000  and @jayzhan211 


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10417:
URL: https://github.com/apache/datafusion/pull/10417#discussion_r1593356686


##########
datafusion/functions/src/core/getfield.rs:
##########
@@ -50,10 +50,31 @@ impl ScalarUDFImpl for GetFieldFunc {
     fn as_any(&self) -> &dyn Any {
         self
     }
+
     fn name(&self) -> &str {
         "get_field"
     }
 
+    fn display_name(&self, args: &[Expr]) -> Result<String> {
+        if args.len() != 2 {
+            return exec_err!(
+                "get_field function requires 2 arguments, got {}",
+                args.len()
+            );
+        }
+
+        let name = match &args[1] {
+            Expr::Literal(name) => name,
+            _ => {
+                return exec_err!(
+                    "get_field function requires the argument field_name to be a string"
+                );
+            }
+        };
+
+        Ok(format!("{}[{}]", args[0].display_name()?, name))
+    }
+

Review Comment:
   cli
   ```
   DataFusion CLI v38.0.0
   > select struct('a');
   +-------------------+
   | struct(Utf8("a")) |
   +-------------------+
   | {c0: a}           |
   +-------------------+
   1 row(s) fetched. 
   Elapsed 0.023 seconds.
   
   > select get_field(struct('a'), 'c0');
   +-----------------------+
   | struct(Utf8("a"))[c0] |
   +-----------------------+
   | a                     |
   +-----------------------+
   1 row(s) fetched. 
   Elapsed 0.007 seconds.
   ```



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

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

   Do you mind adding `display_name` for `get_field` that shows what I expect in the example given in #10376?


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10417:
URL: https://github.com/apache/datafusion/pull/10417#discussion_r1593356686


##########
datafusion/functions/src/core/getfield.rs:
##########
@@ -50,10 +50,31 @@ impl ScalarUDFImpl for GetFieldFunc {
     fn as_any(&self) -> &dyn Any {
         self
     }
+
     fn name(&self) -> &str {
         "get_field"
     }
 
+    fn display_name(&self, args: &[Expr]) -> Result<String> {
+        if args.len() != 2 {
+            return exec_err!(
+                "get_field function requires 2 arguments, got {}",
+                args.len()
+            );
+        }
+
+        let name = match &args[1] {
+            Expr::Literal(name) => name,
+            _ => {
+                return exec_err!(
+                    "get_field function requires the argument field_name to be a string"
+                );
+            }
+        };
+
+        Ok(format!("{}[{}]", args[0].display_name()?, name))
+    }
+

Review Comment:
   cli in this PR
   ```
   DataFusion CLI v38.0.0
   > select struct('a');
   +-------------------+
   | struct(Utf8("a")) |
   +-------------------+
   | {c0: a}           |
   +-------------------+
   1 row(s) fetched. 
   Elapsed 0.023 seconds.
   
   > select get_field(struct('a'), 'c0');
   +-----------------------+
   | struct(Utf8("a"))[c0] |
   +-----------------------+
   | a                     |
   +-----------------------+
   1 row(s) fetched. 
   Elapsed 0.007 seconds.
   ```
   
   main branch:
   ```
   DataFusion CLI v38.0.0
   > select struct('a');
   +-------------------+
   | struct(Utf8("a")) |
   +-------------------+
   | {c0: a}           |
   +-------------------+
   1 row(s) fetched. 
   Elapsed 0.014 seconds.
   
   > select get_field(struct('a'), 'c0');
   +-----------------------------------------+
   | get_field(struct(Utf8("a")),Utf8("c0")) |
   +-----------------------------------------+
   | a                                       |
   +-----------------------------------------+
   1 row(s) fetched. 
   Elapsed 0.004 seconds.
   ```



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

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

   > Do you mind adding `display_name` for `get_field` that shows what I expect in the example given in #10376?
   
   Cool, will do that.


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Enable user defined display_name for ScalarUDF [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #10417:
URL: https://github.com/apache/datafusion/pull/10417


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org