use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class InlineColumnAliasVisitor method mapRecordConstructor.
private Map<Expression, Expression> mapRecordConstructor(RecordConstructor rc) {
Map<Expression, Expression> exprMap = new HashMap<>();
for (FieldBinding binding : rc.getFbList()) {
Expression leftExpr = binding.getLeftExpr();
// (e.g., foo.name AS name) in regular SQL SELECT.
if (leftExpr.getKind() != Kind.LITERAL_EXPRESSION) {
continue;
}
LiteralExpr literalExpr = (LiteralExpr) leftExpr;
if (literalExpr.getValue().getLiteralType() == Literal.Type.STRING) {
String fieldName = SqlppVariableUtil.toInternalVariableName(literalExpr.getValue().getStringValue());
exprMap.put(new VariableExpr(new VarIdentifier(fieldName)), binding.getRightExpr());
}
}
return exprMap;
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class LangRecordParseUtil method exprToStringLiteral.
public static Literal exprToStringLiteral(Expression expr) throws HyracksDataException {
if (expr.getKind() != Expression.Kind.LITERAL_EXPRESSION) {
throw new HyracksDataException(ErrorCode.ASTERIX, ErrorCode.PARSE_ERROR, "Expected expression can only be of type %1$s", Expression.Kind.LITERAL_EXPRESSION);
}
LiteralExpr keyLiteralExpr = (LiteralExpr) expr;
Literal keyLiteral = keyLiteralExpr.getValue();
if (keyLiteral.getLiteralType() != Literal.Type.STRING) {
throw new HyracksDataException(ErrorCode.ASTERIX, ErrorCode.PARSE_ERROR, "Expected Literal can only be of type %1$s", Literal.Type.STRING);
}
return keyLiteral;
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class SqlppDeleteRewriteVisitor method visit.
@Override
public Void visit(DeleteStatement deleteStmt, Void visitArg) {
List<Expression> arguments = new ArrayList<>();
Identifier dataverseName = deleteStmt.getDataverseName();
Identifier datasetName = deleteStmt.getDatasetName();
String arg = dataverseName == null ? datasetName.getValue() : dataverseName.getValue() + "." + datasetName.getValue();
LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
arguments.add(argumentLiteral);
CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1), arguments);
// From clause.
VariableExpr var = deleteStmt.getVariableExpr();
FromTerm fromTerm = new FromTerm(callExpression, var, null, null);
@SuppressWarnings("unchecked") FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
// Where clause.
WhereClause whereClause = null;
Expression condition = deleteStmt.getCondition();
if (condition != null) {
whereClause = new WhereClause(condition);
}
// Select clause.
VariableExpr returnExpr = new VariableExpr(var.getVar());
returnExpr.setIsNewVar(false);
SelectElement selectElement = new SelectElement(returnExpr);
SelectClause selectClause = new SelectClause(selectElement, null, false);
// Construct the select expression.
SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, whereClause, null, null, null);
SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, false);
Query query = new Query(false, false, selectExpression, 0);
query.setBody(selectExpression);
// return the delete statement.
deleteStmt.setQuery(query);
return null;
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CallExpr fcall, Mutable<ILogicalOperator> tupSource) throws CompilationException {
LogicalVariable v = context.newVar();
FunctionSignature signature = fcall.getFunctionSignature();
List<Mutable<ILogicalExpression>> args = new ArrayList<>();
Mutable<ILogicalOperator> topOp = tupSource;
for (Expression expr : fcall.getExprList()) {
switch(expr.getKind()) {
case VARIABLE_EXPRESSION:
LogicalVariable var = context.getVar(((VariableExpr) expr).getVar().getId());
args.add(new MutableObject<>(new VariableReferenceExpression(var)));
break;
case LITERAL_EXPRESSION:
LiteralExpr val = (LiteralExpr) expr;
args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue())))));
break;
default:
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(expr, topOp);
AbstractLogicalOperator o1 = (AbstractLogicalOperator) eo.second.getValue();
args.add(new MutableObject<>(eo.first));
if (o1 != null && !(o1.getOperatorTag() == LogicalOperatorTag.ASSIGN && hasOnlyChild(o1, topOp))) {
topOp = eo.second;
}
break;
}
}
AbstractFunctionCallExpression f;
if ((f = lookupUserDefinedFunction(signature, args)) == null) {
f = lookupBuiltinFunction(signature.getName(), signature.getArity(), args);
}
if (f == null) {
throw new CompilationException(" Unknown function " + signature.getName() + "@" + signature.getArity());
}
// Put hints into function call expr.
if (fcall.hasHints()) {
for (IExpressionAnnotation hint : fcall.getHints()) {
f.getAnnotations().put(hint, hint);
}
}
AssignOperator op = new AssignOperator(v, new MutableObject<>(f));
if (topOp != null) {
op.getInputs().add(topOp);
}
return new Pair<>(op, v);
}
use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.
the class LangExpressionToPlanTranslator method langExprToAlgExpression.
protected Pair<ILogicalExpression, Mutable<ILogicalOperator>> langExprToAlgExpression(Expression expr, Mutable<ILogicalOperator> topOpRef) throws CompilationException {
switch(expr.getKind()) {
case VARIABLE_EXPRESSION:
VariableReferenceExpression ve = new VariableReferenceExpression(context.getVar(((VariableExpr) expr).getVar().getId()));
return new Pair<>(ve, topOpRef);
case LITERAL_EXPRESSION:
LiteralExpr val = (LiteralExpr) expr;
return new Pair<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue()))), topOpRef);
default:
if (expressionNeedsNoNesting(expr)) {
Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, topOpRef);
ILogicalExpression exp = ((AssignOperator) p.first).getExpressions().get(0).getValue();
return new Pair<>(exp, p.first.getInputs().get(0));
} else {
Mutable<ILogicalOperator> srcRef = new MutableObject<>();
Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, srcRef);
if (p.first.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
if (topOpRef.getValue() != null) {
srcRef.setValue(topOpRef.getValue());
} else {
// Re-binds the bottom operator reference to {@code topOpRef}.
rebindBottomOpRef(p.first, srcRef, topOpRef);
}
Mutable<ILogicalOperator> top2 = new MutableObject<>(p.first);
return new Pair<>(new VariableReferenceExpression(p.second), top2);
} else {
SubplanOperator s = new SubplanOperator();
s.getInputs().add(topOpRef);
srcRef.setValue(new NestedTupleSourceOperator(new MutableObject<>(s)));
Mutable<ILogicalOperator> planRoot = new MutableObject<>(p.first);
s.setRootOp(planRoot);
return new Pair<>(new VariableReferenceExpression(p.second), new MutableObject<>(s));
}
}
}
}
Aggregations