Skip to content

Commit

Permalink
fix partition table with partition column name date (#1892)
Browse files Browse the repository at this point in the history
  • Loading branch information
marsishandsome authored Jan 29, 2021
1 parent 7bbabce commit edfea9b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 14 additions & 0 deletions core/src/test/scala/org/apache/spark/sql/IssueTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import org.apache.spark.sql.functions.{col, sum}

class IssueTestSuite extends BaseTiSparkTest {

test("partition table with date partition column name") {
tidbStmt.execute("drop table if exists t1")
tidbStmt.execute("""
|CREATE TABLE t1 (
|date date NOT NULL,
|cost bigint(20) DEFAULT NULL)
|PARTITION BY RANGE COLUMNS(date) (
|PARTITION p20200404 VALUES LESS THAN ("2020-04-05"),
|PARTITION p20200418 VALUES LESS THAN ("2020-04-19")
|)
|""".stripMargin)
spark.sql("select `date`, sum(cost) from t1 where group by `date`").show()
}

test("count(a, b) with null values") {
tidbStmt.execute("drop table if exists t1")
tidbStmt.execute("create table t1(a int, b int)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,27 @@ static void generateRangeExprs(
if (current.equals("MAXVALUE")) {
leftHand = "true";
} else {
leftHand = String.format("%s < %s", partExprStr, current);
leftHand = String.format("%s < %s", wrapColumnName(partExprStr), current);
}
if (i == 0) {
partExprs.add(parser.parseExpression(leftHand));
} else {
String previous = partInfo.getDefs().get(i - 1).getLessThan().get(lessThanIdx);
String and = String.format("%s and %s", partExprStr + ">=" + previous, leftHand);
String and =
String.format("%s >= %s and %s", wrapColumnName(partExprStr), previous, leftHand);
partExprs.add(parser.parseExpression(and));
}
}
}

private static String wrapColumnName(String columnName) {
if (columnName.startsWith("`") && columnName.endsWith("`")) {
return columnName;
} else if (columnName.contains("(") && columnName.contains(")")) {
// function not column name, e.g. year(columnName)
return columnName;
} else {
return String.format("`%s`", columnName);
}
}
}

0 comments on commit edfea9b

Please sign in to comment.