From 31027d65240012a5a0ef5001ea5aaba5e6645a25 Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Fri, 24 Apr 2020 16:20:34 +0300 Subject: [PATCH] Reject queries that act on nested fields or fields with nested field types in their hierarchy. --- .../xpack/eql/analysis/AnalysisUtils.java | 14 ++++++++-- .../querydsl/container/QueryContainer.java | 3 --- .../xpack/eql/analysis/VerifierTests.java | 27 ++++++++++++------- .../src/test/resources/mapping-nested.json | 3 +++ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java index 51764b139c2c5..bf9ab750b5c23 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java @@ -86,11 +86,21 @@ else if (DataTypes.isUnsupported(fa.dataType())) { "Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "]"); } } - // compound fields - else if (allowCompound == false && DataTypes.isPrimitive(fa.dataType()) == false) { + // compound fields that are not of "nested" type + else if (allowCompound == false && DataTypes.isPrimitive(fa.dataType()) == false && fa.dataType() != DataTypes.NESTED) { named = u.withUnresolvedMessage( "Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] only its subfields"); } + // "nested" fields + else if (fa.dataType() == DataTypes.NESTED) { + named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] " + + "due to nested fields not being supported yet"); + } + // fields having nested parents + else if (fa.isNested()) { + named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] " + + "with unsupported nested type in hierarchy (field [" + fa.nestedParent().name() +"])"); + } } return named; } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java index cb33e41b016c8..c903616c4f6f5 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java @@ -95,9 +95,6 @@ private Tuple asFieldExtraction(Attribute attr) if (expression instanceof FieldAttribute) { FieldAttribute fa = (FieldAttribute) expression; - if (fa.isNested()) { - throw new UnsupportedOperationException("Nested not yet supported"); - } return new Tuple<>(this, topHitFieldRef(fa)); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java index 9e97614b8a879..20fd5de9eee8d 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java @@ -288,10 +288,13 @@ public void testObject() { public void testNested() { final IndexResolution idxr = loadIndexResolution("mapping-nested.json"); - accept(idxr, "foo where processes.pid == 0"); - + assertEquals("1:11: Cannot use field [processes] type [nested] due to nested fields not being supported yet", + error(idxr, "foo where processes == 0")); + assertEquals("1:11: Cannot use field [processes.pid] type [long] with unsupported nested type in hierarchy (field [processes])", + error(idxr, "foo where processes.pid == 0")); assertEquals("1:11: Unknown column [processe.pid], did you mean any of [processes.pid, processes.path, processes.path.keyword]?", error(idxr, "foo where processe.pid == 0")); + accept(idxr, "foo where long_field == 123"); } public void testGeo() { @@ -322,20 +325,24 @@ public void testMultiField() { accept(idxr, "foo where multi_field_options.raw == 'bar'"); accept(idxr, "foo where multi_field_options.key == 'bar'"); - accept(idxr, "foo where multi_field_ambiguous.one == 'bar'"); accept(idxr, "foo where multi_field_ambiguous.two == 'bar'"); + assertEquals("1:11: [multi_field_ambiguous.normalized == 'bar'] cannot operate on first argument field of data type [keyword]: " + "Normalized keyword field cannot be used for exact match operations", error(idxr, "foo where multi_field_ambiguous.normalized == 'bar'")); - - assertEquals("1:11: [multi_field_nested.dep_name == 'bar'] cannot operate on first argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [dep_name]; define one or use MATCH/QUERY instead", + assertEquals("1:11: Cannot use field [multi_field_nested.dep_name] type [text] with unsupported nested type in hierarchy " + + "(field [multi_field_nested])", error(idxr, "foo where multi_field_nested.dep_name == 'bar'")); - - accept(idxr, "foo where multi_field_nested.dep_id.keyword == 'bar'"); - accept(idxr, "foo where multi_field_nested.end_date == ''"); - accept(idxr, "foo where multi_field_nested.start_date == 'bar'"); + assertEquals("1:11: Cannot use field [multi_field_nested.dep_id.keyword] type [keyword] with unsupported nested type in " + + "hierarchy (field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.dep_id.keyword == 'bar'")); + assertEquals("1:11: Cannot use field [multi_field_nested.end_date] type [datetime] with unsupported nested type in " + + "hierarchy (field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.end_date == ''")); + assertEquals("1:11: Cannot use field [multi_field_nested.start_date] type [datetime] with unsupported nested type in " + + "hierarchy (field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.start_date == 'bar'")); } public void testStringFunctionWithText() { diff --git a/x-pack/plugin/eql/src/test/resources/mapping-nested.json b/x-pack/plugin/eql/src/test/resources/mapping-nested.json index 5105e56cd2778..6ab53cb2cc479 100644 --- a/x-pack/plugin/eql/src/test/resources/mapping-nested.json +++ b/x-pack/plugin/eql/src/test/resources/mapping-nested.json @@ -25,6 +25,9 @@ } } } + }, + "long_field" : { + "type" : "long" } } }