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

Dimension lookup does not load/store "type of return field" in read only mode #2221 #2246

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -142,17 +142,11 @@ public boolean processRow() throws HopException {
data.fieldnrs = new int[f.getFields().size()];
for (int i = 0; i < data.fieldnrs.length; i++) {
DLField field = f.getFields().get(i);
DimensionUpdateType updateType = field.getUpdateType();
if (updateType == null) {
throw new HopTransformException(
"Please specify an update type for field nr " + (i + 1) + " : " + field.getName());
}
if (meta.isUpdate() && updateType.isWithArgument()) {
if (isLookupOrUpdateTypeWithArgument(meta.isUpdate(), field)) {
data.fieldnrs[i] = data.outputRowMeta.indexOfValue(field.getName());
if (data.fieldnrs[i] < 0) {
throw new HopTransformException(
BaseMessages.getString(
PKG, "DimensionLookup.Exception.KeyFieldNotFound", field.getName()));
throw new HopTransformException(BaseMessages.getString(PKG,
"DimensionLookup.Exception.KeyFieldNotFound", field.getName()));
}
} else {
data.fieldnrs[i] = -1;
Expand Down Expand Up @@ -227,6 +221,21 @@ public boolean processRow() throws HopException {
return true;
}

public boolean isLookupOrUpdateTypeWithArgument(final boolean update, final DLField field) throws HopTransformException {
// Lookup
if (!update) {
return true;
}

// Update type field
DimensionUpdateType updateType = field.getUpdateType();
if (updateType == null) {
throw new HopTransformException(BaseMessages.getString(
PKG, "DimensionLookup.Exception.MissingUpdateTypeField ", field.getName()));
}
return updateType.isWithArgument();
}

private Date determineDimensionUpdatedDate(Object[] row) throws HopException {
if (data.datefieldnr < 0) {
return getPipeline().getExecutionStartDate(); // start of pipeline...
Expand Down Expand Up @@ -822,7 +831,7 @@ private synchronized Object[] lookupValues(IRowMeta rowMeta, Object[] row) throw
* table: dimension table keys[]: which dim-fields do we use to look up key? retval: name of the
* key to return datefield: do we have a datefield? datefrom, dateto: date-range, if any.
*/
private void setDimLookup(IRowMeta rowMeta) throws HopDatabaseException {
private void setDimLookup(IRowMeta rowMeta) throws HopException {
DLFields f = meta.getFields();
DatabaseMeta databaseMeta = meta.getDatabaseMeta();

Expand All @@ -847,8 +856,7 @@ private void setDimLookup(IRowMeta rowMeta) throws HopDatabaseException {

for (DLField field : f.getFields()) {
// Don't retrieve the fields without input
if (StringUtils.isNotEmpty(field.getLookup())
&& meta.isUpdate() && field.getUpdateType().isWithArgument()) {
if (StringUtils.isNotEmpty(field.getLookup()) && isLookupOrUpdateTypeWithArgument(meta.isUpdate(), field)) {
sql += ", " + databaseMeta.quoteField(field.getLookup());

if (StringUtils.isNotEmpty(field.getName()) && !field.getLookup().equals(field.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,7 @@ public void getData() {
DimensionUpdateType updateType = field.getUpdateType();
item.setText(3, updateType == null ? "" : updateType.getDescription());
} else {
// String -> int -> String
item.setText(3, ValueMetaFactory.getValueMetaName(field.getReturnType()));
item.setText(3, Const.NVL(field.getReturnType(),""));
}
}

Expand Down Expand Up @@ -1227,10 +1226,15 @@ private void getInfo(DimensionLookupMeta in) {
DLField field = new DLField();
field.setLookup(item.getText(1));
field.setName(item.getText(2));
DimensionUpdateType updateType = DimensionUpdateType.lookupDescription(item.getText(3));
if ( updateType!=null) {
field.setUpdate(updateType.getCode());
if (in.isUpdate()) {
DimensionUpdateType updateType = DimensionUpdateType.lookupDescription(item.getText(3));
if (updateType != null) {
field.setUpdate(updateType.getCode());
}
} else {
field.setReturnType(item.getText(3));
}

f.getFields().add(field);
}
if (log.isDebug()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.hop.core.row.value.ValueMetaDate;
import org.apache.hop.core.row.value.ValueMetaFactory;
import org.apache.hop.core.row.value.ValueMetaInteger;
import org.apache.hop.core.util.Utils;
import org.apache.hop.core.variables.IVariables;
import org.apache.hop.i18n.BaseMessages;
import org.apache.hop.metadata.api.HopMetadataProperty;
Expand Down Expand Up @@ -643,7 +642,7 @@ private IRowMeta checkTableFields(
new CheckResult(
ICheckResult.TYPE_RESULT_ERROR,
"The update type specified is not valid for field '"
+ field.getName()
+ Const.NVL(field.getName(), field.getLookup())
+ "' : '"
+ field.getUpdate()
+ "'",
Expand All @@ -653,14 +652,15 @@ private IRowMeta checkTableFields(
} else {
// Check the type of the dimension field to look up
//
if (field.getReturnType() <= 0) {
int type = ValueMetaFactory.getIdForValueMeta(field.getReturnType());
if (type == IValueMeta.TYPE_NONE) {
remarks.add(
new CheckResult(
ICheckResult.TYPE_RESULT_ERROR,
"The return type specified is not valid for field '"
+ field.getName()
+ Const.NVL(field.getName(), field.getLookup())
+ "' : '"
+ field.getUpdate()
+ field.getReturnType()
+ "'",
transformMeta));
allOk = false;
Expand Down Expand Up @@ -1510,22 +1510,26 @@ public static class DLField {
injectionKeyDescription = "DimensionLookup.Injection.UPDATE_TYPE")
private String update;

@HopMetadataProperty(
key = "type",
injectionKey = "TYPE_OF_RETURN_FIELD",
injectionKeyDescription = "DimensionLookup.Injection.TYPE_OF_RETURN_FIELD")
private String returnType;

/** Not serialized. This is used to cache the lookup of the dimension type */
private DimensionUpdateType updateType;
/** Not serialized. This is used to cache the lookup of the Hop value return type */
private int returnType;

public DLField() {
this.updateType = null;
this.returnType = -1;
this.returnType = null;
}

public DLField(DLField f) {
this.name = f.name;
this.lookup = f.lookup;
this.update = f.update;
this.updateType = null;
this.returnType = -1;
this.returnType = f.returnType;
}

public DimensionUpdateType getUpdateType() {
Expand All @@ -1541,14 +1545,6 @@ public DimensionUpdateType getUpdateType() {
return null;
}

public int getReturnType() {
if (returnType >= 0) {
return returnType;
}
returnType = ValueMetaFactory.getIdForValueMeta(update);
return returnType;
}

/**
* Gets name
*
Expand Down Expand Up @@ -1586,7 +1582,7 @@ public void setLookup(String lookup) {
}

/**
* Gets update
* Gets update type code
*
* @return value of update
*/
Expand All @@ -1595,14 +1591,31 @@ public String getUpdate() {
}

/**
* Sets update
* Sets update type code
*
* @param update value of update
*/
public void setUpdate(String update) {
this.update = update;
this.updateType = null;
this.returnType = -1;
}

/**
* Gets return type for read only lookup
*
* @return type of
*/
public String getReturnType() {
return returnType;
}

/**
* Sets return type for read only lookup
*
* @param type the return type
*/
public void setReturnType(String type) {
this.returnType = type;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ DimensionLookupDialog.DateField.Label=Stream Datefield
DimensionLookupDialog.UseCache.Label=Enable the cache?
DimensionLookup.Exception.NullDimensionUpdatedDate=Invalid data - dimension updated date cannot be null - {0}
DimensionLookup.Exception.ErrorDetectedInComparingFields=Error comparing fields - cannot find lookup field [{0}]
DimensionLookup.Exception.MissingUpdateTypeField=Please specify an update type for field [{0}]
DimensionLookup.Injection.TARGET_SCHEMA=The name of the database schema to use.
DimensionLookup.Injection.TARGET_TABLE=The name of the target table to write data to.
DimensionLookup.Injection.CONNECTION_NAME=The name of the database connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
package org.apache.hop.pipeline.transforms.dimensionlookup;

import org.apache.hop.core.HopClientEnvironment;
import org.apache.hop.core.HopEnvironment;
import org.apache.hop.core.database.DatabaseMeta;
import org.apache.hop.metadata.serializer.memory.MemoryMetadataProvider;
import org.apache.hop.pipeline.transform.TransformSerializationTestUtil;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class DimensionLookupMetaTest {

Expand All @@ -44,11 +45,12 @@ public void testSerialization() throws Exception {

DimensionLookupMeta meta =
TransformSerializationTestUtil.testSerialization(
"/dimension-lookup-transform.xml", DimensionLookupMeta.class, metadataProvider);
"/dimension-update-transform.xml", DimensionLookupMeta.class, metadataProvider);

assertNotNull(meta.getDatabaseMeta());
assertNotNull(meta.getTableName());
assertNotNull(meta.getSchemaName());
assertTrue(meta.isUpdate());
assertEquals(100, meta.getCommitSize());
assertEquals(5000, meta.getCacheSize());
assertEquals(1, meta.getFields().getKeys().size());
Expand All @@ -63,5 +65,13 @@ public void testSerialization() throws Exception {
assertEquals("lastVersionLookup", meta.getFields().getFields().get(1).getLookup());
assertEquals("LastVersion", meta.getFields().getFields().get(1).getUpdate());
assertEquals(DimensionLookupMeta.DimensionUpdateType.LAST_VERSION, meta.getFields().getFields().get(1).getUpdateType());

meta =
TransformSerializationTestUtil.testSerialization(
"/dimension-lookup-transform.xml", DimensionLookupMeta.class, metadataProvider);

assertFalse(meta.isUpdate());
assertEquals("Number", meta.getFields().getFields().get(0).getReturnType());
assertEquals("String", meta.getFields().getFields().get(1).getReturnType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<table>dimension</table>
<connection>unit-test-db</connection>
<commit>100</commit>
<update>Y</update>
<update>N</update>
<fields>
<key>
<name>key</name>
Expand All @@ -43,14 +43,14 @@
<to>date_to</to>
</date>
<field>
<name>value</name>
<lookup>valueLookup</lookup>
<update>Insert</update>
<name>valueNumber</name>
<lookup>fieldNumber</lookup>
<type>Number</type>
</field>
<field>
<name>lastVersion</name>
<lookup>lastVersionLookup</lookup>
<update>LastVersion</update>
<name>valueString</name>
<lookup>fieldString</lookup>
<type>String</type>
</field>
<return>
<name>dimension_id</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
~
-->
<transform>
<name>Dimension lookup/update</name>
<type>DimensionLookup</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
</partitioning>
<schema>public</schema>
<table>dimension</table>
<connection>unit-test-db</connection>
<commit>100</commit>
<update>Y</update>
<fields>
<key>
<name>key</name>
<lookup>key</lookup>
</key>
<date>
<name/>
<from>date_from</from>
<to>date_to</to>
</date>
<field>
<name>value</name>
<lookup>valueLookup</lookup>
<update>Insert</update>
</field>
<field>
<name>lastVersion</name>
<lookup>lastVersionLookup</lookup>
<update>LastVersion</update>
</field>
<return>
<name>dimension_id</name>
<rename/>
<creation_method>tablemax</creation_method>
<use_autoinc>N</use_autoinc>
<version>version</version>
</return>
</fields>
<sequence/>
<min_year>1900</min_year>
<max_year>2199</max_year>
<cache_size>5000</cache_size>
<preload_cache>N</preload_cache>
<use_start_date_alternative>N</use_start_date_alternative>
<start_date_alternative>none</start_date_alternative>
<start_date_field_name/>
<useBatch>N</useBatch>
<attributes></attributes>
<GUI>
<xloc>364</xloc>
<yloc>94</yloc>
</GUI>
</transform>