Skip to content

Instantly share code, notes, and snippets.

@vlsi
Last active August 29, 2015 14:10
Show Gist options
  • Save vlsi/e08f53b3fd0b3db465c0 to your computer and use it in GitHub Desktop.
Save vlsi/e08f53b3fd0b3db465c0 to your computer and use it in GitHub Desktop.
Test query
select *
from (select 2+pPartkey pPartkey, 3+pPartkey pPartkey2 from part) p
where
exists (
select 1
from (select psPartkey+1 psPartkey from partsupp) ps
where psPartkey=p.pPartkey2
and exists (
select 2
from (select psPartkey+3 p3, psPartkey+4 p4, psPartkey+5 p5 from partsupp)
where p3=ps.psPartkey and p4=p.pPartkey and p5=p.pPartkey2
)
)
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[IS NOT NULL($t2)], proj#0..1=[{exprs}], $condition=[$t3])
EnumerableCorrelate(correlation=[$cor3], joinType=[LEFT_OUTER], requiredColumns=[{0, 1}])
EnumerableCalc(expr#0=[{inputs}], expr#1=[2], expr#2=[+($t1, $t0)], expr#3=[3], expr#4=[+($t3, $t0)], pPartkey=[$t2], pPartkey2=[$t4])
EnumerableTableScan(table=[[tpch, part]])
EnumerableAggregate(group=[{}], agg#0=[MIN($0)])
EnumerableCalc(expr#0..1=[{inputs}], expr#2=[true], expr#3=[$cor3], expr#4=[$t3.pPartkey2], expr#5=[=($t0, $t4)], expr#6=[IS NOT NULL($t1)], expr#7=[AND($t5, $t6)], $f0=[$t2], $condition=[$t7])
EnumerableCorrelate(correlation=[$cor0], joinType=[LEFT_OUTER], requiredColumns=[{0}])
EnumerableCalc(expr#0..1=[{inputs}], expr#2=[1], expr#3=[+($t0, $t2)], psPartkey=[$t3])
EnumerableTableScan(table=[[tpch, partsupp]])
EnumerableAggregate(group=[{}], agg#0=[MIN($0)])
EnumerableCalc(expr#0..1=[{inputs}], expr#2=[true], expr#3=[3], expr#4=[+($t0, $t3)], expr#5=[$cor0], expr#6=[$t5.psPartkey], expr#7=[=($t4, $t6)], expr#8=[4], expr#9=[+($t0, $t8)], expr#10=[$cor3], expr#11=[$t10.pPartkey], expr#12=[=($t9, $t11)], expr#13=[5], expr#14=[+($t0, $t13)], expr#15=[$t10.pPartkey2], expr#16=[=($t14, $t15)], expr#17=[AND($t7, $t12, $t16)], $f0=[$t2], $condition=[$t17])
EnumerableTableScan(table=[[tpch, partsupp]])
// The java code does not yet run, however you might get feel of the "target" code.
// The idea is to produce `Enumerables.correlateJoin(left, Function1<Object[], Enumerable> right)`
public org.apache.calcite.linq4j.Enumerable bind(final org.apache.calcite.DataContext root0) {
root = root0;
final org.apache.calcite.linq4j.Enumerable _inputEnumerable = org.apache.calcite.linq4j.Linq4j.asEnumerable(((org.apache.calcite.tools.TpchSchema) ((org.apache.calcite.adapter.java.ReflectiveSchema) root.getRootSchema().getSubSchema("tpch").unwrap(org.apache.calcite.adapter.java.ReflectiveSchema.class)).getTarget()).part);
final org.apache.calcite.linq4j.AbstractEnumerable left = new org.apache.calcite.linq4j.AbstractEnumerable(){
public org.apache.calcite.linq4j.Enumerator enumerator() {
return new org.apache.calcite.linq4j.Enumerator(){
public final org.apache.calcite.linq4j.Enumerator inputEnumerator = _inputEnumerable.enumerator();
public void reset() {
inputEnumerator.reset();
}
public boolean moveNext() {
return inputEnumerator.moveNext();
}
public void close() {
inputEnumerator.close();
}
public Object current() {
final int current = org.apache.calcite.runtime.SqlFunctions.toInt(inputEnumerator.current());
return new Object[] {
2 + current,
3 + current};
}
};
}
};
final org.apache.calcite.linq4j.Enumerable _inputEnumerable0 = org.apache.calcite.runtime.Enumerables.correlateJoin(left, new org.apache.calcite.linq4j.function.Function1() {
public void apply(final Object[] tst) {
final org.apache.calcite.linq4j.Enumerable _inputEnumerable = org.apache.calcite.linq4j.Linq4j.asEnumerable(((org.apache.calcite.tools.TpchSchema) ((org.apache.calcite.adapter.java.ReflectiveSchema) root.getRootSchema().getSubSchema("tpch").unwrap(org.apache.calcite.adapter.java.ReflectiveSchema.class)).getTarget()).partsupp);
final org.apache.calcite.linq4j.AbstractEnumerable left = new org.apache.calcite.linq4j.AbstractEnumerable(){
public org.apache.calcite.linq4j.Enumerator enumerator() {
return new org.apache.calcite.linq4j.Enumerator(){
public final org.apache.calcite.linq4j.Enumerator inputEnumerator = _inputEnumerable.enumerator();
public void reset() {
inputEnumerator.reset();
}
public boolean moveNext() {
return inputEnumerator.moveNext();
}
public void close() {
inputEnumerator.close();
}
public Object current() {
return ((org.apache.calcite.tools.TpchSchema.PartSupp) inputEnumerator.current()).psPartkey + 1;
}
};
}
};
final org.apache.calcite.linq4j.Enumerable _inputEnumerable0 = org.apache.calcite.runtime.Enumerables.correlateJoin(left, new org.apache.calcite.linq4j.function.Function1() {
public void apply(final int tst) {
final org.apache.calcite.linq4j.Enumerable _inputEnumerable = org.apache.calcite.linq4j.Linq4j.asEnumerable(((org.apache.calcite.tools.TpchSchema) ((org.apache.calcite.adapter.java.ReflectiveSchema) root.getRootSchema().getSubSchema("tpch").unwrap(org.apache.calcite.adapter.java.ReflectiveSchema.class)).getTarget()).partsupp);
final org.apache.calcite.linq4j.AbstractEnumerable child = new org.apache.calcite.linq4j.AbstractEnumerable(){
public org.apache.calcite.linq4j.Enumerator enumerator() {
return new org.apache.calcite.linq4j.Enumerator(){
public final org.apache.calcite.linq4j.Enumerator inputEnumerator = _inputEnumerable.enumerator();
public void reset() {
inputEnumerator.reset();
}
public boolean moveNext() {
while (inputEnumerator.moveNext()) {
final int inp0_ = ((org.apache.calcite.tools.TpchSchema.PartSupp) inputEnumerator.current()).psPartkey;
if (inp0_ + 3 == CF$psPartkey && inp0_ + 4 == CF$pPartkey && inp0_ + 5 == CF$pPartkey2) {
return true;
}
}
return false;
}
public void close() {
inputEnumerator.close();
}
public Object current() {
return true;
}
};
}
};
}
public Object apply(final Integer tst) {
return apply(
tst.intValue());
}
public Object apply(final Object tst) {
return apply(
(Integer) tst);
}
}
);
final org.apache.calcite.linq4j.AbstractEnumerable child = new org.apache.calcite.linq4j.AbstractEnumerable(){
public org.apache.calcite.linq4j.Enumerator enumerator() {
return new org.apache.calcite.linq4j.Enumerator(){
public final org.apache.calcite.linq4j.Enumerator inputEnumerator = _inputEnumerable0.enumerator();
public void reset() {
inputEnumerator.reset();
}
public boolean moveNext() {
while (inputEnumerator.moveNext()) {
final Object[] current = (Object[]) inputEnumerator.current();
if (org.apache.calcite.runtime.SqlFunctions.toInt(current[0]) == CF$pPartkey2 && (Boolean) current[1] != null) {
return true;
}
}
return false;
}
public void close() {
inputEnumerator.close();
}
public Object current() {
return true;
}
};
}
};
}
public Object apply(final Object tst) {
return apply(
(Object[]) tst);
}
}
);
return new org.apache.calcite.linq4j.AbstractEnumerable(){
public org.apache.calcite.linq4j.Enumerator enumerator() {
return new org.apache.calcite.linq4j.Enumerator(){
public final org.apache.calcite.linq4j.Enumerator inputEnumerator = _inputEnumerable0.enumerator();
public void reset() {
inputEnumerator.reset();
}
public boolean moveNext() {
while (inputEnumerator.moveNext()) {
if ((Boolean) ((Object[]) inputEnumerator.current())[2] != null) {
return true;
}
}
return false;
}
public void close() {
inputEnumerator.close();
}
public Object current() {
final Object[] current = (Object[]) inputEnumerator.current();
return new Object[] {
current[0],
current[1]};
}
};
}
};
}
public java.lang.reflect.Type getElementType() {
return java.lang.Object[].class;
}
LogicalProject(pPartkey=[$0], pPartkey2=[$1])
LogicalFilter(condition=[IS NOT NULL($2)])
LogicalCorrelate(correlation=[$cor3], joinType=[LEFT_OUTER], requiredColumns=[{0, 1}])
LogicalProject(pPartkey=[+(2, $0)], pPartkey2=[+(3, $0)])
EnumerableTableScan(table=[[tpch, part]])
LogicalAggregate(group=[{}], agg#0=[MIN($0)])
LogicalProject($f0=[true])
LogicalProject(EXPR$0=[1])
LogicalFilter(condition=[AND(=($0, $cor3.pPartkey2), IS NOT NULL($1))])
LogicalCorrelate(correlation=[$cor0], joinType=[LEFT_OUTER], requiredColumns=[{0}])
LogicalProject(psPartkey=[+($0, 1)])
EnumerableTableScan(table=[[tpch, partsupp]])
LogicalAggregate(group=[{}], agg#0=[MIN($0)])
LogicalProject($f0=[true])
LogicalProject(EXPR$0=[2])
LogicalFilter(condition=[AND(=($0, $cor0.psPartkey), =($1, $cor3.pPartkey), =($2, $cor3.pPartkey2))])
LogicalProject(p3=[+($0, 3)], p4=[+($0, 4)], p5=[+($0, 5)])
EnumerableTableScan(table=[[tpch, partsupp]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment