Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relational directives #641

Open
wants to merge 56 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
a161c7c
testing
saimukkamala May 19, 2023
e1fbdd5
poc for running on spark sql
saimukkamala May 19, 2023
0efb2d3
add RelationalDirective interface to system directive registry
saimukkamala May 22, 2023
e6603da
fixing bug
saimukkamala May 24, 2023
400892a
fixing registry nullPointer exception
saimukkamala May 24, 2023
b4dd875
move relation execution into directive
saimukkamala May 30, 2023
aa99ccd
Case transform directives
shrverma Jun 19, 2023
6a736c9
Implement trim space directives
shrverma Jun 19, 2023
9e492e3
Implement rename directive
shrverma Jun 19, 2023
ddf5f7a
implement keep and copy directives
shrverma Jun 20, 2023
1e0eda0
Implement merge directive
shrverma Jun 20, 2023
2cdc2b3
Undo changes in Lower.execute
shrverma Jun 20, 2023
cd4c680
Implement set-type directive
shrverma Jun 24, 2023
b25accf
Add utility class for set-type sql expression
shrverma Jun 24, 2023
2bb3fe2
Implement ChangeColumnCase Directive
shrverma Jun 25, 2023
f7c5bd3
Move getExpressionfactory() to the Directive interface
shrverma Jun 25, 2023
6d4093b
Move generateColumnExpMap to Directive.java
shrverma Jun 27, 2023
376e313
Clean up code
shrverma Jun 28, 2023
c3002ef
Move sql expression generator functions to a new util class
shrverma Jun 28, 2023
51d3d05
Change util class name
shrverma Jun 28, 2023
31e6f84
Fix checkstyle errors
shrverma Jun 28, 2023
e217358
Changes to set type util function
shrverma Jun 30, 2023
c5165f1
Fix rename directive implementation
shrverma Jul 4, 2023
67755cf
Merge branch 'develop' into relational-directives
shrverma Jul 11, 2023
7cb8aa3
Add UI toggle to wrangler
shrverma Jul 11, 2023
1adbb2d
Fix checkstyle error
shrverma Jul 11, 2023
2a1c2b7
Implement swap directive
shrverma Jul 12, 2023
19518e6
Refactor execution logic
shrverma Jul 14, 2023
35fbf93
Implement filter directives
shrverma Jul 14, 2023
0158b38
Change UI toggle
shrverma Jul 14, 2023
cb5eb7f
Move feature flag checks to separate function
shrverma Jul 14, 2023
8f926a5
Implement SetRecordDelimiter directive
shrverma Jul 15, 2023
68785b5
Implement split-email directive
shrverma Jul 15, 2023
6404c8d
Implement transformation directives
shrverma Jul 17, 2023
ca89034
Refactor code
shrverma Jul 22, 2023
09aa10e
Implement UUID, split-rows and JSON-object directives
shrverma Jul 22, 2023
278a30e
Remove row filter directive implementation
shrverma Jul 22, 2023
480f2aa
Implement fill-null-or-empty
shrverma Jul 22, 2023
7f3f3cb
Implement URL encoding and decoding directives
shrverma Jul 22, 2023
63a5932
Move partially supported directives
shrverma Jul 24, 2023
69e8527
Implement fixed-length-parser
shrverma Jul 24, 2023
3ee71d7
Update Directive.java
shrverma Jul 25, 2023
016c989
Update ChangeColCaseNames.java
shrverma Jul 25, 2023
5899112
Merge pull request #646 from data-integrations/sql-directives
shrverma Jul 25, 2023
ff57c83
Merge branch 'relational-directives' into UI-change-wrangler
shrverma Jul 25, 2023
1e1d898
Merge pull request #648 from data-integrations/UI-change-wrangler
shrverma Jul 25, 2023
7ebc4c3
Add directiverelationaltransform interface
shrverma Jul 25, 2023
6f7d14d
Refactor execution logic
shrverma Jul 25, 2023
29eda87
Refactor code
shrverma Jul 25, 2023
a656f47
Add sql directive validation
shrverma Jul 26, 2023
5ce676d
Refactor code
shrverma Jul 26, 2023
b5ce529
Refactor code
shrverma Jul 26, 2023
ed0e9a4
Fix class not found error
shrverma Jul 26, 2023
36326f4
Merge pull request #653 from data-integrations/Directive-validation
shrverma Jul 31, 2023
bec9679
Remove extra function
shrverma Aug 1, 2023
39e360f
Merge pull request #651 from data-integrations/sql-temp
shrverma Aug 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright © 2017-2019 Cask Data, Inc.
*
* Licensed 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 io.cdap.wrangler.api;

