Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions presto-docs/src/main/sphinx/functions/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,9 @@ Unicode Functions
are replaced with `replace`. The replacement string `replace` must either
be a single character or empty (in which case invalid characters are
removed).

.. function:: key_sampling_percent(varchar) -> double
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also want to introduce a version of this function for varbinary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm interesting. I will open an issue for later. For now are using only varchar version.


Generates a double value between 0.0 and 1.0 based on the hash of the given ``varchar``.
This function is useful for deterministic sampling of data.

Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public final class SystemSessionProperties

//TODO: Prestissimo related session properties that are temporarily put here. They will be relocated in the future
public static final String PRESTISSIMO_SIMPLIFIED_EXPRESSION_EVALUATION_ENABLED = "simplified_expression_evaluation_enabled";
public static final String KEY_BASED_SAMPLING_ENABLED = "key_based_sampling_enabled";
public static final String KEY_BASED_SAMPLING_PERCENTAGE = "key_based_sampling_percentage";
public static final String KEY_BASED_SAMPLING_FUNCTION = "key_based_sampling_function";

private final List<PropertyMetadata<?>> sessionProperties;

Expand Down Expand Up @@ -1142,7 +1145,22 @@ public SystemSessionProperties(
EXCEEDED_MEMORY_LIMIT_HEAP_DUMP_FILE_DIRECTORY,
"Directory to which heap snapshot will be dumped, if heap_dump_on_exceeded_memory_limit_enabled",
System.getProperty("java.io.tmpdir"), // This is intended to be used for debugging purposes only and thus we does not need an associated config property
true));
true),
booleanProperty(
KEY_BASED_SAMPLING_ENABLED,
"Key based sampling of tables enabled",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still not really a description of what this feature does. If you think there's no way to describe it clearly here, add proper documentation of this feature and refer it so people can learn more about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - where do I add it? Add a chapter to the user doc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the PR message has pretty much the documentation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users won't read PR messages though. This is ok for now. But we probably want to document recommended usage, behavior and limitations once we polish the feature off.

false,
false),
doubleProperty(
KEY_BASED_SAMPLING_PERCENTAGE,
"Percentage of keys to be sampled",
0.01,
false),
stringProperty(
KEY_BASED_SAMPLING_FUNCTION,
"Sampling function for key based sampling",
"key_sampling_percent",
false));
}

public static boolean isEmptyJoinOptimization(Session session)
Expand All @@ -1165,6 +1183,21 @@ public static boolean isAllowWindowOrderByLiterals(Session session)
return session.getSystemProperty(ALLOW_WINDOW_ORDER_BY_LITERALS, Boolean.class);
}

public static boolean isKeyBasedSamplingEnabled(Session session)
{
return session.getSystemProperty(KEY_BASED_SAMPLING_ENABLED, Boolean.class);
}

public static double getKeyBasedSamplingPercentage(Session session)
{
return session.getSystemProperty(KEY_BASED_SAMPLING_PERCENTAGE, Double.class);
}

public static String getKeyBasedSamplingFunction(Session session)
{
return session.getSystemProperty(KEY_BASED_SAMPLING_FUNCTION, String.class);
}

public List<PropertyMetadata<?>> getSessionProperties()
{
return sessionProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
import com.facebook.presto.operator.scalar.WordStemFunction;
import com.facebook.presto.operator.scalar.sql.ArraySqlFunctions;
import com.facebook.presto.operator.scalar.sql.MapNormalizeFunction;
import com.facebook.presto.operator.scalar.sql.SimpleSamplingPercent;
import com.facebook.presto.operator.window.CumulativeDistributionFunction;
import com.facebook.presto.operator.window.DenseRankFunction;
import com.facebook.presto.operator.window.FirstValueFunction;
Expand Down Expand Up @@ -842,6 +843,7 @@ private List<? extends SqlFunction> getBuildInFunctions(FeaturesConfig featuresC
.sqlInvokedScalar(MapNormalizeFunction.class)
.sqlInvokedScalars(ArraySqlFunctions.class)
.sqlInvokedScalars(ArrayIntersectFunction.class)
.sqlInvokedScalars(SimpleSamplingPercent.class)
.scalar(DynamicFilterPlaceholderFunction.class)
.scalars(EnumCasts.class)
.scalars(LongEnumOperators.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.operator.scalar.sql;

import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.SqlInvokedScalarFunction;
import com.facebook.presto.spi.function.SqlParameter;
import com.facebook.presto.spi.function.SqlType;

public class SimpleSamplingPercent
{
private SimpleSamplingPercent() {}

@SqlInvokedScalarFunction(value = "key_sampling_percent", deterministic = true, calledOnNullInput = false)
@Description("Returns a value between 0.0 and 1.0 using the hash of the given input string")
@SqlParameter(name = "input", type = "varchar")
@SqlType("double")
public static String keySamplingPercent()
{
return "return (abs(from_ieee754_64(xxhash64(cast(input as varbinary)))) % 100) / 100. ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import com.facebook.presto.sql.planner.optimizations.HashGenerationOptimizer;
import com.facebook.presto.sql.planner.optimizations.ImplementIntersectAndExceptAsUnion;
import com.facebook.presto.sql.planner.optimizations.IndexJoinOptimizer;
import com.facebook.presto.sql.planner.optimizations.KeyBasedSampler;
import com.facebook.presto.sql.planner.optimizations.LimitPushDown;
import com.facebook.presto.sql.planner.optimizations.MetadataDeleteOptimizer;
import com.facebook.presto.sql.planner.optimizations.MetadataQueryOptimizer;
Expand Down Expand Up @@ -406,6 +407,7 @@ public PlanOptimizers(
estimatedExchangesCostCalculator,
ImmutableSet.of(new RewriteAggregationIfToFilter(metadata.getFunctionAndTypeManager()))),
predicatePushDown,
new KeyBasedSampler(metadata, sqlParser),
new IterativeOptimizer(
ruleStats,
statsCalculator,
Expand Down
Loading