Skip to content
Open
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
Expand Up @@ -46,7 +46,7 @@ protected boolean satisfy(List<Column> childrenColumns, int index) {
} else {
for (int i = 1, size = childrenColumns.size(); i < size; i++) {
Column valueColumn = childrenColumns.get(i);
if (!valueColumn.isNull(i)) {
if (!valueColumn.isNull(index)) {
Type valueType = columnTransformerList.get(i).getType();
if (value.equals(valueType.getBinary(valueColumn, index))) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected boolean satisfy(List<Column> childrenColumns, int index) {
} else {
for (int i = 1, size = childrenColumns.size(); i < size; i++) {
Column valueColumn = childrenColumns.get(i);
if (!valueColumn.isNull(i)) {
if (!valueColumn.isNull(index)) {
Type valueType = columnTransformerList.get(i).getType();
if (valueType.getBoolean(valueColumn, index) == value) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected boolean satisfy(List<Column> childrenColumns, int index) {
} else {
for (int i = 1, size = childrenColumns.size(); i < size; i++) {
Column valueColumn = childrenColumns.get(i);
if (!valueColumn.isNull(i)) {
if (!valueColumn.isNull(index)) {
Type valueType = columnTransformerList.get(i).getType();
if (valueType.getDouble(valueColumn, index) == value) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected boolean satisfy(List<Column> childrenColumns, int index) {
} else {
for (int i = 1, size = childrenColumns.size(); i < size; i++) {
Column valueColumn = childrenColumns.get(i);
if (!valueColumn.isNull(i)) {
if (!valueColumn.isNull(index)) {
Type valueType = columnTransformerList.get(i).getType();
if (valueType.getFloat(valueColumn, index) == value) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected boolean satisfy(List<Column> childrenColumns, int index) {
} else {
for (int i = 1, size = childrenColumns.size(); i < size; i++) {
Column valueColumn = childrenColumns.get(i);
if (!valueColumn.isNull(i)) {
if (!valueColumn.isNull(index)) {
Type valueType = columnTransformerList.get(i).getType();
if (valueType.getInt(valueColumn, index) == value) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected boolean satisfy(List<Column> childrenColumns, int index) {
} else {
for (int i = 1, size = childrenColumns.size(); i < size; i++) {
Column valueColumn = childrenColumns.get(i);
if (!valueColumn.isNull(i)) {
if (!valueColumn.isNull(index)) {
Type valueType = columnTransformerList.get(i).getType();
if (valueType.getLong(valueColumn, index) == value) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.iotdb.calc.transformation.dag.column.multi;

import org.apache.iotdb.calc.transformation.dag.column.ColumnTransformer;
import org.apache.iotdb.calc.transformation.dag.column.leaf.IdentityColumnTransformer;

import org.apache.tsfile.block.column.Column;
import org.apache.tsfile.block.column.ColumnBuilder;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.read.common.block.column.BinaryColumn;
import org.apache.tsfile.read.common.block.column.BooleanColumn;
import org.apache.tsfile.read.common.block.column.DoubleColumn;
import org.apache.tsfile.read.common.block.column.FloatColumn;
import org.apache.tsfile.read.common.block.column.IntColumn;
import org.apache.tsfile.read.common.block.column.LongColumn;
import org.apache.tsfile.read.common.type.BooleanType;
import org.apache.tsfile.read.common.type.Type;
import org.apache.tsfile.utils.Binary;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class InMultiColumnTransformerTest {

private static final boolean[] SECOND_ROW_NULL = {false, true};
private static final boolean[] FIRST_ROW_NULL = {true, false};

@Test
public void testSingleRowBlockWithOneNonLiteralItem() {
// `x IN (1, y)` on a one-row block: the non-literal item is at operand index 1, which is out
// of range for the row-level null check.
InMultiColumnTransformer transformer =
new InInt32MultiColumnTransformer(
Collections.singleton(1), identities(TSDataType.INT32, TSDataType.INT32));
Column result =
transform(
transformer,
new IntColumn(1, Optional.empty(), new int[] {5}),
new IntColumn(1, Optional.of(new boolean[] {false}), new int[] {5}));
assertEquals(1, result.getPositionCount());
assertTrue(result.getBoolean(0));
}

@Test
public void testNullFlagIsReadFromCurrentRow() {
// Row 0 has a NULL item whose underlying slot holds the subject value, row 1 has a non-NULL
// item that differs from the subject. Neither row must match.
assertNoMatch(
new InInt32MultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.INT32, TSDataType.INT32)),
new IntColumn(2, Optional.empty(), new int[] {7, 7}),
new IntColumn(2, Optional.of(FIRST_ROW_NULL), new int[] {7, 8}));
assertNoMatch(
new InInt64MultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.INT64, TSDataType.INT64)),
new LongColumn(2, Optional.empty(), new long[] {7L, 7L}),
new LongColumn(2, Optional.of(FIRST_ROW_NULL), new long[] {7L, 8L}));
assertNoMatch(
new InFloatMultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.FLOAT, TSDataType.FLOAT)),
new FloatColumn(2, Optional.empty(), new float[] {7f, 7f}),
new FloatColumn(2, Optional.of(FIRST_ROW_NULL), new float[] {7f, 8f}));
assertNoMatch(
new InDoubleMultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.DOUBLE, TSDataType.DOUBLE)),
new DoubleColumn(2, Optional.empty(), new double[] {7d, 7d}),
new DoubleColumn(2, Optional.of(FIRST_ROW_NULL), new double[] {7d, 8d}));
assertNoMatch(
new InBinaryMultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.STRING, TSDataType.STRING)),
new BinaryColumn(2, Optional.empty(), new Binary[] {binary("a"), binary("a")}),
new BinaryColumn(2, Optional.of(FIRST_ROW_NULL), new Binary[] {binary("a"), binary("b")}));
assertNoMatch(
new InBooleanMultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.BOOLEAN, TSDataType.BOOLEAN)),
new BooleanColumn(2, Optional.empty(), new boolean[] {true, true}),
new BooleanColumn(2, Optional.of(FIRST_ROW_NULL), new boolean[] {true, false}));
}

@Test
public void testNonNullItemIsNotSkippedBecauseOfAnotherRow() {
// Row 1 is NULL, row 0 holds the subject value: row 0 must still match.
InMultiColumnTransformer transformer =
new InInt64MultiColumnTransformer(
Collections.emptySet(), identities(TSDataType.INT64, TSDataType.INT64));
Column result =
transform(
transformer,
new LongColumn(2, Optional.empty(), new long[] {3L, 3L}),
new LongColumn(2, Optional.of(SECOND_ROW_NULL), new long[] {3L, 0L}));
assertTrue(result.getBoolean(0));
assertFalse(result.getBoolean(1));
}

private static void assertNoMatch(
InMultiColumnTransformer transformer, Column subject, Column item) {
Column result = transform(transformer, subject, item);
assertEquals(2, result.getPositionCount());
assertFalse(result.getBoolean(0));
assertFalse(result.getBoolean(1));

boolean[] selection = {true, true};
ColumnBuilder builder = BooleanType.BOOLEAN.createColumnBuilder(2);
transformer.doTransform(Arrays.asList(subject, item), builder, 2, selection);
Column selected = builder.build();
assertFalse(selected.getBoolean(0));
assertFalse(selected.getBoolean(1));
}

private static Column transform(InMultiColumnTransformer transformer, Column... columns) {
int positionCount = columns[0].getPositionCount();
ColumnBuilder builder = BooleanType.BOOLEAN.createColumnBuilder(positionCount);
transformer.doTransform(Arrays.asList(columns), builder, positionCount);
return builder.build();
}

private static List<ColumnTransformer> identities(TSDataType... types) {
ColumnTransformer[] transformers = new ColumnTransformer[types.length];
for (int i = 0; i < types.length; i++) {
Type type = Type.fromTsDataType(types[i]);
transformers[i] = new IdentityColumnTransformer(type, i);
}
return Arrays.asList(transformers);
}

private static Binary binary(String value) {
return new Binary(value, StandardCharsets.UTF_8);
}
}