From d940fdd00056387a1d95a5d7f19276b46c49e934 Mon Sep 17 00:00:00 2001 From: shawkins Date: Fri, 28 Aug 2015 09:55:18 -0400 Subject: [PATCH] TEIID-3662: ensuring that the value set is ordered and updating the index usage costing Conflicts: engine/src/main/java/org/teiid/query/tempdata/IndexInfo.java engine/src/test/java/org/teiid/query/processor/TestTempTables.java --- .../jboss-as7/docs/teiid/teiid-releasenotes.html | 2 ++ .../java/org/teiid/query/tempdata/IndexInfo.java | 4 +--- .../java/org/teiid/query/tempdata/TempTable.java | 14 ++++++++------ .../teiid/query/processor/TestMaterialization.java | 4 ++-- .../org/teiid/query/processor/TestTempTables.java | 12 ++++++++++++ .../teiid/query/unittest/RealMetadataFactory.java | 5 ++++- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/build/kits/jboss-as7/docs/teiid/teiid-releasenotes.html b/build/kits/jboss-as7/docs/teiid/teiid-releasenotes.html index 7bc6b8a997..afb7f1d421 100644 --- a/build/kits/jboss-as7/docs/teiid/teiid-releasenotes.html +++ b/build/kits/jboss-as7/docs/teiid/teiid-releasenotes.html @@ -337,6 +337,8 @@

