You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Weijun-H (via GitHub)" <gi...@apache.org> on 2023/03/16 23:44:10 UTC

[GitHub] [arrow-datafusion] Weijun-H opened a new pull request, #5625: feat: Quote column only if has special characters

Weijun-H opened a new pull request, #5625:
URL: https://github.com/apache/arrow-datafusion/pull/5625

   # 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 #5523
   
   # 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@arrow.apache.org

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


[GitHub] [arrow-datafusion] Weijun-H commented on a diff in pull request #5625: feat: Quote column only if has special characters

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


##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +112,22 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                let column_name = if self.name.chars().all(|c| c.is_ascii()) {

Review Comment:
   Thank you for decant explanation. It is much clear.



-- 
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


[GitHub] [arrow-datafusion] alamb commented on pull request #5625: feat: Quote column only if has special characters

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

   This PR seems to still have some CI failures -- marking as draft 


-- 
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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5625: feat: Quote column only if has special characters

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


##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +115,21 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                lazy_static! {
+                    static ref CAPTURE_VALID_RE: Regex =

Review Comment:
   I spent some more time testing this PR out locally and I found:
   1. `quoted_identifier` didn't seem to have many tests
   2. I think we still need to quote any identifier that has capitals (as it would be normalized to lowercase)
   
   I'll make a PR shortly with an alternate proposal



-- 
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


[GitHub] [arrow-datafusion] alamb closed pull request #5625: feat: Quote column only if has special characters

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed pull request #5625: feat: Quote column only if has special characters
URL: https://github.com/apache/arrow-datafusion/pull/5625


-- 
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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5625: feat: Quote column only if has special characters

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


##########
datafusion/common/Cargo.toml:
##########
@@ -44,10 +44,12 @@ arrow = { workspace = true, default-features = false }
 arrow-array = { version = "35.0.0", default-features = false, features = ["chrono-tz"] }
 chrono = { version = "0.4", default-features = false }
 cranelift-module = { version = "0.92.0", optional = true }
+lazy_static = "1.4.0"

Review Comment:
   I think these are already dependencies of other datafusion crates so this doesn't increase our overall dependency footprint



##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +115,21 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                lazy_static! {
+                    static ref CAPTURE_VALID_RE: Regex =

Review Comment:
   This seems like we could do this via a combination of 
   https://doc.rust-lang.org/std/primitive.char.html#method.is_ascii_digit
   https://doc.rust-lang.org/std/primitive.char.html#method.is_ascii_lowercase
   https://doc.rust-lang.org/std/primitive.char.html#method.is_ascii_uppercase



-- 
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


[GitHub] [arrow-datafusion] Jefffrey commented on a diff in pull request #5625: feat: Quote column only if has special characters

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


##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +114,18 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                let regexp = Regex::new(r"^[a-zA-Z][_a-zA-Z0-9]*$").unwrap();
+                let column_name = if regexp.is_match(&self.name) {
+                    self.name.clone()
+                } else {
+                    quote_identifier(&self.name)
+                };
+
+                format!("{}.{}", r.to_quoted_string(), column_name)

Review Comment:
   probably worth a follow on pr to handle similar logic in `to_quoted_string()` for `TableReference`



##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +114,18 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                let regexp = Regex::new(r"^[a-zA-Z][_a-zA-Z0-9]*$").unwrap();

Review Comment:
   should lazy static the regex initialization itself i think, e.g. https://github.com/apache/arrow-datafusion/blob/26e1b20ea3362ea62cb713004a0636b8af6a16d7/datafusion/physical-expr/src/regex_expressions.rs#L82-L84
   
   regarding the regex itself, i think identifiers are allowed to start with underscore, and if they have capitalized letters then they should be quoted, so regex would be like `^[_a-z][_a-z0-9]*$`



-- 
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


[GitHub] [arrow-datafusion] Weijun-H commented on a diff in pull request #5625: feat: Quote column only if has special characters

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


##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +114,18 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                let regexp = Regex::new(r"^[a-zA-Z][_a-zA-Z0-9]*$").unwrap();
+                let column_name = if regexp.is_match(&self.name) {
+                    self.name.clone()
+                } else {
+                    quote_identifier(&self.name)
+                };
+
+                format!("{}.{}", r.to_quoted_string(), column_name)

Review Comment:
   It is a good idea. I will open a new ticket for it.



-- 
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


[GitHub] [arrow-datafusion] Jefffrey commented on a diff in pull request #5625: feat: Quote column only if has special characters

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


##########
datafusion/common/src/column.rs:
##########
@@ -112,12 +112,22 @@ impl Column {
 
     /// Serialize column into a quoted flat name string
     pub fn quoted_flat_name(&self) -> String {
-        // TODO: quote identifiers only when special characters present
-        // see: https://github.com/apache/arrow-datafusion/issues/5523
         match &self.relation {
             Some(r) => {
-                format!("{}.{}", r.to_quoted_string(), quote_identifier(&self.name))
+                let column_name = if self.name.chars().all(|c| c.is_ascii()) {

Review Comment:
   the `is_ascii()` check isn't sufficient, as for example if you have a column with relation `data.base` and name of `column`, then when quoting you'd have to quote the relation and have output as `"data.base".column`, otherwise the output would be `data.base.name` which you can see is much more confusing (which is the relation part and which is the column part?)
   
   it's not limited to only dots. i believe the only time you have unquoted identifiers is if the identifier is composed of only alphanumeric characters and underscores, and it doesn't start with a numeric (so `abc_123` can stay unquoted, but `"123_abc"` must be quoted since begins with a number)
   
   check the official postgres documentation regarding this as we are similar to postgres in the behaviour: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS



-- 
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