|
| 1 | +/* |
| 2 | + * Copyright (2025) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.delta.kernel.internal.snapshot |
| 18 | + |
| 19 | +import java.util.Arrays |
| 20 | +import java.util.{Collections, Optional} |
| 21 | + |
| 22 | +import io.delta.kernel.internal.fs.Path |
| 23 | +import io.delta.kernel.internal.util.FileNames |
| 24 | +import io.delta.kernel.utils.FileStatus |
| 25 | +import org.scalatest.funsuite.AnyFunSuite |
| 26 | + |
| 27 | +class LogSegmentSuite extends AnyFunSuite { |
| 28 | + private val logPath = new Path("/a/_delta_log") |
| 29 | + private val checkpointFs10 = |
| 30 | + FileStatus.of(FileNames.checkpointFileSingular(logPath, 10).toString, 1, 1) |
| 31 | + private val checkpointFs10List = Collections.singletonList(checkpointFs10) |
| 32 | + private val deltaFs11 = FileStatus.of(FileNames.deltaFile(logPath, 11), 1, 1) |
| 33 | + private val deltaFs11List = Collections.singletonList(deltaFs11) |
| 34 | + private val deltaFs12 = FileStatus.of(FileNames.deltaFile(logPath, 12), 1, 1) |
| 35 | + private val deltaFs12List = Collections.singletonList(deltaFs12) |
| 36 | + private val deltasFs11To12List = Arrays.asList(deltaFs11, deltaFs12) |
| 37 | + private val badJsonsList = Collections.singletonList( |
| 38 | + FileStatus.of(s"${logPath.toString}/gibberish.json", 1, 1)) |
| 39 | + private val badCheckpointsList = Collections.singletonList( |
| 40 | + FileStatus.of(s"${logPath.toString}/gibberish.checkpoint.parquet", 1, 1)) |
| 41 | + |
| 42 | + test("constructor -- valid case (empty)") { |
| 43 | + LogSegment.empty(new Path("/a/_delta_log")) |
| 44 | + } |
| 45 | + |
| 46 | + test("constructor -- valid case (non-empty)") { |
| 47 | + val logPath = new Path("/a/_delta_log") |
| 48 | + new LogSegment(logPath, 12, deltasFs11To12List, checkpointFs10List, 1) |
| 49 | + } |
| 50 | + |
| 51 | + test("constructor -- null arguments => throw") { |
| 52 | + // logPath is null |
| 53 | + intercept[NullPointerException] { |
| 54 | + new LogSegment( |
| 55 | + null, 1, Collections.emptyList(), Collections.emptyList(), -1) |
| 56 | + } |
| 57 | + // deltas is null |
| 58 | + intercept[NullPointerException] { |
| 59 | + new LogSegment( |
| 60 | + new Path("/a/_delta_log"), 1, null, Collections.emptyList(), -1) |
| 61 | + } |
| 62 | + // checkpoints is null |
| 63 | + intercept[NullPointerException] { |
| 64 | + new LogSegment( |
| 65 | + new Path("/a/_delta_log"), 1, Collections.emptyList(), null, -1) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + test("constructor -- all deltas must be actual delta files") { |
| 70 | + val exMsg = intercept[IllegalArgumentException] { |
| 71 | + new LogSegment( |
| 72 | + logPath, 12, badJsonsList, checkpointFs10List, 1) |
| 73 | + }.getMessage |
| 74 | + assert(exMsg === "deltas must all be actual delta (commit) files") |
| 75 | + } |
| 76 | + |
| 77 | + test("constructor -- all checkpoints must be actual checkpoint files") { |
| 78 | + val exMsg = intercept[IllegalArgumentException] { |
| 79 | + new LogSegment( |
| 80 | + logPath, 12, deltasFs11To12List, badCheckpointsList, 1) |
| 81 | + }.getMessage |
| 82 | + assert(exMsg === "checkpoints must all be actual checkpoint files") |
| 83 | + } |
| 84 | + |
| 85 | + test("constructor -- if version >= 0 then both deltas and checkpoints cannot be empty") { |
| 86 | + val exMsg = intercept[IllegalArgumentException] { |
| 87 | + new LogSegment( |
| 88 | + logPath, 12, Collections.emptyList(), Collections.emptyList(), 1) |
| 89 | + }.getMessage |
| 90 | + assert(exMsg === "No files to read") |
| 91 | + } |
| 92 | + |
| 93 | + test("constructor -- if deltas non-empty then first delta must equal checkpointVersion + 1") { |
| 94 | + val exMsg = intercept[IllegalArgumentException] { |
| 95 | + new LogSegment( |
| 96 | + logPath, 12, deltaFs12List, checkpointFs10List, 1) |
| 97 | + }.getMessage |
| 98 | + assert(exMsg === "First delta file version must equal checkpointVersion + 1") |
| 99 | + } |
| 100 | + |
| 101 | + test("constructor -- if deltas non-empty then last delta must equal version") { |
| 102 | + val exMsg = intercept[IllegalArgumentException] { |
| 103 | + new LogSegment( |
| 104 | + logPath, 12, deltaFs11List, checkpointFs10List, 1) |
| 105 | + }.getMessage |
| 106 | + assert(exMsg === "Last delta file version must equal the version of this LogSegment") |
| 107 | + } |
| 108 | + |
| 109 | + test("constructor -- if no deltas then checkpointVersion must equal version") { |
| 110 | + val exMsg = intercept[IllegalArgumentException] { |
| 111 | + new LogSegment( |
| 112 | + logPath, 11, Collections.emptyList(), checkpointFs10List, 1) |
| 113 | + }.getMessage |
| 114 | + assert(exMsg === |
| 115 | + "If there are no deltas, then checkpointVersion must equal the version of this LogSegment") |
| 116 | + } |
| 117 | +} |
0 commit comments