/**
* Directive interface which supports Relational transformations
*/
public interface RelationalDirective extends Directive {

/**
* returns sql expression
* @return expression
*/
String getSQL();

}
13 changes: 12 additions & 1 deletion wrangler-core/src/main/java/io/cdap/directives/column/Drop.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.cdap.wrangler.api.DirectiveExecutionException;
import io.cdap.wrangler.api.DirectiveParseException;
import io.cdap.wrangler.api.ExecutorContext;
import io.cdap.wrangler.api.RelationalDirective;
import io.cdap.wrangler.api.Row;
import io.cdap.wrangler.api.annotations.Categories;
import io.cdap.wrangler.api.lineage.Lineage;
Expand All @@ -43,7 +44,7 @@
@Name(Drop.NAME)
@Categories(categories = { "column"})
@Description("Drop one or more columns.")
public class Drop implements Directive, Lineage {
public class Drop implements RelationalDirective, Lineage {
public static final String NAME = "drop";

// Columns to be dropped.
Expand Down Expand Up @@ -88,4 +89,14 @@ public Mutation lineage() {
.drop(Many.of(columns))
.build();
}

@Override
public String getSQL() {
String sql = "DROP COLUMN ";
for (String col : columns) {
sql += col + ",";
}
sql = sql.substring(0, sql.length() - 1);
return sql;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.cdap.cdap.api.artifact.ArtifactSummary;
import io.cdap.wrangler.api.Directive;
import io.cdap.wrangler.api.DirectiveLoadException;
import io.cdap.wrangler.api.RelationalDirective;
import org.reflections.Reflections;

import java.util.ArrayList;
Expand Down Expand Up @@ -84,6 +85,7 @@ public SystemDirectiveRegistry(List<String> namespaces) throws DirectiveLoadExce
try {
Reflections reflections = new Reflections(namespace);
Set<Class<? extends Directive>> system = reflections.getSubTypesOf(Directive.class);
system.addAll(reflections.getSubTypesOf(RelationalDirective.class));
for (Class<? extends Directive> directive : system) {
DirectiveInfo info = DirectiveInfo.fromSystem(directive);
registry.put(info.name(), info);
Expand Down
76 changes: 62 additions & 14 deletions wrangler-transform/src/main/java/io/cdap/wrangler/Wrangler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.cdap.cdap.etl.api.relational.StringExpressionFactoryType;
import io.cdap.cdap.features.Feature;
import io.cdap.directives.aggregates.DefaultTransientStore;
import io.cdap.directives.column.Drop;
import io.cdap.wrangler.api.CompileException;
import io.cdap.wrangler.api.CompileStatus;
import io.cdap.wrangler.api.Compiler;
Expand All @@ -53,9 +54,11 @@
import io.cdap.wrangler.api.EntityCountMetric;
import io.cdap.wrangler.api.ErrorRecord;
import io.cdap.wrangler.api.ExecutorContext;
import io.cdap.wrangler.api.RecipeException;
import io.cdap.wrangler.api.RecipeParser;
import io.cdap.wrangler.api.RecipePipeline;
import io.cdap.wrangler.api.RecipeSymbol;
import io.cdap.wrangler.api.RelationalDirective;
import io.cdap.wrangler.api.Row;
import io.cdap.wrangler.api.TokenGroup;
import io.cdap.wrangler.api.TransientStore;
Expand Down Expand Up @@ -190,7 +193,7 @@ public void configurePipeline(PipelineConfigurer configurer) {
if (!config.containsMacro(Config.NAME_PRECONDITION_SQL)) {
validatePrecondition(config.getPreconditionSQL(), true, collector);
}
validateSQLModeDirectives(collector);
//validateSQLModeDirectives(collector);
} else {
if (!config.containsMacro(Config.NAME_PRECONDITION)) {
validatePrecondition(config.getPreconditionJEXL(), false, collector);
Expand Down Expand Up @@ -569,23 +572,66 @@ private RecipeParser getRecipeParser(StageContext context)

@Override
public Relation transform(RelationalTranformContext relationalTranformContext, Relation relation) {
if (PRECONDITION_LANGUAGE_SQL.equalsIgnoreCase(config.getPreconditionLanguage())
&& checkPreconditionNotEmpty(true)) {
if (!(PRECONDITION_LANGUAGE_SQL.equalsIgnoreCase(config.getPreconditionLanguage())
&& checkPreconditionNotEmpty(true))) {
return new InvalidRelation("Plugin is not configured for relational transformation");
}

if (!Feature.WRANGLER_PRECONDITION_SQL.isEnabled(relationalTranformContext)) {
throw new RuntimeException("SQL Precondition feature is not available");
}
if (!Feature.WRANGLER_PRECONDITION_SQL.isEnabled(relationalTranformContext)) {
throw new RuntimeException("SQL Precondition feature is not available");
}

Optional<ExpressionFactory<String>> expressionFactory = getExpressionFactory(relationalTranformContext);
if (!expressionFactory.isPresent()) {
return new InvalidRelation("Cannot find an Expression Factory");
}
Optional<ExpressionFactory<String>> expressionFactory = getExpressionFactory(relationalTranformContext);
if (!expressionFactory.isPresent()) {
return new InvalidRelation("Cannot find an Expression Factory");
}

Expression filterExpression = expressionFactory.get().compile(config.getPreconditionSQL());
Relation filteredRelation = relation.filter(filterExpression);

ExpressionFactory<String> expFactory = expressionFactory.get();

String recipe = config.getDirectives();

registry = SystemDirectiveRegistry.INSTANCE;
try {
registry.reload("default");
} catch (DirectiveLoadException e) {
throw new RuntimeException(e);
}

Expression filterExpression = expressionFactory.get().compile(config.getPreconditionSQL());
return relation.filter(filterExpression);
List<Directive> directives = null;
try {
GrammarBasedParser parser = new GrammarBasedParser("default", new MigrateToV2(recipe).migrate(), registry);
directives = parser.parse();
} catch (DirectiveParseException e) {
throw new RuntimeException(e);
} catch (RecipeException e) {
throw new RuntimeException(e);
}

for (Directive directive : directives) {
// Expression exp = expFactory.compile(sql);
if (!(directive instanceof RelationalDirective)) {
throw new RuntimeException("Directive is not relational Directive");
}
// currently supporting only drop column
// SQL will be returned as "DROP COLUMN col1, col2"
String sql = ((RelationalDirective) directive).getSQL();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not invoke execute on directives and offload directive execution to RecipePipelineExecutor. We can introduce a new function for relationalDirective execute

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. will look into offloading directive execution to RecipePipelineExecutor.

List<String> cols = getColumnsOfDropSQL(sql);
for (String col : cols) {
filteredRelation = filteredRelation.dropColumn(col);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this logic into the directive. SparkSQLDataset should be returned from each Directive and passed onto the next one. SparkSQLEngine.transform should be invoked in each directive for executing.
Building just one SQL for each Directive should be good enough. SparkSQLEngine will internally chain the SQL execute in one map operation is my understanding, please check that part.
There is a comment on that already, https://github.com/cdapio/cdap/blob/1cc26e664a22977fb39f29ea03a46ee4ab531f92/cdap-app-templates/cdap-etl/hydrator-spark-core-base/src/main/java/io/cdap/cdap/etl/spark/batch/SparkSQLEngine.java#L193

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. These changes are for poc. will move the logic to directives.

}
}
return filteredRelation;
}

return new InvalidRelation("Plugin is not configured for relational transformation");
private List<String> getColumnsOfDropSQL(String sql) {
List<String> cols = new ArrayList<>();
for (String col : sql.split(" ")[2].split(",")) {
cols.add(col.trim());
}
return cols;
}

private Optional<ExpressionFactory<String>> getExpressionFactory(RelationalTranformContext ctx) {
Expand Down Expand Up @@ -642,6 +688,7 @@ public static class Config extends PluginConfig {
static final String NAME_PRECONDITION_LANGUAGE = "expressionLanguage";
static final String NAME_FIELD = "field";
static final String NAME_DIRECTIVES = "directives";
static final String NAME_RELATIONAL_DIRECTIVES = "relationalDirectives";
static final String NAME_UDD = "udd";
static final String NAME_SCHEMA = "schema";
static final String NAME_ON_ERROR = "on-error";
Expand Down Expand Up @@ -672,6 +719,7 @@ public static class Config extends PluginConfig {
@Nullable
private String directives;


@Name(NAME_UDD)
@Description("List of User Defined Directives (UDD) that have to be loaded.")
@Nullable
Expand All @@ -694,7 +742,7 @@ public static class Config extends PluginConfig {
private final String onError;

public Config(String preconditionLanguage, String precondition, String directives, String udds,
String field, String schema, String onError) {
String field, String schema, String onError, String relationalDirectives) {
this.preconditionLanguage = preconditionLanguage;
this.precondition = precondition;
this.directives = directives;
Expand Down
18 changes: 18 additions & 0 deletions wrangler-transform/widgets/Wrangler-transform.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@
}
]
},
{
"label": "RelationalDirectives",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to keep these changes by feature flag? Otherwise we have to move this code to a branch and keep syncing the branch with Develop or other future changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will add feature flag. until then we won't merge to develop

"properties": [
{
"widget-type": "wrangler-relational-directives",
"label": "RelationalRecipe",
"name": "RelationalDirectives",
"widget-attributes" : {
"placeholder" : "#pragma load-directives my-directive; my-directive :body;"
}
},
{
"widget-type": "csv",
"label": "User Defined SQL(UDS)",
"name": "uds"
}
]
},
{
"label" : "Error Handling",
"properties" : [
Expand Down