Skip to content

Conversation

@Luegg
Copy link
Contributor

@Luegg Luegg commented Jun 18, 2021

Fixes #53001

Adds support for querying indices without columns. Previously, such queries failed.

@Luegg Luegg added >bug :Analytics/SQL SQL querying v8.0.0 Team:QL (Deprecated) Meta label for query languages team v7.14.0 v7.13.3 labels Jun 18, 2021
@Luegg Luegg requested review from astefan, bpintea, costin and matriv June 18, 2021 14:36
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (Team:QL)

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.
Left a comment on null vs emptyList. Small nit, the table name could be improve to no_columns, no_mapping or empty_index.


// the field exists, but cannot be expanded (no sub-fields)
if (expanded.isEmpty()) {
if (expanded == null) {
Copy link
Member

Choose a reason for hiding this comment

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

why not keep the empty list vs a null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's more or less what caused the bug. Previously, expansion errors resulted in an empty list but that's ambiguous if the star is expanded on an index without columns. In this case, it returns an empty list meaning "star has been expanded to no fields".

To distinguish between expansion errors and empty expansions I now had to encode the error case with a null return value.

Copy link
Member

Choose a reason for hiding this comment

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

You could return singletonList(ne) instead of passing null and treat it separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it, makes sense 👍

}
// qualifier resolves to a non-struct field and cannot be expanded
else if (DataTypes.isPrimitive(q.dataType())) {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

Use emptyList() instead of null - allows iteration without blowing up and it's still fast as it avoid any allocation.

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 you could simply return singleton(q) and avoid any special/null handling.

@Luegg
Copy link
Contributor Author

Luegg commented Jun 18, 2021

LGTM.
Left a comment on null vs emptyList. Small nit, the table name could be improve to no_columns, no_mapping or empty_index.

I first used empty_index everywhere but then realized that this can mean either "empty mapping" or "no documents". Looking at the terminology in the docs, "empty_mapping" would probably be most accurate?

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

LGTM. Left a few comments though. Thx!

@@ -0,0 +1,23 @@
selectConstAggregationWithGroupBy
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you choose to have those in .csv-spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they also behave differently in H2 and all return no rows. I'll add a comment to clarify this.

DROP TABLE IF EXISTS "no_cols";
CREATE TABLE "no_cols" ();

INSERT INTO "no_cols" DEFAULT VALUES;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this required?

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 also want to cover cases with empty documents indexed in the empty mapping index.

Copy link
Contributor

Choose a reason for hiding this comment

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

But now we only test a table with a row with no values, but not a completely empty table?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's true. Do you think it would be worth to also have an empty_empty_mapping index for covering the no rows cases? To some extent, the "no rows" cases are already covered by tests that filter out all records.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if that's too much, @costin, what do you think?

selectConstAggregation
SELECT MAX(1), SUM(2) FROM no_cols;

// fails in H2 but cannot be tested in CSV spec because datasets without columns cannot be parsed
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Just curious, why does it fail in H2?
  • Maybe you can open an issue to improve testing and have a way to assert no cols in csv in the feature and link it here.

Copy link
Contributor Author

@Luegg Luegg Jun 21, 2021

Choose a reason for hiding this comment

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

H2 fails with a syntax error:

Syntax error in SQL statement "SELECT
FROM PUBLIC.""no_cols""[*]"; expected "(, USE, AS, RIGHT, LEFT, FULL, INNER, JOIN, CROSS, NATURAL, ,, SELECT"; SQL statement:
CREATE FORCE VIEW PUBLIC._0 AS
SELECT
FROM PUBLIC."no_cols" [42001-197]

Looks like it uses a view to execute the subselect and the creation fails because it selects an empty list of columns. The latest version of H2 supports the query though, so #39895 would resolve the issue as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thx!

Copy link
Contributor

@bpintea bpintea left a comment

Choose a reason for hiding this comment

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

Nice. Lgtm.

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.

I've left few comments, all related to the same issue basically.

Comment on lines +82 to +85
Request request = new Request("POST", "/" + index + "/_bulk");
request.addParameter("refresh", "true");
request.setJsonEntity("{\"index\":{}\n{}\n" + "{\"index\":{}\n{}\n");
client.performRequest(request);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed? Creating an empty index is not enough?

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 think it's helpful to also check whether the behavior is correct with documents in an index with an empty mapping. That's why I'm indexing two empty documents.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Thanks.


a:i | b:l | c:i
---------------+---------------+---------------
1 |2 |1
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this COUNT 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's two empty docs in the index.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, missed that.

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 also initially split the request body in two lines to make it more explicit like this:

request.setJsonEntity(
    "{\"index\":{}\n{}\n" +
    "{\"index\":{}\n{}\n");

but spotless didn't like it and I had to squash it into one line...

1:i
---------------
1
1
Copy link
Contributor

Choose a reason for hiding this comment

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

Second 1 - where does it come from?

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

@Luegg Luegg merged commit 6f93303 into elastic:master Jun 23, 2021
@Luegg Luegg deleted the fix/selectIndexWoCols branch June 23, 2021 08:17
Luegg pushed a commit to Luegg/elasticsearch that referenced this pull request Jun 23, 2021
Fixes elastic#53001

Adds support for querying indices without columns. Previously, such queries failed.

(cherry picked from commit 6f93303)
Luegg pushed a commit to Luegg/elasticsearch that referenced this pull request Jun 23, 2021
Fixes elastic#53001

Adds support for querying indices without columns. Previously, such queries failed.

(cherry picked from commit 6f93303)
Luegg pushed a commit that referenced this pull request Jun 23, 2021
Fixes #53001

Adds support for querying indices without columns. Previously, such queries failed.

(cherry picked from commit 6f93303)
Luegg pushed a commit that referenced this pull request Jun 23, 2021
Fixes #53001

Adds support for querying indices without columns. Previously, such queries failed.

(cherry picked from commit 6f93303)
@joegallo
Copy link
Contributor

$ git log --oneline upstream/master | grep "Fix querying of indices without columns"
6f93303c36f SQL: Fix querying of indices without columns (#74312)
$ git log --oneline upstream/7.x | grep "Fix querying of indices without columns"
2bb34e48605 SQL: Fix querying of indices without columns (#74312) (#74466)
$ git log --oneline upstream/7.14 | grep "Fix querying of indices without columns"
2bb34e48605 SQL: Fix querying of indices without columns (#74312) (#74466)
$ git log --oneline upstream/7.13 | grep "Fix querying of indices without columns"
ae96371d822 SQL: Fix querying of indices without columns (#74312) (#74465)

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: a query touching an empty index shouldn't return an error message

8 participants