Skip to content
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

SQL: Fix issue with IN not resolving to underlying keyword field #38440

Merged
merged 3 commits into from
Feb 6, 2019

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Feb 5, 2019

  • Add resolution to the exact keyword field (if exists) for text fields.
  • Add proper verification and error message if underlying keyword
    doesn't exist.
  • Move check for field attribute in the comparison list to the
    resolveType() method of IN.

Fixes: #38424

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

Add resolution to the exact keyword field (if exists) for text fields.
Add proper verification and error message if underlying keyword doesn't
exist.
Move check for field attribute in the comparison list to the
`resolveType()` method of `IN`.

Fixes: elastic#38424
Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall. Left two comments.

if (fa.isInexact()) {
name = fa.exactAttribute().name();
}
q = new TermsQuery(in.source(), name, in.list());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal preference is for q = new TermsQuery(in.source(), fa.isInexact() ? fa.exactAttribute().name() : fa.name(), in.list());.

try {
((FieldAttribute) value).exactAttribute();
} catch (MappingException ex) {
return new TypeResolution(format(null, "[{}] cannot operate on first argument field of data type [{}]",
Copy link
Contributor

@astefan astefan Feb 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is the correct error message here. Looking at the test here I think it will be confusing for users as to why they get that error message. Meaning, text is a text field, and the query is WHERE text IN ('foo', 'bar') (everything is text) but the error message says one cannot use text. I think it would help users if the error message would be a bit special for text: maybe say between brackets "needs to be of keyword type" or something similar - give a hint to the user for the reason of that error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same message in First/Last.
The MappingException is thrown in this case (text with no underlying keyword) but since it's a generic exception I was "afraid" to add a specific message for the case. (More like future proof driven).
@costin what do you think?

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - left some comments mainly related to style.
I wonder if we should consider supporting text fields as well...

@@ -105,6 +108,26 @@ protected Pipe makePipe() {
return new InPipe(source(), this, children().stream().map(Expressions::pipe).collect(Collectors.toList()));
}

@Override
protected TypeResolution resolveType() {
if (value instanceof FieldAttribute) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying error message should bubble up since it's important. It might make sense for these cases to have a non-exception method as the MappingException itself seems to heavy for the analyzer.

Copy link
Contributor Author

@matriv matriv Feb 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue to improve this (the exception part) globally: #38501

}
}

Optional<Expression> firstNotFoldable = list.stream().filter(expression -> !expression.foldable()).findFirst();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more readable through a basic for loop and early return

for (Expression ex : list) {
    if(ex.foldable() == false) {
         return new TypeResolution...
    }
}

About the same amount of lines, straight-forward and faster.

@matriv
Copy link
Contributor Author

matriv commented Feb 6, 2019

@costin addressed comments.

@matriv
Copy link
Contributor Author

matriv commented Feb 6, 2019

@elasticmachine run elasticsearch-ci/default-distro

@@ -410,7 +410,8 @@ public void testInWithFieldInListOfValues() {
}

public void testInOnFieldTextWithNoKeyword() {
assertEquals("1:26: [IN] cannot operate on first argument field of data type [text]",
assertEquals("1:26: [IN] cannot operate on field of data type [text]: " +
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this one.

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jasontedor jasontedor added v8.0.0 and removed v7.0.0 labels Feb 6, 2019
@matriv matriv added the v7.0.0 label Feb 6, 2019
@matriv matriv merged commit 861eee7 into elastic:master Feb 6, 2019
@matriv matriv deleted the mt/fix-38437 branch February 6, 2019 14:19
matriv added a commit that referenced this pull request Feb 6, 2019
)

- Add resolution to the exact keyword field (if exists) for text fields.
- Add proper verification and error message if underlying keyword
doesn'texist.
- Move check for field attribute in the comparison list to the
`resolveType()` method of `IN`.

Fixes: #38424
matriv added a commit that referenced this pull request Feb 6, 2019
)

- Add resolution to the exact keyword field (if exists) for text fields.
- Add proper verification and error message if underlying keyword
doesn'texist.
- Move check for field attribute in the comparison list to the
`resolveType()` method of `IN`.

Fixes: #38424
matriv added a commit that referenced this pull request Feb 6, 2019
)

- Add resolution to the exact keyword field (if exists) for text fields.
- Add proper verification and error message if underlying keyword
doesn'texist.
- Move check for field attribute in the comparison list to the
`resolveType()` method of `IN`.

Fixes: #38424
matriv added a commit that referenced this pull request Feb 6, 2019
)

- Add resolution to the exact keyword field (if exists) for text fields.
- Add proper verification and error message if underlying keyword
doesn'texist.
- Move check for field attribute in the comparison list to the
`resolveType()` method of `IN`.

Fixes: #38424
@matriv
Copy link
Contributor Author

matriv commented Feb 6, 2019

Backported to 7.x with f96bd2a
to 7.0 with 5e798c1
to 6.7 with f5e4de7
to 6.6 with 841eb71

jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Feb 6, 2019
* master:
  ML: update set_upgrade_mode, add logging (elastic#38372)
  bad formatted JSON object (elastic#38515) (elastic#38525)
  Fix HistoryIntegrationTests timestamp comparsion (elastic#38505)
  SQL: Fix issue with IN not resolving to underlying keyword field (elastic#38440)
  Fix the clock resolution to millis in ScheduledEventTests (elastic#38506)
@colings86 colings86 removed the v7.0.0 label Feb 7, 2019
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Feb 8, 2019
* 6.6: (121 commits)
  [DOCS] Add warning about bypassing ML PUT APIs (elastic#38608)
  fix dissect doc "ip" --> "clientip" (elastic#38512)
  bad formatted JSON object (elastic#38515)
  SQL: Fix issue with IN not resolving to underlying keyword field (elastic#38440)
  Update ilm-api.asciidoc, point to REMOVE policy (elastic#38235)
  Backport changes to the release notes script. (elastic#38347)
  Change the milliseconds precision to 3 digits for intervals. (elastic#38297)
  SecuritySettingsSource license.self_generated: trial (elastic#38233) (elastic#38398)
  Fix IndexAuditTrail rolling upgrade on rollover edge 2 (elastic#38286) (elastic#38381)
  Cleanup construction of interceptors (elastic#38388)
  Skip unsupported languages for tests (elastic#38328) (elastic#38385)
  [ILM][TEST] increase assertBusy timeout (elastic#36864) (elastic#38354)
  Docs: Drop inline callout from scroll example (elastic#38340) (elastic#38365)
  Preserve ILM operation mode when creating new lifecycles (elastic#38134) (elastic#38230)
  [ML] Add explanation so far to file structure finder exceptions (elastic#38337)
  ML: Fix error race condition on stop _all datafeeds and close _all jobs (elastic#38113) (elastic#38211) (elastic#38222)
  SQL: Generate relevant error message when grouping functions are not used in GROUP BY (elastic#38017)
  Fix NPE in Logfile Audit Filter (elastic#38120) (elastic#38273)
  Enable trace log in FollowerFailOverIT (elastic#38148)
  Replace awaitBusy with assertBusy in atLeastDocsIndexed (elastic#38190)
  ...
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Feb 11, 2019
* master: (27 commits)
  Mute AnalysisModuleTests#testStandardFilterBWC (elastic#38636)
  add geotile_grid ref to asciidoc (elastic#38632)
  Enable Dockerfile from artifacts.elastic.co (elastic#38552)
  Mute FollowerFailOverIT testFailOverOnFollower (elastic#38634)
  Account for a possible rolled over file while reading the audit log file (elastic#34909)
  Mute failure in InternalEngineTests (elastic#38622)
  Fix Issue with Concurrent Snapshot Init + Delete (elastic#38518)
  Refactor ZonedDateTime.now in millis resolution (elastic#38577)
  Mute failing WatchStatusIntegrationTests (elastic#38621)
  Mute failing  ApiKeyIntegTests (elastic#38614)
  [DOCS] Add warning about bypassing ML PUT APIs (elastic#38509)
  Add 7.1 and 8.0 version constants to master (elastic#38514)
  ML: update set_upgrade_mode, add logging (elastic#38372)
  bad formatted JSON object (elastic#38515) (elastic#38525)
  Fix HistoryIntegrationTests timestamp comparsion (elastic#38505)
  SQL: Fix issue with IN not resolving to underlying keyword field (elastic#38440)
  Fix the clock resolution to millis in ScheduledEventTests (elastic#38506)
  Enable BWC after backport recovering leases (elastic#38485)
  Collapse retention lease integration tests (elastic#38483)
  TransportVerifyShardBeforeCloseAction should force a flush (elastic#38401)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL: IN does not behave as expected
7 participants