Skip to content

Commit

Permalink
Fixed nested test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Guian Gumpac <guian.gumpac@improving.com>
  • Loading branch information
GumpacG committed Aug 22, 2023
1 parent ba5b851 commit e4e866e
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.opensearch.sql.datasource.model;

import com.google.common.collect.ImmutableMap;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.datasource.DataSourceService;
import org.opensearch.sql.planner.logical.LogicalPlan;
import org.opensearch.sql.planner.physical.PhysicalPlan;
import org.opensearch.sql.storage.StorageEngine;
import org.opensearch.sql.storage.Table;

import java.util.Map;
import java.util.Set;

import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;
import static org.opensearch.sql.data.type.ExprCoreType.STRING;

public class EmptyDataSourceService {
private static DataSourceService emptyDataSourceService = new DataSourceService() {
@Override
public DataSource getDataSource(String dataSourceName) {
return new DataSource(DEFAULT_DATASOURCE_NAME, DataSourceType.OPENSEARCH, storageEngine());
}

@Override
public Set<DataSourceMetadata> getDataSourceMetadata(boolean isDefaultDataSourceRequired) {
return Set.of();
}

@Override
public DataSourceMetadata getDataSourceMetadata(String name) {
return null;
}

@Override
public void createDataSource(DataSourceMetadata metadata) {

}

@Override
public void updateDataSource(DataSourceMetadata dataSourceMetadata) {

}

@Override
public void deleteDataSource(String dataSourceName) {

}

@Override
public Boolean dataSourceExists(String dataSourceName) {
return null;
}
};

private static StorageEngine storageEngine() {
Table table =
new Table() {
@Override
public boolean exists() {
return true;
}

@Override
public void create(Map<String, ExprType> schema) {
throw new UnsupportedOperationException("Create table is not supported");
}

@Override
public Map<String, ExprType> getFieldTypes() {
return null;
}

@Override
public PhysicalPlan implement(LogicalPlan plan) {
throw new UnsupportedOperationException();
}

public Map<String, ExprType> getReservedFieldTypes() {
return ImmutableMap.of("_test", STRING);
}
};
return (dataSourceSchemaName, tableName) -> table;
}


public static DataSourceService getEmptyDataSourceService() {
return emptyDataSourceService;
}
}
11 changes: 10 additions & 1 deletion core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.opensearch.sql.expression.parse.RegexExpression;
import org.opensearch.sql.expression.span.SpanExpression;
import org.opensearch.sql.expression.window.ranking.RankingWindowFunction;
import static org.opensearch.sql.datasource.model.EmptyDataSourceService.getEmptyDataSourceService;

public class DSL {

Expand Down Expand Up @@ -615,6 +616,10 @@ public static FunctionExpression xor(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.XOR, expressions);
}

public static FunctionExpression nested(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.NESTED, expressions);
}

public static FunctionExpression not(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.NOT, expressions);
}
Expand Down Expand Up @@ -823,6 +828,10 @@ public static FunctionExpression typeof(Expression value) {
return compile(FunctionProperties.None, BuiltinFunctionName.TYPEOF, value);
}

public static FunctionExpression match_phrase_prefix(Expression... args) {
return compile(FunctionProperties.None, BuiltinFunctionName.MATCH_PHRASE_PREFIX, args);
}

public static FunctionExpression now(FunctionProperties functionProperties, Expression... args) {
return compile(functionProperties, BuiltinFunctionName.NOW, args);
}
Expand Down Expand Up @@ -905,7 +914,7 @@ public static FunctionExpression utc_timestamp(
private static <T extends FunctionImplementation> T compile(
FunctionProperties functionProperties, BuiltinFunctionName bfn, Expression... args) {
return (T)
BuiltinFunctionRepository.getInstance(null)
BuiltinFunctionRepository.getInstance(getEmptyDataSourceService())
.compile(functionProperties, bfn.getName(), Arrays.asList(args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public class BuiltinFunctionRepository {
* @return singleton instance
*/
public static synchronized BuiltinFunctionRepository getInstance(DataSourceService dataSourceService) {
// Create hash code for tests with dataSourceService as null
int dataSourceServiceHash = dataSourceService == null ? 0 : dataSourceService.hashCode();
// TODO: get hashCode of dataSourceMetadata
int dataSourceServiceHash = dataSourceService.hashCode();

// Creates new Repository for every dataSourceService
if (!instance.containsKey(dataSourceServiceHash)) {
Expand All @@ -86,15 +86,15 @@ public static synchronized BuiltinFunctionRepository getInstance(DataSourceServi
TextFunction.register(repository);
TypeCastOperator.register(repository);
SystemFunctions.register(repository);

if (dataSourceService != null) {
for (DataSourceMetadata metadata : dataSourceService.getDataSourceMetadata(true)) {
dataSourceService
.getDataSource(metadata.getName())
.getStorageEngine()
.getFunctions()
.forEach(repository::register);
}
// Temporary as part of https://github.com/opensearch-project/sql/issues/811
// TODO: remove this resolver when Analyzers are moved to opensearch module
repository.register(new NestedFunctionResolver());

for (DataSourceMetadata metadata : dataSourceService.getDataSourceMetadata(true)) {
dataSourceService
.getDataSource(metadata.getName())
.getStorageEngine().getFunctions().
forEach(function -> repository.register(function));
}
instance.put(dataSourceServiceHash, repository);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.expression.function;

import org.apache.commons.lang3.tuple.Pair;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.FunctionExpression;
import org.opensearch.sql.expression.env.Environment;

public class NestedFunctionResolver implements FunctionResolver{
@Override
public Pair<FunctionSignature, FunctionBuilder> resolve(FunctionSignature unresolvedSignature) {
return Pair.of(unresolvedSignature,
(functionProperties, arguments) ->
new FunctionExpression(BuiltinFunctionName.NESTED.getName(), arguments) {
@Override
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) {
return valueEnv.resolve(getArguments().get(0));
}

@Override
public ExprType type() {
return getArguments().get(0).type();
}
});
}

@Override
public FunctionName getFunctionName() {
return BuiltinFunctionName.NESTED.getName();
}
}
Loading

0 comments on commit e4e866e

Please sign in to comment.