From e113d5dda1e3429d98751e136e2d7296c277ab8d Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 16 Apr 2018 19:46:33 +0300 Subject: [PATCH] SQL: Fix bug in transforming Expression properties (elastic/x-pack-elasticsearch#4373) Fix bug in checking whether the expression properties has been modified or not. Change test to move from SIN to SQRT (as the former seem to return different values (extra digit) across JDK versions - 8 vs 10) Fix elastic/x-pack-elasticsearch#4335 Original commit: elastic/x-pack-elasticsearch@8f672c455d7ee265c54e120e550f52efd7fde7b8 --- .../elasticsearch/xpack/sql/plan/QueryPlan.java | 16 ++++++++-------- .../logical/command/sys/SysTablesTests.java | 7 +++++++ .../qa/sql/jdbc/DatabaseMetaDataTestCase.java | 17 +++++++++++++++++ .../xpack/qa/sql/rest/RestSqlTestCase.java | 6 +++--- ... => setup_mock_metadata_get_table_types.sql} | 0 5 files changed, 35 insertions(+), 11 deletions(-) rename qa/sql/src/main/resources/{setup_mock_metadata_get_table_type.sql => setup_mock_metadata_get_table_types.sql} (100%) diff --git a/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/QueryPlan.java b/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/QueryPlan.java index 5d748b54a3d..e56aa7819fc 100644 --- a/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/QueryPlan.java +++ b/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/QueryPlan.java @@ -5,13 +5,6 @@ */ package org.elasticsearch.xpack.sql.plan; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Function; - import org.elasticsearch.xpack.sql.expression.Attribute; import org.elasticsearch.xpack.sql.expression.AttributeSet; import org.elasticsearch.xpack.sql.expression.Expression; @@ -19,6 +12,13 @@ import org.elasticsearch.xpack.sql.tree.Location; import org.elasticsearch.xpack.sql.tree.Node; import org.elasticsearch.xpack.sql.type.DataType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; + /** * There are two main types of plans, {@code LogicalPlan} and {@code PhysicalPlan} */ @@ -83,7 +83,7 @@ public abstract class QueryPlan> extends No boolean hasChanged = false; for (Object e : c) { Object next = doTransformExpression(e, traversal); - if (!c.equals(next)) { + if (!e.equals(next)) { hasChanged = true; } else { diff --git a/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java b/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java index a1b96373748..212327b7268 100644 --- a/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java +++ b/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java @@ -129,6 +129,13 @@ public class SysTablesTests extends ESTestCase { }, index, alias); } + public void testSysTablesWithCatalogOnlyAliases() throws Exception { + executeCommand("SYS TABLES CATALOG LIKE '%' LIKE 'test' TYPE 'ALIAS'", r -> { + assertEquals(1, r.size()); + assertEquals("alias", r.column(2)); + }, alias); + } + public void testSysTablesWithInvalidType() throws Exception { ParsingException pe = expectThrows(ParsingException.class, () -> sql("SYS TABLES LIKE 'test' TYPE 'QUE HORA ES'")); assertEquals("line 1:2: Invalid table type [QUE HORA ES]", pe.getMessage()); diff --git a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/DatabaseMetaDataTestCase.java b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/DatabaseMetaDataTestCase.java index 86dc4f5a9a8..a5f3a5f8364 100644 --- a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/DatabaseMetaDataTestCase.java +++ b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc/DatabaseMetaDataTestCase.java @@ -71,6 +71,23 @@ public class DatabaseMetaDataTestCase extends JdbcIntegrationTestCase { } } + public void testGetTableTypes() throws Exception { + index("test1", body -> body.field("name", "bob")); + index("test2", body -> body.field("name", "bob")); + + try (Connection h2 = LocalH2.anonymousDb(); Connection es = esJdbc()) { + h2.createStatement().executeUpdate("RUNSCRIPT FROM 'classpath:/setup_mock_metadata_get_table_types.sql'"); + + CheckedSupplier all = () -> h2.createStatement() + .executeQuery("SELECT '" + clusterName() + "' AS TABLE_CAT, * FROM mock"); + assertResultSets(all.get(), es.getMetaData().getTables("%", "%", "%", new String[] { "BASE TABLE" })); + assertResultSets( + h2.createStatement() + .executeQuery("SELECT '" + clusterName() + "' AS TABLE_CAT, * FROM mock WHERE TABLE_NAME = 'test1'"), + es.getMetaData().getTables("%", "%", "test1", new String[] { "BASE TABLE" })); + } + } + public void testColumns() throws Exception { index("test1", body -> body.field("name", "bob")); index("test2", body -> { diff --git a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java index 8860758cf66..32f9d5caeb4 100644 --- a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java +++ b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java @@ -84,7 +84,7 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); String request = "{\"query\":\"" - + " SELECT text, number, SIN(number) AS s, SCORE()" + + " SELECT text, number, SQRT(number) AS s, SCORE()" + " FROM test" + " ORDER BY number, SCORE()\", " + "\"mode\":\"" + mode + "\", " @@ -109,8 +109,8 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe columnInfo(mode, "SCORE()", "float", JDBCType.REAL, 15))); } expected.put("rows", Arrays.asList( - Arrays.asList("text" + i, i, Math.sin(i), 1.0), - Arrays.asList("text" + (i + 1), i + 1, Math.sin(i + 1), 1.0))); + Arrays.asList("text" + i, i, Math.sqrt(i), 1.0), + Arrays.asList("text" + (i + 1), i + 1, Math.sqrt(i + 1), 1.0))); cursor = (String) response.remove("cursor"); assertResponse(expected, response); assertNotNull(cursor); diff --git a/qa/sql/src/main/resources/setup_mock_metadata_get_table_type.sql b/qa/sql/src/main/resources/setup_mock_metadata_get_table_types.sql similarity index 100% rename from qa/sql/src/main/resources/setup_mock_metadata_get_table_type.sql rename to qa/sql/src/main/resources/setup_mock_metadata_get_table_types.sql