Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed 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 io.trino.operator.scalar;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Primitives;
import io.trino.annotation.UsedByGeneratedCode;
import io.trino.metadata.SqlScalarFunction;
import io.trino.spi.block.SqlMap;
import io.trino.spi.function.BoundSignature;
import io.trino.spi.function.FunctionDependencies;
import io.trino.spi.function.FunctionDependencyDeclaration;
import io.trino.spi.function.FunctionMetadata;
import io.trino.spi.function.Signature;
import io.trino.spi.type.MapType;
import io.trino.spi.type.Type;
import io.trino.spi.type.TypeSignature;
import java.lang.invoke.MethodHandle;
import static io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL;
import static io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN;
import static io.trino.spi.function.OperatorType.EQUAL;
import static io.trino.spi.type.TypeSignature.mapType;
import static io.trino.spi.type.TypeUtils.readNativeValue;
import static io.trino.util.Reflection.methodHandle;
public class MapElementAtFunction
extends SqlScalarFunction
{
public static final MapElementAtFunction MAP_ELEMENT_AT = new MapElementAtFunction();
private static final MethodHandle METHOD_HANDLE_BOOLEAN = methodHandle(MapElementAtFunction.class, "elementAt", Type.class, SqlMap.class, boolean.class);
private static final MethodHandle METHOD_HANDLE_LONG = methodHandle(MapElementAtFunction.class, "elementAt", Type.class, SqlMap.class, long.class);
private static final MethodHandle METHOD_HANDLE_DOUBLE = methodHandle(MapElementAtFunction.class, "elementAt", Type.class, SqlMap.class, double.class);
private static final MethodHandle METHOD_HANDLE_OBJECT = methodHandle(MapElementAtFunction.class, "elementAt", Type.class, SqlMap.class, Object.class);
protected MapElementAtFunction()
{
super(FunctionMetadata.scalarBuilder("element_at")
.signature(Signature.builder()
.typeVariable("K")
.typeVariable("V")
.returnType(new TypeSignature("V"))
.argumentType(mapType(new TypeSignature("K"), new TypeSignature("V")))
.argumentType(new TypeSignature("K"))
.build())
.nullable()
.description("Get value for the given key, or null if it does not exist")
.build());
}
@Override
public FunctionDependencyDeclaration getFunctionDependencies()
{
return FunctionDependencyDeclaration.builder()
.addOperatorSignature(EQUAL, ImmutableList.of(new TypeSignature("K"), new TypeSignature("K")))
.build();
}
@Override
public SpecializedSqlScalarFunction specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies)
{
MapType mapType = (MapType) boundSignature.getArgumentType(0);
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
MethodHandle methodHandle;
if (keyType.getJavaType() == boolean.class) {
methodHandle = METHOD_HANDLE_BOOLEAN;
}
else if (keyType.getJavaType() == long.class) {
methodHandle = METHOD_HANDLE_LONG;
}
else if (keyType.getJavaType() == double.class) {
methodHandle = METHOD_HANDLE_DOUBLE;
}
else {
methodHandle = METHOD_HANDLE_OBJECT;
}
methodHandle = methodHandle.bindTo(valueType);
methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(Primitives.wrap(valueType.getJavaType())));
return new ChoicesSpecializedSqlScalarFunction(
boundSignature,
NULLABLE_RETURN,
ImmutableList.of(NEVER_NULL, NEVER_NULL),
methodHandle);
}
@UsedByGeneratedCode
public static Object elementAt(Type valueType, SqlMap sqlMap, boolean key)
{
int index = sqlMap.seekKeyExact(key);
if (index == -1) {
return null;
}
return readNativeValue(valueType, sqlMap.getRawValueBlock(), sqlMap.getRawOffset() + index);
}
@UsedByGeneratedCode
public static Object elementAt(Type valueType, SqlMap sqlMap, long key)
{
int index = sqlMap.seekKeyExact(key);
if (index == -1) {
return null;
}
return readNativeValue(valueType, sqlMap.getRawValueBlock(), sqlMap.getRawOffset() + index);
}
@UsedByGeneratedCode
public static Object elementAt(Type valueType, SqlMap sqlMap, double key)
{
int index = sqlMap.seekKeyExact(key);
if (index == -1) {
return null;
}
return readNativeValue(valueType, sqlMap.getRawValueBlock(), sqlMap.getRawOffset() + index);
}
@UsedByGeneratedCode
public static Object elementAt(Type valueType, SqlMap sqlMap, Object key)
{
int index = sqlMap.seekKeyExact(key);
if (index == -1) {
return null;
}
return readNativeValue(valueType, sqlMap.getRawValueBlock(), sqlMap.getRawOffset() + index);
}
}