from 8.7.4

  • [TEIID-3658] - VDBs may start before transport service
  • +
  • [TEIID-3662] - IN predicate against a materialized/temp table index without an order by returns wrong results +
  • from 8.7.3

    diff --git a/engine/src/main/java/org/teiid/query/tempdata/IndexInfo.java b/engine/src/main/java/org/teiid/query/tempdata/IndexInfo.java index e789327bfb..c671e7c780 100644 --- a/engine/src/main/java/org/teiid/query/tempdata/IndexInfo.java +++ b/engine/src/main/java/org/teiid/query/tempdata/IndexInfo.java @@ -57,9 +57,7 @@ TupleBrowser createTupleBrowser() throws TeiidComponentException { } if (!valueSet.isEmpty()) { LogManager.logDetail(LogConstants.CTX_DQP, "Using index value set"); //$NON-NLS-1$ - if (ordering != null) { - sortValueSet(direction); - } + sortValueSet(direction); CollectionTupleSource cts = new CollectionTupleSource(valueSet.iterator()); return new TupleBrowser(this.table.getTree(), cts, direction); } diff --git a/engine/src/main/java/org/teiid/query/tempdata/TempTable.java b/engine/src/main/java/org/teiid/query/tempdata/TempTable.java index f8f9c7a75e..789ad5015b 100644 --- a/engine/src/main/java/org/teiid/query/tempdata/TempTable.java +++ b/engine/src/main/java/org/teiid/query/tempdata/TempTable.java @@ -571,13 +571,15 @@ private long estimateCost(OrderBy orderBy, IndexInfo ii, long rowCost) { additionalCost = (64 - Long.numberOfLeadingZeros(initialCost - 1)); rowCost /= 3; } - if (rowCost > 1 && (!ii.covering || (orderBy != null && ii.ordering == null))) { + if (rowCost > 1 && !ii.covering) { + //primary lookup + additionalCost += rowCost * (64 - Long.numberOfLeadingZeros(rowCost - 1)); + } + if (rowCost > 1 && orderBy != null && ii.ordering != null) { //pk order or non-covered ordering - rowCost *= (64 - Long.numberOfLeadingZeros(rowCost - 1)); - if (!ii.covering) { - //primary lookup - rowCost *= 2; - } + //TODO: this should be based upon the filtered rowCost, but instead it is + //written as a bonus + additionalCost -= Math.min(additionalCost, rowCost * (64 - Long.numberOfLeadingZeros(rowCost - 1))); } return rowCost + additionalCost; } diff --git a/engine/src/test/java/org/teiid/query/processor/TestMaterialization.java b/engine/src/test/java/org/teiid/query/processor/TestMaterialization.java index a69f844fcf..c88bf969d9 100644 --- a/engine/src/test/java/org/teiid/query/processor/TestMaterialization.java +++ b/engine/src/test/java/org/teiid/query/processor/TestMaterialization.java @@ -157,8 +157,8 @@ private void execute(String sql, List... expectedResults) throws Exception { } @Test public void testNonCoveringSecondaryIndex() throws Exception { - execute("SELECT * from vgroup5 where y in ('zne', 'zwo') order by y desc", Arrays.asList("two", "zwo", 1), Arrays.asList("one", "zne", 1)); - execute("SELECT * from vgroup5 where y is null", Arrays.asList((String)null, (String)null, 1)); + execute("SELECT * from vgroup5 where y in ('zwo', 'zne') order by y desc", Arrays.asList("two", "zwo", 1), Arrays.asList("one", "zne", 1)); + execute("SELECT * from vgroup5 where y is null", Arrays.asList((String)null, (String)null, 1), Arrays.asList(" b", (String)null, 1), Arrays.asList(" c", (String)null, 1), Arrays.asList(" d", (String)null, 1)); execute("SELECT * from vgroup5 where y is null and z = 2"); } diff --git a/engine/src/test/java/org/teiid/query/processor/TestTempTables.java b/engine/src/test/java/org/teiid/query/processor/TestTempTables.java index 9fd5944acd..97da2fc345 100644 --- a/engine/src/test/java/org/teiid/query/processor/TestTempTables.java +++ b/engine/src/test/java/org/teiid/query/processor/TestTempTables.java @@ -577,5 +577,17 @@ private void sampleTable() throws Exception { //multiple out of order values execute("select x.e1 from x inner join /*+ makeind */ x1 on x.e3 = x1.e3 and x.e2 = x1.e2", new List[] {Arrays.asList("2"), Arrays.asList("1")}); //$NON-NLS-1$ } + + @Test public void testIndexInPredicate() throws Exception { + execute("create local temporary table x (e1 string, e2 integer, primary key (e1))", new List[] {Arrays.asList(0)}); //$NON-NLS-1$ + + //the issue is only apparent when the values are from different pages + for (int i = 0; i < 2048; i++) { + execute("insert into x (e2, e1) values ("+i+", '"+i+"')", new List[] {Arrays.asList(1)}); //$NON-NLS-1$ + } + + execute("select e2, e1 from x where e1 in ('2000', '1')", new List[] {Arrays.asList(1, "1"), Arrays.asList(2000, "2000")}); //$NON-NLS-1$ + + } } diff --git a/engine/src/test/java/org/teiid/query/unittest/RealMetadataFactory.java b/engine/src/test/java/org/teiid/query/unittest/RealMetadataFactory.java index ccbcbd99f2..32460b8928 100644 --- a/engine/src/test/java/org/teiid/query/unittest/RealMetadataFactory.java +++ b/engine/src/test/java/org/teiid/query/unittest/RealMetadataFactory.java @@ -499,7 +499,10 @@ public static TransformationMetadata exampleMaterializedView() { new String[] { DataTypeManager.DefaultDataTypes.STRING}); //non-covering index - QueryNode vTrans5 = new QueryNode("SELECT x, 'z' || substring(x, 2) as y, 1 as z FROM matsrc"); //$NON-NLS-1$ //$NON-NLS-2$ + QueryNode vTrans5 = new QueryNode("SELECT x, 'z' || substring(x, 2) as y, 1 as z FROM matsrc " + + "union all SELECT ifnull(x, ' ') || 'b', 'x' || substring(x, 2) as y, 1 as z FROM matsrc " + + "union all SELECT ifnull(x, ' ') || 'c', 'y' || substring(x, 2) as y, 1 as z FROM matsrc " + + "union all SELECT ifnull(x, ' ') || 'd', 'w' || substring(x, 2) as y, 1 as z FROM matsrc"); Table vGroup5 = createVirtualGroup("VGroup5", virtModel, vTrans5); //$NON-NLS-1$ vGroup5.setMaterialized(true); List vElements5 = createElements(vGroup5,