

# DynamoDB의 조건 및 필터 표현식, 연산자, 함수
<a name="Expressions.OperatorsAndFunctions"></a>

DynamoDB 테이블에서 데이터를 조작하려면 `PutItem`, `UpdateItem` 및 `DeleteItem` 작업을 사용합니다. 이러한 데이터 조작 작업의 경우 조건 표현식을 지정하여 어떤 항목을 수정할지를 결정할 수 있습니다. 조건 표현식이 true로 평가되는 경우 작업이 성공합니다. 그렇지 않으면 작업이 실패합니다.

이 섹션에서는 Amazon DynamoDB에서 필터 표현식 및 조건 표현식을 작성하기 위한 기본 제공 함수와 키워드에 대해 살펴봅니다. [DynamoDB를 사용한 함수 및 프로그래밍에 대한 자세한 내용은 [DynamoDB 및 AWS SDK를 사용한 프로그래밍](Programming.md) 및 DynamoDB API 참조](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/)를 참조하세요.

**Topics**
+ [필터 및 조건 표현식 구문](#Expressions.OperatorsAndFunctions.Syntax)
+ [비교 실행](#Expressions.OperatorsAndFunctions.Comparators)
+ [함수](#Expressions.OperatorsAndFunctions.Functions)
+ [논리 평가](#Expressions.OperatorsAndFunctions.LogicalEvaluations)
+ [괄호](#Expressions.OperatorsAndFunctions.Parentheses)
+ [조건식의 우선 순위](#Expressions.OperatorsAndFunctions.Precedence)

## 필터 및 조건 표현식 구문
<a name="Expressions.OperatorsAndFunctions.Syntax"></a>

아래의 구문 요약에서 *피연산 함수*는 다음과 같이 사용됩니다.
+ `Id`, `Title`, `Description`, `ProductCategory` 등의 최상위 속성
+ 내포 속성을 참조하는 문서 경로

```
condition-expression ::=
      operand comparator operand
    | operand BETWEEN operand AND operand
    | operand IN ( operand (',' operand (, ...) ))
    | function
    | condition AND condition
    | condition OR condition
    | NOT condition
    | ( condition )

comparator ::=
    =
    | <>
    | <
    | <=
    | >
    | >=

function ::=
    attribute_exists (path)
    | attribute_not_exists (path)
    | attribute_type (path, type)
    | begins_with (path, substr)
    | contains (path, operand)
    | size (path)
```

## 비교 실행
<a name="Expressions.OperatorsAndFunctions.Comparators"></a>

다음 비교기를 사용하여 피연산자를 단일 값과 비교합니다.
+ `a = b` - *a*가 *b*와 같은 경우 true입니다.
+ `a <> b` - *a*가 *b*와 같지 않은 경우 true입니다.
+ `a < b` - *a*가 *b*보다 작은 경우 true입니다.
+ `a <= b` - *a*가 *b*보다 작거나 같은 경우 true입니다.
+ `a > b` - *a*가 *b*보다 큰 경우 true입니다.
+ `a >= b` - *a*가 *b*보다 크거나 같은 경우 true입니다.

`BETWEEN` 및 `IN` 키워드를 사용하여 피연산 함수와 값의 범위 또는 열거된 값 목록을 비교합니다.
+ `a BETWEEN b AND c` - *a*가 *b*보다 크거나 같고 *c*보다 작거나 같은 경우 true입니다.
+ `a IN (b, c, d) ` - *a*가 목록의 임의 값(*b*, *c*, *d* 값 등)과 같은 경우 true입니다. 목록에는 쉼표로 구분된 최대 100개의 값이 포함될 수 있습니다.

## 함수
<a name="Expressions.OperatorsAndFunctions.Functions"></a>

다음의 함수를 사용하여 항목에 속성이 존재하는지 판단하거나 속성 값을 평가합니다. 함수 이름은 대/소문자를 구분합니다. 중첩 속성의 경우 전체 문서 경로를 제공해야 합니다.


****  

| 함수 | 설명 | 
| --- | --- | 
|  `attribute_exists (path)`  | `path`에 의해 지정된 속성이 항목에 포함되어 있는 경우 true입니다. 예: `Product` 테이블에 있는 항목에 측면 사진이 있는지를 확인합니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  | 
|  `attribute_not_exists (path)`  | `path`에 의해 지정된 속성이 항목에 포함되어 있지 않은 경우 true입니다. 예: 항목에 `Manufacturer` 속성이 포함되어 있는지 확인합니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  | 
|  `attribute_type (path, type)`  |  지정된 경로의 속성이 특정 데이터 유형인 경우 true. `type` 파라미터는 반드시 다음 값 중 하나이어야 합니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) `type` 파라미터에는 반드시 표현식 속성 값을 사용해야 합니다. 예: `QuantityOnHand` 속성이 List 유형인지 확인합니다. 위의 예에서 `:v_sub`가 `L` 문자열의 자리 표시자입니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) `type` 파라미터에는 반드시 표현식 속성 값을 사용해야 합니다.  | 
|  `begins_with (path, substr)`  |  `path`에 의해 지정된 속성이 특정 하위 문자열로 시작하는 경우 true입니다. 예: 정면 사진 URL이 `http://`로 시작되는지 확인합니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) 수식 속성 값인 `:v_sub`가 `http://`의 자리 표시자입니다.  | 
|  `contains (path, operand)`  | `path`에 의해 지정된 속성이 다음에 해당하는 경우 true입니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) `path`에서 지정한 속성이 `String`인 경우 `operand`는 `String`이어야 합니다. `path`에서 지정한 속성이 `Set`인 경우 `operand`는 집합의 요소 형식이어야 합니다. 경로와 피연산자는 서로 달라야 합니다. 즉, `contains (a, a)`는 오류를 반환합니다. 예: `Brand` 속성에 하위 문자열 `Company`가 포함되어 있는지 확인합니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) 수식 속성 값인 `:v_sub`가 `Company`의 자리 표시자입니다. 예: 빨간색 제품 색상을 선택할 수 있는지 확인합니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) 수식 속성 값인 `:v_sub`가 `Red`의 자리 표시자입니다. | 
|  `size (path)`  | 속성의 크기를 나타내는 숫자가 반환됩니다. `size`를 사용하는 데 유효한 데이터 유형은 다음과 같습니다.  속성이 `String` 유형인 경우, `size`에서는 문자열의 길이가 반환됩니다. 예: `Brand` 문자열이 20자 이하인지 확인합니다. 수식 속성 값인 `:v_sub`가 `20`의 자리 표시자입니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  속성이 `Binary` 유형인 경우, `size`에서는 속성 값의 바이트 수가 반환됩니다. 예: `ProductCatalog` 항목에 사용 중인 제품의 동영상 클립이 포함된 `VideoClip`이라는 바이너리 속성이 있는 경우를 가정할 수 있습니다. 다음 수식은 `VideoClip`이 64,000 바이트를 초과하는지 확인합니다. 수식 속성 값인 `:v_sub`가 `64000`의 자리 표시자입니다.[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  속성이 `Set` 데이터 유형인 경우, `size`에서는 집합에 포함된 요소의 개수가 반환됩니다. 예: 한 가지 이상의 제품 색상을 선택할 수 있는지 확인합니다. 수식 속성 값인 `:v_sub`가 `1`의 자리 표시자입니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  속성이 `List` 또는 `Map` 유형인 경우, `size`에서는 집합에 포함된 요소의 개수가 반환됩니다. 예: `OneStar` 리뷰의 개수가 특정 임계 값을 초과하는지 확인합니다. 수식 속성 값인 `:v_sub`가 `3`의 자리 표시자입니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  | 

## 논리 평가
<a name="Expressions.OperatorsAndFunctions.LogicalEvaluations"></a>

`AND`, `OR` 및 `NOT` 키워드를 사용하여 논리 평가를 수행합니다. 아래의 목록에서 *a*와 *b*는 평가될 조건을 나타냅니다.
+ `a AND b` - *a*와 *b*가 모두 true인 경우 true입니다.
+ `a OR b` - *a* 또는 *b*(또는 둘 모두)가 true인 경우 true입니다.
+ `NOT a` - *a*가 false인 경우 true입니다. *a*가 true인 경우 false입니다.

다음은 연산에서 AND를 사용한 코드 예제입니다.

`dynamodb-local (*)> select * from exprtest where a > 3 and a < 5;`

## 괄호
<a name="Expressions.OperatorsAndFunctions.Parentheses"></a>

논리 평가의 우선 순위를 변경하려면 괄호를 사용합니다. 예를 들어 *a* 및 *b* 조건이 true이고 *c* 조건이 false인 경우를 가정할 수 있습니다. 다음 수식들은 true로 평가됩니다.
+ `a OR b AND c`

그러나 조건을 괄호로 묶으면 해당 조건을 먼저 평가하게 됩니다. 예를 들어 아래의 경우에는 false로 평가됩니다.
+  `(a OR b) AND c`

**참고**  
수식에 괄호를 포함시킬 수 있습니다. 가장 안쪽에 위치한 괄호부터 먼저 평가됩니다.

다음은 논리 평가에 괄호가 포함된 코드 예제입니다.

`dynamodb-local (*)> select * from exprtest where attribute_type(b, string) or ( a = 5 and c = “coffee”);`

## 조건식의 우선 순위
<a name="Expressions.OperatorsAndFunctions.Precedence"></a>

 DynamoDB는 다음의 우선 순위 규칙을 사용하여 왼쪽에서 오른쪽 방향으로 조건식을 평가합니다.
+ `= <> < <= > >=`
+ `IN`
+ `BETWEEN`
+ `attribute_exists attribute_not_exists begins_with contains`
+ 괄호
+ `NOT`
+ `AND`
+ `OR`