Skip to content
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 @@ -223,6 +223,7 @@ void determineFieldsTypesQueryingDb() throws HopException {
this, meta.getSchemaName(), meta.getTableName());

IRowMeta fields = data.db.getTableFields(schemaTable);
data.dbRowMeta = fields;
if (fields != null) {
// Fill in the types...
for (int i = 0; i < keyFields.size(); i++) {
Expand Down Expand Up @@ -300,9 +301,9 @@ private void initReturnMeta() {

data.returnValueTypes = new int[returnValues.size()];
for (int i = 0; i < returnValues.size(); i++) {
data.returnValueTypes[i] =
ValueMetaFactory.getIdForValueMeta(returnValues.get(i).getDefaultType());
IValueMeta v = data.outputRowMeta.getValueMeta(getInputRowMeta().size() + i).clone();
// Use resolved output type (may be inferred from the table when default type is empty)
data.returnValueTypes[i] = v.getType();
data.returnMeta.addValueMeta(v);
}
}
Expand All @@ -318,10 +319,6 @@ public boolean processRow() throws HopException {
if (first) {
first = false;

// create the output metadata
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);

Lookup lookup = meta.getLookup();
List<KeyField> keyFields = lookup.getKeyFields();
List<ReturnValue> returnValues = lookup.getReturnValues();
Expand Down Expand Up @@ -406,8 +403,19 @@ public boolean processRow() throws HopException {
data.cache = DefaultCache.newCache(data, meta.getCacheSize());
}

// Query table metadata once: key types + return type inference for empty default types
determineFieldsTypesQueryingDb();

// create the output metadata (pass table fields so empty return types can be inferred)
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(
data.outputRowMeta,
getTransformName(),
new IRowMeta[] {data.dbRowMeta},
null,
this,
metadataProvider);

initNullIf();

initLookupMeta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class DatabaseLookupData extends BaseTransformData implements ITransformD
public IRowMeta outputRowMeta;
public IRowMeta lookupMeta;
public IRowMeta returnMeta;

/** Cached lookup-table field metadata (from getTableFields), reused for return type inference. */
public IRowMeta dbRowMeta;

public boolean isCanceled;
public boolean allEquals;
public int[] conditions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,48 @@ public void getFields(
IHopMetadataProvider metadataProvider)
throws HopTransformException {
try {
// info row metadata: null or length 0 : no lookup row metadata from database
// Prefer an explicit return type. When none is configured, infer from table field
// metadata (info row from design-time / runtime, otherwise query the table).
//
if (Utils.isEmpty(infoRowMeta) || infoRowMeta[0] == null) {
for (ReturnValue returnValue : lookup.getReturnValues()) {
IValueMeta v =
ValueMetaFactory.createValueMeta(
!Utils.isEmpty(returnValue.getNewName())
? returnValue.getNewName()
: returnValue.getTableField(),
ValueMetaFactory.getIdForValueMeta(returnValue.getDefaultType()));
v.setOrigin(name);
row.addValueMeta(v);
}
return;
IRowMeta tableFields = null;
if (!Utils.isEmpty(infoRowMeta) && infoRowMeta[0] != null) {
tableFields = infoRowMeta[0];
}

for (ReturnValue returnValue : lookup.getReturnValues()) {
IValueMeta v = infoRowMeta[0].searchValueMeta(returnValue.getTableField());
if (v != null) {
IValueMeta copy = v.clone(); // avoid renaming other value meta
copy.setName(
!Utils.isEmpty(returnValue.getNewName())
? returnValue.getNewName()
: returnValue.getTableField());
copy.setOrigin(name);
row.addValueMeta(copy);
String fieldName =
!Utils.isEmpty(returnValue.getNewName())
? returnValue.getNewName()
: returnValue.getTableField();
int typeId = ValueMetaFactory.getIdForValueMeta(returnValue.getDefaultType());
IValueMeta v;
if (typeId != IValueMeta.TYPE_NONE) {
v = ValueMetaFactory.createValueMeta(fieldName, typeId);
} else {
if (tableFields == null) {
tableFields = getTableFields(variables);
}
if (tableFields == null) {
throw new HopTransformException(
BaseMessages.getString(
PKG, "DatabaseLookupMeta.Exception.UnableToRetrieveDataTypeOfReturnField"));
}
IValueMeta source = tableFields.searchValueMeta(returnValue.getTableField());
if (source == null) {
throw new HopTransformException(
BaseMessages.getString(
PKG,
"DatabaseLookupMeta.Exception.UnableToFindReturnField",
returnValue.getTableField()));
}
v = source.clone(); // avoid renaming other value meta
v.setName(fieldName);
}
v.setOrigin(name);
row.addValueMeta(v);
}
} catch (HopTransformException e) {
throw e;
} catch (HopException e) {
throw new HopTransformException("Error getting fields metadata", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ DatabaseLookupMeta.Check.MissingReturnFieldsInLookupTable=Missing return fields
DatabaseLookupMeta.Check.NoInputReceivedFromOtherTransforms=No input received from other transforms\!
DatabaseLookupMeta.Check.TransformIsReceivingInfoFromOtherTransforms=Transform is receiving info from other transforms.
DatabaseLookupMeta.ERROR0004.ErrorGettingTableFields=An error occurred\:
DatabaseLookupMeta.Exception.UnableToFindReturnField=Unable to find return field [{0}] in the lookup table.
DatabaseLookupMeta.Exception.UnableToRetrieveDataTypeOfReturnField=Unable to retrieve data type of return fields because table metadata is not available
DatabaseLookupMeta.Impact.Key=Key
DatabaseLookupMeta.Impact.ReturnValue=Return value
DatabaseLookupMeta.Injection.Cache=Cache lookup data?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.
*/

package org.apache.hop.pipeline.transforms.databaselookup;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.hop.core.row.RowMeta;
import org.junit.jupiter.api.Test;

class DatabaseLookupDataTest {

@Test
void defaultConstructor_InitializesNullRuntimeState() {
DatabaseLookupData data = new DatabaseLookupData();

assertNull(data.db);
assertNull(data.cache);
assertNull(data.nullif);
assertNull(data.keynrs);
assertNull(data.keynrs2);
assertNull(data.keytypes);
assertNull(data.outputRowMeta);
assertNull(data.lookupMeta);
assertNull(data.returnMeta);
assertNull(data.dbRowMeta);
assertNull(data.conditions);
assertNull(data.returnValueTypes);
assertNull(data.returnTrimTypes);
assertNull(data.trimIndexes);
assertFalse(data.isCanceled);
assertFalse(data.allEquals);
assertFalse(data.hasDBCondition);
}

@Test
void fields_CanBeAssignedAndRead() {
DatabaseLookupData data = new DatabaseLookupData();
data.dbRowMeta = new RowMeta();
data.lookupMeta = new RowMeta();
data.returnMeta = new RowMeta();
data.outputRowMeta = new RowMeta();
data.keynrs = new int[] {0};
data.keynrs2 = new int[] {-1};
data.keytypes = new int[] {1};
data.conditions = new int[] {DatabaseLookupMeta.CONDITION_EQ};
data.returnValueTypes = new int[] {2};
data.returnTrimTypes = new String[] {"none"};
data.nullif = new Object[] {"N/A"};
data.cache = DefaultCache.newCache(data, 8);
data.allEquals = true;
data.hasDBCondition = true;
data.isCanceled = true;

assertTrue(data.allEquals);
assertTrue(data.hasDBCondition);
assertTrue(data.isCanceled);
assertEquals(0, data.keynrs[0]);
assertEquals(DatabaseLookupMeta.CONDITION_EQ, data.conditions[0]);
assertInstanceOf(DefaultCache.class, data.cache);
}
}
Loading
Loading