
org.dinky.shaded.paimon.table.system.AllTableOptionsTable Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.dinky.shaded.paimon.table.system;
import org.dinky.shaded.paimon.data.BinaryString;
import org.dinky.shaded.paimon.data.GenericRow;
import org.dinky.shaded.paimon.data.InternalRow;
import org.dinky.shaded.paimon.disk.IOManager;
import org.dinky.shaded.paimon.fs.FileIO;
import org.dinky.shaded.paimon.fs.Path;
import org.dinky.shaded.paimon.predicate.Predicate;
import org.dinky.shaded.paimon.reader.RecordReader;
import org.dinky.shaded.paimon.schema.SchemaManager;
import org.dinky.shaded.paimon.table.ReadonlyTable;
import org.dinky.shaded.paimon.table.Table;
import org.dinky.shaded.paimon.table.source.InnerTableRead;
import org.dinky.shaded.paimon.table.source.InnerTableScan;
import org.dinky.shaded.paimon.table.source.ReadOnceTableScan;
import org.dinky.shaded.paimon.table.source.Split;
import org.dinky.shaded.paimon.table.source.TableRead;
import org.dinky.shaded.paimon.types.DataField;
import org.dinky.shaded.paimon.types.RowType;
import org.dinky.shaded.paimon.types.VarCharType;
import org.dinky.shaded.paimon.utils.IteratorRecordReader;
import org.dinky.shaded.paimon.utils.ProjectedRow;
import org.dinky.shaded.paimon.shade.guava30.com.google.common.collect.Iterators;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* This is a system table to display all the database-table properties.
*
*
* For example:
* If we select * from sys.all_table_options, we will get
* databasename tablename key value
* default test0 a b
* my_db test1 c d
* ... ... ... ...
* We can write sql to fetch the information we need.
*
*/
public class AllTableOptionsTable implements ReadonlyTable {
public static final String ALL_TABLE_OPTIONS = "all_table_options";
private final FileIO fileIO;
private final Map> allTablePaths;
public AllTableOptionsTable(FileIO fileIO, Map> allTablePaths) {
// allTablePath is the map of >
this.fileIO = fileIO;
this.allTablePaths = allTablePaths;
}
@Override
public String name() {
return ALL_TABLE_OPTIONS;
}
@Override
public RowType rowType() {
List fields = new ArrayList<>();
fields.add(new DataField(0, "database_name", new VarCharType(VarCharType.MAX_LENGTH)));
fields.add(new DataField(1, "table_name", new VarCharType(VarCharType.MAX_LENGTH)));
fields.add(new DataField(2, "key", new VarCharType(VarCharType.MAX_LENGTH)));
fields.add(new DataField(3, "value", new VarCharType(VarCharType.MAX_LENGTH)));
return new RowType(fields);
}
@Override
public List primaryKeys() {
return Collections.singletonList("table_name");
}
@Override
public InnerTableScan newScan() {
return new AllTableOptionsScan();
}
@Override
public InnerTableRead newRead() {
return new AllTableOptionsRead(fileIO);
}
@Override
public Table copy(Map dynamicOptions) {
return new AllTableOptionsTable(fileIO, allTablePaths);
}
private class AllTableOptionsScan extends ReadOnceTableScan {
@Override
public InnerTableScan withFilter(Predicate predicate) {
return this;
}
@Override
public Plan innerPlan() {
return () -> Collections.singletonList(new AllTableSplit(fileIO, allTablePaths));
}
}
private static class AllTableSplit implements Split {
private static final long serialVersionUID = 1L;
private final FileIO fileIO;
private final Map> allTablePaths;
private AllTableSplit(FileIO fileIO, Map> allTablePaths) {
this.fileIO = fileIO;
this.allTablePaths = allTablePaths;
}
@Override
public long rowCount() {
return options(fileIO, allTablePaths).values().stream()
.flatMap(t -> t.values().stream())
.reduce(0, (a, b) -> a + b.size(), Integer::sum);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AllTableSplit that = (AllTableSplit) o;
return Objects.equals(allTablePaths, that.allTablePaths);
}
@Override
public int hashCode() {
return Objects.hash(allTablePaths);
}
}
private static class AllTableOptionsRead implements InnerTableRead {
private final FileIO fileIO;
private int[][] projection;
public AllTableOptionsRead(FileIO fileIO) {
this.fileIO = fileIO;
}
@Override
public InnerTableRead withFilter(Predicate predicate) {
return this;
}
@Override
public InnerTableRead withProjection(int[][] projection) {
this.projection = projection;
return this;
}
@Override
public TableRead withIOManager(IOManager ioManager) {
return this;
}
@Override
public RecordReader createReader(Split split) throws IOException {
if (!(split instanceof AllTableSplit)) {
throw new IllegalArgumentException("Unsupported split: " + split.getClass());
}
Map> location = ((AllTableSplit) split).allTablePaths;
Iterator rows = toRow(options(fileIO, location));
if (projection != null) {
rows =
Iterators.transform(
rows, row -> ProjectedRow.from(projection).replaceRow(row));
}
return new IteratorRecordReader<>(rows);
}
private Iterator toRow(Map>> option) {
List rows = new ArrayList<>();
for (Map.Entry>> entry0 : option.entrySet()) {
String database = entry0.getKey();
for (Map.Entry> entry1 : entry0.getValue().entrySet()) {
String tableName = entry1.getKey();
for (Map.Entry entry2 : entry1.getValue().entrySet()) {
String key = entry2.getKey();
String value = entry2.getValue();
rows.add(
GenericRow.of(
BinaryString.fromString(database),
BinaryString.fromString(tableName),
BinaryString.fromString(key),
BinaryString.fromString(value)));
}
}
}
return rows.iterator();
}
}
private static Map>> options(
FileIO fileIO, Map> allTablePaths) {
Map>> allOptions = new HashMap<>();
for (Map.Entry> entry0 : allTablePaths.entrySet()) {
Map> m0 =
allOptions.computeIfAbsent(entry0.getKey(), k -> new HashMap<>());
for (Map.Entry entry1 : entry0.getValue().entrySet()) {
Map options =
new SchemaManager(fileIO, entry1.getValue())
.latest()
.orElseThrow(() -> new RuntimeException("Table not exists."))
.options();
m0.put(entry1.getKey(), options);
}
}
return allOptions;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy