Skip to content

Commit

Permalink
Fix race caused by shared PageProcessor
Browse files Browse the repository at this point in the history
Fix race which could cause queries to fail when using
concat(array, array), or when enabling columnar processing.

Previously PageProcessor was assumed to be stateless. However, when
dictionary aware processing was introduced they became stateful, and
became even more stateful with the introduction of instance methods for
parametric functions.
  • Loading branch information
cberner committed Jan 14, 2016
1 parent e106b4f commit c0e32e0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
5 changes: 5 additions & 0 deletions presto-docs/src/main/sphinx/release/release-0.132.rst
Expand Up @@ -2,6 +2,11 @@
Release 0.132
=============

.. warning::

:func:`concat` on :ref:`array_type`, or enabling ``columnar_processing_dictionary``
may cause queries to fail in this release. This is fixed in :doc:`/release/release-0.133`.

General Changes
---------------

Expand Down
2 changes: 2 additions & 0 deletions presto-docs/src/main/sphinx/release/release-0.133.rst
Expand Up @@ -14,3 +14,5 @@ General Changes
This optimization is turned off by default. It can be configured via the
``optimizer.dictionary-aggregation`` config property or the
``dictionary_aggregation`` session property.
* Fix race which could cause queries to fail when using :func:`concat` on
:ref:`array_type`, or when enabling ``columnar_processing_dictionary``.
Expand Up @@ -46,25 +46,25 @@ public class ExpressionCompiler
{
private final Metadata metadata;

private final LoadingCache<CacheKey, PageProcessor> pageProcessors = CacheBuilder.newBuilder().maximumSize(1000).build(
new CacheLoader<CacheKey, PageProcessor>()
private final LoadingCache<CacheKey, Class<? extends PageProcessor>> pageProcessors = CacheBuilder.newBuilder().maximumSize(1000).build(
new CacheLoader<CacheKey, Class<? extends PageProcessor>>()
{
@Override
public PageProcessor load(CacheKey key)
public Class<? extends PageProcessor> load(CacheKey key)
throws Exception
{
return compileAndInstantiate(key.getFilter(), key.getProjections(), new PageProcessorCompiler(metadata), PageProcessor.class);
return compile(key.getFilter(), key.getProjections(), new PageProcessorCompiler(metadata), PageProcessor.class);
}
});

private final LoadingCache<CacheKey, CursorProcessor> cursorProcessors = CacheBuilder.newBuilder().maximumSize(1000).build(
new CacheLoader<CacheKey, CursorProcessor>()
private final LoadingCache<CacheKey, Class<? extends CursorProcessor>> cursorProcessors = CacheBuilder.newBuilder().maximumSize(1000).build(
new CacheLoader<CacheKey, Class<? extends CursorProcessor>>()
{
@Override
public CursorProcessor load(CacheKey key)
public Class<? extends CursorProcessor> load(CacheKey key)
throws Exception
{
return compileAndInstantiate(key.getFilter(), key.getProjections(), new CursorProcessorCompiler(metadata), CursorProcessor.class);
return compile(key.getFilter(), key.getProjections(), new CursorProcessorCompiler(metadata), CursorProcessor.class);
}
});

Expand All @@ -82,23 +82,31 @@ public long getCacheSize()

public CursorProcessor compileCursorProcessor(RowExpression filter, List<RowExpression> projections, Object uniqueKey)
{
return cursorProcessors.getUnchecked(new CacheKey(filter, projections, uniqueKey));
try {
return cursorProcessors.getUnchecked(new CacheKey(filter, projections, uniqueKey))
.newInstance();
}
catch (ReflectiveOperationException e) {
throw Throwables.propagate(e);
}
}

public PageProcessor compilePageProcessor(RowExpression filter, List<RowExpression> projections)
{
return pageProcessors.getUnchecked(new CacheKey(filter, projections, null));
try {
return pageProcessors.getUnchecked(new CacheKey(filter, projections, null))
.newInstance();
}
catch (ReflectiveOperationException e) {
throw Throwables.propagate(e);
}
}

private <T> T compileAndInstantiate(RowExpression filter, List<RowExpression> projections, BodyCompiler<T> bodyCompiler, Class<? extends T> superType)
private <T> Class<? extends T> compile(RowExpression filter, List<RowExpression> projections, BodyCompiler<T> bodyCompiler, Class<? extends T> superType)
{
// create filter and project page iterator class
try {
Class<? extends T> clazz = compileProcessor(filter, projections, bodyCompiler, superType);
return clazz.newInstance();
}
catch (ReflectiveOperationException e) {
throw Throwables.propagate(e);
return compileProcessor(filter, projections, bodyCompiler, superType);
}
catch (CompilationException e) {
throw new PrestoException(COMPILER_ERROR, e.getCause());
Expand Down
@@ -0,0 +1,51 @@
/*
* 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;

import com.facebook.presto.metadata.FunctionKind;
import com.facebook.presto.metadata.MetadataManager;
import com.facebook.presto.metadata.Signature;
import com.facebook.presto.operator.PageProcessor;
import com.facebook.presto.spi.type.BooleanType;
import com.facebook.presto.sql.gen.ExpressionCompiler;
import com.facebook.presto.sql.relational.CallExpression;
import com.facebook.presto.sql.relational.ConstantExpression;
import com.facebook.presto.sql.relational.InputReferenceExpression;
import com.facebook.presto.sql.relational.RowExpression;
import com.facebook.presto.type.ArrayType;
import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;

import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
import static org.testng.Assert.assertTrue;

public class TestPageProcessorCompiler
{
@Test
public void testNoCaching()
throws Throwable
{
MetadataManager metadata = MetadataManager.createTestMetadataManager();
ExpressionCompiler compiler = new ExpressionCompiler(metadata);
ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder();
ArrayType arrayType = new ArrayType(VARCHAR);
Signature signature = new Signature("concat", FunctionKind.SCALAR, arrayType.getTypeSignature(), arrayType.getTypeSignature(), arrayType.getTypeSignature());
projectionsBuilder.add(new CallExpression(signature, arrayType, ImmutableList.of(new InputReferenceExpression(0, arrayType), new InputReferenceExpression(1, arrayType))));

ImmutableList<RowExpression> projections = projectionsBuilder.build();
PageProcessor pageProcessor = compiler.compilePageProcessor(new ConstantExpression(true, BooleanType.BOOLEAN), projections);
PageProcessor pageProcessor2 = compiler.compilePageProcessor(new ConstantExpression(true, BooleanType.BOOLEAN), projections);
assertTrue(pageProcessor != pageProcessor2);
}
}

0 comments on commit c0e32e0

Please sign in to comment.