|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.iceberg; |
| 15 | + |
| 16 | +import com.facebook.presto.common.CatalogSchemaName; |
| 17 | +import com.facebook.presto.iceberg.function.IcebergBucketFunction; |
| 18 | +import com.facebook.presto.metadata.FunctionExtractor; |
| 19 | +import com.facebook.presto.operator.scalar.AbstractTestFunctions; |
| 20 | +import com.facebook.presto.sql.analyzer.FeaturesConfig; |
| 21 | +import com.facebook.presto.sql.analyzer.FunctionsConfig; |
| 22 | +import com.facebook.presto.type.DateOperators; |
| 23 | +import com.facebook.presto.type.TimestampOperators; |
| 24 | +import com.facebook.presto.type.TimestampWithTimeZoneOperators; |
| 25 | +import org.testcontainers.shaded.com.google.common.collect.ImmutableList; |
| 26 | +import org.testng.annotations.BeforeClass; |
| 27 | +import org.testng.annotations.Test; |
| 28 | + |
| 29 | +import java.math.BigDecimal; |
| 30 | + |
| 31 | +import static com.facebook.presto.SessionTestUtils.TEST_SESSION; |
| 32 | +import static com.facebook.presto.common.type.BigintType.BIGINT; |
| 33 | +import static com.facebook.presto.common.type.Decimals.encodeScaledValue; |
| 34 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.Bucket.bucketLongDecimal; |
| 35 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.Bucket.bucketShortDecimal; |
| 36 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.bucketDate; |
| 37 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.bucketInteger; |
| 38 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.bucketTimestamp; |
| 39 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.bucketTimestampWithTimeZone; |
| 40 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.bucketVarbinary; |
| 41 | +import static com.facebook.presto.iceberg.function.IcebergBucketFunction.bucketVarchar; |
| 42 | +import static io.airlift.slice.Slices.utf8Slice; |
| 43 | + |
| 44 | +public class TestIcebergScalarFunctions |
| 45 | + extends AbstractTestFunctions |
| 46 | +{ |
| 47 | + public TestIcebergScalarFunctions() |
| 48 | + { |
| 49 | + super(TEST_SESSION, new FeaturesConfig(), new FunctionsConfig(), false); |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public void registerFunction() |
| 54 | + { |
| 55 | + ImmutableList.Builder<Class<?>> functions = ImmutableList.builder(); |
| 56 | + functions.add(IcebergBucketFunction.class) |
| 57 | + .add(IcebergBucketFunction.Bucket.class); |
| 58 | + functionAssertions.addConnectorFunctions(FunctionExtractor.extractFunctions(functions.build(), |
| 59 | + new CatalogSchemaName("iceberg", "system")), "iceberg"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testBucketFunction() |
| 64 | + { |
| 65 | + String catalogSchema = "iceberg.system"; |
| 66 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast(10 as tinyint), 3)", BIGINT, bucketInteger(10, 3)); |
| 67 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast(1950 as smallint), 4)", BIGINT, bucketInteger(1950, 4)); |
| 68 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast(2375645 as int), 5)", BIGINT, bucketInteger(2375645, 5)); |
| 69 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast(2779099983928392323 as bigint), 6)", BIGINT, bucketInteger(2779099983928392323L, 6)); |
| 70 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast(456.43 as DECIMAL(5,2)), 12)", BIGINT, bucketShortDecimal(5, 2, 45643, 12)); |
| 71 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast('12345678901234567890.1234567890' as DECIMAL(30,10)), 12)", BIGINT, bucketLongDecimal(30, 10, encodeScaledValue(new BigDecimal("12345678901234567890.1234567890")), 12)); |
| 72 | + |
| 73 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast('nasdbsdnsdms' as varchar), 7)", BIGINT, bucketVarchar(utf8Slice("nasdbsdnsdms"), 7)); |
| 74 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast('nasdbsdnsdms' as varbinary), 8)", BIGINT, bucketVarbinary(utf8Slice("nasdbsdnsdms"), 8)); |
| 75 | + |
| 76 | + functionAssertions.assertFunction(catalogSchema + ".bucket(cast('2018-04-06' as date), 9)", BIGINT, bucketDate(DateOperators.castFromSlice(utf8Slice("2018-04-06")), 9)); |
| 77 | + functionAssertions.assertFunction(catalogSchema + ".bucket(CAST('2018-04-06 04:35:00.000' AS TIMESTAMP),10)", BIGINT, bucketTimestamp(TimestampOperators.castFromSlice(TEST_SESSION.getSqlFunctionProperties(), utf8Slice("2018-04-06 04:35:00.000")), 10)); |
| 78 | + functionAssertions.assertFunction(catalogSchema + ".bucket(CAST('2018-04-06 04:35:00.000 GMT' AS TIMESTAMP WITH TIME ZONE), 11)", BIGINT, bucketTimestampWithTimeZone(TimestampWithTimeZoneOperators.castFromSlice(TEST_SESSION.getSqlFunctionProperties(), utf8Slice("2018-04-06 04:35:00.000 GMT")), 11)); |
| 79 | + } |
| 80 | +} |
0 commit comments