From 2efa5984b889916642c68782610d8ba504e45199 Mon Sep 17 00:00:00 2001 From: alberic Date: Thu, 17 Nov 2022 23:27:06 +0800 Subject: [PATCH] Support hoodie table for test, add hoodie_nation table --- src/main/java/io/trino/tpch/HudiNation.java | 75 ++++++++++++ .../java/io/trino/tpch/HudiNationColumn.java | 112 ++++++++++++++++++ .../io/trino/tpch/HudiNationGenerator.java | 89 ++++++++++++++ src/main/java/io/trino/tpch/TpchTable.java | 14 ++- 4 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/trino/tpch/HudiNation.java create mode 100644 src/main/java/io/trino/tpch/HudiNationColumn.java create mode 100644 src/main/java/io/trino/tpch/HudiNationGenerator.java diff --git a/src/main/java/io/trino/tpch/HudiNation.java b/src/main/java/io/trino/tpch/HudiNation.java new file mode 100644 index 0000000..5916f1b --- /dev/null +++ b/src/main/java/io/trino/tpch/HudiNation.java @@ -0,0 +1,75 @@ +/* + * 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 io.trino.tpch; + +import static java.util.Locale.ENGLISH; +import static java.util.Objects.requireNonNull; + +public class HudiNation + implements TpchEntity +{ + private final long rowNumber; + private final long nationKey; + private final String name; + private final long regionKey; + private final String comment; + private final String hoodieCommitTime; + + public HudiNation(long rowNumber, long nationKey, String name, long regionKey, String comment, String hoodieCommitTime) + { + this.rowNumber = rowNumber; + this.nationKey = nationKey; + this.name = requireNonNull(name, "name is null"); + this.regionKey = regionKey; + this.comment = requireNonNull(comment, "comment is null"); + this.hoodieCommitTime = requireNonNull(hoodieCommitTime, "hoodieCommitTime is null"); + } + + @Override + public long getRowNumber() + { + return rowNumber; + } + + public long getNationKey() + { + return nationKey; + } + + public String getName() + { + return name; + } + + public long getRegionKey() + { + return regionKey; + } + + public String getComment() + { + return comment; + } + + public String getHoodieCommitTime() + { + return hoodieCommitTime; + } + + @Override + public String toLine() + { + return String.format(ENGLISH, "%d|%s|%d|%s|%s|", nationKey, name, regionKey, comment, hoodieCommitTime); + } +} diff --git a/src/main/java/io/trino/tpch/HudiNationColumn.java b/src/main/java/io/trino/tpch/HudiNationColumn.java new file mode 100644 index 0000000..dba3329 --- /dev/null +++ b/src/main/java/io/trino/tpch/HudiNationColumn.java @@ -0,0 +1,112 @@ +/* + * 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 io.trino.tpch; + +import static io.trino.tpch.TpchColumnTypes.IDENTIFIER; +import static io.trino.tpch.TpchColumnTypes.varchar; + +public enum HudiNationColumn + implements TpchColumn +{ + NATION_KEY("n_nationkey", IDENTIFIER) { + @Override + public long getIdentifier(HudiNation nation) + { + return nation.getNationKey(); + } + }, + + NAME("n_name", varchar(25)) { + @Override + public String getString(HudiNation nation) + { + return nation.getName(); + } + }, + + REGION_KEY("n_regionkey", IDENTIFIER) { + @Override + public long getIdentifier(HudiNation nation) + { + return nation.getRegionKey(); + } + }, + + COMMENT("n_comment", varchar(152)) { + @Override + public String getString(HudiNation nation) + { + return nation.getComment(); + } + }, + + _HUDI_NATION_COLUMN("n__hoodie_commit_time", varchar(17)){ + @Override + public String getString(HudiNation nation) + { + return nation.getHoodieCommitTime(); + } + }; + + private final String columnName; + private final TpchColumnType type; + + HudiNationColumn(String columnName, TpchColumnType type) + { + this.columnName = columnName; + this.type = type; + } + + @Override + public String getColumnName() + { + return columnName; + } + + @Override + public TpchColumnType getType() + { + return type; + } + + @Override + public double getDouble(HudiNation nation) + { + throw new UnsupportedOperationException(); + } + + @Override + public long getIdentifier(HudiNation nation) + { + throw new UnsupportedOperationException(); + } + + @Override + public int getInteger(HudiNation nation) + { + throw new UnsupportedOperationException(); + } + + @Override + public String getString(HudiNation nation) + { + throw new UnsupportedOperationException(); + } + + @Override + public int getDate(HudiNation entity) + { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/io/trino/tpch/HudiNationGenerator.java b/src/main/java/io/trino/tpch/HudiNationGenerator.java new file mode 100644 index 0000000..2cb18a0 --- /dev/null +++ b/src/main/java/io/trino/tpch/HudiNationGenerator.java @@ -0,0 +1,89 @@ +/* + * 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 io.trino.tpch; + +import com.google.common.collect.AbstractIterator; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Iterator; + +import static java.util.Objects.requireNonNull; + +public class HudiNationGenerator + implements Iterable +{ + private static final int COMMENT_AVERAGE_LENGTH = 72; + + private final Distributions distributions; + private final TextPool textPool; + private static final String HOODIE_INSTANT_TIME_FORMAT = "yyyyMMddHHmmssSSS"; + + public HudiNationGenerator() + { + this(Distributions.getDefaultDistributions(), TextPool.getDefaultTestPool()); + } + + public HudiNationGenerator(Distributions distributions, TextPool textPool) + { + this.distributions = requireNonNull(distributions, "distributions is null"); + this.textPool = requireNonNull(textPool, "textPool is null"); + } + + @Override + public Iterator iterator() + { + SimpleDateFormat instantTimeFormat = new SimpleDateFormat(HOODIE_INSTANT_TIME_FORMAT); + Date timestamp = new Date(); + String hoodieInstantTime = instantTimeFormat.format(timestamp); + return new HudiNationGeneratorIterator(distributions.getNations(), textPool, hoodieInstantTime); + } + + private static class HudiNationGeneratorIterator + extends AbstractIterator + { + private final Distribution nations; + private final RandomText commentRandom; + + private final String hoodieInstantTime; + + private int index; + + private HudiNationGeneratorIterator(Distribution nations, TextPool textPool, String hoodieInstantTime) + { + this.nations = nations; + this.commentRandom = new RandomText(606179079, textPool, COMMENT_AVERAGE_LENGTH); + this.hoodieInstantTime = hoodieInstantTime; + } + + @Override + protected HudiNation computeNext() + { + if (index >= nations.size()) { + return endOfData(); + } + HudiNation nation = new HudiNation(index, + index, + nations.getValue(index), + nations.getWeight(index), + commentRandom.nextValue(), + hoodieInstantTime); + + commentRandom.rowFinished(); + index++; + + return nation; + } + } +} diff --git a/src/main/java/io/trino/tpch/TpchTable.java b/src/main/java/io/trino/tpch/TpchTable.java index 1a07093..15e4d29 100644 --- a/src/main/java/io/trino/tpch/TpchTable.java +++ b/src/main/java/io/trino/tpch/TpchTable.java @@ -89,6 +89,18 @@ public Iterable createGenerator(double scaleFactor, int part, int partCo } }; + public static final TpchTable HUDINATION = new TpchTable("hoodie_nation", HudiNationColumn.values()) + { + @Override + public Iterable createGenerator(double scaleFactor, int part, int partCount) + { + if (part != 1) { + return ImmutableList.of(); + } + return new HudiNationGenerator(); + } + }; + public static final TpchTable REGION = new TpchTable("region", RegionColumn.values()) { @Override @@ -105,7 +117,7 @@ public Iterable createGenerator(double scaleFactor, int part, int partCo private static final Map> TABLES_BY_NAME; static { - TABLES = ImmutableList.of(CUSTOMER, ORDERS, LINE_ITEM, PART, PART_SUPPLIER, SUPPLIER, NATION, REGION); + TABLES = ImmutableList.of(CUSTOMER, ORDERS, LINE_ITEM, PART, PART_SUPPLIER, SUPPLIER, NATION, REGION, HUDINATION); TABLES_BY_NAME = Maps.uniqueIndex(TABLES, TpchTable::getTableName); }