Skip to content

Commit

Permalink
Clean up optionals
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Henneberger <git@danielhenneberger.com>
  • Loading branch information
henneberger committed Oct 27, 2023
1 parent 79c331c commit 8b1ea9f
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public CatalogReader(SqrlSchema rootSchema, RelDataTypeFactory typeFactory, Calc
}

public RelOptTable getTableFromPath(List<String> names) {

NamePath absolutePath = getSqrlAbsolutePath(NamePath.system(names));
String sysTableName = schema.getPathToSysTableMap().get(absolutePath);
if (sysTableName == null) {
Expand All @@ -50,32 +49,18 @@ public NamePath getSqrlAbsolutePath(NamePath path) {
return (rel == null) ? path : rel;
}

public SqlUserDefinedTableFunction getTableFunction(List<String> path) {
List<SqlOperator> result = new ArrayList<>();
String tableFunctionName = String.join(".", path);
//get latest function
if (tableFunctionName.isEmpty()) {
return null;
}
String latestVersionName = SqrlNameMatcher.getLatestVersion(NameCanonicalizer.SYSTEM,
schema.plus().getFunctionNames(), tableFunctionName);
if (latestVersionName == null) {
//todo return optional
return null;
}

Optional<SqlOperator> first = this.getOperatorList()
.stream()
.filter(f -> f.getName().equalsIgnoreCase(latestVersionName))
.findFirst();
// operatorTable.lookupOperatorOverloads(new SqlIdentifier(List.of(latestVersionName), SqlParserPos.ZERO),
// SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, result, nameMatcher());

if (first.isEmpty()) {
return null;
public Optional<SqlUserDefinedTableFunction> getTableFunction(List<String> path) {
if (path.isEmpty()) {
return Optional.empty();
}

return (SqlUserDefinedTableFunction)first.get();
return SqrlNameMatcher.getLatestVersion(NameCanonicalizer.SYSTEM, schema.plus().getFunctionNames(),
String.join(".", path))
.flatMap(s -> this.getOperatorList().stream()
.filter(f -> f.getName().equalsIgnoreCase(s))
.filter(f -> f instanceof SqlUserDefinedTableFunction)
.map(f -> (SqlUserDefinedTableFunction) f)
.findFirst());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.datasqrl.util.DataContextImpl;
import com.datasqrl.calcite.convert.PostgresSqlConverter;
import java.util.Arrays;
import java.util.Optional;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.calcite.adapter.enumerable.EnumerableInterpretable;
Expand Down Expand Up @@ -429,7 +430,7 @@ public String relToString(Dialect dialect, RelNode relNode) {
return sqlToString(dialect, relToSql(dialect, relNode));
}

public SqlUserDefinedTableFunction getTableFunction(List<String> path) {
public Optional<SqlUserDefinedTableFunction> getTableFunction(List<String> path) {
return catalogReader.getTableFunction(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
public class SqrlNameMatcher {
NameCanonicalizer canonicalizer;

final SqlNameMatcher delegate = SqlNameMatchers.withCaseSensitive(false);

public static String getLatestVersion(NameCanonicalizer canonicalizer, Collection<String> functionNames, String prefix) {
public static Optional<String> getLatestVersion(NameCanonicalizer canonicalizer, Collection<String> functionNames, String prefix) {
//todo: use name comparator
Set<String> functionNamesCanon = functionNames.stream()
.map(f-> canonicalizer.name(f).getCanonical())
Expand All @@ -48,6 +46,6 @@ public static String getLatestVersion(NameCanonicalizer canonicalizer, Collectio
}
}

return maxVersion != -1 ? name : null;
return maxVersion != -1 ? Optional.of(name) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.datasqrl.calcite.type.TypeFactory;
import com.datasqrl.canonicalizer.ReservedName;
import com.datasqrl.function.SqrlFunctionParameter;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -68,8 +69,9 @@ private List<PullupColumn> processFirstItem(Iterator<SqlNode> input, PathWalker
String identifier = getIdentifier(item)
.orElseThrow(() -> new RuntimeException("Subqueries are not yet implemented"));

if (catalogReader.getTableFunction(List.of(identifier)) != null) {
handleTableFunction(pathWalker, builder, identifier, params);
if (catalogReader.getTableFunction(List.of(identifier)).isPresent()) {
pathWalker.walk(identifier);
builder.scanFunction(pathWalker.getPath(), List.of());
} else if (context.hasAlias(identifier)) {
handleAlias(input, pathWalker, builder, context, identifier, params);
} else if (identifier.equals(ReservedName.SELF_IDENTIFIER.getCanonical())) {
Expand All @@ -81,12 +83,6 @@ private List<PullupColumn> processFirstItem(Iterator<SqlNode> input, PathWalker
return List.of();
}

private void handleTableFunction(PathWalker pathWalker, SqlJoinPathBuilder builder, String identifier,
List<FunctionParameter> params) {
pathWalker.walk(identifier);
builder.scanFunction(pathWalker.getPath(), List.of());
}

private void handleAlias(Iterator<SqlNode> input, PathWalker pathWalker, SqlJoinPathBuilder builder, Context context, String identifier,
List<FunctionParameter> params) {
if (!input.hasNext()) {
Expand All @@ -98,9 +94,10 @@ private void handleAlias(Iterator<SqlNode> input, PathWalker pathWalker, SqlJoin
.orElseThrow(() -> new RuntimeException("Subqueries are not yet implemented"));
pathWalker.walk(nextIdentifier);

SqlUserDefinedTableFunction fnc = catalogReader.getTableFunction(pathWalker.getPath());
List<SqlNode> args = rewriteArgs(identifier, fnc.getFunction(), context, params);
builder.scanFunction(fnc, args);
Optional<SqlUserDefinedTableFunction> fnc = catalogReader.getTableFunction(pathWalker.getPath());
Preconditions.checkState(fnc.isPresent(), "Table function not found %s", pathWalker.getPath());
List<SqlNode> args = rewriteArgs(identifier, fnc.get().getFunction(), context, params);
builder.scanFunction(fnc.get(), args);
}

private List<PullupColumn> handleSelf(Iterator<SqlNode> input, PathWalker pathWalker, SqlJoinPathBuilder builder, Context context,
Expand All @@ -119,10 +116,11 @@ private List<PullupColumn> handleSelf(Iterator<SqlNode> input, PathWalker pathWa
.orElseThrow(() -> new RuntimeException("Subqueries are not yet implemented"));
pathWalker.walk(nextIdentifier);

SqlUserDefinedTableFunction fnc = catalogReader.getTableFunction(pathWalker.getAbsolutePath());
List<SqlNode> args = rewriteArgs(ReservedName.SELF_IDENTIFIER.getCanonical(), fnc.getFunction(), context,
Optional<SqlUserDefinedTableFunction> fnc = catalogReader.getTableFunction(pathWalker.getAbsolutePath());
Preconditions.checkState(fnc.isPresent(), "Table function not found %s", pathWalker.getPath());
List<SqlNode> args = rewriteArgs(ReservedName.SELF_IDENTIFIER.getCanonical(), fnc.get().getFunction(), context,
params);
builder.scanFunction(fnc, args);
builder.scanFunction(fnc.get(), args);
}
return List.of();
}
Expand All @@ -147,13 +145,13 @@ private void processRemainingItems(Iterator<SqlNode> input, PathWalker pathWalke
pathWalker.walk(nextIdentifier);

String alias = builder.getLatestAlias();
SqlUserDefinedTableFunction fnc = catalogReader.getTableFunction(pathWalker.getPath());
if (fnc == null) {
Optional<SqlUserDefinedTableFunction> fnc = catalogReader.getTableFunction(pathWalker.getPath());
if (fnc.isEmpty()) {
builder.scanNestedTable(pathWalker.getPath());
} else {
List<SqlNode> args = rewriteArgs(alias, fnc.getFunction(), context,
List<SqlNode> args = rewriteArgs(alias, fnc.get().getFunction(), context,
params);
builder.scanFunction(fnc, args)
builder.scanFunction(fnc.get(), args)
.joinLateral();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.datasqrl.util.CalciteUtil.RelDataTypeFieldBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -37,13 +38,16 @@ public class SqlJoinPathBuilder {
final AtomicInteger aliasInt = new AtomicInteger(0);

public SqlJoinPathBuilder scanFunction(List<String> path, List<SqlNode> args) {
SqlUserDefinedTableFunction op = catalogReader.getTableFunction(path);
if (op == null && args.isEmpty()) {
Optional<SqlUserDefinedTableFunction> op = catalogReader.getTableFunction(path);
if (op.isEmpty() && args.isEmpty()) {
scanNestedTable(path);
return this;
}
if (op.isEmpty()) {
throw new RuntimeException(String.format("Could not find table: %s", path));
}

scanFunction(op, args);
scanFunction(op.get(), args);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.datasqrl.calcite.sqrl;

import java.util.List;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.sql.validate.SqlUserDefinedTableFunction;

public interface SqrlCatalogResolver {
RelOptTable getTableFromPath(List<String> names);
SqlUserDefinedTableFunction getTableFunction(List<String> path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public class QualifiedExport {
public Void visit(SqrlAssignment assignment, Void context) {
if (assignment.getIdentifier().names.size() > 1) {
List<String> path = SqrlListUtil.popLast(assignment.getIdentifier().names);
SqlUserDefinedTableFunction tableFunction = framework.getQueryPlanner()
Optional<SqlUserDefinedTableFunction> tableFunction = framework.getQueryPlanner()
.getTableFunction(path);
if (tableFunction == null) {
if (tableFunction.isEmpty()) {
throw addError(ErrorLabel.GENERIC, assignment.getIdentifier(), "Could not find table: %s", String.join(".", path));
}
TableFunction function = tableFunction.getFunction();
TableFunction function = tableFunction.get().getFunction();
if (function instanceof Relationship && ((Relationship) function).getJoinType() != JoinType.CHILD) {
addError(ErrorLabel.GENERIC, assignment.getIdentifier(), "Cannot assign query to table");
}
Expand Down Expand Up @@ -328,8 +328,8 @@ public void validateTable(SqrlAssignment statement, SqlNode query,
List<String> parent = List.of();
if (statement.getIdentifier().names.size() > 1) {
parent = getParentPath(statement);
SqlUserDefinedTableFunction sqrlTable = planner.getTableFunction(parent);
if (sqrlTable == null) {
Optional<SqlUserDefinedTableFunction> sqrlTable = planner.getTableFunction(parent);
if (sqrlTable.isEmpty()) {
throw addError(ErrorLabel.GENERIC, statement.getIdentifier()
.getComponent(statement.getIdentifier().names.size()-1),
"Could not find parent assignment table: %s",
Expand Down Expand Up @@ -646,10 +646,9 @@ private void checkAssignable(SqrlAssignment node) {
return;
}

SqlUserDefinedTableFunction tableFunction = planner.getTableFunction(path);
if (tableFunction == null) {
Optional<SqlUserDefinedTableFunction> tableFunction = planner.getTableFunction(path);
if (tableFunction.isEmpty()) {
addError(ErrorLabel.GENERIC, node, "Cannot column or query to table");
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ public Result visitQuerySpecification(SqlSelect call, Context context) {
SqlIdentifier ident = (SqlIdentifier) node;
if (ident.isStar() && ident.names.size() == 1) {
for (List<String> path : newContext.getAliasPathMap().values()) {
SqlUserDefinedTableFunction sqrlTable = planner.getTableFunction(path);
isA.put(context.root, sqrlTable.getFunction());
Optional<SqlUserDefinedTableFunction> sqrlTable = planner.getTableFunction(path);
if (sqrlTable.isEmpty()) {
throw addError(errorCollector, ErrorLabel.GENERIC, node, "Could not find table %s",getDisplay(ident));
}
isA.put(context.root, sqrlTable.get().getFunction());
}
} else if (ident.isStar() && ident.names.size() == 2) {
List<String> path = newContext.getAliasPath(ident.names.get(0));
SqlUserDefinedTableFunction sqrlTable = planner.getTableFunction(path);
isA.put(context.root, sqrlTable.getFunction());
Optional<SqlUserDefinedTableFunction> sqrlTable = planner.getTableFunction(path);
if (sqrlTable.isEmpty()) {
throw addError(errorCollector, ErrorLabel.GENERIC, node, "Could not find table %s",getDisplay(ident));
}
isA.put(context.root, sqrlTable.get().getFunction());
}
}
}
Expand Down Expand Up @@ -151,12 +157,12 @@ public Result visitTable(SqlIdentifier node, Context context) {

boolean isAlias = context.hasAlias(identifier);
boolean isSelf = identifier.equals(ReservedName.SELF_IDENTIFIER.getCanonical());
SqlUserDefinedTableFunction tableFunction = planner.getTableFunction(List.of(identifier));
Optional<SqlUserDefinedTableFunction> tableFunction = planner.getTableFunction(List.of(identifier));

TableFunction latestTable;
if (tableFunction != null) {
if (tableFunction.isPresent()) {
pathWalker.walk(identifier);
latestTable = tableFunction.getFunction();
latestTable = tableFunction.get().getFunction();
} else if (isAlias) {
if (!input.hasNext()) {
throw addError(errorCollector, ErrorLabel.GENERIC, item, "Alias by itself.");
Expand All @@ -173,23 +179,23 @@ public Result visitTable(SqlIdentifier node, Context context) {

pathWalker.walk(nextIdentifier.get());
//get table of current path (no args)
SqlUserDefinedTableFunction table = planner.getTableFunction(pathWalker.getPath());
Optional<SqlUserDefinedTableFunction> table = planner.getTableFunction(pathWalker.getPath());

if (table == null) {
if (table.isEmpty()) {
throw addError(errorCollector, ErrorLabel.GENERIC, item, "Could not find path: %s",
flattenNames(pathWalker.getUserDefined()));
}

latestTable = table.getFunction();
latestTable = table.get().getFunction();
} else if (isSelf) {
pathWalker.setPath(context.getCurrentPath());
if (!input.hasNext()) {//treat self as a table
SqlUserDefinedTableFunction table = planner.getTableFunction(context.getCurrentPath());
if (table == null) {
Optional<SqlUserDefinedTableFunction> table = planner.getTableFunction(context.getCurrentPath());
if (table.isEmpty()) {
throw addError(errorCollector, ErrorLabel.GENERIC, item, "Could not find parent table: %s",
flattenNames(context.getCurrentPath()));
}
latestTable = table.getFunction();
latestTable = table.get().getFunction();
} else { //treat self as a parameterized binding to the next function
item = input.next();
Optional<String> nextIdentifier = getIdentifier(item);
Expand All @@ -198,12 +204,12 @@ public Result visitTable(SqlIdentifier node, Context context) {
}
pathWalker.walk(nextIdentifier.get());

SqlUserDefinedTableFunction table = planner.getTableFunction(pathWalker.getPath());
if (table == null) {
Optional<SqlUserDefinedTableFunction> table = planner.getTableFunction(pathWalker.getPath());
if (table.isEmpty()) {
throw addError(errorCollector, ErrorLabel.GENERIC, item, "Could not find table: %s",
flattenNames(pathWalker.getUserDefined()));
}
latestTable = table.getFunction();
latestTable = table.get().getFunction();
}
} else {
throw addError(errorCollector, ErrorLabel.GENERIC, item, "Could not find table: %s",
Expand All @@ -218,12 +224,12 @@ public Result visitTable(SqlIdentifier node, Context context) {
}
pathWalker.walk(nextIdentifier.get());

SqlUserDefinedTableFunction table = planner.getTableFunction(pathWalker.getPath());
if (table == null) {
Optional<SqlUserDefinedTableFunction> table = planner.getTableFunction(pathWalker.getPath());
if (table.isEmpty()) {
throw addError(errorCollector, ErrorLabel.GENERIC, item, "Could not find table: %s",
flattenNames(pathWalker.getUserDefined()));
}
latestTable = table.getFunction();
latestTable = table.get().getFunction();
}

RelDataTypeFieldBuilder b = new RelDataTypeFieldBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -160,9 +161,10 @@ public Coords visitObjectField(InferredObjectField field, Object context) {
currentPath = newPath;
}

SqlUserDefinedTableFunction op = framework.getQueryPlanner()
Optional<SqlUserDefinedTableFunction> op = framework.getQueryPlanner()
.getTableFunction(currentPath);
TableFunction function = op.getFunction();
Preconditions.checkState(op.isPresent(), "Could not find table %s", String.join(".", currentPath));
TableFunction function = op.get().getFunction();

Set<String> limitOffset = Set.of("limit", "offset");

Expand All @@ -179,7 +181,7 @@ public Coords visitObjectField(InferredObjectField field, Object context) {

AtomicInteger uniqueOrdinal = new AtomicInteger(0);
//Anticipate all args being found
builder.functionScan(op, 0, function.getParameters().stream()
builder.functionScan(op.get(), 0, function.getParameters().stream()
.map(param -> new RexDynamicParam(param.getType(null), uniqueOrdinal.getAndIncrement()))
.collect(Collectors.toList()));

Expand All @@ -190,7 +192,7 @@ public Coords visitObjectField(InferredObjectField field, Object context) {
Map<List<String>, InputValueDefinition> argMap = new HashMap<>(Maps.uniqueIndex(arg, n->List.of(n.getName())));

//Iterate over all required args in the function
for (FunctionParameter p : op.getFunction().getParameters()) {
for (FunctionParameter p : op.get().getFunction().getParameters()) {
SqrlFunctionParameter parameter = (SqrlFunctionParameter) p;
//check parameter is in the arg list, if so then dynamic param
InputValueDefinition def;
Expand Down
Loading

0 comments on commit 8b1ea9f

Please sign in to comment.