|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.carbondata.spark.testsuite.deletesegment |
| 18 | + |
| 19 | +import org.apache.spark.sql.test.util.QueryTest |
| 20 | +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} |
| 21 | + |
| 22 | +import org.apache.carbondata.core.constants.CarbonCommonConstants |
| 23 | +import org.apache.carbondata.core.util.CarbonProperties |
| 24 | + |
| 25 | +/** |
| 26 | + * test class for testing the delete segment expect remaining number. |
| 27 | + */ |
| 28 | +class DeleteSegmentByRemainNumberTestCase extends QueryTest with BeforeAndAfterAll with BeforeAndAfterEach { |
| 29 | + val DELETED_STATUS = "Marked for Delete" |
| 30 | + |
| 31 | + val SUCCESSFUL_STATUS = "Success" |
| 32 | + |
| 33 | + override def beforeAll { |
| 34 | + CarbonProperties.getInstance() |
| 35 | + .addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT, "dd-MM-yyyy") |
| 36 | + .addProperty(CarbonCommonConstants.CARBON_DATE_FORMAT, "dd-MM-yyyy") |
| 37 | + } |
| 38 | + |
| 39 | + override def beforeEach(): Unit = { |
| 40 | + initTestTable |
| 41 | + } |
| 42 | + |
| 43 | + test("delete segment, remain_number = 1") { |
| 44 | + sql("delete from table deleteSegmentTable expect segment.remain_number = 1") |
| 45 | + val rows = sql("show segments on deleteSegmentTable").collect() |
| 46 | + assertResult(SUCCESSFUL_STATUS)(rows(0).get(1)) |
| 47 | + assertResult(DELETED_STATUS)(rows(1).get(1)) |
| 48 | + assertResult(DELETED_STATUS)(rows(2).get(1)) |
| 49 | + } |
| 50 | + |
| 51 | + test("delete segment, remain nothing") { |
| 52 | + sql("delete from table deleteSegmentTable expect segment.remain_number = 0") |
| 53 | + val rows = sql("show segments on deleteSegmentTable").collect() |
| 54 | + rows.foreach(row => assertResult(DELETED_STATUS)(row.get(1))) |
| 55 | + } |
| 56 | + |
| 57 | + test("delete segment, remain all") { |
| 58 | + sql("delete from table deleteSegmentTable expect segment.remain_number = 3") |
| 59 | + val rows = sql("show segments on deleteSegmentTable").collect() |
| 60 | + rows.foreach(row => assertResult(SUCCESSFUL_STATUS)(row.get(1))) |
| 61 | + } |
| 62 | + |
| 63 | + test("delete segment, remain_number = -1") { |
| 64 | + val ex = intercept[Exception] { |
| 65 | + sql("delete from table deleteSegmentTable expect segment.remain_number = -1") |
| 66 | + } |
| 67 | + assert(ex.getMessage.contains("not found in database")) |
| 68 | + } |
| 69 | + |
| 70 | + test("delete segment after update") { |
| 71 | + sql("update deleteSegmentTable d set (d.country) = ('fr') where d.country = 'aus'") |
| 72 | + sql("delete from table deleteSegmentTable expect segment.remain_number = 1") |
| 73 | + val rows = sql("select * from deleteSegmentTable").collect() |
| 74 | + rows.foreach(row => assertResult("fr")(row(2))) |
| 75 | + } |
| 76 | + |
| 77 | + test("delete segment after delete newest segment by segmentId") { |
| 78 | + sql("delete from table deleteSegmentTable where segment.id in (2)") |
| 79 | + sql("delete from table deleteSegmentTable expect segment.remain_number = 1") |
| 80 | + val rows = sql("show segments on deleteSegmentTable").collect() |
| 81 | + assertResult(DELETED_STATUS)(rows(0).get(1)) |
| 82 | + assertResult(SUCCESSFUL_STATUS)(rows(1).get(1)) |
| 83 | + assertResult(DELETED_STATUS)(rows(2).get(1)) |
| 84 | + } |
| 85 | + |
| 86 | + private def initTestTable = { |
| 87 | + sql("drop table if exists deleteSegmentTable") |
| 88 | + sql( |
| 89 | + "CREATE table deleteSegmentTable (ID int, date String, country String, name " + |
| 90 | + "String, phonetype String, serialname String, salary String) STORED AS carbondata" |
| 91 | + ) |
| 92 | + sql( |
| 93 | + s"""LOAD DATA local inpath '$resourcesPath/dataretention1.csv' |
| 94 | + | INTO TABLE deleteSegmentTable OPTIONS('DELIMITER'= ',', 'QUOTECHAR'= '"')""".stripMargin) |
| 95 | + sql( |
| 96 | + s"""LOAD DATA local inpath '$resourcesPath/dataretention2.csv' |
| 97 | + | INTO TABLE deleteSegmentTable OPTIONS('DELIMITER'= ',', 'QUOTECHAR'= '"')""".stripMargin) |
| 98 | + sql( |
| 99 | + s"""LOAD DATA local inpath '$resourcesPath/dataretention3.csv' |
| 100 | + | INTO TABLE deleteSegmentTable OPTIONS('DELIMITER'= ',', 'QUOTECHAR'= '"')""".stripMargin) |
| 101 | + } |
| 102 | +} |
0 commit comments