You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "MaxGekk (via GitHub)" <gi...@apache.org> on 2023/05/29 11:56:54 UTC

[GitHub] [spark] MaxGekk opened a new pull request, #41368: [WIP][SQL] Improve suggested candidates for unresolved attribute

MaxGekk opened a new pull request, #41368:
URL: https://github.com/apache/spark/pull/41368

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #41368:
URL: https://github.com/apache/spark/pull/41368#issuecomment-1568484289

   > Someone might follow the suggestion and use __auto_generated_subquery_name.c1 in their query, only to have a later update to Spark change the internal name. Not sure if that's in scope here.
   
   I do believe it is out of scope of this PR. Let's strip the prefix separately.


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1210403462


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,

Review Comment:
   Unfortunately, not. In some cases, the caller have to deal with attribute sub-classes where `qualifier` is defined as:
   ```
     override def qualifier: Seq[String] = throw new UnresolvedException("qualifier")
   ```
   and there is not method which returns all name parts.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1209915958


##########
sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala:
##########
@@ -2240,7 +2240,7 @@ class DatasetSuite extends QueryTest
       sqlState = None,
       parameters = Map(
         "objectName" -> "`nonexisting`",
-        "proposal" -> "`map`, `other.column`"))
+        "proposal" -> "`map`, `other`.`column`"))

Review Comment:
   this is a bug, the column name is indeed `other.column`



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #41368:
URL: https://github.com/apache/spark/pull/41368#issuecomment-1571449066

   > Someone might follow the suggestion and use __auto_generated_subquery_name.c1 in their query, only to have a later update to Spark change the internal name. Not sure if that's in scope here.
   
   Here is the PR https://github.com/apache/spark/pull/41411


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] bersprockets commented on pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "bersprockets (via GitHub)" <gi...@apache.org>.
bersprockets commented on PR #41368:
URL: https://github.com/apache/spark/pull/41368#issuecomment-1567622960

   I don't know all the details about prefixes (how many parts there can be, etc.), but given that, this looks fine to me.
   
   Related to this area, but not caused by this PR, is the oddity of seeing auto-generated prefixes, which I assume are internal to Spark, in the list of suggestions:
   ```
   with v1 as (
    select * from values (1, 2) as (c1, c2)
   ),
   v2 as (
     select * from values (2, 3) as (c1, c2)
   )
   select v1.b
   from (
     select coalesce(v1.c1, v2.c1) as c1, v1.c1 as v1_c1, v1.c2 as v1_c2, v2.c1 as v2_c1, v2.c2 as v2_c2
     from v1
     full outer join v2
     on v1.c1 = v2.c1
   );
   [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column or function parameter with name `v1`.`b` cannot be resolved. Did you mean one of the following?
   [`__auto_generated_subquery_name`.`c1`, `__auto_generated_subquery_name`.`v1_c1`, `__auto_generated_subquery_name`.`v1_c2`, `__auto_generated_subquery_name`.`v2_c1`, `__auto_generated_subquery_name`.`v2_c2`].; line 7 pos 7;
   ```
   Someone might follow the suggestion and use `__auto_generated_subquery_name.c1` in their query, only to have a later update to Spark change the internal name. Not sure if that's in scope here.


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1210409898


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,

Review Comment:
   Also, the caller gets struct fields (not attributes).



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on a diff in pull request #41368: [WIP][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1209517197


##########
sql/core/src/test/resources/sql-tests/results/identifier-clause.sql.out:
##########
@@ -535,7 +535,7 @@ Catalog Name	spark_catalog
 Comment	some comment
 Location [not included in comparison]/{warehouse_dir}/someloc
 Namespace Name	ident
-Owner	runner
+Owner	maximgekk

Review Comment:
   Probably it is related to https://github.com/apache/spark/pull/41007. @srielau @gengliangwang Is it possible to rewrite the test in a way to don't depend of local settings. Otherwise it will bother every time while re-generating golden files.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk closed pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk closed pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute
URL: https://github.com/apache/spark/pull/41368


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #41368:
URL: https://github.com/apache/spark/pull/41368#issuecomment-1567496549

   @cloud-fan @vitaliili-db @bersprockets @srielau Could you review this PR, please.


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1210318525


##########
sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala:
##########
@@ -2240,7 +2240,7 @@ class DatasetSuite extends QueryTest
       sqlState = None,
       parameters = Map(
         "objectName" -> "`nonexisting`",
-        "proposal" -> "`map`, `other.column`"))
+        "proposal" -> "`map`, `other`.`column`"))

Review Comment:
   Yep, it is.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1210405700


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,

Review Comment:
   I would try to do such refactoring outside of the PR since it is not related to the algorithm which we focus on.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1209895436


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,
       testStrings: Seq[String]): Seq[String] = {

Review Comment:
   ditto, it should be `Seq[Seq[String]]`



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1211875623


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -82,36 +82,26 @@ object StringUtils extends Logging {
 
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,
-      testStrings: Seq[String]): Seq[String] = {
-    // This method is used to generate suggested list of candidates closest to `baseString` from the
-    // list of `testStrings`. Spark uses it to clarify error message in case a query refers to non
-    // existent column or attribute. The `baseString` could be single part or multi part and this
-    // method will try to match suggestions.
-    // Note that identifiers from `testStrings` could represent columns or attributes from different
-    // catalogs, schemas or tables. We preserve suggested identifier prefix and reconstruct
-    // multi-part identifier after ordering if there are more than one unique prefix in a list. This
-    // will also reconstruct multi-part identifier for the cases of nested columns. E.g. for a
-    // table `t` with columns `a`, `b`, `c.d` (nested) and requested column `d` we will create
-    // prefixes `t`, `t`, and `t.c`. Since there is more than one distinct prefix we will return
-    // sorted suggestions as multi-part identifiers => (`t`.`c`.`d`, `t`.`a`, `t`.`b`).
-    val multiPart = UnresolvedAttribute.parseAttributeName(baseString).size > 1
-    if (multiPart) {
-      testStrings.sortBy(LevenshteinDistance.getDefaultInstance.apply(_, baseString))
-    } else {
-      val split = testStrings.map { ident =>
-        val parts = UnresolvedAttribute.parseAttributeName(ident).map(quoteIfNeeded)
-        (parts.init.mkString("."), parts.last)
-      }
-      val sorted =
-        split.sortBy(pair => LevenshteinDistance.getDefaultInstance.apply(pair._2, baseString))
-      if (sorted.map(_._1).toSet.size == 1) {
-        // All identifier belong to the same relation
-        sorted.map(_._2)
+      candidates: Seq[Seq[String]]): Seq[String] = {
+    val baseParts = UnresolvedAttribute.parseAttributeName(baseString)
+    val strippedCandidates =
+      // Group by the qualifier. If all identifiers have the same qualifier, strip it.
+      // For example: Seq(`abc`.`def`.`t1`, `abc`.`def`.`t2`) => Seq(`t1`, `t2`)
+      if (baseParts.size == 1 && candidates.groupBy(_.dropRight(1)).size == 1) {
+        candidates.map(_.takeRight(1))
+      // Group by the qualifier excluding table name. If all identifiers have the same prefix
+      // (namespace) excluding table names, strip this prefix.
+      // For example: Seq(`abc`.`def`.`t1`, `abc`.`xyz`.`t2`) => Seq(`def`.`t1`, `xyz`.`t2`)
+      } else if (baseParts.size <= 2 && candidates.groupBy(_.dropRight(2)).size == 1) {
+        candidates.map(_.takeRight(2))
       } else {
-        // More than one relation
-        sorted.map(x => if (x._1.isEmpty) s"${x._2}" else s"${x._1}.${x._2}")
+        // Some candidates have different qualifiers
+        candidates
       }
-    }
+
+    strippedCandidates
+      .map(quoteNameParts)
+      .sortBy(LevenshteinDistance.getDefaultInstance.apply(_, baseString))

Review Comment:
   One followup we can do: instead of sorting by the quoted qualified name, we can sort by the `Seq[String]` directly. The algorithm should sort by the last name part first, and then the last second name part, and so on.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #41368:
URL: https://github.com/apache/spark/pull/41368#issuecomment-1570680041

   Merging to master. Thank you, @cloud-fan @bersprockets for review.


-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1209894273


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,

Review Comment:
   can we let the caller pass the column name as `Seq[String]`.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,

Review Comment:
   can we let the caller pass the column name as `Seq[String]`?



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] MaxGekk commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1211563753


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -83,35 +83,27 @@ object StringUtils extends Logging {
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,
       testStrings: Seq[String]): Seq[String] = {

Review Comment:
   done



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #41368: [SPARK-43867][SQL] Improve suggested candidates for unresolved attribute

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #41368:
URL: https://github.com/apache/spark/pull/41368#discussion_r1211875623


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringUtils.scala:
##########
@@ -82,36 +82,26 @@ object StringUtils extends Logging {
 
   private[spark] def orderSuggestedIdentifiersBySimilarity(
       baseString: String,
-      testStrings: Seq[String]): Seq[String] = {
-    // This method is used to generate suggested list of candidates closest to `baseString` from the
-    // list of `testStrings`. Spark uses it to clarify error message in case a query refers to non
-    // existent column or attribute. The `baseString` could be single part or multi part and this
-    // method will try to match suggestions.
-    // Note that identifiers from `testStrings` could represent columns or attributes from different
-    // catalogs, schemas or tables. We preserve suggested identifier prefix and reconstruct
-    // multi-part identifier after ordering if there are more than one unique prefix in a list. This
-    // will also reconstruct multi-part identifier for the cases of nested columns. E.g. for a
-    // table `t` with columns `a`, `b`, `c.d` (nested) and requested column `d` we will create
-    // prefixes `t`, `t`, and `t.c`. Since there is more than one distinct prefix we will return
-    // sorted suggestions as multi-part identifiers => (`t`.`c`.`d`, `t`.`a`, `t`.`b`).
-    val multiPart = UnresolvedAttribute.parseAttributeName(baseString).size > 1
-    if (multiPart) {
-      testStrings.sortBy(LevenshteinDistance.getDefaultInstance.apply(_, baseString))
-    } else {
-      val split = testStrings.map { ident =>
-        val parts = UnresolvedAttribute.parseAttributeName(ident).map(quoteIfNeeded)
-        (parts.init.mkString("."), parts.last)
-      }
-      val sorted =
-        split.sortBy(pair => LevenshteinDistance.getDefaultInstance.apply(pair._2, baseString))
-      if (sorted.map(_._1).toSet.size == 1) {
-        // All identifier belong to the same relation
-        sorted.map(_._2)
+      candidates: Seq[Seq[String]]): Seq[String] = {
+    val baseParts = UnresolvedAttribute.parseAttributeName(baseString)
+    val strippedCandidates =
+      // Group by the qualifier. If all identifiers have the same qualifier, strip it.
+      // For example: Seq(`abc`.`def`.`t1`, `abc`.`def`.`t2`) => Seq(`t1`, `t2`)
+      if (baseParts.size == 1 && candidates.groupBy(_.dropRight(1)).size == 1) {
+        candidates.map(_.takeRight(1))
+      // Group by the qualifier excluding table name. If all identifiers have the same prefix
+      // (namespace) excluding table names, strip this prefix.
+      // For example: Seq(`abc`.`def`.`t1`, `abc`.`xyz`.`t2`) => Seq(`def`.`t1`, `xyz`.`t2`)
+      } else if (baseParts.size <= 2 && candidates.groupBy(_.dropRight(2)).size == 1) {
+        candidates.map(_.takeRight(2))
       } else {
-        // More than one relation
-        sorted.map(x => if (x._1.isEmpty) s"${x._2}" else s"${x._1}.${x._2}")
+        // Some candidates have different qualifiers
+        candidates
       }
-    }
+
+    strippedCandidates
+      .map(quoteNameParts)
+      .sortBy(LevenshteinDistance.getDefaultInstance.apply(_, baseString))

Review Comment:
   One followup we can do: instead of sorting by the quoted qualified name, we can sort by the `Seq[String]` directly. The algorithm should sort by the last name part first, and then the last second name part, and so on.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org