Skip to content

Commit

Permalink
Enable listing SQL functions with session property
Browse files Browse the repository at this point in the history
Also, rename configuration property list-non-built-in-functions to
list-sql-functions.
  • Loading branch information
caithagoras0 authored and Rongrong Zhong committed Jan 14, 2020
1 parent b590a5c commit 479e655
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public final class SystemSessionProperties
public static final String INDEX_LOADER_TIMEOUT = "index_loader_timeout";
public static final String OPTIMIZED_REPARTITIONING_ENABLED = "optimized_repartitioning";
public static final String AGGREGATION_PARTITIONING_MERGING_STRATEGY = "aggregation_partitioning_merging_strategy";
public static final String LIST_BUILT_IN_FUNCTIONS_ONLY = "list_built_in_functions_only";

private final List<PropertyMetadata<?>> sessionProperties;

Expand Down Expand Up @@ -678,7 +679,12 @@ public SystemSessionProperties(
featuresConfig.getAggregationPartitioningMergingStrategy(),
false,
value -> AggregationPartitioningMergingStrategy.valueOf(((String) value).toUpperCase()),
AggregationPartitioningMergingStrategy::name));
AggregationPartitioningMergingStrategy::name),
booleanProperty(
LIST_BUILT_IN_FUNCTIONS_ONLY,
"Only List built-in functions in SHOW FUNCTIONS",
featuresConfig.isListBuiltInFunctionsOnly(),
false));
}

public List<PropertyMetadata<?>> getSessionProperties()
Expand Down Expand Up @@ -1153,4 +1159,9 @@ public static AggregationPartitioningMergingStrategy getAggregationPartitioningM
{
return session.getSystemProperty(AGGREGATION_PARTITIONING_MERGING_STRATEGY, AggregationPartitioningMergingStrategy.class);
}

