Skip to content

Commit

Permalink
Merge branch 'prestodb:master' into okie-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariamalmesfer authored and Mariam Almesfer committed Dec 25, 2024
2 parents 3de8c35 + dba4fca commit 472111e
Show file tree
Hide file tree
Showing 225 changed files with 3,446 additions and 650 deletions.
17 changes: 10 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<dep.joda.version>2.12.7</dep.joda.version>
<dep.tempto.version>1.53</dep.tempto.version>
<dep.testng.version>7.5</dep.testng.version>
<dep.lucene.version>8.10.0</dep.lucene.version>
<dep.assertj-core.version>3.8.0</dep.assertj-core.version>
<dep.logback.version>1.2.13</dep.logback.version>
<dep.parquet.version>1.13.1</dep.parquet.version>
Expand Down Expand Up @@ -2038,13 +2039,13 @@
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>7.2.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</exclusion>
</exclusions>
<version>${dep.lucene.version}</version>
</dependency>

<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>${dep.lucene.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -2346,6 +2347,8 @@
<ignoredClassPattern>META-INF.versions.9.module-info</ignoredClassPattern>
<!-- Duplicate class is being brought in by several netty dependencies-->
<ignoredClassPattern>META-INF.versions.11.module-info</ignoredClassPattern>
<!-- Ignore duplicate classes related to lucene-core and ranger-apache -->
<ignoredClassPattern>META-INF.versions.9.org.apache.lucene.*</ignoredClassPattern>
</ignoredClassPatterns>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public enum SemanticErrorCode
VIEW_IS_STALE,
VIEW_IS_RECURSIVE,
MATERIALIZED_VIEW_IS_RECURSIVE,
MISSING_VIEW,
VIEW_ALREADY_EXISTS,

NON_NUMERIC_SAMPLE_PERCENTAGE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.facebook.presto.sql.tree.RenameColumn;
import com.facebook.presto.sql.tree.RenameSchema;
import com.facebook.presto.sql.tree.RenameTable;
import com.facebook.presto.sql.tree.RenameView;
import com.facebook.presto.sql.tree.ResetSession;
import com.facebook.presto.sql.tree.Revoke;
import com.facebook.presto.sql.tree.RevokeRoles;
Expand Down Expand Up @@ -134,6 +135,7 @@ private StatementUtils() {}
builder.put(AddConstraint.class, QueryType.DATA_DEFINITION);
builder.put(AlterColumnNotNull.class, QueryType.DATA_DEFINITION);
builder.put(CreateView.class, QueryType.DATA_DEFINITION);
builder.put(RenameView.class, QueryType.DATA_DEFINITION);
builder.put(TruncateTable.class, QueryType.DATA_DEFINITION);
builder.put(DropView.class, QueryType.DATA_DEFINITION);
builder.put(CreateMaterializedView.class, QueryType.DATA_DEFINITION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static Object execute(Function<Scope, BytecodeNode> nodeGenerator, Parame
System.out.println(tree);
}

ClassLoader classLoader = parentClassLoader.orElse(BytecodeExpressionAssertions.class.getClassLoader());
ClassLoader classLoader = parentClassLoader.orElseGet(BytecodeExpressionAssertions.class::getClassLoader);

return classGenerator(classLoader)
.defineClass(classDefinition, Object.class)
Expand Down
70 changes: 67 additions & 3 deletions presto-common/src/main/java/com/facebook/presto/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
import com.facebook.presto.common.predicate.Primitives;
import com.facebook.presto.common.type.Type;

import javax.annotation.Nullable;

import java.util.function.Supplier;

import static com.facebook.presto.common.type.TypeUtils.readNativeValue;
import static com.facebook.presto.common.type.TypeUtils.writeNativeValue;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class Utils
{
Expand All @@ -30,7 +36,7 @@ private Utils()
public static Block nativeValueToBlock(Type type, Object object)
{
if (object != null && !Primitives.wrap(type.getJavaType()).isInstance(object)) {
throw new IllegalArgumentException(String.format("Object '%s' does not match type %s", object, type.getJavaType()));
throw new IllegalArgumentException(format("Object '%s' does not match type %s", object, type.getJavaType()));
}
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
writeNativeValue(type, blockBuilder, object);
Expand All @@ -49,10 +55,68 @@ public static void checkArgument(boolean expression)
}
}

public static void checkArgument(boolean expression, String errorMessage)
public static void checkArgument(boolean expression, String message, Object... args)
{
if (!expression) {
throw new IllegalArgumentException(errorMessage);
throw new IllegalArgumentException(format(message, args));
}
}

/**
* Returns a supplier which caches the instance retrieved during the first call to {@code get()}
* and returns that value on subsequent calls to {@code get()}.
*/
public static <T> Supplier<T> memoizedSupplier(Supplier<T> delegate)
{
if (delegate instanceof MemoizingSupplier) {
return delegate;
}
return new MemoizingSupplier<>(delegate);
}

/**
* Vendored from Guava
*/
static class MemoizingSupplier<T>
implements Supplier<T>
{
volatile Supplier<T> delegate;
volatile boolean initialized;
// "value" does not need to be volatile; visibility piggy-backs
// on volatile read of "initialized".
@Nullable T value;

MemoizingSupplier(Supplier<T> delegate)
{
this.delegate = requireNonNull(delegate);
}

@Override
public T get()
{
// A 2-field variant of Double Checked Locking.
if (!initialized) {
synchronized (this) {
if (!initialized) {
T t = delegate.get();
value = t;
initialized = true;
// Release the delegate to GC.
delegate = null;
return t;
}
}
}
return value;
}

@Override
public String toString()
{
Supplier<T> delegate = this.delegate;
return "Suppliers.memoize("
+ (delegate == null ? "<supplier that returned " + value + ">" : delegate)
+ ")";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public Object getValue()
return Utils.blockToNativeValue(type, valueBlock.get());
}

public Optional<Object> getObjectValue()
{
return valueBlock.map(block -> Utils.blockToNativeValue(type, block));
}

public Object getPrintableValue(SqlFunctionProperties properties)
{
if (!valueBlock.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public boolean equals(Object obj)
Objects.equals(this.high, other.high);
}

@Override
public String toString()
{
return (low.getBound() == Marker.Bound.EXACTLY ? "[" : "(") +
low.getObjectValue().orElse(Double.NEGATIVE_INFINITY) +
".." +
high.getObjectValue().orElse(Double.POSITIVE_INFINITY) +
(high.getBound() == Marker.Bound.EXACTLY ? "]" : ")");
}

private void appendQuotedValue(StringBuilder buffer, Marker marker, SqlFunctionProperties properties)
{
buffer.append('"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ public Object getSingleValue()
return lowIndexedRanges.values().iterator().next().getSingleValue();
}

/**
* Build a new {@link SortedRangeSet} that contains ranges which lie within the argument range
*
* @param span the range which the new set should span
* @return a new range set
*/
public SortedRangeSet subRangeSet(Range span)
{
Builder builder = new Builder(type);

for (Range range : getOrderedRanges()) {
if (span.contains(range)) {
builder.add(range);
}
else if (span.overlaps(range)) {
builder.add(range.intersect(span));
}
}

return builder.build();
}

@Override
public boolean containsValue(Object value)
{
Expand Down
148 changes: 148 additions & 0 deletions presto-common/src/test/java/com/facebook/presto/common/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* 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.common;

import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.fail;

public class TestUtils
{
@Test
public void testCheckArgumentFailWithMessage()
{
try {
Utils.checkArgument(false, "test %s", "test");
fail();
}
catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "test test");
}
}

@Test
public void testCheckArgumentPassWithMessage()
{
try {
Utils.checkArgument(true, "test %s", "test");
}
catch (IllegalArgumentException e) {
fail();
}
}

@Test
public void testMemoizedSupplierThreadSafe()
throws Throwable
{
Function<Supplier<Boolean>, Supplier<Boolean>> memoizer =
supplier -> Utils.memoizedSupplier(supplier);
testSupplierThreadSafe(memoizer);
}

/**
* Vendored from Guava
*/
private void testSupplierThreadSafe(Function<Supplier<Boolean>, Supplier<Boolean>> memoizer)
throws Throwable
{
final AtomicInteger count = new AtomicInteger(0);
final AtomicReference<Throwable> thrown = new AtomicReference<>(null);
final int numThreads = 3;
final Thread[] threads = new Thread[numThreads];
final long timeout = TimeUnit.SECONDS.toNanos(60);

final Supplier<Boolean> supplier =
new Supplier<Boolean>()
{
boolean isWaiting(Thread thread)
{
switch (thread.getState()) {
case BLOCKED:
case WAITING:
case TIMED_WAITING:
return true;
default:
return false;
}
}

int waitingThreads()
{
int waitingThreads = 0;
for (Thread thread : threads) {
if (isWaiting(thread)) {
waitingThreads++;
}
}
return waitingThreads;
}

@Override
@SuppressWarnings("ThreadPriorityCheck") // doing our best to test for races
public Boolean get()
{
// Check that this method is called exactly once, by the first
// thread to synchronize.
long t0 = System.nanoTime();
while (waitingThreads() != numThreads - 1) {
if (System.nanoTime() - t0 > timeout) {
thrown.set(
new TimeoutException(
"timed out waiting for other threads to block"
+ " synchronizing on supplier"));
break;
}
Thread.yield();
}
count.getAndIncrement();
return Boolean.TRUE;
}
};

final Supplier<Boolean> memoizedSupplier = memoizer.apply(supplier);

for (int i = 0; i < numThreads; i++) {
threads[i] =
new Thread()
{
@Override
public void run()
{
assertSame(Boolean.TRUE, memoizedSupplier.get());
}
};
}
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}

if (thrown.get() != null) {
throw thrown.get();
}
assertEquals(1, count.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ public void testCanonicalize()
assertDifferentMarker(Marker.upperUnbounded(BIGINT), Marker.lowerUnbounded(BIGINT), true);
}

@Test
public void testGetValue()
{
assertTrue(Marker.exactly(BIGINT, 1L).getObjectValue().isPresent());
assertTrue(Marker.above(BIGINT, 1L).getObjectValue().isPresent());
assertTrue(Marker.below(BIGINT, 1L).getObjectValue().isPresent());
assertFalse(Marker.upperUnbounded(BIGINT).getObjectValue().isPresent());
assertFalse(Marker.lowerUnbounded(BIGINT).getObjectValue().isPresent());
}

private void assertSameMarker(Marker marker1, Marker marker2, boolean removeConstants)
throws Exception
{
Expand Down
Loading

0 comments on commit 472111e

Please sign in to comment.