Skip to content

Commit

Permalink
Use query execution start time as the value of now-like functions. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Ksyunz authored Dec 2, 2022
1 parent 1287e89 commit 43ceda1
Show file tree
Hide file tree
Showing 56 changed files with 1,706 additions and 774 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
package org.opensearch.sql.analysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.NamedExpression;
import org.opensearch.sql.expression.function.FunctionProperties;

/**
* The context used for Analyzer.
Expand All @@ -26,13 +24,8 @@ public class AnalysisContext {
@Getter
private final List<NamedExpression> namedParseExpressions;

/**
* Storage for values of functions which return a constant value.
* We are storing the values there to use it in sequential calls to those functions.
* For example, `now` function should the same value during processing a query.
*/
@Getter
private final Map<String, Expression> constantFunctionValues;
private final FunctionProperties functionProperties;

public AnalysisContext() {
this(new TypeEnvironment(null));
Expand All @@ -45,7 +38,7 @@ public AnalysisContext() {
public AnalysisContext(TypeEnvironment environment) {
this.environment = environment;
this.namedParseExpressions = new ArrayList<>();
this.constantFunctionValues = new HashMap<>();
this.functionProperties = new FunctionProperties();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public LogicalPlan visitTableFunction(TableFunction node, AnalysisContext contex
.map(unresolvedExpression -> this.expressionAnalyzer.analyze(unresolvedExpression, context))
.collect(Collectors.toList());
TableFunctionImplementation tableFunctionImplementation
= (TableFunctionImplementation) repository.compile(
= (TableFunctionImplementation) repository.compile(context.getFunctionProperties(),
dataSourceSchemaIdentifierNameResolver.getDataSourceName(), functionName, arguments);
context.push();
TypeEnvironment curEnv = context.peek();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.opensearch.sql.ast.expression.Case;
import org.opensearch.sql.ast.expression.Cast;
import org.opensearch.sql.ast.expression.Compare;
import org.opensearch.sql.ast.expression.ConstantFunction;
import org.opensearch.sql.ast.expression.EqualTo;
import org.opensearch.sql.ast.expression.Field;
import org.opensearch.sql.ast.expression.Function;
Expand Down Expand Up @@ -78,7 +77,8 @@ public class ExpressionAnalyzer extends AbstractNodeVisitor<Expression, Analysis
public Expression visitCast(Cast node, AnalysisContext context) {
final Expression expression = node.getExpression().accept(this, context);
return (Expression) repository
.compile(node.convertFunctionName(), Collections.singletonList(expression));
.compile(context.getFunctionProperties(), node.convertFunctionName(),
Collections.singletonList(expression));
}

public ExpressionAnalyzer(
Expand Down Expand Up @@ -156,7 +156,7 @@ public Expression visitAggregateFunction(AggregateFunction node, AnalysisContext
builder.add(arg.accept(this, context));
}
Aggregator aggregator = (Aggregator) repository.compile(
builtinFunctionName.get().getName(), builder.build());
context.getFunctionProperties(), builtinFunctionName.get().getName(), builder.build());
aggregator.distinct(node.getDistinct());
if (node.condition() != null) {
aggregator.condition(analyze(node.condition(), context));
Expand All @@ -173,27 +173,15 @@ public Expression visitRelevanceFieldList(RelevanceFieldList node, AnalysisConte
ImmutableMap.copyOf(node.getFieldList())));
}

@Override
public Expression visitConstantFunction(ConstantFunction node, AnalysisContext context) {
var valueName = node.getFuncName();
if (context.getConstantFunctionValues().containsKey(valueName)) {
return context.getConstantFunctionValues().get(valueName);
}

var value = visitFunction(node, context);
value = DSL.literal(value.valueOf());
context.getConstantFunctionValues().put(valueName, value);
return value;
}

@Override
public Expression visitFunction(Function node, AnalysisContext context) {
FunctionName functionName = FunctionName.of(node.getFuncName());
List<Expression> arguments =
node.getFuncArgs().stream()
.map(unresolvedExpression -> analyze(unresolvedExpression, context))
.collect(Collectors.toList());
return (Expression) repository.compile(functionName, arguments);
return (Expression) repository.compile(context.getFunctionProperties(),
functionName, arguments);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -237,7 +225,8 @@ public Expression visitCompare(Compare node, AnalysisContext context) {
Expression left = analyze(node.getLeft(), context);
Expression right = analyze(node.getRight(), context);
return (Expression)
repository.compile(functionName, Arrays.asList(left, right));
repository.compile(context.getFunctionProperties(),
functionName, Arrays.asList(left, right));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public Expression visitFunction(FunctionExpression node, AnalysisContext context
final List<Expression> args =
node.getArguments().stream().map(expr -> expr.accept(this, context))
.collect(Collectors.toList());
return (Expression) repository.compile(node.getFunctionName(), args);
return (Expression) repository.compile(context.getFunctionProperties(),
node.getFunctionName(), args);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.opensearch.sql.ast.expression.Case;
import org.opensearch.sql.ast.expression.Cast;
import org.opensearch.sql.ast.expression.Compare;
import org.opensearch.sql.ast.expression.ConstantFunction;
import org.opensearch.sql.ast.expression.EqualTo;
import org.opensearch.sql.ast.expression.Field;
import org.opensearch.sql.ast.expression.Function;
Expand Down Expand Up @@ -126,10 +125,6 @@ public T visitRelevanceFieldList(RelevanceFieldList node, C context) {
return visitChildren(node, context);
}

public T visitConstantFunction(ConstantFunction node, C context) {
return visitChildren(node, context);
}

public T visitUnresolvedAttribute(UnresolvedAttribute node, C context) {
return visitChildren(node, context);
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/main/java/org/opensearch/sql/ast/dsl/AstDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.opensearch.sql.ast.expression.Case;
import org.opensearch.sql.ast.expression.Cast;
import org.opensearch.sql.ast.expression.Compare;
import org.opensearch.sql.ast.expression.ConstantFunction;
import org.opensearch.sql.ast.expression.DataType;
import org.opensearch.sql.ast.expression.EqualTo;
import org.opensearch.sql.ast.expression.Field;
Expand Down Expand Up @@ -246,10 +245,6 @@ public static Function function(String funcName, UnresolvedExpression... funcArg
return new Function(funcName, Arrays.asList(funcArgs));
}

public static Function constantFunction(String funcName, UnresolvedExpression... funcArgs) {
return new ConstantFunction(funcName, Arrays.asList(funcArgs));
}

/**
* CASE
* WHEN search_condition THEN result_expr
Expand Down

This file was deleted.

Loading

0 comments on commit 43ceda1

Please sign in to comment.