Skip to content

Commit

Permalink
DENG-2598: adding support for select * except
Browse files Browse the repository at this point in the history
  • Loading branch information
kkalanda-score committed May 28, 2024
1 parent e14d90e commit fd6c611
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.hudi.common.util.ConfigUtils.getStringWithAltKeys;

Expand Down Expand Up @@ -59,6 +61,24 @@ public Dataset<Row> apply(JavaSparkContext jsc, SparkSession sparkSession, Datas
return rowDataset;
}

// Extract except clause into formattedColumns if found
Pattern pattern = Pattern.compile("(?i)(.*\\*) except\\(([^)]*)\\)(.*)");
Matcher matcher = pattern.matcher(transformerSQL);
String[] formattedColumns = {};
boolean dropColumns = false;
if (matcher.find()) {
String columnString = matcher.group(2);
String[] columns = columnString.split(",", 0);
formattedColumns = new String[columns.length];
for (int i = 0; i < columns.length; i++) {
formattedColumns[i] = columns[i].trim();
}
LOG.info("Found 'except' clause in SQL query transform for columns: " + String.join(", ", formattedColumns));
dropColumns = true;
transformerSQL = matcher.group(1) + matcher.group(3);
LOG.info("Generated new SQL query transform: " + transformerSQL);
}

try {
// tmp table name doesn't like dashes
String tmpTable = TMP_TABLE.concat(UUID.randomUUID().toString().replace("-", "_"));
Expand All @@ -68,6 +88,12 @@ public Dataset<Row> apply(JavaSparkContext jsc, SparkSession sparkSession, Datas
LOG.debug("SQL Query for transformation : (" + sqlStr + ")");
Dataset<Row> transformed = sparkSession.sql(sqlStr);
sparkSession.catalog().dropTempView(tmpTable);
if (dropColumns) {
LOG.info("Dropping columns: " + String.join(", ", formattedColumns));
for (String column : formattedColumns) {
transformed = transformed.drop(column);
}
}
return transformed;
} catch (Exception e) {
throw new HoodieTransformExecutionException("Failed to apply sql query based transformer", e);
Expand Down

0 comments on commit fd6c611

Please sign in to comment.