Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HashJoinSegment, a virtual segment for joins. (#9111)
* Add HashJoinSegment, a virtual segment for joins. An initial step towards #8728. This patch adds enough functionality to implement a joining cursor on top of a normal datasource. It does not include enough to actually do a query. For that, future patches will need to wire this low-level functionality into the query language. * Fixups. * Fix missing format argument. * Various tests and minor improvements. * Changes. * Remove or add tests for unused stuff. * Fix up package locations.
- Loading branch information
Showing
79 changed files
with
6,804 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.druid.math.expr; | ||
|
||
import org.apache.druid.java.util.common.Pair; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Stack; | ||
|
||
public class Exprs | ||
{ | ||
/** | ||
* Decomposes any expr into a list of exprs that, if ANDed together, are equivalent to the input expr. | ||
* | ||
* @param expr any expr | ||
* | ||
* @return list of exprs that, if ANDed together, are equivalent to the input expr | ||
*/ | ||
public static List<Expr> decomposeAnd(final Expr expr) | ||
{ | ||
final List<Expr> retVal = new ArrayList<>(); | ||
final Stack<Expr> stack = new Stack<>(); | ||
stack.push(expr); | ||
|
||
while (!stack.empty()) { | ||
final Expr current = stack.pop(); | ||
|
||
if (current instanceof BinAndExpr) { | ||
stack.push(((BinAndExpr) current).right); | ||
stack.push(((BinAndExpr) current).left); | ||
} else { | ||
retVal.add(current); | ||
} | ||
} | ||
|
||
return retVal; | ||
} | ||
|
||
/** | ||
* Decomposes an equality expr into the left- and right-hand side. | ||
* | ||
* @return decomposed equality, or empty if the input expr was not an equality expr | ||
*/ | ||
public static Optional<Pair<Expr, Expr>> decomposeEquals(final Expr expr) | ||
{ | ||
if (expr instanceof BinEqExpr) { | ||
return Optional.of(Pair.of(((BinEqExpr) expr).left, ((BinEqExpr) expr).right)); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
core/src/test/java/org/apache/druid/common/config/NullHandlingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.druid.common.config; | ||
|
||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class NullHandlingTest | ||
{ | ||
@BeforeClass | ||
public static void setUpClass() | ||
{ | ||
NullHandling.initializeForTests(); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_float() | ||
{ | ||
Assert.assertEquals( | ||
NullHandling.replaceWithDefault() ? 0f : null, | ||
NullHandling.defaultValueForClass(Float.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_double() | ||
{ | ||
Assert.assertEquals( | ||
NullHandling.replaceWithDefault() ? 0d : null, | ||
NullHandling.defaultValueForClass(Double.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_integer() | ||
{ | ||
Assert.assertNull(NullHandling.defaultValueForClass(Integer.class)); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_long() | ||
{ | ||
Assert.assertEquals( | ||
NullHandling.replaceWithDefault() ? 0L : null, | ||
NullHandling.defaultValueForClass(Long.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_number() | ||
{ | ||
Assert.assertEquals( | ||
NullHandling.replaceWithDefault() ? 0d : null, | ||
NullHandling.defaultValueForClass(Number.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_string() | ||
{ | ||
Assert.assertEquals( | ||
NullHandling.replaceWithDefault() ? "" : null, | ||
NullHandling.defaultValueForClass(String.class) | ||
); | ||
} | ||
|
||
@Test | ||
public void test_defaultValueForClass_object() | ||
{ | ||
Assert.assertNull(NullHandling.defaultValueForClass(Object.class)); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
core/src/test/java/org/apache/druid/math/expr/ExprsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.druid.math.expr; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.druid.java.util.common.Pair; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class ExprsTest | ||
{ | ||
@Test | ||
public void test_decomposeAnd_notAnAnd() | ||
{ | ||
final List<Expr> decomposed = Exprs.decomposeAnd(new IdentifierExpr("foo")); | ||
|
||
// Expr instances don't, in general, implement value-based equals and hashCode. So we need to verify each field. | ||
Assert.assertEquals(1, decomposed.size()); | ||
Assert.assertThat(decomposed.get(0), CoreMatchers.instanceOf(IdentifierExpr.class)); | ||
Assert.assertEquals("foo", ((IdentifierExpr) decomposed.get(0)).getIdentifier()); | ||
} | ||
|
||
@Test | ||
public void test_decomposeAnd_basic() | ||
{ | ||
final List<Expr> decomposed = Exprs.decomposeAnd( | ||
new BinAndExpr( | ||
"&&", | ||
new BinAndExpr("&&", new IdentifierExpr("foo"), new IdentifierExpr("bar")), | ||
new BinAndExpr("&&", new IdentifierExpr("baz"), new IdentifierExpr("qux")) | ||
) | ||
); | ||
|
||
// Expr instances don't, in general, implement value-based equals and hashCode. So we need to verify each field. | ||
Assert.assertEquals(4, decomposed.size()); | ||
|
||
for (Expr expr : decomposed) { | ||
Assert.assertThat(expr, CoreMatchers.instanceOf(IdentifierExpr.class)); | ||
} | ||
|
||
final List<String> identifiers = decomposed.stream() | ||
.map(expr -> ((IdentifierExpr) expr).getIdentifier()) | ||
.collect(Collectors.toList()); | ||
|
||
Assert.assertEquals( | ||
ImmutableList.of("foo", "bar", "baz", "qux"), | ||
identifiers | ||
); | ||
} | ||
|
||
@Test | ||
public void test_decomposeEquals_notAnEquals() | ||
{ | ||
final Optional<Pair<Expr, Expr>> optionalPair = Exprs.decomposeEquals(new IdentifierExpr("foo")); | ||
Assert.assertFalse(optionalPair.isPresent()); | ||
} | ||
|
||
@Test | ||
public void test_decomposeEquals_basic() | ||
{ | ||
final Optional<Pair<Expr, Expr>> optionalPair = Exprs.decomposeEquals( | ||
new BinEqExpr( | ||
"==", | ||
new IdentifierExpr("foo"), | ||
new IdentifierExpr("bar") | ||
) | ||
); | ||
|
||
Assert.assertTrue(optionalPair.isPresent()); | ||
|
||
final Pair<Expr, Expr> pair = optionalPair.get(); | ||
Assert.assertThat(pair.lhs, CoreMatchers.instanceOf(IdentifierExpr.class)); | ||
Assert.assertThat(pair.rhs, CoreMatchers.instanceOf(IdentifierExpr.class)); | ||
Assert.assertEquals("foo", ((IdentifierExpr) pair.lhs).getIdentifier()); | ||
Assert.assertEquals("bar", ((IdentifierExpr) pair.rhs).getIdentifier()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.