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
Expand Up @@ -62,6 +62,7 @@ public enum BuiltinFunctionName {
/** Collection functions */
ARRAY(FunctionName.of("array")),
ARRAY_LENGTH(FunctionName.of("array_length")),
MVAPPEND(FunctionName.of("mvappend")),
MVJOIN(FunctionName.of("mvjoin")),
FORALL(FunctionName.of("forall")),
EXISTS(FunctionName.of("exists")),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.expression.function.CollectionUDF;

import static org.apache.calcite.sql.type.SqlTypeUtil.createArrayType;

import java.util.ArrayList;
import java.util.List;
import org.apache.calcite.adapter.enumerable.NotNullImplementor;
import org.apache.calcite.adapter.enumerable.NullPolicy;
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.apache.calcite.linq4j.tree.Types;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.type.SqlReturnTypeInference;
import org.apache.calcite.sql.type.SqlTypeName;
import org.opensearch.sql.expression.function.ImplementorUDF;
import org.opensearch.sql.expression.function.UDFOperandMetadata;

/**
* MVAppend function that appends all elements from arguments to create an array. Always returns an
* array or null for consistent type behavior.
*/
public class MVAppendFunctionImpl extends ImplementorUDF {

public MVAppendFunctionImpl() {
super(new MVAppendImplementor(), NullPolicy.ALL);
}

@Override
public SqlReturnTypeInference getReturnTypeInference() {
return sqlOperatorBinding -> {
RelDataTypeFactory typeFactory = sqlOperatorBinding.getTypeFactory();

if (sqlOperatorBinding.getOperandCount() == 0) {
return typeFactory.createSqlType(SqlTypeName.NULL);
}

RelDataType elementType = determineElementType(sqlOperatorBinding, typeFactory);
return createArrayType(
typeFactory, typeFactory.createTypeWithNullability(elementType, true), true);
};
}

@Override
public UDFOperandMetadata getOperandMetadata() {
return null;
}

private static RelDataType determineElementType(
SqlOperatorBinding sqlOperatorBinding, RelDataTypeFactory typeFactory) {
RelDataType mostGeneralType = null;

for (int i = 0; i < sqlOperatorBinding.getOperandCount(); i++) {
RelDataType operandType = getComponentType(sqlOperatorBinding.getOperandType(i));

mostGeneralType = updateMostGeneralType(mostGeneralType, operandType, typeFactory);
}

return mostGeneralType != null ? mostGeneralType : typeFactory.createSqlType(SqlTypeName.NULL);
}

private static RelDataType getComponentType(RelDataType operandType) {
if (!operandType.isStruct() && operandType.getComponentType() != null) {
return operandType.getComponentType();
}
return operandType;
}

private static RelDataType updateMostGeneralType(
RelDataType current, RelDataType candidate, RelDataTypeFactory typeFactory) {
if (current == null) {
return candidate;
}

if (!current.equals(candidate)) {
return typeFactory.createSqlType(SqlTypeName.ANY);
} else {
return current;
}
}

public static class MVAppendImplementor implements NotNullImplementor {
@Override
public Expression implement(
RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) {
return Expressions.call(
Types.lookupMethod(MVAppendFunctionImpl.class, "mvappend", Object[].class),
Expressions.newArrayInit(Object.class, translatedOperands));
}
}

public static Object mvappend(Object... args) {
List<Object> elements = collectElements(args);
return elements.isEmpty() ? null : elements;
}

private static List<Object> collectElements(Object... args) {
List<Object> elements = new ArrayList<>();

for (Object arg : args) {
if (arg == null) {
continue;
}

if (arg instanceof List) {
addListElements((List<?>) arg, elements);
} else {
elements.add(arg);
}
}

return elements;
}

private static void addListElements(List<?> list, List<Object> elements) {
for (Object item : list) {
if (item != null) {
elements.add(item);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.opensearch.sql.expression.function.CollectionUDF.ExistsFunctionImpl;
import org.opensearch.sql.expression.function.CollectionUDF.FilterFunctionImpl;
import org.opensearch.sql.expression.function.CollectionUDF.ForallFunctionImpl;
import org.opensearch.sql.expression.function.CollectionUDF.MVAppendFunctionImpl;
import org.opensearch.sql.expression.function.CollectionUDF.ReduceFunctionImpl;
import org.opensearch.sql.expression.function.CollectionUDF.TransformFunctionImpl;
import org.opensearch.sql.expression.function.jsonUDF.JsonAppendFunctionImpl;
Expand Down Expand Up @@ -383,6 +384,7 @@ public class PPLBuiltinOperators extends ReflectiveSqlOperatorTable {
public static final SqlOperator FORALL = new ForallFunctionImpl().toUDF("forall");
public static final SqlOperator EXISTS = new ExistsFunctionImpl().toUDF("exists");
public static final SqlOperator ARRAY = new ArrayFunctionImpl().toUDF("array");
public static final SqlOperator MVAPPEND = new MVAppendFunctionImpl().toUDF("mvappend");
public static final SqlOperator FILTER = new FilterFunctionImpl().toUDF("filter");
public static final SqlOperator TRANSFORM = new TransformFunctionImpl().toUDF("transform");
public static final SqlOperator REDUCE = new ReduceFunctionImpl().toUDF("reduce");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
import static org.opensearch.sql.expression.function.BuiltinFunctionName.MULTIPLY;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.MULTIPLYFUNCTION;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.MULTI_MATCH;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.MVAPPEND;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.MVJOIN;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.NOT;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.NOTEQUAL;
Expand Down Expand Up @@ -834,6 +835,7 @@ void populate() {
PPLTypeChecker.family(SqlTypeFamily.ARRAY, SqlTypeFamily.CHARACTER));

registerOperator(ARRAY, PPLBuiltinOperators.ARRAY);
registerOperator(MVAPPEND, PPLBuiltinOperators.MVAPPEND);
registerOperator(ARRAY_LENGTH, SqlLibraryOperators.ARRAY_LENGTH);
registerOperator(FORALL, PPLBuiltinOperators.FORALL);
registerOperator(EXISTS, PPLBuiltinOperators.EXISTS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.expression.function.CollectionUDF;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

/** Unit tests for MVAppendFunctionImpl. */
public class MVAppendFunctionImplTest {

@Test
public void testMvappendWithNoArguments() {
Object result = MVAppendFunctionImpl.mvappend();
assertNull(result);
}

@Test
public void testMvappendWithSingleElement() {
Object result = MVAppendFunctionImpl.mvappend(42);
assertEquals(Arrays.asList(42), result);
}

@Test
public void testMvappendWithMultipleElements() {
Object result = MVAppendFunctionImpl.mvappend(1, 2, 3);
assertEquals(Arrays.asList(1, 2, 3), result);
}

@Test
public void testMvappendWithNullValues() {
Object result = MVAppendFunctionImpl.mvappend(null, 1, null);
assertEquals(Arrays.asList(1), result);
}

@Test
public void testMvappendWithAllNulls() {
Object result = MVAppendFunctionImpl.mvappend(null, null);
assertNull(result);
}

@Test
public void testMvappendWithArrayFlattening() {
List<Integer> array1 = Arrays.asList(1, 2);
List<Integer> array2 = Arrays.asList(3, 4);
Object result = MVAppendFunctionImpl.mvappend(array1, array2);
assertEquals(Arrays.asList(1, 2, 3, 4), result);
}

@Test
public void testMvappendWithMixedTypes() {
List<Integer> array = Arrays.asList(1, 2);
Object result = MVAppendFunctionImpl.mvappend(array, 3, "hello");
assertEquals(Arrays.asList(1, 2, 3, "hello"), result);
}

@Test
public void testMvappendWithArrayAndNulls() {
List<Integer> array = Arrays.asList(1, 2);
Object result = MVAppendFunctionImpl.mvappend(null, array, null, 3);
assertEquals(Arrays.asList(1, 2, 3), result);
}

@Test
public void testMvappendWithSingleNull() {
Object result = MVAppendFunctionImpl.mvappend((Object) null);
assertNull(result);
}

@Test
public void testMvappendWithEmptyArray() {
List<Object> emptyArray = Arrays.asList();
Object result = MVAppendFunctionImpl.mvappend(emptyArray, 1);
assertEquals(Arrays.asList(1), result);
}
}
1 change: 1 addition & 0 deletions docs/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"user/ppl/cmd/top.rst",
"user/ppl/cmd/trendline.rst",
"user/ppl/cmd/where.rst",
"user/ppl/functions/collection.rst",
"user/ppl/functions/condition.rst",
"user/ppl/functions/datetime.rst",
"user/ppl/functions/expressions.rst",
Expand Down
Loading
Loading