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

[ZEPPELIN-6131] Update Interpreter to Store Values as Integers When Applicable #4878

Merged
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 @@ -39,10 +39,55 @@ public UpdateInterpreterSettingRequest(Map<String, InterpreterProperty> properti
this.option = option;
}

/**
* Retrieves the properties of the interpreter and, if the property type is "number"
* and its value is a double that represents a whole number, converts that value to an integer.
*
* @return A map of the interpreter's properties with possible modifications to the numeric properties.
*/
public Map<String, InterpreterProperty> getProperties() {
properties.forEach((key, property) -> {
if (isNumberType(property) && isWholeNumberDouble(property.getValue())) {
convertDoubleToInt(property);
}
});
return properties;
}

/**
* Checks if the property type is "number".
*
* @param property The InterpreterProperty to check.
* @return true if the property type is "number", false otherwise.
*/
private boolean isNumberType(InterpreterProperty property) {
return "number".equals(property.getType());
}

/**
* Checks if the given value is a Double and represents a whole number.
*
* @param value The object to check.
* @return true if the value is a Double and a whole number, false otherwise.
*/
private boolean isWholeNumberDouble(Object value) {
if (value instanceof Double) {
Double doubleValue = (Double) value;
return doubleValue == Math.floor(doubleValue);
}
return false;
}

/**
* Converts the value of the given property from a Double to an Integer if the Double represents a whole number.
*
* @param property The InterpreterProperty whose value will be converted.
*/
private void convertDoubleToInt(InterpreterProperty property) {
Double doubleValue = (Double) property.getValue();
property.setValue(doubleValue.intValue());
}

public List<Dependency> getDependencies() {
return dependencies;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.zeppelin.rest.message;

import org.apache.zeppelin.dep.Dependency;
import org.apache.zeppelin.interpreter.InterpreterOption;
import org.apache.zeppelin.interpreter.InterpreterProperty;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Unit test class for the UpdateInterpreterSettingRequest.
* This class tests the behavior of UpdateInterpreterSettingRequest methods,
* especially focusing on property type handling and value conversions.
*/
class UpdateInterpreterSettingRequestTest {

private Map<String, InterpreterProperty> properties;
private List<Dependency> dependencies;
private InterpreterOption option;
private UpdateInterpreterSettingRequest request;

/**
* Setup method that initializes test fixtures before each test.
* Mocks dependencies and option objects to isolate UpdateInterpreterSettingRequest behavior.
*/
@BeforeEach
void setUp() {
properties = new HashMap<>();
dependencies = Collections.emptyList();
option = mock(InterpreterOption.class);
request = new UpdateInterpreterSettingRequest(properties, dependencies, option);
}

/**
* Tests getProperties method to verify that properties with "number" type
* and whole-number Double values are correctly converted to Integer.
* Verifies that only whole-number Doubles are converted and non-integer Doubles remain unchanged.
*/
@Test
void testGetPropertiesWithWholeNumberDoubleConversion() {
InterpreterProperty property1 = mock(InterpreterProperty.class);
when(property1.getType()).thenReturn("number");
when(property1.getValue()).thenReturn(5.0);

InterpreterProperty property2 = mock(InterpreterProperty.class);
when(property2.getType()).thenReturn("number");
when(property2.getValue()).thenReturn(5.5);

properties.put("property1", property1);
properties.put("property2", property2);

Map<String, InterpreterProperty> resultProperties = request.getProperties();

verify(property1).setValue(5);
verify(property2, never()).setValue(any());
assertEquals(properties, resultProperties);
}

/**
* Tests getProperties method when the property type is not "number".
* Verifies that no conversion is performed on non-number types.
*/
@Test
void testGetPropertiesWithoutConversion() {
InterpreterProperty property = mock(InterpreterProperty.class);
when(property.getType()).thenReturn("string");
when(property.getValue()).thenReturn("test");

properties.put("property", property);

Map<String, InterpreterProperty> resultProperties = request.getProperties();

verify(property, never()).setValue(any());
assertEquals(properties, resultProperties);
}

/**
* Tests getDependencies method to confirm that it returns the correct dependencies list.
*/
@Test
void testGetDependencies() {
assertEquals(dependencies, request.getDependencies());
}

/**
* Tests getOption method to confirm that it returns the correct interpreter option.
*/
@Test
void testGetOption() {
assertEquals(option, request.getOption());
}
}
Loading