Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.query.filter.vector;

import org.apache.druid.math.expr.ExprEval;
import org.apache.druid.math.expr.ExpressionType;
import org.apache.druid.query.filter.DruidObjectPredicate;
import org.apache.druid.query.filter.DruidPredicateFactory;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.vector.VectorObjectSelector;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

public class MultiValueStringObjectVectorValueMatcher implements VectorValueMatcherFactory
{
protected final VectorObjectSelector selector;

public MultiValueStringObjectVectorValueMatcher(final VectorObjectSelector selector)
{
this.selector = selector;
}

@Override
public VectorValueMatcher makeMatcher(@Nullable String value)
{
return new BaseVectorValueMatcher(selector)
{
final VectorMatch match = VectorMatch.wrap(new int[selector.getMaxVectorSize()]);

@Override
public ReadableVectorMatch match(final ReadableVectorMatch mask, boolean includeUnknown)
{
final Object[] vector = selector.getObjectVector();
final int[] selection = match.getSelection();

int numRows = 0;

for (int i = 0; i < mask.getSelectionSize(); i++) {
final int rowNum = mask.getSelection()[i];
final Object val = vector[rowNum];
if (val instanceof List) {
for (Object o : (List) val) {
if ((o == null && includeUnknown) || Objects.equals(value, o)) {
selection[numRows++] = rowNum;
break;
}
}
} else {
if ((val == null && includeUnknown) || Objects.equals(value, val)) {
selection[numRows++] = rowNum;
}
}
}

match.setSelectionSize(numRows);
return match;
}
};
}

@Override
public VectorValueMatcher makeMatcher(Object matchValue, ColumnType matchValueType)
{
final ExprEval<?> eval = ExprEval.ofType(ExpressionType.fromColumnType(matchValueType), matchValue);
final ExprEval<?> castForComparison = ExprEval.castForEqualityComparison(eval, ExpressionType.STRING);
if (castForComparison == null || castForComparison.asString() == null) {
return VectorValueMatcher.allFalseObjectMatcher(selector);
}
return makeMatcher(castForComparison.asString());
}

@Override
public VectorValueMatcher makeMatcher(DruidPredicateFactory predicateFactory)
{
final DruidObjectPredicate<String> predicate = predicateFactory.makeStringPredicate();

return new BaseVectorValueMatcher(selector)
{
final VectorMatch match = VectorMatch.wrap(new int[selector.getMaxVectorSize()]);

@Override
public ReadableVectorMatch match(final ReadableVectorMatch mask, boolean includeUnknown)
{
final Object[] vector = selector.getObjectVector();
final int[] selection = match.getSelection();

int numRows = 0;

for (int i = 0; i < mask.getSelectionSize(); i++) {
final int rowNum = mask.getSelection()[i];
Object val = vector[rowNum];
if (val instanceof List) {
for (Object o : (List) val) {
if (predicate.apply((String) o).matches(includeUnknown)) {
selection[numRows++] = rowNum;
break;
}
}
} else if (predicate.apply((String) val).matches(includeUnknown)) {
selection[numRows++] = rowNum;
}
}

match.setSelectionSize(numRows);
return match;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public VectorValueMatcherFactory makeObjectProcessor(
)
{
if (capabilities.is(ValueType.STRING)) {
if (capabilities.hasMultipleValues().isTrue()) {
return new MultiValueStringObjectVectorValueMatcher(selector);
}
return new StringObjectVectorValueMatcher(selector);
}
return new ObjectVectorValueMatcher(selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,18 @@ public VectorValueMatcher makeVectorMatcher(VectorColumnSelectorFactory factory)
case STRING:
return VectorValueMatcherColumnProcessorFactory.instance().makeObjectProcessor(
ColumnCapabilitiesImpl.createSimpleSingleValueStringColumnCapabilities(),
ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr)
ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr, null)
).makeMatcher(predicateFactory);
case ARRAY:
return VectorValueMatcherColumnProcessorFactory.instance().makeObjectProcessor(
ColumnCapabilitiesImpl.createDefault().setType(ExpressionType.toColumnType(outputType)).setHasNulls(true),
ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr)
ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr, null)
).makeMatcher(predicateFactory);
default:
if (ExpressionType.NESTED_DATA.equals(outputType)) {
return VectorValueMatcherColumnProcessorFactory.instance().makeObjectProcessor(
ColumnCapabilitiesImpl.createDefault().setType(ExpressionType.toColumnType(outputType)).setHasNulls(true),
ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr)
ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr, null)
).makeMatcher(predicateFactory);
}
throw new UOE("Vectorized expression matchers not implemented for type: [%s]", outputType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ String getValue(ExprEval evaluated)
return evaluated.asString();
}

@Nullable
List<String> getArrayAsList(ExprEval evaluated)
{
assert evaluated.isArray();
//noinspection ConstantConditions
if (evaluated.asArray() == null) {
return null;
}
return Arrays.stream(evaluated.asArray())
.map(Evals::asString)
.collect(Collectors.toList());
Expand Down Expand Up @@ -133,6 +137,9 @@ public boolean matches(boolean includeUnknown)
ExprEval evaluated = getEvaluated();
if (evaluated.isArray()) {
List<String> array = getArrayAsList(evaluated);
if (array == null) {
return includeUnknown || value == null;
}
return array.stream().anyMatch(x -> (includeUnknown && x == null) || Objects.equals(x, value));
}
final String rowValue = getValue(evaluated);
Expand All @@ -159,6 +166,9 @@ public boolean matches(boolean includeUnknown)
final DruidObjectPredicate<String> predicate = predicateFactory.makeStringPredicate();
if (evaluated.isArray()) {
List<String> array = getArrayAsList(evaluated);
if (array == null) {
return predicate.apply(null).matches(includeUnknown);
}
return array.stream().anyMatch(x -> predicate.apply(x).matches(includeUnknown));
}
final String rowValue = getValue(evaluated);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.segment.virtual;

import com.google.common.base.Preconditions;
import org.apache.druid.math.expr.Expr;
import org.apache.druid.math.expr.vector.ExprVectorProcessor;
import org.apache.druid.segment.vector.ReadableVectorInspector;
import org.apache.druid.segment.vector.VectorObjectSelector;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

import java.util.Arrays;

public class ExpressionVectorMultiValueStringObjectSelector implements VectorObjectSelector
{
private final Expr.VectorInputBinding bindings;
private final ExprVectorProcessor<?> processor;

@MonotonicNonNull
private Object[] cached;
private int currentId = ReadableVectorInspector.NULL_ID;

public ExpressionVectorMultiValueStringObjectSelector(
ExprVectorProcessor<?> processor,
Expr.VectorInputBinding bindings
)
{
this.processor = Preconditions.checkNotNull(processor, "processor");
this.bindings = Preconditions.checkNotNull(bindings, "bindings");
this.cached = new Object[bindings.getMaxVectorSize()];
}

@Override
public Object[] getObjectVector()
{
if (bindings.getCurrentVectorId() != currentId) {
currentId = bindings.getCurrentVectorId();
final Object[] tmp = processor.evalVector(bindings).getObjectVector();
for (int i = 0; i < bindings.getCurrentVectorSize(); i++) {
Object[] tmpi = (Object[]) tmp[i];
if (tmpi == null) {
cached[i] = null;
} else if (tmpi.length == 1) {
cached[i] = tmpi[0];
} else {
cached[i] = Arrays.asList(tmpi);
}
}
}
return cached;
}

@Override
public int getMaxVectorSize()
{
return bindings.getMaxVectorSize();
}

@Override
public int getCurrentVectorSize()
{
return bindings.getCurrentVectorSize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.base.Preconditions;
import org.apache.druid.math.expr.Expr;
import org.apache.druid.math.expr.ExprEval;
import org.apache.druid.math.expr.ExprType;
import org.apache.druid.math.expr.ExpressionType;
import org.apache.druid.math.expr.InputBindings;
Expand All @@ -33,6 +34,8 @@
import org.apache.druid.segment.column.ColumnCapabilities;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.column.RowSignature;
import org.apache.druid.segment.column.Types;
import org.apache.druid.segment.column.ValueType;
import org.apache.druid.segment.vector.ConstantVectorSelectors;
import org.apache.druid.segment.vector.ReadableVectorInspector;
import org.apache.druid.segment.vector.SingleValueDimensionVectorSelector;
Expand Down Expand Up @@ -94,21 +97,32 @@ public static VectorValueSelector makeVectorValueSelector(

public static VectorObjectSelector makeVectorObjectSelector(
VectorColumnSelectorFactory factory,
Expr expression
Expr expression,
@Nullable ColumnType outputTypeHint
)
{
final ExpressionPlan plan = ExpressionPlanner.plan(factory, expression);
Preconditions.checkArgument(plan.is(ExpressionPlan.Trait.VECTORIZABLE));

if (plan.isConstant()) {
final ExprEval<?> eval = plan.getExpression().eval(InputBindings.nilBindings());
if (Types.is(outputTypeHint, ValueType.STRING) && eval.type().isArray()) {
return ConstantVectorSelectors.vectorObjectSelector(
factory.getReadableVectorInspector(),
ExpressionSelectors.coerceEvalToObjectOrList(eval)
);
}
return ConstantVectorSelectors.vectorObjectSelector(
factory.getReadableVectorInspector(),
plan.getExpression().eval(InputBindings.nilBindings()).valueOrDefault()
eval.valueOrDefault()
);
}

final Expr.VectorInputBinding bindings = createVectorBindings(plan.getAnalysis(), factory);
final ExprVectorProcessor<?> processor = plan.getExpression().asVectorProcessor(bindings);
if (Types.is(outputTypeHint, ValueType.STRING) && processor.getOutputType().isArray()) {
return new ExpressionVectorMultiValueStringObjectSelector(processor, bindings);
}
return new ExpressionVectorObjectSelector(processor, bindings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public VectorObjectSelector makeVectorObjectSelector(String columnName, VectorCo
return factory.makeObjectSelector(parsedExpression.get().getBindingIfIdentifier());
}

return ExpressionVectorSelectors.makeVectorObjectSelector(factory, parsedExpression.get());
return ExpressionVectorSelectors.makeVectorObjectSelector(factory, parsedExpression.get(), expression.outputType);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ public abstract class BaseFilterTest extends InitializedNullHandlingTest
new NestedFieldVirtualColumn("nested", "$.l0", "nested.l0", ColumnType.LONG),
new NestedFieldVirtualColumn("nested", "$.arrayLong", "nested.arrayLong", ColumnType.LONG_ARRAY),
new NestedFieldVirtualColumn("nested", "$.arrayDouble", "nested.arrayDouble", ColumnType.DOUBLE_ARRAY),
new NestedFieldVirtualColumn("nested", "$.arrayString", "nested.arrayString", ColumnType.STRING_ARRAY)
new NestedFieldVirtualColumn("nested", "$.arrayString", "nested.arrayString", ColumnType.STRING_ARRAY),
new ExpressionVirtualColumn("arrayLongAsMvd", "array_to_mv(arrayLong)", ColumnType.STRING, TestExprMacroTable.INSTANCE),
new ExpressionVirtualColumn("arrayDoubleAsMvd", "array_to_mv(arrayDouble)", ColumnType.STRING, TestExprMacroTable.INSTANCE),
new ExpressionVirtualColumn("arrayStringAsMvd", "array_to_mv(arrayString)", ColumnType.STRING, TestExprMacroTable.INSTANCE),
new ExpressionVirtualColumn("arrayConstantAsMvd", "array_to_mv(array(1,2,3))", ColumnType.STRING, TestExprMacroTable.INSTANCE)
)
);

Expand Down
Loading