Skip to content

Commit

Permalink
[core] Support creating Iceberg metadata based on old ones (#3739)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsreaper authored Aug 19, 2024
1 parent cf8ef5e commit 13bf95b
Show file tree
Hide file tree
Showing 18 changed files with 1,059 additions and 438 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.apache.paimon.iceberg;

import org.apache.paimon.io.DataFileMeta;
import org.apache.paimon.table.FileStoreTable;

/** {@link AbstractIcebergCommitCallback} for append only tables. */
public class AppendOnlyIcebergCommitCallback extends AbstractIcebergCommitCallback {

public AppendOnlyIcebergCommitCallback(FileStoreTable table, String commitUser) {
super(table, commitUser);
}

@Override
protected boolean shouldAddFileToIceberg(DataFileMeta meta) {
return true;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.apache.paimon.iceberg;

import org.apache.paimon.io.DataFileMeta;
import org.apache.paimon.table.FileStoreTable;

/** {@link AbstractIcebergCommitCallback} for primary key tables. */
public class PrimaryKeyIcebergCommitCallback extends AbstractIcebergCommitCallback {

public PrimaryKeyIcebergCommitCallback(FileStoreTable table, String commitUser) {
super(table, commitUser);
}

@Override
protected boolean shouldAddFileToIceberg(DataFileMeta meta) {
int maxLevel = table.coreOptions().numLevels() - 1;
return meta.level() == maxLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

package org.apache.paimon.iceberg.manifest;

import org.apache.paimon.data.BinaryString;
import org.apache.paimon.data.Decimal;
import org.apache.paimon.types.DataType;
import org.apache.paimon.types.DecimalType;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;

Expand All @@ -40,6 +43,8 @@ private IcebergConversions() {}

private static final ThreadLocal<CharsetEncoder> ENCODER =
ThreadLocal.withInitial(StandardCharsets.UTF_8::newEncoder);
private static final ThreadLocal<CharsetDecoder> DECODER =
ThreadLocal.withInitial(StandardCharsets.UTF_8::newDecoder);

public static ByteBuffer toByteBuffer(DataType type, Object value) {
switch (type.getTypeRoot()) {
Expand Down Expand Up @@ -78,4 +83,37 @@ public static ByteBuffer toByteBuffer(DataType type, Object value) {
throw new UnsupportedOperationException("Cannot serialize type: " + type);
}
}

public static Object toPaimonObject(DataType type, byte[] bytes) {
switch (type.getTypeRoot()) {
case BOOLEAN:
return bytes[0] != 0;
case INTEGER:
case DATE:
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
case BIGINT:
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getLong();
case FLOAT:
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();
case DOUBLE:
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getDouble();
case CHAR:
case VARCHAR:
try {
return BinaryString.fromString(
DECODER.get().decode(ByteBuffer.wrap(bytes)).toString());
} catch (CharacterCodingException e) {
throw new RuntimeException("Failed to decode bytes as UTF-8", e);
}
case BINARY:
case VARBINARY:
return bytes;
case DECIMAL:
DecimalType decimalType = (DecimalType) type;
return Decimal.fromUnscaledBytes(
bytes, decimalType.getPrecision(), decimalType.getScale());
default:
throw new UnsupportedOperationException("Cannot deserialize type: " + type);
}
}
}
Loading

0 comments on commit 13bf95b

Please sign in to comment.