com.hazelcast.jet.sql.impl.expression.json.JsonValueFunction Maven / Gradle / Ivy
/*
* Copyright 2021 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.jet.sql.impl.expression.json;
import com.hazelcast.core.HazelcastJsonValue;
import com.hazelcast.jet.sql.impl.JetSqlSerializerHook;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.sql.impl.QueryException;
import com.hazelcast.sql.impl.expression.ConcurrentInitialSetCache;
import com.hazelcast.sql.impl.expression.ConstantExpression;
import com.hazelcast.sql.impl.expression.Expression;
import com.hazelcast.sql.impl.expression.ExpressionEvalContext;
import com.hazelcast.sql.impl.expression.VariExpressionWithType;
import com.hazelcast.sql.impl.row.Row;
import com.hazelcast.sql.impl.type.QueryDataType;
import com.hazelcast.sql.impl.type.QueryDataTypeFamily;
import com.hazelcast.sql.impl.type.converter.Converter;
import com.hazelcast.sql.impl.type.converter.Converters;
import com.hazelcast.org.apache.calcite.sql.SqlJsonValueEmptyOrErrorBehavior;
import com.hazelcast.org.jsfr.json.path.JsonPath;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
public class JsonValueFunction extends VariExpressionWithType implements IdentifiedDataSerializable {
private static final ILogger LOGGER = Logger.getLogger(JsonValueFunction.class);
private static final int DEFAULT_ON_EMPTY_OPERAND_INDEX = 2;
private static final int DEFAULT_ON_ERROR_OPERAND_INDEX = 3;
private static final Function COMPILE_FUNCTION = JsonPathUtil::compile;
private transient ConcurrentInitialSetCache pathCache;
private JsonPath constantPathCache;
private SqlJsonValueEmptyOrErrorBehavior onEmpty;
private SqlJsonValueEmptyOrErrorBehavior onError;
public JsonValueFunction() {
}
private JsonValueFunction(
Expression>[] operands,
QueryDataType resultType,
SqlJsonValueEmptyOrErrorBehavior onEmpty,
SqlJsonValueEmptyOrErrorBehavior onError
) {
super(operands, resultType);
this.onEmpty = onEmpty;
this.onError = onError;
prepareCache();
}
public static JsonValueFunction> create(
Expression> json,
Expression> path,
Expression> defaultValueOnEmpty,
Expression> defaultValueOnError,
QueryDataType resultType,
SqlJsonValueEmptyOrErrorBehavior onEmpty,
SqlJsonValueEmptyOrErrorBehavior onError
) {
final Expression>[] operands = new Expression>[]{
json,
path,
defaultValueOnEmpty,
defaultValueOnError
};
return new JsonValueFunction<>(operands, resultType, onEmpty, onError);
}
@Override
public int getFactoryId() {
return JetSqlSerializerHook.F_ID;
}
@Override
public int getClassId() {
return JetSqlSerializerHook.JSON_VALUE;
}
@Override
public T eval(final Row row, final ExpressionEvalContext context) {
// first evaluate the required parameter
final String path = (String) operands[1].eval(row, context);
validatePath(path);
final Object operand0 = operands[0].eval(row, context);
String json = operand0 instanceof HazelcastJsonValue
? operand0.toString()
: (String) operand0;
if (json == null) {
json = "";
}
final JsonPath jsonPath = constantPathCache != null ? constantPathCache :
pathCache.computeIfAbsent(path, COMPILE_FUNCTION);
Collection
© 2015 - 2024 Weber Informatics LLC | Privacy Policy