generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persistence Component: Add support for digest generation for DuckDB (#…
- Loading branch information
Showing
28 changed files
with
947 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...finos/legend/engine/persistence/components/logicalplan/values/ConcatFunctionAbstract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.persistence.components.logicalplan.values; | ||
|
||
import org.immutables.value.Value.Immutable; | ||
import org.immutables.value.Value.Style; | ||
|
||
import java.util.List; | ||
|
||
@Immutable | ||
@Style( | ||
typeAbstract = "*Abstract", | ||
typeImmutable = "*", | ||
jdkOnly = true, | ||
optionalAcceptNullable = true, | ||
strictBuilder = true | ||
) | ||
public interface ConcatFunctionAbstract extends Value | ||
{ | ||
List<Value> values(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...va/org/finos/legend/engine/persistence/components/relational/duckdb/DuckDBDigestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.persistence.components.relational.duckdb; | ||
|
||
import org.finos.legend.engine.persistence.components.relational.jdbc.JdbcHelper; | ||
|
||
public class DuckDBDigestUtil | ||
{ | ||
private static final String CUSTOM_MD5_UDF = "MD5(SUBSTRING(HEX(IFNULL(NULLIF(A, FROM_HEX('')), FROM_HEX('DADB2C00'))), 9))"; | ||
private static final String CUSTOM_COLUMN_UDF = "IFNULL((FROM_HEX('DADB2C00') || ENCODE(COLUMN_NAME) || FROM_HEX('DADB2C00') || ENCODE(COLUMN_VALUE)), FROM_HEX(''))"; | ||
private static final String MD5_UDF = "MD5(A)"; | ||
|
||
public static void registerMD5Udf(JdbcHelper sink, String UdfName) | ||
{ | ||
sink.executeStatement("CREATE OR REPLACE FUNCTION " + UdfName + "(A) AS " + CUSTOM_MD5_UDF); | ||
} | ||
|
||
public static void registerColumnUdf(JdbcHelper sink, String UdfName) | ||
{ | ||
sink.executeStatement("CREATE OR REPLACE FUNCTION " + UdfName + "(COLUMN_NAME, COLUMN_VALUE) AS " + CUSTOM_COLUMN_UDF); | ||
} | ||
|
||
public static void registerBasicMD5Udf(JdbcHelper sink, String UdfName) | ||
{ | ||
sink.executeStatement("CREATE OR REPLACE FUNCTION " + UdfName + "(A) AS " + MD5_UDF); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...gend/engine/persistence/components/relational/duckdb/sql/visitor/CastFunctionVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.persistence.components.relational.duckdb.sql.visitor; | ||
|
||
import org.finos.legend.engine.persistence.components.logicalplan.LogicalPlanNode; | ||
import org.finos.legend.engine.persistence.components.logicalplan.values.CastFunction; | ||
import org.finos.legend.engine.persistence.components.optimizer.Optimizer; | ||
import org.finos.legend.engine.persistence.components.physicalplan.PhysicalPlanNode; | ||
import org.finos.legend.engine.persistence.components.relational.duckdb.sql.DuckDBDataTypeMapping; | ||
import org.finos.legend.engine.persistence.components.relational.sqldom.schema.DataType; | ||
import org.finos.legend.engine.persistence.components.transformer.LogicalPlanVisitor; | ||
import org.finos.legend.engine.persistence.components.transformer.VisitorContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CastFunctionVisitor implements LogicalPlanVisitor<CastFunction> | ||
{ | ||
@Override | ||
public VisitorResult visit(PhysicalPlanNode prev, CastFunction current, VisitorContext context) | ||
{ | ||
DataType dataType = new DuckDBDataTypeMapping().getDataType(current.type()); | ||
|
||
org.finos.legend.engine.persistence.components.relational.sqldom.schemaops.values.CastFunction castFunction = | ||
new org.finos.legend.engine.persistence.components.relational.sqldom.schemaops.values.CastFunction(dataType, context.quoteIdentifier()); | ||
for (Optimizer optimizer : context.optimizers()) | ||
{ | ||
castFunction = (org.finos.legend.engine.persistence.components.relational.sqldom.schemaops.values.CastFunction) optimizer.optimize(castFunction); | ||
} | ||
prev.push(castFunction); | ||
|
||
List<LogicalPlanNode> logicalPlanNodeList = new ArrayList<>(); | ||
logicalPlanNodeList.add(current.field()); | ||
|
||
return new VisitorResult(castFunction, logicalPlanNodeList); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...nd/engine/persistence/components/relational/duckdb/sql/visitor/ConcatFunctionVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.persistence.components.relational.duckdb.sql.visitor; | ||
|
||
import org.finos.legend.engine.persistence.components.logicalplan.values.ConcatFunction; | ||
import org.finos.legend.engine.persistence.components.physicalplan.PhysicalPlanNode; | ||
import org.finos.legend.engine.persistence.components.transformer.LogicalPlanVisitor; | ||
import org.finos.legend.engine.persistence.components.transformer.VisitorContext; | ||
|
||
public class ConcatFunctionVisitor implements LogicalPlanVisitor<ConcatFunction> | ||
{ | ||
@Override | ||
public VisitorResult visit(PhysicalPlanNode prev, ConcatFunction current, VisitorContext context) | ||
{ | ||
org.finos.legend.engine.persistence.components.relational.duckdb.sqldom.schemaops.values.ConcatFunction concatFunction = new org.finos.legend.engine.persistence.components.relational.duckdb.sqldom.schemaops.values.ConcatFunction(context.quoteIdentifier()); | ||
prev.push(concatFunction); | ||
|
||
return new VisitorResult(concatFunction, current.values()); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../legend/engine/persistence/components/relational/duckdb/sql/visitor/DigestUdfVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.persistence.components.relational.duckdb.sql.visitor; | ||
|
||
import org.finos.legend.engine.persistence.components.logicalplan.datasets.DataType; | ||
import org.finos.legend.engine.persistence.components.logicalplan.datasets.FieldType; | ||
import org.finos.legend.engine.persistence.components.logicalplan.values.CastFunction; | ||
import org.finos.legend.engine.persistence.components.logicalplan.values.StagedFilesFieldValue; | ||
import org.finos.legend.engine.persistence.components.logicalplan.values.Value; | ||
|
||
import java.util.Map; | ||
|
||
public class DigestUdfVisitor extends org.finos.legend.engine.persistence.components.relational.ansi.sql.visitors.DigestUdfVisitor | ||
{ | ||
protected Value getColumnValueAsStringType(Value value, FieldType dataType, Map<DataType, String> typeConversionUdfNames) | ||
{ | ||
if (value instanceof StagedFilesFieldValue) | ||
{ | ||
if (typeConversionUdfNames.containsKey(dataType.dataType())) | ||
{ | ||
// TO_STRING(CAST(field AS original_type)) | ||
return org.finos.legend.engine.persistence.components.logicalplan.values.Udf.builder() | ||
.udfName(typeConversionUdfNames.get(dataType.dataType())) | ||
.addParameters(CastFunction.builder().field(value).type(dataType).build()).build(); | ||
} | ||
else | ||
{ | ||
// CAST(CAST(field AS original_type) AS VARCHAR) | ||
return CastFunction.builder() | ||
.field(CastFunction.builder().field(value).type(dataType).build()) | ||
.type(FieldType.builder().dataType(DataType.VARCHAR).build()).build(); | ||
} | ||
} | ||
else | ||
{ | ||
if (typeConversionUdfNames.containsKey(dataType.dataType())) | ||
{ | ||
// TO_STRING(field) | ||
return org.finos.legend.engine.persistence.components.logicalplan.values.Udf.builder() | ||
.udfName(typeConversionUdfNames.get(dataType.dataType())) | ||
.addParameters(value).build(); | ||
} | ||
else | ||
{ | ||
// CAST(field AS VARCHAR) | ||
return CastFunction.builder() | ||
.field(value) | ||
.type(FieldType.builder().dataType(DataType.VARCHAR).build()).build(); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...gine/persistence/components/relational/duckdb/sqldom/schemaops/values/ConcatFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// 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 org.finos.legend.engine.persistence.components.relational.duckdb.sqldom.schemaops.values; | ||
|
||
import org.finos.legend.engine.persistence.components.relational.sqldom.SqlDomException; | ||
import org.finos.legend.engine.persistence.components.relational.sqldom.schemaops.values.Value; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ConcatFunction extends Value | ||
{ | ||
private List<Value> values; | ||
|
||
public ConcatFunction(String quoteIdentifier) | ||
{ | ||
super(quoteIdentifier); | ||
this.values = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public void genSql(StringBuilder builder) throws SqlDomException | ||
{ | ||
genSqlWithoutAlias(builder); | ||
super.genSql(builder); | ||
} | ||
|
||
@Override | ||
public void genSqlWithoutAlias(StringBuilder builder) throws SqlDomException | ||
{ | ||
for (int ctr = 0; ctr < values.size(); ctr++) | ||
{ | ||
values.get(ctr).genSqlWithoutAlias(builder); | ||
if (ctr < (values.size() - 1)) | ||
{ | ||
builder.append("||"); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void push(Object node) | ||
{ | ||
if (node instanceof Value) | ||
{ | ||
values.add((Value) node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.