Skip to content

Commit

Permalink
TEIID-4812: Processor is not exiting the join as soon as it's determi…
Browse files Browse the repository at this point in the history
…ned one side contains 0 rows (addressing issues related to no rows and larger reads from)

temp tables
  • Loading branch information
shawkins authored and jolee committed Mar 30, 2017
1 parent eb95130 commit 8a410eb
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ <h4 class="western">from ${project.version}</h4>
<a href='https://issues.jboss.org/browse/TEIID-4793'>TEIID-4793</a> - Change of class loading no longer requires module dependencies
<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-4812'>TEIID-4812</a> - Processor is not exiting the join as soon as it's determined one side contains 0 rows (addressing issues related to no rows and larger reads from)
<li/>
<p style="margin-bottom: 0in">
<a href='https://issues.jboss.org/browse/TEIID-4817'>TEIID-4817</a> - Infinispan DSL Resource Adapter: can't query after JDG restart
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,8 @@ private static float estimatePredicateCost(float childCost, PlanNode currentNode
if (dsc.getNdv() == UNKNOWN_VALUE) {
return childCost / 3;
}

cost = childCost * dsc.getNdv() / ndv;
cost = childCost * dsc.getNdv() / Math.max(1, ndv);
}

if (cost == UNKNOWN_VALUE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.teiid.core.TeiidComponentException;
import org.teiid.query.analysis.AnalysisRecord;
import org.teiid.query.metadata.QueryMetadataInterface;
import org.teiid.query.metadata.TempMetadataAdapter;
import org.teiid.query.optimizer.capabilities.CapabilitiesFinder;
import org.teiid.query.optimizer.relational.OptimizerRule;
import org.teiid.query.optimizer.relational.RuleStack;
Expand Down Expand Up @@ -265,10 +266,14 @@ static boolean insertSort(PlanNode childNode, List<Expression> expressions, Plan
boolean sort = true;

if (sourceNode.getType() == NodeConstants.Types.ACCESS) {
if (distinct || NewCalculateCostUtil.usesKey(sourceNode, expressions, metadata)) {
boolean usesKey = NewCalculateCostUtil.usesKey(sourceNode, expressions, metadata);
if (distinct || usesKey) {
joinNode.setProperty(joinNode.getFirstChild() == childNode ? NodeConstants.Info.IS_LEFT_DISTINCT : NodeConstants.Info.IS_RIGHT_DISTINCT, true);
}
if (attemptPush && RuleRaiseAccess.canRaiseOverSort(sourceNode, metadata, capFinder, sortNode, null, false, true)) {
if (!usesKey && RuleRaiseAccess.getModelIDFromAccess(sourceNode, metadata) == TempMetadataAdapter.TEMP_MODEL) {
attemptPush = false;
}
if (attemptPush && RuleRaiseAccess.canRaiseOverSort(sourceNode, metadata, capFinder, sortNode, null, false, true)) {
sourceNode.getFirstChild().addAsParent(sortNode);

if (needsCorrection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ protected void loadRight() throws TeiidComponentException,
}
}
if (this.processingSortLeft != SortOption.NOT_SORTED && this.processingSortRight != SortOption.NOT_SORTED) {
if (this.leftSource.rowCountLE(0)) {
return;
}
super.loadRight();
super.loadLeft();
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
Expand Down Expand Up @@ -348,6 +351,9 @@ private boolean shouldIndex(SourceState possibleIndex, SourceState other) throws
int schemaSize = this.joinNode.getBufferManager().getSchemaSize(other.getSource().getOutputElements());
int toReserve = this.joinNode.getBufferManager().getMaxProcessingSize();
int minSize = toReserve/schemaSize*this.joinNode.getBatchSize();
if (otherSize != -1 && otherSize < this.joinNode.getBatchSize()) {
//return false;
}

//determine sizes in an incremental fashion as to avoid a full buffer of the unsorted side
while (size < Integer.MAX_VALUE && (indexSize == -1 || otherSize == -1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ protected TupleBatch nextBatchDirect() throws BlockedException,
}
} catch (BlockedException e) {
if (!isDependent()) {
if (getJoinType() != JoinType.JOIN_FULL_OUTER && this.joinStrategy.leftSource.getSortUtility() == null && this.joinStrategy.leftSource.rowCountLE(0)) {
this.terminateBatches();
return pullBatch();
}
this.joinStrategy.openRight();
this.joinStrategy.loadRight();
prefetch(this.joinStrategy.rightSource, this.joinStrategy.leftSource);
Expand All @@ -220,6 +224,10 @@ protected TupleBatch nextBatchDirect() throws BlockedException,
}
try {
if (state == State.LOAD_RIGHT) {
if (getJoinType() != JoinType.JOIN_FULL_OUTER && this.joinStrategy.leftSource.getSortUtility() == null && this.joinStrategy.leftSource.rowCountLE(0)) {
this.terminateBatches();
return pullBatch();
}
this.joinStrategy.openRight();
this.joinStrategy.loadRight();
state = State.EXECUTE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.teiid.query.sql.lang.OrderBy;
import org.teiid.query.sql.lang.OrderByItem;
import org.teiid.query.sql.symbol.Expression;
import org.teiid.query.util.CommandContext;


/**
Expand Down Expand Up @@ -249,6 +250,14 @@ private TupleBuffer createTupleBuffer() throws TeiidComponentException {
* creates sorted sublists stored in tuplebuffers
*/
protected void initialSort(boolean onePass, boolean lowLatency) throws TeiidComponentException, TeiidProcessingException {
long end = Long.MAX_VALUE;
if (!nonBlocking) {
//obey the timeslice
CommandContext cc = CommandContext.getThreadLocalContext();
if (cc != null) {
end = System.nanoTime() + (cc.getTimeSliceEnd()-System.currentTimeMillis())*1000000;
}
}
outer: while (!doneReading) {

if (this.source != null) {
Expand All @@ -269,6 +278,9 @@ protected void initialSort(boolean onePass, boolean lowLatency) throws TeiidComp

if (onePass && lowLatency && this.workingBuffer.getRowCount() > 2*this.targetRowCount) {
break outer;
} else if (end != Long.MAX_VALUE && (this.workingBuffer.getRowCount()%32)==1 && System.nanoTime() > end) {
CommandContext.getThreadLocalContext().getWorkItem().moreWork();
throw BlockedException.block("Blocking on large sort"); //$NON-NLS-1$
}
} catch(BlockedException e) {
/*there are three cases here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public boolean rowCountLE(long count) throws TeiidComponentException, TeiidProce
if (getIncrementalRowCount(true) > count) {
return false;
}
prefetch(Long.MAX_VALUE);
prefetch(count + 1);
}
return buffer.getRowCount() <= count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class TestSourceHints {
ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());

List<?>[] expected = new List[] {};
helpProcess(plan, manager("foo x", "leading", "foo", "leading"), expected);
helpProcess(plan, manager("foo x", "leading", "foo x", "leading"), expected);
}

@Test public void testWithHintPushdown() throws TeiidException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@ public void run() {
@Test public void testCompositeKeyJoinUsesKeyOrder() throws Exception {
execute("create local temporary table x (e1 string, e2 integer, primary key (e1, e2))", new List[] {Arrays.asList(0)}); //$NON-NLS-1$
execute("create local temporary table x1 (e1 string, e2 integer)", new List[] {Arrays.asList(0)}); //$NON-NLS-1$
TestOptimizer.helpPlan("select * from x, x1 where x.e2 = x1.e2 and x.e1 = x1.e1", this.metadata, new String[] {"SELECT x1.e2, x1.e1 FROM x1 ORDER BY x1.e1, x1.e2", "SELECT x.e2, x.e1 FROM x ORDER BY x.e1, x.e2"}, ComparisonMode.EXACT_COMMAND_STRING);
TestOptimizer.helpPlan("select * from x, x1 where x.e2 = x1.e2 and x.e1 = x1.e1", this.metadata, new String[] {"SELECT x1.e2, x1.e1 FROM x1", "SELECT x.e2, x.e1 FROM x ORDER BY x.e1, x.e2"}, ComparisonMode.EXACT_COMMAND_STRING);
}

@Test public void testUnneededMergePredicate() throws Exception {
execute("create local temporary table x (e1 string, e2 integer, primary key (e1))", new List[] {Arrays.asList(0)}); //$NON-NLS-1$
execute("create local temporary table x1 (e1 string, e2 integer)", new List[] {Arrays.asList(0)}); //$NON-NLS-1$
TestOptimizer.helpPlan("select x.e1 from x makenotdep, x1 makenotdep where x.e2 = x1.e2 and x.e1 = x1.e1", this.metadata, new String[] {"SELECT x.e2, x.e1 FROM x ORDER BY x.e1", "SELECT x1.e2, x1.e1 FROM x1 ORDER BY x1.e1"}, ComparisonMode.EXACT_COMMAND_STRING);
TestOptimizer.helpPlan("select x.e1 from x makenotdep, x1 makenotdep where x.e2 = x1.e2 and x.e1 = x1.e1", this.metadata, new String[] {"SELECT x.e2, x.e1 FROM x ORDER BY x.e1", "SELECT x1.e2, x1.e1 FROM x1"}, ComparisonMode.EXACT_COMMAND_STRING);
execute("insert into x (e2, e1) values (2, 'b')", new List[] {Arrays.asList(1)}); //$NON-NLS-1$
execute("insert into x1 (e2, e1) values (3, 'b')", new List[] {Arrays.asList(1)}); //$NON-NLS-1$
execute("select x.e1 from x makenotdep, x1 makenotdep where x.e2 = x1.e2 and x.e1 = x1.e1", new List[0]); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class TestWithClauseProcessing {
sampleData1(dataManager);

ProcessorPlan plan = TestOptimizer.helpPlan(sql, RealMetadataFactory.example1Cached(), null, new DefaultCapabilitiesFinder(TestOptimizer.getTypicalCapabilities()),
new String[] {"SELECT a.x, a.z FROM a WHERE a.z = TRUE ORDER BY a.x", "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM pm1.g2 AS g_0 WHERE g_0.e1 IN (<dependent values>) ORDER BY c_0"}, ComparisonMode.EXACT_COMMAND_STRING);
new String[] {"SELECT a.x, a.z FROM a WHERE a.z = TRUE", "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM pm1.g2 AS g_0 WHERE g_0.e1 IN (<dependent values>) ORDER BY c_0"}, ComparisonMode.EXACT_COMMAND_STRING);

helpProcess(plan, dataManager, expected);
}
Expand Down Expand Up @@ -277,14 +277,14 @@ public class TestWithClauseProcessing {
RealMetadataFactory.setCardinality("pm1.g2", 100000, metadata);
String sql = "with a (x) as (select e1 from pm1.g1) SELECT a.x from pm1.g2, a where (pm1.g2.e1 = a.x)"; //$NON-NLS-1$

TestOptimizer.helpPlan(sql, metadata, null, TestOptimizer.getGenericFinder(false), new String[] {"SELECT g_0.e1 FROM pm1.g2 AS g_0 WHERE g_0.e1 IN (<dependent values>)", "SELECT a.x FROM a ORDER BY a.x"}, ComparisonMode.EXACT_COMMAND_STRING);
TestOptimizer.helpPlan(sql, metadata, null, TestOptimizer.getGenericFinder(false), new String[] {"SELECT g_0.e1 FROM pm1.g2 AS g_0 WHERE g_0.e1 IN (<dependent values>)", "SELECT a.x FROM a"}, ComparisonMode.EXACT_COMMAND_STRING);
}

@Test public void testWithJoinPlanning1() throws TeiidException {
TransformationMetadata metadata = RealMetadataFactory.example1Cached();
String sql = "with a (x) as (select e1 from pm1.g1) SELECT a.x from pm1.g2, a where (pm1.g2.e1 = a.x)"; //$NON-NLS-1$

TestOptimizer.helpPlan(sql, metadata, null, TestOptimizer.getGenericFinder(false), new String[] {"SELECT g_0.e1 FROM pm1.g2 AS g_0 WHERE g_0.e1 IN (<dependent values>)", "SELECT a.x FROM a ORDER BY a.x"}, ComparisonMode.EXACT_COMMAND_STRING);
TestOptimizer.helpPlan(sql, metadata, null, TestOptimizer.getGenericFinder(false), new String[] {"SELECT g_0.e1 FROM pm1.g2 AS g_0 WHERE g_0.e1 IN (<dependent values>)", "SELECT a.x FROM a"}, ComparisonMode.EXACT_COMMAND_STRING);
}

@Test public void testWithBlockingJoin() throws TeiidException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,31 @@ private void process(List[] expectedResults)
helpTestJoin();
}

@Test public void testFullOuterJoinNoRows() throws Exception {
this.joinType = JoinType.JOIN_FULL_OUTER;
this.leftTuples = new List[0];
this.rightTuples = new List[] {Arrays.asList(1) };
expected = new List[] {
Arrays.asList(new Object[] { null, 1 }),
};
expectedReversed = new List[] {
Arrays.asList(new Object[] { 1, null }),
};
helpTestJoin();
}

@Test public void testLeftJoinNoRows() throws Exception {
this.joinType = JoinType.JOIN_LEFT_OUTER;
this.leftTuples = new List[0];
this.rightTuples = new List[] {Arrays.asList(1) };
expected = new List[] {
};
expectedReversed = new List[] {
Arrays.asList(1, null)
};
helpTestJoin();
}

@Test public void testMergeJoinOptimizationRepeatedElements() throws Exception {
this.joinType = JoinType.JOIN_INNER;
this.leftTuples = createMultiColTuples(9, 2);
Expand Down

0 comments on commit 8a410eb

Please sign in to comment.