public static boolean isListBuiltInFunctionsOnly(Session session)
{
return session.getSystemProperty(LIST_BUILT_IN_FUNCTIONS_ONLY, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package com.facebook.presto.metadata;

import com.facebook.presto.Session;
import com.facebook.presto.operator.aggregation.InternalAggregationFunction;
import com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation;
import com.facebook.presto.operator.window.WindowFunctionSupplier;
Expand Down Expand Up @@ -61,6 +62,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static com.facebook.presto.SystemSessionProperties.isListBuiltInFunctionsOnly;
import static com.facebook.presto.metadata.BuiltInFunctionNamespaceManager.DEFAULT_NAMESPACE;
import static com.facebook.presto.metadata.CastType.toOperatorType;
import static com.facebook.presto.spi.StandardErrorCode.AMBIGUOUS_FUNCTION_CALL;
Expand Down Expand Up @@ -99,8 +101,6 @@ public class FunctionManager
private final Map<CatalogSchemaPrefix, String> functionNamespaces = new ConcurrentHashMap<>();
private final Map<String, FunctionNamespaceManager<?>> functionNamespaceManagers = new ConcurrentHashMap<>();

private final boolean listNonBuiltInFunctions;

@Inject
public FunctionManager(
TypeManager typeManager,
Expand All @@ -121,7 +121,6 @@ public FunctionManager(
}
// TODO: Provide a more encapsulated way for TransactionManager to register FunctionNamespaceManager
transactionManager.registerFunctionNamespaceManager(BuiltInFunctionNamespaceManager.ID, builtInFunctionNamespaceManager);
this.listNonBuiltInFunctions = featuresConfig.isListNonBuiltInFunctions();
}

@VisibleForTesting
Expand Down Expand Up @@ -171,11 +170,11 @@ public void registerBuiltInFunctions(List<? extends BuiltInFunction> functions)
builtInFunctionNamespaceManager.registerBuiltInFunctions(functions);
}

public List<SqlFunction> listFunctions()
public List<SqlFunction> listFunctions(Session session)
{
Collection<FunctionNamespaceManager<?>> managers = listNonBuiltInFunctions ?
functionNamespaceManagers.values() :
ImmutableSet.of(builtInFunctionNamespaceManager);
Collection<FunctionNamespaceManager<?>> managers = isListBuiltInFunctionsOnly(session) ?
ImmutableSet.of(builtInFunctionNamespaceManager) :
functionNamespaceManagers.values();

return managers.stream()
.flatMap(manager -> manager.listFunctions().stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface Metadata

Type getType(TypeSignature signature);

List<SqlFunction> listFunctions();
List<SqlFunction> listFunctions(Session session);

void registerBuiltInFunctions(List<? extends BuiltInFunction> functions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ public Type getType(TypeSignature signature)
return typeManager.getType(signature);
}

public List<SqlFunction> listFunctions()
public List<SqlFunction> listFunctions(Session session)
{
// TODO: transactional when FunctionManager is made transactional
return functions.listFunctions();
return functions.listFunctions(session);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class FeaturesConfig

private Duration indexLoaderTimeout = new Duration(20, SECONDS);

private boolean listNonBuiltInFunctions;
private boolean listBuiltInFunctionsOnly = true;

public enum JoinReorderingStrategy
{
Expand Down Expand Up @@ -1135,15 +1135,15 @@ public FeaturesConfig setOptimizedRepartitioningEnabled(boolean optimizedReparti
return this;
}

public boolean isListNonBuiltInFunctions()
public boolean isListBuiltInFunctionsOnly()
{
return listNonBuiltInFunctions;
return listBuiltInFunctionsOnly;
}

@Config("list-non-built-in-functions")
public FeaturesConfig setListNonBuiltInFunctions(boolean listNonBuiltInFunctions)
@Config("list-built-in-functions-only")
public FeaturesConfig setListBuiltInFunctionsOnly(boolean listBuiltInFunctionsOnly)
{
this.listNonBuiltInFunctions = listNonBuiltInFunctions;
this.listBuiltInFunctionsOnly = listBuiltInFunctionsOnly;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private static String toQualifedName(Object objectName, Optional<String> columnN
protected Node visitShowFunctions(ShowFunctions node, Void context)
{
ImmutableList.Builder<Expression> rows = ImmutableList.builder();
for (SqlFunction function : metadata.listFunctions()) {
for (SqlFunction function : metadata.listFunctions(session)) {
rows.add(row(
function.getSignature().getName().getFunctionNamespace().equals(DEFAULT_NAMESPACE) ?
new StringLiteral(function.getSignature().getNameSuffix()) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Type getType(TypeSignature signature)
}

@Override
public List<SqlFunction> listFunctions()
public List<SqlFunction> listFunctions(Session session)
{
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void testListingHiddenFunctions()
{
TypeRegistry typeManager = new TypeRegistry();
FunctionManager functionManager = createFunctionManager(typeManager);
List<SqlFunction> functions = functionManager.listFunctions();
List<SqlFunction> functions = functionManager.listFunctions(TEST_SESSION);
List<String> names = transform(functions, input -> input.getSignature().getNameSuffix());

assertTrue(names.contains("length"), "Expected function names " + names + " to contain 'length'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testDefaults()
.setOptimizeFullOuterJoinWithCoalesce(true)
.setIndexLoaderTimeout(new Duration(20, SECONDS))
.setOptimizedRepartitioningEnabled(false)
.setListNonBuiltInFunctions(false));
.setListBuiltInFunctionsOnly(true));
}

@Test
Expand Down Expand Up @@ -207,7 +207,7 @@ public void testExplicitPropertyMappings()
.put("optimizer.optimize-full-outer-join-with-coalesce", "false")
.put("index-loader-timeout", "10s")
.put("experimental.optimized-repartitioning", "true")
.put("list-non-built-in-functions", "true")
.put("list-built-in-functions-only", "false")
.build();

FeaturesConfig expected = new FeaturesConfig()
Expand Down Expand Up @@ -286,7 +286,7 @@ public void testExplicitPropertyMappings()
.setOptimizeFullOuterJoinWithCoalesce(false)
.setIndexLoaderTimeout(new Duration(10, SECONDS))
.setOptimizedRepartitioningEnabled(true)
.setListNonBuiltInFunctions(true);
.setListBuiltInFunctionsOnly(false);
assertFullMapping(properties, expected);
}

Expand Down

0 comments on commit 479e655

Please sign in to comment.