use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.
the class RecordMergeTypeComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
String funcName = f.getFunctionIdentifier().getName();
IAType t0 = (IAType) env.getType(f.getArguments().get(0).getValue());
IAType t1 = (IAType) env.getType(f.getArguments().get(1).getValue());
boolean unknownable = TypeHelper.canBeUnknown(t0) || TypeHelper.canBeUnknown(t1);
ARecordType recType0 = TypeComputeUtils.extractRecordType(t0);
if (recType0 == null) {
throw new TypeMismatchException(funcName, 0, t0.getTypeTag(), ATypeTag.OBJECT);
}
ARecordType recType1 = TypeComputeUtils.extractRecordType(t1);
if (recType1 == null) {
throw new TypeMismatchException(funcName, 1, t1.getTypeTag(), ATypeTag.OBJECT);
}
List<String> resultFieldNames = new ArrayList<>();
for (String fieldName : recType0.getFieldNames()) {
resultFieldNames.add(fieldName);
}
Collections.sort(resultFieldNames);
List<IAType> resultFieldTypes = new ArrayList<>();
for (String fieldName : resultFieldNames) {
if (recType0.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
ARecordType nestedType = (ARecordType) recType0.getFieldType(fieldName);
//Deep Copy prevents altering of input types
resultFieldTypes.add(nestedType.deepCopy(nestedType));
} else {
resultFieldTypes.add(recType0.getFieldType(fieldName));
}
}
List<String> additionalFieldNames = new ArrayList<>();
List<IAType> additionalFieldTypes = new ArrayList<>();
String[] fieldNames = recType1.getFieldNames();
IAType[] fieldTypes = recType1.getFieldTypes();
for (int i = 0; i < fieldNames.length; ++i) {
int pos = Collections.binarySearch(resultFieldNames, fieldNames[i]);
if (pos >= 0) {
IAType resultFieldType = resultFieldTypes.get(pos);
if (resultFieldType.getTypeTag() != fieldTypes[i].getTypeTag()) {
throw new CompilationException(ErrorCode.COMPILATION_DUPLICATE_FIELD_NAME, fieldNames[i]);
}
// Assuming fieldTypes[i].getTypeTag() = resultFieldType.getTypeTag()
if (fieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
resultFieldTypes.set(pos, mergedNestedType(fieldNames[i], fieldTypes[i], resultFieldType));
}
} else {
additionalFieldNames.add(fieldNames[i]);
additionalFieldTypes.add(fieldTypes[i]);
}
}
resultFieldNames.addAll(additionalFieldNames);
resultFieldTypes.addAll(additionalFieldTypes);
String resultTypeName = "merged(" + recType0.getTypeName() + ", " + recType1.getTypeName() + ")";
boolean isOpen = recType0.isOpen() || recType1.isOpen();
IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[] {}), resultFieldTypes.toArray(new IAType[] {}), isOpen);
if (unknownable) {
resultType = AUnionType.createUnknownableType(resultType);
}
return resultType;
}
use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.
the class SwitchCaseComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expression;
String funcName = fce.getFunctionIdentifier().getName();
int argNumber = fce.getArguments().size();
if (argNumber < 3) {
throw new CompilationException(ErrorCode.COMPILATION_INVALID_PARAMETER_NUMBER, funcName, argNumber);
}
int argSize = fce.getArguments().size();
List<IAType> types = new ArrayList<>();
// The last return expression is from the ELSE branch and it is optional.
for (int argIndex = 2; argIndex < argSize; argIndex += (argIndex + 2 == argSize) ? 1 : 2) {
IAType type = (IAType) env.getType(fce.getArguments().get(argIndex).getValue());
types.add(type);
}
return TypeResolverUtil.resolve(types);
}
use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.
the class AqlQueryRewriter method inlineDeclaredUdfs.
private void inlineDeclaredUdfs() throws CompilationException {
if (topStatement == null) {
return;
}
List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
for (FunctionDecl fdecl : declaredFunctions) {
funIds.add(fdecl.getSignature());
}
List<FunctionDecl> storedFunctionDecls = new ArrayList<>();
for (Expression topLevelExpr : topStatement.getDirectlyEnclosedExpressions()) {
storedFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionParser.getFunctionDecl(func), signature -> CommonFunctionMapUtil.normalizeBuiltinFunctionSignature(signature)));
declaredFunctions.addAll(storedFunctionDecls);
}
if (!declaredFunctions.isEmpty()) {
AQLInlineUdfsVisitor visitor = new AQLInlineUdfsVisitor(context, new AQLRewriterFactory(), declaredFunctions, metadataProvider);
while (topStatement.accept(visitor, declaredFunctions)) {
// loop until no more changes
}
}
declaredFunctions.removeAll(storedFunctionDecls);
}
use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.
the class SubscribeFeedStatement method initialize.
public void initialize(MetadataTransactionContext mdTxnCtx) throws MetadataException {
this.query = new Query(false);
EntityId sourceFeedId = connectionRequest.getReceivingFeedId();
Feed subscriberFeed = MetadataManager.INSTANCE.getFeed(mdTxnCtx, connectionRequest.getReceivingFeedId().getDataverse(), connectionRequest.getReceivingFeedId().getEntityName());
if (subscriberFeed == null) {
throw new IllegalStateException(" Subscriber feed " + subscriberFeed + " not found.");
}
String feedOutputType = getOutputType(mdTxnCtx);
StringBuilder builder = new StringBuilder();
builder.append("use dataverse " + sourceFeedId.getDataverse() + ";\n");
builder.append("set" + " " + FunctionUtil.IMPORT_PRIVATE_FUNCTIONS + " " + "'" + Boolean.TRUE + "'" + ";\n");
builder.append("set" + " " + FeedActivityDetails.FEED_POLICY_NAME + " " + "'" + connectionRequest.getPolicy() + "'" + ";\n");
builder.append("insert into dataset " + connectionRequest.getTargetDataset() + " ");
builder.append(" (" + " for $x in feed-collect ('" + sourceFeedId.getDataverse() + "'" + "," + "'" + sourceFeedId.getEntityName() + "'" + "," + "'" + connectionRequest.getReceivingFeedId().getEntityName() + "'" + "," + "'" + connectionRequest.getSubscriptionLocation().name() + "'" + "," + "'" + connectionRequest.getTargetDataset() + "'" + "," + "'" + feedOutputType + "'" + ")");
List<FunctionSignature> functionsToApply = connectionRequest.getFunctionsToApply();
if ((functionsToApply != null) && functionsToApply.isEmpty()) {
builder.append(" return $x");
} else {
Function function;
String rValueName = "x";
String lValueName = "y";
int variableIndex = 0;
for (FunctionSignature appliedFunction : functionsToApply) {
function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, appliedFunction);
variableIndex++;
switch(function.getLanguage().toUpperCase()) {
case Function.LANGUAGE_AQL:
builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
rValueName = lValueName + variableIndex;
break;
case Function.LANGUAGE_JAVA:
builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
rValueName = lValueName + variableIndex;
break;
}
builder.append("\n");
}
builder.append("return $" + lValueName + variableIndex);
}
builder.append(")");
builder.append(";");
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Connect feed statement translated to\n" + builder.toString());
}
IParser parser = parserFactory.createParser(new StringReader(builder.toString()));
List<Statement> statements;
try {
statements = parser.parse();
query = ((InsertStatement) statements.get(INSERT_STATEMENT_POS)).getQuery();
} catch (CompilationException pe) {
throw new MetadataException(pe);
}
}
use of org.apache.asterix.common.exceptions.CompilationException in project asterixdb by apache.
the class RangeMapBuilder method parseLiteralToBytes.
@SuppressWarnings("unchecked")
private static void parseLiteralToBytes(Expression item, DataOutput out) throws CompilationException {
AMutableDouble aDouble = new AMutableDouble(0);
AMutableFloat aFloat = new AMutableFloat(0);
AMutableInt64 aInt64 = new AMutableInt64(0);
AMutableInt32 aInt32 = new AMutableInt32(0);
AMutableString aString = new AMutableString("");
@SuppressWarnings("rawtypes") ISerializerDeserializer serde;
Literal l = ((LiteralExpr) item).getValue();
try {
switch(l.getLiteralType()) {
case DOUBLE:
DoubleLiteral dl = (DoubleLiteral) l;
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
aDouble.setValue(dl.getValue());
serde.serialize(aDouble, out);
break;
case FLOAT:
FloatLiteral fl = (FloatLiteral) l;
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
aFloat.setValue(fl.getValue());
serde.serialize(aFloat, out);
break;
case INTEGER:
IntegerLiteral il = (IntegerLiteral) l;
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
aInt32.setValue(il.getValue());
serde.serialize(aInt32, out);
break;
case LONG:
LongIntegerLiteral lil = (LongIntegerLiteral) l;
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
aInt64.setValue(lil.getValue());
serde.serialize(aInt64, out);
break;
case STRING:
StringLiteral sl = (StringLiteral) l;
serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
aString.setValue(sl.getValue());
serde.serialize(aString, out);
break;
default:
throw new NotImplementedException("The range map builder has not been implemented for " + item.getKind() + " type of expressions.");
}
} catch (HyracksDataException e) {
throw new CompilationException(e.getMessage());
}
}
Aggregations